blob: 648d938dc25d787632029d0abc7ad2ec7397dd9a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
383#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
384static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
385#endif
386static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
387static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
388static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
390static void list_glob_vars __ARGS((int *first));
391static void list_buf_vars __ARGS((int *first));
392static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_vim_vars __ARGS((int *first));
397static void list_script_vars __ARGS((int *first));
398static void list_func_vars __ARGS((int *first));
399static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
401static int check_changedtick __ARGS((char_u *arg));
402static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
403static void clear_lval __ARGS((lval_T *lp));
404static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
405static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
406static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
407static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static listitem_T *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem_T *item));
432static void listitem_remove __ARGS((list_T *l, listitem_T *item));
433static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100434static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
435static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
436static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000441static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
443static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
444static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000445static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *list2string __ARGS((typval_T *tv, int copyID));
448static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000449static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000450static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
451static void set_ref_in_list __ARGS((list_T *l, int copyID));
452static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200453static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000455static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
457static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000458static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
463static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static int string2float __ARGS((char_u *text, float_T *value));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
469static int find_internal_func __ARGS((char_u *name));
470static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
471static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200472static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000473static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000474static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
503#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000504static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000507static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000510static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#ifdef FEAT_FLOAT
517static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
523static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000590static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000604static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200623static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638#ifdef vim_mkdir
639static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100642#ifdef FEAT_MZSCHEME
643static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200792static int var_check_func_name __ARGS((char_u *name, int new_var));
793static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
882 * Sort the function table, to enable binary sort.
883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200914 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 /* script-local variables */
923 for (i = 1; i <= ga_scripts.ga_len; ++i)
924 {
925 vars_clear(&SCRIPT_VARS(i));
926 vim_free(SCRIPT_SV(i));
927 }
928 ga_clear(&ga_scripts);
929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000930 /* unreferenced lists and dicts */
931 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932
933 /* functions */
934 free_all_functions();
935 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936}
937#endif
938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Return the name of the executed function.
941 */
942 char_u *
943func_name(cookie)
944 void *cookie;
945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the next breakpoint line for a funccall cookie.
951 */
952 linenr_T *
953func_breakpoint(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the debug tick for a funccall cookie.
961 */
962 int *
963func_dbg_tick(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the nesting level for a funccall cookie.
971 */
972 int
973func_level(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000980funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000982/* pointer to list of previously used funccal, still around because some
983 * item in it is still being used. */
984funccall_T *previous_funccal = NULL;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Return TRUE when a function was ended by a ":return" command.
988 */
989 int
990current_func_returned()
991{
992 return current_funccal->returned;
993}
994
995
996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Set an internal variable to a string value. Creates the variable if it does
998 * not already exist.
999 */
1000 void
1001set_internal_string_var(name, value)
1002 char_u *name;
1003 char_u *value;
1004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 val = vim_strsave(value);
1009 if (val != NULL)
1010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018}
1019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001021static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022static char_u *redir_endp = NULL;
1023static char_u *redir_varname = NULL;
1024
1025/*
1026 * Start recording command output to a variable
1027 * Returns OK if successfully completed the setup. FAIL otherwise.
1028 */
1029 int
1030var_redir_start(name, append)
1031 char_u *name;
1032 int append; /* append to an existing variable */
1033{
1034 int save_emsg;
1035 int err;
1036 typval_T tv;
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001039 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 {
1041 EMSG(_(e_invarg));
1042 return FAIL;
1043 }
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 redir_varname = vim_strsave(name);
1047 if (redir_varname == NULL)
1048 return FAIL;
1049
1050 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1051 if (redir_lval == NULL)
1052 {
1053 var_redir_stop();
1054 return FAIL;
1055 }
1056
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 /* The output is stored in growarray "redir_ga" until redirection ends. */
1058 ga_init2(&redir_ga, (int)sizeof(char), 500);
1059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1062 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1064 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001065 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp != NULL && *redir_endp != NUL)
1067 /* Trailing characters are present after the variable name */
1068 EMSG(_(e_trailing));
1069 else
1070 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 var_redir_stop();
1073 return FAIL;
1074 }
1075
1076 /* check if we can write to the variable: set it to or append an empty
1077 * string */
1078 save_emsg = did_emsg;
1079 did_emsg = FALSE;
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = (char_u *)"";
1082 if (append)
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1084 else
1085 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001086 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001088 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 if (err)
1090 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 var_redir_stop();
1093 return FAIL;
1094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 * Append "value[value_len]" to the variable set by var_redir_start().
1101 * The actual appending is postponed until redirection ends, because the value
1102 * appended may in fact be the string we write to, changing it may cause freed
1103 * memory to be used:
1104 * :redir => foo
1105 * :let foo
1106 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 */
1108 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 if (redir_lval == NULL)
1116 return;
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 {
1125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 }
1128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130}
1131
1132/*
1133 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
1137var_redir_stop()
1138{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 typval_T tv;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_lval != NULL)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* If there was no error: assign the text to the variable. */
1144 if (redir_endp != NULL)
1145 {
1146 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 /* Call get_lval() again, if it's inside a Dict or List it may
1150 * have changed. */
1151 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1152 FALSE, FALSE, FALSE, FNE_CHECK_START);
1153 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1154 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1155 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* free the collected output */
1159 vim_free(redir_ga.ga_data);
1160 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 vim_free(redir_lval);
1163 redir_lval = NULL;
1164 }
1165 vim_free(redir_varname);
1166 redir_varname = NULL;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# if defined(FEAT_MBYTE) || defined(PROTO)
1170 int
1171eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1172 char_u *enc_from;
1173 char_u *enc_to;
1174 char_u *fname_from;
1175 char_u *fname_to;
1176{
1177 int err = FALSE;
1178
1179 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1180 set_vim_var_string(VV_CC_TO, enc_to, -1);
1181 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1182 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1183 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1184 err = TRUE;
1185 set_vim_var_string(VV_CC_FROM, NULL, -1);
1186 set_vim_var_string(VV_CC_TO, NULL, -1);
1187 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1188 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1189
1190 if (err)
1191 return FAIL;
1192 return OK;
1193}
1194# endif
1195
1196# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1197 int
1198eval_printexpr(fname, args)
1199 char_u *fname;
1200 char_u *args;
1201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_FNAME_IN, fname, -1);
1205 set_vim_var_string(VV_CMDARG, args, -1);
1206 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1207 err = TRUE;
1208 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1209 set_vim_var_string(VV_CMDARG, NULL, -1);
1210
1211 if (err)
1212 {
1213 mch_remove(fname);
1214 return FAIL;
1215 }
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_DIFF) || defined(PROTO)
1221 void
1222eval_diff(origfile, newfile, outfile)
1223 char_u *origfile;
1224 char_u *newfile;
1225 char_u *outfile;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1230 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1231 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1232 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1235 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1236}
1237
1238 void
1239eval_patch(origfile, difffile, outfile)
1240 char_u *origfile;
1241 char_u *difffile;
1242 char_u *outfile;
1243{
1244 int err;
1245
1246 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1248 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1249 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1252 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1253}
1254# endif
1255
1256/*
1257 * Top level evaluation function, returning a boolean.
1258 * Sets "error" to TRUE if there was an error.
1259 * Return TRUE or FALSE.
1260 */
1261 int
1262eval_to_bool(arg, error, nextcmd, skip)
1263 char_u *arg;
1264 int *error;
1265 char_u **nextcmd;
1266 int skip; /* only parse, don't execute */
1267{
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 int retval = FALSE;
1270
1271 if (skip)
1272 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
1276 {
1277 *error = FALSE;
1278 if (!skip)
1279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
1291 * Top level evaluation function, returning a string. If "skip" is TRUE,
1292 * only parsing to "nextcmd" is done, without reporting errors. Return
1293 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1294 */
1295 char_u *
1296eval_to_string_skip(arg, nextcmd, skip)
1297 char_u *arg;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *retval;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = NULL;
1308 else
1309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 retval = vim_strsave(get_tv_string(&tv));
1311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320 * Skip over an expression at "*pp".
1321 * Return FAIL for an error, OK otherwise.
1322 */
1323 int
1324skip_expr(pp)
1325 char_u **pp;
1326{
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328
1329 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331}
1332
1333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 * When "convert" is TRUE convert a List into a sequence of lines and convert
1336 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Return pointer to allocated memory, or NULL for failure.
1338 */
1339 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *arg;
1342 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 {
1358 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001359 if (tv.vval.v_list != NULL)
1360 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar4f688582007-07-24 12:34:30 +00001554#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1555 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001557 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558 * Uses argv[argc] for the function arguments. Only Number and String
1559 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 static int
1563call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 char_u *func;
1565 int argc;
1566 char_u **argv;
1567 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 long n;
1572 int len;
1573 int i;
1574 int doesrange;
1575 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001578 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 for (i = 0; i < argc; i++)
1583 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001584 /* Pass a NULL or empty argument as an empty string */
1585 if (argv[i] == NULL || *argv[i] == NUL)
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 continue;
1590 }
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* Recognize a number argument, the others must be strings. */
1593 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1594 if (len != 0 && len == (int)STRLEN(argv[i]))
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_NUMBER;
1597 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_STRING;
1602 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 }
1605
1606 if (safe)
1607 {
1608 save_funccalp = save_funccal();
1609 ++sandbox;
1610 }
1611
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1613 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (safe)
1617 {
1618 --sandbox;
1619 restore_funccal(save_funccalp);
1620 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 vim_free(argvars);
1622
1623 if (ret == FAIL)
1624 clear_tv(rettv);
1625
1626 return ret;
1627}
1628
Bram Moolenaar4f688582007-07-24 12:34:30 +00001629# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001631 * Call vimL function "func" and return the result as a string.
1632 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 * Uses argv[argc] for the function arguments.
1634 */
1635 void *
1636call_func_retstr(func, argc, argv, safe)
1637 char_u *func;
1638 int argc;
1639 char_u **argv;
1640 int safe; /* use the sandbox */
1641{
1642 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001643 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644
1645 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1646 return NULL;
1647
1648 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 return retval;
1651}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001652# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653
Bram Moolenaar4f688582007-07-24 12:34:30 +00001654# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001656 * Call vimL function "func" and return the result as a number.
1657 * Returns -1 when calling the function fails.
1658 * Uses argv[argc] for the function arguments.
1659 */
1660 long
1661call_func_retnr(func, argc, argv, safe)
1662 char_u *func;
1663 int argc;
1664 char_u **argv;
1665 int safe; /* use the sandbox */
1666{
1667 typval_T rettv;
1668 long retval;
1669
1670 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1671 return -1;
1672
1673 retval = get_tv_number_chk(&rettv, NULL);
1674 clear_tv(&rettv);
1675 return retval;
1676}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001678
1679/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 */
1684 void *
1685call_func_retlist(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
1692
1693 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1694 return NULL;
1695
1696 if (rettv.v_type != VAR_LIST)
1697 {
1698 clear_tv(&rettv);
1699 return NULL;
1700 }
1701
1702 return rettv.vval.v_list;
1703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705
Bram Moolenaar4f688582007-07-24 12:34:30 +00001706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
1708 * Save the current function call pointer, and set it to NULL.
1709 * Used when executing autocommands and for ":source".
1710 */
1711 void *
1712save_funccal()
1713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 current_funccal = NULL;
1717 return (void *)fc;
1718}
1719
1720 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721restore_funccal(vfc)
1722 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = (funccall_T *)vfc;
1725
1726 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729#if defined(FEAT_PROFILE) || defined(PROTO)
1730/*
1731 * Prepare profiling for entering a child or something else that is not
1732 * counted for the script/function itself.
1733 * Should always be called in pair with prof_child_exit().
1734 */
1735 void
1736prof_child_enter(tm)
1737 proftime_T *tm; /* place to store waittime */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 profile_start(&fc->prof_child);
1743 script_prof_save(tm);
1744}
1745
1746/*
1747 * Take care of time spent in a child.
1748 * Should always be called after prof_child_enter().
1749 */
1750 void
1751prof_child_exit(tm)
1752 proftime_T *tm; /* where waittime was stored */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 {
1758 profile_end(&fc->prof_child);
1759 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1760 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1761 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1762 }
1763 script_prof_restore(tm);
1764}
1765#endif
1766
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_FOLDING
1769/*
1770 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1771 * it in "*cp". Doesn't give error messages.
1772 */
1773 int
1774eval_foldexpr(arg, cp)
1775 char_u *arg;
1776 int *cp;
1777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int retval;
1780 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001781 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1782 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001785 if (use_sandbox)
1786 ++sandbox;
1787 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (tv.v_type == VAR_NUMBER)
1795 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001796 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 retval = 0;
1798 else
1799 {
1800 /* If the result is a string, check if there is a non-digit before
1801 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!VIM_ISDIGIT(*s) && *s != '-')
1804 *cp = *s++;
1805 retval = atol((char *)s);
1806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 --sandbox;
1812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 return retval;
1815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let" list all variable values
1820 * ":let var1 var2" list variable values
1821 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 * ":let var += expr" assignment command.
1823 * ":let var -= expr" assignment command.
1824 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 void
1828ex_let(eap)
1829 exarg_T *eap;
1830{
1831 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 int var_count = 0;
1836 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 argend = skip_var_list(arg, &var_count, &semicolon);
1842 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001844 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1845 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001849 /*
1850 * ":let" without "=": list variables
1851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (*arg == '[')
1853 EMSG(_(e_invarg));
1854 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_glob_vars(&first);
1861 list_buf_vars(&first);
1862 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_script_vars(&first);
1867 list_func_vars(&first);
1868 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 eap->nextcmd = check_nextcmd(arg);
1871 }
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 op[0] = '=';
1875 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 {
1878 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1879 op[0] = expr[-1]; /* +=, -= or .= */
1880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (eap->skip)
1884 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 {
1888 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 --emsg_skip;
1891 }
1892 else if (i != FAIL)
1893 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899}
1900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901/*
1902 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1903 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 * When "nextchars" is not NULL it points to a string with characters that
1905 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1906 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 * Returns OK or FAIL;
1908 */
1909 static int
1910ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1911 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int copy; /* copy values from "tv", don't move */
1914 int semicolon; /* from skip_var_list() */
1915 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917{
1918 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 listitem_T *item;
1922 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923
1924 if (*arg != '[')
1925 {
1926 /*
1927 * ":let var = expr" or ":for var in list"
1928 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return FAIL;
1931 return OK;
1932 }
1933
1934 /*
1935 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1936 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 {
1939 EMSG(_(e_listreq));
1940 return FAIL;
1941 }
1942
1943 i = list_len(l);
1944 if (semicolon == 0 && var_count < i)
1945 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001946 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 }
1949 if (var_count - semicolon > i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954
1955 item = l->lv_first;
1956 while (*arg != ']')
1957 {
1958 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 item = item->li_next;
1961 if (arg == NULL)
1962 return FAIL;
1963
1964 arg = skipwhite(arg);
1965 if (*arg == ';')
1966 {
1967 /* Put the rest of the list (may be empty) in the var after ';'.
1968 * Create a new list for this. */
1969 l = list_alloc();
1970 if (l == NULL)
1971 return FAIL;
1972 while (item != NULL)
1973 {
1974 list_append_tv(l, &item->li_tv);
1975 item = item->li_next;
1976 }
1977
1978 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001979 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 ltv.vval.v_list = l;
1981 l->lv_refcount = 1;
1982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1984 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 clear_tv(&ltv);
1986 if (arg == NULL)
1987 return FAIL;
1988 break;
1989 }
1990 else if (*arg != ',' && *arg != ']')
1991 {
1992 EMSG2(_(e_intern2), "ex_let_vars()");
1993 return FAIL;
1994 }
1995 }
1996
1997 return OK;
1998}
1999
2000/*
2001 * Skip over assignable variable "var" or list of variables "[var, var]".
2002 * Used for ":let varvar = expr" and ":for varvar in expr".
2003 * For "[var, var]" increment "*var_count" for each variable.
2004 * for "[var, var; var]" set "semicolon".
2005 * Return NULL for an error.
2006 */
2007 static char_u *
2008skip_var_list(arg, var_count, semicolon)
2009 char_u *arg;
2010 int *var_count;
2011 int *semicolon;
2012{
2013 char_u *p, *s;
2014
2015 if (*arg == '[')
2016 {
2017 /* "[var, var]": find the matching ']'. */
2018 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002019 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2022 s = skip_var_one(p);
2023 if (s == p)
2024 {
2025 EMSG2(_(e_invarg2), p);
2026 return NULL;
2027 }
2028 ++*var_count;
2029
2030 p = skipwhite(s);
2031 if (*p == ']')
2032 break;
2033 else if (*p == ';')
2034 {
2035 if (*semicolon == 1)
2036 {
2037 EMSG(_("Double ; in list of variables"));
2038 return NULL;
2039 }
2040 *semicolon = 1;
2041 }
2042 else if (*p != ',')
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 }
2048 return p + 1;
2049 }
2050 else
2051 return skip_var_one(arg);
2052}
2053
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002055 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 static char_u *
2059skip_var_one(arg)
2060 char_u *arg;
2061{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 if (*arg == '@' && arg[1] != NUL)
2063 return arg + 2;
2064 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2065 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066}
2067
Bram Moolenaara7043832005-01-21 11:56:39 +00002068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 * List variables for hashtab "ht" with prefix "prefix".
2070 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078{
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashitem_T *hi;
2080 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 int todo;
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2085 {
2086 if (!HASHITEM_EMPTY(hi))
2087 {
2088 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 di = HI2DI(hi);
2090 if (empty || di->di_tv.v_type != VAR_STRING
2091 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 }
2094 }
2095}
2096
2097/*
2098 * List global variables.
2099 */
2100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_glob_vars(first)
2102 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105}
2106
2107/*
2108 * List buffer variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_buf_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 char_u numbuf[NUMBUFLEN];
2115
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2117 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2121 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List window variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_win_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2132 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135#ifdef FEAT_WINDOWS
2136/*
2137 * List tab page variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_tab_vars(first)
2141 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2144 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145}
2146#endif
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
2149 * List Vim variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_vim_vars(first)
2153 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156}
2157
2158/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002159 * List script-local variables, if there is a script.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_script_vars(first)
2163 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164{
2165 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2167 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168}
2169
2170/*
2171 * List function variables, if there is a function.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_func_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_funccal != NULL)
2178 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 * List variables in "arg".
2184 */
2185 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 exarg_T *eap;
2188 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
2191 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 char_u *name_start;
2195 char_u *arg_subsc;
2196 char_u *tofree;
2197 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198
2199 while (!ends_excmd(*arg) && !got_int)
2200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2205 {
2206 emsg_severe = TRUE;
2207 EMSG(_(e_trailing));
2208 break;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 /* get_name_len() takes care of expanding curly braces */
2214 name_start = name = arg;
2215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2216 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* This is mainly to keep test 49 working: when expanding
2219 * curly braces fails overrule the exception error message. */
2220 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 emsg_severe = TRUE;
2223 EMSG2(_(e_invarg2), arg);
2224 break;
2225 }
2226 error = TRUE;
2227 }
2228 else
2229 {
2230 if (tofree != NULL)
2231 name = tofree;
2232 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
2235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* handle d.key, l[idx], f(expr) */
2237 arg_subsc = arg;
2238 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'g': list_glob_vars(first); break;
2247 case 'b': list_buf_vars(first); break;
2248 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'v': list_vim_vars(first); break;
2253 case 's': list_script_vars(first); break;
2254 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 default:
2256 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 }
2259 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 {
2261 char_u numbuf[NUMBUFLEN];
2262 char_u *tf;
2263 int c;
2264 char_u *s;
2265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002266 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 c = *arg;
2268 *arg = NUL;
2269 list_one_var_a((char_u *)"",
2270 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 tv.v_type,
2272 s == NULL ? (char_u *)"" : s,
2273 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 *arg = c;
2275 vim_free(tf);
2276 }
2277 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281
2282 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287
2288 return arg;
2289}
2290
2291/*
2292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2293 * Returns a pointer to the char just after the var name.
2294 * Returns NULL if there is an error.
2295 */
2296 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303{
2304 int c1;
2305 char_u *name;
2306 char_u *p;
2307 char_u *arg_end = NULL;
2308 int len;
2309 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311
2312 /*
2313 * ":let $VAR = expr": Set environment variable.
2314 */
2315 if (*arg == '$')
2316 {
2317 /* Find the end of the name. */
2318 ++arg;
2319 name = arg;
2320 len = get_env_len(&arg);
2321 if (len == 0)
2322 EMSG2(_(e_invarg2), name - 1);
2323 else
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && (*op == '+' || *op == '-'))
2326 EMSG2(_(e_letwrong), op);
2327 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002330 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
2332 c1 = name[len];
2333 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 p = get_tv_string_chk(tv);
2335 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 int mustfree = FALSE;
2338 char_u *s = vim_getenv(name, &mustfree);
2339
2340 if (s != NULL)
2341 {
2342 p = tofree = concat_str(s, p);
2343 if (mustfree)
2344 vim_free(s);
2345 }
2346 }
2347 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (STRICMP(name, "HOME") == 0)
2351 init_homedir();
2352 else if (didset_vim && STRICMP(name, "VIM") == 0)
2353 didset_vim = FALSE;
2354 else if (didset_vimruntime
2355 && STRICMP(name, "VIMRUNTIME") == 0)
2356 didset_vimruntime = FALSE;
2357 arg_end = arg;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363 }
2364
2365 /*
2366 * ":let &option = expr": Set option value.
2367 * ":let &l:option = expr": Set local option value.
2368 * ":let &g:option = expr": Set global option value.
2369 */
2370 else if (*arg == '&')
2371 {
2372 /* Find the end of the name. */
2373 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (p == NULL || (endchars != NULL
2375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 long n;
2380 int opt_type;
2381 long numval;
2382 char_u *stringval = NULL;
2383 char_u *s;
2384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 c1 = *p;
2386 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387
2388 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 s = get_tv_string_chk(tv); /* != NULL if number or string */
2390 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 opt_type = get_option_value(arg, &numval,
2393 &stringval, opt_flags);
2394 if ((opt_type == 1 && *op == '.')
2395 || (opt_type == 0 && *op != '.'))
2396 EMSG2(_(e_letwrong), op);
2397 else
2398 {
2399 if (opt_type == 1) /* number */
2400 {
2401 if (*op == '+')
2402 n = numval + n;
2403 else
2404 n = numval - n;
2405 }
2406 else if (opt_type == 0 && stringval != NULL) /* string */
2407 {
2408 s = concat_str(stringval, s);
2409 vim_free(stringval);
2410 stringval = s;
2411 }
2412 }
2413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (s != NULL)
2415 {
2416 set_option_value(arg, n, s, opt_flags);
2417 arg_end = p;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423
2424 /*
2425 * ":let @r = expr": Set register contents.
2426 */
2427 else if (*arg == '@')
2428 {
2429 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (op != NULL && (*op == '+' || *op == '-'))
2431 EMSG2(_(e_letwrong), op);
2432 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002437 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 char_u *s;
2439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002443 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (s != NULL)
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 arg_end = arg + 1;
2454 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458
2459 /*
2460 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002465 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
2481 else
2482 EMSG2(_(e_invarg2), arg);
2483
2484 return arg_end;
2485}
2486
2487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2489 */
2490 static int
2491check_changedtick(arg)
2492 char_u *arg;
2493{
2494 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2495 {
2496 EMSG2(_(e_readonlyvar), arg);
2497 return TRUE;
2498 }
2499 return FALSE;
2500}
2501
2502/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Get an lval: variable, Dict item or List item that can be assigned a value
2504 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2505 * "name.key", "name.key[expr]" etc.
2506 * Indexing only works if "name" is an existing List or Dictionary.
2507 * "name" points to the start of the name.
2508 * If "rettv" is not NULL it points to the value to be assigned.
2509 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2510 * wrong; must end in space or cmd separator.
2511 *
2512 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002513 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
2516 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 typval_T *rettv;
2520 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int unlet;
2522 int skip;
2523 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 char_u *expr_start, *expr_end;
2528 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 dictitem_T *v;
2530 typval_T var1;
2531 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540
2541 if (skip)
2542 {
2543 /* When skipping just find the end of the name. */
2544 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 }
2547
2548 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (expr_start != NULL)
2551 {
2552 /* Don't expand the name when we already know there is an error. */
2553 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2554 && *p != '[' && *p != '.')
2555 {
2556 EMSG(_(e_trailing));
2557 return NULL;
2558 }
2559
2560 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2561 if (lp->ll_exp_name == NULL)
2562 {
2563 /* Report an invalid expression in braces, unless the
2564 * expression evaluation has been cancelled due to an
2565 * aborting error, an interrupt, or an exception. */
2566 if (!aborting() && !quiet)
2567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 EMSG2(_(e_invarg2), name);
2570 return NULL;
2571 }
2572 }
2573 lp->ll_name = lp->ll_exp_name;
2574 }
2575 else
2576 lp->ll_name = name;
2577
2578 /* Without [idx] or .key we are done. */
2579 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2580 return p;
2581
2582 cc = *p;
2583 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (v == NULL && !quiet)
2586 EMSG2(_(e_undefvar), lp->ll_name);
2587 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 if (v == NULL)
2589 return NULL;
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /*
2592 * Loop until no more [idx] or .key is following.
2593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2598 && !(lp->ll_tv->v_type == VAR_DICT
2599 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E689: Can only index a List or Dictionary"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E708: [:] must come last"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 len = -1;
2613 if (*p == '.')
2614 {
2615 key = p + 1;
2616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2617 ;
2618 if (len == 0)
2619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
2621 EMSG(_(e_emptykey));
2622 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624 p = key + len;
2625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 if (*p == ':')
2631 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 empty1 = FALSE;
2635 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (get_tv_string_chk(&var1) == NULL)
2638 {
2639 /* not a number or string */
2640 clear_tv(&var1);
2641 return NULL;
2642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 }
2644
2645 /* Optionally get the second index [ :expr]. */
2646 if (*p == ':')
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 if (!empty1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (rettv != NULL && (rettv->v_type != VAR_LIST
2657 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = skipwhite(p + 1);
2666 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 else
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var2) == NULL)
2678 {
2679 /* not a number or string */
2680 if (!empty1)
2681 clear_tv(&var1);
2682 clear_tv(&var2);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!quiet)
2694 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Skip to past ']'. */
2703 ++p;
2704 }
2705
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
2708 if (len == -1)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*key == NUL)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 lp->ll_list = NULL;
2721 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002722 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002723
2724 /* When assigning to g: check that a function and variable name is
2725 * valid. */
2726 if (rettv != NULL && lp->ll_dict == &globvardict)
2727 {
2728 if (rettv->v_type == VAR_FUNC
2729 && var_check_func_name(key, lp->ll_di == NULL))
2730 return NULL;
2731 if (!valid_varname(key))
2732 return NULL;
2733 }
2734
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002737 /* Can't add "v:" variable. */
2738 if (lp->ll_dict == &vimvardict)
2739 {
2740 EMSG2(_(e_illvar), name);
2741 return NULL;
2742 }
2743
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (len == -1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (len == -1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 p = NULL;
2761 break;
2762 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 /* existing variable, need to check if it can be changed */
2764 else if (var_check_ro(lp->ll_di->di_flags, name))
2765 return NULL;
2766
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (len == -1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 else
2772 {
2773 /*
2774 * Get the number and item for the only or first index of the List.
2775 */
2776 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 else
2779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var1);
2782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_list = lp->ll_tv->vval.v_list;
2785 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2786 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002788 if (lp->ll_n1 < 0)
2789 {
2790 lp->ll_n1 = 0;
2791 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2792 }
2793 }
2794 if (lp->ll_li == NULL)
2795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002798 if (!quiet)
2799 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802
2803 /*
2804 * May need to find the item or absolute index for the second
2805 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 * When no index given: "lp->ll_empty2" is TRUE.
2807 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 {
2818 if (!quiet)
2819 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2826 if (lp->ll_n1 < 0)
2827 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2828 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 {
2830 if (!quiet)
2831 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002834 }
2835
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return p;
2841}
2842
2843/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 */
2846 static void
2847clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849{
2850 vim_free(lp->ll_exp_name);
2851 vim_free(lp->ll_newkey);
2852}
2853
2854/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002855 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 */
2859 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002860set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866{
2867 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 listitem_T *ri;
2869 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870
2871 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 cc = *endp;
2876 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 if (op != NULL && *op != '=')
2878 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002882 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002883 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 {
2885 if (tv_op(&tv, rettv, op) == OK)
2886 set_var(lp->ll_name, &tv, FALSE);
2887 clear_tv(&tv);
2888 }
2889 }
2890 else
2891 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 else if (tv_check_lock(lp->ll_newkey == NULL
2896 ? lp->ll_tv->v_lock
2897 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2898 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 else if (lp->ll_range)
2900 {
2901 /*
2902 * Assign the List values to the list items.
2903 */
2904 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 if (op != NULL && *op != '=')
2907 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2908 else
2909 {
2910 clear_tv(&lp->ll_li->li_tv);
2911 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 ri = ri->li_next;
2914 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2915 break;
2916 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002919 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ri = NULL;
2922 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 lp->ll_li = lp->ll_li->li_next;
2926 ++lp->ll_n1;
2927 }
2928 if (ri != NULL)
2929 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 else if (lp->ll_empty2
2931 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 : lp->ll_n1 != lp->ll_n2)
2933 EMSG(_("E711: List value has not enough items"));
2934 }
2935 else
2936 {
2937 /*
2938 * Assign to a List or Dictionary item.
2939 */
2940 if (lp->ll_newkey != NULL)
2941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 {
2944 EMSG2(_(e_letwrong), op);
2945 return;
2946 }
2947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002949 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 if (di == NULL)
2951 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002952 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2953 {
2954 vim_free(di);
2955 return;
2956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 else if (op != NULL && *op != '=')
2960 {
2961 tv_op(lp->ll_tv, rettv, op);
2962 return;
2963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /*
2968 * Assign the value to the variable or list item.
2969 */
2970 if (copy)
2971 copy_tv(rettv, lp->ll_tv);
2972 else
2973 {
2974 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002975 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 }
2978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979}
2980
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2983 * Returns OK or FAIL.
2984 */
2985 static int
2986tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 typval_T *tv1;
2988 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 char_u *op;
2990{
2991 long n;
2992 char_u numbuf[NUMBUFLEN];
2993 char_u *s;
2994
2995 /* Can't do anything with a Funcref or a Dict on the right. */
2996 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2997 {
2998 switch (tv1->v_type)
2999 {
3000 case VAR_DICT:
3001 case VAR_FUNC:
3002 break;
3003
3004 case VAR_LIST:
3005 if (*op != '+' || tv2->v_type != VAR_LIST)
3006 break;
3007 /* List += List */
3008 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3009 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3010 return OK;
3011
3012 case VAR_NUMBER:
3013 case VAR_STRING:
3014 if (tv2->v_type == VAR_LIST)
3015 break;
3016 if (*op == '+' || *op == '-')
3017 {
3018 /* nr += nr or nr -= nr*/
3019 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003020#ifdef FEAT_FLOAT
3021 if (tv2->v_type == VAR_FLOAT)
3022 {
3023 float_T f = n;
3024
3025 if (*op == '+')
3026 f += tv2->vval.v_float;
3027 else
3028 f -= tv2->vval.v_float;
3029 clear_tv(tv1);
3030 tv1->v_type = VAR_FLOAT;
3031 tv1->vval.v_float = f;
3032 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#endif
3035 {
3036 if (*op == '+')
3037 n += get_tv_number(tv2);
3038 else
3039 n -= get_tv_number(tv2);
3040 clear_tv(tv1);
3041 tv1->v_type = VAR_NUMBER;
3042 tv1->vval.v_number = n;
3043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 }
3045 else
3046 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 if (tv2->v_type == VAR_FLOAT)
3048 break;
3049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 /* str .= str */
3051 s = get_tv_string(tv1);
3052 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3053 clear_tv(tv1);
3054 tv1->v_type = VAR_STRING;
3055 tv1->vval.v_string = s;
3056 }
3057 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058
3059#ifdef FEAT_FLOAT
3060 case VAR_FLOAT:
3061 {
3062 float_T f;
3063
3064 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3065 && tv2->v_type != VAR_NUMBER
3066 && tv2->v_type != VAR_STRING))
3067 break;
3068 if (tv2->v_type == VAR_FLOAT)
3069 f = tv2->vval.v_float;
3070 else
3071 f = get_tv_number(tv2);
3072 if (*op == '+')
3073 tv1->vval.v_float += f;
3074 else
3075 tv1->vval.v_float -= f;
3076 }
3077 return OK;
3078#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 }
3080 }
3081
3082 EMSG2(_(e_letwrong), op);
3083 return FAIL;
3084}
3085
3086/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087 * Add a watcher to a list.
3088 */
3089 static void
3090list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 list_T *l;
3092 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003093{
3094 lw->lw_next = l->lv_watch;
3095 l->lv_watch = lw;
3096}
3097
3098/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003099 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003100 * No warning when it isn't found...
3101 */
3102 static void
3103list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003104 list_T *l;
3105 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106{
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108
3109 lwp = &l->lv_watch;
3110 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3111 {
3112 if (lw == lwrem)
3113 {
3114 *lwp = lw->lw_next;
3115 break;
3116 }
3117 lwp = &lw->lw_next;
3118 }
3119}
3120
3121/*
3122 * Just before removing an item from a list: advance watchers to the next
3123 * item.
3124 */
3125 static void
3126list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 list_T *l;
3128 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129{
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131
3132 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3133 if (lw->lw_item == item)
3134 lw->lw_item = item->li_next;
3135}
3136
3137/*
3138 * Evaluate the expression used in a ":for var in expr" command.
3139 * "arg" points to "var".
3140 * Set "*errp" to TRUE for an error, FALSE otherwise;
3141 * Return a pointer that holds the info. Null when there is an error.
3142 */
3143 void *
3144eval_for_line(arg, errp, nextcmdp, skip)
3145 char_u *arg;
3146 int *errp;
3147 char_u **nextcmdp;
3148 int skip;
3149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 typval_T tv;
3153 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154
3155 *errp = TRUE; /* default: there is an error */
3156
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158 if (fi == NULL)
3159 return NULL;
3160
3161 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3162 if (expr == NULL)
3163 return fi;
3164
3165 expr = skipwhite(expr);
3166 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3167 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003168 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 return fi;
3170 }
3171
3172 if (skip)
3173 ++emsg_skip;
3174 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3175 {
3176 *errp = FALSE;
3177 if (!skip)
3178 {
3179 l = tv.vval.v_list;
3180 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003181 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003183 clear_tv(&tv);
3184 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 else
3186 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003187 /* No need to increment the refcount, it's already set for the
3188 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 fi->fi_list = l;
3190 list_add_watch(l, &fi->fi_lw);
3191 fi->fi_lw.lw_item = l->lv_first;
3192 }
3193 }
3194 }
3195 if (skip)
3196 --emsg_skip;
3197
3198 return fi;
3199}
3200
3201/*
3202 * Use the first item in a ":for" list. Advance to the next.
3203 * Assign the values to the variable (list). "arg" points to the first one.
3204 * Return TRUE when a valid item was found, FALSE when at end of list or
3205 * something wrong.
3206 */
3207 int
3208next_for_item(fi_void, arg)
3209 void *fi_void;
3210 char_u *arg;
3211{
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215
3216 item = fi->fi_lw.lw_item;
3217 if (item == NULL)
3218 result = FALSE;
3219 else
3220 {
3221 fi->fi_lw.lw_item = item->li_next;
3222 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3223 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3224 }
3225 return result;
3226}
3227
3228/*
3229 * Free the structure used to store info used by ":for".
3230 */
3231 void
3232free_for_info(fi_void)
3233 void *fi_void;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003237 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003238 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003240 list_unref(fi->fi_list);
3241 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 vim_free(fi);
3243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3246
3247 void
3248set_context_for_expression(xp, arg, cmdidx)
3249 expand_T *xp;
3250 char_u *arg;
3251 cmdidx_T cmdidx;
3252{
3253 int got_eq = FALSE;
3254 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 if (cmdidx == CMD_let)
3258 {
3259 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003260 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 {
3262 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003263 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 {
3265 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 if (vim_iswhite(*p))
3268 break;
3269 }
3270 return;
3271 }
3272 }
3273 else
3274 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3275 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 while ((xp->xp_pattern = vim_strpbrk(arg,
3277 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3278 {
3279 c = *xp->xp_pattern;
3280 if (c == '&')
3281 {
3282 c = xp->xp_pattern[1];
3283 if (c == '&')
3284 {
3285 ++xp->xp_pattern;
3286 xp->xp_context = cmdidx != CMD_let || got_eq
3287 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3288 }
3289 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003292 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3293 xp->xp_pattern += 2;
3294
3295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 else if (c == '$')
3298 {
3299 /* environment variable */
3300 xp->xp_context = EXPAND_ENV_VARS;
3301 }
3302 else if (c == '=')
3303 {
3304 got_eq = TRUE;
3305 xp->xp_context = EXPAND_EXPRESSION;
3306 }
3307 else if (c == '<'
3308 && xp->xp_context == EXPAND_FUNCTIONS
3309 && vim_strchr(xp->xp_pattern, '(') == NULL)
3310 {
3311 /* Function name can start with "<SNR>" */
3312 break;
3313 }
3314 else if (cmdidx != CMD_let || got_eq)
3315 {
3316 if (c == '"') /* string */
3317 {
3318 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3319 if (c == '\\' && xp->xp_pattern[1] != NUL)
3320 ++xp->xp_pattern;
3321 xp->xp_context = EXPAND_NOTHING;
3322 }
3323 else if (c == '\'') /* literal string */
3324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3327 /* skip */ ;
3328 xp->xp_context = EXPAND_NOTHING;
3329 }
3330 else if (c == '|')
3331 {
3332 if (xp->xp_pattern[1] == '|')
3333 {
3334 ++xp->xp_pattern;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else
3338 xp->xp_context = EXPAND_COMMANDS;
3339 }
3340 else
3341 xp->xp_context = EXPAND_EXPRESSION;
3342 }
3343 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 /* Doesn't look like something valid, expand as an expression
3345 * anyway. */
3346 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 arg = xp->xp_pattern;
3348 if (*arg != NUL)
3349 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3350 /* skip */ ;
3351 }
3352 xp->xp_pattern = arg;
3353}
3354
3355#endif /* FEAT_CMDL_COMPL */
3356
3357/*
3358 * ":1,25call func(arg1, arg2)" function call.
3359 */
3360 void
3361ex_call(eap)
3362 exarg_T *eap;
3363{
3364 char_u *arg = eap->arg;
3365 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003367 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003369 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 linenr_T lnum;
3371 int doesrange;
3372 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003375 if (eap->skip)
3376 {
3377 /* trans_function_name() doesn't work well when skipping, use eval0()
3378 * instead to skip to any following command, e.g. for:
3379 * :if 0 | call dict.foo().bar() | endif */
3380 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3381 return;
3382 }
3383
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003384 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003385 if (fudi.fd_newkey != NULL)
3386 {
3387 /* Still need to give an error message for missing key. */
3388 EMSG2(_(e_dictkey), fudi.fd_newkey);
3389 vim_free(fudi.fd_newkey);
3390 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003391 if (tofree == NULL)
3392 return;
3393
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003394 /* Increase refcount on dictionary, it could get deleted when evaluating
3395 * the arguments. */
3396 if (fudi.fd_dict != NULL)
3397 ++fudi.fd_dict->dv_refcount;
3398
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003399 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003400 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003401 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaar532c7802005-01-27 14:44:31 +00003403 /* Skip white space to allow ":call func ()". Not good, but required for
3404 * backward compatibility. */
3405 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 if (*startarg != '(')
3409 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003410 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 goto end;
3412 }
3413
3414 /*
3415 * When skipping, evaluate the function once, to find the end of the
3416 * arguments.
3417 * When the function takes a range, this is discovered after the first
3418 * call, and the loop is broken.
3419 */
3420 if (eap->skip)
3421 {
3422 ++emsg_skip;
3423 lnum = eap->line2; /* do it once, also with an invalid range */
3424 }
3425 else
3426 lnum = eap->line1;
3427 for ( ; lnum <= eap->line2; ++lnum)
3428 {
3429 if (!eap->skip && eap->addr_count > 0)
3430 {
3431 curwin->w_cursor.lnum = lnum;
3432 curwin->w_cursor.col = 0;
3433 }
3434 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003435 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003436 eap->line1, eap->line2, &doesrange,
3437 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 failed = TRUE;
3440 break;
3441 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003442
3443 /* Handle a function returning a Funcref, Dictionary or List. */
3444 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3445 {
3446 failed = TRUE;
3447 break;
3448 }
3449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (doesrange || eap->skip)
3452 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003455 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003457 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (aborting())
3459 break;
3460 }
3461 if (eap->skip)
3462 --emsg_skip;
3463
3464 if (!failed)
3465 {
3466 /* Check for trailing illegal characters and a following command. */
3467 if (!ends_excmd(*arg))
3468 {
3469 emsg_severe = TRUE;
3470 EMSG(_(e_trailing));
3471 }
3472 else
3473 eap->nextcmd = check_nextcmd(arg);
3474 }
3475
3476end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003477 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479}
3480
3481/*
3482 * ":unlet[!] var1 ... " command.
3483 */
3484 void
3485ex_unlet(eap)
3486 exarg_T *eap;
3487{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003488 ex_unletlock(eap, eap->arg, 0);
3489}
3490
3491/*
3492 * ":lockvar" and ":unlockvar" commands
3493 */
3494 void
3495ex_lockvar(eap)
3496 exarg_T *eap;
3497{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003499 int deep = 2;
3500
3501 if (eap->forceit)
3502 deep = -1;
3503 else if (vim_isdigit(*arg))
3504 {
3505 deep = getdigits(&arg);
3506 arg = skipwhite(arg);
3507 }
3508
3509 ex_unletlock(eap, arg, deep);
3510}
3511
3512/*
3513 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3514 */
3515 static void
3516ex_unletlock(eap, argstart, deep)
3517 exarg_T *eap;
3518 char_u *argstart;
3519 int deep;
3520{
3521 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 do
3527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003529 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3530 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003531 if (lv.ll_name == NULL)
3532 error = TRUE; /* error but continue parsing */
3533 if (name_end == NULL || (!vim_iswhite(*name_end)
3534 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 if (name_end != NULL)
3537 {
3538 emsg_severe = TRUE;
3539 EMSG(_(e_trailing));
3540 }
3541 if (!(eap->skip || error))
3542 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 break;
3544 }
3545
3546 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003547 {
3548 if (eap->cmdidx == CMD_unlet)
3549 {
3550 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3551 error = TRUE;
3552 }
3553 else
3554 {
3555 if (do_lock_var(&lv, name_end, deep,
3556 eap->cmdidx == CMD_lockvar) == FAIL)
3557 error = TRUE;
3558 }
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (!eap->skip)
3562 clear_lval(&lv);
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 arg = skipwhite(name_end);
3565 } while (!ends_excmd(*arg));
3566
3567 eap->nextcmd = check_nextcmd(arg);
3568}
3569
Bram Moolenaar8c711452005-01-14 21:53:12 +00003570 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574 int forceit;
3575{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003576 int ret = OK;
3577 int cc;
3578
3579 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581 cc = *name_end;
3582 *name_end = NUL;
3583
3584 /* Normal name or expanded name. */
3585 if (check_changedtick(lp->ll_name))
3586 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003589 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003591 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3592 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 else if (lp->ll_range)
3594 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596
3597 /* Delete a range of List items. */
3598 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3599 {
3600 li = lp->ll_li->li_next;
3601 listitem_remove(lp->ll_list, lp->ll_li);
3602 lp->ll_li = li;
3603 ++lp->ll_n1;
3604 }
3605 }
3606 else
3607 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 /* unlet a List item. */
3610 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003613 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 }
3615
3616 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617}
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619/*
3620 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
3623 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003626 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar33570922005-01-25 22:26:29 +00003628 hashtab_T *ht;
3629 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003630 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003631 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar33570922005-01-25 22:26:29 +00003633 ht = find_var_ht(name, &varname);
3634 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003636 hi = hash_find(ht, varname);
3637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003638 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003639 di = HI2DI(hi);
3640 if (var_check_fixed(di->di_flags, name)
3641 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642 return FAIL;
3643 delete_var(ht, hi);
3644 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 if (forceit)
3648 return OK;
3649 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return FAIL;
3651}
3652
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653/*
3654 * Lock or unlock variable indicated by "lp".
3655 * "deep" is the levels to go (-1 for unlimited);
3656 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3657 */
3658 static int
3659do_lock_var(lp, name_end, deep, lock)
3660 lval_T *lp;
3661 char_u *name_end;
3662 int deep;
3663 int lock;
3664{
3665 int ret = OK;
3666 int cc;
3667 dictitem_T *di;
3668
3669 if (deep == 0) /* nothing to do */
3670 return OK;
3671
3672 if (lp->ll_tv == NULL)
3673 {
3674 cc = *name_end;
3675 *name_end = NUL;
3676
3677 /* Normal name or expanded name. */
3678 if (check_changedtick(lp->ll_name))
3679 ret = FAIL;
3680 else
3681 {
3682 di = find_var(lp->ll_name, NULL);
3683 if (di == NULL)
3684 ret = FAIL;
3685 else
3686 {
3687 if (lock)
3688 di->di_flags |= DI_FLAGS_LOCK;
3689 else
3690 di->di_flags &= ~DI_FLAGS_LOCK;
3691 item_lock(&di->di_tv, deep, lock);
3692 }
3693 }
3694 *name_end = cc;
3695 }
3696 else if (lp->ll_range)
3697 {
3698 listitem_T *li = lp->ll_li;
3699
3700 /* (un)lock a range of List items. */
3701 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3702 {
3703 item_lock(&li->li_tv, deep, lock);
3704 li = li->li_next;
3705 ++lp->ll_n1;
3706 }
3707 }
3708 else if (lp->ll_list != NULL)
3709 /* (un)lock a List item. */
3710 item_lock(&lp->ll_li->li_tv, deep, lock);
3711 else
3712 /* un(lock) a Dictionary item. */
3713 item_lock(&lp->ll_di->di_tv, deep, lock);
3714
3715 return ret;
3716}
3717
3718/*
3719 * Lock or unlock an item. "deep" is nr of levels to go.
3720 */
3721 static void
3722item_lock(tv, deep, lock)
3723 typval_T *tv;
3724 int deep;
3725 int lock;
3726{
3727 static int recurse = 0;
3728 list_T *l;
3729 listitem_T *li;
3730 dict_T *d;
3731 hashitem_T *hi;
3732 int todo;
3733
3734 if (recurse >= DICT_MAXNEST)
3735 {
3736 EMSG(_("E743: variable nested too deep for (un)lock"));
3737 return;
3738 }
3739 if (deep == 0)
3740 return;
3741 ++recurse;
3742
3743 /* lock/unlock the item itself */
3744 if (lock)
3745 tv->v_lock |= VAR_LOCKED;
3746 else
3747 tv->v_lock &= ~VAR_LOCKED;
3748
3749 switch (tv->v_type)
3750 {
3751 case VAR_LIST:
3752 if ((l = tv->vval.v_list) != NULL)
3753 {
3754 if (lock)
3755 l->lv_lock |= VAR_LOCKED;
3756 else
3757 l->lv_lock &= ~VAR_LOCKED;
3758 if (deep < 0 || deep > 1)
3759 /* recursive: lock/unlock the items the List contains */
3760 for (li = l->lv_first; li != NULL; li = li->li_next)
3761 item_lock(&li->li_tv, deep - 1, lock);
3762 }
3763 break;
3764 case VAR_DICT:
3765 if ((d = tv->vval.v_dict) != NULL)
3766 {
3767 if (lock)
3768 d->dv_lock |= VAR_LOCKED;
3769 else
3770 d->dv_lock &= ~VAR_LOCKED;
3771 if (deep < 0 || deep > 1)
3772 {
3773 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3776 {
3777 if (!HASHITEM_EMPTY(hi))
3778 {
3779 --todo;
3780 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3781 }
3782 }
3783 }
3784 }
3785 }
3786 --recurse;
3787}
3788
Bram Moolenaara40058a2005-07-11 22:42:07 +00003789/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003790 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3791 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003792 */
3793 static int
3794tv_islocked(tv)
3795 typval_T *tv;
3796{
3797 return (tv->v_lock & VAR_LOCKED)
3798 || (tv->v_type == VAR_LIST
3799 && tv->vval.v_list != NULL
3800 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3801 || (tv->v_type == VAR_DICT
3802 && tv->vval.v_dict != NULL
3803 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3804}
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3807/*
3808 * Delete all "menutrans_" variables.
3809 */
3810 void
3811del_menutrans_vars()
3812{
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003814 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaar33570922005-01-25 22:26:29 +00003816 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003817 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003818 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003819 {
3820 if (!HASHITEM_EMPTY(hi))
3821 {
3822 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3824 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003825 }
3826 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828}
3829#endif
3830
3831#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3832
3833/*
3834 * Local string buffer for the next two functions to store a variable name
3835 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3836 * get_user_var_name().
3837 */
3838
3839static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3840
3841static char_u *varnamebuf = NULL;
3842static int varnamebuflen = 0;
3843
3844/*
3845 * Function to concatenate a prefix and a variable name.
3846 */
3847 static char_u *
3848cat_prefix_varname(prefix, name)
3849 int prefix;
3850 char_u *name;
3851{
3852 int len;
3853
3854 len = (int)STRLEN(name) + 3;
3855 if (len > varnamebuflen)
3856 {
3857 vim_free(varnamebuf);
3858 len += 10; /* some additional space */
3859 varnamebuf = alloc(len);
3860 if (varnamebuf == NULL)
3861 {
3862 varnamebuflen = 0;
3863 return NULL;
3864 }
3865 varnamebuflen = len;
3866 }
3867 *varnamebuf = prefix;
3868 varnamebuf[1] = ':';
3869 STRCPY(varnamebuf + 2, name);
3870 return varnamebuf;
3871}
3872
3873/*
3874 * Function given to ExpandGeneric() to obtain the list of user defined
3875 * (global/buffer/window/built-in) variable names.
3876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 char_u *
3878get_user_var_name(xp, idx)
3879 expand_T *xp;
3880 int idx;
3881{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003882 static long_u gdone;
3883 static long_u bdone;
3884 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003885#ifdef FEAT_WINDOWS
3886 static long_u tdone;
3887#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003888 static int vidx;
3889 static hashitem_T *hi;
3890 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003893 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003894 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895#ifdef FEAT_WINDOWS
3896 tdone = 0;
3897#endif
3898 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003899
3900 /* Global variables */
3901 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003903 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003905 else
3906 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003907 while (HASHITEM_EMPTY(hi))
3908 ++hi;
3909 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3910 return cat_prefix_varname('g', hi->hi_key);
3911 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003913
3914 /* b: variables */
3915 ht = &curbuf->b_vars.dv_hashtab;
3916 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003920 else
3921 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003922 while (HASHITEM_EMPTY(hi))
3923 ++hi;
3924 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return (char_u *)"b:changedtick";
3930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003931
3932 /* w: variables */
3933 ht = &curwin->w_vars.dv_hashtab;
3934 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003936 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003938 else
3939 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 while (HASHITEM_EMPTY(hi))
3941 ++hi;
3942 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003944
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003945#ifdef FEAT_WINDOWS
3946 /* t: variables */
3947 ht = &curtab->tp_vars.dv_hashtab;
3948 if (tdone < ht->ht_used)
3949 {
3950 if (tdone++ == 0)
3951 hi = ht->ht_array;
3952 else
3953 ++hi;
3954 while (HASHITEM_EMPTY(hi))
3955 ++hi;
3956 return cat_prefix_varname('t', hi->hi_key);
3957 }
3958#endif
3959
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 /* v: variables */
3961 if (vidx < VV_LEN)
3962 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964 vim_free(varnamebuf);
3965 varnamebuf = NULL;
3966 varnamebuflen = 0;
3967 return NULL;
3968}
3969
3970#endif /* FEAT_CMDL_COMPL */
3971
3972/*
3973 * types for expressions.
3974 */
3975typedef enum
3976{
3977 TYPE_UNKNOWN = 0
3978 , TYPE_EQUAL /* == */
3979 , TYPE_NEQUAL /* != */
3980 , TYPE_GREATER /* > */
3981 , TYPE_GEQUAL /* >= */
3982 , TYPE_SMALLER /* < */
3983 , TYPE_SEQUAL /* <= */
3984 , TYPE_MATCH /* =~ */
3985 , TYPE_NOMATCH /* !~ */
3986} exptype_T;
3987
3988/*
3989 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3992 */
3993
3994/*
3995 * Handle zero level expression.
3996 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003998 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Return OK or FAIL.
4000 */
4001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004002eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u **nextcmd;
4006 int evaluate;
4007{
4008 int ret;
4009 char_u *p;
4010
4011 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (ret == FAIL || !ends_excmd(*p))
4014 {
4015 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 /*
4018 * Report the invalid expression unless the expression evaluation has
4019 * been cancelled due to an aborting error, an interrupt, or an
4020 * exception.
4021 */
4022 if (!aborting())
4023 EMSG2(_(e_invexpr2), arg);
4024 ret = FAIL;
4025 }
4026 if (nextcmd != NULL)
4027 *nextcmd = check_nextcmd(p);
4028
4029 return ret;
4030}
4031
4032/*
4033 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004034 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 *
4036 * "arg" must point to the first non-white of the expression.
4037 * "arg" is advanced to the next non-white after the recognized expression.
4038 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004039 * Note: "rettv.v_lock" is not set.
4040 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 * Return OK or FAIL.
4042 */
4043 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 int evaluate;
4048{
4049 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 /*
4053 * Get the first variable.
4054 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 return FAIL;
4057
4058 if ((*arg)[0] == '?')
4059 {
4060 result = FALSE;
4061 if (evaluate)
4062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 int error = FALSE;
4064
4065 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 if (error)
4069 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071
4072 /*
4073 * Get the second variable.
4074 */
4075 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return FAIL;
4078
4079 /*
4080 * Check for the ":".
4081 */
4082 if ((*arg)[0] != ':')
4083 {
4084 EMSG(_("E109: Missing ':' after '?'"));
4085 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088 }
4089
4090 /*
4091 * Get the third variable.
4092 */
4093 *arg = skipwhite(*arg + 1);
4094 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4095 {
4096 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099 }
4100 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103
4104 return OK;
4105}
4106
4107/*
4108 * Handle first level expression:
4109 * expr2 || expr2 || expr2 logical OR
4110 *
4111 * "arg" must point to the first non-white of the expression.
4112 * "arg" is advanced to the next non-white after the recognized expression.
4113 *
4114 * Return OK or FAIL.
4115 */
4116 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 int evaluate;
4121{
Bram Moolenaar33570922005-01-25 22:26:29 +00004122 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 long result;
4124 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004125 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /*
4128 * Get the first variable.
4129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 return FAIL;
4132
4133 /*
4134 * Repeat until there is no following "||".
4135 */
4136 first = TRUE;
4137 result = FALSE;
4138 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4139 {
4140 if (evaluate && first)
4141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004142 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004145 if (error)
4146 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 first = FALSE;
4148 }
4149
4150 /*
4151 * Get the second variable.
4152 */
4153 *arg = skipwhite(*arg + 2);
4154 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4155 return FAIL;
4156
4157 /*
4158 * Compute the result.
4159 */
4160 if (evaluate && !result)
4161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (error)
4166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 if (evaluate)
4169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 rettv->v_type = VAR_NUMBER;
4171 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 }
4174
4175 return OK;
4176}
4177
4178/*
4179 * Handle second level expression:
4180 * expr3 && expr3 && expr3 logical AND
4181 *
4182 * "arg" must point to the first non-white of the expression.
4183 * "arg" is advanced to the next non-white after the recognized expression.
4184 *
4185 * Return OK or FAIL.
4186 */
4187 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 int evaluate;
4192{
Bram Moolenaar33570922005-01-25 22:26:29 +00004193 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 long result;
4195 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 /*
4199 * Get the first variable.
4200 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203
4204 /*
4205 * Repeat until there is no following "&&".
4206 */
4207 first = TRUE;
4208 result = TRUE;
4209 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4210 {
4211 if (evaluate && first)
4212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004213 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 if (error)
4217 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 first = FALSE;
4219 }
4220
4221 /*
4222 * Get the second variable.
4223 */
4224 *arg = skipwhite(*arg + 2);
4225 if (eval4(arg, &var2, evaluate && result) == FAIL)
4226 return FAIL;
4227
4228 /*
4229 * Compute the result.
4230 */
4231 if (evaluate && result)
4232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (error)
4237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 if (evaluate)
4240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 rettv->v_type = VAR_NUMBER;
4242 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 }
4245
4246 return OK;
4247}
4248
4249/*
4250 * Handle third level expression:
4251 * var1 == var2
4252 * var1 =~ var2
4253 * var1 != var2
4254 * var1 !~ var2
4255 * var1 > var2
4256 * var1 >= var2
4257 * var1 < var2
4258 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004259 * var1 is var2
4260 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 *
4262 * "arg" must point to the first non-white of the expression.
4263 * "arg" is advanced to the next non-white after the recognized expression.
4264 *
4265 * Return OK or FAIL.
4266 */
4267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int evaluate;
4272{
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 char_u *p;
4275 int i;
4276 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004277 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int len = 2;
4279 long n1, n2;
4280 char_u *s1, *s2;
4281 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4282 regmatch_T regmatch;
4283 int ic;
4284 char_u *save_cpo;
4285
4286 /*
4287 * Get the first variable.
4288 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return FAIL;
4291
4292 p = *arg;
4293 switch (p[0])
4294 {
4295 case '=': if (p[1] == '=')
4296 type = TYPE_EQUAL;
4297 else if (p[1] == '~')
4298 type = TYPE_MATCH;
4299 break;
4300 case '!': if (p[1] == '=')
4301 type = TYPE_NEQUAL;
4302 else if (p[1] == '~')
4303 type = TYPE_NOMATCH;
4304 break;
4305 case '>': if (p[1] != '=')
4306 {
4307 type = TYPE_GREATER;
4308 len = 1;
4309 }
4310 else
4311 type = TYPE_GEQUAL;
4312 break;
4313 case '<': if (p[1] != '=')
4314 {
4315 type = TYPE_SMALLER;
4316 len = 1;
4317 }
4318 else
4319 type = TYPE_SEQUAL;
4320 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004321 case 'i': if (p[1] == 's')
4322 {
4323 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4324 len = 5;
4325 if (!vim_isIDc(p[len]))
4326 {
4327 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4328 type_is = TRUE;
4329 }
4330 }
4331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333
4334 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004335 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 */
4337 if (type != TYPE_UNKNOWN)
4338 {
4339 /* extra question mark appended: ignore case */
4340 if (p[len] == '?')
4341 {
4342 ic = TRUE;
4343 ++len;
4344 }
4345 /* extra '#' appended: match case */
4346 else if (p[len] == '#')
4347 {
4348 ic = FALSE;
4349 ++len;
4350 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004351 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 else
4353 ic = p_ic;
4354
4355 /*
4356 * Get the second variable.
4357 */
4358 *arg = skipwhite(p + len);
4359 if (eval5(arg, &var2, evaluate) == FAIL)
4360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return FAIL;
4363 }
4364
4365 if (evaluate)
4366 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 if (type_is && rettv->v_type != var2.v_type)
4368 {
4369 /* For "is" a different type always means FALSE, for "notis"
4370 * it means TRUE. */
4371 n1 = (type == TYPE_NEQUAL);
4372 }
4373 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4374 {
4375 if (type_is)
4376 {
4377 n1 = (rettv->v_type == var2.v_type
4378 && rettv->vval.v_list == var2.vval.v_list);
4379 if (type == TYPE_NEQUAL)
4380 n1 = !n1;
4381 }
4382 else if (rettv->v_type != var2.v_type
4383 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4384 {
4385 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004386 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004388 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 clear_tv(rettv);
4390 clear_tv(&var2);
4391 return FAIL;
4392 }
4393 else
4394 {
4395 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004396 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4397 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 if (type == TYPE_NEQUAL)
4399 n1 = !n1;
4400 }
4401 }
4402
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004403 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4404 {
4405 if (type_is)
4406 {
4407 n1 = (rettv->v_type == var2.v_type
4408 && rettv->vval.v_dict == var2.vval.v_dict);
4409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 else if (rettv->v_type != var2.v_type
4413 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4414 {
4415 if (rettv->v_type != var2.v_type)
4416 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4417 else
4418 EMSG(_("E736: Invalid operation for Dictionary"));
4419 clear_tv(rettv);
4420 clear_tv(&var2);
4421 return FAIL;
4422 }
4423 else
4424 {
4425 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004426 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4427 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004428 if (type == TYPE_NEQUAL)
4429 n1 = !n1;
4430 }
4431 }
4432
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4434 {
4435 if (rettv->v_type != var2.v_type
4436 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4437 {
4438 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004439 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004441 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 clear_tv(rettv);
4443 clear_tv(&var2);
4444 return FAIL;
4445 }
4446 else
4447 {
4448 /* Compare two Funcrefs for being equal or unequal. */
4449 if (rettv->vval.v_string == NULL
4450 || var2.vval.v_string == NULL)
4451 n1 = FALSE;
4452 else
4453 n1 = STRCMP(rettv->vval.v_string,
4454 var2.vval.v_string) == 0;
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 }
4459
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460#ifdef FEAT_FLOAT
4461 /*
4462 * If one of the two variables is a float, compare as a float.
4463 * When using "=~" or "!~", always compare as string.
4464 */
4465 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4466 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4467 {
4468 float_T f1, f2;
4469
4470 if (rettv->v_type == VAR_FLOAT)
4471 f1 = rettv->vval.v_float;
4472 else
4473 f1 = get_tv_number(rettv);
4474 if (var2.v_type == VAR_FLOAT)
4475 f2 = var2.vval.v_float;
4476 else
4477 f2 = get_tv_number(&var2);
4478 n1 = FALSE;
4479 switch (type)
4480 {
4481 case TYPE_EQUAL: n1 = (f1 == f2); break;
4482 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4483 case TYPE_GREATER: n1 = (f1 > f2); break;
4484 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4485 case TYPE_SMALLER: n1 = (f1 < f2); break;
4486 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4487 case TYPE_UNKNOWN:
4488 case TYPE_MATCH:
4489 case TYPE_NOMATCH: break; /* avoid gcc warning */
4490 }
4491 }
4492#endif
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /*
4495 * If one of the two variables is a number, compare as a number.
4496 * When using "=~" or "!~", always compare as string.
4497 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004501 n1 = get_tv_number(rettv);
4502 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 switch (type)
4504 {
4505 case TYPE_EQUAL: n1 = (n1 == n2); break;
4506 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4507 case TYPE_GREATER: n1 = (n1 > n2); break;
4508 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4509 case TYPE_SMALLER: n1 = (n1 < n2); break;
4510 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4511 case TYPE_UNKNOWN:
4512 case TYPE_MATCH:
4513 case TYPE_NOMATCH: break; /* avoid gcc warning */
4514 }
4515 }
4516 else
4517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518 s1 = get_tv_string_buf(rettv, buf1);
4519 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4521 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4522 else
4523 i = 0;
4524 n1 = FALSE;
4525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (i == 0); break;
4528 case TYPE_NEQUAL: n1 = (i != 0); break;
4529 case TYPE_GREATER: n1 = (i > 0); break;
4530 case TYPE_GEQUAL: n1 = (i >= 0); break;
4531 case TYPE_SMALLER: n1 = (i < 0); break;
4532 case TYPE_SEQUAL: n1 = (i <= 0); break;
4533
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH:
4536 /* avoid 'l' flag in 'cpoptions' */
4537 save_cpo = p_cpo;
4538 p_cpo = (char_u *)"";
4539 regmatch.regprog = vim_regcomp(s2,
4540 RE_MAGIC + RE_STRING);
4541 regmatch.rm_ic = ic;
4542 if (regmatch.regprog != NULL)
4543 {
4544 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4545 vim_free(regmatch.regprog);
4546 if (type == TYPE_NOMATCH)
4547 n1 = !n1;
4548 }
4549 p_cpo = save_cpo;
4550 break;
4551
4552 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4553 }
4554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 rettv->v_type = VAR_NUMBER;
4558 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 }
4561
4562 return OK;
4563}
4564
4565/*
4566 * Handle fourth level expression:
4567 * + number addition
4568 * - number subtraction
4569 * . string concatenation
4570 *
4571 * "arg" must point to the first non-white of the expression.
4572 * "arg" is advanced to the next non-white after the recognized expression.
4573 *
4574 * Return OK or FAIL.
4575 */
4576 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 int evaluate;
4581{
Bram Moolenaar33570922005-01-25 22:26:29 +00004582 typval_T var2;
4583 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 int op;
4585 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586#ifdef FEAT_FLOAT
4587 float_T f1 = 0, f2 = 0;
4588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 char_u *s1, *s2;
4590 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4591 char_u *p;
4592
4593 /*
4594 * Get the first variable.
4595 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004596 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 return FAIL;
4598
4599 /*
4600 * Repeat computing, until no '+', '-' or '.' is following.
4601 */
4602 for (;;)
4603 {
4604 op = **arg;
4605 if (op != '+' && op != '-' && op != '.')
4606 break;
4607
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004608 if ((op != '+' || rettv->v_type != VAR_LIST)
4609#ifdef FEAT_FLOAT
4610 && (op == '.' || rettv->v_type != VAR_FLOAT)
4611#endif
4612 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004613 {
4614 /* For "list + ...", an illegal use of the first operand as
4615 * a number cannot be determined before evaluating the 2nd
4616 * operand: if this is also a list, all is ok.
4617 * For "something . ...", "something - ..." or "non-list + ...",
4618 * we know that the first operand needs to be a string or number
4619 * without evaluating the 2nd operand. So check before to avoid
4620 * side effects after an error. */
4621 if (evaluate && get_tv_string_chk(rettv) == NULL)
4622 {
4623 clear_tv(rettv);
4624 return FAIL;
4625 }
4626 }
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 /*
4629 * Get the second variable.
4630 */
4631 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004632 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004634 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 return FAIL;
4636 }
4637
4638 if (evaluate)
4639 {
4640 /*
4641 * Compute the result.
4642 */
4643 if (op == '.')
4644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4646 s2 = get_tv_string_buf_chk(&var2, buf2);
4647 if (s2 == NULL) /* type error ? */
4648 {
4649 clear_tv(rettv);
4650 clear_tv(&var2);
4651 return FAIL;
4652 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004653 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 clear_tv(rettv);
4655 rettv->v_type = VAR_STRING;
4656 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004658 else if (op == '+' && rettv->v_type == VAR_LIST
4659 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004660 {
4661 /* concatenate Lists */
4662 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4663 &var3) == FAIL)
4664 {
4665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 return FAIL;
4668 }
4669 clear_tv(rettv);
4670 *rettv = var3;
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 else
4673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 int error = FALSE;
4675
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004676#ifdef FEAT_FLOAT
4677 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004678 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004679 f1 = rettv->vval.v_float;
4680 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682 else
4683#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004685 n1 = get_tv_number_chk(rettv, &error);
4686 if (error)
4687 {
4688 /* This can only happen for "list + non-list". For
4689 * "non-list + ..." or "something - ...", we returned
4690 * before evaluating the 2nd operand. */
4691 clear_tv(rettv);
4692 return FAIL;
4693 }
4694#ifdef FEAT_FLOAT
4695 if (var2.v_type == VAR_FLOAT)
4696 f1 = n1;
4697#endif
4698 }
4699#ifdef FEAT_FLOAT
4700 if (var2.v_type == VAR_FLOAT)
4701 {
4702 f2 = var2.vval.v_float;
4703 n2 = 0;
4704 }
4705 else
4706#endif
4707 {
4708 n2 = get_tv_number_chk(&var2, &error);
4709 if (error)
4710 {
4711 clear_tv(rettv);
4712 clear_tv(&var2);
4713 return FAIL;
4714 }
4715#ifdef FEAT_FLOAT
4716 if (rettv->v_type == VAR_FLOAT)
4717 f2 = n2;
4718#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721
4722#ifdef FEAT_FLOAT
4723 /* If there is a float on either side the result is a float. */
4724 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4725 {
4726 if (op == '+')
4727 f1 = f1 + f2;
4728 else
4729 f1 = f1 - f2;
4730 rettv->v_type = VAR_FLOAT;
4731 rettv->vval.v_float = f1;
4732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#endif
4735 {
4736 if (op == '+')
4737 n1 = n1 + n2;
4738 else
4739 n1 = n1 - n2;
4740 rettv->v_type = VAR_NUMBER;
4741 rettv->vval.v_number = n1;
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746 }
4747 return OK;
4748}
4749
4750/*
4751 * Handle fifth level expression:
4752 * * number multiplication
4753 * / number division
4754 * % number modulo
4755 *
4756 * "arg" must point to the first non-white of the expression.
4757 * "arg" is advanced to the next non-white after the recognized expression.
4758 *
4759 * Return OK or FAIL.
4760 */
4761 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004762eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004766 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
Bram Moolenaar33570922005-01-25 22:26:29 +00004768 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 int op;
4770 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#ifdef FEAT_FLOAT
4772 int use_float = FALSE;
4773 float_T f1 = 0, f2;
4774#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 /*
4778 * Get the first variable.
4779 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004780 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return FAIL;
4782
4783 /*
4784 * Repeat computing, until no '*', '/' or '%' is following.
4785 */
4786 for (;;)
4787 {
4788 op = **arg;
4789 if (op != '*' && op != '/' && op != '%')
4790 break;
4791
4792 if (evaluate)
4793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794#ifdef FEAT_FLOAT
4795 if (rettv->v_type == VAR_FLOAT)
4796 {
4797 f1 = rettv->vval.v_float;
4798 use_float = TRUE;
4799 n1 = 0;
4800 }
4801 else
4802#endif
4803 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 if (error)
4806 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 else
4809 n1 = 0;
4810
4811 /*
4812 * Get the second variable.
4813 */
4814 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004815 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return FAIL;
4817
4818 if (evaluate)
4819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820#ifdef FEAT_FLOAT
4821 if (var2.v_type == VAR_FLOAT)
4822 {
4823 if (!use_float)
4824 {
4825 f1 = n1;
4826 use_float = TRUE;
4827 }
4828 f2 = var2.vval.v_float;
4829 n2 = 0;
4830 }
4831 else
4832#endif
4833 {
4834 n2 = get_tv_number_chk(&var2, &error);
4835 clear_tv(&var2);
4836 if (error)
4837 return FAIL;
4838#ifdef FEAT_FLOAT
4839 if (use_float)
4840 f2 = n2;
4841#endif
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 /*
4845 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848#ifdef FEAT_FLOAT
4849 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004851 if (op == '*')
4852 f1 = f1 * f2;
4853 else if (op == '/')
4854 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004855# ifdef VMS
4856 /* VMS crashes on divide by zero, work around it */
4857 if (f2 == 0.0)
4858 {
4859 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004860 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004861 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004862 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004863 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004864 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004865 }
4866 else
4867 f1 = f1 / f2;
4868# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869 /* We rely on the floating point library to handle divide
4870 * by zero to result in "inf" and not a crash. */
4871 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004872# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004876 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 return FAIL;
4878 }
4879 rettv->v_type = VAR_FLOAT;
4880 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885 if (op == '*')
4886 n1 = n1 * n2;
4887 else if (op == '/')
4888 {
4889 if (n2 == 0) /* give an error message? */
4890 {
4891 if (n1 == 0)
4892 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4893 else if (n1 < 0)
4894 n1 = -0x7fffffffL;
4895 else
4896 n1 = 0x7fffffffL;
4897 }
4898 else
4899 n1 = n1 / n2;
4900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 {
4903 if (n2 == 0) /* give an error message? */
4904 n1 = 0;
4905 else
4906 n1 = n1 % n2;
4907 }
4908 rettv->v_type = VAR_NUMBER;
4909 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 }
4913
4914 return OK;
4915}
4916
4917/*
4918 * Handle sixth level expression:
4919 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004920 * "string" string constant
4921 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 * &option-name option value
4923 * @r register contents
4924 * identifier variable value
4925 * function() function call
4926 * $VAR environment variable
4927 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004928 * [expr, expr] List
4929 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 *
4931 * Also handle:
4932 * ! in front logical NOT
4933 * - in front unary minus
4934 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 * trailing [] subscript in String or List
4936 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 *
4938 * "arg" must point to the first non-white of the expression.
4939 * "arg" is advanced to the next non-white after the recognized expression.
4940 *
4941 * Return OK or FAIL.
4942 */
4943 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004944eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004948 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 long n;
4951 int len;
4952 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 char_u *start_leader, *end_leader;
4954 int ret = OK;
4955 char_u *alias;
4956
4957 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004958 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /*
4964 * Skip '!' and '-' characters. They are handled later.
4965 */
4966 start_leader = *arg;
4967 while (**arg == '!' || **arg == '-' || **arg == '+')
4968 *arg = skipwhite(*arg + 1);
4969 end_leader = *arg;
4970
4971 switch (**arg)
4972 {
4973 /*
4974 * Number constant.
4975 */
4976 case '0':
4977 case '1':
4978 case '2':
4979 case '3':
4980 case '4':
4981 case '5':
4982 case '6':
4983 case '7':
4984 case '8':
4985 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 {
4987#ifdef FEAT_FLOAT
4988 char_u *p = skipdigits(*arg + 1);
4989 int get_float = FALSE;
4990
4991 /* We accept a float when the format matches
4992 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004993 * strict to avoid backwards compatibility problems.
4994 * Don't look for a float after the "." operator, so that
4995 * ":let vers = 1.2.3" doesn't fail. */
4996 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998 get_float = TRUE;
4999 p = skipdigits(p + 2);
5000 if (*p == 'e' || *p == 'E')
5001 {
5002 ++p;
5003 if (*p == '-' || *p == '+')
5004 ++p;
5005 if (!vim_isdigit(*p))
5006 get_float = FALSE;
5007 else
5008 p = skipdigits(p + 1);
5009 }
5010 if (ASCII_ISALPHA(*p) || *p == '.')
5011 get_float = FALSE;
5012 }
5013 if (get_float)
5014 {
5015 float_T f;
5016
5017 *arg += string2float(*arg, &f);
5018 if (evaluate)
5019 {
5020 rettv->v_type = VAR_FLOAT;
5021 rettv->vval.v_float = f;
5022 }
5023 }
5024 else
5025#endif
5026 {
5027 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5028 *arg += len;
5029 if (evaluate)
5030 {
5031 rettv->v_type = VAR_NUMBER;
5032 rettv->vval.v_number = n;
5033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 /*
5039 * String constant: "string".
5040 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043
5044 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005048 break;
5049
5050 /*
5051 * List: [expr, expr]
5052 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055
5056 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 * Dictionary: {key: val, key: val}
5058 */
5059 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5060 break;
5061
5062 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005063 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005065 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 break;
5067
5068 /*
5069 * Environment variable: $VAR.
5070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /*
5075 * Register contents: @r.
5076 */
5077 case '@': ++*arg;
5078 if (evaluate)
5079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005081 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 if (**arg != NUL)
5084 ++*arg;
5085 break;
5086
5087 /*
5088 * nested expression: (expression).
5089 */
5090 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (**arg == ')')
5093 ++*arg;
5094 else if (ret == OK)
5095 {
5096 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 ret = FAIL;
5099 }
5100 break;
5101
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 break;
5104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105
5106 if (ret == NOTDONE)
5107 {
5108 /*
5109 * Must be a variable or function name.
5110 * Can also be a curly-braces kind of name: {expr}.
5111 */
5112 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005113 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 if (alias != NULL)
5115 s = alias;
5116
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005117 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 ret = FAIL;
5119 else
5120 {
5121 if (**arg == '(') /* recursive! */
5122 {
5123 /* If "s" is the name of a variable of type VAR_FUNC
5124 * use its contents. */
5125 s = deref_func_name(s, &len);
5126
5127 /* Invoke the function. */
5128 ret = get_func_tv(s, len, rettv, arg,
5129 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005130 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 /* Stop the expression evaluation when immediately
5132 * aborting on error, or when an interrupt occurred or
5133 * an exception was thrown but not caught. */
5134 if (aborting())
5135 {
5136 if (ret == OK)
5137 clear_tv(rettv);
5138 ret = FAIL;
5139 }
5140 }
5141 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005143 else
5144 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005146 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 }
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 *arg = skipwhite(*arg);
5150
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005151 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5152 * expr(expr). */
5153 if (ret == OK)
5154 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 /*
5157 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5158 */
5159 if (ret == OK && evaluate && end_leader > start_leader)
5160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 int val = 0;
5163#ifdef FEAT_FLOAT
5164 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005165
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005166 if (rettv->v_type == VAR_FLOAT)
5167 f = rettv->vval.v_float;
5168 else
5169#endif
5170 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005173 clear_tv(rettv);
5174 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176 else
5177 {
5178 while (end_leader > start_leader)
5179 {
5180 --end_leader;
5181 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005182 {
5183#ifdef FEAT_FLOAT
5184 if (rettv->v_type == VAR_FLOAT)
5185 f = !f;
5186 else
5187#endif
5188 val = !val;
5189 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005190 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005191 {
5192#ifdef FEAT_FLOAT
5193 if (rettv->v_type == VAR_FLOAT)
5194 f = -f;
5195 else
5196#endif
5197 val = -val;
5198 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005199 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005200#ifdef FEAT_FLOAT
5201 if (rettv->v_type == VAR_FLOAT)
5202 {
5203 clear_tv(rettv);
5204 rettv->vval.v_float = f;
5205 }
5206 else
5207#endif
5208 {
5209 clear_tv(rettv);
5210 rettv->v_type = VAR_NUMBER;
5211 rettv->vval.v_number = val;
5212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215
5216 return ret;
5217}
5218
5219/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005220 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5221 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5223 */
5224 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230{
5231 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005232 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 long len = -1;
5235 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 if (rettv->v_type == VAR_FUNC
5240#ifdef FEAT_FLOAT
5241 || rettv->v_type == VAR_FLOAT
5242#endif
5243 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005245 if (verbose)
5246 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 return FAIL;
5248 }
5249
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /*
5253 * dict.name
5254 */
5255 key = *arg + 1;
5256 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5257 ;
5258 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 }
5262 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 /*
5265 * something[idx]
5266 *
5267 * Get the (first) variable from inside the [].
5268 */
5269 *arg = skipwhite(*arg + 1);
5270 if (**arg == ':')
5271 empty1 = TRUE;
5272 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5273 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005274 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5275 {
5276 /* not a number or string */
5277 clear_tv(&var1);
5278 return FAIL;
5279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280
5281 /*
5282 * Get the second variable from inside the [:].
5283 */
5284 if (**arg == ':')
5285 {
5286 range = TRUE;
5287 *arg = skipwhite(*arg + 1);
5288 if (**arg == ']')
5289 empty2 = TRUE;
5290 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (!empty1)
5293 clear_tv(&var1);
5294 return FAIL;
5295 }
5296 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5297 {
5298 /* not a number or string */
5299 if (!empty1)
5300 clear_tv(&var1);
5301 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 return FAIL;
5303 }
5304 }
5305
5306 /* Check for the ']'. */
5307 if (**arg != ']')
5308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005309 if (verbose)
5310 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 clear_tv(&var1);
5312 if (range)
5313 clear_tv(&var2);
5314 return FAIL;
5315 }
5316 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 }
5318
5319 if (evaluate)
5320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 n1 = 0;
5322 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005324 n1 = get_tv_number(&var1);
5325 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 }
5327 if (range)
5328 {
5329 if (empty2)
5330 n2 = -1;
5331 else
5332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333 n2 = get_tv_number(&var2);
5334 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 }
5336 }
5337
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 {
5340 case VAR_NUMBER:
5341 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005342 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 len = (long)STRLEN(s);
5344 if (range)
5345 {
5346 /* The resulting variable is a substring. If the indexes
5347 * are out of range the result is empty. */
5348 if (n1 < 0)
5349 {
5350 n1 = len + n1;
5351 if (n1 < 0)
5352 n1 = 0;
5353 }
5354 if (n2 < 0)
5355 n2 = len + n2;
5356 else if (n2 >= len)
5357 n2 = len;
5358 if (n1 >= len || n2 < 0 || n1 > n2)
5359 s = NULL;
5360 else
5361 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5362 }
5363 else
5364 {
5365 /* The resulting variable is a string of a single
5366 * character. If the index is too big or negative the
5367 * result is empty. */
5368 if (n1 >= len || n1 < 0)
5369 s = NULL;
5370 else
5371 s = vim_strnsave(s + n1, 1);
5372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 clear_tv(rettv);
5374 rettv->v_type = VAR_STRING;
5375 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 break;
5377
5378 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 if (n1 < 0)
5381 n1 = len + n1;
5382 if (!empty1 && (n1 < 0 || n1 >= len))
5383 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005384 /* For a range we allow invalid values and return an empty
5385 * list. A list index out of range is an error. */
5386 if (!range)
5387 {
5388 if (verbose)
5389 EMSGN(_(e_listidx), n1);
5390 return FAIL;
5391 }
5392 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 if (range)
5395 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 list_T *l;
5397 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398
5399 if (n2 < 0)
5400 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005401 else if (n2 >= len)
5402 n2 = len - 1;
5403 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005404 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 l = list_alloc();
5406 if (l == NULL)
5407 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 n1 <= n2; ++n1)
5410 {
5411 if (list_append_tv(l, &item->li_tv) == FAIL)
5412 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005413 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 return FAIL;
5415 }
5416 item = item->li_next;
5417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 clear_tv(rettv);
5419 rettv->v_type = VAR_LIST;
5420 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005421 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 else
5424 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005425 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005426 clear_tv(rettv);
5427 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430
5431 case VAR_DICT:
5432 if (range)
5433 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005434 if (verbose)
5435 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 if (len == -1)
5437 clear_tv(&var1);
5438 return FAIL;
5439 }
5440 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442
5443 if (len == -1)
5444 {
5445 key = get_tv_string(&var1);
5446 if (*key == NUL)
5447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 if (verbose)
5449 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 clear_tv(&var1);
5451 return FAIL;
5452 }
5453 }
5454
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005455 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005457 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005458 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 if (len == -1)
5460 clear_tv(&var1);
5461 if (item == NULL)
5462 return FAIL;
5463
5464 copy_tv(&item->di_tv, &var1);
5465 clear_tv(rettv);
5466 *rettv = var1;
5467 }
5468 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 }
5471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 return OK;
5473}
5474
5475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * Get an option value.
5477 * "arg" points to the '&' or '+' before the option name.
5478 * "arg" is advanced to character after the option name.
5479 * Return OK or FAIL.
5480 */
5481 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 int evaluate;
5486{
5487 char_u *option_end;
5488 long numval;
5489 char_u *stringval;
5490 int opt_type;
5491 int c;
5492 int working = (**arg == '+'); /* has("+option") */
5493 int ret = OK;
5494 int opt_flags;
5495
5496 /*
5497 * Isolate the option name and find its value.
5498 */
5499 option_end = find_option_end(arg, &opt_flags);
5500 if (option_end == NULL)
5501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 EMSG2(_("E112: Option name missing: %s"), *arg);
5504 return FAIL;
5505 }
5506
5507 if (!evaluate)
5508 {
5509 *arg = option_end;
5510 return OK;
5511 }
5512
5513 c = *option_end;
5514 *option_end = NUL;
5515 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518 if (opt_type == -3) /* invalid name */
5519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 EMSG2(_("E113: Unknown option: %s"), *arg);
5522 ret = FAIL;
5523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 if (opt_type == -2) /* hidden string option */
5527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 rettv->v_type = VAR_STRING;
5529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531 else if (opt_type == -1) /* hidden number option */
5532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 rettv->v_type = VAR_NUMBER;
5534 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 }
5536 else if (opt_type == 1) /* number option */
5537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 rettv->v_type = VAR_NUMBER;
5539 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541 else /* string option */
5542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 rettv->v_type = VAR_STRING;
5544 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 }
5547 else if (working && (opt_type == -2 || opt_type == -1))
5548 ret = FAIL;
5549
5550 *option_end = c; /* put back for error messages */
5551 *arg = option_end;
5552
5553 return ret;
5554}
5555
5556/*
5557 * Allocate a variable for a string constant.
5558 * Return OK or FAIL.
5559 */
5560 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 int evaluate;
5565{
5566 char_u *p;
5567 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 int extra = 0;
5569
5570 /*
5571 * Find the end of the string, skipping backslashed characters.
5572 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005573 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 if (*p == '\\' && p[1] != NUL)
5576 {
5577 ++p;
5578 /* A "\<x>" form occupies at least 4 characters, and produces up
5579 * to 6 characters: reserve space for 2 extra */
5580 if (*p == '<')
5581 extra += 2;
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584
5585 if (*p != '"')
5586 {
5587 EMSG2(_("E114: Missing quote: %s"), *arg);
5588 return FAIL;
5589 }
5590
5591 /* If only parsing, set *arg and return here */
5592 if (!evaluate)
5593 {
5594 *arg = p + 1;
5595 return OK;
5596 }
5597
5598 /*
5599 * Copy the string into allocated memory, handling backslashed
5600 * characters.
5601 */
5602 name = alloc((unsigned)(p - *arg + extra));
5603 if (name == NULL)
5604 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 rettv->v_type = VAR_STRING;
5606 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 if (*p == '\\')
5611 {
5612 switch (*++p)
5613 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 case 'b': *name++ = BS; ++p; break;
5615 case 'e': *name++ = ESC; ++p; break;
5616 case 'f': *name++ = FF; ++p; break;
5617 case 'n': *name++ = NL; ++p; break;
5618 case 'r': *name++ = CAR; ++p; break;
5619 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 case 'X': /* hex: "\x1", "\x12" */
5622 case 'x':
5623 case 'u': /* Unicode: "\u0023" */
5624 case 'U':
5625 if (vim_isxdigit(p[1]))
5626 {
5627 int n, nr;
5628 int c = toupper(*p);
5629
5630 if (c == 'X')
5631 n = 2;
5632 else
5633 n = 4;
5634 nr = 0;
5635 while (--n >= 0 && vim_isxdigit(p[1]))
5636 {
5637 ++p;
5638 nr = (nr << 4) + hex2nr(*p);
5639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641#ifdef FEAT_MBYTE
5642 /* For "\u" store the number according to
5643 * 'encoding'. */
5644 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
5647#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 break;
5651
5652 /* octal: "\1", "\12", "\123" */
5653 case '0':
5654 case '1':
5655 case '2':
5656 case '3':
5657 case '4':
5658 case '5':
5659 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 case '7': *name = *p++ - '0';
5661 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 *name = (*name << 3) + *p++ - '0';
5664 if (*p >= '0' && *p <= '7')
5665 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 break;
5669
5670 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if (extra != 0)
5673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 break;
5676 }
5677 /* FALLTHROUGH */
5678
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681 }
5682 }
5683 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 *arg = p + 1;
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 return OK;
5691}
5692
5693/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 * Return OK or FAIL.
5696 */
5697 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005698get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 int evaluate;
5702{
5703 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705 int reduce = 0;
5706
5707 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 */
5710 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 break;
5716 ++reduce;
5717 ++p;
5718 }
5719 }
5720
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 return FAIL;
5725 }
5726
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005728 if (!evaluate)
5729 {
5730 *arg = p + 1;
5731 return OK;
5732 }
5733
5734 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 */
5737 str = alloc((unsigned)((p - *arg) - reduce));
5738 if (str == NULL)
5739 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 rettv->v_type = VAR_STRING;
5741 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 break;
5749 ++p;
5750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 *arg = p + 1;
5755
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 return OK;
5757}
5758
5759/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 * Allocate a variable for a List and fill it from "*arg".
5761 * Return OK or FAIL.
5762 */
5763 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005764get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005766 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 int evaluate;
5768{
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 list_T *l = NULL;
5770 typval_T tv;
5771 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772
5773 if (evaluate)
5774 {
5775 l = list_alloc();
5776 if (l == NULL)
5777 return FAIL;
5778 }
5779
5780 *arg = skipwhite(*arg + 1);
5781 while (**arg != ']' && **arg != NUL)
5782 {
5783 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5784 goto failret;
5785 if (evaluate)
5786 {
5787 item = listitem_alloc();
5788 if (item != NULL)
5789 {
5790 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005791 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 list_append(l, item);
5793 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005794 else
5795 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 }
5797
5798 if (**arg == ']')
5799 break;
5800 if (**arg != ',')
5801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 goto failret;
5804 }
5805 *arg = skipwhite(*arg + 1);
5806 }
5807
5808 if (**arg != ']')
5809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811failret:
5812 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005813 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 return FAIL;
5815 }
5816
5817 *arg = skipwhite(*arg + 1);
5818 if (evaluate)
5819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005820 rettv->v_type = VAR_LIST;
5821 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 ++l->lv_refcount;
5823 }
5824
5825 return OK;
5826}
5827
5828/*
5829 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005830 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005832 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833list_alloc()
5834{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005835 list_T *l;
5836
5837 l = (list_T *)alloc_clear(sizeof(list_T));
5838 if (l != NULL)
5839 {
5840 /* Prepend the list to the list of lists for garbage collection. */
5841 if (first_list != NULL)
5842 first_list->lv_used_prev = l;
5843 l->lv_used_prev = NULL;
5844 l->lv_used_next = first_list;
5845 first_list = l;
5846 }
5847 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848}
5849
5850/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005851 * Allocate an empty list for a return value.
5852 * Returns OK or FAIL.
5853 */
5854 static int
5855rettv_list_alloc(rettv)
5856 typval_T *rettv;
5857{
5858 list_T *l = list_alloc();
5859
5860 if (l == NULL)
5861 return FAIL;
5862
5863 rettv->vval.v_list = l;
5864 rettv->v_type = VAR_LIST;
5865 ++l->lv_refcount;
5866 return OK;
5867}
5868
5869/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 * Unreference a list: decrement the reference count and free it when it
5871 * becomes zero.
5872 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005873 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005877 if (l != NULL && --l->lv_refcount <= 0)
5878 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879}
5880
5881/*
5882 * Free a list, including all items it points to.
5883 * Ignores the reference count.
5884 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005885 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005886list_free(l, recurse)
5887 list_T *l;
5888 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889{
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 /* Remove the list from the list of lists for garbage collection. */
5893 if (l->lv_used_prev == NULL)
5894 first_list = l->lv_used_next;
5895 else
5896 l->lv_used_prev->lv_used_next = l->lv_used_next;
5897 if (l->lv_used_next != NULL)
5898 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5899
Bram Moolenaard9fba312005-06-26 22:34:35 +00005900 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005902 /* Remove the item before deleting it. */
5903 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005904 if (recurse || (item->li_tv.v_type != VAR_LIST
5905 && item->li_tv.v_type != VAR_DICT))
5906 clear_tv(&item->li_tv);
5907 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 }
5909 vim_free(l);
5910}
5911
5912/*
5913 * Allocate a list item.
5914 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916listitem_alloc()
5917{
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919}
5920
5921/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005922 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 */
5924 static void
5925listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005928 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 vim_free(item);
5930}
5931
5932/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005933 * Remove a list item from a List and free it. Also clears the value.
5934 */
5935 static void
5936listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 list_T *l;
5938 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005939{
5940 list_remove(l, item, item);
5941 listitem_free(item);
5942}
5943
5944/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 * Get the number of items in a list.
5946 */
5947 static long
5948list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 if (l == NULL)
5952 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005953 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954}
5955
5956/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005957 * Return TRUE when two lists have exactly the same values.
5958 */
5959 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005960list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005961 list_T *l1;
5962 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005963 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005964 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005965{
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005968 if (l1 == NULL || l2 == NULL)
5969 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005970 if (l1 == l2)
5971 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005972 if (list_len(l1) != list_len(l2))
5973 return FALSE;
5974
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005975 for (item1 = l1->lv_first, item2 = l2->lv_first;
5976 item1 != NULL && item2 != NULL;
5977 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005978 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979 return FALSE;
5980 return item1 == NULL && item2 == NULL;
5981}
5982
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005983#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5984 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005985/*
5986 * Return the dictitem that an entry in a hashtable points to.
5987 */
5988 dictitem_T *
5989dict_lookup(hi)
5990 hashitem_T *hi;
5991{
5992 return HI2DI(hi);
5993}
5994#endif
5995
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005997 * Return TRUE when two dictionaries have exactly the same key/values.
5998 */
5999 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 dict_T *d1;
6002 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006003 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006004 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 hashitem_T *hi;
6007 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006008 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006009
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006010 if (d1 == NULL || d2 == NULL)
6011 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006012 if (d1 == d2)
6013 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006014 if (dict_len(d1) != dict_len(d2))
6015 return FALSE;
6016
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006017 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006018 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006019 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006020 if (!HASHITEM_EMPTY(hi))
6021 {
6022 item2 = dict_find(d2, hi->hi_key, -1);
6023 if (item2 == NULL)
6024 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006025 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006026 return FALSE;
6027 --todo;
6028 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 }
6030 return TRUE;
6031}
6032
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006033static int tv_equal_recurse_limit;
6034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006037 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006038 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006039 */
6040 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006041tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 typval_T *tv1;
6043 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044 int ic; /* ignore case */
6045 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046{
6047 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006048 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006049 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006050 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006051
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006052 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006053 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006054
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006055 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056 * recursiveness to a limit. We guess they are equal then.
6057 * A fixed limit has the problem of still taking an awful long time.
6058 * Reduce the limit every time running into it. That should work fine for
6059 * deeply linked structures that are not recursively linked and catch
6060 * recursiveness quickly. */
6061 if (!recursive)
6062 tv_equal_recurse_limit = 1000;
6063 if (recursive_cnt >= tv_equal_recurse_limit)
6064 {
6065 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006066 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006068
6069 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006070 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006071 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072 ++recursive_cnt;
6073 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6074 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006075 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006076
6077 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 ++recursive_cnt;
6079 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6080 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006081 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006082
6083 case VAR_FUNC:
6084 return (tv1->vval.v_string != NULL
6085 && tv2->vval.v_string != NULL
6086 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6087
6088 case VAR_NUMBER:
6089 return tv1->vval.v_number == tv2->vval.v_number;
6090
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006091#ifdef FEAT_FLOAT
6092 case VAR_FLOAT:
6093 return tv1->vval.v_float == tv2->vval.v_float;
6094#endif
6095
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006096 case VAR_STRING:
6097 s1 = get_tv_string_buf(tv1, buf1);
6098 s2 = get_tv_string_buf(tv2, buf2);
6099 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006101
6102 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 return TRUE;
6104}
6105
6106/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 * Locate item with index "n" in list "l" and return it.
6108 * A negative index is counted from the end; -1 is the last item.
6109 * Returns NULL when "n" is out of range.
6110 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 long n;
6115{
Bram Moolenaar33570922005-01-25 22:26:29 +00006116 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 long idx;
6118
6119 if (l == NULL)
6120 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006121
6122 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006124 n = l->lv_len + n;
6125
6126 /* Check for index out of range. */
6127 if (n < 0 || n >= l->lv_len)
6128 return NULL;
6129
6130 /* When there is a cached index may start search from there. */
6131 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006132 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006133 if (n < l->lv_idx / 2)
6134 {
6135 /* closest to the start of the list */
6136 item = l->lv_first;
6137 idx = 0;
6138 }
6139 else if (n > (l->lv_idx + l->lv_len) / 2)
6140 {
6141 /* closest to the end of the list */
6142 item = l->lv_last;
6143 idx = l->lv_len - 1;
6144 }
6145 else
6146 {
6147 /* closest to the cached index */
6148 item = l->lv_idx_item;
6149 idx = l->lv_idx;
6150 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 }
6152 else
6153 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006154 if (n < l->lv_len / 2)
6155 {
6156 /* closest to the start of the list */
6157 item = l->lv_first;
6158 idx = 0;
6159 }
6160 else
6161 {
6162 /* closest to the end of the list */
6163 item = l->lv_last;
6164 idx = l->lv_len - 1;
6165 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006167
6168 while (n > idx)
6169 {
6170 /* search forward */
6171 item = item->li_next;
6172 ++idx;
6173 }
6174 while (n < idx)
6175 {
6176 /* search backward */
6177 item = item->li_prev;
6178 --idx;
6179 }
6180
6181 /* cache the used index */
6182 l->lv_idx = idx;
6183 l->lv_idx_item = item;
6184
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 return item;
6186}
6187
6188/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006189 * Get list item "l[idx]" as a number.
6190 */
6191 static long
6192list_find_nr(l, idx, errorp)
6193 list_T *l;
6194 long idx;
6195 int *errorp; /* set to TRUE when something wrong */
6196{
6197 listitem_T *li;
6198
6199 li = list_find(l, idx);
6200 if (li == NULL)
6201 {
6202 if (errorp != NULL)
6203 *errorp = TRUE;
6204 return -1L;
6205 }
6206 return get_tv_number_chk(&li->li_tv, errorp);
6207}
6208
6209/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006210 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6211 */
6212 char_u *
6213list_find_str(l, idx)
6214 list_T *l;
6215 long idx;
6216{
6217 listitem_T *li;
6218
6219 li = list_find(l, idx - 1);
6220 if (li == NULL)
6221 {
6222 EMSGN(_(e_listidx), idx);
6223 return NULL;
6224 }
6225 return get_tv_string(&li->li_tv);
6226}
6227
6228/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006229 * Locate "item" list "l" and return its index.
6230 * Returns -1 when "item" is not in the list.
6231 */
6232 static long
6233list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 list_T *l;
6235 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006236{
6237 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006239
6240 if (l == NULL)
6241 return -1;
6242 idx = 0;
6243 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6244 ++idx;
6245 if (li == NULL)
6246 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006247 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006248}
6249
6250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 * Append item "item" to the end of list "l".
6252 */
6253 static void
6254list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 list_T *l;
6256 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257{
6258 if (l->lv_last == NULL)
6259 {
6260 /* empty list */
6261 l->lv_first = item;
6262 l->lv_last = item;
6263 item->li_prev = NULL;
6264 }
6265 else
6266 {
6267 l->lv_last->li_next = item;
6268 item->li_prev = l->lv_last;
6269 l->lv_last = item;
6270 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 item->li_next = NULL;
6273}
6274
6275/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006279 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 list_T *l;
6282 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006284 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285
Bram Moolenaar05159a02005-02-26 23:04:13 +00006286 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006288 copy_tv(tv, &li->li_tv);
6289 list_append(l, li);
6290 return OK;
6291}
6292
6293/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006294 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006295 * Return FAIL when out of memory.
6296 */
6297 int
6298list_append_dict(list, dict)
6299 list_T *list;
6300 dict_T *dict;
6301{
6302 listitem_T *li = listitem_alloc();
6303
6304 if (li == NULL)
6305 return FAIL;
6306 li->li_tv.v_type = VAR_DICT;
6307 li->li_tv.v_lock = 0;
6308 li->li_tv.vval.v_dict = dict;
6309 list_append(list, li);
6310 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 return OK;
6312}
6313
6314/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006315 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006316 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006317 * Returns FAIL when out of memory.
6318 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006319 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006320list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006321 list_T *l;
6322 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006323 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006324{
6325 listitem_T *li = listitem_alloc();
6326
6327 if (li == NULL)
6328 return FAIL;
6329 list_append(l, li);
6330 li->li_tv.v_type = VAR_STRING;
6331 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006332 if (str == NULL)
6333 li->li_tv.vval.v_string = NULL;
6334 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006335 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006336 return FAIL;
6337 return OK;
6338}
6339
6340/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006341 * Append "n" to list "l".
6342 * Returns FAIL when out of memory.
6343 */
6344 static int
6345list_append_number(l, n)
6346 list_T *l;
6347 varnumber_T n;
6348{
6349 listitem_T *li;
6350
6351 li = listitem_alloc();
6352 if (li == NULL)
6353 return FAIL;
6354 li->li_tv.v_type = VAR_NUMBER;
6355 li->li_tv.v_lock = 0;
6356 li->li_tv.vval.v_number = n;
6357 list_append(l, li);
6358 return OK;
6359}
6360
6361/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006363 * If "item" is NULL append at the end.
6364 * Return FAIL when out of memory.
6365 */
6366 static int
6367list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 list_T *l;
6369 typval_T *tv;
6370 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371{
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373
6374 if (ni == NULL)
6375 return FAIL;
6376 copy_tv(tv, &ni->li_tv);
6377 if (item == NULL)
6378 /* Append new item at end of list. */
6379 list_append(l, ni);
6380 else
6381 {
6382 /* Insert new item before existing item. */
6383 ni->li_prev = item->li_prev;
6384 ni->li_next = item;
6385 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006386 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006387 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006388 ++l->lv_idx;
6389 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006390 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006391 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006392 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006393 l->lv_idx_item = NULL;
6394 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397 }
6398 return OK;
6399}
6400
6401/*
6402 * Extend "l1" with "l2".
6403 * If "bef" is NULL append at the end, otherwise insert before this item.
6404 * Returns FAIL when out of memory.
6405 */
6406 static int
6407list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 list_T *l1;
6409 list_T *l2;
6410 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411{
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006413 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006415 /* We also quit the loop when we have inserted the original item count of
6416 * the list, avoid a hang when we extend a list with itself. */
6417 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6419 return FAIL;
6420 return OK;
6421}
6422
6423/*
6424 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6425 * Return FAIL when out of memory.
6426 */
6427 static int
6428list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 list_T *l1;
6430 list_T *l2;
6431 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006435 if (l1 == NULL || l2 == NULL)
6436 return FAIL;
6437
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 if (l == NULL)
6441 return FAIL;
6442 tv->v_type = VAR_LIST;
6443 tv->vval.v_list = l;
6444
6445 /* append all items from the second list */
6446 return list_extend(l, l2, NULL);
6447}
6448
6449/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006450 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006452 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 * Returns NULL when out of memory.
6454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006456list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460{
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 list_T *copy;
6462 listitem_T *item;
6463 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464
6465 if (orig == NULL)
6466 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467
6468 copy = list_alloc();
6469 if (copy != NULL)
6470 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 if (copyID != 0)
6472 {
6473 /* Do this before adding the items, because one of the items may
6474 * refer back to this list. */
6475 orig->lv_copyID = copyID;
6476 orig->lv_copylist = copy;
6477 }
6478 for (item = orig->lv_first; item != NULL && !got_int;
6479 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 {
6481 ni = listitem_alloc();
6482 if (ni == NULL)
6483 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 {
6486 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6487 {
6488 vim_free(ni);
6489 break;
6490 }
6491 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 list_append(copy, ni);
6495 }
6496 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 if (item != NULL)
6498 {
6499 list_unref(copy);
6500 copy = NULL;
6501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 }
6503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 return copy;
6505}
6506
6507/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006509 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006512list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 list_T *l;
6514 listitem_T *item;
6515 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516{
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 /* notify watchers */
6520 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 list_fix_watch(l, ip);
6524 if (ip == item2)
6525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527
6528 if (item2->li_next == NULL)
6529 l->lv_last = item->li_prev;
6530 else
6531 item2->li_next->li_prev = item->li_prev;
6532 if (item->li_prev == NULL)
6533 l->lv_first = item2->li_next;
6534 else
6535 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537}
6538
6539/*
6540 * Return an allocated string with the string representation of a list.
6541 * May return NULL.
6542 */
6543 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006544list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006546 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547{
6548 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549
6550 if (tv->vval.v_list == NULL)
6551 return NULL;
6552 ga_init2(&ga, (int)sizeof(char), 80);
6553 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006554 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 {
6556 vim_free(ga.ga_data);
6557 return NULL;
6558 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 ga_append(&ga, ']');
6560 ga_append(&ga, NUL);
6561 return (char_u *)ga.ga_data;
6562}
6563
6564/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565 * Join list "l" into a string in "*gap", using separator "sep".
6566 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006568 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006569 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006570list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006573 char_u *sep;
6574 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006575 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006576{
6577 int first = TRUE;
6578 char_u *tofree;
6579 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 char_u *s;
6582
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006584 {
6585 if (first)
6586 first = FALSE;
6587 else
6588 ga_concat(gap, sep);
6589
6590 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006591 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006592 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 if (s != NULL)
6595 ga_concat(gap, s);
6596 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 if (s == NULL)
6598 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006599 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006600 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006602}
6603
6604/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006605 * Garbage collection for lists and dictionaries.
6606 *
6607 * We use reference counts to be able to free most items right away when they
6608 * are no longer used. But for composite items it's possible that it becomes
6609 * unused while the reference count is > 0: When there is a recursive
6610 * reference. Example:
6611 * :let l = [1, 2, 3]
6612 * :let d = {9: l}
6613 * :let l[1] = d
6614 *
6615 * Since this is quite unusual we handle this with garbage collection: every
6616 * once in a while find out which lists and dicts are not referenced from any
6617 * variable.
6618 *
6619 * Here is a good reference text about garbage collection (refers to Python
6620 * but it applies to all reference-counting mechanisms):
6621 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006622 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006623
6624/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006625 * Do garbage collection for lists and dicts.
6626 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006627 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006628 int
6629garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006630{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006631 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006632 buf_T *buf;
6633 win_T *wp;
6634 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006635 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006636 int did_free;
6637 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006638#ifdef FEAT_WINDOWS
6639 tabpage_T *tp;
6640#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006641
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006642 /* Only do this once. */
6643 want_garbage_collect = FALSE;
6644 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006645 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006646
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006647 /* We advance by two because we add one for items referenced through
6648 * previous_funccal. */
6649 current_copyID += COPYID_INC;
6650 copyID = current_copyID;
6651
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006652 /*
6653 * 1. Go through all accessible variables and mark all lists and dicts
6654 * with copyID.
6655 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006656
6657 /* Don't free variables in the previous_funccal list unless they are only
6658 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006659 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006660 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6661 {
6662 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6663 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6664 }
6665
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006666 /* script-local variables */
6667 for (i = 1; i <= ga_scripts.ga_len; ++i)
6668 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6669
6670 /* buffer-local variables */
6671 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6672 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6673
6674 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006675 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6677
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006678#ifdef FEAT_WINDOWS
6679 /* tabpage-local variables */
6680 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6681 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6682#endif
6683
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 /* global variables */
6685 set_ref_in_ht(&globvarht, copyID);
6686
6687 /* function-local variables */
6688 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6689 {
6690 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6691 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6692 }
6693
Bram Moolenaard812df62008-11-09 12:46:09 +00006694 /* v: vars */
6695 set_ref_in_ht(&vimvarht, copyID);
6696
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006697 /*
6698 * 2. Free lists and dictionaries that are not referenced.
6699 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006700 did_free = free_unref_items(copyID);
6701
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006702 /*
6703 * 3. Check if any funccal can be freed now.
6704 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006705 for (pfc = &previous_funccal; *pfc != NULL; )
6706 {
6707 if (can_free_funccal(*pfc, copyID))
6708 {
6709 fc = *pfc;
6710 *pfc = fc->caller;
6711 free_funccal(fc, TRUE);
6712 did_free = TRUE;
6713 did_free_funccal = TRUE;
6714 }
6715 else
6716 pfc = &(*pfc)->caller;
6717 }
6718 if (did_free_funccal)
6719 /* When a funccal was freed some more items might be garbage
6720 * collected, so run again. */
6721 (void)garbage_collect();
6722
6723 return did_free;
6724}
6725
6726/*
6727 * Free lists and dictionaries that are no longer referenced.
6728 */
6729 static int
6730free_unref_items(copyID)
6731 int copyID;
6732{
6733 dict_T *dd;
6734 list_T *ll;
6735 int did_free = FALSE;
6736
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006738 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 */
6740 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006741 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006743 /* Free the Dictionary and ordinary items it contains, but don't
6744 * recurse into Lists and Dictionaries, they will be in the list
6745 * of dicts or list of lists. */
6746 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 did_free = TRUE;
6748
6749 /* restart, next dict may also have been freed */
6750 dd = first_dict;
6751 }
6752 else
6753 dd = dd->dv_used_next;
6754
6755 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006756 * Go through the list of lists and free items without the copyID.
6757 * But don't free a list that has a watcher (used in a for loop), these
6758 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 */
6760 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006761 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6762 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006764 /* Free the List and ordinary items it contains, but don't recurse
6765 * into Lists and Dictionaries, they will be in the list of dicts
6766 * or list of lists. */
6767 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 did_free = TRUE;
6769
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006770 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 ll = first_list;
6772 }
6773 else
6774 ll = ll->lv_used_next;
6775
6776 return did_free;
6777}
6778
6779/*
6780 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6781 */
6782 static void
6783set_ref_in_ht(ht, copyID)
6784 hashtab_T *ht;
6785 int copyID;
6786{
6787 int todo;
6788 hashitem_T *hi;
6789
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006790 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 for (hi = ht->ht_array; todo > 0; ++hi)
6792 if (!HASHITEM_EMPTY(hi))
6793 {
6794 --todo;
6795 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6796 }
6797}
6798
6799/*
6800 * Mark all lists and dicts referenced through list "l" with "copyID".
6801 */
6802 static void
6803set_ref_in_list(l, copyID)
6804 list_T *l;
6805 int copyID;
6806{
6807 listitem_T *li;
6808
6809 for (li = l->lv_first; li != NULL; li = li->li_next)
6810 set_ref_in_item(&li->li_tv, copyID);
6811}
6812
6813/*
6814 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6815 */
6816 static void
6817set_ref_in_item(tv, copyID)
6818 typval_T *tv;
6819 int copyID;
6820{
6821 dict_T *dd;
6822 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006823
6824 switch (tv->v_type)
6825 {
6826 case VAR_DICT:
6827 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006828 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 /* Didn't see this dict yet. */
6831 dd->dv_copyID = copyID;
6832 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835
6836 case VAR_LIST:
6837 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006838 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 /* Didn't see this list yet. */
6841 ll->lv_copyID = copyID;
6842 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006843 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006847}
6848
6849/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006850 * Allocate an empty header for a dictionary.
6851 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006852 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006853dict_alloc()
6854{
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006856
Bram Moolenaar33570922005-01-25 22:26:29 +00006857 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006858 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006859 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006860 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 if (first_dict != NULL)
6862 first_dict->dv_used_prev = d;
6863 d->dv_used_next = first_dict;
6864 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006865 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006868 d->dv_lock = 0;
6869 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006870 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006871 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006872 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873}
6874
6875/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006876 * Allocate an empty dict for a return value.
6877 * Returns OK or FAIL.
6878 */
6879 static int
6880rettv_dict_alloc(rettv)
6881 typval_T *rettv;
6882{
6883 dict_T *d = dict_alloc();
6884
6885 if (d == NULL)
6886 return FAIL;
6887
6888 rettv->vval.v_dict = d;
6889 rettv->v_type = VAR_DICT;
6890 ++d->dv_refcount;
6891 return OK;
6892}
6893
6894
6895/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006896 * Unreference a Dictionary: decrement the reference count and free it when it
6897 * becomes zero.
6898 */
6899 static void
6900dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006903 if (d != NULL && --d->dv_refcount <= 0)
6904 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006905}
6906
6907/*
6908 * Free a Dictionary, including all items it contains.
6909 * Ignores the reference count.
6910 */
6911 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912dict_free(d, recurse)
6913 dict_T *d;
6914 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006915{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006916 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006918 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006919
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 /* Remove the dict from the list of dicts for garbage collection. */
6921 if (d->dv_used_prev == NULL)
6922 first_dict = d->dv_used_next;
6923 else
6924 d->dv_used_prev->dv_used_next = d->dv_used_next;
6925 if (d->dv_used_next != NULL)
6926 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6927
6928 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006929 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006930 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006931 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006933 if (!HASHITEM_EMPTY(hi))
6934 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 /* Remove the item before deleting it, just in case there is
6936 * something recursive causing trouble. */
6937 di = HI2DI(hi);
6938 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006939 if (recurse || (di->di_tv.v_type != VAR_LIST
6940 && di->di_tv.v_type != VAR_DICT))
6941 clear_tv(&di->di_tv);
6942 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 --todo;
6944 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947 vim_free(d);
6948}
6949
6950/*
6951 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006952 * The "key" is copied to the new item.
6953 * Note that the value of the item "di_tv" still needs to be initialized!
6954 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006955 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006956 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006957dictitem_alloc(key)
6958 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006959{
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006961
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006962 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006964 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006966 di->di_flags = 0;
6967 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969}
6970
6971/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972 * Make a copy of a Dictionary item.
6973 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006977{
Bram Moolenaar33570922005-01-25 22:26:29 +00006978 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006979
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006980 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6981 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006982 if (di != NULL)
6983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006984 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006985 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 copy_tv(&org->di_tv, &di->di_tv);
6987 }
6988 return di;
6989}
6990
6991/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 * Remove item "item" from Dictionary "dict" and free it.
6993 */
6994 static void
6995dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006996 dict_T *dict;
6997 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006998{
Bram Moolenaar33570922005-01-25 22:26:29 +00006999 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007000
Bram Moolenaar33570922005-01-25 22:26:29 +00007001 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007002 if (HASHITEM_EMPTY(hi))
7003 EMSG2(_(e_intern2), "dictitem_remove()");
7004 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006 dictitem_free(item);
7007}
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Free a dict item. Also clears the value.
7011 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007012 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016 clear_tv(&item->di_tv);
7017 vim_free(item);
7018}
7019
7020/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007021 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7022 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007023 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 * Returns NULL when out of memory.
7025 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007027dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007028 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007029 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007030 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031{
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 dict_T *copy;
7033 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007034 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036
7037 if (orig == NULL)
7038 return NULL;
7039
7040 copy = dict_alloc();
7041 if (copy != NULL)
7042 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007043 if (copyID != 0)
7044 {
7045 orig->dv_copyID = copyID;
7046 orig->dv_copydict = copy;
7047 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007048 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007049 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007050 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007052 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053 --todo;
7054
7055 di = dictitem_alloc(hi->hi_key);
7056 if (di == NULL)
7057 break;
7058 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007059 {
7060 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7061 copyID) == FAIL)
7062 {
7063 vim_free(di);
7064 break;
7065 }
7066 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007067 else
7068 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7069 if (dict_add(copy, di) == FAIL)
7070 {
7071 dictitem_free(di);
7072 break;
7073 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007075 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076
Bram Moolenaare9a41262005-01-15 22:18:47 +00007077 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007078 if (todo > 0)
7079 {
7080 dict_unref(copy);
7081 copy = NULL;
7082 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007083 }
7084
7085 return copy;
7086}
7087
7088/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007090 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007092 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007094 dict_T *d;
7095 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096{
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098}
7099
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007101 * Add a number or string entry to dictionary "d".
7102 * When "str" is NULL use number "nr", otherwise use "str".
7103 * Returns FAIL when out of memory and when key already exists.
7104 */
7105 int
7106dict_add_nr_str(d, key, nr, str)
7107 dict_T *d;
7108 char *key;
7109 long nr;
7110 char_u *str;
7111{
7112 dictitem_T *item;
7113
7114 item = dictitem_alloc((char_u *)key);
7115 if (item == NULL)
7116 return FAIL;
7117 item->di_tv.v_lock = 0;
7118 if (str == NULL)
7119 {
7120 item->di_tv.v_type = VAR_NUMBER;
7121 item->di_tv.vval.v_number = nr;
7122 }
7123 else
7124 {
7125 item->di_tv.v_type = VAR_STRING;
7126 item->di_tv.vval.v_string = vim_strsave(str);
7127 }
7128 if (dict_add(d, item) == FAIL)
7129 {
7130 dictitem_free(item);
7131 return FAIL;
7132 }
7133 return OK;
7134}
7135
7136/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007137 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007138 * Returns FAIL when out of memory and when key already exists.
7139 */
7140 int
7141dict_add_list(d, key, list)
7142 dict_T *d;
7143 char *key;
7144 list_T *list;
7145{
7146 dictitem_T *item;
7147
7148 item = dictitem_alloc((char_u *)key);
7149 if (item == NULL)
7150 return FAIL;
7151 item->di_tv.v_lock = 0;
7152 item->di_tv.v_type = VAR_LIST;
7153 item->di_tv.vval.v_list = list;
7154 if (dict_add(d, item) == FAIL)
7155 {
7156 dictitem_free(item);
7157 return FAIL;
7158 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007159 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007160 return OK;
7161}
7162
7163/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 * Get the number of items in a Dictionary.
7165 */
7166 static long
7167dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 if (d == NULL)
7171 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007172 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173}
7174
7175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 * Find item "key[len]" in Dictionary "d".
7177 * If "len" is negative use strlen(key).
7178 * Returns NULL when not found.
7179 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007180 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183 char_u *key;
7184 int len;
7185{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186#define AKEYLEN 200
7187 char_u buf[AKEYLEN];
7188 char_u *akey;
7189 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 if (len < 0)
7193 akey = key;
7194 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 tofree = akey = vim_strnsave(key, len);
7197 if (akey == NULL)
7198 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 else
7201 {
7202 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007203 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204 akey = buf;
7205 }
7206
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 vim_free(tofree);
7209 if (HASHITEM_EMPTY(hi))
7210 return NULL;
7211 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212}
7213
7214/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007215 * Get a string item from a dictionary.
7216 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007217 * Returns NULL if the entry doesn't exist or out of memory.
7218 */
7219 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007220get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007221 dict_T *d;
7222 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007223 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007224{
7225 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007226 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007227
7228 di = dict_find(d, key, -1);
7229 if (di == NULL)
7230 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007231 s = get_tv_string(&di->di_tv);
7232 if (save && s != NULL)
7233 s = vim_strsave(s);
7234 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007235}
7236
7237/*
7238 * Get a number item from a dictionary.
7239 * Returns 0 if the entry doesn't exist or out of memory.
7240 */
7241 long
7242get_dict_number(d, key)
7243 dict_T *d;
7244 char_u *key;
7245{
7246 dictitem_T *di;
7247
7248 di = dict_find(d, key, -1);
7249 if (di == NULL)
7250 return 0;
7251 return get_tv_number(&di->di_tv);
7252}
7253
7254/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 * Return an allocated string with the string representation of a Dictionary.
7256 * May return NULL.
7257 */
7258 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007259dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007260 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007261 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262{
7263 garray_T ga;
7264 int first = TRUE;
7265 char_u *tofree;
7266 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 return NULL;
7274 ga_init2(&ga, (int)sizeof(char), 80);
7275 ga_append(&ga, '{');
7276
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007277 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007278 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 --todo;
7283
7284 if (first)
7285 first = FALSE;
7286 else
7287 ga_concat(&ga, (char_u *)", ");
7288
7289 tofree = string_quote(hi->hi_key, FALSE);
7290 if (tofree != NULL)
7291 {
7292 ga_concat(&ga, tofree);
7293 vim_free(tofree);
7294 }
7295 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007296 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 if (s != NULL)
7298 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007300 if (s == NULL)
7301 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007304 if (todo > 0)
7305 {
7306 vim_free(ga.ga_data);
7307 return NULL;
7308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309
7310 ga_append(&ga, '}');
7311 ga_append(&ga, NUL);
7312 return (char_u *)ga.ga_data;
7313}
7314
7315/*
7316 * Allocate a variable for a Dictionary and fill it from "*arg".
7317 * Return OK or FAIL. Returns NOTDONE for {expr}.
7318 */
7319 static int
7320get_dict_tv(arg, rettv, evaluate)
7321 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323 int evaluate;
7324{
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dict_T *d = NULL;
7326 typval_T tvkey;
7327 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007328 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332
7333 /*
7334 * First check if it's not a curly-braces thing: {expr}.
7335 * Must do this without evaluating, otherwise a function may be called
7336 * twice. Unfortunately this means we need to call eval1() twice for the
7337 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 if (*start != '}')
7341 {
7342 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7343 return FAIL;
7344 if (*start == '}')
7345 return NOTDONE;
7346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347
7348 if (evaluate)
7349 {
7350 d = dict_alloc();
7351 if (d == NULL)
7352 return FAIL;
7353 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354 tvkey.v_type = VAR_UNKNOWN;
7355 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356
7357 *arg = skipwhite(*arg + 1);
7358 while (**arg != '}' && **arg != NUL)
7359 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 goto failret;
7362 if (**arg != ':')
7363 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007364 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 goto failret;
7367 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007368 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007370 key = get_tv_string_buf_chk(&tvkey, buf);
7371 if (key == NULL || *key == NUL)
7372 {
7373 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7374 if (key != NULL)
7375 EMSG(_(e_emptykey));
7376 clear_tv(&tvkey);
7377 goto failret;
7378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380
7381 *arg = skipwhite(*arg + 1);
7382 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7383 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007384 if (evaluate)
7385 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 goto failret;
7387 }
7388 if (evaluate)
7389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 if (item != NULL)
7392 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007393 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395 clear_tv(&tv);
7396 goto failret;
7397 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 item = dictitem_alloc(key);
7399 clear_tv(&tvkey);
7400 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007403 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 if (dict_add(d, item) == FAIL)
7405 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 }
7407 }
7408
7409 if (**arg == '}')
7410 break;
7411 if (**arg != ',')
7412 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007413 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 goto failret;
7415 }
7416 *arg = skipwhite(*arg + 1);
7417 }
7418
7419 if (**arg != '}')
7420 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007421 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422failret:
7423 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007424 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 return FAIL;
7426 }
7427
7428 *arg = skipwhite(*arg + 1);
7429 if (evaluate)
7430 {
7431 rettv->v_type = VAR_DICT;
7432 rettv->vval.v_dict = d;
7433 ++d->dv_refcount;
7434 }
7435
7436 return OK;
7437}
7438
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007440 * Return a string with the string representation of a variable.
7441 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007442 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007443 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007444 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007445 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007446 */
7447 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007448echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007451 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007452 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007453{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454 static int recurse = 0;
7455 char_u *r = NULL;
7456
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007459 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 *tofree = NULL;
7461 return NULL;
7462 }
7463 ++recurse;
7464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007465 switch (tv->v_type)
7466 {
7467 case VAR_FUNC:
7468 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 r = tv->vval.v_string;
7470 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007473 if (tv->vval.v_list == NULL)
7474 {
7475 *tofree = NULL;
7476 r = NULL;
7477 }
7478 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7479 {
7480 *tofree = NULL;
7481 r = (char_u *)"[...]";
7482 }
7483 else
7484 {
7485 tv->vval.v_list->lv_copyID = copyID;
7486 *tofree = list2string(tv, copyID);
7487 r = *tofree;
7488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007490
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007492 if (tv->vval.v_dict == NULL)
7493 {
7494 *tofree = NULL;
7495 r = NULL;
7496 }
7497 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7498 {
7499 *tofree = NULL;
7500 r = (char_u *)"{...}";
7501 }
7502 else
7503 {
7504 tv->vval.v_dict->dv_copyID = copyID;
7505 *tofree = dict2string(tv, copyID);
7506 r = *tofree;
7507 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 case VAR_STRING:
7511 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 *tofree = NULL;
7513 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007514 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007515
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007516#ifdef FEAT_FLOAT
7517 case VAR_FLOAT:
7518 *tofree = NULL;
7519 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7520 r = numbuf;
7521 break;
7522#endif
7523
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007525 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007526 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528
7529 --recurse;
7530 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007531}
7532
7533/*
7534 * Return a string with the string representation of a variable.
7535 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7536 * "numbuf" is used for a number.
7537 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007538 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007539 */
7540 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007541tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007543 char_u **tofree;
7544 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007545 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007546{
7547 switch (tv->v_type)
7548 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007549 case VAR_FUNC:
7550 *tofree = string_quote(tv->vval.v_string, TRUE);
7551 return *tofree;
7552 case VAR_STRING:
7553 *tofree = string_quote(tv->vval.v_string, FALSE);
7554 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007555#ifdef FEAT_FLOAT
7556 case VAR_FLOAT:
7557 *tofree = NULL;
7558 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7559 return numbuf;
7560#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007562 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007565 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007566 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007568 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569}
7570
7571/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 * Return string "str" in ' quotes, doubling ' characters.
7573 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007575 */
7576 static char_u *
7577string_quote(str, function)
7578 char_u *str;
7579 int function;
7580{
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007582 char_u *p, *r, *s;
7583
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 len = (function ? 13 : 3);
7585 if (str != NULL)
7586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007587 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 for (p = str; *p != NUL; mb_ptr_adv(p))
7589 if (*p == '\'')
7590 ++len;
7591 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 s = r = alloc(len);
7593 if (r != NULL)
7594 {
7595 if (function)
7596 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007598 r += 10;
7599 }
7600 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 if (str != NULL)
7603 for (p = str; *p != NUL; )
7604 {
7605 if (*p == '\'')
7606 *r++ = '\'';
7607 MB_COPY_CHAR(p, r);
7608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007610 if (function)
7611 *r++ = ')';
7612 *r++ = NUL;
7613 }
7614 return s;
7615}
7616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007617#ifdef FEAT_FLOAT
7618/*
7619 * Convert the string "text" to a floating point number.
7620 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7621 * this always uses a decimal point.
7622 * Returns the length of the text that was consumed.
7623 */
7624 static int
7625string2float(text, value)
7626 char_u *text;
7627 float_T *value; /* result stored here */
7628{
7629 char *s = (char *)text;
7630 float_T f;
7631
7632 f = strtod(s, &s);
7633 *value = f;
7634 return (int)((char_u *)s - text);
7635}
7636#endif
7637
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 * Get the value of an environment variable.
7640 * "arg" is pointing to the '$'. It is advanced to after the name.
7641 * If the environment variable was not set, silently assume it is empty.
7642 * Always return OK.
7643 */
7644 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 int evaluate;
7649{
7650 char_u *string = NULL;
7651 int len;
7652 int cc;
7653 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007654 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
7656 ++*arg;
7657 name = *arg;
7658 len = get_env_len(arg);
7659 if (evaluate)
7660 {
7661 if (len != 0)
7662 {
7663 cc = name[len];
7664 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007665 /* first try vim_getenv(), fast for normal environment vars */
7666 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007668 {
7669 if (!mustfree)
7670 string = vim_strsave(string);
7671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 else
7673 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007674 if (mustfree)
7675 vim_free(string);
7676
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 /* next try expanding things like $VIM and ${HOME} */
7678 string = expand_env_save(name - 1);
7679 if (string != NULL && *string == '$')
7680 {
7681 vim_free(string);
7682 string = NULL;
7683 }
7684 }
7685 name[len] = cc;
7686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->v_type = VAR_STRING;
7688 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
7690
7691 return OK;
7692}
7693
7694/*
7695 * Array with names and number of arguments of all internal functions
7696 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7697 */
7698static struct fst
7699{
7700 char *f_name; /* function name */
7701 char f_min_argc; /* minimal number of arguments */
7702 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007704 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705} functions[] =
7706{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007707#ifdef FEAT_FLOAT
7708 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007709 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007710#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"append", 2, 2, f_append},
7713 {"argc", 0, 0, f_argc},
7714 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007715 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007717 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007718 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007719 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007722 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"bufexists", 1, 1, f_bufexists},
7724 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7725 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7726 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7727 {"buflisted", 1, 1, f_buflisted},
7728 {"bufloaded", 1, 1, f_bufloaded},
7729 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007730 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {"bufwinnr", 1, 1, f_bufwinnr},
7732 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007733 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007734 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007735#ifdef FEAT_FLOAT
7736 {"ceil", 1, 1, f_ceil},
7737#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007738 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"char2nr", 1, 1, f_char2nr},
7740 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007741 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007743#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007744 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007745 {"complete_add", 1, 1, f_complete_add},
7746 {"complete_check", 0, 0, f_complete_check},
7747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007750#ifdef FEAT_FLOAT
7751 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007752 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007753#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007754 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007756 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007757 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {"delete", 1, 1, f_delete},
7759 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007760 {"diff_filler", 1, 1, f_diff_filler},
7761 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007762 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007764 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"eventhandler", 0, 0, f_eventhandler},
7766 {"executable", 1, 1, f_executable},
7767 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007768#ifdef FEAT_FLOAT
7769 {"exp", 1, 1, f_exp},
7770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007772 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007773 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7775 {"filereadable", 1, 1, f_filereadable},
7776 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007777 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007778 {"finddir", 1, 3, f_finddir},
7779 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007780#ifdef FEAT_FLOAT
7781 {"float2nr", 1, 1, f_float2nr},
7782 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007783 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007784#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007785 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 {"fnamemodify", 2, 2, f_fnamemodify},
7787 {"foldclosed", 1, 1, f_foldclosed},
7788 {"foldclosedend", 1, 1, f_foldclosedend},
7789 {"foldlevel", 1, 1, f_foldlevel},
7790 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007791 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007794 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007795 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007796 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 {"getbufvar", 2, 2, f_getbufvar},
7798 {"getchar", 0, 1, f_getchar},
7799 {"getcharmod", 0, 0, f_getcharmod},
7800 {"getcmdline", 0, 0, f_getcmdline},
7801 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007802 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007804 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007805 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {"getfsize", 1, 1, f_getfsize},
7807 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007808 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007809 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007810 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007811 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007812 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007813 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007814 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007815 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007817 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007818 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"getwinposx", 0, 0, f_getwinposx},
7820 {"getwinposy", 0, 0, f_getwinposy},
7821 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007822 {"glob", 1, 2, f_glob},
7823 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007826 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007827 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7829 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7830 {"histadd", 2, 2, f_histadd},
7831 {"histdel", 1, 2, f_histdel},
7832 {"histget", 1, 2, f_histget},
7833 {"histnr", 1, 1, f_histnr},
7834 {"hlID", 1, 1, f_hlID},
7835 {"hlexists", 1, 1, f_hlexists},
7836 {"hostname", 0, 0, f_hostname},
7837 {"iconv", 3, 3, f_iconv},
7838 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007839 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007840 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007842 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"inputrestore", 0, 0, f_inputrestore},
7844 {"inputsave", 0, 0, f_inputsave},
7845 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007848 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007849 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007850 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007853 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"libcall", 3, 3, f_libcall},
7855 {"libcallnr", 3, 3, f_libcallnr},
7856 {"line", 1, 1, f_line},
7857 {"line2byte", 1, 1, f_line2byte},
7858 {"lispindent", 1, 1, f_lispindent},
7859 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007860#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007861 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862 {"log10", 1, 1, f_log10},
7863#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007864 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007865 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007866 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007867 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007868 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007869 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007871 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007872 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007873 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007874 {"max", 1, 1, f_max},
7875 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007876#ifdef vim_mkdir
7877 {"mkdir", 1, 3, f_mkdir},
7878#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007880#ifdef FEAT_MZSCHEME
7881 {"mzeval", 1, 1, f_mzeval},
7882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"nextnonblank", 1, 1, f_nextnonblank},
7884 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007885 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"pow", 2, 2, f_pow},
7888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007890 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007891 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007892 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007893 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007894 {"reltime", 0, 2, f_reltime},
7895 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"remote_expr", 2, 3, f_remote_expr},
7897 {"remote_foreground", 1, 1, f_remote_foreground},
7898 {"remote_peek", 1, 2, f_remote_peek},
7899 {"remote_read", 1, 1, f_remote_read},
7900 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007903 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007905 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906#ifdef FEAT_FLOAT
7907 {"round", 1, 1, f_round},
7908#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007909 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007910 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007911 {"searchpair", 3, 7, f_searchpair},
7912 {"searchpairpos", 3, 7, f_searchpairpos},
7913 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"server2client", 2, 2, f_server2client},
7915 {"serverlist", 0, 0, f_serverlist},
7916 {"setbufvar", 3, 3, f_setbufvar},
7917 {"setcmdpos", 1, 1, f_setcmdpos},
7918 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007919 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007920 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007921 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007922 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007924 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007925 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007927 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007929#ifdef FEAT_FLOAT
7930 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007931 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007932#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02007933 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007934 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007935 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007936 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007937 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007938#ifdef FEAT_FLOAT
7939 {"sqrt", 1, 1, f_sqrt},
7940 {"str2float", 1, 1, f_str2float},
7941#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007942 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007943 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007944 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945#ifdef HAVE_STRFTIME
7946 {"strftime", 1, 2, f_strftime},
7947#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"strlen", 1, 1, f_strlen},
7951 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007952 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007954 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {"submatch", 1, 1, f_submatch},
7956 {"substitute", 4, 4, f_substitute},
7957 {"synID", 3, 3, f_synID},
7958 {"synIDattr", 2, 3, f_synIDattr},
7959 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007960 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007961 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007962 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007963 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007964 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007965 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007966 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007967 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007968#ifdef FEAT_FLOAT
7969 {"tan", 1, 1, f_tan},
7970 {"tanh", 1, 1, f_tanh},
7971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007973 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"tolower", 1, 1, f_tolower},
7975 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007976 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007977#ifdef FEAT_FLOAT
7978 {"trunc", 1, 1, f_trunc},
7979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007981 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007982 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007983 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"virtcol", 1, 1, f_virtcol},
7985 {"visualmode", 0, 1, f_visualmode},
7986 {"winbufnr", 1, 1, f_winbufnr},
7987 {"wincol", 0, 0, f_wincol},
7988 {"winheight", 1, 1, f_winheight},
7989 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007990 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007992 {"winrestview", 1, 1, f_winrestview},
7993 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007995 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996};
7997
7998#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7999
8000/*
8001 * Function given to ExpandGeneric() to obtain the list of internal
8002 * or user defined function names.
8003 */
8004 char_u *
8005get_function_name(xp, idx)
8006 expand_T *xp;
8007 int idx;
8008{
8009 static int intidx = -1;
8010 char_u *name;
8011
8012 if (idx == 0)
8013 intidx = -1;
8014 if (intidx < 0)
8015 {
8016 name = get_user_func_name(xp, idx);
8017 if (name != NULL)
8018 return name;
8019 }
8020 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8021 {
8022 STRCPY(IObuff, functions[intidx].f_name);
8023 STRCAT(IObuff, "(");
8024 if (functions[intidx].f_max_argc == 0)
8025 STRCAT(IObuff, ")");
8026 return IObuff;
8027 }
8028
8029 return NULL;
8030}
8031
8032/*
8033 * Function given to ExpandGeneric() to obtain the list of internal or
8034 * user defined variable or function names.
8035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 char_u *
8037get_expr_name(xp, idx)
8038 expand_T *xp;
8039 int idx;
8040{
8041 static int intidx = -1;
8042 char_u *name;
8043
8044 if (idx == 0)
8045 intidx = -1;
8046 if (intidx < 0)
8047 {
8048 name = get_function_name(xp, idx);
8049 if (name != NULL)
8050 return name;
8051 }
8052 return get_user_var_name(xp, ++intidx);
8053}
8054
8055#endif /* FEAT_CMDL_COMPL */
8056
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008057#if defined(EBCDIC) || defined(PROTO)
8058/*
8059 * Compare struct fst by function name.
8060 */
8061 static int
8062compare_func_name(s1, s2)
8063 const void *s1;
8064 const void *s2;
8065{
8066 struct fst *p1 = (struct fst *)s1;
8067 struct fst *p2 = (struct fst *)s2;
8068
8069 return STRCMP(p1->f_name, p2->f_name);
8070}
8071
8072/*
8073 * Sort the function table by function name.
8074 * The sorting of the table above is ASCII dependant.
8075 * On machines using EBCDIC we have to sort it.
8076 */
8077 static void
8078sortFunctions()
8079{
8080 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8081
8082 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8083}
8084#endif
8085
8086
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087/*
8088 * Find internal function in table above.
8089 * Return index, or -1 if not found
8090 */
8091 static int
8092find_internal_func(name)
8093 char_u *name; /* name of the function */
8094{
8095 int first = 0;
8096 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8097 int cmp;
8098 int x;
8099
8100 /*
8101 * Find the function name in the table. Binary search.
8102 */
8103 while (first <= last)
8104 {
8105 x = first + ((unsigned)(last - first) >> 1);
8106 cmp = STRCMP(name, functions[x].f_name);
8107 if (cmp < 0)
8108 last = x - 1;
8109 else if (cmp > 0)
8110 first = x + 1;
8111 else
8112 return x;
8113 }
8114 return -1;
8115}
8116
8117/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008118 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8119 * name it contains, otherwise return "name".
8120 */
8121 static char_u *
8122deref_func_name(name, lenp)
8123 char_u *name;
8124 int *lenp;
8125{
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008127 int cc;
8128
8129 cc = name[*lenp];
8130 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008131 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136 {
8137 *lenp = 0;
8138 return (char_u *)""; /* just in case */
8139 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008140 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 }
8143
8144 return name;
8145}
8146
8147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 * Allocate a variable for the result of a function.
8149 * Return OK or FAIL.
8150 */
8151 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008152get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8153 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 char_u *name; /* name of the function */
8155 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 char_u **arg; /* argument, pointing to the '(' */
8158 linenr_T firstline; /* first line of range */
8159 linenr_T lastline; /* last line of range */
8160 int *doesrange; /* return: function handled range */
8161 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
8164 char_u *argp;
8165 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008166 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 int argcount = 0; /* number of arguments found */
8168
8169 /*
8170 * Get the arguments.
8171 */
8172 argp = *arg;
8173 while (argcount < MAX_FUNC_ARGS)
8174 {
8175 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8176 if (*argp == ')' || *argp == ',' || *argp == NUL)
8177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8179 {
8180 ret = FAIL;
8181 break;
8182 }
8183 ++argcount;
8184 if (*argp != ',')
8185 break;
8186 }
8187 if (*argp == ')')
8188 ++argp;
8189 else
8190 ret = FAIL;
8191
8192 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008193 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008196 {
8197 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008198 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008200 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202
8203 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008204 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
8206 *arg = skipwhite(argp);
8207 return ret;
8208}
8209
8210
8211/*
8212 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008213 * Return OK when the function can't be called, FAIL otherwise.
8214 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 */
8216 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008217call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008219 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008223 typval_T *argvars; /* vars for arguments, must have "argcount"
8224 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 linenr_T firstline; /* first line of range */
8226 linenr_T lastline; /* last line of range */
8227 int *doesrange; /* return: function handled range */
8228 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
8231 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232#define ERROR_UNKNOWN 0
8233#define ERROR_TOOMANY 1
8234#define ERROR_TOOFEW 2
8235#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008236#define ERROR_DICT 4
8237#define ERROR_NONE 5
8238#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 int error = ERROR_NONE;
8240 int i;
8241 int llen;
8242 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243#define FLEN_FIXED 40
8244 char_u fname_buf[FLEN_FIXED + 1];
8245 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008246 char_u *name;
8247
8248 /* Make a copy of the name, if it comes from a funcref variable it could
8249 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008250 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008251 if (name == NULL)
8252 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 /*
8255 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8256 * Change <SNR>123_name() to K_SNR 123_name().
8257 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 llen = eval_fname_script(name);
8260 if (llen > 0)
8261 {
8262 fname_buf[0] = K_SPECIAL;
8263 fname_buf[1] = KS_EXTRA;
8264 fname_buf[2] = (int)KE_SNR;
8265 i = 3;
8266 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8267 {
8268 if (current_SID <= 0)
8269 error = ERROR_SCRIPT;
8270 else
8271 {
8272 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8273 i = (int)STRLEN(fname_buf);
8274 }
8275 }
8276 if (i + STRLEN(name + llen) < FLEN_FIXED)
8277 {
8278 STRCPY(fname_buf + i, name + llen);
8279 fname = fname_buf;
8280 }
8281 else
8282 {
8283 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8284 if (fname == NULL)
8285 error = ERROR_OTHER;
8286 else
8287 {
8288 mch_memmove(fname, fname_buf, (size_t)i);
8289 STRCPY(fname + i, name + llen);
8290 }
8291 }
8292 }
8293 else
8294 fname = name;
8295
8296 *doesrange = FALSE;
8297
8298
8299 /* execute the function if no errors detected and executing */
8300 if (evaluate && error == ERROR_NONE)
8301 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008302 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8303 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 error = ERROR_UNKNOWN;
8305
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008306 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 /*
8309 * User defined function.
8310 */
8311 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008314 /* Trigger FuncUndefined event, may load the function. */
8315 if (fp == NULL
8316 && apply_autocmds(EVENT_FUNCUNDEFINED,
8317 fname, fname, TRUE, NULL)
8318 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008320 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 fp = find_func(fname);
8322 }
8323#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008324 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008325 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008326 {
8327 /* loaded a package, search for the function again */
8328 fp = find_func(fname);
8329 }
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (fp != NULL)
8332 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008333 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008335 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008337 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008339 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008340 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 else
8342 {
8343 /*
8344 * Call the user function.
8345 * Save and restore search patterns, script variables and
8346 * redo buffer.
8347 */
8348 save_search_patterns();
8349 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008350 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008351 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008352 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008353 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8354 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8355 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008356 /* Function was unreferenced while being used, free it
8357 * now. */
8358 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 restoreRedobuff();
8360 restore_search_patterns();
8361 error = ERROR_NONE;
8362 }
8363 }
8364 }
8365 else
8366 {
8367 /*
8368 * Find the function name in the table, call its implementation.
8369 */
8370 i = find_internal_func(fname);
8371 if (i >= 0)
8372 {
8373 if (argcount < functions[i].f_min_argc)
8374 error = ERROR_TOOFEW;
8375 else if (argcount > functions[i].f_max_argc)
8376 error = ERROR_TOOMANY;
8377 else
8378 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008379 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 error = ERROR_NONE;
8382 }
8383 }
8384 }
8385 /*
8386 * The function call (or "FuncUndefined" autocommand sequence) might
8387 * have been aborted by an error, an interrupt, or an explicitly thrown
8388 * exception that has not been caught so far. This situation can be
8389 * tested for by calling aborting(). For an error in an internal
8390 * function or for the "E132" error in call_user_func(), however, the
8391 * throw point at which the "force_abort" flag (temporarily reset by
8392 * emsg()) is normally updated has not been reached yet. We need to
8393 * update that flag first to make aborting() reliable.
8394 */
8395 update_force_abort();
8396 }
8397 if (error == ERROR_NONE)
8398 ret = OK;
8399
8400 /*
8401 * Report an error unless the argument evaluation or function call has been
8402 * cancelled due to an aborting error, an interrupt, or an exception.
8403 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008404 if (!aborting())
8405 {
8406 switch (error)
8407 {
8408 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008409 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008410 break;
8411 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008412 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008413 break;
8414 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008415 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 name);
8417 break;
8418 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008419 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008420 name);
8421 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008423 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 name);
8425 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008426 }
8427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (fname != name && fname != fname_buf)
8430 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008431 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
8433 return ret;
8434}
8435
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008436/*
8437 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008438 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439 */
8440 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008441emsg_funcname(ermsg, name)
8442 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008443 char_u *name;
8444{
8445 char_u *p;
8446
8447 if (*name == K_SPECIAL)
8448 p = concat_str((char_u *)"<SNR>", name + 3);
8449 else
8450 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008451 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008452 if (p != name)
8453 vim_free(p);
8454}
8455
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008456/*
8457 * Return TRUE for a non-zero Number and a non-empty String.
8458 */
8459 static int
8460non_zero_arg(argvars)
8461 typval_T *argvars;
8462{
8463 return ((argvars[0].v_type == VAR_NUMBER
8464 && argvars[0].vval.v_number != 0)
8465 || (argvars[0].v_type == VAR_STRING
8466 && argvars[0].vval.v_string != NULL
8467 && *argvars[0].vval.v_string != NUL));
8468}
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470/*********************************************
8471 * Implementation of the built-in functions
8472 */
8473
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008475static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8476
8477/*
8478 * Get the float value of "argvars[0]" into "f".
8479 * Returns FAIL when the argument is not a Number or Float.
8480 */
8481 static int
8482get_float_arg(argvars, f)
8483 typval_T *argvars;
8484 float_T *f;
8485{
8486 if (argvars[0].v_type == VAR_FLOAT)
8487 {
8488 *f = argvars[0].vval.v_float;
8489 return OK;
8490 }
8491 if (argvars[0].v_type == VAR_NUMBER)
8492 {
8493 *f = (float_T)argvars[0].vval.v_number;
8494 return OK;
8495 }
8496 EMSG(_("E808: Number or Float required"));
8497 return FAIL;
8498}
8499
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008500/*
8501 * "abs(expr)" function
8502 */
8503 static void
8504f_abs(argvars, rettv)
8505 typval_T *argvars;
8506 typval_T *rettv;
8507{
8508 if (argvars[0].v_type == VAR_FLOAT)
8509 {
8510 rettv->v_type = VAR_FLOAT;
8511 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8512 }
8513 else
8514 {
8515 varnumber_T n;
8516 int error = FALSE;
8517
8518 n = get_tv_number_chk(&argvars[0], &error);
8519 if (error)
8520 rettv->vval.v_number = -1;
8521 else if (n > 0)
8522 rettv->vval.v_number = n;
8523 else
8524 rettv->vval.v_number = -n;
8525 }
8526}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008527
8528/*
8529 * "acos()" function
8530 */
8531 static void
8532f_acos(argvars, rettv)
8533 typval_T *argvars;
8534 typval_T *rettv;
8535{
8536 float_T f;
8537
8538 rettv->v_type = VAR_FLOAT;
8539 if (get_float_arg(argvars, &f) == OK)
8540 rettv->vval.v_float = acos(f);
8541 else
8542 rettv->vval.v_float = 0.0;
8543}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008544#endif
8545
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008547 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 */
8549 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008550f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 typval_T *argvars;
8552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
Bram Moolenaar33570922005-01-25 22:26:29 +00008554 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008556 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008559 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008560 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008561 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 }
8564 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008565 EMSG(_(e_listreq));
8566}
8567
8568/*
8569 * "append(lnum, string/list)" function
8570 */
8571 static void
8572f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 typval_T *argvars;
8574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008575{
8576 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008577 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 list_T *l = NULL;
8579 listitem_T *li = NULL;
8580 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008581 long added = 0;
8582
Bram Moolenaar0d660222005-01-07 21:51:51 +00008583 lnum = get_tv_lnum(argvars);
8584 if (lnum >= 0
8585 && lnum <= curbuf->b_ml.ml_line_count
8586 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008587 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008588 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008589 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008590 l = argvars[1].vval.v_list;
8591 if (l == NULL)
8592 return;
8593 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008594 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008595 for (;;)
8596 {
8597 if (l == NULL)
8598 tv = &argvars[1]; /* append a string */
8599 else if (li == NULL)
8600 break; /* end of list */
8601 else
8602 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 line = get_tv_string_chk(tv);
8604 if (line == NULL) /* type error */
8605 {
8606 rettv->vval.v_number = 1; /* Failed */
8607 break;
8608 }
8609 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008610 ++added;
8611 if (l == NULL)
8612 break;
8613 li = li->li_next;
8614 }
8615
8616 appended_lines_mark(lnum, added);
8617 if (curwin->w_cursor.lnum > lnum)
8618 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 else
8621 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622}
8623
8624/*
8625 * "argc()" function
8626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008629 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
8636 * "argidx()" function
8637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008640 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645
8646/*
8647 * "argv(nr)" function
8648 */
8649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 typval_T *argvars;
8652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
8654 int idx;
8655
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008656 if (argvars[0].v_type != VAR_UNKNOWN)
8657 {
8658 idx = get_tv_number_chk(&argvars[0], NULL);
8659 if (idx >= 0 && idx < ARGCOUNT)
8660 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8661 else
8662 rettv->vval.v_string = NULL;
8663 rettv->v_type = VAR_STRING;
8664 }
8665 else if (rettv_list_alloc(rettv) == OK)
8666 for (idx = 0; idx < ARGCOUNT; ++idx)
8667 list_append_string(rettv->vval.v_list,
8668 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008671#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008672/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008673 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008674 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008675 static void
8676f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008678 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008679{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008680 float_T f;
8681
8682 rettv->v_type = VAR_FLOAT;
8683 if (get_float_arg(argvars, &f) == OK)
8684 rettv->vval.v_float = asin(f);
8685 else
8686 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008687}
8688
8689/*
8690 * "atan()" function
8691 */
8692 static void
8693f_atan(argvars, rettv)
8694 typval_T *argvars;
8695 typval_T *rettv;
8696{
8697 float_T f;
8698
8699 rettv->v_type = VAR_FLOAT;
8700 if (get_float_arg(argvars, &f) == OK)
8701 rettv->vval.v_float = atan(f);
8702 else
8703 rettv->vval.v_float = 0.0;
8704}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008705
8706/*
8707 * "atan2()" function
8708 */
8709 static void
8710f_atan2(argvars, rettv)
8711 typval_T *argvars;
8712 typval_T *rettv;
8713{
8714 float_T fx, fy;
8715
8716 rettv->v_type = VAR_FLOAT;
8717 if (get_float_arg(argvars, &fx) == OK
8718 && get_float_arg(&argvars[1], &fy) == OK)
8719 rettv->vval.v_float = atan2(fx, fy);
8720 else
8721 rettv->vval.v_float = 0.0;
8722}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008723#endif
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725/*
8726 * "browse(save, title, initdir, default)" function
8727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733#ifdef FEAT_BROWSE
8734 int save;
8735 char_u *title;
8736 char_u *initdir;
8737 char_u *defname;
8738 char_u buf[NUMBUFLEN];
8739 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 save = get_tv_number_chk(&argvars[0], &error);
8743 title = get_tv_string_chk(&argvars[1]);
8744 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8745 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 if (error || title == NULL || initdir == NULL || defname == NULL)
8748 rettv->vval.v_string = NULL;
8749 else
8750 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008751 do_browse(save ? BROWSE_SAVE : 0,
8752 title, defname, NULL, initdir, NULL, curbuf);
8753#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008755#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008757}
8758
8759/*
8760 * "browsedir(title, initdir)" function
8761 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008764 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008766{
8767#ifdef FEAT_BROWSE
8768 char_u *title;
8769 char_u *initdir;
8770 char_u buf[NUMBUFLEN];
8771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 title = get_tv_string_chk(&argvars[0]);
8773 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 if (title == NULL || initdir == NULL)
8776 rettv->vval.v_string = NULL;
8777 else
8778 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008779 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
Bram Moolenaar33570922005-01-25 22:26:29 +00008786static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008787
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788/*
8789 * Find a buffer by number or exact name.
8790 */
8791 static buf_T *
8792find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008793 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008797 if (avar->v_type == VAR_NUMBER)
8798 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008799 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008801 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008802 if (buf == NULL)
8803 {
8804 /* No full path name match, try a match with a URL or a "nofile"
8805 * buffer, these don't use the full path. */
8806 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8807 if (buf->b_fname != NULL
8808 && (path_with_url(buf->b_fname)
8809#ifdef FEAT_QUICKFIX
8810 || bt_nofile(buf)
8811#endif
8812 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008813 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008814 break;
8815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 return buf;
8818}
8819
8820/*
8821 * "bufexists(expr)" function
8822 */
8823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 typval_T *argvars;
8826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831/*
8832 * "buflisted(expr)" function
8833 */
8834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *argvars;
8837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 buf_T *buf;
8840
8841 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843}
8844
8845/*
8846 * "bufloaded(expr)" function
8847 */
8848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T *argvars;
8851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 buf_T *buf;
8854
8855 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857}
8858
Bram Moolenaar33570922005-01-25 22:26:29 +00008859static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861/*
8862 * Get buffer by number or pattern.
8863 */
8864 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 int save_magic;
8870 char_u *save_cpo;
8871 buf_T *buf;
8872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 if (tv->v_type == VAR_NUMBER)
8874 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008875 if (tv->v_type != VAR_STRING)
8876 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 if (name == NULL || *name == NUL)
8878 return curbuf;
8879 if (name[0] == '$' && name[1] == NUL)
8880 return lastbuf;
8881
8882 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8883 save_magic = p_magic;
8884 p_magic = TRUE;
8885 save_cpo = p_cpo;
8886 p_cpo = (char_u *)"";
8887
8888 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8889 TRUE, FALSE));
8890
8891 p_magic = save_magic;
8892 p_cpo = save_cpo;
8893
8894 /* If not found, try expanding the name, like done for bufexists(). */
8895 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
8898 return buf;
8899}
8900
8901/*
8902 * "bufname(expr)" function
8903 */
8904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008906 typval_T *argvars;
8907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 buf_T *buf;
8910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 buf = get_buf_tv(&argvars[0]);
8914 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 --emsg_off;
8920}
8921
8922/*
8923 * "bufnr(expr)" function
8924 */
8925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008927 typval_T *argvars;
8928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008931 int error = FALSE;
8932 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008937 --emsg_off;
8938
8939 /* If the buffer isn't found and the second argument is not zero create a
8940 * new buffer. */
8941 if (buf == NULL
8942 && argvars[1].v_type != VAR_UNKNOWN
8943 && get_tv_number_chk(&argvars[1], &error) != 0
8944 && !error
8945 && (name = get_tv_string_chk(&argvars[0])) != NULL
8946 && !error)
8947 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8948
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953}
8954
8955/*
8956 * "bufwinnr(nr)" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963#ifdef FEAT_WINDOWS
8964 win_T *wp;
8965 int winnr = 0;
8966#endif
8967 buf_T *buf;
8968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972#ifdef FEAT_WINDOWS
8973 for (wp = firstwin; wp; wp = wp->w_next)
8974 {
8975 ++winnr;
8976 if (wp->w_buffer == buf)
8977 break;
8978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982#endif
8983 --emsg_off;
8984}
8985
8986/*
8987 * "byte2line(byte)" function
8988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008991 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996#else
8997 long boff = 0;
8998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009003 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 (linenr_T)0, &boff);
9005#endif
9006}
9007
9008/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009009 * "byteidx()" function
9010 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009015{
9016#ifdef FEAT_MBYTE
9017 char_u *t;
9018#endif
9019 char_u *str;
9020 long idx;
9021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022 str = get_tv_string_chk(&argvars[0]);
9023 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009026 return;
9027
9028#ifdef FEAT_MBYTE
9029 t = str;
9030 for ( ; idx > 0; idx--)
9031 {
9032 if (*t == NUL) /* EOL reached */
9033 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009034 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009035 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009036 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009037#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009038 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009040#endif
9041}
9042
9043/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009044 * "call(func, arglist)" function
9045 */
9046 static void
9047f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 typval_T *argvars;
9049 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009050{
9051 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009052 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009053 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009055 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009057
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009058 if (argvars[1].v_type != VAR_LIST)
9059 {
9060 EMSG(_(e_listreq));
9061 return;
9062 }
9063 if (argvars[1].vval.v_list == NULL)
9064 return;
9065
9066 if (argvars[0].v_type == VAR_FUNC)
9067 func = argvars[0].vval.v_string;
9068 else
9069 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 if (*func == NUL)
9071 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009072
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 if (argvars[2].v_type != VAR_UNKNOWN)
9074 {
9075 if (argvars[2].v_type != VAR_DICT)
9076 {
9077 EMSG(_(e_dictreq));
9078 return;
9079 }
9080 selfdict = argvars[2].vval.v_dict;
9081 }
9082
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009083 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9084 item = item->li_next)
9085 {
9086 if (argc == MAX_FUNC_ARGS)
9087 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009088 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009089 break;
9090 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009091 /* Make a copy of each argument. This is needed to be able to set
9092 * v_lock to VAR_FIXED in the copy without changing the original list.
9093 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009094 copy_tv(&item->li_tv, &argv[argc++]);
9095 }
9096
9097 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009098 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009099 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9100 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009101
9102 /* Free the arguments. */
9103 while (argc > 0)
9104 clear_tv(&argv[--argc]);
9105}
9106
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009107#ifdef FEAT_FLOAT
9108/*
9109 * "ceil({float})" function
9110 */
9111 static void
9112f_ceil(argvars, rettv)
9113 typval_T *argvars;
9114 typval_T *rettv;
9115{
9116 float_T f;
9117
9118 rettv->v_type = VAR_FLOAT;
9119 if (get_float_arg(argvars, &f) == OK)
9120 rettv->vval.v_float = ceil(f);
9121 else
9122 rettv->vval.v_float = 0.0;
9123}
9124#endif
9125
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009126/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009127 * "changenr()" function
9128 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009129 static void
9130f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009131 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009132 typval_T *rettv;
9133{
9134 rettv->vval.v_number = curbuf->b_u_seq_cur;
9135}
9136
9137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 * "char2nr(string)" function
9139 */
9140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009141f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009142 typval_T *argvars;
9143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145#ifdef FEAT_MBYTE
9146 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009147 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 else
9149#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151}
9152
9153/*
9154 * "cindent(lnum)" function
9155 */
9156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009158 typval_T *argvars;
9159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161#ifdef FEAT_CINDENT
9162 pos_T pos;
9163 linenr_T lnum;
9164
9165 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9168 {
9169 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 curwin->w_cursor = pos;
9172 }
9173 else
9174#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176}
9177
9178/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009179 * "clearmatches()" function
9180 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009181 static void
9182f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009183 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009184 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009185{
9186#ifdef FEAT_SEARCH_EXTRA
9187 clear_matches(curwin);
9188#endif
9189}
9190
9191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 * "col(string)" function
9193 */
9194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 colnr_T col = 0;
9200 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009201 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009203 fp = var2fpos(&argvars[0], FALSE, &fnum);
9204 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 {
9206 if (fp->col == MAXCOL)
9207 {
9208 /* '> can be MAXCOL, get the length of the line then */
9209 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009210 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 else
9212 col = MAXCOL;
9213 }
9214 else
9215 {
9216 col = fp->col + 1;
9217#ifdef FEAT_VIRTUALEDIT
9218 /* col(".") when the cursor is on the NUL at the end of the line
9219 * because of "coladd" can be seen as an extra column. */
9220 if (virtual_active() && fp == &curwin->w_cursor)
9221 {
9222 char_u *p = ml_get_cursor();
9223
9224 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9225 curwin->w_virtcol - curwin->w_cursor.coladd))
9226 {
9227# ifdef FEAT_MBYTE
9228 int l;
9229
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009230 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 col += l;
9232# else
9233 if (*p != NUL && p[1] == NUL)
9234 ++col;
9235# endif
9236 }
9237 }
9238#endif
9239 }
9240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009241 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242}
9243
Bram Moolenaar572cb562005-08-05 21:35:02 +00009244#if defined(FEAT_INS_EXPAND)
9245/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009246 * "complete()" function
9247 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009248 static void
9249f_complete(argvars, rettv)
9250 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009251 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009252{
9253 int startcol;
9254
9255 if ((State & INSERT) == 0)
9256 {
9257 EMSG(_("E785: complete() can only be used in Insert mode"));
9258 return;
9259 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009260
9261 /* Check for undo allowed here, because if something was already inserted
9262 * the line was already saved for undo and this check isn't done. */
9263 if (!undo_allowed())
9264 return;
9265
Bram Moolenaarade00832006-03-10 21:46:58 +00009266 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9267 {
9268 EMSG(_(e_invarg));
9269 return;
9270 }
9271
9272 startcol = get_tv_number_chk(&argvars[0], NULL);
9273 if (startcol <= 0)
9274 return;
9275
9276 set_completion(startcol - 1, argvars[1].vval.v_list);
9277}
9278
9279/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009280 * "complete_add()" function
9281 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009282 static void
9283f_complete_add(argvars, rettv)
9284 typval_T *argvars;
9285 typval_T *rettv;
9286{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009287 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009288}
9289
9290/*
9291 * "complete_check()" function
9292 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009293 static void
9294f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009295 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009296 typval_T *rettv;
9297{
9298 int saved = RedrawingDisabled;
9299
9300 RedrawingDisabled = 0;
9301 ins_compl_check_keys(0);
9302 rettv->vval.v_number = compl_interrupted;
9303 RedrawingDisabled = saved;
9304}
9305#endif
9306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307/*
9308 * "confirm(message, buttons[, default [, type]])" function
9309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009312 typval_T *argvars UNUSED;
9313 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9316 char_u *message;
9317 char_u *buttons = NULL;
9318 char_u buf[NUMBUFLEN];
9319 char_u buf2[NUMBUFLEN];
9320 int def = 1;
9321 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 char_u *typestr;
9323 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 message = get_tv_string_chk(&argvars[0]);
9326 if (message == NULL)
9327 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009328 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009330 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9331 if (buttons == NULL)
9332 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009333 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009336 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9339 if (typestr == NULL)
9340 error = TRUE;
9341 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 switch (TOUPPER_ASC(*typestr))
9344 {
9345 case 'E': type = VIM_ERROR; break;
9346 case 'Q': type = VIM_QUESTION; break;
9347 case 'I': type = VIM_INFO; break;
9348 case 'W': type = VIM_WARNING; break;
9349 case 'G': type = VIM_GENERIC; break;
9350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 }
9352 }
9353 }
9354 }
9355
9356 if (buttons == NULL || *buttons == NUL)
9357 buttons = (char_u *)_("&Ok");
9358
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009359 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009361 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362#endif
9363}
9364
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009365/*
9366 * "copy()" function
9367 */
9368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009370 typval_T *argvars;
9371 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009372{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009373 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009376#ifdef FEAT_FLOAT
9377/*
9378 * "cos()" function
9379 */
9380 static void
9381f_cos(argvars, rettv)
9382 typval_T *argvars;
9383 typval_T *rettv;
9384{
9385 float_T f;
9386
9387 rettv->v_type = VAR_FLOAT;
9388 if (get_float_arg(argvars, &f) == OK)
9389 rettv->vval.v_float = cos(f);
9390 else
9391 rettv->vval.v_float = 0.0;
9392}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009393
9394/*
9395 * "cosh()" function
9396 */
9397 static void
9398f_cosh(argvars, rettv)
9399 typval_T *argvars;
9400 typval_T *rettv;
9401{
9402 float_T f;
9403
9404 rettv->v_type = VAR_FLOAT;
9405 if (get_float_arg(argvars, &f) == OK)
9406 rettv->vval.v_float = cosh(f);
9407 else
9408 rettv->vval.v_float = 0.0;
9409}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009410#endif
9411
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009413 * "count()" function
9414 */
9415 static void
9416f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 typval_T *argvars;
9418 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009419{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009420 long n = 0;
9421 int ic = FALSE;
9422
Bram Moolenaare9a41262005-01-15 22:18:47 +00009423 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009424 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 listitem_T *li;
9426 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009427 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009428
Bram Moolenaare9a41262005-01-15 22:18:47 +00009429 if ((l = argvars[0].vval.v_list) != NULL)
9430 {
9431 li = l->lv_first;
9432 if (argvars[2].v_type != VAR_UNKNOWN)
9433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434 int error = FALSE;
9435
9436 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009437 if (argvars[3].v_type != VAR_UNKNOWN)
9438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009439 idx = get_tv_number_chk(&argvars[3], &error);
9440 if (!error)
9441 {
9442 li = list_find(l, idx);
9443 if (li == NULL)
9444 EMSGN(_(e_listidx), idx);
9445 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 if (error)
9448 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009449 }
9450
9451 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009452 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009453 ++n;
9454 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009456 else if (argvars[0].v_type == VAR_DICT)
9457 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009458 int todo;
9459 dict_T *d;
9460 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009461
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009462 if ((d = argvars[0].vval.v_dict) != NULL)
9463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009464 int error = FALSE;
9465
Bram Moolenaare9a41262005-01-15 22:18:47 +00009466 if (argvars[2].v_type != VAR_UNKNOWN)
9467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009468 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009469 if (argvars[3].v_type != VAR_UNKNOWN)
9470 EMSG(_(e_invarg));
9471 }
9472
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009473 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009475 {
9476 if (!HASHITEM_EMPTY(hi))
9477 {
9478 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009479 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009480 ++n;
9481 }
9482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009483 }
9484 }
9485 else
9486 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009487 rettv->vval.v_number = n;
9488}
9489
9490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9492 *
9493 * Checks the existence of a cscope connection.
9494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009497 typval_T *argvars UNUSED;
9498 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500#ifdef FEAT_CSCOPE
9501 int num = 0;
9502 char_u *dbpath = NULL;
9503 char_u *prepend = NULL;
9504 char_u buf[NUMBUFLEN];
9505
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009506 if (argvars[0].v_type != VAR_UNKNOWN
9507 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 num = (int)get_tv_number(&argvars[0]);
9510 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009511 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
9514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516#endif
9517}
9518
9519/*
9520 * "cursor(lnum, col)" function
9521 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009522 * Moves the cursor to the specified line and column.
9523 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 typval_T *argvars;
9528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009531#ifdef FEAT_VIRTUALEDIT
9532 long coladd = 0;
9533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009535 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009536 if (argvars[1].v_type == VAR_UNKNOWN)
9537 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009538 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009539
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009540 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009541 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009542 line = pos.lnum;
9543 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009544#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009545 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009546#endif
9547 }
9548 else
9549 {
9550 line = get_tv_lnum(argvars);
9551 col = get_tv_number_chk(&argvars[1], NULL);
9552#ifdef FEAT_VIRTUALEDIT
9553 if (argvars[2].v_type != VAR_UNKNOWN)
9554 coladd = get_tv_number_chk(&argvars[2], NULL);
9555#endif
9556 }
9557 if (line < 0 || col < 0
9558#ifdef FEAT_VIRTUALEDIT
9559 || coladd < 0
9560#endif
9561 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 if (line > 0)
9564 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 if (col > 0)
9566 curwin->w_cursor.col = col - 1;
9567#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009568 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#endif
9570
9571 /* Make sure the cursor is in a valid position. */
9572 check_cursor();
9573#ifdef FEAT_MBYTE
9574 /* Correct cursor for multi-byte character. */
9575 if (has_mbyte)
9576 mb_adjust_cursor();
9577#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009578
9579 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581}
9582
9583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009584 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 */
9586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009588 typval_T *argvars;
9589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009591 int noref = 0;
9592
9593 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009595 if (noref < 0 || noref > 1)
9596 EMSG(_(e_invarg));
9597 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009598 {
9599 current_copyID += COPYID_INC;
9600 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602}
9603
9604/*
9605 * "delete()" function
9606 */
9607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 typval_T *argvars;
9610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611{
9612 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616}
9617
9618/*
9619 * "did_filetype()" function
9620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009623 typval_T *argvars UNUSED;
9624 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628#endif
9629}
9630
9631/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009632 * "diff_filler()" function
9633 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009636 typval_T *argvars UNUSED;
9637 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009638{
9639#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009641#endif
9642}
9643
9644/*
9645 * "diff_hlID()" function
9646 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009649 typval_T *argvars UNUSED;
9650 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009651{
9652#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009654 static linenr_T prev_lnum = 0;
9655 static int changedtick = 0;
9656 static int fnum = 0;
9657 static int change_start = 0;
9658 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009659 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009660 int filler_lines;
9661 int col;
9662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 if (lnum < 0) /* ignore type error in {lnum} arg */
9664 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009665 if (lnum != prev_lnum
9666 || changedtick != curbuf->b_changedtick
9667 || fnum != curbuf->b_fnum)
9668 {
9669 /* New line, buffer, change: need to get the values. */
9670 filler_lines = diff_check(curwin, lnum);
9671 if (filler_lines < 0)
9672 {
9673 if (filler_lines == -1)
9674 {
9675 change_start = MAXCOL;
9676 change_end = -1;
9677 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9678 hlID = HLF_ADD; /* added line */
9679 else
9680 hlID = HLF_CHD; /* changed line */
9681 }
9682 else
9683 hlID = HLF_ADD; /* added line */
9684 }
9685 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009686 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009687 prev_lnum = lnum;
9688 changedtick = curbuf->b_changedtick;
9689 fnum = curbuf->b_fnum;
9690 }
9691
9692 if (hlID == HLF_CHD || hlID == HLF_TXD)
9693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009695 if (col >= change_start && col <= change_end)
9696 hlID = HLF_TXD; /* changed text */
9697 else
9698 hlID = HLF_CHD; /* changed line */
9699 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009700 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009701#endif
9702}
9703
9704/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009705 * "empty({expr})" function
9706 */
9707 static void
9708f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *argvars;
9710 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009711{
9712 int n;
9713
9714 switch (argvars[0].v_type)
9715 {
9716 case VAR_STRING:
9717 case VAR_FUNC:
9718 n = argvars[0].vval.v_string == NULL
9719 || *argvars[0].vval.v_string == NUL;
9720 break;
9721 case VAR_NUMBER:
9722 n = argvars[0].vval.v_number == 0;
9723 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009724#ifdef FEAT_FLOAT
9725 case VAR_FLOAT:
9726 n = argvars[0].vval.v_float == 0.0;
9727 break;
9728#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009729 case VAR_LIST:
9730 n = argvars[0].vval.v_list == NULL
9731 || argvars[0].vval.v_list->lv_first == NULL;
9732 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009733 case VAR_DICT:
9734 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009736 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009737 default:
9738 EMSG2(_(e_intern2), "f_empty()");
9739 n = 0;
9740 }
9741
9742 rettv->vval.v_number = n;
9743}
9744
9745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 * "escape({string}, {chars})" function
9747 */
9748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 typval_T *argvars;
9751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753 char_u buf[NUMBUFLEN];
9754
Bram Moolenaar758711c2005-02-02 23:11:38 +00009755 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9756 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009761 * "eval()" function
9762 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009763 static void
9764f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009767{
9768 char_u *s;
9769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 s = get_tv_string_chk(&argvars[0]);
9771 if (s != NULL)
9772 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009774 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9775 {
9776 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009777 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009779 else if (*s != NUL)
9780 EMSG(_(e_trailing));
9781}
9782
9783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 * "eventhandler()" function
9785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009788 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * "executable()" function
9796 */
9797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009799 typval_T *argvars;
9800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
9805/*
9806 * "exists()" function
9807 */
9808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009810 typval_T *argvars;
9811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813 char_u *p;
9814 char_u *name;
9815 int n = FALSE;
9816 int len = 0;
9817
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009818 no_autoload = TRUE;
9819
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 if (*p == '$') /* environment variable */
9822 {
9823 /* first try "normal" environment variables (fast) */
9824 if (mch_getenv(p + 1) != NULL)
9825 n = TRUE;
9826 else
9827 {
9828 /* try expanding things like $VIM and ${HOME} */
9829 p = expand_env_save(p);
9830 if (p != NULL && *p != '$')
9831 n = TRUE;
9832 vim_free(p);
9833 }
9834 }
9835 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009837 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009838 if (*skipwhite(p) != NUL)
9839 n = FALSE; /* trailing garbage */
9840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 else if (*p == '*') /* internal or user defined function */
9842 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009843 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 }
9845 else if (*p == ':')
9846 {
9847 n = cmd_exists(p + 1);
9848 }
9849 else if (*p == '#')
9850 {
9851#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009852 if (p[1] == '#')
9853 n = autocmd_supported(p + 2);
9854 else
9855 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856#endif
9857 }
9858 else /* internal variable */
9859 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009860 char_u *tofree;
9861 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009863 /* get_name_len() takes care of expanding curly braces */
9864 name = p;
9865 len = get_name_len(&p, &tofree, TRUE, FALSE);
9866 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009868 if (tofree != NULL)
9869 name = tofree;
9870 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9871 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009873 /* handle d.key, l[idx], f(expr) */
9874 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9875 if (n)
9876 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 }
9878 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009879 if (*p != NUL)
9880 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009882 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 }
9884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009886
9887 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888}
9889
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009890#ifdef FEAT_FLOAT
9891/*
9892 * "exp()" function
9893 */
9894 static void
9895f_exp(argvars, rettv)
9896 typval_T *argvars;
9897 typval_T *rettv;
9898{
9899 float_T f;
9900
9901 rettv->v_type = VAR_FLOAT;
9902 if (get_float_arg(argvars, &f) == OK)
9903 rettv->vval.v_float = exp(f);
9904 else
9905 rettv->vval.v_float = 0.0;
9906}
9907#endif
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909/*
9910 * "expand()" function
9911 */
9912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916{
9917 char_u *s;
9918 int len;
9919 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009920 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009924 rettv->v_type = VAR_STRING;
9925 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (*s == '%' || *s == '#' || *s == '<')
9927 {
9928 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009929 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 --emsg_off;
9931 }
9932 else
9933 {
9934 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009935 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 if (argvars[1].v_type != VAR_UNKNOWN
9937 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009938 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 if (!error)
9940 {
9941 ExpandInit(&xpc);
9942 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009943 if (p_wic)
9944 options += WILD_ICASE;
9945 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 }
9947 else
9948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 }
9950}
9951
9952/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009953 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009955 */
9956 static void
9957f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *argvars;
9959 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009960{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009961 char *arg_errmsg = N_("extend() argument");
9962
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009964 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 list_T *l1, *l2;
9966 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 l1 = argvars[0].vval.v_list;
9971 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009972 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009973 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 {
9975 if (argvars[2].v_type != VAR_UNKNOWN)
9976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009977 before = get_tv_number_chk(&argvars[2], &error);
9978 if (error)
9979 return; /* type error; errmsg already given */
9980
Bram Moolenaar758711c2005-02-02 23:11:38 +00009981 if (before == l1->lv_len)
9982 item = NULL;
9983 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009985 item = list_find(l1, before);
9986 if (item == NULL)
9987 {
9988 EMSGN(_(e_listidx), before);
9989 return;
9990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 }
9992 }
9993 else
9994 item = NULL;
9995 list_extend(l1, l2, item);
9996
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 copy_tv(&argvars[0], rettv);
9998 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010000 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10001 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 dict_T *d1, *d2;
10003 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010004 char_u *action;
10005 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010006 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010007 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010008
10009 d1 = argvars[0].vval.v_dict;
10010 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010011 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010012 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010013 {
10014 /* Check the third argument. */
10015 if (argvars[2].v_type != VAR_UNKNOWN)
10016 {
10017 static char *(av[]) = {"keep", "force", "error"};
10018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 action = get_tv_string_chk(&argvars[2]);
10020 if (action == NULL)
10021 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010022 for (i = 0; i < 3; ++i)
10023 if (STRCMP(action, av[i]) == 0)
10024 break;
10025 if (i == 3)
10026 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010027 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010028 return;
10029 }
10030 }
10031 else
10032 action = (char_u *)"force";
10033
10034 /* Go over all entries in the second dict and add them to the
10035 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010036 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010038 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010039 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010040 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010041 --todo;
10042 di1 = dict_find(d1, hi2->hi_key, -1);
10043 if (di1 == NULL)
10044 {
10045 di1 = dictitem_copy(HI2DI(hi2));
10046 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10047 dictitem_free(di1);
10048 }
10049 else if (*action == 'e')
10050 {
10051 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10052 break;
10053 }
10054 else if (*action == 'f')
10055 {
10056 clear_tv(&di1->di_tv);
10057 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10058 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010059 }
10060 }
10061
Bram Moolenaare9a41262005-01-15 22:18:47 +000010062 copy_tv(&argvars[0], rettv);
10063 }
10064 }
10065 else
10066 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010067}
10068
10069/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010070 * "feedkeys()" function
10071 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010072 static void
10073f_feedkeys(argvars, rettv)
10074 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010075 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010076{
10077 int remap = TRUE;
10078 char_u *keys, *flags;
10079 char_u nbuf[NUMBUFLEN];
10080 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010081 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010082
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010083 /* This is not allowed in the sandbox. If the commands would still be
10084 * executed in the sandbox it would be OK, but it probably happens later,
10085 * when "sandbox" is no longer set. */
10086 if (check_secure())
10087 return;
10088
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010089 keys = get_tv_string(&argvars[0]);
10090 if (*keys != NUL)
10091 {
10092 if (argvars[1].v_type != VAR_UNKNOWN)
10093 {
10094 flags = get_tv_string_buf(&argvars[1], nbuf);
10095 for ( ; *flags != NUL; ++flags)
10096 {
10097 switch (*flags)
10098 {
10099 case 'n': remap = FALSE; break;
10100 case 'm': remap = TRUE; break;
10101 case 't': typed = TRUE; break;
10102 }
10103 }
10104 }
10105
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010106 /* Need to escape K_SPECIAL and CSI before putting the string in the
10107 * typeahead buffer. */
10108 keys_esc = vim_strsave_escape_csi(keys);
10109 if (keys_esc != NULL)
10110 {
10111 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010112 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010113 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010114 if (vgetc_busy)
10115 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010116 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010117 }
10118}
10119
10120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 * "filereadable()" function
10122 */
10123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010125 typval_T *argvars;
10126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010128 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 char_u *p;
10130 int n;
10131
Bram Moolenaarc236c162008-07-13 17:41:49 +000010132#ifndef O_NONBLOCK
10133# define O_NONBLOCK 0
10134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010136 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10137 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 {
10139 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010140 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 }
10142 else
10143 n = FALSE;
10144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146}
10147
10148/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010149 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 * rights to write into.
10151 */
10152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 typval_T *argvars;
10155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010157 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158}
10159
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010160static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010161
10162 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010163findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010164 typval_T *argvars;
10165 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010166 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010167{
10168#ifdef FEAT_SEARCHPATH
10169 char_u *fname;
10170 char_u *fresult = NULL;
10171 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10172 char_u *p;
10173 char_u pathbuf[NUMBUFLEN];
10174 int count = 1;
10175 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010176 int error = FALSE;
10177#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010178
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010179 rettv->vval.v_string = NULL;
10180 rettv->v_type = VAR_STRING;
10181
10182#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010184
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010185 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10188 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010189 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 else
10191 {
10192 if (*p != NUL)
10193 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010196 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010198 }
10199
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010200 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10201 error = TRUE;
10202
10203 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 do
10206 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010207 if (rettv->v_type == VAR_STRING)
10208 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010209 fresult = find_file_in_path_option(first ? fname : NULL,
10210 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010211 0, first, path,
10212 find_what,
10213 curbuf->b_ffname,
10214 find_what == FINDFILE_DIR
10215 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010217
10218 if (fresult != NULL && rettv->v_type == VAR_LIST)
10219 list_append_string(rettv->vval.v_list, fresult, -1);
10220
10221 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010223
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010224 if (rettv->v_type == VAR_STRING)
10225 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010226#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010227}
10228
Bram Moolenaar33570922005-01-25 22:26:29 +000010229static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10230static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010231
10232/*
10233 * Implementation of map() and filter().
10234 */
10235 static void
10236filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010237 typval_T *argvars;
10238 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010239 int map;
10240{
10241 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010242 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 listitem_T *li, *nli;
10244 list_T *l = NULL;
10245 dictitem_T *di;
10246 hashtab_T *ht;
10247 hashitem_T *hi;
10248 dict_T *d = NULL;
10249 typval_T save_val;
10250 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010252 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010253 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10254 char *arg_errmsg = (map ? N_("map() argument")
10255 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010256 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010257 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010258
Bram Moolenaare9a41262005-01-15 22:18:47 +000010259 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010260 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010261 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010262 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 return;
10264 }
10265 else if (argvars[0].v_type == VAR_DICT)
10266 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010267 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010268 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010269 return;
10270 }
10271 else
10272 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010273 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 return;
10275 }
10276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 expr = get_tv_string_buf_chk(&argvars[1], buf);
10278 /* On type errors, the preceding call has already displayed an error
10279 * message. Avoid a misleading error message for an empty string that
10280 * was not passed as argument. */
10281 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 prepare_vimvar(VV_VAL, &save_val);
10284 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010285
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010286 /* We reset "did_emsg" to be able to detect whether an error
10287 * occurred during evaluation of the expression. */
10288 save_did_emsg = did_emsg;
10289 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010290
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010291 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 vimvars[VV_KEY].vv_type = VAR_STRING;
10295
10296 ht = &d->dv_hashtab;
10297 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010298 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 if (!HASHITEM_EMPTY(hi))
10302 {
10303 --todo;
10304 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010305 if (tv_check_lock(di->di_tv.v_lock,
10306 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 break;
10308 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010309 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010310 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 break;
10312 if (!map && rem)
10313 dictitem_remove(d, di);
10314 clear_tv(&vimvars[VV_KEY].vv_tv);
10315 }
10316 }
10317 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 }
10319 else
10320 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010321 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 for (li = l->lv_first; li != NULL; li = nli)
10324 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010325 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010326 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010328 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010329 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010330 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010331 break;
10332 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010334 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010335 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010336 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010337
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010338 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010340
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010341 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010342 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343
10344 copy_tv(&argvars[0], rettv);
10345}
10346
10347 static int
10348filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010349 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010350 char_u *expr;
10351 int map;
10352 int *remp;
10353{
Bram Moolenaar33570922005-01-25 22:26:29 +000010354 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010356 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010357
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010359 s = expr;
10360 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010361 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362 if (*s != NUL) /* check for trailing chars after expr */
10363 {
10364 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010365 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010366 }
10367 if (map)
10368 {
10369 /* map(): replace the list item value */
10370 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010371 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010372 *tv = rettv;
10373 }
10374 else
10375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 int error = FALSE;
10377
Bram Moolenaare9a41262005-01-15 22:18:47 +000010378 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010380 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010381 /* On type error, nothing has been removed; return FAIL to stop the
10382 * loop. The error message was given by get_tv_number_chk(). */
10383 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010384 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010386 retval = OK;
10387theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010389 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010390}
10391
10392/*
10393 * "filter()" function
10394 */
10395 static void
10396f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010397 typval_T *argvars;
10398 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010399{
10400 filter_map(argvars, rettv, FALSE);
10401}
10402
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010403/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010404 * "finddir({fname}[, {path}[, {count}]])" function
10405 */
10406 static void
10407f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 typval_T *argvars;
10409 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010410{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010411 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010412}
10413
10414/*
10415 * "findfile({fname}[, {path}[, {count}]])" function
10416 */
10417 static void
10418f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 typval_T *argvars;
10420 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010421{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010422 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010423}
10424
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010425#ifdef FEAT_FLOAT
10426/*
10427 * "float2nr({float})" function
10428 */
10429 static void
10430f_float2nr(argvars, rettv)
10431 typval_T *argvars;
10432 typval_T *rettv;
10433{
10434 float_T f;
10435
10436 if (get_float_arg(argvars, &f) == OK)
10437 {
10438 if (f < -0x7fffffff)
10439 rettv->vval.v_number = -0x7fffffff;
10440 else if (f > 0x7fffffff)
10441 rettv->vval.v_number = 0x7fffffff;
10442 else
10443 rettv->vval.v_number = (varnumber_T)f;
10444 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010445}
10446
10447/*
10448 * "floor({float})" function
10449 */
10450 static void
10451f_floor(argvars, rettv)
10452 typval_T *argvars;
10453 typval_T *rettv;
10454{
10455 float_T f;
10456
10457 rettv->v_type = VAR_FLOAT;
10458 if (get_float_arg(argvars, &f) == OK)
10459 rettv->vval.v_float = floor(f);
10460 else
10461 rettv->vval.v_float = 0.0;
10462}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010463
10464/*
10465 * "fmod()" function
10466 */
10467 static void
10468f_fmod(argvars, rettv)
10469 typval_T *argvars;
10470 typval_T *rettv;
10471{
10472 float_T fx, fy;
10473
10474 rettv->v_type = VAR_FLOAT;
10475 if (get_float_arg(argvars, &fx) == OK
10476 && get_float_arg(&argvars[1], &fy) == OK)
10477 rettv->vval.v_float = fmod(fx, fy);
10478 else
10479 rettv->vval.v_float = 0.0;
10480}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010481#endif
10482
Bram Moolenaar0d660222005-01-07 21:51:51 +000010483/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010484 * "fnameescape({string})" function
10485 */
10486 static void
10487f_fnameescape(argvars, rettv)
10488 typval_T *argvars;
10489 typval_T *rettv;
10490{
10491 rettv->vval.v_string = vim_strsave_fnameescape(
10492 get_tv_string(&argvars[0]), FALSE);
10493 rettv->v_type = VAR_STRING;
10494}
10495
10496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 * "fnamemodify({fname}, {mods})" function
10498 */
10499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 char_u *fname;
10505 char_u *mods;
10506 int usedlen = 0;
10507 int len;
10508 char_u *fbuf = NULL;
10509 char_u buf[NUMBUFLEN];
10510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 fname = get_tv_string_chk(&argvars[0]);
10512 mods = get_tv_string_buf_chk(&argvars[1], buf);
10513 if (fname == NULL || mods == NULL)
10514 fname = NULL;
10515 else
10516 {
10517 len = (int)STRLEN(fname);
10518 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 vim_free(fbuf);
10527}
10528
Bram Moolenaar33570922005-01-25 22:26:29 +000010529static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
10531/*
10532 * "foldclosed()" function
10533 */
10534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 typval_T *argvars;
10537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 int end;
10539{
10540#ifdef FEAT_FOLDING
10541 linenr_T lnum;
10542 linenr_T first, last;
10543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10546 {
10547 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10548 {
10549 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 return;
10554 }
10555 }
10556#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561 * "foldclosed()" function
10562 */
10563 static void
10564f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567{
10568 foldclosed_both(argvars, rettv, FALSE);
10569}
10570
10571/*
10572 * "foldclosedend()" function
10573 */
10574 static void
10575f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010576 typval_T *argvars;
10577 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010578{
10579 foldclosed_both(argvars, rettv, TRUE);
10580}
10581
10582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 * "foldlevel()" function
10584 */
10585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010586f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010587 typval_T *argvars;
10588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589{
10590#ifdef FEAT_FOLDING
10591 linenr_T lnum;
10592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597}
10598
10599/*
10600 * "foldtext()" function
10601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010604 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606{
10607#ifdef FEAT_FOLDING
10608 linenr_T lnum;
10609 char_u *s;
10610 char_u *r;
10611 int len;
10612 char *txt;
10613#endif
10614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->v_type = VAR_STRING;
10616 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10619 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10620 <= curbuf->b_ml.ml_line_count
10621 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 {
10623 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10625 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 {
10627 if (!linewhite(lnum))
10628 break;
10629 ++lnum;
10630 }
10631
10632 /* Find interesting text in this line. */
10633 s = skipwhite(ml_get(lnum));
10634 /* skip C comment-start */
10635 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010638 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010640 {
10641 s = skipwhite(ml_get(lnum + 1));
10642 if (*s == '*')
10643 s = skipwhite(s + 1);
10644 }
10645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 txt = _("+-%s%3ld lines: ");
10647 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010648 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 + 20 /* for %3ld */
10650 + STRLEN(s))); /* concatenated */
10651 if (r != NULL)
10652 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10654 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10655 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 len = (int)STRLEN(r);
10657 STRCAT(r, s);
10658 /* remove 'foldmarker' and 'commentstring' */
10659 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 }
10662 }
10663#endif
10664}
10665
10666/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010667 * "foldtextresult(lnum)" function
10668 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010673{
10674#ifdef FEAT_FOLDING
10675 linenr_T lnum;
10676 char_u *text;
10677 char_u buf[51];
10678 foldinfo_T foldinfo;
10679 int fold_count;
10680#endif
10681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->v_type = VAR_STRING;
10683 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010684#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 /* treat illegal types and illegal string values for {lnum} the same */
10687 if (lnum < 0)
10688 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010689 fold_count = foldedCount(curwin, lnum, &foldinfo);
10690 if (fold_count > 0)
10691 {
10692 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10693 &foldinfo, buf);
10694 if (text == buf)
10695 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010697 }
10698#endif
10699}
10700
10701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 * "foreground()" function
10703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010705f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010706 typval_T *argvars UNUSED;
10707 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709#ifdef FEAT_GUI
10710 if (gui.in_use)
10711 gui_mch_set_foreground();
10712#else
10713# ifdef WIN32
10714 win32_set_foreground();
10715# endif
10716#endif
10717}
10718
10719/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010720 * "function()" function
10721 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010724 typval_T *argvars;
10725 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010726{
10727 char_u *s;
10728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010730 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010731 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010732 /* Don't check an autoload name for existence here. */
10733 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010734 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010735 else
10736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->vval.v_string = vim_strsave(s);
10738 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010739 }
10740}
10741
10742/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010743 * "garbagecollect()" function
10744 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010745 static void
10746f_garbagecollect(argvars, rettv)
10747 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010748 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010749{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010750 /* This is postponed until we are back at the toplevel, because we may be
10751 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10752 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010753
10754 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10755 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010756}
10757
10758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010759 * "get()" function
10760 */
10761 static void
10762f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010765{
Bram Moolenaar33570922005-01-25 22:26:29 +000010766 listitem_T *li;
10767 list_T *l;
10768 dictitem_T *di;
10769 dict_T *d;
10770 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010771
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010773 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010774 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 int error = FALSE;
10777
10778 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10779 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010780 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010783 else if (argvars[0].v_type == VAR_DICT)
10784 {
10785 if ((d = argvars[0].vval.v_dict) != NULL)
10786 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010787 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788 if (di != NULL)
10789 tv = &di->di_tv;
10790 }
10791 }
10792 else
10793 EMSG2(_(e_listdictarg), "get()");
10794
10795 if (tv == NULL)
10796 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010798 copy_tv(&argvars[2], rettv);
10799 }
10800 else
10801 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802}
10803
Bram Moolenaar342337a2005-07-21 21:11:17 +000010804static 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 +000010805
10806/*
10807 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010808 * Return a range (from start to end) of lines in rettv from the specified
10809 * buffer.
10810 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010811 */
10812 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010813get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010814 buf_T *buf;
10815 linenr_T start;
10816 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010817 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010818 typval_T *rettv;
10819{
10820 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010821
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010822 if (retlist && rettv_list_alloc(rettv) == FAIL)
10823 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010824
10825 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10826 return;
10827
10828 if (!retlist)
10829 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010830 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10831 p = ml_get_buf(buf, start, FALSE);
10832 else
10833 p = (char_u *)"";
10834
10835 rettv->v_type = VAR_STRING;
10836 rettv->vval.v_string = vim_strsave(p);
10837 }
10838 else
10839 {
10840 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010841 return;
10842
10843 if (start < 1)
10844 start = 1;
10845 if (end > buf->b_ml.ml_line_count)
10846 end = buf->b_ml.ml_line_count;
10847 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010848 if (list_append_string(rettv->vval.v_list,
10849 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010850 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010851 }
10852}
10853
10854/*
10855 * "getbufline()" function
10856 */
10857 static void
10858f_getbufline(argvars, rettv)
10859 typval_T *argvars;
10860 typval_T *rettv;
10861{
10862 linenr_T lnum;
10863 linenr_T end;
10864 buf_T *buf;
10865
10866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10867 ++emsg_off;
10868 buf = get_buf_tv(&argvars[0]);
10869 --emsg_off;
10870
Bram Moolenaar661b1822005-07-28 22:36:45 +000010871 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010872 if (argvars[2].v_type == VAR_UNKNOWN)
10873 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010874 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010875 end = get_tv_lnum_buf(&argvars[2], buf);
10876
Bram Moolenaar342337a2005-07-21 21:11:17 +000010877 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010878}
10879
Bram Moolenaar0d660222005-01-07 21:51:51 +000010880/*
10881 * "getbufvar()" function
10882 */
10883 static void
10884f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010885 typval_T *argvars;
10886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010887{
10888 buf_T *buf;
10889 buf_T *save_curbuf;
10890 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010891 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10894 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010895 ++emsg_off;
10896 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897
10898 rettv->v_type = VAR_STRING;
10899 rettv->vval.v_string = NULL;
10900
10901 if (buf != NULL && varname != NULL)
10902 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010903 /* set curbuf to be our buf, temporarily */
10904 save_curbuf = curbuf;
10905 curbuf = buf;
10906
Bram Moolenaar0d660222005-01-07 21:51:51 +000010907 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010908 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010909 else if (STRCMP(varname, "changedtick") == 0)
10910 {
10911 rettv->v_type = VAR_NUMBER;
10912 rettv->vval.v_number = curbuf->b_changedtick;
10913 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010914 else
10915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 if (*varname == NUL)
10917 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10918 * scope prefix before the NUL byte is required by
10919 * find_var_in_ht(). */
10920 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010921 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010922 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010923 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010925 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010926
10927 /* restore previous notion of curbuf */
10928 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010929 }
10930
10931 --emsg_off;
10932}
10933
10934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 * "getchar()" function
10936 */
10937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 typval_T *argvars;
10940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
10942 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010945 /* Position the cursor. Needed after a message that ends in a space. */
10946 windgoto(msg_row, msg_col);
10947
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 ++no_mapping;
10949 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010950 for (;;)
10951 {
10952 if (argvars[0].v_type == VAR_UNKNOWN)
10953 /* getchar(): blocking wait. */
10954 n = safe_vgetc();
10955 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10956 /* getchar(1): only check if char avail */
10957 n = vpeekc();
10958 else if (error || vpeekc() == NUL)
10959 /* illegal argument or getchar(0) and no char avail: return zero */
10960 n = 0;
10961 else
10962 /* getchar(0) and char avail: return char */
10963 n = safe_vgetc();
10964 if (n == K_IGNORE)
10965 continue;
10966 break;
10967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 --no_mapping;
10969 --allow_keys;
10970
Bram Moolenaar219b8702006-11-01 14:32:36 +000010971 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10972 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10973 vimvars[VV_MOUSE_COL].vv_nr = 0;
10974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 if (IS_SPECIAL(n) || mod_mask != 0)
10977 {
10978 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10979 int i = 0;
10980
10981 /* Turn a special key into three bytes, plus modifier. */
10982 if (mod_mask != 0)
10983 {
10984 temp[i++] = K_SPECIAL;
10985 temp[i++] = KS_MODIFIER;
10986 temp[i++] = mod_mask;
10987 }
10988 if (IS_SPECIAL(n))
10989 {
10990 temp[i++] = K_SPECIAL;
10991 temp[i++] = K_SECOND(n);
10992 temp[i++] = K_THIRD(n);
10993 }
10994#ifdef FEAT_MBYTE
10995 else if (has_mbyte)
10996 i += (*mb_char2bytes)(n, temp + i);
10997#endif
10998 else
10999 temp[i++] = n;
11000 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->v_type = VAR_STRING;
11002 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011003
11004#ifdef FEAT_MOUSE
11005 if (n == K_LEFTMOUSE
11006 || n == K_LEFTMOUSE_NM
11007 || n == K_LEFTDRAG
11008 || n == K_LEFTRELEASE
11009 || n == K_LEFTRELEASE_NM
11010 || n == K_MIDDLEMOUSE
11011 || n == K_MIDDLEDRAG
11012 || n == K_MIDDLERELEASE
11013 || n == K_RIGHTMOUSE
11014 || n == K_RIGHTDRAG
11015 || n == K_RIGHTRELEASE
11016 || n == K_X1MOUSE
11017 || n == K_X1DRAG
11018 || n == K_X1RELEASE
11019 || n == K_X2MOUSE
11020 || n == K_X2DRAG
11021 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011022 || n == K_MOUSELEFT
11023 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011024 || n == K_MOUSEDOWN
11025 || n == K_MOUSEUP)
11026 {
11027 int row = mouse_row;
11028 int col = mouse_col;
11029 win_T *win;
11030 linenr_T lnum;
11031# ifdef FEAT_WINDOWS
11032 win_T *wp;
11033# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011034 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011035
11036 if (row >= 0 && col >= 0)
11037 {
11038 /* Find the window at the mouse coordinates and compute the
11039 * text position. */
11040 win = mouse_find_win(&row, &col);
11041 (void)mouse_comp_pos(win, &row, &col, &lnum);
11042# ifdef FEAT_WINDOWS
11043 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011044 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011045# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011046 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011047 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11048 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11049 }
11050 }
11051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 }
11053}
11054
11055/*
11056 * "getcharmod()" function
11057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064}
11065
11066/*
11067 * "getcmdline()" function
11068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011071 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->v_type = VAR_STRING;
11075 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076}
11077
11078/*
11079 * "getcmdpos()" function
11080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011083 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087}
11088
11089/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011090 * "getcmdtype()" function
11091 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011092 static void
11093f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011094 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011095 typval_T *rettv;
11096{
11097 rettv->v_type = VAR_STRING;
11098 rettv->vval.v_string = alloc(2);
11099 if (rettv->vval.v_string != NULL)
11100 {
11101 rettv->vval.v_string[0] = get_cmdline_type();
11102 rettv->vval.v_string[1] = NUL;
11103 }
11104}
11105
11106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 * "getcwd()" function
11108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011111 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011114 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011117 rettv->vval.v_string = NULL;
11118 cwd = alloc(MAXPATHL);
11119 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011121 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11122 {
11123 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011125 if (rettv->vval.v_string != NULL)
11126 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011128 }
11129 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 }
11131}
11132
11133/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011134 * "getfontname()" function
11135 */
11136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011139 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011140{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 rettv->v_type = VAR_STRING;
11142 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011143#ifdef FEAT_GUI
11144 if (gui.in_use)
11145 {
11146 GuiFont font;
11147 char_u *name = NULL;
11148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011150 {
11151 /* Get the "Normal" font. Either the name saved by
11152 * hl_set_font_name() or from the font ID. */
11153 font = gui.norm_font;
11154 name = hl_get_font_name();
11155 }
11156 else
11157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011159 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11160 return;
11161 font = gui_mch_get_font(name, FALSE);
11162 if (font == NOFONT)
11163 return; /* Invalid font name, return empty string. */
11164 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011166 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011167 gui_mch_free_font(font);
11168 }
11169#endif
11170}
11171
11172/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011173 * "getfperm({fname})" function
11174 */
11175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 typval_T *argvars;
11178 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011179{
11180 char_u *fname;
11181 struct stat st;
11182 char_u *perm = NULL;
11183 char_u flags[] = "rwx";
11184 int i;
11185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011189 if (mch_stat((char *)fname, &st) >= 0)
11190 {
11191 perm = vim_strsave((char_u *)"---------");
11192 if (perm != NULL)
11193 {
11194 for (i = 0; i < 9; i++)
11195 {
11196 if (st.st_mode & (1 << (8 - i)))
11197 perm[i] = flags[i % 3];
11198 }
11199 }
11200 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011202}
11203
11204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 * "getfsize({fname})" function
11206 */
11207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011209 typval_T *argvars;
11210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211{
11212 char_u *fname;
11213 struct stat st;
11214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218
11219 if (mch_stat((char *)fname, &st) >= 0)
11220 {
11221 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011226
11227 /* non-perfect check for overflow */
11228 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11229 rettv->vval.v_number = -2;
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 }
11232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234}
11235
11236/*
11237 * "getftime({fname})" function
11238 */
11239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011241 typval_T *argvars;
11242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243{
11244 char_u *fname;
11245 struct stat st;
11246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248
11249 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253}
11254
11255/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011256 * "getftype({fname})" function
11257 */
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011262{
11263 char_u *fname;
11264 struct stat st;
11265 char_u *type = NULL;
11266 char *t;
11267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011271 if (mch_lstat((char *)fname, &st) >= 0)
11272 {
11273#ifdef S_ISREG
11274 if (S_ISREG(st.st_mode))
11275 t = "file";
11276 else if (S_ISDIR(st.st_mode))
11277 t = "dir";
11278# ifdef S_ISLNK
11279 else if (S_ISLNK(st.st_mode))
11280 t = "link";
11281# endif
11282# ifdef S_ISBLK
11283 else if (S_ISBLK(st.st_mode))
11284 t = "bdev";
11285# endif
11286# ifdef S_ISCHR
11287 else if (S_ISCHR(st.st_mode))
11288 t = "cdev";
11289# endif
11290# ifdef S_ISFIFO
11291 else if (S_ISFIFO(st.st_mode))
11292 t = "fifo";
11293# endif
11294# ifdef S_ISSOCK
11295 else if (S_ISSOCK(st.st_mode))
11296 t = "fifo";
11297# endif
11298 else
11299 t = "other";
11300#else
11301# ifdef S_IFMT
11302 switch (st.st_mode & S_IFMT)
11303 {
11304 case S_IFREG: t = "file"; break;
11305 case S_IFDIR: t = "dir"; break;
11306# ifdef S_IFLNK
11307 case S_IFLNK: t = "link"; break;
11308# endif
11309# ifdef S_IFBLK
11310 case S_IFBLK: t = "bdev"; break;
11311# endif
11312# ifdef S_IFCHR
11313 case S_IFCHR: t = "cdev"; break;
11314# endif
11315# ifdef S_IFIFO
11316 case S_IFIFO: t = "fifo"; break;
11317# endif
11318# ifdef S_IFSOCK
11319 case S_IFSOCK: t = "socket"; break;
11320# endif
11321 default: t = "other";
11322 }
11323# else
11324 if (mch_isdir(fname))
11325 t = "dir";
11326 else
11327 t = "file";
11328# endif
11329#endif
11330 type = vim_strsave((char_u *)t);
11331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011333}
11334
11335/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011336 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337 */
11338 static void
11339f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 typval_T *argvars;
11341 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011342{
11343 linenr_T lnum;
11344 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011345 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011346
11347 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011348 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011349 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011350 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011351 retlist = FALSE;
11352 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011353 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011354 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011355 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011356 retlist = TRUE;
11357 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011358
Bram Moolenaar342337a2005-07-21 21:11:17 +000011359 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360}
11361
11362/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011363 * "getmatches()" function
11364 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011365 static void
11366f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011367 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011368 typval_T *rettv;
11369{
11370#ifdef FEAT_SEARCH_EXTRA
11371 dict_T *dict;
11372 matchitem_T *cur = curwin->w_match_head;
11373
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011374 if (rettv_list_alloc(rettv) == OK)
11375 {
11376 while (cur != NULL)
11377 {
11378 dict = dict_alloc();
11379 if (dict == NULL)
11380 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011381 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11382 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11383 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11384 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11385 list_append_dict(rettv->vval.v_list, dict);
11386 cur = cur->next;
11387 }
11388 }
11389#endif
11390}
11391
11392/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011393 * "getpid()" function
11394 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011395 static void
11396f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011397 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011398 typval_T *rettv;
11399{
11400 rettv->vval.v_number = mch_get_pid();
11401}
11402
11403/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011404 * "getpos(string)" function
11405 */
11406 static void
11407f_getpos(argvars, rettv)
11408 typval_T *argvars;
11409 typval_T *rettv;
11410{
11411 pos_T *fp;
11412 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011413 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011414
11415 if (rettv_list_alloc(rettv) == OK)
11416 {
11417 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011418 fp = var2fpos(&argvars[0], TRUE, &fnum);
11419 if (fnum != -1)
11420 list_append_number(l, (varnumber_T)fnum);
11421 else
11422 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011423 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11424 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011425 list_append_number(l, (fp != NULL)
11426 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011427 : (varnumber_T)0);
11428 list_append_number(l,
11429#ifdef FEAT_VIRTUALEDIT
11430 (fp != NULL) ? (varnumber_T)fp->coladd :
11431#endif
11432 (varnumber_T)0);
11433 }
11434 else
11435 rettv->vval.v_number = FALSE;
11436}
11437
11438/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011439 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011440 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011441 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011442f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011443 typval_T *argvars UNUSED;
11444 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011445{
11446#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011447 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011448#endif
11449
Bram Moolenaar2641f772005-03-25 21:58:17 +000011450#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011451 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011452 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011453 wp = NULL;
11454 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11455 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011456 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011457 if (wp == NULL)
11458 return;
11459 }
11460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011461 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011462 }
11463#endif
11464}
11465
11466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 * "getreg()" function
11468 */
11469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011471 typval_T *argvars;
11472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473{
11474 char_u *strregname;
11475 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011476 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011479 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481 strregname = get_tv_string_chk(&argvars[0]);
11482 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011483 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011487 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 regname = (strregname == NULL ? '"' : *strregname);
11489 if (regname == 0)
11490 regname = '"';
11491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 rettv->vval.v_string = error ? NULL :
11494 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495}
11496
11497/*
11498 * "getregtype()" function
11499 */
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504{
11505 char_u *strregname;
11506 int regname;
11507 char_u buf[NUMBUFLEN + 2];
11508 long reglen = 0;
11509
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011510 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511 {
11512 strregname = get_tv_string_chk(&argvars[0]);
11513 if (strregname == NULL) /* type error; errmsg already given */
11514 {
11515 rettv->v_type = VAR_STRING;
11516 rettv->vval.v_string = NULL;
11517 return;
11518 }
11519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 else
11521 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523
11524 regname = (strregname == NULL ? '"' : *strregname);
11525 if (regname == 0)
11526 regname = '"';
11527
11528 buf[0] = NUL;
11529 buf[1] = NUL;
11530 switch (get_reg_type(regname, &reglen))
11531 {
11532 case MLINE: buf[0] = 'V'; break;
11533 case MCHAR: buf[0] = 'v'; break;
11534#ifdef FEAT_VISUAL
11535 case MBLOCK:
11536 buf[0] = Ctrl_V;
11537 sprintf((char *)buf + 1, "%ld", reglen + 1);
11538 break;
11539#endif
11540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->v_type = VAR_STRING;
11542 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543}
11544
11545/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011546 * "gettabvar()" function
11547 */
11548 static void
11549f_gettabvar(argvars, rettv)
11550 typval_T *argvars;
11551 typval_T *rettv;
11552{
11553 tabpage_T *tp;
11554 dictitem_T *v;
11555 char_u *varname;
11556
11557 rettv->v_type = VAR_STRING;
11558 rettv->vval.v_string = NULL;
11559
11560 varname = get_tv_string_chk(&argvars[1]);
11561 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11562 if (tp != NULL && varname != NULL)
11563 {
11564 /* look up the variable */
11565 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11566 if (v != NULL)
11567 copy_tv(&v->di_tv, rettv);
11568 }
11569}
11570
11571/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011572 * "gettabwinvar()" function
11573 */
11574 static void
11575f_gettabwinvar(argvars, rettv)
11576 typval_T *argvars;
11577 typval_T *rettv;
11578{
11579 getwinvar(argvars, rettv, 1);
11580}
11581
11582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 * "getwinposx()" function
11584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011587 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591#ifdef FEAT_GUI
11592 if (gui.in_use)
11593 {
11594 int x, y;
11595
11596 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 }
11599#endif
11600}
11601
11602/*
11603 * "getwinposy()" function
11604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611#ifdef FEAT_GUI
11612 if (gui.in_use)
11613 {
11614 int x, y;
11615
11616 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 }
11619#endif
11620}
11621
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011622/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011623 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011624 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011625 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011626find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011627 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011628 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011629{
11630#ifdef FEAT_WINDOWS
11631 win_T *wp;
11632#endif
11633 int nr;
11634
11635 nr = get_tv_number_chk(vp, NULL);
11636
11637#ifdef FEAT_WINDOWS
11638 if (nr < 0)
11639 return NULL;
11640 if (nr == 0)
11641 return curwin;
11642
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011643 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11644 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011645 if (--nr <= 0)
11646 break;
11647 return wp;
11648#else
11649 if (nr == 0 || nr == 1)
11650 return curwin;
11651 return NULL;
11652#endif
11653}
11654
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655/*
11656 * "getwinvar()" function
11657 */
11658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_getwinvar(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{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011663 getwinvar(argvars, rettv, 0);
11664}
11665
11666/*
11667 * getwinvar() and gettabwinvar()
11668 */
11669 static void
11670getwinvar(argvars, rettv, off)
11671 typval_T *argvars;
11672 typval_T *rettv;
11673 int off; /* 1 for gettabwinvar() */
11674{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 win_T *win, *oldcurwin;
11676 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011678 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011680#ifdef FEAT_WINDOWS
11681 if (off == 1)
11682 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11683 else
11684 tp = curtab;
11685#endif
11686 win = find_win_by_nr(&argvars[off], tp);
11687 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->v_type = VAR_STRING;
11691 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692
11693 if (win != NULL && varname != NULL)
11694 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011695 /* Set curwin to be our win, temporarily. Also set curbuf, so
11696 * that we can get buffer-local options. */
11697 oldcurwin = curwin;
11698 curwin = win;
11699 curbuf = win->w_buffer;
11700
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 else
11704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 if (*varname == NUL)
11706 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11707 * scope prefix before the NUL byte is required by
11708 * find_var_in_ht(). */
11709 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011711 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011713 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011715
11716 /* restore previous notion of curwin */
11717 curwin = oldcurwin;
11718 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 }
11720
11721 --emsg_off;
11722}
11723
11724/*
11725 * "glob()" function
11726 */
11727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011728f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 typval_T *argvars;
11730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011732 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011734 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011736 /* When the optional second argument is non-zero, don't remove matches
11737 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11738 if (argvars[1].v_type != VAR_UNKNOWN
11739 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011740 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011742 if (!error)
11743 {
11744 ExpandInit(&xpc);
11745 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011746 if (p_wic)
11747 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011748 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011749 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011750 }
11751 else
11752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753}
11754
11755/*
11756 * "globpath()" function
11757 */
11758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011760 typval_T *argvars;
11761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011763 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011766 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011768 /* When the optional second argument is non-zero, don't remove matches
11769 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11770 if (argvars[2].v_type != VAR_UNKNOWN
11771 && get_tv_number_chk(&argvars[2], &error))
11772 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011774 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775 rettv->vval.v_string = NULL;
11776 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011777 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11778 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779}
11780
11781/*
11782 * "has()" function
11783 */
11784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011786 typval_T *argvars;
11787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788{
11789 int i;
11790 char_u *name;
11791 int n = FALSE;
11792 static char *(has_list[]) =
11793 {
11794#ifdef AMIGA
11795 "amiga",
11796# ifdef FEAT_ARP
11797 "arp",
11798# endif
11799#endif
11800#ifdef __BEOS__
11801 "beos",
11802#endif
11803#ifdef MSDOS
11804# ifdef DJGPP
11805 "dos32",
11806# else
11807 "dos16",
11808# endif
11809#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011810#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 "mac",
11812#endif
11813#if defined(MACOS_X_UNIX)
11814 "macunix",
11815#endif
11816#ifdef OS2
11817 "os2",
11818#endif
11819#ifdef __QNX__
11820 "qnx",
11821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822#ifdef UNIX
11823 "unix",
11824#endif
11825#ifdef VMS
11826 "vms",
11827#endif
11828#ifdef WIN16
11829 "win16",
11830#endif
11831#ifdef WIN32
11832 "win32",
11833#endif
11834#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11835 "win32unix",
11836#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011837#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 "win64",
11839#endif
11840#ifdef EBCDIC
11841 "ebcdic",
11842#endif
11843#ifndef CASE_INSENSITIVE_FILENAME
11844 "fname_case",
11845#endif
11846#ifdef FEAT_ARABIC
11847 "arabic",
11848#endif
11849#ifdef FEAT_AUTOCMD
11850 "autocmd",
11851#endif
11852#ifdef FEAT_BEVAL
11853 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011854# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11855 "balloon_multiline",
11856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857#endif
11858#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11859 "builtin_terms",
11860# ifdef ALL_BUILTIN_TCAPS
11861 "all_builtin_terms",
11862# endif
11863#endif
11864#ifdef FEAT_BYTEOFF
11865 "byte_offset",
11866#endif
11867#ifdef FEAT_CINDENT
11868 "cindent",
11869#endif
11870#ifdef FEAT_CLIENTSERVER
11871 "clientserver",
11872#endif
11873#ifdef FEAT_CLIPBOARD
11874 "clipboard",
11875#endif
11876#ifdef FEAT_CMDL_COMPL
11877 "cmdline_compl",
11878#endif
11879#ifdef FEAT_CMDHIST
11880 "cmdline_hist",
11881#endif
11882#ifdef FEAT_COMMENTS
11883 "comments",
11884#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011885#ifdef FEAT_CONCEAL
11886 "conceal",
11887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888#ifdef FEAT_CRYPT
11889 "cryptv",
11890#endif
11891#ifdef FEAT_CSCOPE
11892 "cscope",
11893#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011894#ifdef FEAT_CURSORBIND
11895 "cursorbind",
11896#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011897#ifdef CURSOR_SHAPE
11898 "cursorshape",
11899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900#ifdef DEBUG
11901 "debug",
11902#endif
11903#ifdef FEAT_CON_DIALOG
11904 "dialog_con",
11905#endif
11906#ifdef FEAT_GUI_DIALOG
11907 "dialog_gui",
11908#endif
11909#ifdef FEAT_DIFF
11910 "diff",
11911#endif
11912#ifdef FEAT_DIGRAPHS
11913 "digraphs",
11914#endif
11915#ifdef FEAT_DND
11916 "dnd",
11917#endif
11918#ifdef FEAT_EMACS_TAGS
11919 "emacs_tags",
11920#endif
11921 "eval", /* always present, of course! */
11922#ifdef FEAT_EX_EXTRA
11923 "ex_extra",
11924#endif
11925#ifdef FEAT_SEARCH_EXTRA
11926 "extra_search",
11927#endif
11928#ifdef FEAT_FKMAP
11929 "farsi",
11930#endif
11931#ifdef FEAT_SEARCHPATH
11932 "file_in_path",
11933#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011934#if defined(UNIX) && !defined(USE_SYSTEM)
11935 "filterpipe",
11936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937#ifdef FEAT_FIND_ID
11938 "find_in_path",
11939#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011940#ifdef FEAT_FLOAT
11941 "float",
11942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943#ifdef FEAT_FOLDING
11944 "folding",
11945#endif
11946#ifdef FEAT_FOOTER
11947 "footer",
11948#endif
11949#if !defined(USE_SYSTEM) && defined(UNIX)
11950 "fork",
11951#endif
11952#ifdef FEAT_GETTEXT
11953 "gettext",
11954#endif
11955#ifdef FEAT_GUI
11956 "gui",
11957#endif
11958#ifdef FEAT_GUI_ATHENA
11959# ifdef FEAT_GUI_NEXTAW
11960 "gui_neXtaw",
11961# else
11962 "gui_athena",
11963# endif
11964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965#ifdef FEAT_GUI_GTK
11966 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011969#ifdef FEAT_GUI_GNOME
11970 "gui_gnome",
11971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972#ifdef FEAT_GUI_MAC
11973 "gui_mac",
11974#endif
11975#ifdef FEAT_GUI_MOTIF
11976 "gui_motif",
11977#endif
11978#ifdef FEAT_GUI_PHOTON
11979 "gui_photon",
11980#endif
11981#ifdef FEAT_GUI_W16
11982 "gui_win16",
11983#endif
11984#ifdef FEAT_GUI_W32
11985 "gui_win32",
11986#endif
11987#ifdef FEAT_HANGULIN
11988 "hangul_input",
11989#endif
11990#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11991 "iconv",
11992#endif
11993#ifdef FEAT_INS_EXPAND
11994 "insert_expand",
11995#endif
11996#ifdef FEAT_JUMPLIST
11997 "jumplist",
11998#endif
11999#ifdef FEAT_KEYMAP
12000 "keymap",
12001#endif
12002#ifdef FEAT_LANGMAP
12003 "langmap",
12004#endif
12005#ifdef FEAT_LIBCALL
12006 "libcall",
12007#endif
12008#ifdef FEAT_LINEBREAK
12009 "linebreak",
12010#endif
12011#ifdef FEAT_LISP
12012 "lispindent",
12013#endif
12014#ifdef FEAT_LISTCMDS
12015 "listcmds",
12016#endif
12017#ifdef FEAT_LOCALMAP
12018 "localmap",
12019#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012020#ifdef FEAT_LUA
12021# ifndef DYNAMIC_LUA
12022 "lua",
12023# endif
12024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025#ifdef FEAT_MENU
12026 "menu",
12027#endif
12028#ifdef FEAT_SESSION
12029 "mksession",
12030#endif
12031#ifdef FEAT_MODIFY_FNAME
12032 "modify_fname",
12033#endif
12034#ifdef FEAT_MOUSE
12035 "mouse",
12036#endif
12037#ifdef FEAT_MOUSESHAPE
12038 "mouseshape",
12039#endif
12040#if defined(UNIX) || defined(VMS)
12041# ifdef FEAT_MOUSE_DEC
12042 "mouse_dec",
12043# endif
12044# ifdef FEAT_MOUSE_GPM
12045 "mouse_gpm",
12046# endif
12047# ifdef FEAT_MOUSE_JSB
12048 "mouse_jsbterm",
12049# endif
12050# ifdef FEAT_MOUSE_NET
12051 "mouse_netterm",
12052# endif
12053# ifdef FEAT_MOUSE_PTERM
12054 "mouse_pterm",
12055# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012056# ifdef FEAT_SYSMOUSE
12057 "mouse_sysmouse",
12058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059# ifdef FEAT_MOUSE_XTERM
12060 "mouse_xterm",
12061# endif
12062#endif
12063#ifdef FEAT_MBYTE
12064 "multi_byte",
12065#endif
12066#ifdef FEAT_MBYTE_IME
12067 "multi_byte_ime",
12068#endif
12069#ifdef FEAT_MULTI_LANG
12070 "multi_lang",
12071#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012072#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012073#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012074 "mzscheme",
12075#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077#ifdef FEAT_OLE
12078 "ole",
12079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080#ifdef FEAT_PATH_EXTRA
12081 "path_extra",
12082#endif
12083#ifdef FEAT_PERL
12084#ifndef DYNAMIC_PERL
12085 "perl",
12086#endif
12087#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012088#ifdef FEAT_PERSISTENT_UNDO
12089 "persistent_undo",
12090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091#ifdef FEAT_PYTHON
12092#ifndef DYNAMIC_PYTHON
12093 "python",
12094#endif
12095#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012096#ifdef FEAT_PYTHON3
12097#ifndef DYNAMIC_PYTHON3
12098 "python3",
12099#endif
12100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101#ifdef FEAT_POSTSCRIPT
12102 "postscript",
12103#endif
12104#ifdef FEAT_PRINTER
12105 "printer",
12106#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012107#ifdef FEAT_PROFILE
12108 "profile",
12109#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012110#ifdef FEAT_RELTIME
12111 "reltime",
12112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113#ifdef FEAT_QUICKFIX
12114 "quickfix",
12115#endif
12116#ifdef FEAT_RIGHTLEFT
12117 "rightleft",
12118#endif
12119#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12120 "ruby",
12121#endif
12122#ifdef FEAT_SCROLLBIND
12123 "scrollbind",
12124#endif
12125#ifdef FEAT_CMDL_INFO
12126 "showcmd",
12127 "cmdline_info",
12128#endif
12129#ifdef FEAT_SIGNS
12130 "signs",
12131#endif
12132#ifdef FEAT_SMARTINDENT
12133 "smartindent",
12134#endif
12135#ifdef FEAT_SNIFF
12136 "sniff",
12137#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012138#ifdef STARTUPTIME
12139 "startuptime",
12140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#ifdef FEAT_STL_OPT
12142 "statusline",
12143#endif
12144#ifdef FEAT_SUN_WORKSHOP
12145 "sun_workshop",
12146#endif
12147#ifdef FEAT_NETBEANS_INTG
12148 "netbeans_intg",
12149#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012150#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012151 "spell",
12152#endif
12153#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 "syntax",
12155#endif
12156#if defined(USE_SYSTEM) || !defined(UNIX)
12157 "system",
12158#endif
12159#ifdef FEAT_TAG_BINS
12160 "tag_binary",
12161#endif
12162#ifdef FEAT_TAG_OLDSTATIC
12163 "tag_old_static",
12164#endif
12165#ifdef FEAT_TAG_ANYWHITE
12166 "tag_any_white",
12167#endif
12168#ifdef FEAT_TCL
12169# ifndef DYNAMIC_TCL
12170 "tcl",
12171# endif
12172#endif
12173#ifdef TERMINFO
12174 "terminfo",
12175#endif
12176#ifdef FEAT_TERMRESPONSE
12177 "termresponse",
12178#endif
12179#ifdef FEAT_TEXTOBJ
12180 "textobjects",
12181#endif
12182#ifdef HAVE_TGETENT
12183 "tgetent",
12184#endif
12185#ifdef FEAT_TITLE
12186 "title",
12187#endif
12188#ifdef FEAT_TOOLBAR
12189 "toolbar",
12190#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012191#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12192 "unnamedplus",
12193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#ifdef FEAT_USR_CMDS
12195 "user-commands", /* was accidentally included in 5.4 */
12196 "user_commands",
12197#endif
12198#ifdef FEAT_VIMINFO
12199 "viminfo",
12200#endif
12201#ifdef FEAT_VERTSPLIT
12202 "vertsplit",
12203#endif
12204#ifdef FEAT_VIRTUALEDIT
12205 "virtualedit",
12206#endif
12207#ifdef FEAT_VISUAL
12208 "visual",
12209#endif
12210#ifdef FEAT_VISUALEXTRA
12211 "visualextra",
12212#endif
12213#ifdef FEAT_VREPLACE
12214 "vreplace",
12215#endif
12216#ifdef FEAT_WILDIGN
12217 "wildignore",
12218#endif
12219#ifdef FEAT_WILDMENU
12220 "wildmenu",
12221#endif
12222#ifdef FEAT_WINDOWS
12223 "windows",
12224#endif
12225#ifdef FEAT_WAK
12226 "winaltkeys",
12227#endif
12228#ifdef FEAT_WRITEBACKUP
12229 "writebackup",
12230#endif
12231#ifdef FEAT_XIM
12232 "xim",
12233#endif
12234#ifdef FEAT_XFONTSET
12235 "xfontset",
12236#endif
12237#ifdef USE_XSMP
12238 "xsmp",
12239#endif
12240#ifdef USE_XSMP_INTERACT
12241 "xsmp_interact",
12242#endif
12243#ifdef FEAT_XCLIPBOARD
12244 "xterm_clipboard",
12245#endif
12246#ifdef FEAT_XTERM_SAVE
12247 "xterm_save",
12248#endif
12249#if defined(UNIX) && defined(FEAT_X11)
12250 "X11",
12251#endif
12252 NULL
12253 };
12254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 for (i = 0; has_list[i] != NULL; ++i)
12257 if (STRICMP(name, has_list[i]) == 0)
12258 {
12259 n = TRUE;
12260 break;
12261 }
12262
12263 if (n == FALSE)
12264 {
12265 if (STRNICMP(name, "patch", 5) == 0)
12266 n = has_patch(atoi((char *)name + 5));
12267 else if (STRICMP(name, "vim_starting") == 0)
12268 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012269#ifdef FEAT_MBYTE
12270 else if (STRICMP(name, "multi_byte_encoding") == 0)
12271 n = has_mbyte;
12272#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012273#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12274 else if (STRICMP(name, "balloon_multiline") == 0)
12275 n = multiline_balloon_available();
12276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277#ifdef DYNAMIC_TCL
12278 else if (STRICMP(name, "tcl") == 0)
12279 n = tcl_enabled(FALSE);
12280#endif
12281#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12282 else if (STRICMP(name, "iconv") == 0)
12283 n = iconv_enabled(FALSE);
12284#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012285#ifdef DYNAMIC_LUA
12286 else if (STRICMP(name, "lua") == 0)
12287 n = lua_enabled(FALSE);
12288#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012289#ifdef DYNAMIC_MZSCHEME
12290 else if (STRICMP(name, "mzscheme") == 0)
12291 n = mzscheme_enabled(FALSE);
12292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293#ifdef DYNAMIC_RUBY
12294 else if (STRICMP(name, "ruby") == 0)
12295 n = ruby_enabled(FALSE);
12296#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012297#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#ifdef DYNAMIC_PYTHON
12299 else if (STRICMP(name, "python") == 0)
12300 n = python_enabled(FALSE);
12301#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012302#endif
12303#ifdef FEAT_PYTHON3
12304#ifdef DYNAMIC_PYTHON3
12305 else if (STRICMP(name, "python3") == 0)
12306 n = python3_enabled(FALSE);
12307#endif
12308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309#ifdef DYNAMIC_PERL
12310 else if (STRICMP(name, "perl") == 0)
12311 n = perl_enabled(FALSE);
12312#endif
12313#ifdef FEAT_GUI
12314 else if (STRICMP(name, "gui_running") == 0)
12315 n = (gui.in_use || gui.starting);
12316# ifdef FEAT_GUI_W32
12317 else if (STRICMP(name, "gui_win32s") == 0)
12318 n = gui_is_win32s();
12319# endif
12320# ifdef FEAT_BROWSE
12321 else if (STRICMP(name, "browse") == 0)
12322 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12323# endif
12324#endif
12325#ifdef FEAT_SYN_HL
12326 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012327 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328#endif
12329#if defined(WIN3264)
12330 else if (STRICMP(name, "win95") == 0)
12331 n = mch_windows95();
12332#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012333#ifdef FEAT_NETBEANS_INTG
12334 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012335 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 }
12338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340}
12341
12342/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012343 * "has_key()" function
12344 */
12345 static void
12346f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012347 typval_T *argvars;
12348 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012349{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012350 if (argvars[0].v_type != VAR_DICT)
12351 {
12352 EMSG(_(e_dictreq));
12353 return;
12354 }
12355 if (argvars[0].vval.v_dict == NULL)
12356 return;
12357
12358 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012359 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012360}
12361
12362/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012363 * "haslocaldir()" function
12364 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012365 static void
12366f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012367 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012368 typval_T *rettv;
12369{
12370 rettv->vval.v_number = (curwin->w_localdir != NULL);
12371}
12372
12373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 * "hasmapto()" function
12375 */
12376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *argvars;
12379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380{
12381 char_u *name;
12382 char_u *mode;
12383 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012384 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012386 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 mode = (char_u *)"nvo";
12389 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012392 if (argvars[2].v_type != VAR_UNKNOWN)
12393 abbr = get_tv_number(&argvars[2]);
12394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395
Bram Moolenaar2c932302006-03-18 21:42:09 +000012396 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400}
12401
12402/*
12403 * "histadd()" function
12404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
12410#ifdef FEAT_CMDHIST
12411 int histype;
12412 char_u *str;
12413 char_u buf[NUMBUFLEN];
12414#endif
12415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 if (check_restricted() || check_secure())
12418 return;
12419#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012420 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12421 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 if (histype >= 0)
12423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 if (*str != NUL)
12426 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012427 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 return;
12431 }
12432 }
12433#endif
12434}
12435
12436/*
12437 * "histdel()" function
12438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012441 typval_T *argvars UNUSED;
12442 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
12444#ifdef FEAT_CMDHIST
12445 int n;
12446 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012447 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012449 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12450 if (str == NULL)
12451 n = 0;
12452 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012455 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 else
12460 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012461 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462 get_tv_string_buf(&argvars[1], buf));
12463 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464#endif
12465}
12466
12467/*
12468 * "histget()" function
12469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012472 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474{
12475#ifdef FEAT_CMDHIST
12476 int type;
12477 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012478 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012480 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12481 if (str == NULL)
12482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 {
12485 type = get_histtype(str);
12486 if (argvars[1].v_type == VAR_UNKNOWN)
12487 idx = get_history_idx(type);
12488 else
12489 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12490 /* -1 on type error */
12491 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497}
12498
12499/*
12500 * "histnr()" function
12501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506{
12507 int i;
12508
12509#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 char_u *history = get_tv_string_chk(&argvars[0]);
12511
12512 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 if (i >= HIST_CMD && i < HIST_COUNT)
12514 i = get_history_idx(i);
12515 else
12516#endif
12517 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519}
12520
12521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 * "highlightID(name)" function
12523 */
12524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 typval_T *argvars;
12527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530}
12531
12532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 * "highlight_exists()" function
12534 */
12535 static void
12536f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 typval_T *argvars;
12538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539{
12540 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12541}
12542
12543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 * "hostname()" function
12545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012548 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550{
12551 char_u hostname[256];
12552
12553 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554 rettv->v_type = VAR_STRING;
12555 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556}
12557
12558/*
12559 * iconv() function
12560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565{
12566#ifdef FEAT_MBYTE
12567 char_u buf1[NUMBUFLEN];
12568 char_u buf2[NUMBUFLEN];
12569 char_u *from, *to, *str;
12570 vimconv_T vimconv;
12571#endif
12572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573 rettv->v_type = VAR_STRING;
12574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575
12576#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577 str = get_tv_string(&argvars[0]);
12578 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12579 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 vimconv.vc_type = CONV_NONE;
12581 convert_setup(&vimconv, from, to);
12582
12583 /* If the encodings are equal, no conversion needed. */
12584 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588
12589 convert_setup(&vimconv, NULL, NULL);
12590 vim_free(from);
12591 vim_free(to);
12592#endif
12593}
12594
12595/*
12596 * "indent()" function
12597 */
12598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 typval_T *argvars;
12601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602{
12603 linenr_T lnum;
12604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610}
12611
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012612/*
12613 * "index()" function
12614 */
12615 static void
12616f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012617 typval_T *argvars;
12618 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012619{
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 list_T *l;
12621 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012622 long idx = 0;
12623 int ic = FALSE;
12624
12625 rettv->vval.v_number = -1;
12626 if (argvars[0].v_type != VAR_LIST)
12627 {
12628 EMSG(_(e_listreq));
12629 return;
12630 }
12631 l = argvars[0].vval.v_list;
12632 if (l != NULL)
12633 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012634 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012635 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 int error = FALSE;
12638
Bram Moolenaar758711c2005-02-02 23:11:38 +000012639 /* Start at specified item. Use the cached index that list_find()
12640 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012642 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012643 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 ic = get_tv_number_chk(&argvars[3], &error);
12645 if (error)
12646 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012648
Bram Moolenaar758711c2005-02-02 23:11:38 +000012649 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012650 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012651 {
12652 rettv->vval.v_number = idx;
12653 break;
12654 }
12655 }
12656}
12657
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658static int inputsecret_flag = 0;
12659
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012660static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12661
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012663 * This function is used by f_input() and f_inputdialog() functions. The third
12664 * argument to f_input() specifies the type of completion to use at the
12665 * prompt. The third argument to f_inputdialog() specifies the value to return
12666 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 */
12668 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012669get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012670 typval_T *argvars;
12671 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012672 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 char_u *p = NULL;
12676 int c;
12677 char_u buf[NUMBUFLEN];
12678 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012680 int xp_type = EXPAND_NOTHING;
12681 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012683 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012684 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685
12686#ifdef NO_CONSOLE_INPUT
12687 /* While starting up, there is no place to enter text. */
12688 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690#endif
12691
12692 cmd_silent = FALSE; /* Want to see the prompt. */
12693 if (prompt != NULL)
12694 {
12695 /* Only the part of the message after the last NL is considered as
12696 * prompt for the command line */
12697 p = vim_strrchr(prompt, '\n');
12698 if (p == NULL)
12699 p = prompt;
12700 else
12701 {
12702 ++p;
12703 c = *p;
12704 *p = NUL;
12705 msg_start();
12706 msg_clr_eos();
12707 msg_puts_attr(prompt, echo_attr);
12708 msg_didout = FALSE;
12709 msg_starthere();
12710 *p = c;
12711 }
12712 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 if (argvars[1].v_type != VAR_UNKNOWN)
12715 {
12716 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12717 if (defstr != NULL)
12718 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012720 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012721 {
12722 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012723 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012724 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012725
Bram Moolenaar4463f292005-09-25 22:20:24 +000012726 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012727
Bram Moolenaar4463f292005-09-25 22:20:24 +000012728 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12729 if (xp_name == NULL)
12730 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012731
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012732 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012733
Bram Moolenaar4463f292005-09-25 22:20:24 +000012734 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12735 &xp_arg) == FAIL)
12736 return;
12737 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012738 }
12739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 if (defstr != NULL)
12741 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012742 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12743 xp_type, xp_arg);
12744
12745 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 /* since the user typed this, no need to wait for return */
12748 need_wait_return = FALSE;
12749 msg_didout = FALSE;
12750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 cmd_silent = cmd_silent_save;
12752}
12753
12754/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012755 * "input()" function
12756 * Also handles inputsecret() when inputsecret is set.
12757 */
12758 static void
12759f_input(argvars, rettv)
12760 typval_T *argvars;
12761 typval_T *rettv;
12762{
12763 get_user_input(argvars, rettv, FALSE);
12764}
12765
12766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767 * "inputdialog()" function
12768 */
12769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *argvars;
12772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773{
12774#if defined(FEAT_GUI_TEXTDIALOG)
12775 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12776 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12777 {
12778 char_u *message;
12779 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012780 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 message = get_tv_string_chk(&argvars[0]);
12783 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012784 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012785 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 else
12787 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 if (message != NULL && defstr != NULL
12789 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012790 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 else
12793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012794 if (message != NULL && defstr != NULL
12795 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012796 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_string = vim_strsave(
12798 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 }
12804 else
12805#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012806 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807}
12808
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012809/*
12810 * "inputlist()" function
12811 */
12812 static void
12813f_inputlist(argvars, rettv)
12814 typval_T *argvars;
12815 typval_T *rettv;
12816{
12817 listitem_T *li;
12818 int selected;
12819 int mouse_used;
12820
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012821#ifdef NO_CONSOLE_INPUT
12822 /* While starting up, there is no place to enter text. */
12823 if (no_console_input())
12824 return;
12825#endif
12826 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12827 {
12828 EMSG2(_(e_listarg), "inputlist()");
12829 return;
12830 }
12831
12832 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012833 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012834 lines_left = Rows; /* avoid more prompt */
12835 msg_scroll = TRUE;
12836 msg_clr_eos();
12837
12838 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12839 {
12840 msg_puts(get_tv_string(&li->li_tv));
12841 msg_putchar('\n');
12842 }
12843
12844 /* Ask for choice. */
12845 selected = prompt_for_number(&mouse_used);
12846 if (mouse_used)
12847 selected -= lines_left;
12848
12849 rettv->vval.v_number = selected;
12850}
12851
12852
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12854
12855/*
12856 * "inputrestore()" function
12857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012860 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
12863 if (ga_userinput.ga_len > 0)
12864 {
12865 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12867 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012868 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 }
12870 else if (p_verbose > 1)
12871 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012872 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 }
12875}
12876
12877/*
12878 * "inputsave()" function
12879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012882 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012885 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 if (ga_grow(&ga_userinput, 1) == OK)
12887 {
12888 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12889 + ga_userinput.ga_len);
12890 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012891 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 }
12893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895}
12896
12897/*
12898 * "inputsecret()" function
12899 */
12900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012902 typval_T *argvars;
12903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904{
12905 ++cmdline_star;
12906 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 --cmdline_star;
12909 --inputsecret_flag;
12910}
12911
12912/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012913 * "insert()" function
12914 */
12915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012917 typval_T *argvars;
12918 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012919{
12920 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012921 listitem_T *item;
12922 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012924
12925 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012926 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012927 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012928 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012929 {
12930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 before = get_tv_number_chk(&argvars[2], &error);
12932 if (error)
12933 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012934
Bram Moolenaar758711c2005-02-02 23:11:38 +000012935 if (before == l->lv_len)
12936 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012937 else
12938 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012939 item = list_find(l, before);
12940 if (item == NULL)
12941 {
12942 EMSGN(_(e_listidx), before);
12943 l = NULL;
12944 }
12945 }
12946 if (l != NULL)
12947 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012948 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012949 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012950 }
12951 }
12952}
12953
12954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 * "isdirectory()" function
12956 */
12957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012959 typval_T *argvars;
12960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963}
12964
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012965/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012966 * "islocked()" function
12967 */
12968 static void
12969f_islocked(argvars, rettv)
12970 typval_T *argvars;
12971 typval_T *rettv;
12972{
12973 lval_T lv;
12974 char_u *end;
12975 dictitem_T *di;
12976
12977 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012978 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12979 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012980 if (end != NULL && lv.ll_name != NULL)
12981 {
12982 if (*end != NUL)
12983 EMSG(_(e_trailing));
12984 else
12985 {
12986 if (lv.ll_tv == NULL)
12987 {
12988 if (check_changedtick(lv.ll_name))
12989 rettv->vval.v_number = 1; /* always locked */
12990 else
12991 {
12992 di = find_var(lv.ll_name, NULL);
12993 if (di != NULL)
12994 {
12995 /* Consider a variable locked when:
12996 * 1. the variable itself is locked
12997 * 2. the value of the variable is locked.
12998 * 3. the List or Dict value is locked.
12999 */
13000 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13001 || tv_islocked(&di->di_tv));
13002 }
13003 }
13004 }
13005 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013006 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013007 else if (lv.ll_newkey != NULL)
13008 EMSG2(_(e_dictkey), lv.ll_newkey);
13009 else if (lv.ll_list != NULL)
13010 /* List item. */
13011 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13012 else
13013 /* Dictionary item. */
13014 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13015 }
13016 }
13017
13018 clear_lval(&lv);
13019}
13020
Bram Moolenaar33570922005-01-25 22:26:29 +000013021static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013022
13023/*
13024 * Turn a dict into a list:
13025 * "what" == 0: list of keys
13026 * "what" == 1: list of values
13027 * "what" == 2: list of items
13028 */
13029 static void
13030dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013031 typval_T *argvars;
13032 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013033 int what;
13034{
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 list_T *l2;
13036 dictitem_T *di;
13037 hashitem_T *hi;
13038 listitem_T *li;
13039 listitem_T *li2;
13040 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013041 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013042
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 if (argvars[0].v_type != VAR_DICT)
13044 {
13045 EMSG(_(e_dictreq));
13046 return;
13047 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013048 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013049 return;
13050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013052 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013053
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013054 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013055 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013056 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013057 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013059 --todo;
13060 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013062 li = listitem_alloc();
13063 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013064 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013065 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013066
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013067 if (what == 0)
13068 {
13069 /* keys() */
13070 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013071 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013072 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13073 }
13074 else if (what == 1)
13075 {
13076 /* values() */
13077 copy_tv(&di->di_tv, &li->li_tv);
13078 }
13079 else
13080 {
13081 /* items() */
13082 l2 = list_alloc();
13083 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013084 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013085 li->li_tv.vval.v_list = l2;
13086 if (l2 == NULL)
13087 break;
13088 ++l2->lv_refcount;
13089
13090 li2 = listitem_alloc();
13091 if (li2 == NULL)
13092 break;
13093 list_append(l2, li2);
13094 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013095 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013096 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13097
13098 li2 = listitem_alloc();
13099 if (li2 == NULL)
13100 break;
13101 list_append(l2, li2);
13102 copy_tv(&di->di_tv, &li2->li_tv);
13103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013104 }
13105 }
13106}
13107
13108/*
13109 * "items(dict)" function
13110 */
13111 static void
13112f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *argvars;
13114 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013115{
13116 dict_list(argvars, rettv, 2);
13117}
13118
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013120 * "join()" function
13121 */
13122 static void
13123f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013124 typval_T *argvars;
13125 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013126{
13127 garray_T ga;
13128 char_u *sep;
13129
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013130 if (argvars[0].v_type != VAR_LIST)
13131 {
13132 EMSG(_(e_listreq));
13133 return;
13134 }
13135 if (argvars[0].vval.v_list == NULL)
13136 return;
13137 if (argvars[1].v_type == VAR_UNKNOWN)
13138 sep = (char_u *)" ";
13139 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013141
13142 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013143
13144 if (sep != NULL)
13145 {
13146 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013147 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013148 ga_append(&ga, NUL);
13149 rettv->vval.v_string = (char_u *)ga.ga_data;
13150 }
13151 else
13152 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013153}
13154
13155/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013156 * "keys()" function
13157 */
13158 static void
13159f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013160 typval_T *argvars;
13161 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013162{
13163 dict_list(argvars, rettv, 0);
13164}
13165
13166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 * "last_buffer_nr()" function.
13168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173{
13174 int n = 0;
13175 buf_T *buf;
13176
13177 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13178 if (n < buf->b_fnum)
13179 n = buf->b_fnum;
13180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013182}
13183
13184/*
13185 * "len()" function
13186 */
13187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013189 typval_T *argvars;
13190 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013191{
13192 switch (argvars[0].v_type)
13193 {
13194 case VAR_STRING:
13195 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 rettv->vval.v_number = (varnumber_T)STRLEN(
13197 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198 break;
13199 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013201 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013202 case VAR_DICT:
13203 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13204 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013206 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013207 break;
13208 }
13209}
13210
Bram Moolenaar33570922005-01-25 22:26:29 +000013211static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212
13213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 typval_T *argvars;
13216 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217 int type;
13218{
13219#ifdef FEAT_LIBCALL
13220 char_u *string_in;
13221 char_u **string_result;
13222 int nr_result;
13223#endif
13224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013226 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228
13229 if (check_restricted() || check_secure())
13230 return;
13231
13232#ifdef FEAT_LIBCALL
13233 /* The first two args must be strings, otherwise its meaningless */
13234 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13235 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013236 string_in = NULL;
13237 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013238 string_in = argvars[2].vval.v_string;
13239 if (type == VAR_NUMBER)
13240 string_result = NULL;
13241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013243 if (mch_libcall(argvars[0].vval.v_string,
13244 argvars[1].vval.v_string,
13245 string_in,
13246 argvars[2].vval.v_number,
13247 string_result,
13248 &nr_result) == OK
13249 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013251 }
13252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253}
13254
13255/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256 * "libcall()" function
13257 */
13258 static void
13259f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013260 typval_T *argvars;
13261 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013262{
13263 libcall_common(argvars, rettv, VAR_STRING);
13264}
13265
13266/*
13267 * "libcallnr()" function
13268 */
13269 static void
13270f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 typval_T *argvars;
13272 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013273{
13274 libcall_common(argvars, rettv, VAR_NUMBER);
13275}
13276
13277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 * "line(string)" function
13279 */
13280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013282 typval_T *argvars;
13283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284{
13285 linenr_T lnum = 0;
13286 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013287 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013289 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 if (fp != NULL)
13291 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293}
13294
13295/*
13296 * "line2byte(lnum)" function
13297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013299f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013300 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302{
13303#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305#else
13306 linenr_T lnum;
13307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13313 if (rettv->vval.v_number >= 0)
13314 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#endif
13316}
13317
13318/*
13319 * "lispindent(lnum)" function
13320 */
13321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013323 typval_T *argvars;
13324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325{
13326#ifdef FEAT_LISP
13327 pos_T pos;
13328 linenr_T lnum;
13329
13330 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13333 {
13334 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 curwin->w_cursor = pos;
13337 }
13338 else
13339#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341}
13342
13343/*
13344 * "localtime()" function
13345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013351 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352}
13353
Bram Moolenaar33570922005-01-25 22:26:29 +000013354static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355
13356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013358 typval_T *argvars;
13359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 int exact;
13361{
13362 char_u *keys;
13363 char_u *which;
13364 char_u buf[NUMBUFLEN];
13365 char_u *keys_buf = NULL;
13366 char_u *rhs;
13367 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013368 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013369 int get_dict = FALSE;
13370 mapblock_T *mp;
13371 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372
13373 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->v_type = VAR_STRING;
13375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 if (*keys == NUL)
13379 return;
13380
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013381 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013384 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013385 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013386 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013387 if (argvars[3].v_type != VAR_UNKNOWN)
13388 get_dict = get_tv_number(&argvars[3]);
13389 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 else
13392 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013393 if (which == NULL)
13394 return;
13395
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 mode = get_map_mode(&which, 0);
13397
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013398 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013399 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013401
13402 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013404 /* Return a string. */
13405 if (rhs != NULL)
13406 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407
Bram Moolenaarbd743252010-10-20 21:23:33 +020013408 }
13409 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13410 {
13411 /* Return a dictionary. */
13412 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13413 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13414 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415
Bram Moolenaarbd743252010-10-20 21:23:33 +020013416 dict_add_nr_str(dict, "lhs", 0L, lhs);
13417 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13418 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13419 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13420 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13421 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13422 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13423 dict_add_nr_str(dict, "mode", 0L, mapmode);
13424
13425 vim_free(lhs);
13426 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 }
13428}
13429
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013430#ifdef FEAT_FLOAT
13431/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013432 * "log()" function
13433 */
13434 static void
13435f_log(argvars, rettv)
13436 typval_T *argvars;
13437 typval_T *rettv;
13438{
13439 float_T f;
13440
13441 rettv->v_type = VAR_FLOAT;
13442 if (get_float_arg(argvars, &f) == OK)
13443 rettv->vval.v_float = log(f);
13444 else
13445 rettv->vval.v_float = 0.0;
13446}
13447
13448/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013449 * "log10()" function
13450 */
13451 static void
13452f_log10(argvars, rettv)
13453 typval_T *argvars;
13454 typval_T *rettv;
13455{
13456 float_T f;
13457
13458 rettv->v_type = VAR_FLOAT;
13459 if (get_float_arg(argvars, &f) == OK)
13460 rettv->vval.v_float = log10(f);
13461 else
13462 rettv->vval.v_float = 0.0;
13463}
13464#endif
13465
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013467 * "map()" function
13468 */
13469 static void
13470f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013471 typval_T *argvars;
13472 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013473{
13474 filter_map(argvars, rettv, TRUE);
13475}
13476
13477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013478 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 */
13480 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486}
13487
13488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013489 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 */
13491 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497}
13498
Bram Moolenaar33570922005-01-25 22:26:29 +000013499static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500
13501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *argvars;
13504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505 int type;
13506{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013507 char_u *str = NULL;
13508 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 char_u *pat;
13510 regmatch_T regmatch;
13511 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 char_u *save_cpo;
13514 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013515 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013516 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013517 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 list_T *l = NULL;
13519 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013520 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013521 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522
13523 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13524 save_cpo = p_cpo;
13525 p_cpo = (char_u *)"";
13526
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013527 rettv->vval.v_number = -1;
13528 if (type == 3)
13529 {
13530 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013531 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013532 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013533 }
13534 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 rettv->v_type = VAR_STRING;
13537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013540 if (argvars[0].v_type == VAR_LIST)
13541 {
13542 if ((l = argvars[0].vval.v_list) == NULL)
13543 goto theend;
13544 li = l->lv_first;
13545 }
13546 else
13547 expr = str = get_tv_string(&argvars[0]);
13548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13550 if (pat == NULL)
13551 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013552
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 int error = FALSE;
13556
13557 start = get_tv_number_chk(&argvars[2], &error);
13558 if (error)
13559 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013560 if (l != NULL)
13561 {
13562 li = list_find(l, start);
13563 if (li == NULL)
13564 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013565 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013566 }
13567 else
13568 {
13569 if (start < 0)
13570 start = 0;
13571 if (start > (long)STRLEN(str))
13572 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013573 /* When "count" argument is there ignore matches before "start",
13574 * otherwise skip part of the string. Differs when pattern is "^"
13575 * or "\<". */
13576 if (argvars[3].v_type != VAR_UNKNOWN)
13577 startcol = start;
13578 else
13579 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013581
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013582 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 nth = get_tv_number_chk(&argvars[3], &error);
13584 if (error)
13585 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 }
13587
13588 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13589 if (regmatch.regprog != NULL)
13590 {
13591 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013592
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013593 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013594 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013595 if (l != NULL)
13596 {
13597 if (li == NULL)
13598 {
13599 match = FALSE;
13600 break;
13601 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013602 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013603 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013604 if (str == NULL)
13605 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013606 }
13607
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013608 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013609
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013610 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013611 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013612 if (l == NULL && !match)
13613 break;
13614
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013615 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013616 if (l != NULL)
13617 {
13618 li = li->li_next;
13619 ++idx;
13620 }
13621 else
13622 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013623#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013624 startcol = (colnr_T)(regmatch.startp[0]
13625 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013626#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013627 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013628#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013629 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013630 }
13631
13632 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013634 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013635 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013636 int i;
13637
13638 /* return list with matched string and submatches */
13639 for (i = 0; i < NSUBEXP; ++i)
13640 {
13641 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013642 {
13643 if (list_append_string(rettv->vval.v_list,
13644 (char_u *)"", 0) == FAIL)
13645 break;
13646 }
13647 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013648 regmatch.startp[i],
13649 (int)(regmatch.endp[i] - regmatch.startp[i]))
13650 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013651 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013652 }
13653 }
13654 else if (type == 2)
13655 {
13656 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013657 if (l != NULL)
13658 copy_tv(&li->li_tv, rettv);
13659 else
13660 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013662 }
13663 else if (l != NULL)
13664 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 else
13666 {
13667 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 (varnumber_T)(regmatch.startp[0] - str);
13670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013673 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 }
13675 }
13676 vim_free(regmatch.regprog);
13677 }
13678
13679theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013680 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 p_cpo = save_cpo;
13682}
13683
13684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013685 * "match()" function
13686 */
13687 static void
13688f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013689 typval_T *argvars;
13690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691{
13692 find_some_match(argvars, rettv, 1);
13693}
13694
13695/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013696 * "matchadd()" function
13697 */
13698 static void
13699f_matchadd(argvars, rettv)
13700 typval_T *argvars;
13701 typval_T *rettv;
13702{
13703#ifdef FEAT_SEARCH_EXTRA
13704 char_u buf[NUMBUFLEN];
13705 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13706 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13707 int prio = 10; /* default priority */
13708 int id = -1;
13709 int error = FALSE;
13710
13711 rettv->vval.v_number = -1;
13712
13713 if (grp == NULL || pat == NULL)
13714 return;
13715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013716 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013717 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013718 if (argvars[3].v_type != VAR_UNKNOWN)
13719 id = get_tv_number_chk(&argvars[3], &error);
13720 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013721 if (error == TRUE)
13722 return;
13723 if (id >= 1 && id <= 3)
13724 {
13725 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13726 return;
13727 }
13728
13729 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13730#endif
13731}
13732
13733/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013734 * "matcharg()" function
13735 */
13736 static void
13737f_matcharg(argvars, rettv)
13738 typval_T *argvars;
13739 typval_T *rettv;
13740{
13741 if (rettv_list_alloc(rettv) == OK)
13742 {
13743#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013744 int id = get_tv_number(&argvars[0]);
13745 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013746
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013747 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013748 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013749 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13750 {
13751 list_append_string(rettv->vval.v_list,
13752 syn_id2name(m->hlg_id), -1);
13753 list_append_string(rettv->vval.v_list, m->pattern, -1);
13754 }
13755 else
13756 {
13757 list_append_string(rettv->vval.v_list, NUL, -1);
13758 list_append_string(rettv->vval.v_list, NUL, -1);
13759 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013760 }
13761#endif
13762 }
13763}
13764
13765/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013766 * "matchdelete()" function
13767 */
13768 static void
13769f_matchdelete(argvars, rettv)
13770 typval_T *argvars;
13771 typval_T *rettv;
13772{
13773#ifdef FEAT_SEARCH_EXTRA
13774 rettv->vval.v_number = match_delete(curwin,
13775 (int)get_tv_number(&argvars[0]), TRUE);
13776#endif
13777}
13778
13779/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013780 * "matchend()" function
13781 */
13782 static void
13783f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *argvars;
13785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786{
13787 find_some_match(argvars, rettv, 0);
13788}
13789
13790/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013791 * "matchlist()" function
13792 */
13793 static void
13794f_matchlist(argvars, rettv)
13795 typval_T *argvars;
13796 typval_T *rettv;
13797{
13798 find_some_match(argvars, rettv, 3);
13799}
13800
13801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802 * "matchstr()" function
13803 */
13804 static void
13805f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 typval_T *argvars;
13807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808{
13809 find_some_match(argvars, rettv, 2);
13810}
13811
Bram Moolenaar33570922005-01-25 22:26:29 +000013812static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013813
13814 static void
13815max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013816 typval_T *argvars;
13817 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013818 int domax;
13819{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013820 long n = 0;
13821 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013823
13824 if (argvars[0].v_type == VAR_LIST)
13825 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 list_T *l;
13827 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013828
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013829 l = argvars[0].vval.v_list;
13830 if (l != NULL)
13831 {
13832 li = l->lv_first;
13833 if (li != NULL)
13834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013836 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013837 {
13838 li = li->li_next;
13839 if (li == NULL)
13840 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013842 if (domax ? i > n : i < n)
13843 n = i;
13844 }
13845 }
13846 }
13847 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013848 else if (argvars[0].v_type == VAR_DICT)
13849 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013851 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013853 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013854
13855 d = argvars[0].vval.v_dict;
13856 if (d != NULL)
13857 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013858 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013859 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013860 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013861 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013862 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013863 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013865 if (first)
13866 {
13867 n = i;
13868 first = FALSE;
13869 }
13870 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013871 n = i;
13872 }
13873 }
13874 }
13875 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013876 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013877 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013879}
13880
13881/*
13882 * "max()" function
13883 */
13884 static void
13885f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013886 typval_T *argvars;
13887 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013888{
13889 max_min(argvars, rettv, TRUE);
13890}
13891
13892/*
13893 * "min()" function
13894 */
13895 static void
13896f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013897 typval_T *argvars;
13898 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013899{
13900 max_min(argvars, rettv, FALSE);
13901}
13902
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013903static int mkdir_recurse __ARGS((char_u *dir, int prot));
13904
13905/*
13906 * Create the directory in which "dir" is located, and higher levels when
13907 * needed.
13908 */
13909 static int
13910mkdir_recurse(dir, prot)
13911 char_u *dir;
13912 int prot;
13913{
13914 char_u *p;
13915 char_u *updir;
13916 int r = FAIL;
13917
13918 /* Get end of directory name in "dir".
13919 * We're done when it's "/" or "c:/". */
13920 p = gettail_sep(dir);
13921 if (p <= get_past_head(dir))
13922 return OK;
13923
13924 /* If the directory exists we're done. Otherwise: create it.*/
13925 updir = vim_strnsave(dir, (int)(p - dir));
13926 if (updir == NULL)
13927 return FAIL;
13928 if (mch_isdir(updir))
13929 r = OK;
13930 else if (mkdir_recurse(updir, prot) == OK)
13931 r = vim_mkdir_emsg(updir, prot);
13932 vim_free(updir);
13933 return r;
13934}
13935
13936#ifdef vim_mkdir
13937/*
13938 * "mkdir()" function
13939 */
13940 static void
13941f_mkdir(argvars, rettv)
13942 typval_T *argvars;
13943 typval_T *rettv;
13944{
13945 char_u *dir;
13946 char_u buf[NUMBUFLEN];
13947 int prot = 0755;
13948
13949 rettv->vval.v_number = FAIL;
13950 if (check_restricted() || check_secure())
13951 return;
13952
13953 dir = get_tv_string_buf(&argvars[0], buf);
13954 if (argvars[1].v_type != VAR_UNKNOWN)
13955 {
13956 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013957 prot = get_tv_number_chk(&argvars[2], NULL);
13958 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013959 mkdir_recurse(dir, prot);
13960 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013962}
13963#endif
13964
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 * "mode()" function
13967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013970 typval_T *argvars;
13971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013973 char_u buf[3];
13974
13975 buf[1] = NUL;
13976 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977
13978#ifdef FEAT_VISUAL
13979 if (VIsual_active)
13980 {
13981 if (VIsual_select)
13982 buf[0] = VIsual_mode + 's' - 'v';
13983 else
13984 buf[0] = VIsual_mode;
13985 }
13986 else
13987#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013988 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13989 || State == CONFIRM)
13990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013992 if (State == ASKMORE)
13993 buf[1] = 'm';
13994 else if (State == CONFIRM)
13995 buf[1] = '?';
13996 }
13997 else if (State == EXTERNCMD)
13998 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 else if (State & INSERT)
14000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014001#ifdef FEAT_VREPLACE
14002 if (State & VREPLACE_FLAG)
14003 {
14004 buf[0] = 'R';
14005 buf[1] = 'v';
14006 }
14007 else
14008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 if (State & REPLACE_FLAG)
14010 buf[0] = 'R';
14011 else
14012 buf[0] = 'i';
14013 }
14014 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014017 if (exmode_active)
14018 buf[1] = 'v';
14019 }
14020 else if (exmode_active)
14021 {
14022 buf[0] = 'c';
14023 buf[1] = 'e';
14024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014028 if (finish_op)
14029 buf[1] = 'o';
14030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014032 /* Clear out the minor mode when the argument is not a non-zero number or
14033 * non-empty string. */
14034 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014035 buf[1] = NUL;
14036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->vval.v_string = vim_strsave(buf);
14038 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039}
14040
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014041#ifdef FEAT_MZSCHEME
14042/*
14043 * "mzeval()" function
14044 */
14045 static void
14046f_mzeval(argvars, rettv)
14047 typval_T *argvars;
14048 typval_T *rettv;
14049{
14050 char_u *str;
14051 char_u buf[NUMBUFLEN];
14052
14053 str = get_tv_string_buf(&argvars[0], buf);
14054 do_mzeval(str, rettv);
14055}
14056#endif
14057
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059 * "nextnonblank()" function
14060 */
14061 static void
14062f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014063 typval_T *argvars;
14064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065{
14066 linenr_T lnum;
14067
14068 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 {
14072 lnum = 0;
14073 break;
14074 }
14075 if (*skipwhite(ml_get(lnum)) != NUL)
14076 break;
14077 }
14078 rettv->vval.v_number = lnum;
14079}
14080
14081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 * "nr2char()" function
14083 */
14084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088{
14089 char_u buf[NUMBUFLEN];
14090
14091#ifdef FEAT_MBYTE
14092 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014093 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 else
14095#endif
14096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 buf[1] = NUL;
14099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 rettv->v_type = VAR_STRING;
14101 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014102}
14103
14104/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014105 * "pathshorten()" function
14106 */
14107 static void
14108f_pathshorten(argvars, rettv)
14109 typval_T *argvars;
14110 typval_T *rettv;
14111{
14112 char_u *p;
14113
14114 rettv->v_type = VAR_STRING;
14115 p = get_tv_string_chk(&argvars[0]);
14116 if (p == NULL)
14117 rettv->vval.v_string = NULL;
14118 else
14119 {
14120 p = vim_strsave(p);
14121 rettv->vval.v_string = p;
14122 if (p != NULL)
14123 shorten_dir(p);
14124 }
14125}
14126
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014127#ifdef FEAT_FLOAT
14128/*
14129 * "pow()" function
14130 */
14131 static void
14132f_pow(argvars, rettv)
14133 typval_T *argvars;
14134 typval_T *rettv;
14135{
14136 float_T fx, fy;
14137
14138 rettv->v_type = VAR_FLOAT;
14139 if (get_float_arg(argvars, &fx) == OK
14140 && get_float_arg(&argvars[1], &fy) == OK)
14141 rettv->vval.v_float = pow(fx, fy);
14142 else
14143 rettv->vval.v_float = 0.0;
14144}
14145#endif
14146
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014147/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014148 * "prevnonblank()" function
14149 */
14150 static void
14151f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *argvars;
14153 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154{
14155 linenr_T lnum;
14156
14157 lnum = get_tv_lnum(argvars);
14158 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14159 lnum = 0;
14160 else
14161 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14162 --lnum;
14163 rettv->vval.v_number = lnum;
14164}
14165
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014166#ifdef HAVE_STDARG_H
14167/* This dummy va_list is here because:
14168 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14169 * - locally in the function results in a "used before set" warning
14170 * - using va_start() to initialize it gives "function with fixed args" error */
14171static va_list ap;
14172#endif
14173
Bram Moolenaar8c711452005-01-14 21:53:12 +000014174/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014175 * "printf()" function
14176 */
14177 static void
14178f_printf(argvars, rettv)
14179 typval_T *argvars;
14180 typval_T *rettv;
14181{
14182 rettv->v_type = VAR_STRING;
14183 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014184#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014185 {
14186 char_u buf[NUMBUFLEN];
14187 int len;
14188 char_u *s;
14189 int saved_did_emsg = did_emsg;
14190 char *fmt;
14191
14192 /* Get the required length, allocate the buffer and do it for real. */
14193 did_emsg = FALSE;
14194 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014195 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014196 if (!did_emsg)
14197 {
14198 s = alloc(len + 1);
14199 if (s != NULL)
14200 {
14201 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014202 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014203 }
14204 }
14205 did_emsg |= saved_did_emsg;
14206 }
14207#endif
14208}
14209
14210/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014211 * "pumvisible()" function
14212 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014213 static void
14214f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014215 typval_T *argvars UNUSED;
14216 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014217{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218#ifdef FEAT_INS_EXPAND
14219 if (pum_visible())
14220 rettv->vval.v_number = 1;
14221#endif
14222}
14223
14224/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225 * "range()" function
14226 */
14227 static void
14228f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014231{
14232 long start;
14233 long end;
14234 long stride = 1;
14235 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014239 if (argvars[1].v_type == VAR_UNKNOWN)
14240 {
14241 end = start - 1;
14242 start = 0;
14243 }
14244 else
14245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014247 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014249 }
14250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 if (error)
14252 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014253 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014254 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014255 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014256 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014257 else
14258 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014259 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014260 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014261 if (list_append_number(rettv->vval.v_list,
14262 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014263 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014264 }
14265}
14266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014267/*
14268 * "readfile()" function
14269 */
14270 static void
14271f_readfile(argvars, rettv)
14272 typval_T *argvars;
14273 typval_T *rettv;
14274{
14275 int binary = FALSE;
14276 char_u *fname;
14277 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014278 listitem_T *li;
14279#define FREAD_SIZE 200 /* optimized for text lines */
14280 char_u buf[FREAD_SIZE];
14281 int readlen; /* size of last fread() */
14282 int buflen; /* nr of valid chars in buf[] */
14283 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14284 int tolist; /* first byte in buf[] still to be put in list */
14285 int chop; /* how many CR to chop off */
14286 char_u *prev = NULL; /* previously read bytes, if any */
14287 int prevlen = 0; /* length of "prev" if not NULL */
14288 char_u *s;
14289 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014290 long maxline = MAXLNUM;
14291 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014292
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014293 if (argvars[1].v_type != VAR_UNKNOWN)
14294 {
14295 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14296 binary = TRUE;
14297 if (argvars[2].v_type != VAR_UNKNOWN)
14298 maxline = get_tv_number(&argvars[2]);
14299 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014301 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014302 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014303
14304 /* Always open the file in binary mode, library functions have a mind of
14305 * their own about CR-LF conversion. */
14306 fname = get_tv_string(&argvars[0]);
14307 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14308 {
14309 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14310 return;
14311 }
14312
14313 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014314 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014315 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014316 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014317 buflen = filtd + readlen;
14318 tolist = 0;
14319 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14320 {
14321 if (buf[filtd] == '\n' || readlen <= 0)
14322 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014323 /* In binary mode add an empty list item when the last
14324 * non-empty line ends in a '\n'. */
14325 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014326 break;
14327
14328 /* Found end-of-line or end-of-file: add a text line to the
14329 * list. */
14330 chop = 0;
14331 if (!binary)
14332 while (filtd - chop - 1 >= tolist
14333 && buf[filtd - chop - 1] == '\r')
14334 ++chop;
14335 len = filtd - tolist - chop;
14336 if (prev == NULL)
14337 s = vim_strnsave(buf + tolist, len);
14338 else
14339 {
14340 s = alloc((unsigned)(prevlen + len + 1));
14341 if (s != NULL)
14342 {
14343 mch_memmove(s, prev, prevlen);
14344 vim_free(prev);
14345 prev = NULL;
14346 mch_memmove(s + prevlen, buf + tolist, len);
14347 s[prevlen + len] = NUL;
14348 }
14349 }
14350 tolist = filtd + 1;
14351
14352 li = listitem_alloc();
14353 if (li == NULL)
14354 {
14355 vim_free(s);
14356 break;
14357 }
14358 li->li_tv.v_type = VAR_STRING;
14359 li->li_tv.v_lock = 0;
14360 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014361 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014362
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014363 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014364 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014365 if (readlen <= 0)
14366 break;
14367 }
14368 else if (buf[filtd] == NUL)
14369 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014370#ifdef FEAT_MBYTE
14371 else if (buf[filtd] == 0xef
14372 && enc_utf8
14373 && filtd + 2 < buflen
14374 && !binary
14375 && buf[filtd + 1] == 0xbb
14376 && buf[filtd + 2] == 0xbf)
14377 {
14378 /* remove utf-8 byte order mark */
14379 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14380 --filtd;
14381 buflen -= 3;
14382 }
14383#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014384 }
14385 if (readlen <= 0)
14386 break;
14387
14388 if (tolist == 0)
14389 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014390 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014392 /* "buf" is full, need to move text to an allocated buffer */
14393 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014394 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014395 prev = vim_strnsave(buf, buflen);
14396 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014398 else
14399 {
14400 s = alloc((unsigned)(prevlen + buflen));
14401 if (s != NULL)
14402 {
14403 mch_memmove(s, prev, prevlen);
14404 mch_memmove(s + prevlen, buf, buflen);
14405 vim_free(prev);
14406 prev = s;
14407 prevlen += buflen;
14408 }
14409 }
14410 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014411 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014412 }
14413 else
14414 {
14415 mch_memmove(buf, buf + tolist, buflen - tolist);
14416 filtd -= tolist;
14417 }
14418 }
14419
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014420 /*
14421 * For a negative line count use only the lines at the end of the file,
14422 * free the rest.
14423 */
14424 if (maxline < 0)
14425 while (cnt > -maxline)
14426 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014427 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014428 --cnt;
14429 }
14430
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014431 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014432 fclose(fd);
14433}
14434
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014435#if defined(FEAT_RELTIME)
14436static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14437
14438/*
14439 * Convert a List to proftime_T.
14440 * Return FAIL when there is something wrong.
14441 */
14442 static int
14443list2proftime(arg, tm)
14444 typval_T *arg;
14445 proftime_T *tm;
14446{
14447 long n1, n2;
14448 int error = FALSE;
14449
14450 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14451 || arg->vval.v_list->lv_len != 2)
14452 return FAIL;
14453 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14454 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14455# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014456 tm->HighPart = n1;
14457 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014458# else
14459 tm->tv_sec = n1;
14460 tm->tv_usec = n2;
14461# endif
14462 return error ? FAIL : OK;
14463}
14464#endif /* FEAT_RELTIME */
14465
14466/*
14467 * "reltime()" function
14468 */
14469 static void
14470f_reltime(argvars, rettv)
14471 typval_T *argvars;
14472 typval_T *rettv;
14473{
14474#ifdef FEAT_RELTIME
14475 proftime_T res;
14476 proftime_T start;
14477
14478 if (argvars[0].v_type == VAR_UNKNOWN)
14479 {
14480 /* No arguments: get current time. */
14481 profile_start(&res);
14482 }
14483 else if (argvars[1].v_type == VAR_UNKNOWN)
14484 {
14485 if (list2proftime(&argvars[0], &res) == FAIL)
14486 return;
14487 profile_end(&res);
14488 }
14489 else
14490 {
14491 /* Two arguments: compute the difference. */
14492 if (list2proftime(&argvars[0], &start) == FAIL
14493 || list2proftime(&argvars[1], &res) == FAIL)
14494 return;
14495 profile_sub(&res, &start);
14496 }
14497
14498 if (rettv_list_alloc(rettv) == OK)
14499 {
14500 long n1, n2;
14501
14502# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014503 n1 = res.HighPart;
14504 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014505# else
14506 n1 = res.tv_sec;
14507 n2 = res.tv_usec;
14508# endif
14509 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14510 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14511 }
14512#endif
14513}
14514
14515/*
14516 * "reltimestr()" function
14517 */
14518 static void
14519f_reltimestr(argvars, rettv)
14520 typval_T *argvars;
14521 typval_T *rettv;
14522{
14523#ifdef FEAT_RELTIME
14524 proftime_T tm;
14525#endif
14526
14527 rettv->v_type = VAR_STRING;
14528 rettv->vval.v_string = NULL;
14529#ifdef FEAT_RELTIME
14530 if (list2proftime(&argvars[0], &tm) == OK)
14531 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14532#endif
14533}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014534
Bram Moolenaar0d660222005-01-07 21:51:51 +000014535#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14536static void make_connection __ARGS((void));
14537static int check_connection __ARGS((void));
14538
14539 static void
14540make_connection()
14541{
14542 if (X_DISPLAY == NULL
14543# ifdef FEAT_GUI
14544 && !gui.in_use
14545# endif
14546 )
14547 {
14548 x_force_connect = TRUE;
14549 setup_term_clip();
14550 x_force_connect = FALSE;
14551 }
14552}
14553
14554 static int
14555check_connection()
14556{
14557 make_connection();
14558 if (X_DISPLAY == NULL)
14559 {
14560 EMSG(_("E240: No connection to Vim server"));
14561 return FAIL;
14562 }
14563 return OK;
14564}
14565#endif
14566
14567#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014568static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014569
14570 static void
14571remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *argvars;
14573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574 int expr;
14575{
14576 char_u *server_name;
14577 char_u *keys;
14578 char_u *r = NULL;
14579 char_u buf[NUMBUFLEN];
14580# ifdef WIN32
14581 HWND w;
14582# else
14583 Window w;
14584# endif
14585
14586 if (check_restricted() || check_secure())
14587 return;
14588
14589# ifdef FEAT_X11
14590 if (check_connection() == FAIL)
14591 return;
14592# endif
14593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 server_name = get_tv_string_chk(&argvars[0]);
14595 if (server_name == NULL)
14596 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597 keys = get_tv_string_buf(&argvars[1], buf);
14598# ifdef WIN32
14599 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14600# else
14601 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14602 < 0)
14603# endif
14604 {
14605 if (r != NULL)
14606 EMSG(r); /* sending worked but evaluation failed */
14607 else
14608 EMSG2(_("E241: Unable to send to %s"), server_name);
14609 return;
14610 }
14611
14612 rettv->vval.v_string = r;
14613
14614 if (argvars[2].v_type != VAR_UNKNOWN)
14615 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014617 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014620 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 v.di_tv.v_type = VAR_STRING;
14622 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 idvar = get_tv_string_chk(&argvars[2]);
14624 if (idvar != NULL)
14625 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014627 }
14628}
14629#endif
14630
14631/*
14632 * "remote_expr()" function
14633 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 static void
14635f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638{
14639 rettv->v_type = VAR_STRING;
14640 rettv->vval.v_string = NULL;
14641#ifdef FEAT_CLIENTSERVER
14642 remote_common(argvars, rettv, TRUE);
14643#endif
14644}
14645
14646/*
14647 * "remote_foreground()" function
14648 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 static void
14650f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014651 typval_T *argvars UNUSED;
14652 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014653{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654#ifdef FEAT_CLIENTSERVER
14655# ifdef WIN32
14656 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 {
14658 char_u *server_name = get_tv_string_chk(&argvars[0]);
14659
14660 if (server_name != NULL)
14661 serverForeground(server_name);
14662 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663# else
14664 /* Send a foreground() expression to the server. */
14665 argvars[1].v_type = VAR_STRING;
14666 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14667 argvars[2].v_type = VAR_UNKNOWN;
14668 remote_common(argvars, rettv, TRUE);
14669 vim_free(argvars[1].vval.v_string);
14670# endif
14671#endif
14672}
14673
Bram Moolenaar0d660222005-01-07 21:51:51 +000014674 static void
14675f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014676 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678{
14679#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 char_u *s = NULL;
14682# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014683 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014685 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686
14687 if (check_restricted() || check_secure())
14688 {
14689 rettv->vval.v_number = -1;
14690 return;
14691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 serverid = get_tv_string_chk(&argvars[0]);
14693 if (serverid == NULL)
14694 {
14695 rettv->vval.v_number = -1;
14696 return; /* type error; errmsg already given */
14697 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014699 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700 if (n == 0)
14701 rettv->vval.v_number = -1;
14702 else
14703 {
14704 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14705 rettv->vval.v_number = (s != NULL);
14706 }
14707# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708 if (check_connection() == FAIL)
14709 return;
14710
14711 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713# endif
14714
14715 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014717 char_u *retvar;
14718
Bram Moolenaar33570922005-01-25 22:26:29 +000014719 v.di_tv.v_type = VAR_STRING;
14720 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 retvar = get_tv_string_chk(&argvars[1]);
14722 if (retvar != NULL)
14723 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014724 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014725 }
14726#else
14727 rettv->vval.v_number = -1;
14728#endif
14729}
14730
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 static void
14732f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735{
14736 char_u *r = NULL;
14737
14738#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014739 char_u *serverid = get_tv_string_chk(&argvars[0]);
14740
14741 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742 {
14743# ifdef WIN32
14744 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014745 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014746
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014747 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748 if (n != 0)
14749 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14750 if (r == NULL)
14751# else
14752 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014753 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754# endif
14755 EMSG(_("E277: Unable to read a server reply"));
14756 }
14757#endif
14758 rettv->v_type = VAR_STRING;
14759 rettv->vval.v_string = r;
14760}
14761
14762/*
14763 * "remote_send()" function
14764 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765 static void
14766f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014767 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014768 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014769{
14770 rettv->v_type = VAR_STRING;
14771 rettv->vval.v_string = NULL;
14772#ifdef FEAT_CLIENTSERVER
14773 remote_common(argvars, rettv, FALSE);
14774#endif
14775}
14776
14777/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014778 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014779 */
14780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014781f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 typval_T *argvars;
14783 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784{
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 list_T *l;
14786 listitem_T *item, *item2;
14787 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014788 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014789 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014790 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 dict_T *d;
14792 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014793 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014794
Bram Moolenaar8c711452005-01-14 21:53:12 +000014795 if (argvars[0].v_type == VAR_DICT)
14796 {
14797 if (argvars[2].v_type != VAR_UNKNOWN)
14798 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014799 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014800 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 key = get_tv_string_chk(&argvars[1]);
14803 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 di = dict_find(d, key, -1);
14806 if (di == NULL)
14807 EMSG2(_(e_dictkey), key);
14808 else
14809 {
14810 *rettv = di->di_tv;
14811 init_tv(&di->di_tv);
14812 dictitem_remove(d, di);
14813 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014815 }
14816 }
14817 else if (argvars[0].v_type != VAR_LIST)
14818 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014819 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014820 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 int error = FALSE;
14823
14824 idx = get_tv_number_chk(&argvars[1], &error);
14825 if (error)
14826 ; /* type error: do nothing, errmsg already given */
14827 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014828 EMSGN(_(e_listidx), idx);
14829 else
14830 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014831 if (argvars[2].v_type == VAR_UNKNOWN)
14832 {
14833 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014834 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014835 *rettv = item->li_tv;
14836 vim_free(item);
14837 }
14838 else
14839 {
14840 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 end = get_tv_number_chk(&argvars[2], &error);
14842 if (error)
14843 ; /* type error: do nothing */
14844 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014845 EMSGN(_(e_listidx), end);
14846 else
14847 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014848 int cnt = 0;
14849
14850 for (li = item; li != NULL; li = li->li_next)
14851 {
14852 ++cnt;
14853 if (li == item2)
14854 break;
14855 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014856 if (li == NULL) /* didn't find "item2" after "item" */
14857 EMSG(_(e_invrange));
14858 else
14859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014860 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014861 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014862 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014863 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014864 l->lv_first = item;
14865 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014866 item->li_prev = NULL;
14867 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014868 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 }
14870 }
14871 }
14872 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 }
14874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875}
14876
14877/*
14878 * "rename({from}, {to})" function
14879 */
14880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 typval_T *argvars;
14883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884{
14885 char_u buf[NUMBUFLEN];
14886
14887 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14891 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892}
14893
14894/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014895 * "repeat()" function
14896 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014897 static void
14898f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014899 typval_T *argvars;
14900 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014901{
14902 char_u *p;
14903 int n;
14904 int slen;
14905 int len;
14906 char_u *r;
14907 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014908
14909 n = get_tv_number(&argvars[1]);
14910 if (argvars[0].v_type == VAR_LIST)
14911 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014912 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014913 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014914 if (list_extend(rettv->vval.v_list,
14915 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014916 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014917 }
14918 else
14919 {
14920 p = get_tv_string(&argvars[0]);
14921 rettv->v_type = VAR_STRING;
14922 rettv->vval.v_string = NULL;
14923
14924 slen = (int)STRLEN(p);
14925 len = slen * n;
14926 if (len <= 0)
14927 return;
14928
14929 r = alloc(len + 1);
14930 if (r != NULL)
14931 {
14932 for (i = 0; i < n; i++)
14933 mch_memmove(r + i * slen, p, (size_t)slen);
14934 r[len] = NUL;
14935 }
14936
14937 rettv->vval.v_string = r;
14938 }
14939}
14940
14941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 * "resolve()" function
14943 */
14944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014945f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014946 typval_T *argvars;
14947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948{
14949 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020014950#ifdef HAVE_READLINK
14951 char_u *buf = NULL;
14952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014954 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955#ifdef FEAT_SHORTCUT
14956 {
14957 char_u *v = NULL;
14958
14959 v = mch_resolve_shortcut(p);
14960 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 }
14965#else
14966# ifdef HAVE_READLINK
14967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 char_u *cpy;
14969 int len;
14970 char_u *remain = NULL;
14971 char_u *q;
14972 int is_relative_to_current = FALSE;
14973 int has_trailing_pathsep = FALSE;
14974 int limit = 100;
14975
14976 p = vim_strsave(p);
14977
14978 if (p[0] == '.' && (vim_ispathsep(p[1])
14979 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14980 is_relative_to_current = TRUE;
14981
14982 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014983 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014986 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
14987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988
14989 q = getnextcomp(p);
14990 if (*q != NUL)
14991 {
14992 /* Separate the first path component in "p", and keep the
14993 * remainder (beginning with the path separator). */
14994 remain = vim_strsave(q - 1);
14995 q[-1] = NUL;
14996 }
14997
Bram Moolenaard9462e32011-04-11 21:35:11 +020014998 buf = alloc(MAXPATHL + 1);
14999 if (buf == NULL)
15000 goto fail;
15001
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 for (;;)
15003 {
15004 for (;;)
15005 {
15006 len = readlink((char *)p, (char *)buf, MAXPATHL);
15007 if (len <= 0)
15008 break;
15009 buf[len] = NUL;
15010
15011 if (limit-- == 0)
15012 {
15013 vim_free(p);
15014 vim_free(remain);
15015 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015016 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 goto fail;
15018 }
15019
15020 /* Ensure that the result will have a trailing path separator
15021 * if the argument has one. */
15022 if (remain == NULL && has_trailing_pathsep)
15023 add_pathsep(buf);
15024
15025 /* Separate the first path component in the link value and
15026 * concatenate the remainders. */
15027 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15028 if (*q != NUL)
15029 {
15030 if (remain == NULL)
15031 remain = vim_strsave(q - 1);
15032 else
15033 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015034 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 if (cpy != NULL)
15036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 vim_free(remain);
15038 remain = cpy;
15039 }
15040 }
15041 q[-1] = NUL;
15042 }
15043
15044 q = gettail(p);
15045 if (q > p && *q == NUL)
15046 {
15047 /* Ignore trailing path separator. */
15048 q[-1] = NUL;
15049 q = gettail(p);
15050 }
15051 if (q > p && !mch_isFullName(buf))
15052 {
15053 /* symlink is relative to directory of argument */
15054 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15055 if (cpy != NULL)
15056 {
15057 STRCPY(cpy, p);
15058 STRCPY(gettail(cpy), buf);
15059 vim_free(p);
15060 p = cpy;
15061 }
15062 }
15063 else
15064 {
15065 vim_free(p);
15066 p = vim_strsave(buf);
15067 }
15068 }
15069
15070 if (remain == NULL)
15071 break;
15072
15073 /* Append the first path component of "remain" to "p". */
15074 q = getnextcomp(remain + 1);
15075 len = q - remain - (*q != NUL);
15076 cpy = vim_strnsave(p, STRLEN(p) + len);
15077 if (cpy != NULL)
15078 {
15079 STRNCAT(cpy, remain, len);
15080 vim_free(p);
15081 p = cpy;
15082 }
15083 /* Shorten "remain". */
15084 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015085 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 else
15087 {
15088 vim_free(remain);
15089 remain = NULL;
15090 }
15091 }
15092
15093 /* If the result is a relative path name, make it explicitly relative to
15094 * the current directory if and only if the argument had this form. */
15095 if (!vim_ispathsep(*p))
15096 {
15097 if (is_relative_to_current
15098 && *p != NUL
15099 && !(p[0] == '.'
15100 && (p[1] == NUL
15101 || vim_ispathsep(p[1])
15102 || (p[1] == '.'
15103 && (p[2] == NUL
15104 || vim_ispathsep(p[2]))))))
15105 {
15106 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015107 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 if (cpy != NULL)
15109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 vim_free(p);
15111 p = cpy;
15112 }
15113 }
15114 else if (!is_relative_to_current)
15115 {
15116 /* Strip leading "./". */
15117 q = p;
15118 while (q[0] == '.' && vim_ispathsep(q[1]))
15119 q += 2;
15120 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015121 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 }
15123 }
15124
15125 /* Ensure that the result will have no trailing path separator
15126 * if the argument had none. But keep "/" or "//". */
15127 if (!has_trailing_pathsep)
15128 {
15129 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015130 if (after_pathsep(p, q))
15131 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 }
15133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015134 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 }
15136# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138# endif
15139#endif
15140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
15143#ifdef HAVE_READLINK
15144fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015145 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148}
15149
15150/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 */
15153 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 typval_T *argvars;
15156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157{
Bram Moolenaar33570922005-01-25 22:26:29 +000015158 list_T *l;
15159 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161 if (argvars[0].v_type != VAR_LIST)
15162 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015163 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015164 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 {
15166 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015167 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015168 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 while (li != NULL)
15170 {
15171 ni = li->li_prev;
15172 list_append(l, li);
15173 li = ni;
15174 }
15175 rettv->vval.v_list = l;
15176 rettv->v_type = VAR_LIST;
15177 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015178 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180}
15181
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015182#define SP_NOMOVE 0x01 /* don't move cursor */
15183#define SP_REPEAT 0x02 /* repeat to find outer pair */
15184#define SP_RETCOUNT 0x04 /* return matchcount */
15185#define SP_SETPCMARK 0x08 /* set previous context mark */
15186#define SP_START 0x10 /* accept match at start position */
15187#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15188#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015189
Bram Moolenaar33570922005-01-25 22:26:29 +000015190static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191
15192/*
15193 * Get flags for a search function.
15194 * Possibly sets "p_ws".
15195 * Returns BACKWARD, FORWARD or zero (for an error).
15196 */
15197 static int
15198get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 int *flagsp;
15201{
15202 int dir = FORWARD;
15203 char_u *flags;
15204 char_u nbuf[NUMBUFLEN];
15205 int mask;
15206
15207 if (varp->v_type != VAR_UNKNOWN)
15208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015209 flags = get_tv_string_buf_chk(varp, nbuf);
15210 if (flags == NULL)
15211 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212 while (*flags != NUL)
15213 {
15214 switch (*flags)
15215 {
15216 case 'b': dir = BACKWARD; break;
15217 case 'w': p_ws = TRUE; break;
15218 case 'W': p_ws = FALSE; break;
15219 default: mask = 0;
15220 if (flagsp != NULL)
15221 switch (*flags)
15222 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015223 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015224 case 'e': mask = SP_END; break;
15225 case 'm': mask = SP_RETCOUNT; break;
15226 case 'n': mask = SP_NOMOVE; break;
15227 case 'p': mask = SP_SUBPAT; break;
15228 case 'r': mask = SP_REPEAT; break;
15229 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230 }
15231 if (mask == 0)
15232 {
15233 EMSG2(_(e_invarg2), flags);
15234 dir = 0;
15235 }
15236 else
15237 *flagsp |= mask;
15238 }
15239 if (dir == 0)
15240 break;
15241 ++flags;
15242 }
15243 }
15244 return dir;
15245}
15246
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015248 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015250 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015251search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015253 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015254 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015256 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 char_u *pat;
15258 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015259 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 int save_p_ws = p_ws;
15261 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015262 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015263 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015264 proftime_T tm;
15265#ifdef FEAT_RELTIME
15266 long time_limit = 0;
15267#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015268 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015269 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015272 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015273 if (dir == 0)
15274 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015275 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015276 if (flags & SP_START)
15277 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015278 if (flags & SP_END)
15279 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015280
Bram Moolenaar76929292008-01-06 19:07:36 +000015281 /* Optional arguments: line number to stop searching and timeout. */
15282 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015283 {
15284 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15285 if (lnum_stop < 0)
15286 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015287#ifdef FEAT_RELTIME
15288 if (argvars[3].v_type != VAR_UNKNOWN)
15289 {
15290 time_limit = get_tv_number_chk(&argvars[3], NULL);
15291 if (time_limit < 0)
15292 goto theend;
15293 }
15294#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015295 }
15296
Bram Moolenaar76929292008-01-06 19:07:36 +000015297#ifdef FEAT_RELTIME
15298 /* Set the time limit, if there is one. */
15299 profile_setlimit(time_limit, &tm);
15300#endif
15301
Bram Moolenaar231334e2005-07-25 20:46:57 +000015302 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015303 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015304 * Check to make sure only those flags are set.
15305 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15306 * flags cannot be set. Check for that condition also.
15307 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015308 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015309 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015312 goto theend;
15313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015315 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015316 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015317 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015318 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015320 if (flags & SP_SUBPAT)
15321 retval = subpatnum;
15322 else
15323 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015324 if (flags & SP_SETPCMARK)
15325 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015327 if (match_pos != NULL)
15328 {
15329 /* Store the match cursor position */
15330 match_pos->lnum = pos.lnum;
15331 match_pos->col = pos.col + 1;
15332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 /* "/$" will put the cursor after the end of the line, may need to
15334 * correct that here */
15335 check_cursor();
15336 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015337
15338 /* If 'n' flag is used: restore cursor position. */
15339 if (flags & SP_NOMOVE)
15340 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015341 else
15342 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015343theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015345
15346 return retval;
15347}
15348
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015349#ifdef FEAT_FLOAT
15350/*
15351 * "round({float})" function
15352 */
15353 static void
15354f_round(argvars, rettv)
15355 typval_T *argvars;
15356 typval_T *rettv;
15357{
15358 float_T f;
15359
15360 rettv->v_type = VAR_FLOAT;
15361 if (get_float_arg(argvars, &f) == OK)
15362 /* round() is not in C90, use ceil() or floor() instead. */
15363 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15364 else
15365 rettv->vval.v_float = 0.0;
15366}
15367#endif
15368
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015369/*
15370 * "search()" function
15371 */
15372 static void
15373f_search(argvars, rettv)
15374 typval_T *argvars;
15375 typval_T *rettv;
15376{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015377 int flags = 0;
15378
15379 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380}
15381
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015383 * "searchdecl()" function
15384 */
15385 static void
15386f_searchdecl(argvars, rettv)
15387 typval_T *argvars;
15388 typval_T *rettv;
15389{
15390 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015391 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015392 int error = FALSE;
15393 char_u *name;
15394
15395 rettv->vval.v_number = 1; /* default: FAIL */
15396
15397 name = get_tv_string_chk(&argvars[0]);
15398 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015399 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015400 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015401 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15402 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15403 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015404 if (!error && name != NULL)
15405 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015406 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015407}
15408
15409/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015410 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015412 static int
15413searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015414 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015415 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416{
15417 char_u *spat, *mpat, *epat;
15418 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 int dir;
15421 int flags = 0;
15422 char_u nbuf1[NUMBUFLEN];
15423 char_u nbuf2[NUMBUFLEN];
15424 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015425 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015426 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015427 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015430 spat = get_tv_string_chk(&argvars[0]);
15431 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15432 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15433 if (spat == NULL || mpat == NULL || epat == NULL)
15434 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 /* Handle the optional fourth argument: flags */
15437 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015438 if (dir == 0)
15439 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015440
15441 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015442 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15443 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015444 if ((flags & (SP_END | SP_SUBPAT)) != 0
15445 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015446 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015447 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015448 goto theend;
15449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015451 /* Using 'r' implies 'W', otherwise it doesn't work. */
15452 if (flags & SP_REPEAT)
15453 p_ws = FALSE;
15454
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015455 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015456 if (argvars[3].v_type == VAR_UNKNOWN
15457 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 skip = (char_u *)"";
15459 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015461 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015462 if (argvars[5].v_type != VAR_UNKNOWN)
15463 {
15464 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15465 if (lnum_stop < 0)
15466 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015467#ifdef FEAT_RELTIME
15468 if (argvars[6].v_type != VAR_UNKNOWN)
15469 {
15470 time_limit = get_tv_number_chk(&argvars[6], NULL);
15471 if (time_limit < 0)
15472 goto theend;
15473 }
15474#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015475 }
15476 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015477 if (skip == NULL)
15478 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015480 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015481 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015482
15483theend:
15484 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015485
15486 return retval;
15487}
15488
15489/*
15490 * "searchpair()" function
15491 */
15492 static void
15493f_searchpair(argvars, rettv)
15494 typval_T *argvars;
15495 typval_T *rettv;
15496{
15497 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15498}
15499
15500/*
15501 * "searchpairpos()" function
15502 */
15503 static void
15504f_searchpairpos(argvars, rettv)
15505 typval_T *argvars;
15506 typval_T *rettv;
15507{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015508 pos_T match_pos;
15509 int lnum = 0;
15510 int col = 0;
15511
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015512 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015513 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015514
15515 if (searchpair_cmn(argvars, &match_pos) > 0)
15516 {
15517 lnum = match_pos.lnum;
15518 col = match_pos.col;
15519 }
15520
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015521 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15522 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015523}
15524
15525/*
15526 * Search for a start/middle/end thing.
15527 * Used by searchpair(), see its documentation for the details.
15528 * Returns 0 or -1 for no match,
15529 */
15530 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015531do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15532 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015533 char_u *spat; /* start pattern */
15534 char_u *mpat; /* middle pattern */
15535 char_u *epat; /* end pattern */
15536 int dir; /* BACKWARD or FORWARD */
15537 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015538 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015539 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015540 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015541 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015542{
15543 char_u *save_cpo;
15544 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15545 long retval = 0;
15546 pos_T pos;
15547 pos_T firstpos;
15548 pos_T foundpos;
15549 pos_T save_cursor;
15550 pos_T save_pos;
15551 int n;
15552 int r;
15553 int nest = 1;
15554 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015555 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015556 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015557
15558 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15559 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015560 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015561
Bram Moolenaar76929292008-01-06 19:07:36 +000015562#ifdef FEAT_RELTIME
15563 /* Set the time limit, if there is one. */
15564 profile_setlimit(time_limit, &tm);
15565#endif
15566
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015567 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15568 * start/middle/end (pat3, for the top pair). */
15569 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15570 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15571 if (pat2 == NULL || pat3 == NULL)
15572 goto theend;
15573 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15574 if (*mpat == NUL)
15575 STRCPY(pat3, pat2);
15576 else
15577 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15578 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015579 if (flags & SP_START)
15580 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015581
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 save_cursor = curwin->w_cursor;
15583 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015584 clearpos(&firstpos);
15585 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 pat = pat3;
15587 for (;;)
15588 {
15589 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015590 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15592 /* didn't find it or found the first match again: FAIL */
15593 break;
15594
15595 if (firstpos.lnum == 0)
15596 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015597 if (equalpos(pos, foundpos))
15598 {
15599 /* Found the same position again. Can happen with a pattern that
15600 * has "\zs" at the end and searching backwards. Advance one
15601 * character and try again. */
15602 if (dir == BACKWARD)
15603 decl(&pos);
15604 else
15605 incl(&pos);
15606 }
15607 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015609 /* clear the start flag to avoid getting stuck here */
15610 options &= ~SEARCH_START;
15611
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 /* If the skip pattern matches, ignore this match. */
15613 if (*skip != NUL)
15614 {
15615 save_pos = curwin->w_cursor;
15616 curwin->w_cursor = pos;
15617 r = eval_to_bool(skip, &err, NULL, FALSE);
15618 curwin->w_cursor = save_pos;
15619 if (err)
15620 {
15621 /* Evaluating {skip} caused an error, break here. */
15622 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015623 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 break;
15625 }
15626 if (r)
15627 continue;
15628 }
15629
15630 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15631 {
15632 /* Found end when searching backwards or start when searching
15633 * forward: nested pair. */
15634 ++nest;
15635 pat = pat2; /* nested, don't search for middle */
15636 }
15637 else
15638 {
15639 /* Found end when searching forward or start when searching
15640 * backward: end of (nested) pair; or found middle in outer pair. */
15641 if (--nest == 1)
15642 pat = pat3; /* outer level, search for middle */
15643 }
15644
15645 if (nest == 0)
15646 {
15647 /* Found the match: return matchcount or line number. */
15648 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015649 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015651 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015652 if (flags & SP_SETPCMARK)
15653 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 curwin->w_cursor = pos;
15655 if (!(flags & SP_REPEAT))
15656 break;
15657 nest = 1; /* search for next unmatched */
15658 }
15659 }
15660
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015661 if (match_pos != NULL)
15662 {
15663 /* Store the match cursor position */
15664 match_pos->lnum = curwin->w_cursor.lnum;
15665 match_pos->col = curwin->w_cursor.col + 1;
15666 }
15667
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015669 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 curwin->w_cursor = save_cursor;
15671
15672theend:
15673 vim_free(pat2);
15674 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015675 if (p_cpo == empty_option)
15676 p_cpo = save_cpo;
15677 else
15678 /* Darn, evaluating the {skip} expression changed the value. */
15679 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015680
15681 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682}
15683
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015684/*
15685 * "searchpos()" function
15686 */
15687 static void
15688f_searchpos(argvars, rettv)
15689 typval_T *argvars;
15690 typval_T *rettv;
15691{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015692 pos_T match_pos;
15693 int lnum = 0;
15694 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015695 int n;
15696 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015697
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015698 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015700
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015701 n = search_cmn(argvars, &match_pos, &flags);
15702 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015703 {
15704 lnum = match_pos.lnum;
15705 col = match_pos.col;
15706 }
15707
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15709 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015710 if (flags & SP_SUBPAT)
15711 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015712}
15713
15714
Bram Moolenaar0d660222005-01-07 21:51:51 +000015715 static void
15716f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015717 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720#ifdef FEAT_CLIENTSERVER
15721 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015722 char_u *server = get_tv_string_chk(&argvars[0]);
15723 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 if (server == NULL || reply == NULL)
15727 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728 if (check_restricted() || check_secure())
15729 return;
15730# ifdef FEAT_X11
15731 if (check_connection() == FAIL)
15732 return;
15733# endif
15734
15735 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737 EMSG(_("E258: Unable to send to client"));
15738 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740 rettv->vval.v_number = 0;
15741#else
15742 rettv->vval.v_number = -1;
15743#endif
15744}
15745
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 static void
15747f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015750{
15751 char_u *r = NULL;
15752
15753#ifdef FEAT_CLIENTSERVER
15754# ifdef WIN32
15755 r = serverGetVimNames();
15756# else
15757 make_connection();
15758 if (X_DISPLAY != NULL)
15759 r = serverGetVimNames(X_DISPLAY);
15760# endif
15761#endif
15762 rettv->v_type = VAR_STRING;
15763 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764}
15765
15766/*
15767 * "setbufvar()" function
15768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015770f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015772 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773{
15774 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 char_u nbuf[NUMBUFLEN];
15779
15780 if (check_restricted() || check_secure())
15781 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15783 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 varp = &argvars[2];
15786
15787 if (buf != NULL && varname != NULL && varp != NULL)
15788 {
15789 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791
15792 if (*varname == '&')
15793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015794 long numval;
15795 char_u *strval;
15796 int error = FALSE;
15797
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015799 numval = get_tv_number_chk(varp, &error);
15800 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 if (!error && strval != NULL)
15802 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 }
15804 else
15805 {
15806 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15807 if (bufvarname != NULL)
15808 {
15809 STRCPY(bufvarname, "b:");
15810 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015811 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 vim_free(bufvarname);
15813 }
15814 }
15815
15816 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819}
15820
15821/*
15822 * "setcmdpos()" function
15823 */
15824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015825f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015826 typval_T *argvars;
15827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015829 int pos = (int)get_tv_number(&argvars[0]) - 1;
15830
15831 if (pos >= 0)
15832 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833}
15834
15835/*
15836 * "setline()" function
15837 */
15838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015839f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015840 typval_T *argvars;
15841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842{
15843 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015844 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015845 list_T *l = NULL;
15846 listitem_T *li = NULL;
15847 long added = 0;
15848 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015850 lnum = get_tv_lnum(&argvars[0]);
15851 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015853 l = argvars[1].vval.v_list;
15854 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015856 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015857 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015858
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015859 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015860 for (;;)
15861 {
15862 if (l != NULL)
15863 {
15864 /* list argument, get next string */
15865 if (li == NULL)
15866 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015867 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015868 li = li->li_next;
15869 }
15870
15871 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015873 break;
15874 if (lnum <= curbuf->b_ml.ml_line_count)
15875 {
15876 /* existing line, replace it */
15877 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15878 {
15879 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015880 if (lnum == curwin->w_cursor.lnum)
15881 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015882 rettv->vval.v_number = 0; /* OK */
15883 }
15884 }
15885 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15886 {
15887 /* lnum is one past the last line, append the line */
15888 ++added;
15889 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15890 rettv->vval.v_number = 0; /* OK */
15891 }
15892
15893 if (l == NULL) /* only one string argument */
15894 break;
15895 ++lnum;
15896 }
15897
15898 if (added > 0)
15899 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900}
15901
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015902static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15903
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015905 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015906 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015907 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015908set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015909 win_T *wp UNUSED;
15910 typval_T *list_arg UNUSED;
15911 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015912 typval_T *rettv;
15913{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015914#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015915 char_u *act;
15916 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015917#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015918
Bram Moolenaar2641f772005-03-25 21:58:17 +000015919 rettv->vval.v_number = -1;
15920
15921#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015922 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015923 EMSG(_(e_listreq));
15924 else
15925 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015926 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015927
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015928 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015929 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015930 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015931 if (act == NULL)
15932 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015933 if (*act == 'a' || *act == 'r')
15934 action = *act;
15935 }
15936
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015937 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015938 rettv->vval.v_number = 0;
15939 }
15940#endif
15941}
15942
15943/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015944 * "setloclist()" function
15945 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015946 static void
15947f_setloclist(argvars, rettv)
15948 typval_T *argvars;
15949 typval_T *rettv;
15950{
15951 win_T *win;
15952
15953 rettv->vval.v_number = -1;
15954
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015955 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015956 if (win != NULL)
15957 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15958}
15959
15960/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015961 * "setmatches()" function
15962 */
15963 static void
15964f_setmatches(argvars, rettv)
15965 typval_T *argvars;
15966 typval_T *rettv;
15967{
15968#ifdef FEAT_SEARCH_EXTRA
15969 list_T *l;
15970 listitem_T *li;
15971 dict_T *d;
15972
15973 rettv->vval.v_number = -1;
15974 if (argvars[0].v_type != VAR_LIST)
15975 {
15976 EMSG(_(e_listreq));
15977 return;
15978 }
15979 if ((l = argvars[0].vval.v_list) != NULL)
15980 {
15981
15982 /* To some extent make sure that we are dealing with a list from
15983 * "getmatches()". */
15984 li = l->lv_first;
15985 while (li != NULL)
15986 {
15987 if (li->li_tv.v_type != VAR_DICT
15988 || (d = li->li_tv.vval.v_dict) == NULL)
15989 {
15990 EMSG(_(e_invarg));
15991 return;
15992 }
15993 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15994 && dict_find(d, (char_u *)"pattern", -1) != NULL
15995 && dict_find(d, (char_u *)"priority", -1) != NULL
15996 && dict_find(d, (char_u *)"id", -1) != NULL))
15997 {
15998 EMSG(_(e_invarg));
15999 return;
16000 }
16001 li = li->li_next;
16002 }
16003
16004 clear_matches(curwin);
16005 li = l->lv_first;
16006 while (li != NULL)
16007 {
16008 d = li->li_tv.vval.v_dict;
16009 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16010 get_dict_string(d, (char_u *)"pattern", FALSE),
16011 (int)get_dict_number(d, (char_u *)"priority"),
16012 (int)get_dict_number(d, (char_u *)"id"));
16013 li = li->li_next;
16014 }
16015 rettv->vval.v_number = 0;
16016 }
16017#endif
16018}
16019
16020/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016021 * "setpos()" function
16022 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016023 static void
16024f_setpos(argvars, rettv)
16025 typval_T *argvars;
16026 typval_T *rettv;
16027{
16028 pos_T pos;
16029 int fnum;
16030 char_u *name;
16031
Bram Moolenaar08250432008-02-13 11:42:46 +000016032 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016033 name = get_tv_string_chk(argvars);
16034 if (name != NULL)
16035 {
16036 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16037 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016038 if (--pos.col < 0)
16039 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016040 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016041 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016042 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016043 if (fnum == curbuf->b_fnum)
16044 {
16045 curwin->w_cursor = pos;
16046 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016047 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016048 }
16049 else
16050 EMSG(_(e_invarg));
16051 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016052 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16053 {
16054 /* set mark */
16055 if (setmark_pos(name[1], &pos, fnum) == OK)
16056 rettv->vval.v_number = 0;
16057 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016058 else
16059 EMSG(_(e_invarg));
16060 }
16061 }
16062}
16063
16064/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016065 * "setqflist()" function
16066 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016067 static void
16068f_setqflist(argvars, rettv)
16069 typval_T *argvars;
16070 typval_T *rettv;
16071{
16072 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16073}
16074
16075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 * "setreg()" function
16077 */
16078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 typval_T *argvars;
16081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082{
16083 int regname;
16084 char_u *strregname;
16085 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 int append;
16088 char_u yank_type;
16089 long block_len;
16090
16091 block_len = -1;
16092 yank_type = MAUTO;
16093 append = FALSE;
16094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016095 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 if (strregname == NULL)
16099 return; /* type error; errmsg already given */
16100 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 if (regname == 0 || regname == '@')
16102 regname = '"';
16103 else if (regname == '=')
16104 return;
16105
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016106 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016108 stropt = get_tv_string_chk(&argvars[2]);
16109 if (stropt == NULL)
16110 return; /* type error */
16111 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 switch (*stropt)
16113 {
16114 case 'a': case 'A': /* append */
16115 append = TRUE;
16116 break;
16117 case 'v': case 'c': /* character-wise selection */
16118 yank_type = MCHAR;
16119 break;
16120 case 'V': case 'l': /* line-wise selection */
16121 yank_type = MLINE;
16122 break;
16123#ifdef FEAT_VISUAL
16124 case 'b': case Ctrl_V: /* block-wise selection */
16125 yank_type = MBLOCK;
16126 if (VIM_ISDIGIT(stropt[1]))
16127 {
16128 ++stropt;
16129 block_len = getdigits(&stropt) - 1;
16130 --stropt;
16131 }
16132 break;
16133#endif
16134 }
16135 }
16136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016137 strval = get_tv_string_chk(&argvars[1]);
16138 if (strval != NULL)
16139 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142}
16143
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016144/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016145 * "settabvar()" function
16146 */
16147 static void
16148f_settabvar(argvars, rettv)
16149 typval_T *argvars;
16150 typval_T *rettv;
16151{
16152 tabpage_T *save_curtab;
16153 char_u *varname, *tabvarname;
16154 typval_T *varp;
16155 tabpage_T *tp;
16156
16157 rettv->vval.v_number = 0;
16158
16159 if (check_restricted() || check_secure())
16160 return;
16161
16162 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16163 varname = get_tv_string_chk(&argvars[1]);
16164 varp = &argvars[2];
16165
16166 if (tp != NULL && varname != NULL && varp != NULL)
16167 {
16168 save_curtab = curtab;
16169 goto_tabpage_tp(tp);
16170
16171 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16172 if (tabvarname != NULL)
16173 {
16174 STRCPY(tabvarname, "t:");
16175 STRCPY(tabvarname + 2, varname);
16176 set_var(tabvarname, varp, TRUE);
16177 vim_free(tabvarname);
16178 }
16179
16180 /* Restore current tabpage */
16181 if (valid_tabpage(save_curtab))
16182 goto_tabpage_tp(save_curtab);
16183 }
16184}
16185
16186/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016187 * "settabwinvar()" function
16188 */
16189 static void
16190f_settabwinvar(argvars, rettv)
16191 typval_T *argvars;
16192 typval_T *rettv;
16193{
16194 setwinvar(argvars, rettv, 1);
16195}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196
16197/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016198 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016202 typval_T *argvars;
16203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016205 setwinvar(argvars, rettv, 0);
16206}
16207
16208/*
16209 * "setwinvar()" and "settabwinvar()" functions
16210 */
16211 static void
16212setwinvar(argvars, rettv, off)
16213 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016214 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016215 int off;
16216{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 win_T *win;
16218#ifdef FEAT_WINDOWS
16219 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016220 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221#endif
16222 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016225 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226
16227 if (check_restricted() || check_secure())
16228 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016229
16230#ifdef FEAT_WINDOWS
16231 if (off == 1)
16232 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16233 else
16234 tp = curtab;
16235#endif
16236 win = find_win_by_nr(&argvars[off], tp);
16237 varname = get_tv_string_chk(&argvars[off + 1]);
16238 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239
16240 if (win != NULL && varname != NULL && varp != NULL)
16241 {
16242#ifdef FEAT_WINDOWS
16243 /* set curwin to be our win, temporarily */
16244 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016245 save_curtab = curtab;
16246 goto_tabpage_tp(tp);
16247 if (!win_valid(win))
16248 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 curwin = win;
16250 curbuf = curwin->w_buffer;
16251#endif
16252
16253 if (*varname == '&')
16254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 long numval;
16256 char_u *strval;
16257 int error = FALSE;
16258
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016260 numval = get_tv_number_chk(varp, &error);
16261 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 if (!error && strval != NULL)
16263 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 }
16265 else
16266 {
16267 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16268 if (winvarname != NULL)
16269 {
16270 STRCPY(winvarname, "w:");
16271 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016272 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 vim_free(winvarname);
16274 }
16275 }
16276
16277#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016278 /* Restore current tabpage and window, if still valid (autocomands can
16279 * make them invalid). */
16280 if (valid_tabpage(save_curtab))
16281 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 if (win_valid(save_curwin))
16283 {
16284 curwin = save_curwin;
16285 curbuf = curwin->w_buffer;
16286 }
16287#endif
16288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289}
16290
16291/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016292 * "shellescape({string})" function
16293 */
16294 static void
16295f_shellescape(argvars, rettv)
16296 typval_T *argvars;
16297 typval_T *rettv;
16298{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016299 rettv->vval.v_string = vim_strsave_shellescape(
16300 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016301 rettv->v_type = VAR_STRING;
16302}
16303
16304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016305 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 */
16307 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 typval_T *argvars;
16310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 p = get_tv_string(&argvars[0]);
16315 rettv->vval.v_string = vim_strsave(p);
16316 simplify_filename(rettv->vval.v_string); /* simplify in place */
16317 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318}
16319
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016320#ifdef FEAT_FLOAT
16321/*
16322 * "sin()" function
16323 */
16324 static void
16325f_sin(argvars, rettv)
16326 typval_T *argvars;
16327 typval_T *rettv;
16328{
16329 float_T f;
16330
16331 rettv->v_type = VAR_FLOAT;
16332 if (get_float_arg(argvars, &f) == OK)
16333 rettv->vval.v_float = sin(f);
16334 else
16335 rettv->vval.v_float = 0.0;
16336}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016337
16338/*
16339 * "sinh()" function
16340 */
16341 static void
16342f_sinh(argvars, rettv)
16343 typval_T *argvars;
16344 typval_T *rettv;
16345{
16346 float_T f;
16347
16348 rettv->v_type = VAR_FLOAT;
16349 if (get_float_arg(argvars, &f) == OK)
16350 rettv->vval.v_float = sinh(f);
16351 else
16352 rettv->vval.v_float = 0.0;
16353}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016354#endif
16355
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356static int
16357#ifdef __BORLANDC__
16358 _RTLENTRYF
16359#endif
16360 item_compare __ARGS((const void *s1, const void *s2));
16361static int
16362#ifdef __BORLANDC__
16363 _RTLENTRYF
16364#endif
16365 item_compare2 __ARGS((const void *s1, const void *s2));
16366
16367static int item_compare_ic;
16368static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016369static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016370static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371#define ITEM_COMPARE_FAIL 999
16372
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 static int
16377#ifdef __BORLANDC__
16378_RTLENTRYF
16379#endif
16380item_compare(s1, s2)
16381 const void *s1;
16382 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 char_u *p1, *p2;
16385 char_u *tofree1, *tofree2;
16386 int res;
16387 char_u numbuf1[NUMBUFLEN];
16388 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016390 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16391 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016392 if (p1 == NULL)
16393 p1 = (char_u *)"";
16394 if (p2 == NULL)
16395 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396 if (item_compare_ic)
16397 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 res = STRCMP(p1, p2);
16400 vim_free(tofree1);
16401 vim_free(tofree2);
16402 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403}
16404
16405 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406#ifdef __BORLANDC__
16407_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409item_compare2(s1, s2)
16410 const void *s1;
16411 const void *s2;
16412{
16413 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016414 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016415 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016418 /* shortcut after failure in previous call; compare all items equal */
16419 if (item_compare_func_err)
16420 return 0;
16421
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016422 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16423 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16425 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016426
16427 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016428 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016429 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16430 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431 clear_tv(&argv[0]);
16432 clear_tv(&argv[1]);
16433
16434 if (res == FAIL)
16435 res = ITEM_COMPARE_FAIL;
16436 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016437 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16438 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016439 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440 clear_tv(&rettv);
16441 return res;
16442}
16443
16444/*
16445 * "sort({list})" function
16446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T *argvars;
16450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 list_T *l;
16453 listitem_T *li;
16454 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 long len;
16456 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 if (argvars[0].v_type != VAR_LIST)
16459 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 else
16461 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016463 if (l == NULL || tv_check_lock(l->lv_lock,
16464 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 return;
16466 rettv->vval.v_list = l;
16467 rettv->v_type = VAR_LIST;
16468 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470 len = list_len(l);
16471 if (len <= 1)
16472 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 item_compare_ic = FALSE;
16475 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016476 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 if (argvars[1].v_type != VAR_UNKNOWN)
16478 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016479 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482 else
16483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016484 int error = FALSE;
16485
16486 i = get_tv_number_chk(&argvars[1], &error);
16487 if (error)
16488 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 if (i == 1)
16490 item_compare_ic = TRUE;
16491 else
16492 item_compare_func = get_tv_string(&argvars[1]);
16493 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016494
16495 if (argvars[2].v_type != VAR_UNKNOWN)
16496 {
16497 /* optional third argument: {dict} */
16498 if (argvars[2].v_type != VAR_DICT)
16499 {
16500 EMSG(_(e_dictreq));
16501 return;
16502 }
16503 item_compare_selfdict = argvars[2].vval.v_dict;
16504 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506
Bram Moolenaar0d660222005-01-07 21:51:51 +000016507 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509 if (ptrs == NULL)
16510 return;
16511 i = 0;
16512 for (li = l->lv_first; li != NULL; li = li->li_next)
16513 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516 /* test the compare function */
16517 if (item_compare_func != NULL
16518 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16519 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016520 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 {
16523 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 item_compare_func == NULL ? item_compare : item_compare2);
16526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016527 if (!item_compare_func_err)
16528 {
16529 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016530 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 l->lv_len = 0;
16532 for (i = 0; i < len; ++i)
16533 list_append(l, ptrs[i]);
16534 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016535 }
16536
16537 vim_free(ptrs);
16538 }
16539}
16540
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016541/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016542 * "soundfold({word})" function
16543 */
16544 static void
16545f_soundfold(argvars, rettv)
16546 typval_T *argvars;
16547 typval_T *rettv;
16548{
16549 char_u *s;
16550
16551 rettv->v_type = VAR_STRING;
16552 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016553#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016554 rettv->vval.v_string = eval_soundfold(s);
16555#else
16556 rettv->vval.v_string = vim_strsave(s);
16557#endif
16558}
16559
16560/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016561 * "spellbadword()" function
16562 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016563 static void
16564f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016565 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016566 typval_T *rettv;
16567{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016568 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016569 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016570 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016571
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016572 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016573 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016574
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016575#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016576 if (argvars[0].v_type == VAR_UNKNOWN)
16577 {
16578 /* Find the start and length of the badly spelled word. */
16579 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16580 if (len != 0)
16581 word = ml_get_cursor();
16582 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016583 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016584 {
16585 char_u *str = get_tv_string_chk(&argvars[0]);
16586 int capcol = -1;
16587
16588 if (str != NULL)
16589 {
16590 /* Check the argument for spelling. */
16591 while (*str != NUL)
16592 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016593 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016594 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016595 {
16596 word = str;
16597 break;
16598 }
16599 str += len;
16600 }
16601 }
16602 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016603#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016604
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016605 list_append_string(rettv->vval.v_list, word, len);
16606 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016607 attr == HLF_SPB ? "bad" :
16608 attr == HLF_SPR ? "rare" :
16609 attr == HLF_SPL ? "local" :
16610 attr == HLF_SPC ? "caps" :
16611 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016612}
16613
16614/*
16615 * "spellsuggest()" function
16616 */
16617 static void
16618f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016619 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016620 typval_T *rettv;
16621{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016622#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016623 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016624 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016625 int maxcount;
16626 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016627 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016628 listitem_T *li;
16629 int need_capital = FALSE;
16630#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016631
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016632 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016633 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016634
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016635#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016636 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016637 {
16638 str = get_tv_string(&argvars[0]);
16639 if (argvars[1].v_type != VAR_UNKNOWN)
16640 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016641 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016642 if (maxcount <= 0)
16643 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016644 if (argvars[2].v_type != VAR_UNKNOWN)
16645 {
16646 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16647 if (typeerr)
16648 return;
16649 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016650 }
16651 else
16652 maxcount = 25;
16653
Bram Moolenaar4770d092006-01-12 23:22:24 +000016654 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016655
16656 for (i = 0; i < ga.ga_len; ++i)
16657 {
16658 str = ((char_u **)ga.ga_data)[i];
16659
16660 li = listitem_alloc();
16661 if (li == NULL)
16662 vim_free(str);
16663 else
16664 {
16665 li->li_tv.v_type = VAR_STRING;
16666 li->li_tv.v_lock = 0;
16667 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016668 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016669 }
16670 }
16671 ga_clear(&ga);
16672 }
16673#endif
16674}
16675
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016677f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016678 typval_T *argvars;
16679 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016680{
16681 char_u *str;
16682 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016683 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 regmatch_T regmatch;
16685 char_u patbuf[NUMBUFLEN];
16686 char_u *save_cpo;
16687 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016688 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016689 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016690 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691
16692 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16693 save_cpo = p_cpo;
16694 p_cpo = (char_u *)"";
16695
16696 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016697 if (argvars[1].v_type != VAR_UNKNOWN)
16698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16700 if (pat == NULL)
16701 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016704 }
16705 if (pat == NULL || *pat == NUL)
16706 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016707
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016708 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016710 if (typeerr)
16711 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712
Bram Moolenaar0d660222005-01-07 21:51:51 +000016713 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16714 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016716 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016717 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016718 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016719 if (*str == NUL)
16720 match = FALSE; /* empty item at the end */
16721 else
16722 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016723 if (match)
16724 end = regmatch.startp[0];
16725 else
16726 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016727 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16728 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016730 if (list_append_string(rettv->vval.v_list, str,
16731 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016733 }
16734 if (!match)
16735 break;
16736 /* Advance to just after the match. */
16737 if (regmatch.endp[0] > str)
16738 col = 0;
16739 else
16740 {
16741 /* Don't get stuck at the same match. */
16742#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016743 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016744#else
16745 col = 1;
16746#endif
16747 }
16748 str = regmatch.endp[0];
16749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750
Bram Moolenaar0d660222005-01-07 21:51:51 +000016751 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755}
16756
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016757#ifdef FEAT_FLOAT
16758/*
16759 * "sqrt()" function
16760 */
16761 static void
16762f_sqrt(argvars, rettv)
16763 typval_T *argvars;
16764 typval_T *rettv;
16765{
16766 float_T f;
16767
16768 rettv->v_type = VAR_FLOAT;
16769 if (get_float_arg(argvars, &f) == OK)
16770 rettv->vval.v_float = sqrt(f);
16771 else
16772 rettv->vval.v_float = 0.0;
16773}
16774
16775/*
16776 * "str2float()" function
16777 */
16778 static void
16779f_str2float(argvars, rettv)
16780 typval_T *argvars;
16781 typval_T *rettv;
16782{
16783 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16784
16785 if (*p == '+')
16786 p = skipwhite(p + 1);
16787 (void)string2float(p, &rettv->vval.v_float);
16788 rettv->v_type = VAR_FLOAT;
16789}
16790#endif
16791
Bram Moolenaar2c932302006-03-18 21:42:09 +000016792/*
16793 * "str2nr()" function
16794 */
16795 static void
16796f_str2nr(argvars, rettv)
16797 typval_T *argvars;
16798 typval_T *rettv;
16799{
16800 int base = 10;
16801 char_u *p;
16802 long n;
16803
16804 if (argvars[1].v_type != VAR_UNKNOWN)
16805 {
16806 base = get_tv_number(&argvars[1]);
16807 if (base != 8 && base != 10 && base != 16)
16808 {
16809 EMSG(_(e_invarg));
16810 return;
16811 }
16812 }
16813
16814 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016815 if (*p == '+')
16816 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016817 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16818 rettv->vval.v_number = n;
16819}
16820
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821#ifdef HAVE_STRFTIME
16822/*
16823 * "strftime({format}[, {time}])" function
16824 */
16825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016826f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 typval_T *argvars;
16828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829{
16830 char_u result_buf[256];
16831 struct tm *curtime;
16832 time_t seconds;
16833 char_u *p;
16834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016837 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016838 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 seconds = time(NULL);
16840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 curtime = localtime(&seconds);
16843 /* MSVC returns NULL for an invalid value of seconds. */
16844 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 else
16847 {
16848# ifdef FEAT_MBYTE
16849 vimconv_T conv;
16850 char_u *enc;
16851
16852 conv.vc_type = CONV_NONE;
16853 enc = enc_locale();
16854 convert_setup(&conv, p_enc, enc);
16855 if (conv.vc_type != CONV_NONE)
16856 p = string_convert(&conv, p, NULL);
16857# endif
16858 if (p != NULL)
16859 (void)strftime((char *)result_buf, sizeof(result_buf),
16860 (char *)p, curtime);
16861 else
16862 result_buf[0] = NUL;
16863
16864# ifdef FEAT_MBYTE
16865 if (conv.vc_type != CONV_NONE)
16866 vim_free(p);
16867 convert_setup(&conv, enc, p_enc);
16868 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016869 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 else
16871# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016872 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873
16874# ifdef FEAT_MBYTE
16875 /* Release conversion descriptors */
16876 convert_setup(&conv, NULL, NULL);
16877 vim_free(enc);
16878# endif
16879 }
16880}
16881#endif
16882
16883/*
16884 * "stridx()" function
16885 */
16886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016887f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016888 typval_T *argvars;
16889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890{
16891 char_u buf[NUMBUFLEN];
16892 char_u *needle;
16893 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016896 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016898 needle = get_tv_string_chk(&argvars[1]);
16899 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 if (needle == NULL || haystack == NULL)
16902 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 if (argvars[2].v_type != VAR_UNKNOWN)
16905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 int error = FALSE;
16907
16908 start_idx = get_tv_number_chk(&argvars[2], &error);
16909 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016911 if (start_idx >= 0)
16912 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 }
16914
16915 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16916 if (pos != NULL)
16917 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918}
16919
16920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016921 * "string()" function
16922 */
16923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016924f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016925 typval_T *argvars;
16926 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016927{
16928 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016929 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016932 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016933 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016934 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016935 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936}
16937
16938/*
16939 * "strlen()" function
16940 */
16941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016942f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 typval_T *argvars;
16944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016946 rettv->vval.v_number = (varnumber_T)(STRLEN(
16947 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948}
16949
16950/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016951 * "strchars()" function
16952 */
16953 static void
16954f_strchars(argvars, rettv)
16955 typval_T *argvars;
16956 typval_T *rettv;
16957{
16958 char_u *s = get_tv_string(&argvars[0]);
16959#ifdef FEAT_MBYTE
16960 varnumber_T len = 0;
16961
16962 while (*s != NUL)
16963 {
16964 mb_cptr2char_adv(&s);
16965 ++len;
16966 }
16967 rettv->vval.v_number = len;
16968#else
16969 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16970#endif
16971}
16972
16973/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016974 * "strdisplaywidth()" function
16975 */
16976 static void
16977f_strdisplaywidth(argvars, rettv)
16978 typval_T *argvars;
16979 typval_T *rettv;
16980{
16981 char_u *s = get_tv_string(&argvars[0]);
16982 int col = 0;
16983
16984 if (argvars[1].v_type != VAR_UNKNOWN)
16985 col = get_tv_number(&argvars[1]);
16986
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016987 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016988}
16989
16990/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016991 * "strwidth()" function
16992 */
16993 static void
16994f_strwidth(argvars, rettv)
16995 typval_T *argvars;
16996 typval_T *rettv;
16997{
16998 char_u *s = get_tv_string(&argvars[0]);
16999
17000 rettv->vval.v_number = (varnumber_T)(
17001#ifdef FEAT_MBYTE
17002 mb_string2cells(s, -1)
17003#else
17004 STRLEN(s)
17005#endif
17006 );
17007}
17008
17009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 * "strpart()" function
17011 */
17012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 typval_T *argvars;
17015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016{
17017 char_u *p;
17018 int n;
17019 int len;
17020 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017023 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 slen = (int)STRLEN(p);
17025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 n = get_tv_number_chk(&argvars[1], &error);
17027 if (error)
17028 len = 0;
17029 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017030 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 else
17032 len = slen - n; /* default len: all bytes that are available. */
17033
17034 /*
17035 * Only return the overlap between the specified part and the actual
17036 * string.
17037 */
17038 if (n < 0)
17039 {
17040 len += n;
17041 n = 0;
17042 }
17043 else if (n > slen)
17044 n = slen;
17045 if (len < 0)
17046 len = 0;
17047 else if (n + len > slen)
17048 len = slen - n;
17049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050 rettv->v_type = VAR_STRING;
17051 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052}
17053
17054/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 * "strridx()" function
17056 */
17057 static void
17058f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017059 typval_T *argvars;
17060 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061{
17062 char_u buf[NUMBUFLEN];
17063 char_u *needle;
17064 char_u *haystack;
17065 char_u *rest;
17066 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017067 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 needle = get_tv_string_chk(&argvars[1]);
17070 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017071
17072 rettv->vval.v_number = -1;
17073 if (needle == NULL || haystack == NULL)
17074 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017075
17076 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017077 if (argvars[2].v_type != VAR_UNKNOWN)
17078 {
17079 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017081 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017083 }
17084 else
17085 end_idx = haystack_len;
17086
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017088 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017090 lastmatch = haystack + end_idx;
17091 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017093 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094 for (rest = haystack; *rest != '\0'; ++rest)
17095 {
17096 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017097 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 break;
17099 lastmatch = rest;
17100 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017101 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102
17103 if (lastmatch == NULL)
17104 rettv->vval.v_number = -1;
17105 else
17106 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17107}
17108
17109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 * "strtrans()" function
17111 */
17112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017114 typval_T *argvars;
17115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 rettv->v_type = VAR_STRING;
17118 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119}
17120
17121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122 * "submatch()" function
17123 */
17124 static void
17125f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017126 typval_T *argvars;
17127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128{
17129 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017130 rettv->vval.v_string =
17131 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132}
17133
17134/*
17135 * "substitute()" function
17136 */
17137 static void
17138f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017139 typval_T *argvars;
17140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141{
17142 char_u patbuf[NUMBUFLEN];
17143 char_u subbuf[NUMBUFLEN];
17144 char_u flagsbuf[NUMBUFLEN];
17145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017146 char_u *str = get_tv_string_chk(&argvars[0]);
17147 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17148 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17149 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17150
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17153 rettv->vval.v_string = NULL;
17154 else
17155 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156}
17157
17158/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017159 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017162f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165{
17166 int id = 0;
17167#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017168 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 long col;
17170 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017171 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017173 lnum = get_tv_lnum(argvars); /* -1 on type error */
17174 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17175 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017178 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017179 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180#endif
17181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017182 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183}
17184
17185/*
17186 * "synIDattr(id, what [, mode])" function
17187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017190 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192{
17193 char_u *p = NULL;
17194#ifdef FEAT_SYN_HL
17195 int id;
17196 char_u *what;
17197 char_u *mode;
17198 char_u modebuf[NUMBUFLEN];
17199 int modec;
17200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 id = get_tv_number(&argvars[0]);
17202 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017205 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017207 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 modec = 0; /* replace invalid with current */
17209 }
17210 else
17211 {
17212#ifdef FEAT_GUI
17213 if (gui.in_use)
17214 modec = 'g';
17215 else
17216#endif
17217 if (t_colors > 1)
17218 modec = 'c';
17219 else
17220 modec = 't';
17221 }
17222
17223
17224 switch (TOLOWER_ASC(what[0]))
17225 {
17226 case 'b':
17227 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17228 p = highlight_color(id, what, modec);
17229 else /* bold */
17230 p = highlight_has_attr(id, HL_BOLD, modec);
17231 break;
17232
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017233 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 p = highlight_color(id, what, modec);
17235 break;
17236
17237 case 'i':
17238 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17239 p = highlight_has_attr(id, HL_INVERSE, modec);
17240 else /* italic */
17241 p = highlight_has_attr(id, HL_ITALIC, modec);
17242 break;
17243
17244 case 'n': /* name */
17245 p = get_highlight_name(NULL, id - 1);
17246 break;
17247
17248 case 'r': /* reverse */
17249 p = highlight_has_attr(id, HL_INVERSE, modec);
17250 break;
17251
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017252 case 's':
17253 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17254 p = highlight_color(id, what, modec);
17255 else /* standout */
17256 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 break;
17258
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017259 case 'u':
17260 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17261 /* underline */
17262 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17263 else
17264 /* undercurl */
17265 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 break;
17267 }
17268
17269 if (p != NULL)
17270 p = vim_strsave(p);
17271#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272 rettv->v_type = VAR_STRING;
17273 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274}
17275
17276/*
17277 * "synIDtrans(id)" function
17278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017280f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017281 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283{
17284 int id;
17285
17286#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288
17289 if (id > 0)
17290 id = syn_get_final_id(id);
17291 else
17292#endif
17293 id = 0;
17294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017295 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017299 * "synconcealed(lnum, col)" function
17300 */
17301 static void
17302f_synconcealed(argvars, rettv)
17303 typval_T *argvars UNUSED;
17304 typval_T *rettv;
17305{
17306#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17307 long lnum;
17308 long col;
17309 int syntax_flags = 0;
17310 int cchar;
17311 int matchid = 0;
17312 char_u str[NUMBUFLEN];
17313#endif
17314
17315 rettv->v_type = VAR_LIST;
17316 rettv->vval.v_list = NULL;
17317
17318#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17319 lnum = get_tv_lnum(argvars); /* -1 on type error */
17320 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17321
17322 vim_memset(str, NUL, sizeof(str));
17323
17324 if (rettv_list_alloc(rettv) != FAIL)
17325 {
17326 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17327 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17328 && curwin->w_p_cole > 0)
17329 {
17330 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17331 syntax_flags = get_syntax_info(&matchid);
17332
17333 /* get the conceal character */
17334 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17335 {
17336 cchar = syn_get_sub_char();
17337 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17338 cchar = lcs_conceal;
17339 if (cchar != NUL)
17340 {
17341# ifdef FEAT_MBYTE
17342 if (has_mbyte)
17343 (*mb_char2bytes)(cchar, str);
17344 else
17345# endif
17346 str[0] = cchar;
17347 }
17348 }
17349 }
17350
17351 list_append_number(rettv->vval.v_list,
17352 (syntax_flags & HL_CONCEAL) != 0);
17353 /* -1 to auto-determine strlen */
17354 list_append_string(rettv->vval.v_list, str, -1);
17355 list_append_number(rettv->vval.v_list, matchid);
17356 }
17357#endif
17358}
17359
17360/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017361 * "synstack(lnum, col)" function
17362 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017363 static void
17364f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017365 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017366 typval_T *rettv;
17367{
17368#ifdef FEAT_SYN_HL
17369 long lnum;
17370 long col;
17371 int i;
17372 int id;
17373#endif
17374
17375 rettv->v_type = VAR_LIST;
17376 rettv->vval.v_list = NULL;
17377
17378#ifdef FEAT_SYN_HL
17379 lnum = get_tv_lnum(argvars); /* -1 on type error */
17380 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17381
17382 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017383 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017384 && rettv_list_alloc(rettv) != FAIL)
17385 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017386 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017387 for (i = 0; ; ++i)
17388 {
17389 id = syn_get_stack_item(i);
17390 if (id < 0)
17391 break;
17392 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17393 break;
17394 }
17395 }
17396#endif
17397}
17398
17399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 * "system()" function
17401 */
17402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 typval_T *argvars;
17405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017407 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017409 char_u *infile = NULL;
17410 char_u buf[NUMBUFLEN];
17411 int err = FALSE;
17412 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017414 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017415 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017417 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017418 {
17419 /*
17420 * Write the string to a temp file, to be used for input of the shell
17421 * command.
17422 */
17423 if ((infile = vim_tempname('i')) == NULL)
17424 {
17425 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017426 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017427 }
17428
17429 fd = mch_fopen((char *)infile, WRITEBIN);
17430 if (fd == NULL)
17431 {
17432 EMSG2(_(e_notopen), infile);
17433 goto done;
17434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017435 p = get_tv_string_buf_chk(&argvars[1], buf);
17436 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017437 {
17438 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017439 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017440 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017441 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17442 err = TRUE;
17443 if (fclose(fd) != 0)
17444 err = TRUE;
17445 if (err)
17446 {
17447 EMSG(_("E677: Error writing temp file"));
17448 goto done;
17449 }
17450 }
17451
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017452 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17453 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017454
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455#ifdef USE_CR
17456 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017457 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 {
17459 char_u *s;
17460
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017461 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 {
17463 if (*s == CAR)
17464 *s = NL;
17465 }
17466 }
17467#else
17468# ifdef USE_CRNL
17469 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017470 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 {
17472 char_u *s, *d;
17473
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017474 d = res;
17475 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 {
17477 if (s[0] == CAR && s[1] == NL)
17478 ++s;
17479 *d++ = *s;
17480 }
17481 *d = NUL;
17482 }
17483# endif
17484#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017485
17486done:
17487 if (infile != NULL)
17488 {
17489 mch_remove(infile);
17490 vim_free(infile);
17491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 rettv->v_type = VAR_STRING;
17493 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494}
17495
17496/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017497 * "tabpagebuflist()" function
17498 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017499 static void
17500f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017501 typval_T *argvars UNUSED;
17502 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017503{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017504#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017505 tabpage_T *tp;
17506 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017507
17508 if (argvars[0].v_type == VAR_UNKNOWN)
17509 wp = firstwin;
17510 else
17511 {
17512 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17513 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017514 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017515 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017516 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017517 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017518 for (; wp != NULL; wp = wp->w_next)
17519 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017520 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017521 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017522 }
17523#endif
17524}
17525
17526
17527/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017528 * "tabpagenr()" function
17529 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017530 static void
17531f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017532 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017533 typval_T *rettv;
17534{
17535 int nr = 1;
17536#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017537 char_u *arg;
17538
17539 if (argvars[0].v_type != VAR_UNKNOWN)
17540 {
17541 arg = get_tv_string_chk(&argvars[0]);
17542 nr = 0;
17543 if (arg != NULL)
17544 {
17545 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017546 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017547 else
17548 EMSG2(_(e_invexpr2), arg);
17549 }
17550 }
17551 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017552 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017553#endif
17554 rettv->vval.v_number = nr;
17555}
17556
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017557
17558#ifdef FEAT_WINDOWS
17559static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17560
17561/*
17562 * Common code for tabpagewinnr() and winnr().
17563 */
17564 static int
17565get_winnr(tp, argvar)
17566 tabpage_T *tp;
17567 typval_T *argvar;
17568{
17569 win_T *twin;
17570 int nr = 1;
17571 win_T *wp;
17572 char_u *arg;
17573
17574 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17575 if (argvar->v_type != VAR_UNKNOWN)
17576 {
17577 arg = get_tv_string_chk(argvar);
17578 if (arg == NULL)
17579 nr = 0; /* type error; errmsg already given */
17580 else if (STRCMP(arg, "$") == 0)
17581 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17582 else if (STRCMP(arg, "#") == 0)
17583 {
17584 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17585 if (twin == NULL)
17586 nr = 0;
17587 }
17588 else
17589 {
17590 EMSG2(_(e_invexpr2), arg);
17591 nr = 0;
17592 }
17593 }
17594
17595 if (nr > 0)
17596 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17597 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017598 {
17599 if (wp == NULL)
17600 {
17601 /* didn't find it in this tabpage */
17602 nr = 0;
17603 break;
17604 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017605 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017606 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017607 return nr;
17608}
17609#endif
17610
17611/*
17612 * "tabpagewinnr()" function
17613 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017614 static void
17615f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017616 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017617 typval_T *rettv;
17618{
17619 int nr = 1;
17620#ifdef FEAT_WINDOWS
17621 tabpage_T *tp;
17622
17623 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17624 if (tp == NULL)
17625 nr = 0;
17626 else
17627 nr = get_winnr(tp, &argvars[1]);
17628#endif
17629 rettv->vval.v_number = nr;
17630}
17631
17632
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017633/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017634 * "tagfiles()" function
17635 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017636 static void
17637f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017638 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017639 typval_T *rettv;
17640{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017641 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017642 tagname_T tn;
17643 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017644
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017645 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017646 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017647 fname = alloc(MAXPATHL);
17648 if (fname == NULL)
17649 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017650
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017651 for (first = TRUE; ; first = FALSE)
17652 if (get_tagfname(&tn, first, fname) == FAIL
17653 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017654 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017655 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017656 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017657}
17658
17659/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017660 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017661 */
17662 static void
17663f_taglist(argvars, rettv)
17664 typval_T *argvars;
17665 typval_T *rettv;
17666{
17667 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017668
17669 tag_pattern = get_tv_string(&argvars[0]);
17670
17671 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017672 if (*tag_pattern == NUL)
17673 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017674
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017675 if (rettv_list_alloc(rettv) == OK)
17676 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017677}
17678
17679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 * "tempname()" function
17681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017683f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686{
17687 static int x = 'A';
17688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689 rettv->v_type = VAR_STRING;
17690 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691
17692 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17693 * names. Skip 'I' and 'O', they are used for shell redirection. */
17694 do
17695 {
17696 if (x == 'Z')
17697 x = '0';
17698 else if (x == '9')
17699 x = 'A';
17700 else
17701 {
17702#ifdef EBCDIC
17703 if (x == 'I')
17704 x = 'J';
17705 else if (x == 'R')
17706 x = 'S';
17707 else
17708#endif
17709 ++x;
17710 }
17711 } while (x == 'I' || x == 'O');
17712}
17713
17714/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017715 * "test(list)" function: Just checking the walls...
17716 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017717 static void
17718f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017719 typval_T *argvars UNUSED;
17720 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017721{
17722 /* Used for unit testing. Change the code below to your liking. */
17723#if 0
17724 listitem_T *li;
17725 list_T *l;
17726 char_u *bad, *good;
17727
17728 if (argvars[0].v_type != VAR_LIST)
17729 return;
17730 l = argvars[0].vval.v_list;
17731 if (l == NULL)
17732 return;
17733 li = l->lv_first;
17734 if (li == NULL)
17735 return;
17736 bad = get_tv_string(&li->li_tv);
17737 li = li->li_next;
17738 if (li == NULL)
17739 return;
17740 good = get_tv_string(&li->li_tv);
17741 rettv->vval.v_number = test_edit_score(bad, good);
17742#endif
17743}
17744
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017745#ifdef FEAT_FLOAT
17746/*
17747 * "tan()" function
17748 */
17749 static void
17750f_tan(argvars, rettv)
17751 typval_T *argvars;
17752 typval_T *rettv;
17753{
17754 float_T f;
17755
17756 rettv->v_type = VAR_FLOAT;
17757 if (get_float_arg(argvars, &f) == OK)
17758 rettv->vval.v_float = tan(f);
17759 else
17760 rettv->vval.v_float = 0.0;
17761}
17762
17763/*
17764 * "tanh()" function
17765 */
17766 static void
17767f_tanh(argvars, rettv)
17768 typval_T *argvars;
17769 typval_T *rettv;
17770{
17771 float_T f;
17772
17773 rettv->v_type = VAR_FLOAT;
17774 if (get_float_arg(argvars, &f) == OK)
17775 rettv->vval.v_float = tanh(f);
17776 else
17777 rettv->vval.v_float = 0.0;
17778}
17779#endif
17780
Bram Moolenaard52d9742005-08-21 22:20:28 +000017781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 * "tolower(string)" function
17783 */
17784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017785f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 typval_T *argvars;
17787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788{
17789 char_u *p;
17790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017791 p = vim_strsave(get_tv_string(&argvars[0]));
17792 rettv->v_type = VAR_STRING;
17793 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794
17795 if (p != NULL)
17796 while (*p != NUL)
17797 {
17798#ifdef FEAT_MBYTE
17799 int l;
17800
17801 if (enc_utf8)
17802 {
17803 int c, lc;
17804
17805 c = utf_ptr2char(p);
17806 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017807 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 /* TODO: reallocate string when byte count changes. */
17809 if (utf_char2len(lc) == l)
17810 utf_char2bytes(lc, p);
17811 p += l;
17812 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017813 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 p += l; /* skip multi-byte character */
17815 else
17816#endif
17817 {
17818 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17819 ++p;
17820 }
17821 }
17822}
17823
17824/*
17825 * "toupper(string)" function
17826 */
17827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 typval_T *argvars;
17830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017832 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017833 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834}
17835
17836/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017837 * "tr(string, fromstr, tostr)" function
17838 */
17839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017840f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 typval_T *argvars;
17842 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017843{
17844 char_u *instr;
17845 char_u *fromstr;
17846 char_u *tostr;
17847 char_u *p;
17848#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017849 int inlen;
17850 int fromlen;
17851 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017852 int idx;
17853 char_u *cpstr;
17854 int cplen;
17855 int first = TRUE;
17856#endif
17857 char_u buf[NUMBUFLEN];
17858 char_u buf2[NUMBUFLEN];
17859 garray_T ga;
17860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017861 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017862 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17863 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017864
17865 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017866 rettv->v_type = VAR_STRING;
17867 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017868 if (fromstr == NULL || tostr == NULL)
17869 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017870 ga_init2(&ga, (int)sizeof(char), 80);
17871
17872#ifdef FEAT_MBYTE
17873 if (!has_mbyte)
17874#endif
17875 /* not multi-byte: fromstr and tostr must be the same length */
17876 if (STRLEN(fromstr) != STRLEN(tostr))
17877 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017878#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017879error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017880#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017881 EMSG2(_(e_invarg2), fromstr);
17882 ga_clear(&ga);
17883 return;
17884 }
17885
17886 /* fromstr and tostr have to contain the same number of chars */
17887 while (*instr != NUL)
17888 {
17889#ifdef FEAT_MBYTE
17890 if (has_mbyte)
17891 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017892 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017893 cpstr = instr;
17894 cplen = inlen;
17895 idx = 0;
17896 for (p = fromstr; *p != NUL; p += fromlen)
17897 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017898 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017899 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17900 {
17901 for (p = tostr; *p != NUL; p += tolen)
17902 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017903 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017904 if (idx-- == 0)
17905 {
17906 cplen = tolen;
17907 cpstr = p;
17908 break;
17909 }
17910 }
17911 if (*p == NUL) /* tostr is shorter than fromstr */
17912 goto error;
17913 break;
17914 }
17915 ++idx;
17916 }
17917
17918 if (first && cpstr == instr)
17919 {
17920 /* Check that fromstr and tostr have the same number of
17921 * (multi-byte) characters. Done only once when a character
17922 * of instr doesn't appear in fromstr. */
17923 first = FALSE;
17924 for (p = tostr; *p != NUL; p += tolen)
17925 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017926 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017927 --idx;
17928 }
17929 if (idx != 0)
17930 goto error;
17931 }
17932
17933 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017934 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017935 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017936
17937 instr += inlen;
17938 }
17939 else
17940#endif
17941 {
17942 /* When not using multi-byte chars we can do it faster. */
17943 p = vim_strchr(fromstr, *instr);
17944 if (p != NULL)
17945 ga_append(&ga, tostr[p - fromstr]);
17946 else
17947 ga_append(&ga, *instr);
17948 ++instr;
17949 }
17950 }
17951
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017952 /* add a terminating NUL */
17953 ga_grow(&ga, 1);
17954 ga_append(&ga, NUL);
17955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017956 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017957}
17958
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017959#ifdef FEAT_FLOAT
17960/*
17961 * "trunc({float})" function
17962 */
17963 static void
17964f_trunc(argvars, rettv)
17965 typval_T *argvars;
17966 typval_T *rettv;
17967{
17968 float_T f;
17969
17970 rettv->v_type = VAR_FLOAT;
17971 if (get_float_arg(argvars, &f) == OK)
17972 /* trunc() is not in C90, use floor() or ceil() instead. */
17973 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17974 else
17975 rettv->vval.v_float = 0.0;
17976}
17977#endif
17978
Bram Moolenaar8299df92004-07-10 09:47:34 +000017979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 * "type(expr)" function
17981 */
17982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017983f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017984 typval_T *argvars;
17985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017987 int n;
17988
17989 switch (argvars[0].v_type)
17990 {
17991 case VAR_NUMBER: n = 0; break;
17992 case VAR_STRING: n = 1; break;
17993 case VAR_FUNC: n = 2; break;
17994 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017995 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017996#ifdef FEAT_FLOAT
17997 case VAR_FLOAT: n = 5; break;
17998#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017999 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18000 }
18001 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002}
18003
18004/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018005 * "undofile(name)" function
18006 */
18007 static void
18008f_undofile(argvars, rettv)
18009 typval_T *argvars;
18010 typval_T *rettv;
18011{
18012 rettv->v_type = VAR_STRING;
18013#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018014 {
18015 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18016
18017 if (ffname != NULL)
18018 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18019 vim_free(ffname);
18020 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018021#else
18022 rettv->vval.v_string = NULL;
18023#endif
18024}
18025
18026/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018027 * "undotree()" function
18028 */
18029 static void
18030f_undotree(argvars, rettv)
18031 typval_T *argvars UNUSED;
18032 typval_T *rettv;
18033{
18034 if (rettv_dict_alloc(rettv) == OK)
18035 {
18036 dict_T *dict = rettv->vval.v_dict;
18037 list_T *list;
18038
Bram Moolenaar730cde92010-06-27 05:18:54 +020018039 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018040 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018041 dict_add_nr_str(dict, "save_last",
18042 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018043 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18044 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018045 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018046
18047 list = list_alloc();
18048 if (list != NULL)
18049 {
18050 u_eval_tree(curbuf->b_u_oldhead, list);
18051 dict_add_list(dict, "entries", list);
18052 }
18053 }
18054}
18055
18056/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018057 * "values(dict)" function
18058 */
18059 static void
18060f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 typval_T *argvars;
18062 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018063{
18064 dict_list(argvars, rettv, 1);
18065}
18066
18067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 * "virtcol(string)" function
18069 */
18070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018071f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018072 typval_T *argvars;
18073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
18075 colnr_T vcol = 0;
18076 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018077 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018079 fp = var2fpos(&argvars[0], FALSE, &fnum);
18080 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18081 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 {
18083 getvvcol(curwin, fp, NULL, NULL, &vcol);
18084 ++vcol;
18085 }
18086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088}
18089
18090/*
18091 * "visualmode()" function
18092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018094f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018095 typval_T *argvars UNUSED;
18096 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097{
18098#ifdef FEAT_VISUAL
18099 char_u str[2];
18100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018101 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 str[0] = curbuf->b_visual_mode_eval;
18103 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018104 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105
18106 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018107 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109#endif
18110}
18111
18112/*
18113 * "winbufnr(nr)" function
18114 */
18115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018116f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018117 typval_T *argvars;
18118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
18120 win_T *wp;
18121
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018122 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018124 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018126 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127}
18128
18129/*
18130 * "wincol()" function
18131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136{
18137 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139}
18140
18141/*
18142 * "winheight(nr)" function
18143 */
18144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018145f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018146 typval_T *argvars;
18147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148{
18149 win_T *wp;
18150
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018151 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156}
18157
18158/*
18159 * "winline()" function
18160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165{
18166 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168}
18169
18170/*
18171 * "winnr()" function
18172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177{
18178 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018179
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018181 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184}
18185
18186/*
18187 * "winrestcmd()" function
18188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193{
18194#ifdef FEAT_WINDOWS
18195 win_T *wp;
18196 int winnr = 1;
18197 garray_T ga;
18198 char_u buf[50];
18199
18200 ga_init2(&ga, (int)sizeof(char), 70);
18201 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18202 {
18203 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18204 ga_concat(&ga, buf);
18205# ifdef FEAT_VERTSPLIT
18206 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18207 ga_concat(&ga, buf);
18208# endif
18209 ++winnr;
18210 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018211 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018217 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218}
18219
18220/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018221 * "winrestview()" function
18222 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018223 static void
18224f_winrestview(argvars, rettv)
18225 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018226 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018227{
18228 dict_T *dict;
18229
18230 if (argvars[0].v_type != VAR_DICT
18231 || (dict = argvars[0].vval.v_dict) == NULL)
18232 EMSG(_(e_invarg));
18233 else
18234 {
18235 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18236 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18237#ifdef FEAT_VIRTUALEDIT
18238 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18239#endif
18240 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018241 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018242
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018243 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018244#ifdef FEAT_DIFF
18245 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18246#endif
18247 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18248 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18249
18250 check_cursor();
18251 changed_cline_bef_curs();
18252 invalidate_botline();
18253 redraw_later(VALID);
18254
18255 if (curwin->w_topline == 0)
18256 curwin->w_topline = 1;
18257 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18258 curwin->w_topline = curbuf->b_ml.ml_line_count;
18259#ifdef FEAT_DIFF
18260 check_topfill(curwin, TRUE);
18261#endif
18262 }
18263}
18264
18265/*
18266 * "winsaveview()" function
18267 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018268 static void
18269f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018270 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018271 typval_T *rettv;
18272{
18273 dict_T *dict;
18274
Bram Moolenaara800b422010-06-27 01:15:55 +020018275 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018276 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018277 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018278
18279 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18280 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18281#ifdef FEAT_VIRTUALEDIT
18282 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18283#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018284 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018285 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18286
18287 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18288#ifdef FEAT_DIFF
18289 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18290#endif
18291 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18292 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18293}
18294
18295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 * "winwidth(nr)" function
18297 */
18298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018300 typval_T *argvars;
18301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302{
18303 win_T *wp;
18304
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018305 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 else
18309#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018310 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018312 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313#endif
18314}
18315
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018317 * "writefile()" function
18318 */
18319 static void
18320f_writefile(argvars, rettv)
18321 typval_T *argvars;
18322 typval_T *rettv;
18323{
18324 int binary = FALSE;
18325 char_u *fname;
18326 FILE *fd;
18327 listitem_T *li;
18328 char_u *s;
18329 int ret = 0;
18330 int c;
18331
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018332 if (check_restricted() || check_secure())
18333 return;
18334
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018335 if (argvars[0].v_type != VAR_LIST)
18336 {
18337 EMSG2(_(e_listarg), "writefile()");
18338 return;
18339 }
18340 if (argvars[0].vval.v_list == NULL)
18341 return;
18342
18343 if (argvars[2].v_type != VAR_UNKNOWN
18344 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18345 binary = TRUE;
18346
18347 /* Always open the file in binary mode, library functions have a mind of
18348 * their own about CR-LF conversion. */
18349 fname = get_tv_string(&argvars[1]);
18350 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18351 {
18352 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18353 ret = -1;
18354 }
18355 else
18356 {
18357 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18358 li = li->li_next)
18359 {
18360 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18361 {
18362 if (*s == '\n')
18363 c = putc(NUL, fd);
18364 else
18365 c = putc(*s, fd);
18366 if (c == EOF)
18367 {
18368 ret = -1;
18369 break;
18370 }
18371 }
18372 if (!binary || li->li_next != NULL)
18373 if (putc('\n', fd) == EOF)
18374 {
18375 ret = -1;
18376 break;
18377 }
18378 if (ret < 0)
18379 {
18380 EMSG(_(e_write));
18381 break;
18382 }
18383 }
18384 fclose(fd);
18385 }
18386
18387 rettv->vval.v_number = ret;
18388}
18389
18390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018392 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 */
18394 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018395var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018396 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018397 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018398 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018400 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018402 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403
Bram Moolenaara5525202006-03-02 22:52:09 +000018404 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018405 if (varp->v_type == VAR_LIST)
18406 {
18407 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018408 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018409 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018410 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018411
18412 l = varp->vval.v_list;
18413 if (l == NULL)
18414 return NULL;
18415
18416 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018417 pos.lnum = list_find_nr(l, 0L, &error);
18418 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018419 return NULL; /* invalid line number */
18420
18421 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018422 pos.col = list_find_nr(l, 1L, &error);
18423 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018424 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018425 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018426
18427 /* We accept "$" for the column number: last column. */
18428 li = list_find(l, 1L);
18429 if (li != NULL && li->li_tv.v_type == VAR_STRING
18430 && li->li_tv.vval.v_string != NULL
18431 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18432 pos.col = len + 1;
18433
Bram Moolenaara5525202006-03-02 22:52:09 +000018434 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018435 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018436 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018437 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018438
Bram Moolenaara5525202006-03-02 22:52:09 +000018439#ifdef FEAT_VIRTUALEDIT
18440 /* Get the virtual offset. Defaults to zero. */
18441 pos.coladd = list_find_nr(l, 2L, &error);
18442 if (error)
18443 pos.coladd = 0;
18444#endif
18445
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018446 return &pos;
18447 }
18448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018449 name = get_tv_string_chk(varp);
18450 if (name == NULL)
18451 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018452 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018454#ifdef FEAT_VISUAL
18455 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18456 {
18457 if (VIsual_active)
18458 return &VIsual;
18459 return &curwin->w_cursor;
18460 }
18461#endif
18462 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018464 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18466 return NULL;
18467 return pp;
18468 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018469
18470#ifdef FEAT_VIRTUALEDIT
18471 pos.coladd = 0;
18472#endif
18473
Bram Moolenaar477933c2007-07-17 14:32:23 +000018474 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018475 {
18476 pos.col = 0;
18477 if (name[1] == '0') /* "w0": first visible line */
18478 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018479 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018480 pos.lnum = curwin->w_topline;
18481 return &pos;
18482 }
18483 else if (name[1] == '$') /* "w$": last visible line */
18484 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018485 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018486 pos.lnum = curwin->w_botline - 1;
18487 return &pos;
18488 }
18489 }
18490 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018492 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 {
18494 pos.lnum = curbuf->b_ml.ml_line_count;
18495 pos.col = 0;
18496 }
18497 else
18498 {
18499 pos.lnum = curwin->w_cursor.lnum;
18500 pos.col = (colnr_T)STRLEN(ml_get_curline());
18501 }
18502 return &pos;
18503 }
18504 return NULL;
18505}
18506
18507/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018508 * Convert list in "arg" into a position and optional file number.
18509 * When "fnump" is NULL there is no file number, only 3 items.
18510 * Note that the column is passed on as-is, the caller may want to decrement
18511 * it to use 1 for the first column.
18512 * Return FAIL when conversion is not possible, doesn't check the position for
18513 * validity.
18514 */
18515 static int
18516list2fpos(arg, posp, fnump)
18517 typval_T *arg;
18518 pos_T *posp;
18519 int *fnump;
18520{
18521 list_T *l = arg->vval.v_list;
18522 long i = 0;
18523 long n;
18524
Bram Moolenaarbde35262006-07-23 20:12:24 +000018525 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18526 * when "fnump" isn't NULL and "coladd" is optional. */
18527 if (arg->v_type != VAR_LIST
18528 || l == NULL
18529 || l->lv_len < (fnump == NULL ? 2 : 3)
18530 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018531 return FAIL;
18532
18533 if (fnump != NULL)
18534 {
18535 n = list_find_nr(l, i++, NULL); /* fnum */
18536 if (n < 0)
18537 return FAIL;
18538 if (n == 0)
18539 n = curbuf->b_fnum; /* current buffer */
18540 *fnump = n;
18541 }
18542
18543 n = list_find_nr(l, i++, NULL); /* lnum */
18544 if (n < 0)
18545 return FAIL;
18546 posp->lnum = n;
18547
18548 n = list_find_nr(l, i++, NULL); /* col */
18549 if (n < 0)
18550 return FAIL;
18551 posp->col = n;
18552
18553#ifdef FEAT_VIRTUALEDIT
18554 n = list_find_nr(l, i, NULL);
18555 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018556 posp->coladd = 0;
18557 else
18558 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018559#endif
18560
18561 return OK;
18562}
18563
18564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 * Get the length of an environment variable name.
18566 * Advance "arg" to the first character after the name.
18567 * Return 0 for error.
18568 */
18569 static int
18570get_env_len(arg)
18571 char_u **arg;
18572{
18573 char_u *p;
18574 int len;
18575
18576 for (p = *arg; vim_isIDc(*p); ++p)
18577 ;
18578 if (p == *arg) /* no name found */
18579 return 0;
18580
18581 len = (int)(p - *arg);
18582 *arg = p;
18583 return len;
18584}
18585
18586/*
18587 * Get the length of the name of a function or internal variable.
18588 * "arg" is advanced to the first non-white character after the name.
18589 * Return 0 if something is wrong.
18590 */
18591 static int
18592get_id_len(arg)
18593 char_u **arg;
18594{
18595 char_u *p;
18596 int len;
18597
18598 /* Find the end of the name. */
18599 for (p = *arg; eval_isnamec(*p); ++p)
18600 ;
18601 if (p == *arg) /* no name found */
18602 return 0;
18603
18604 len = (int)(p - *arg);
18605 *arg = skipwhite(p);
18606
18607 return len;
18608}
18609
18610/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018611 * Get the length of the name of a variable or function.
18612 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018614 * Return -1 if curly braces expansion failed.
18615 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 * If the name contains 'magic' {}'s, expand them and return the
18617 * expanded name in an allocated string via 'alias' - caller must free.
18618 */
18619 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018620get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 char_u **arg;
18622 char_u **alias;
18623 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018624 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625{
18626 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 char_u *p;
18628 char_u *expr_start;
18629 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630
18631 *alias = NULL; /* default to no alias */
18632
18633 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18634 && (*arg)[2] == (int)KE_SNR)
18635 {
18636 /* hard coded <SNR>, already translated */
18637 *arg += 3;
18638 return get_id_len(arg) + 3;
18639 }
18640 len = eval_fname_script(*arg);
18641 if (len > 0)
18642 {
18643 /* literal "<SID>", "s:" or "<SNR>" */
18644 *arg += len;
18645 }
18646
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018648 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018650 p = find_name_end(*arg, &expr_start, &expr_end,
18651 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 if (expr_start != NULL)
18653 {
18654 char_u *temp_string;
18655
18656 if (!evaluate)
18657 {
18658 len += (int)(p - *arg);
18659 *arg = skipwhite(p);
18660 return len;
18661 }
18662
18663 /*
18664 * Include any <SID> etc in the expanded string:
18665 * Thus the -len here.
18666 */
18667 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18668 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018669 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 *alias = temp_string;
18671 *arg = skipwhite(p);
18672 return (int)STRLEN(temp_string);
18673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674
18675 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018676 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 EMSG2(_(e_invexpr2), *arg);
18678
18679 return len;
18680}
18681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682/*
18683 * Find the end of a variable or function name, taking care of magic braces.
18684 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18685 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018686 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687 * Return a pointer to just after the name. Equal to "arg" if there is no
18688 * valid name.
18689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018691find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 char_u *arg;
18693 char_u **expr_start;
18694 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018695 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 int mb_nest = 0;
18698 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 char_u *p;
18700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 *expr_start = NULL;
18704 *expr_end = NULL;
18705 }
18706
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018707 /* Quick check for valid starting character. */
18708 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18709 return arg;
18710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 for (p = arg; *p != NUL
18712 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018713 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018714 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018716 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018718 if (*p == '\'')
18719 {
18720 /* skip over 'string' to avoid counting [ and ] inside it. */
18721 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18722 ;
18723 if (*p == NUL)
18724 break;
18725 }
18726 else if (*p == '"')
18727 {
18728 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18729 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18730 if (*p == '\\' && p[1] != NUL)
18731 ++p;
18732 if (*p == NUL)
18733 break;
18734 }
18735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738 if (*p == '[')
18739 ++br_nest;
18740 else if (*p == ']')
18741 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746 if (*p == '{')
18747 {
18748 mb_nest++;
18749 if (expr_start != NULL && *expr_start == NULL)
18750 *expr_start = p;
18751 }
18752 else if (*p == '}')
18753 {
18754 mb_nest--;
18755 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18756 *expr_end = p;
18757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 }
18760
18761 return p;
18762}
18763
18764/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018765 * Expands out the 'magic' {}'s in a variable/function name.
18766 * Note that this can call itself recursively, to deal with
18767 * constructs like foo{bar}{baz}{bam}
18768 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18769 * "in_start" ^
18770 * "expr_start" ^
18771 * "expr_end" ^
18772 * "in_end" ^
18773 *
18774 * Returns a new allocated string, which the caller must free.
18775 * Returns NULL for failure.
18776 */
18777 static char_u *
18778make_expanded_name(in_start, expr_start, expr_end, in_end)
18779 char_u *in_start;
18780 char_u *expr_start;
18781 char_u *expr_end;
18782 char_u *in_end;
18783{
18784 char_u c1;
18785 char_u *retval = NULL;
18786 char_u *temp_result;
18787 char_u *nextcmd = NULL;
18788
18789 if (expr_end == NULL || in_end == NULL)
18790 return NULL;
18791 *expr_start = NUL;
18792 *expr_end = NUL;
18793 c1 = *in_end;
18794 *in_end = NUL;
18795
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018796 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018797 if (temp_result != NULL && nextcmd == NULL)
18798 {
18799 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18800 + (in_end - expr_end) + 1));
18801 if (retval != NULL)
18802 {
18803 STRCPY(retval, in_start);
18804 STRCAT(retval, temp_result);
18805 STRCAT(retval, expr_end + 1);
18806 }
18807 }
18808 vim_free(temp_result);
18809
18810 *in_end = c1; /* put char back for error messages */
18811 *expr_start = '{';
18812 *expr_end = '}';
18813
18814 if (retval != NULL)
18815 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018816 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018817 if (expr_start != NULL)
18818 {
18819 /* Further expansion! */
18820 temp_result = make_expanded_name(retval, expr_start,
18821 expr_end, temp_result);
18822 vim_free(retval);
18823 retval = temp_result;
18824 }
18825 }
18826
18827 return retval;
18828}
18829
18830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018832 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 */
18834 static int
18835eval_isnamec(c)
18836 int c;
18837{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018838 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18839}
18840
18841/*
18842 * Return TRUE if character "c" can be used as the first character in a
18843 * variable or function name (excluding '{' and '}').
18844 */
18845 static int
18846eval_isnamec1(c)
18847 int c;
18848{
18849 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850}
18851
18852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 * Set number v: variable to "val".
18854 */
18855 void
18856set_vim_var_nr(idx, val)
18857 int idx;
18858 long val;
18859{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018860 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861}
18862
18863/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018864 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 */
18866 long
18867get_vim_var_nr(idx)
18868 int idx;
18869{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018870 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871}
18872
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018873/*
18874 * Get string v: variable value. Uses a static buffer, can only be used once.
18875 */
18876 char_u *
18877get_vim_var_str(idx)
18878 int idx;
18879{
18880 return get_tv_string(&vimvars[idx].vv_tv);
18881}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018882
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018884 * Get List v: variable value. Caller must take care of reference count when
18885 * needed.
18886 */
18887 list_T *
18888get_vim_var_list(idx)
18889 int idx;
18890{
18891 return vimvars[idx].vv_list;
18892}
18893
18894/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018895 * Set v:char to character "c".
18896 */
18897 void
18898set_vim_var_char(c)
18899 int c;
18900{
18901#ifdef FEAT_MBYTE
18902 char_u buf[MB_MAXBYTES];
18903#else
18904 char_u buf[2];
18905#endif
18906
18907#ifdef FEAT_MBYTE
18908 if (has_mbyte)
18909 buf[(*mb_char2bytes)(c, buf)] = NUL;
18910 else
18911#endif
18912 {
18913 buf[0] = c;
18914 buf[1] = NUL;
18915 }
18916 set_vim_var_string(VV_CHAR, buf, -1);
18917}
18918
18919/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018920 * Set v:count to "count" and v:count1 to "count1".
18921 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 */
18923 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018924set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 long count;
18926 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018927 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018929 if (set_prevcount)
18930 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018931 vimvars[VV_COUNT].vv_nr = count;
18932 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933}
18934
18935/*
18936 * Set string v: variable to a copy of "val".
18937 */
18938 void
18939set_vim_var_string(idx, val, len)
18940 int idx;
18941 char_u *val;
18942 int len; /* length of "val" to use or -1 (whole string) */
18943{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018944 /* Need to do this (at least) once, since we can't initialize a union.
18945 * Will always be invoked when "v:progname" is set. */
18946 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18947
Bram Moolenaare9a41262005-01-15 22:18:47 +000018948 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018950 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018952 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018954 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955}
18956
18957/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018958 * Set List v: variable to "val".
18959 */
18960 void
18961set_vim_var_list(idx, val)
18962 int idx;
18963 list_T *val;
18964{
18965 list_unref(vimvars[idx].vv_list);
18966 vimvars[idx].vv_list = val;
18967 if (val != NULL)
18968 ++val->lv_refcount;
18969}
18970
18971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 * Set v:register if needed.
18973 */
18974 void
18975set_reg_var(c)
18976 int c;
18977{
18978 char_u regname;
18979
18980 if (c == 0 || c == ' ')
18981 regname = '"';
18982 else
18983 regname = c;
18984 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018985 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 set_vim_var_string(VV_REG, &regname, 1);
18987}
18988
18989/*
18990 * Get or set v:exception. If "oldval" == NULL, return the current value.
18991 * Otherwise, restore the value to "oldval" and return NULL.
18992 * Must always be called in pairs to save and restore v:exception! Does not
18993 * take care of memory allocations.
18994 */
18995 char_u *
18996v_exception(oldval)
18997 char_u *oldval;
18998{
18999 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019000 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001
Bram Moolenaare9a41262005-01-15 22:18:47 +000019002 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 return NULL;
19004}
19005
19006/*
19007 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19008 * Otherwise, restore the value to "oldval" and return NULL.
19009 * Must always be called in pairs to save and restore v:throwpoint! Does not
19010 * take care of memory allocations.
19011 */
19012 char_u *
19013v_throwpoint(oldval)
19014 char_u *oldval;
19015{
19016 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019017 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018
Bram Moolenaare9a41262005-01-15 22:18:47 +000019019 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 return NULL;
19021}
19022
19023#if defined(FEAT_AUTOCMD) || defined(PROTO)
19024/*
19025 * Set v:cmdarg.
19026 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19027 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19028 * Must always be called in pairs!
19029 */
19030 char_u *
19031set_cmdarg(eap, oldarg)
19032 exarg_T *eap;
19033 char_u *oldarg;
19034{
19035 char_u *oldval;
19036 char_u *newval;
19037 unsigned len;
19038
Bram Moolenaare9a41262005-01-15 22:18:47 +000019039 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019040 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019042 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019043 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019044 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 }
19046
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019047 if (eap->force_bin == FORCE_BIN)
19048 len = 6;
19049 else if (eap->force_bin == FORCE_NOBIN)
19050 len = 8;
19051 else
19052 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019053
19054 if (eap->read_edit)
19055 len += 7;
19056
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019057 if (eap->force_ff != 0)
19058 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19059# ifdef FEAT_MBYTE
19060 if (eap->force_enc != 0)
19061 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019062 if (eap->bad_char != 0)
19063 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019064# endif
19065
19066 newval = alloc(len + 1);
19067 if (newval == NULL)
19068 return NULL;
19069
19070 if (eap->force_bin == FORCE_BIN)
19071 sprintf((char *)newval, " ++bin");
19072 else if (eap->force_bin == FORCE_NOBIN)
19073 sprintf((char *)newval, " ++nobin");
19074 else
19075 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019076
19077 if (eap->read_edit)
19078 STRCAT(newval, " ++edit");
19079
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019080 if (eap->force_ff != 0)
19081 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19082 eap->cmd + eap->force_ff);
19083# ifdef FEAT_MBYTE
19084 if (eap->force_enc != 0)
19085 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19086 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019087 if (eap->bad_char == BAD_KEEP)
19088 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19089 else if (eap->bad_char == BAD_DROP)
19090 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19091 else if (eap->bad_char != 0)
19092 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019093# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019094 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019095 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096}
19097#endif
19098
19099/*
19100 * Get the value of internal variable "name".
19101 * Return OK or FAIL.
19102 */
19103 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019104get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 char_u *name;
19106 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019108 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109{
19110 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 typval_T *tv = NULL;
19112 typval_T atv;
19113 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115
19116 /* truncate the name, so that we can use strcmp() */
19117 cc = name[len];
19118 name[len] = NUL;
19119
19120 /*
19121 * Check for "b:changedtick".
19122 */
19123 if (STRCMP(name, "b:changedtick") == 0)
19124 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019125 atv.v_type = VAR_NUMBER;
19126 atv.vval.v_number = curbuf->b_changedtick;
19127 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 }
19129
19130 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 * Check for user-defined variables.
19132 */
19133 else
19134 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019135 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 }
19139
Bram Moolenaare9a41262005-01-15 22:18:47 +000019140 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019142 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 ret = FAIL;
19145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019147 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148
19149 name[len] = cc;
19150
19151 return ret;
19152}
19153
19154/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019155 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19156 * Also handle function call with Funcref variable: func(expr)
19157 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19158 */
19159 static int
19160handle_subscript(arg, rettv, evaluate, verbose)
19161 char_u **arg;
19162 typval_T *rettv;
19163 int evaluate; /* do more than finding the end */
19164 int verbose; /* give error messages */
19165{
19166 int ret = OK;
19167 dict_T *selfdict = NULL;
19168 char_u *s;
19169 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019170 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019171
19172 while (ret == OK
19173 && (**arg == '['
19174 || (**arg == '.' && rettv->v_type == VAR_DICT)
19175 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19176 && !vim_iswhite(*(*arg - 1)))
19177 {
19178 if (**arg == '(')
19179 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019180 /* need to copy the funcref so that we can clear rettv */
19181 functv = *rettv;
19182 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019183
19184 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019185 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019186 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019187 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19188 &len, evaluate, selfdict);
19189
19190 /* Clear the funcref afterwards, so that deleting it while
19191 * evaluating the arguments is possible (see test55). */
19192 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019193
19194 /* Stop the expression evaluation when immediately aborting on
19195 * error, or when an interrupt occurred or an exception was thrown
19196 * but not caught. */
19197 if (aborting())
19198 {
19199 if (ret == OK)
19200 clear_tv(rettv);
19201 ret = FAIL;
19202 }
19203 dict_unref(selfdict);
19204 selfdict = NULL;
19205 }
19206 else /* **arg == '[' || **arg == '.' */
19207 {
19208 dict_unref(selfdict);
19209 if (rettv->v_type == VAR_DICT)
19210 {
19211 selfdict = rettv->vval.v_dict;
19212 if (selfdict != NULL)
19213 ++selfdict->dv_refcount;
19214 }
19215 else
19216 selfdict = NULL;
19217 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19218 {
19219 clear_tv(rettv);
19220 ret = FAIL;
19221 }
19222 }
19223 }
19224 dict_unref(selfdict);
19225 return ret;
19226}
19227
19228/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019229 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019230 * value).
19231 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019233alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019234{
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019236}
19237
19238/*
19239 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 * The string "s" must have been allocated, it is consumed.
19241 * Return NULL for out of memory, the variable otherwise.
19242 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019243 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 char_u *s;
19246{
Bram Moolenaar33570922005-01-25 22:26:29 +000019247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019249 rettv = alloc_tv();
19250 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 rettv->v_type = VAR_STRING;
19253 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 }
19255 else
19256 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019257 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258}
19259
19260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019261 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019263 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019264free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019265 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266{
19267 if (varp != NULL)
19268 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019269 switch (varp->v_type)
19270 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019271 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019272 func_unref(varp->vval.v_string);
19273 /*FALLTHROUGH*/
19274 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019275 vim_free(varp->vval.v_string);
19276 break;
19277 case VAR_LIST:
19278 list_unref(varp->vval.v_list);
19279 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019280 case VAR_DICT:
19281 dict_unref(varp->vval.v_dict);
19282 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019283 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019284#ifdef FEAT_FLOAT
19285 case VAR_FLOAT:
19286#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019287 case VAR_UNKNOWN:
19288 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019289 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019290 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019291 break;
19292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 vim_free(varp);
19294 }
19295}
19296
19297/*
19298 * Free the memory for a variable value and set the value to NULL or 0.
19299 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019300 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019301clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303{
19304 if (varp != NULL)
19305 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019306 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019308 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019309 func_unref(varp->vval.v_string);
19310 /*FALLTHROUGH*/
19311 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019312 vim_free(varp->vval.v_string);
19313 varp->vval.v_string = NULL;
19314 break;
19315 case VAR_LIST:
19316 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019317 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019318 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019319 case VAR_DICT:
19320 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019321 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019322 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324 varp->vval.v_number = 0;
19325 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019326#ifdef FEAT_FLOAT
19327 case VAR_FLOAT:
19328 varp->vval.v_float = 0.0;
19329 break;
19330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 case VAR_UNKNOWN:
19332 break;
19333 default:
19334 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019336 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 }
19338}
19339
19340/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019341 * Set the value of a variable to NULL without freeing items.
19342 */
19343 static void
19344init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019345 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019346{
19347 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019349}
19350
19351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 * Get the number value of a variable.
19353 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019354 * For incompatible types, return 0.
19355 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19356 * caller of incompatible types: it sets *denote to TRUE if "denote"
19357 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 */
19359 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019360get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019361 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019363 int error = FALSE;
19364
19365 return get_tv_number_chk(varp, &error); /* return 0L on error */
19366}
19367
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019368 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019369get_tv_number_chk(varp, denote)
19370 typval_T *varp;
19371 int *denote;
19372{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019373 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019375 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019377 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019378 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019379#ifdef FEAT_FLOAT
19380 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019381 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019382 break;
19383#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019384 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019385 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019386 break;
19387 case VAR_STRING:
19388 if (varp->vval.v_string != NULL)
19389 vim_str2nr(varp->vval.v_string, NULL, NULL,
19390 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019391 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019392 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019393 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019394 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019395 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019396 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019397 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019398 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019399 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019402 if (denote == NULL) /* useful for values that must be unsigned */
19403 n = -1;
19404 else
19405 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019406 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407}
19408
19409/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019410 * Get the lnum from the first argument.
19411 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019412 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 */
19414 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019415get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019416 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417{
Bram Moolenaar33570922005-01-25 22:26:29 +000019418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 linenr_T lnum;
19420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019421 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 if (lnum == 0) /* no valid number, try using line() */
19423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019424 rettv.v_type = VAR_NUMBER;
19425 f_line(argvars, &rettv);
19426 lnum = rettv.vval.v_number;
19427 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 }
19429 return lnum;
19430}
19431
19432/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019433 * Get the lnum from the first argument.
19434 * Also accepts "$", then "buf" is used.
19435 * Returns 0 on error.
19436 */
19437 static linenr_T
19438get_tv_lnum_buf(argvars, buf)
19439 typval_T *argvars;
19440 buf_T *buf;
19441{
19442 if (argvars[0].v_type == VAR_STRING
19443 && argvars[0].vval.v_string != NULL
19444 && argvars[0].vval.v_string[0] == '$'
19445 && buf != NULL)
19446 return buf->b_ml.ml_line_count;
19447 return get_tv_number_chk(&argvars[0], NULL);
19448}
19449
19450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 * Get the string value of a variable.
19452 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019453 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19454 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 * If the String variable has never been set, return an empty string.
19456 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019457 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19458 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 */
19460 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019461get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463{
19464 static char_u mybuf[NUMBUFLEN];
19465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019466 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467}
19468
19469 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019470get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019471 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019472 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019474 char_u *res = get_tv_string_buf_chk(varp, buf);
19475
19476 return res != NULL ? res : (char_u *)"";
19477}
19478
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019479 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019480get_tv_string_chk(varp)
19481 typval_T *varp;
19482{
19483 static char_u mybuf[NUMBUFLEN];
19484
19485 return get_tv_string_buf_chk(varp, mybuf);
19486}
19487
19488 static char_u *
19489get_tv_string_buf_chk(varp, buf)
19490 typval_T *varp;
19491 char_u *buf;
19492{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019493 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019495 case VAR_NUMBER:
19496 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19497 return buf;
19498 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019499 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019500 break;
19501 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019502 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019503 break;
19504 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019505 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019507#ifdef FEAT_FLOAT
19508 case VAR_FLOAT:
19509 EMSG(_("E806: using Float as a String"));
19510 break;
19511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019512 case VAR_STRING:
19513 if (varp->vval.v_string != NULL)
19514 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019515 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019516 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019517 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019520 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521}
19522
19523/*
19524 * Find variable "name" in the list of variables.
19525 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019526 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019527 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019528 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019531find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019533 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537
Bram Moolenaara7043832005-01-21 11:56:39 +000019538 ht = find_var_ht(name, &varname);
19539 if (htp != NULL)
19540 *htp = ht;
19541 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019543 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544}
19545
19546/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019547 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019548 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019550 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019551find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019552 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019553 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019554 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019555{
Bram Moolenaar33570922005-01-25 22:26:29 +000019556 hashitem_T *hi;
19557
19558 if (*varname == NUL)
19559 {
19560 /* Must be something like "s:", otherwise "ht" would be NULL. */
19561 switch (varname[-2])
19562 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019563 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 case 'g': return &globvars_var;
19565 case 'v': return &vimvars_var;
19566 case 'b': return &curbuf->b_bufvar;
19567 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019568#ifdef FEAT_WINDOWS
19569 case 't': return &curtab->tp_winvar;
19570#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019571 case 'l': return current_funccal == NULL
19572 ? NULL : &current_funccal->l_vars_var;
19573 case 'a': return current_funccal == NULL
19574 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019575 }
19576 return NULL;
19577 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019578
19579 hi = hash_find(ht, varname);
19580 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019581 {
19582 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019583 * worked find the variable again. Don't auto-load a script if it was
19584 * loaded already, otherwise it would be loaded every time when
19585 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019586 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019587 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019588 hi = hash_find(ht, varname);
19589 if (HASHITEM_EMPTY(hi))
19590 return NULL;
19591 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019592 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019593}
19594
19595/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019597 * Set "varname" to the start of name without ':'.
19598 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019600find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 char_u *name;
19602 char_u **varname;
19603{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019604 hashitem_T *hi;
19605
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 if (name[1] != ':')
19607 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019608 /* The name must not start with a colon or #. */
19609 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 return NULL;
19611 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019612
19613 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019614 hi = hash_find(&compat_hashtab, name);
19615 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019616 return &compat_hashtab;
19617
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 return &globvarht; /* global variable */
19620 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 }
19622 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019623 if (*name == 'g') /* global variable */
19624 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019625 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19626 */
19627 if (vim_strchr(name + 2, ':') != NULL
19628 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019629 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019631 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019633 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019634#ifdef FEAT_WINDOWS
19635 if (*name == 't') /* tab page variable */
19636 return &curtab->tp_vars.dv_hashtab;
19637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 if (*name == 'v') /* v: variable */
19639 return &vimvarht;
19640 if (*name == 'a' && current_funccal != NULL) /* function argument */
19641 return &current_funccal->l_avars.dv_hashtab;
19642 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19643 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 if (*name == 's' /* script variable */
19645 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19646 return &SCRIPT_VARS(current_SID);
19647 return NULL;
19648}
19649
19650/*
19651 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019652 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 * Returns NULL when it doesn't exist.
19654 */
19655 char_u *
19656get_var_value(name)
19657 char_u *name;
19658{
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660
Bram Moolenaara7043832005-01-21 11:56:39 +000019661 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 if (v == NULL)
19663 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019664 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665}
19666
19667/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 * sourcing this script and when executing functions defined in the script.
19670 */
19671 void
19672new_script_vars(id)
19673 scid_T id;
19674{
Bram Moolenaara7043832005-01-21 11:56:39 +000019675 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019676 hashtab_T *ht;
19677 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019678
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19680 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019681 /* Re-allocating ga_data means that an ht_array pointing to
19682 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019683 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019684 for (i = 1; i <= ga_scripts.ga_len; ++i)
19685 {
19686 ht = &SCRIPT_VARS(i);
19687 if (ht->ht_mask == HT_INIT_SIZE - 1)
19688 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019689 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019690 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019691 }
19692
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 while (ga_scripts.ga_len < id)
19694 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019695 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019696 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 }
19700 }
19701}
19702
19703/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019704 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19705 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 */
19707 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019708init_var_dict(dict, dict_var)
19709 dict_T *dict;
19710 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711{
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019713 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019714 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 dict_var->di_tv.vval.v_dict = dict;
19716 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019717 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019718 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19719 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720}
19721
19722/*
19723 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019724 * Frees all allocated variables and the value they contain.
19725 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 */
19727 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019728vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 hashtab_T *ht;
19730{
19731 vars_clear_ext(ht, TRUE);
19732}
19733
19734/*
19735 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19736 */
19737 static void
19738vars_clear_ext(ht, free_val)
19739 hashtab_T *ht;
19740 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741{
Bram Moolenaara7043832005-01-21 11:56:39 +000019742 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 hashitem_T *hi;
19744 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019747 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019748 for (hi = ht->ht_array; todo > 0; ++hi)
19749 {
19750 if (!HASHITEM_EMPTY(hi))
19751 {
19752 --todo;
19753
Bram Moolenaar33570922005-01-25 22:26:29 +000019754 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019755 * ht_array might change then. hash_clear() takes care of it
19756 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 v = HI2DI(hi);
19758 if (free_val)
19759 clear_tv(&v->di_tv);
19760 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19761 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019762 }
19763 }
19764 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019765 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766}
19767
Bram Moolenaara7043832005-01-21 11:56:39 +000019768/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019769 * Delete a variable from hashtab "ht" at item "hi".
19770 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019773delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 hashtab_T *ht;
19775 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776{
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019778
19779 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 clear_tv(&di->di_tv);
19781 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782}
19783
19784/*
19785 * List the value of one internal variable.
19786 */
19787 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019788list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019789 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019791 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019793 char_u *tofree;
19794 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019797 current_copyID += COPYID_INC;
19798 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019800 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019801 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802}
19803
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019805list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 char_u *prefix;
19807 char_u *name;
19808 int type;
19809 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019810 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811{
Bram Moolenaar31859182007-08-14 20:41:13 +000019812 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19813 msg_start();
19814 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 if (name != NULL) /* "a:" vars don't have a name stored */
19816 msg_puts(name);
19817 msg_putchar(' ');
19818 msg_advance(22);
19819 if (type == VAR_NUMBER)
19820 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 else if (type == VAR_FUNC)
19822 msg_putchar('*');
19823 else if (type == VAR_LIST)
19824 {
19825 msg_putchar('[');
19826 if (*string == '[')
19827 ++string;
19828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019829 else if (type == VAR_DICT)
19830 {
19831 msg_putchar('{');
19832 if (*string == '{')
19833 ++string;
19834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 else
19836 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019837
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839
19840 if (type == VAR_FUNC)
19841 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019842 if (*first)
19843 {
19844 msg_clr_eos();
19845 *first = FALSE;
19846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847}
19848
19849/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019850 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 * If the variable already exists, the value is updated.
19852 * Otherwise the variable is created.
19853 */
19854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019857 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859{
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019864 ht = find_var_ht(name, &varname);
19865 if (ht == NULL || *varname == NUL)
19866 {
19867 EMSG2(_(e_illvar), name);
19868 return;
19869 }
19870 v = find_var_in_ht(ht, varname, TRUE);
19871
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019872 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19873 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019874
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019878 if (var_check_ro(v->di_flags, name)
19879 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 return;
19881 if (v->di_tv.v_type != tv->v_type
19882 && !((v->di_tv.v_type == VAR_STRING
19883 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019884 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019885 || tv->v_type == VAR_NUMBER))
19886#ifdef FEAT_FLOAT
19887 && !((v->di_tv.v_type == VAR_NUMBER
19888 || v->di_tv.v_type == VAR_FLOAT)
19889 && (tv->v_type == VAR_NUMBER
19890 || tv->v_type == VAR_FLOAT))
19891#endif
19892 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019893 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019894 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019895 return;
19896 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019897
19898 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019899 * Handle setting internal v: variables separately: we don't change
19900 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019901 */
19902 if (ht == &vimvarht)
19903 {
19904 if (v->di_tv.v_type == VAR_STRING)
19905 {
19906 vim_free(v->di_tv.vval.v_string);
19907 if (copy || tv->v_type != VAR_STRING)
19908 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19909 else
19910 {
19911 /* Take over the string to avoid an extra alloc/free. */
19912 v->di_tv.vval.v_string = tv->vval.v_string;
19913 tv->vval.v_string = NULL;
19914 }
19915 }
19916 else if (v->di_tv.v_type != VAR_NUMBER)
19917 EMSG2(_(e_intern2), "set_var()");
19918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019919 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019921 if (STRCMP(varname, "searchforward") == 0)
19922 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19923 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 return;
19925 }
19926
19927 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 }
19929 else /* add a new variable */
19930 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019931 /* Can't add "v:" variable. */
19932 if (ht == &vimvarht)
19933 {
19934 EMSG2(_(e_illvar), name);
19935 return;
19936 }
19937
Bram Moolenaar92124a32005-06-17 22:03:40 +000019938 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019939 if (!valid_varname(varname))
19940 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019941
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019942 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19943 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019944 if (v == NULL)
19945 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019949 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 return;
19951 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019952 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019954
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019955 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019957 else
19958 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019959 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019960 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963}
19964
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019965/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019966 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019967 * Also give an error message.
19968 */
19969 static int
19970var_check_ro(flags, name)
19971 int flags;
19972 char_u *name;
19973{
19974 if (flags & DI_FLAGS_RO)
19975 {
19976 EMSG2(_(e_readonlyvar), name);
19977 return TRUE;
19978 }
19979 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19980 {
19981 EMSG2(_(e_readonlysbx), name);
19982 return TRUE;
19983 }
19984 return FALSE;
19985}
19986
19987/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019988 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19989 * Also give an error message.
19990 */
19991 static int
19992var_check_fixed(flags, name)
19993 int flags;
19994 char_u *name;
19995{
19996 if (flags & DI_FLAGS_FIX)
19997 {
19998 EMSG2(_("E795: Cannot delete variable %s"), name);
19999 return TRUE;
20000 }
20001 return FALSE;
20002}
20003
20004/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020005 * Check if a funcref is assigned to a valid variable name.
20006 * Return TRUE and give an error if not.
20007 */
20008 static int
20009var_check_func_name(name, new_var)
20010 char_u *name; /* points to start of variable name */
20011 int new_var; /* TRUE when creating the variable */
20012{
20013 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20014 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20015 ? name[2] : name[0]))
20016 {
20017 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20018 name);
20019 return TRUE;
20020 }
20021 /* Don't allow hiding a function. When "v" is not NULL we might be
20022 * assigning another function to the same var, the type is checked
20023 * below. */
20024 if (new_var && function_exists(name))
20025 {
20026 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20027 name);
20028 return TRUE;
20029 }
20030 return FALSE;
20031}
20032
20033/*
20034 * Check if a variable name is valid.
20035 * Return FALSE and give an error if not.
20036 */
20037 static int
20038valid_varname(varname)
20039 char_u *varname;
20040{
20041 char_u *p;
20042
20043 for (p = varname; *p != NUL; ++p)
20044 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20045 && *p != AUTOLOAD_CHAR)
20046 {
20047 EMSG2(_(e_illvar), varname);
20048 return FALSE;
20049 }
20050 return TRUE;
20051}
20052
20053/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020054 * Return TRUE if typeval "tv" is set to be locked (immutable).
20055 * Also give an error message, using "name".
20056 */
20057 static int
20058tv_check_lock(lock, name)
20059 int lock;
20060 char_u *name;
20061{
20062 if (lock & VAR_LOCKED)
20063 {
20064 EMSG2(_("E741: Value is locked: %s"),
20065 name == NULL ? (char_u *)_("Unknown") : name);
20066 return TRUE;
20067 }
20068 if (lock & VAR_FIXED)
20069 {
20070 EMSG2(_("E742: Cannot change value of %s"),
20071 name == NULL ? (char_u *)_("Unknown") : name);
20072 return TRUE;
20073 }
20074 return FALSE;
20075}
20076
20077/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020079 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020080 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020081 * It is OK for "from" and "to" to point to the same item. This is used to
20082 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020083 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020084 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 typval_T *from;
20087 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020089 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020090 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091 switch (from->v_type)
20092 {
20093 case VAR_NUMBER:
20094 to->vval.v_number = from->vval.v_number;
20095 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020096#ifdef FEAT_FLOAT
20097 case VAR_FLOAT:
20098 to->vval.v_float = from->vval.v_float;
20099 break;
20100#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020101 case VAR_STRING:
20102 case VAR_FUNC:
20103 if (from->vval.v_string == NULL)
20104 to->vval.v_string = NULL;
20105 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020106 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020107 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020108 if (from->v_type == VAR_FUNC)
20109 func_ref(to->vval.v_string);
20110 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020111 break;
20112 case VAR_LIST:
20113 if (from->vval.v_list == NULL)
20114 to->vval.v_list = NULL;
20115 else
20116 {
20117 to->vval.v_list = from->vval.v_list;
20118 ++to->vval.v_list->lv_refcount;
20119 }
20120 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020121 case VAR_DICT:
20122 if (from->vval.v_dict == NULL)
20123 to->vval.v_dict = NULL;
20124 else
20125 {
20126 to->vval.v_dict = from->vval.v_dict;
20127 ++to->vval.v_dict->dv_refcount;
20128 }
20129 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020132 break;
20133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134}
20135
20136/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020137 * Make a copy of an item.
20138 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020139 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20140 * reference to an already copied list/dict can be used.
20141 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020142 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020143 static int
20144item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 typval_T *from;
20146 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020147 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020148 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020149{
20150 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020151 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020152
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020154 {
20155 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020156 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020157 }
20158 ++recurse;
20159
20160 switch (from->v_type)
20161 {
20162 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020163#ifdef FEAT_FLOAT
20164 case VAR_FLOAT:
20165#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020166 case VAR_STRING:
20167 case VAR_FUNC:
20168 copy_tv(from, to);
20169 break;
20170 case VAR_LIST:
20171 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020172 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020173 if (from->vval.v_list == NULL)
20174 to->vval.v_list = NULL;
20175 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20176 {
20177 /* use the copy made earlier */
20178 to->vval.v_list = from->vval.v_list->lv_copylist;
20179 ++to->vval.v_list->lv_refcount;
20180 }
20181 else
20182 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20183 if (to->vval.v_list == NULL)
20184 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020185 break;
20186 case VAR_DICT:
20187 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020188 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020189 if (from->vval.v_dict == NULL)
20190 to->vval.v_dict = NULL;
20191 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20192 {
20193 /* use the copy made earlier */
20194 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20195 ++to->vval.v_dict->dv_refcount;
20196 }
20197 else
20198 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20199 if (to->vval.v_dict == NULL)
20200 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020201 break;
20202 default:
20203 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020204 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020205 }
20206 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020207 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020208}
20209
20210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 * ":echo expr1 ..." print each argument separated with a space, add a
20212 * newline at the end.
20213 * ":echon expr1 ..." print each argument plain.
20214 */
20215 void
20216ex_echo(eap)
20217 exarg_T *eap;
20218{
20219 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020221 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 char_u *p;
20223 int needclr = TRUE;
20224 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020225 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226
20227 if (eap->skip)
20228 ++emsg_skip;
20229 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20230 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020231 /* If eval1() causes an error message the text from the command may
20232 * still need to be cleared. E.g., "echo 22,44". */
20233 need_clr_eos = needclr;
20234
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 {
20238 /*
20239 * Report the invalid expression unless the expression evaluation
20240 * has been cancelled due to an aborting error, an interrupt, or an
20241 * exception.
20242 */
20243 if (!aborting())
20244 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020245 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 break;
20247 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020248 need_clr_eos = FALSE;
20249
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 if (!eap->skip)
20251 {
20252 if (atstart)
20253 {
20254 atstart = FALSE;
20255 /* Call msg_start() after eval1(), evaluating the expression
20256 * may cause a message to appear. */
20257 if (eap->cmdidx == CMD_echo)
20258 msg_start();
20259 }
20260 else if (eap->cmdidx == CMD_echo)
20261 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020262 current_copyID += COPYID_INC;
20263 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020264 if (p != NULL)
20265 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020267 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020269 if (*p != TAB && needclr)
20270 {
20271 /* remove any text still there from the command */
20272 msg_clr_eos();
20273 needclr = FALSE;
20274 }
20275 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 }
20277 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020278 {
20279#ifdef FEAT_MBYTE
20280 if (has_mbyte)
20281 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020282 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020283
20284 (void)msg_outtrans_len_attr(p, i, echo_attr);
20285 p += i - 1;
20286 }
20287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020289 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020292 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020294 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 arg = skipwhite(arg);
20296 }
20297 eap->nextcmd = check_nextcmd(arg);
20298
20299 if (eap->skip)
20300 --emsg_skip;
20301 else
20302 {
20303 /* remove text that may still be there from the command */
20304 if (needclr)
20305 msg_clr_eos();
20306 if (eap->cmdidx == CMD_echo)
20307 msg_end();
20308 }
20309}
20310
20311/*
20312 * ":echohl {name}".
20313 */
20314 void
20315ex_echohl(eap)
20316 exarg_T *eap;
20317{
20318 int id;
20319
20320 id = syn_name2id(eap->arg);
20321 if (id == 0)
20322 echo_attr = 0;
20323 else
20324 echo_attr = syn_id2attr(id);
20325}
20326
20327/*
20328 * ":execute expr1 ..." execute the result of an expression.
20329 * ":echomsg expr1 ..." Print a message
20330 * ":echoerr expr1 ..." Print an error
20331 * Each gets spaces around each argument and a newline at the end for
20332 * echo commands
20333 */
20334 void
20335ex_execute(eap)
20336 exarg_T *eap;
20337{
20338 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020339 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 int ret = OK;
20341 char_u *p;
20342 garray_T ga;
20343 int len;
20344 int save_did_emsg;
20345
20346 ga_init2(&ga, 1, 80);
20347
20348 if (eap->skip)
20349 ++emsg_skip;
20350 while (*arg != NUL && *arg != '|' && *arg != '\n')
20351 {
20352 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 {
20355 /*
20356 * Report the invalid expression unless the expression evaluation
20357 * has been cancelled due to an aborting error, an interrupt, or an
20358 * exception.
20359 */
20360 if (!aborting())
20361 EMSG2(_(e_invexpr2), p);
20362 ret = FAIL;
20363 break;
20364 }
20365
20366 if (!eap->skip)
20367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 len = (int)STRLEN(p);
20370 if (ga_grow(&ga, len + 2) == FAIL)
20371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020372 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 ret = FAIL;
20374 break;
20375 }
20376 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 ga.ga_len += len;
20380 }
20381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 arg = skipwhite(arg);
20384 }
20385
20386 if (ret != FAIL && ga.ga_data != NULL)
20387 {
20388 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020391 out_flush();
20392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 else if (eap->cmdidx == CMD_echoerr)
20394 {
20395 /* We don't want to abort following commands, restore did_emsg. */
20396 save_did_emsg = did_emsg;
20397 EMSG((char_u *)ga.ga_data);
20398 if (!force_abort)
20399 did_emsg = save_did_emsg;
20400 }
20401 else if (eap->cmdidx == CMD_execute)
20402 do_cmdline((char_u *)ga.ga_data,
20403 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20404 }
20405
20406 ga_clear(&ga);
20407
20408 if (eap->skip)
20409 --emsg_skip;
20410
20411 eap->nextcmd = check_nextcmd(arg);
20412}
20413
20414/*
20415 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20416 * "arg" points to the "&" or '+' when called, to "option" when returning.
20417 * Returns NULL when no option name found. Otherwise pointer to the char
20418 * after the option name.
20419 */
20420 static char_u *
20421find_option_end(arg, opt_flags)
20422 char_u **arg;
20423 int *opt_flags;
20424{
20425 char_u *p = *arg;
20426
20427 ++p;
20428 if (*p == 'g' && p[1] == ':')
20429 {
20430 *opt_flags = OPT_GLOBAL;
20431 p += 2;
20432 }
20433 else if (*p == 'l' && p[1] == ':')
20434 {
20435 *opt_flags = OPT_LOCAL;
20436 p += 2;
20437 }
20438 else
20439 *opt_flags = 0;
20440
20441 if (!ASCII_ISALPHA(*p))
20442 return NULL;
20443 *arg = p;
20444
20445 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20446 p += 4; /* termcap option */
20447 else
20448 while (ASCII_ISALPHA(*p))
20449 ++p;
20450 return p;
20451}
20452
20453/*
20454 * ":function"
20455 */
20456 void
20457ex_function(eap)
20458 exarg_T *eap;
20459{
20460 char_u *theline;
20461 int j;
20462 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 char_u *name = NULL;
20465 char_u *p;
20466 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020467 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 garray_T newargs;
20469 garray_T newlines;
20470 int varargs = FALSE;
20471 int mustend = FALSE;
20472 int flags = 0;
20473 ufunc_T *fp;
20474 int indent;
20475 int nesting;
20476 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 dictitem_T *v;
20478 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020479 static int func_nr = 0; /* number for nameless function */
20480 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020481 hashtab_T *ht;
20482 int todo;
20483 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020484 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485
20486 /*
20487 * ":function" without argument: list functions.
20488 */
20489 if (ends_excmd(*eap->arg))
20490 {
20491 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020492 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020493 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020494 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020495 {
20496 if (!HASHITEM_EMPTY(hi))
20497 {
20498 --todo;
20499 fp = HI2UF(hi);
20500 if (!isdigit(*fp->uf_name))
20501 list_func_head(fp, FALSE);
20502 }
20503 }
20504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 eap->nextcmd = check_nextcmd(eap->arg);
20506 return;
20507 }
20508
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020509 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020510 * ":function /pat": list functions matching pattern.
20511 */
20512 if (*eap->arg == '/')
20513 {
20514 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20515 if (!eap->skip)
20516 {
20517 regmatch_T regmatch;
20518
20519 c = *p;
20520 *p = NUL;
20521 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20522 *p = c;
20523 if (regmatch.regprog != NULL)
20524 {
20525 regmatch.rm_ic = p_ic;
20526
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020527 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020528 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20529 {
20530 if (!HASHITEM_EMPTY(hi))
20531 {
20532 --todo;
20533 fp = HI2UF(hi);
20534 if (!isdigit(*fp->uf_name)
20535 && vim_regexec(&regmatch, fp->uf_name, 0))
20536 list_func_head(fp, FALSE);
20537 }
20538 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020539 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020540 }
20541 }
20542 if (*p == '/')
20543 ++p;
20544 eap->nextcmd = check_nextcmd(p);
20545 return;
20546 }
20547
20548 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020549 * Get the function name. There are these situations:
20550 * func normal function name
20551 * "name" == func, "fudi.fd_dict" == NULL
20552 * dict.func new dictionary entry
20553 * "name" == NULL, "fudi.fd_dict" set,
20554 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20555 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020556 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020557 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20558 * dict.func existing dict entry that's not a Funcref
20559 * "name" == NULL, "fudi.fd_dict" set,
20560 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020563 name = trans_function_name(&p, eap->skip, 0, &fudi);
20564 paren = (vim_strchr(p, '(') != NULL);
20565 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 {
20567 /*
20568 * Return on an invalid expression in braces, unless the expression
20569 * evaluation has been cancelled due to an aborting error, an
20570 * interrupt, or an exception.
20571 */
20572 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020574 if (!eap->skip && fudi.fd_newkey != NULL)
20575 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020576 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 else
20580 eap->skip = TRUE;
20581 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020582
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 /* An error in a function call during evaluation of an expression in magic
20584 * braces should not cause the function not to be defined. */
20585 saved_did_emsg = did_emsg;
20586 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587
20588 /*
20589 * ":function func" with only function name: list function.
20590 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020591 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
20593 if (!ends_excmd(*skipwhite(p)))
20594 {
20595 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020596 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 }
20598 eap->nextcmd = check_nextcmd(p);
20599 if (eap->nextcmd != NULL)
20600 *p = NUL;
20601 if (!eap->skip && !got_int)
20602 {
20603 fp = find_func(name);
20604 if (fp != NULL)
20605 {
20606 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020607 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020609 if (FUNCLINE(fp, j) == NULL)
20610 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 msg_putchar('\n');
20612 msg_outnum((long)(j + 1));
20613 if (j < 9)
20614 msg_putchar(' ');
20615 if (j < 99)
20616 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020617 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 out_flush(); /* show a line at a time */
20619 ui_breakcheck();
20620 }
20621 if (!got_int)
20622 {
20623 msg_putchar('\n');
20624 msg_puts((char_u *)" endfunction");
20625 }
20626 }
20627 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020628 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020630 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 }
20632
20633 /*
20634 * ":function name(arg1, arg2)" Define function.
20635 */
20636 p = skipwhite(p);
20637 if (*p != '(')
20638 {
20639 if (!eap->skip)
20640 {
20641 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020642 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 }
20644 /* attempt to continue by skipping some text */
20645 if (vim_strchr(p, '(') != NULL)
20646 p = vim_strchr(p, '(');
20647 }
20648 p = skipwhite(p + 1);
20649
20650 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20651 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20652
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020653 if (!eap->skip)
20654 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020655 /* Check the name of the function. Unless it's a dictionary function
20656 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020657 if (name != NULL)
20658 arg = name;
20659 else
20660 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020661 if (arg != NULL && (fudi.fd_di == NULL
20662 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020663 {
20664 if (*arg == K_SPECIAL)
20665 j = 3;
20666 else
20667 j = 0;
20668 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20669 : eval_isnamec(arg[j])))
20670 ++j;
20671 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020672 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020673 }
20674 }
20675
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 /*
20677 * Isolate the arguments: "arg1, arg2, ...)"
20678 */
20679 while (*p != ')')
20680 {
20681 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20682 {
20683 varargs = TRUE;
20684 p += 3;
20685 mustend = TRUE;
20686 }
20687 else
20688 {
20689 arg = p;
20690 while (ASCII_ISALNUM(*p) || *p == '_')
20691 ++p;
20692 if (arg == p || isdigit(*arg)
20693 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20694 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20695 {
20696 if (!eap->skip)
20697 EMSG2(_("E125: Illegal argument: %s"), arg);
20698 break;
20699 }
20700 if (ga_grow(&newargs, 1) == FAIL)
20701 goto erret;
20702 c = *p;
20703 *p = NUL;
20704 arg = vim_strsave(arg);
20705 if (arg == NULL)
20706 goto erret;
20707 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20708 *p = c;
20709 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 if (*p == ',')
20711 ++p;
20712 else
20713 mustend = TRUE;
20714 }
20715 p = skipwhite(p);
20716 if (mustend && *p != ')')
20717 {
20718 if (!eap->skip)
20719 EMSG2(_(e_invarg2), eap->arg);
20720 break;
20721 }
20722 }
20723 ++p; /* skip the ')' */
20724
Bram Moolenaare9a41262005-01-15 22:18:47 +000020725 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 for (;;)
20727 {
20728 p = skipwhite(p);
20729 if (STRNCMP(p, "range", 5) == 0)
20730 {
20731 flags |= FC_RANGE;
20732 p += 5;
20733 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734 else if (STRNCMP(p, "dict", 4) == 0)
20735 {
20736 flags |= FC_DICT;
20737 p += 4;
20738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 else if (STRNCMP(p, "abort", 5) == 0)
20740 {
20741 flags |= FC_ABORT;
20742 p += 5;
20743 }
20744 else
20745 break;
20746 }
20747
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020748 /* When there is a line break use what follows for the function body.
20749 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20750 if (*p == '\n')
20751 line_arg = p + 1;
20752 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 EMSG(_(e_trailing));
20754
20755 /*
20756 * Read the body of the function, until ":endfunction" is found.
20757 */
20758 if (KeyTyped)
20759 {
20760 /* Check if the function already exists, don't let the user type the
20761 * whole function before telling him it doesn't work! For a script we
20762 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 if (!eap->skip && !eap->forceit)
20764 {
20765 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20766 EMSG(_(e_funcdict));
20767 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020768 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020771 if (!eap->skip && did_emsg)
20772 goto erret;
20773
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 msg_putchar('\n'); /* don't overwrite the function name */
20775 cmdline_row = msg_row;
20776 }
20777
20778 indent = 2;
20779 nesting = 0;
20780 for (;;)
20781 {
20782 msg_scroll = TRUE;
20783 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020784 sourcing_lnum_off = sourcing_lnum;
20785
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020786 if (line_arg != NULL)
20787 {
20788 /* Use eap->arg, split up in parts by line breaks. */
20789 theline = line_arg;
20790 p = vim_strchr(theline, '\n');
20791 if (p == NULL)
20792 line_arg += STRLEN(line_arg);
20793 else
20794 {
20795 *p = NUL;
20796 line_arg = p + 1;
20797 }
20798 }
20799 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 theline = getcmdline(':', 0L, indent);
20801 else
20802 theline = eap->getline(':', eap->cookie, indent);
20803 if (KeyTyped)
20804 lines_left = Rows - 1;
20805 if (theline == NULL)
20806 {
20807 EMSG(_("E126: Missing :endfunction"));
20808 goto erret;
20809 }
20810
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020811 /* Detect line continuation: sourcing_lnum increased more than one. */
20812 if (sourcing_lnum > sourcing_lnum_off + 1)
20813 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20814 else
20815 sourcing_lnum_off = 0;
20816
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 if (skip_until != NULL)
20818 {
20819 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20820 * don't check for ":endfunc". */
20821 if (STRCMP(theline, skip_until) == 0)
20822 {
20823 vim_free(skip_until);
20824 skip_until = NULL;
20825 }
20826 }
20827 else
20828 {
20829 /* skip ':' and blanks*/
20830 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20831 ;
20832
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020833 /* Check for "endfunction". */
20834 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020836 if (line_arg == NULL)
20837 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 break;
20839 }
20840
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020841 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 * at "end". */
20843 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20844 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020845 else if (STRNCMP(p, "if", 2) == 0
20846 || STRNCMP(p, "wh", 2) == 0
20847 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 || STRNCMP(p, "try", 3) == 0)
20849 indent += 2;
20850
20851 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020852 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020854 if (*p == '!')
20855 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 p += eval_fname_script(p);
20857 if (ASCII_ISALPHA(*p))
20858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020859 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 if (*skipwhite(p) == '(')
20861 {
20862 ++nesting;
20863 indent += 2;
20864 }
20865 }
20866 }
20867
20868 /* Check for ":append" or ":insert". */
20869 p = skip_range(p, NULL);
20870 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20871 || (p[0] == 'i'
20872 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20873 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20874 skip_until = vim_strsave((char_u *)".");
20875
20876 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20877 arg = skipwhite(skiptowhite(p));
20878 if (arg[0] == '<' && arg[1] =='<'
20879 && ((p[0] == 'p' && p[1] == 'y'
20880 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20881 || (p[0] == 'p' && p[1] == 'e'
20882 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20883 || (p[0] == 't' && p[1] == 'c'
20884 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20885 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20886 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020887 || (p[0] == 'm' && p[1] == 'z'
20888 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 ))
20890 {
20891 /* ":python <<" continues until a dot, like ":append" */
20892 p = skipwhite(arg + 2);
20893 if (*p == NUL)
20894 skip_until = vim_strsave((char_u *)".");
20895 else
20896 skip_until = vim_strsave(p);
20897 }
20898 }
20899
20900 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020901 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020902 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020903 if (line_arg == NULL)
20904 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020906 }
20907
20908 /* Copy the line to newly allocated memory. get_one_sourceline()
20909 * allocates 250 bytes per line, this saves 80% on average. The cost
20910 * is an extra alloc/free. */
20911 p = vim_strsave(theline);
20912 if (p != NULL)
20913 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020914 if (line_arg == NULL)
20915 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020916 theline = p;
20917 }
20918
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020919 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20920
20921 /* Add NULL lines for continuation lines, so that the line count is
20922 * equal to the index in the growarray. */
20923 while (sourcing_lnum_off-- > 0)
20924 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020925
20926 /* Check for end of eap->arg. */
20927 if (line_arg != NULL && *line_arg == NUL)
20928 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 }
20930
20931 /* Don't define the function when skipping commands or when an error was
20932 * detected. */
20933 if (eap->skip || did_emsg)
20934 goto erret;
20935
20936 /*
20937 * If there are no errors, add the function
20938 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020940 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020941 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020942 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020944 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020945 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020946 goto erret;
20947 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020948
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020949 fp = find_func(name);
20950 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 if (!eap->forceit)
20953 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020954 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020955 goto erret;
20956 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020957 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020958 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020959 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020960 name);
20961 goto erret;
20962 }
20963 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 ga_clear_strings(&(fp->uf_args));
20965 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020966 vim_free(name);
20967 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 }
20970 else
20971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020972 char numbuf[20];
20973
20974 fp = NULL;
20975 if (fudi.fd_newkey == NULL && !eap->forceit)
20976 {
20977 EMSG(_(e_funcdict));
20978 goto erret;
20979 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020980 if (fudi.fd_di == NULL)
20981 {
20982 /* Can't add a function to a locked dictionary */
20983 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20984 goto erret;
20985 }
20986 /* Can't change an existing function if it is locked */
20987 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20988 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020989
20990 /* Give the function a sequential number. Can only be used with a
20991 * Funcref! */
20992 vim_free(name);
20993 sprintf(numbuf, "%d", ++func_nr);
20994 name = vim_strsave((char_u *)numbuf);
20995 if (name == NULL)
20996 goto erret;
20997 }
20998
20999 if (fp == NULL)
21000 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021001 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021002 {
21003 int slen, plen;
21004 char_u *scriptname;
21005
21006 /* Check that the autoload name matches the script name. */
21007 j = FAIL;
21008 if (sourcing_name != NULL)
21009 {
21010 scriptname = autoload_name(name);
21011 if (scriptname != NULL)
21012 {
21013 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021014 plen = (int)STRLEN(p);
21015 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021016 if (slen > plen && fnamecmp(p,
21017 sourcing_name + slen - plen) == 0)
21018 j = OK;
21019 vim_free(scriptname);
21020 }
21021 }
21022 if (j == FAIL)
21023 {
21024 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21025 goto erret;
21026 }
21027 }
21028
21029 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 if (fp == NULL)
21031 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021032
21033 if (fudi.fd_dict != NULL)
21034 {
21035 if (fudi.fd_di == NULL)
21036 {
21037 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021038 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021039 if (fudi.fd_di == NULL)
21040 {
21041 vim_free(fp);
21042 goto erret;
21043 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021044 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21045 {
21046 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021047 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021048 goto erret;
21049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 }
21051 else
21052 /* overwrite existing dict entry */
21053 clear_tv(&fudi.fd_di->di_tv);
21054 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021055 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021056 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021057 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021058
21059 /* behave like "dict" was used */
21060 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021061 }
21062
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021064 STRCPY(fp->uf_name, name);
21065 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021067 fp->uf_args = newargs;
21068 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021069#ifdef FEAT_PROFILE
21070 fp->uf_tml_count = NULL;
21071 fp->uf_tml_total = NULL;
21072 fp->uf_tml_self = NULL;
21073 fp->uf_profiling = FALSE;
21074 if (prof_def_func())
21075 func_do_profile(fp);
21076#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021077 fp->uf_varargs = varargs;
21078 fp->uf_flags = flags;
21079 fp->uf_calls = 0;
21080 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021081 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082
21083erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 ga_clear_strings(&newargs);
21085 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021086ret_free:
21087 vim_free(skip_until);
21088 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091}
21092
21093/*
21094 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021095 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021097 * flags:
21098 * TFN_INT: internal function name OK
21099 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 * Advances "pp" to just after the function name (if no error).
21101 */
21102 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021103trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 char_u **pp;
21105 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021106 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021107 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021109 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 char_u *start;
21111 char_u *end;
21112 int lead;
21113 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021115 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021116
21117 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021118 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021120
21121 /* Check for hard coded <SNR>: already translated function ID (from a user
21122 * command). */
21123 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21124 && (*pp)[2] == (int)KE_SNR)
21125 {
21126 *pp += 3;
21127 len = get_id_len(pp) + 3;
21128 return vim_strnsave(start, len);
21129 }
21130
21131 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21132 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021134 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021136
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021137 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21138 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021139 if (end == start)
21140 {
21141 if (!skip)
21142 EMSG(_("E129: Function name required"));
21143 goto theend;
21144 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021145 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021146 {
21147 /*
21148 * Report an invalid expression in braces, unless the expression
21149 * evaluation has been cancelled due to an aborting error, an
21150 * interrupt, or an exception.
21151 */
21152 if (!aborting())
21153 {
21154 if (end != NULL)
21155 EMSG2(_(e_invarg2), start);
21156 }
21157 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021158 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021159 goto theend;
21160 }
21161
21162 if (lv.ll_tv != NULL)
21163 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021164 if (fdp != NULL)
21165 {
21166 fdp->fd_dict = lv.ll_dict;
21167 fdp->fd_newkey = lv.ll_newkey;
21168 lv.ll_newkey = NULL;
21169 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021170 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021171 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21172 {
21173 name = vim_strsave(lv.ll_tv->vval.v_string);
21174 *pp = end;
21175 }
21176 else
21177 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021178 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21179 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021180 EMSG(_(e_funcref));
21181 else
21182 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021183 name = NULL;
21184 }
21185 goto theend;
21186 }
21187
21188 if (lv.ll_name == NULL)
21189 {
21190 /* Error found, but continue after the function name. */
21191 *pp = end;
21192 goto theend;
21193 }
21194
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021195 /* Check if the name is a Funcref. If so, use the value. */
21196 if (lv.ll_exp_name != NULL)
21197 {
21198 len = (int)STRLEN(lv.ll_exp_name);
21199 name = deref_func_name(lv.ll_exp_name, &len);
21200 if (name == lv.ll_exp_name)
21201 name = NULL;
21202 }
21203 else
21204 {
21205 len = (int)(end - *pp);
21206 name = deref_func_name(*pp, &len);
21207 if (name == *pp)
21208 name = NULL;
21209 }
21210 if (name != NULL)
21211 {
21212 name = vim_strsave(name);
21213 *pp = end;
21214 goto theend;
21215 }
21216
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021217 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021218 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021219 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021220 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21221 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21222 {
21223 /* When there was "s:" already or the name expanded to get a
21224 * leading "s:" then remove it. */
21225 lv.ll_name += 2;
21226 len -= 2;
21227 lead = 2;
21228 }
21229 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021230 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021231 {
21232 if (lead == 2) /* skip over "s:" */
21233 lv.ll_name += 2;
21234 len = (int)(end - lv.ll_name);
21235 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021236
21237 /*
21238 * Copy the function name to allocated memory.
21239 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21240 * Accept <SNR>123_name() outside a script.
21241 */
21242 if (skip)
21243 lead = 0; /* do nothing */
21244 else if (lead > 0)
21245 {
21246 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021247 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21248 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021249 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021250 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021251 if (current_SID <= 0)
21252 {
21253 EMSG(_(e_usingsid));
21254 goto theend;
21255 }
21256 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21257 lead += (int)STRLEN(sid_buf);
21258 }
21259 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021260 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021261 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021262 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021263 goto theend;
21264 }
21265 name = alloc((unsigned)(len + lead + 1));
21266 if (name != NULL)
21267 {
21268 if (lead > 0)
21269 {
21270 name[0] = K_SPECIAL;
21271 name[1] = KS_EXTRA;
21272 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021273 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021274 STRCPY(name + 3, sid_buf);
21275 }
21276 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21277 name[len + lead] = NUL;
21278 }
21279 *pp = end;
21280
21281theend:
21282 clear_lval(&lv);
21283 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284}
21285
21286/*
21287 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21288 * Return 2 if "p" starts with "s:".
21289 * Return 0 otherwise.
21290 */
21291 static int
21292eval_fname_script(p)
21293 char_u *p;
21294{
21295 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21296 || STRNICMP(p + 1, "SNR>", 4) == 0))
21297 return 5;
21298 if (p[0] == 's' && p[1] == ':')
21299 return 2;
21300 return 0;
21301}
21302
21303/*
21304 * Return TRUE if "p" starts with "<SID>" or "s:".
21305 * Only works if eval_fname_script() returned non-zero for "p"!
21306 */
21307 static int
21308eval_fname_sid(p)
21309 char_u *p;
21310{
21311 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21312}
21313
21314/*
21315 * List the head of the function: "name(arg1, arg2)".
21316 */
21317 static void
21318list_func_head(fp, indent)
21319 ufunc_T *fp;
21320 int indent;
21321{
21322 int j;
21323
21324 msg_start();
21325 if (indent)
21326 MSG_PUTS(" ");
21327 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021328 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 {
21330 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021331 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 }
21333 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021334 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021336 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 {
21338 if (j)
21339 MSG_PUTS(", ");
21340 msg_puts(FUNCARG(fp, j));
21341 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021342 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 {
21344 if (j)
21345 MSG_PUTS(", ");
21346 MSG_PUTS("...");
21347 }
21348 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021349 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021350 if (p_verbose > 0)
21351 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352}
21353
21354/*
21355 * Find a function by name, return pointer to it in ufuncs.
21356 * Return NULL for unknown function.
21357 */
21358 static ufunc_T *
21359find_func(name)
21360 char_u *name;
21361{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021362 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021364 hi = hash_find(&func_hashtab, name);
21365 if (!HASHITEM_EMPTY(hi))
21366 return HI2UF(hi);
21367 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368}
21369
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021370#if defined(EXITFREE) || defined(PROTO)
21371 void
21372free_all_functions()
21373{
21374 hashitem_T *hi;
21375
21376 /* Need to start all over every time, because func_free() may change the
21377 * hash table. */
21378 while (func_hashtab.ht_used > 0)
21379 for (hi = func_hashtab.ht_array; ; ++hi)
21380 if (!HASHITEM_EMPTY(hi))
21381 {
21382 func_free(HI2UF(hi));
21383 break;
21384 }
21385}
21386#endif
21387
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021388/*
21389 * Return TRUE if a function "name" exists.
21390 */
21391 static int
21392function_exists(name)
21393 char_u *name;
21394{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021395 char_u *nm = name;
21396 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021397 int n = FALSE;
21398
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021399 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021400 nm = skipwhite(nm);
21401
21402 /* Only accept "funcname", "funcname ", "funcname (..." and
21403 * "funcname(...", not "funcname!...". */
21404 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021405 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021406 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021407 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021408 else
21409 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021410 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021411 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021412 return n;
21413}
21414
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021415/*
21416 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021417 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021418 */
21419 static int
21420builtin_function(name)
21421 char_u *name;
21422{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021423 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21424 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021425}
21426
Bram Moolenaar05159a02005-02-26 23:04:13 +000021427#if defined(FEAT_PROFILE) || defined(PROTO)
21428/*
21429 * Start profiling function "fp".
21430 */
21431 static void
21432func_do_profile(fp)
21433 ufunc_T *fp;
21434{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021435 int len = fp->uf_lines.ga_len;
21436
21437 if (len == 0)
21438 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021439 fp->uf_tm_count = 0;
21440 profile_zero(&fp->uf_tm_self);
21441 profile_zero(&fp->uf_tm_total);
21442 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021443 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021444 if (fp->uf_tml_total == NULL)
21445 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021446 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021447 if (fp->uf_tml_self == NULL)
21448 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021449 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021450 fp->uf_tml_idx = -1;
21451 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21452 || fp->uf_tml_self == NULL)
21453 return; /* out of memory */
21454
21455 fp->uf_profiling = TRUE;
21456}
21457
21458/*
21459 * Dump the profiling results for all functions in file "fd".
21460 */
21461 void
21462func_dump_profile(fd)
21463 FILE *fd;
21464{
21465 hashitem_T *hi;
21466 int todo;
21467 ufunc_T *fp;
21468 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021469 ufunc_T **sorttab;
21470 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021472 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021473 if (todo == 0)
21474 return; /* nothing to dump */
21475
Bram Moolenaar73830342005-02-28 22:48:19 +000021476 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21477
Bram Moolenaar05159a02005-02-26 23:04:13 +000021478 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21479 {
21480 if (!HASHITEM_EMPTY(hi))
21481 {
21482 --todo;
21483 fp = HI2UF(hi);
21484 if (fp->uf_profiling)
21485 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021486 if (sorttab != NULL)
21487 sorttab[st_len++] = fp;
21488
Bram Moolenaar05159a02005-02-26 23:04:13 +000021489 if (fp->uf_name[0] == K_SPECIAL)
21490 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21491 else
21492 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21493 if (fp->uf_tm_count == 1)
21494 fprintf(fd, "Called 1 time\n");
21495 else
21496 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21497 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21498 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21499 fprintf(fd, "\n");
21500 fprintf(fd, "count total (s) self (s)\n");
21501
21502 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21503 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021504 if (FUNCLINE(fp, i) == NULL)
21505 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021506 prof_func_line(fd, fp->uf_tml_count[i],
21507 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021508 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21509 }
21510 fprintf(fd, "\n");
21511 }
21512 }
21513 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021514
21515 if (sorttab != NULL && st_len > 0)
21516 {
21517 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21518 prof_total_cmp);
21519 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21520 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21521 prof_self_cmp);
21522 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21523 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021524
21525 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021526}
Bram Moolenaar73830342005-02-28 22:48:19 +000021527
21528 static void
21529prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21530 FILE *fd;
21531 ufunc_T **sorttab;
21532 int st_len;
21533 char *title;
21534 int prefer_self; /* when equal print only self time */
21535{
21536 int i;
21537 ufunc_T *fp;
21538
21539 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21540 fprintf(fd, "count total (s) self (s) function\n");
21541 for (i = 0; i < 20 && i < st_len; ++i)
21542 {
21543 fp = sorttab[i];
21544 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21545 prefer_self);
21546 if (fp->uf_name[0] == K_SPECIAL)
21547 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21548 else
21549 fprintf(fd, " %s()\n", fp->uf_name);
21550 }
21551 fprintf(fd, "\n");
21552}
21553
21554/*
21555 * Print the count and times for one function or function line.
21556 */
21557 static void
21558prof_func_line(fd, count, total, self, prefer_self)
21559 FILE *fd;
21560 int count;
21561 proftime_T *total;
21562 proftime_T *self;
21563 int prefer_self; /* when equal print only self time */
21564{
21565 if (count > 0)
21566 {
21567 fprintf(fd, "%5d ", count);
21568 if (prefer_self && profile_equal(total, self))
21569 fprintf(fd, " ");
21570 else
21571 fprintf(fd, "%s ", profile_msg(total));
21572 if (!prefer_self && profile_equal(total, self))
21573 fprintf(fd, " ");
21574 else
21575 fprintf(fd, "%s ", profile_msg(self));
21576 }
21577 else
21578 fprintf(fd, " ");
21579}
21580
21581/*
21582 * Compare function for total time sorting.
21583 */
21584 static int
21585#ifdef __BORLANDC__
21586_RTLENTRYF
21587#endif
21588prof_total_cmp(s1, s2)
21589 const void *s1;
21590 const void *s2;
21591{
21592 ufunc_T *p1, *p2;
21593
21594 p1 = *(ufunc_T **)s1;
21595 p2 = *(ufunc_T **)s2;
21596 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21597}
21598
21599/*
21600 * Compare function for self time sorting.
21601 */
21602 static int
21603#ifdef __BORLANDC__
21604_RTLENTRYF
21605#endif
21606prof_self_cmp(s1, s2)
21607 const void *s1;
21608 const void *s2;
21609{
21610 ufunc_T *p1, *p2;
21611
21612 p1 = *(ufunc_T **)s1;
21613 p2 = *(ufunc_T **)s2;
21614 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21615}
21616
Bram Moolenaar05159a02005-02-26 23:04:13 +000021617#endif
21618
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021619/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021620 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021621 * Return TRUE if a package was loaded.
21622 */
21623 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021624script_autoload(name, reload)
21625 char_u *name;
21626 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021627{
21628 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021629 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021630 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021631 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021632
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021633 /* Return quickly when autoload disabled. */
21634 if (no_autoload)
21635 return FALSE;
21636
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021637 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021638 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021639 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021640 return FALSE;
21641
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021642 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021643
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021644 /* Find the name in the list of previously loaded package names. Skip
21645 * "autoload/", it's always the same. */
21646 for (i = 0; i < ga_loaded.ga_len; ++i)
21647 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21648 break;
21649 if (!reload && i < ga_loaded.ga_len)
21650 ret = FALSE; /* was loaded already */
21651 else
21652 {
21653 /* Remember the name if it wasn't loaded already. */
21654 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21655 {
21656 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21657 tofree = NULL;
21658 }
21659
21660 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021661 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021662 ret = TRUE;
21663 }
21664
21665 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021666 return ret;
21667}
21668
21669/*
21670 * Return the autoload script name for a function or variable name.
21671 * Returns NULL when out of memory.
21672 */
21673 static char_u *
21674autoload_name(name)
21675 char_u *name;
21676{
21677 char_u *p;
21678 char_u *scriptname;
21679
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021680 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021681 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21682 if (scriptname == NULL)
21683 return FALSE;
21684 STRCPY(scriptname, "autoload/");
21685 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021686 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021687 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021688 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021689 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021690 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021691}
21692
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21694
21695/*
21696 * Function given to ExpandGeneric() to obtain the list of user defined
21697 * function names.
21698 */
21699 char_u *
21700get_user_func_name(xp, idx)
21701 expand_T *xp;
21702 int idx;
21703{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021704 static long_u done;
21705 static hashitem_T *hi;
21706 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707
21708 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 done = 0;
21711 hi = func_hashtab.ht_array;
21712 }
21713 if (done < func_hashtab.ht_used)
21714 {
21715 if (done++ > 0)
21716 ++hi;
21717 while (HASHITEM_EMPTY(hi))
21718 ++hi;
21719 fp = HI2UF(hi);
21720
21721 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21722 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723
21724 cat_func_name(IObuff, fp);
21725 if (xp->xp_context != EXPAND_USER_FUNC)
21726 {
21727 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021728 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 STRCAT(IObuff, ")");
21730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 return IObuff;
21732 }
21733 return NULL;
21734}
21735
21736#endif /* FEAT_CMDL_COMPL */
21737
21738/*
21739 * Copy the function name of "fp" to buffer "buf".
21740 * "buf" must be able to hold the function name plus three bytes.
21741 * Takes care of script-local function names.
21742 */
21743 static void
21744cat_func_name(buf, fp)
21745 char_u *buf;
21746 ufunc_T *fp;
21747{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021748 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 {
21750 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021751 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 }
21753 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021754 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755}
21756
21757/*
21758 * ":delfunction {name}"
21759 */
21760 void
21761ex_delfunction(eap)
21762 exarg_T *eap;
21763{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021764 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 char_u *p;
21766 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768
21769 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021770 name = trans_function_name(&p, eap->skip, 0, &fudi);
21771 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021773 {
21774 if (fudi.fd_dict != NULL && !eap->skip)
21775 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 if (!ends_excmd(*skipwhite(p)))
21779 {
21780 vim_free(name);
21781 EMSG(_(e_trailing));
21782 return;
21783 }
21784 eap->nextcmd = check_nextcmd(p);
21785 if (eap->nextcmd != NULL)
21786 *p = NUL;
21787
21788 if (!eap->skip)
21789 fp = find_func(name);
21790 vim_free(name);
21791
21792 if (!eap->skip)
21793 {
21794 if (fp == NULL)
21795 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021796 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 return;
21798 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021799 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 {
21801 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21802 return;
21803 }
21804
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021805 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021807 /* Delete the dict item that refers to the function, it will
21808 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021809 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021811 else
21812 func_free(fp);
21813 }
21814}
21815
21816/*
21817 * Free a function and remove it from the list of functions.
21818 */
21819 static void
21820func_free(fp)
21821 ufunc_T *fp;
21822{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021823 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021824
21825 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021826 ga_clear_strings(&(fp->uf_args));
21827 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021828#ifdef FEAT_PROFILE
21829 vim_free(fp->uf_tml_count);
21830 vim_free(fp->uf_tml_total);
21831 vim_free(fp->uf_tml_self);
21832#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021833
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021834 /* remove the function from the function hashtable */
21835 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21836 if (HASHITEM_EMPTY(hi))
21837 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021838 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021839 hash_remove(&func_hashtab, hi);
21840
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021841 vim_free(fp);
21842}
21843
21844/*
21845 * Unreference a Function: decrement the reference count and free it when it
21846 * becomes zero. Only for numbered functions.
21847 */
21848 static void
21849func_unref(name)
21850 char_u *name;
21851{
21852 ufunc_T *fp;
21853
21854 if (name != NULL && isdigit(*name))
21855 {
21856 fp = find_func(name);
21857 if (fp == NULL)
21858 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021859 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021860 {
21861 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021862 * when "uf_calls" becomes zero. */
21863 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021864 func_free(fp);
21865 }
21866 }
21867}
21868
21869/*
21870 * Count a reference to a Function.
21871 */
21872 static void
21873func_ref(name)
21874 char_u *name;
21875{
21876 ufunc_T *fp;
21877
21878 if (name != NULL && isdigit(*name))
21879 {
21880 fp = find_func(name);
21881 if (fp == NULL)
21882 EMSG2(_(e_intern2), "func_ref()");
21883 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021884 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 }
21886}
21887
21888/*
21889 * Call a user function.
21890 */
21891 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021892call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 ufunc_T *fp; /* pointer to function */
21894 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 typval_T *argvars; /* arguments */
21896 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 linenr_T firstline; /* first line of range */
21898 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900{
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 char_u *save_sourcing_name;
21902 linenr_T save_sourcing_lnum;
21903 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021904 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021905 int save_did_emsg;
21906 static int depth = 0;
21907 dictitem_T *v;
21908 int fixvar_idx = 0; /* index in fixvar[] */
21909 int i;
21910 int ai;
21911 char_u numbuf[NUMBUFLEN];
21912 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021913#ifdef FEAT_PROFILE
21914 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021915 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917
21918 /* If depth of calling is getting too high, don't execute the function */
21919 if (depth >= p_mfd)
21920 {
21921 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021922 rettv->v_type = VAR_NUMBER;
21923 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 return;
21925 }
21926 ++depth;
21927
21928 line_breakcheck(); /* check for CTRL-C hit */
21929
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021930 fc = (funccall_T *)alloc(sizeof(funccall_T));
21931 fc->caller = current_funccal;
21932 current_funccal = fc;
21933 fc->func = fp;
21934 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021936 fc->linenr = 0;
21937 fc->returned = FALSE;
21938 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021940 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21941 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942
Bram Moolenaar33570922005-01-25 22:26:29 +000021943 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021944 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21946 * each argument variable and saves a lot of time.
21947 */
21948 /*
21949 * Init l: variables.
21950 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021951 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021952 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021953 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021954 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21955 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021956 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021957 name = v->di_key;
21958 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021960 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021961 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021962 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 v->di_tv.vval.v_dict = selfdict;
21964 ++selfdict->dv_refcount;
21965 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 /*
21968 * Init a: variables.
21969 * Set a:0 to "argcount".
21970 * Set a:000 to a list with room for the "..." arguments.
21971 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021972 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21973 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021974 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021975 /* Use "name" to avoid a warning from some compiler that checks the
21976 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021977 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021978 name = v->di_key;
21979 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021981 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021983 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021984 v->di_tv.vval.v_list = &fc->l_varlist;
21985 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21986 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21987 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021988
21989 /*
21990 * Set a:firstline to "firstline" and a:lastline to "lastline".
21991 * Set a:name to named arguments.
21992 * Set a:N to the "..." arguments.
21993 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021994 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021995 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021996 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021997 (varnumber_T)lastline);
21998 for (i = 0; i < argcount; ++i)
21999 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022000 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022001 if (ai < 0)
22002 /* named argument a:name */
22003 name = FUNCARG(fp, i);
22004 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022005 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022006 /* "..." argument a:1, a:2, etc. */
22007 sprintf((char *)numbuf, "%d", ai + 1);
22008 name = numbuf;
22009 }
22010 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22011 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022012 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22014 }
22015 else
22016 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022017 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22018 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 if (v == NULL)
22020 break;
22021 v->di_flags = DI_FLAGS_RO;
22022 }
22023 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022024 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022025
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022026 /* Note: the values are copied directly to avoid alloc/free.
22027 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022028 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022029 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022030
22031 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22032 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022033 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22034 fc->l_listitems[ai].li_tv = argvars[i];
22035 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022036 }
22037 }
22038
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 /* Don't redraw while executing the function. */
22040 ++RedrawingDisabled;
22041 save_sourcing_name = sourcing_name;
22042 save_sourcing_lnum = sourcing_lnum;
22043 sourcing_lnum = 1;
22044 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022045 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 if (sourcing_name != NULL)
22047 {
22048 if (save_sourcing_name != NULL
22049 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22050 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22051 else
22052 STRCPY(sourcing_name, "function ");
22053 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22054
22055 if (p_verbose >= 12)
22056 {
22057 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022058 verbose_enter_scroll();
22059
Bram Moolenaar555b2802005-05-19 21:08:39 +000022060 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 if (p_verbose >= 14)
22062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022064 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022065 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022066 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067
22068 msg_puts((char_u *)"(");
22069 for (i = 0; i < argcount; ++i)
22070 {
22071 if (i > 0)
22072 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022073 if (argvars[i].v_type == VAR_NUMBER)
22074 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 else
22076 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022077 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22078 if (s != NULL)
22079 {
22080 trunc_string(s, buf, MSG_BUF_CLEN);
22081 msg_puts(buf);
22082 vim_free(tofree);
22083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 }
22085 }
22086 msg_puts((char_u *)")");
22087 }
22088 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022089
22090 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 --no_wait_return;
22092 }
22093 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022094#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022095 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022096 {
22097 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22098 func_do_profile(fp);
22099 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022100 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022101 {
22102 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022103 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022104 profile_zero(&fp->uf_tm_children);
22105 }
22106 script_prof_save(&wait_start);
22107 }
22108#endif
22109
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022111 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 save_did_emsg = did_emsg;
22113 did_emsg = FALSE;
22114
22115 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022116 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22118
22119 --RedrawingDisabled;
22120
22121 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022122 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022124 clear_tv(rettv);
22125 rettv->v_type = VAR_NUMBER;
22126 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 }
22128
Bram Moolenaar05159a02005-02-26 23:04:13 +000022129#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022130 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022131 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022132 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022133 profile_end(&call_start);
22134 profile_sub_wait(&wait_start, &call_start);
22135 profile_add(&fp->uf_tm_total, &call_start);
22136 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022137 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022138 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022139 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22140 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022141 }
22142 }
22143#endif
22144
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 /* when being verbose, mention the return value */
22146 if (p_verbose >= 12)
22147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022149 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022152 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022153 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022154 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022155 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022158 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022159 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022160 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022161 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022162
Bram Moolenaar555b2802005-05-19 21:08:39 +000022163 /* The value may be very long. Skip the middle part, so that we
22164 * have some idea how it starts and ends. smsg() would always
22165 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022166 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022167 if (s != NULL)
22168 {
22169 trunc_string(s, buf, MSG_BUF_CLEN);
22170 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22171 vim_free(tofree);
22172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 }
22174 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022175
22176 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 --no_wait_return;
22178 }
22179
22180 vim_free(sourcing_name);
22181 sourcing_name = save_sourcing_name;
22182 sourcing_lnum = save_sourcing_lnum;
22183 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022184#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022185 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022186 script_prof_restore(&wait_start);
22187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188
22189 if (p_verbose >= 12 && sourcing_name != NULL)
22190 {
22191 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022192 verbose_enter_scroll();
22193
Bram Moolenaar555b2802005-05-19 21:08:39 +000022194 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022196
22197 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 --no_wait_return;
22199 }
22200
22201 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022202 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022204
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022205 /* If the a:000 list and the l: and a: dicts are not referenced we can
22206 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022207 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22208 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22209 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22210 {
22211 free_funccal(fc, FALSE);
22212 }
22213 else
22214 {
22215 hashitem_T *hi;
22216 listitem_T *li;
22217 int todo;
22218
22219 /* "fc" is still in use. This can happen when returning "a:000" or
22220 * assigning "l:" to a global variable.
22221 * Link "fc" in the list for garbage collection later. */
22222 fc->caller = previous_funccal;
22223 previous_funccal = fc;
22224
22225 /* Make a copy of the a: variables, since we didn't do that above. */
22226 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22227 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22228 {
22229 if (!HASHITEM_EMPTY(hi))
22230 {
22231 --todo;
22232 v = HI2DI(hi);
22233 copy_tv(&v->di_tv, &v->di_tv);
22234 }
22235 }
22236
22237 /* Make a copy of the a:000 items, since we didn't do that above. */
22238 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22239 copy_tv(&li->li_tv, &li->li_tv);
22240 }
22241}
22242
22243/*
22244 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022245 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022246 */
22247 static int
22248can_free_funccal(fc, copyID)
22249 funccall_T *fc;
22250 int copyID;
22251{
22252 return (fc->l_varlist.lv_copyID != copyID
22253 && fc->l_vars.dv_copyID != copyID
22254 && fc->l_avars.dv_copyID != copyID);
22255}
22256
22257/*
22258 * Free "fc" and what it contains.
22259 */
22260 static void
22261free_funccal(fc, free_val)
22262 funccall_T *fc;
22263 int free_val; /* a: vars were allocated */
22264{
22265 listitem_T *li;
22266
22267 /* The a: variables typevals may not have been allocated, only free the
22268 * allocated variables. */
22269 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22270
22271 /* free all l: variables */
22272 vars_clear(&fc->l_vars.dv_hashtab);
22273
22274 /* Free the a:000 variables if they were allocated. */
22275 if (free_val)
22276 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22277 clear_tv(&li->li_tv);
22278
22279 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280}
22281
22282/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022283 * Add a number variable "name" to dict "dp" with value "nr".
22284 */
22285 static void
22286add_nr_var(dp, v, name, nr)
22287 dict_T *dp;
22288 dictitem_T *v;
22289 char *name;
22290 varnumber_T nr;
22291{
22292 STRCPY(v->di_key, name);
22293 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22294 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22295 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022296 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022297 v->di_tv.vval.v_number = nr;
22298}
22299
22300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 * ":return [expr]"
22302 */
22303 void
22304ex_return(eap)
22305 exarg_T *eap;
22306{
22307 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 int returning = FALSE;
22310
22311 if (current_funccal == NULL)
22312 {
22313 EMSG(_("E133: :return not inside a function"));
22314 return;
22315 }
22316
22317 if (eap->skip)
22318 ++emsg_skip;
22319
22320 eap->nextcmd = NULL;
22321 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022322 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 {
22324 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022325 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022327 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 }
22329 /* It's safer to return also on error. */
22330 else if (!eap->skip)
22331 {
22332 /*
22333 * Return unless the expression evaluation has been cancelled due to an
22334 * aborting error, an interrupt, or an exception.
22335 */
22336 if (!aborting())
22337 returning = do_return(eap, FALSE, TRUE, NULL);
22338 }
22339
22340 /* When skipping or the return gets pending, advance to the next command
22341 * in this line (!returning). Otherwise, ignore the rest of the line.
22342 * Following lines will be ignored by get_func_line(). */
22343 if (returning)
22344 eap->nextcmd = NULL;
22345 else if (eap->nextcmd == NULL) /* no argument */
22346 eap->nextcmd = check_nextcmd(arg);
22347
22348 if (eap->skip)
22349 --emsg_skip;
22350}
22351
22352/*
22353 * Return from a function. Possibly makes the return pending. Also called
22354 * for a pending return at the ":endtry" or after returning from an extra
22355 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022356 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022357 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 * FALSE when the return gets pending.
22359 */
22360 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022361do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 exarg_T *eap;
22363 int reanimate;
22364 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022365 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366{
22367 int idx;
22368 struct condstack *cstack = eap->cstack;
22369
22370 if (reanimate)
22371 /* Undo the return. */
22372 current_funccal->returned = FALSE;
22373
22374 /*
22375 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22376 * not in its finally clause (which then is to be executed next) is found.
22377 * In this case, make the ":return" pending for execution at the ":endtry".
22378 * Otherwise, return normally.
22379 */
22380 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22381 if (idx >= 0)
22382 {
22383 cstack->cs_pending[idx] = CSTP_RETURN;
22384
22385 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022386 /* A pending return again gets pending. "rettv" points to an
22387 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022389 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 else
22391 {
22392 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022395 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022397 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 {
22399 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022400 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022401 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 else
22403 EMSG(_(e_outofmem));
22404 }
22405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022406 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407
22408 if (reanimate)
22409 {
22410 /* The pending return value could be overwritten by a ":return"
22411 * without argument in a finally clause; reset the default
22412 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022413 current_funccal->rettv->v_type = VAR_NUMBER;
22414 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 }
22416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022417 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 }
22419 else
22420 {
22421 current_funccal->returned = TRUE;
22422
22423 /* If the return is carried out now, store the return value. For
22424 * a return immediately after reanimation, the value is already
22425 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022426 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022428 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022431 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 }
22433 }
22434
22435 return idx < 0;
22436}
22437
22438/*
22439 * Free the variable with a pending return value.
22440 */
22441 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022442discard_pending_return(rettv)
22443 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444{
Bram Moolenaar33570922005-01-25 22:26:29 +000022445 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446}
22447
22448/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022449 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 * is an allocated string. Used by report_pending() for verbose messages.
22451 */
22452 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022453get_return_cmd(rettv)
22454 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022456 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022457 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022458 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022460 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022461 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022462 if (s == NULL)
22463 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022464
22465 STRCPY(IObuff, ":return ");
22466 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22467 if (STRLEN(s) + 8 >= IOSIZE)
22468 STRCPY(IObuff + IOSIZE - 4, "...");
22469 vim_free(tofree);
22470 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471}
22472
22473/*
22474 * Get next function line.
22475 * Called by do_cmdline() to get the next line.
22476 * Returns allocated string, or NULL for end of function.
22477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 char_u *
22479get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022480 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022482 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483{
Bram Moolenaar33570922005-01-25 22:26:29 +000022484 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022485 ufunc_T *fp = fcp->func;
22486 char_u *retval;
22487 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488
22489 /* If breakpoints have been added/deleted need to check for it. */
22490 if (fcp->dbg_tick != debug_tick)
22491 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022492 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 sourcing_lnum);
22494 fcp->dbg_tick = debug_tick;
22495 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022496#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022497 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022498 func_line_end(cookie);
22499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500
Bram Moolenaar05159a02005-02-26 23:04:13 +000022501 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022502 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22503 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 retval = NULL;
22505 else
22506 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022507 /* Skip NULL lines (continuation lines). */
22508 while (fcp->linenr < gap->ga_len
22509 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22510 ++fcp->linenr;
22511 if (fcp->linenr >= gap->ga_len)
22512 retval = NULL;
22513 else
22514 {
22515 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22516 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022517#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022518 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022519 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022520#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 }
22523
22524 /* Did we encounter a breakpoint? */
22525 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22526 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022527 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022529 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 sourcing_lnum);
22531 fcp->dbg_tick = debug_tick;
22532 }
22533
22534 return retval;
22535}
22536
Bram Moolenaar05159a02005-02-26 23:04:13 +000022537#if defined(FEAT_PROFILE) || defined(PROTO)
22538/*
22539 * Called when starting to read a function line.
22540 * "sourcing_lnum" must be correct!
22541 * When skipping lines it may not actually be executed, but we won't find out
22542 * until later and we need to store the time now.
22543 */
22544 void
22545func_line_start(cookie)
22546 void *cookie;
22547{
22548 funccall_T *fcp = (funccall_T *)cookie;
22549 ufunc_T *fp = fcp->func;
22550
22551 if (fp->uf_profiling && sourcing_lnum >= 1
22552 && sourcing_lnum <= fp->uf_lines.ga_len)
22553 {
22554 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022555 /* Skip continuation lines. */
22556 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22557 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022558 fp->uf_tml_execed = FALSE;
22559 profile_start(&fp->uf_tml_start);
22560 profile_zero(&fp->uf_tml_children);
22561 profile_get_wait(&fp->uf_tml_wait);
22562 }
22563}
22564
22565/*
22566 * Called when actually executing a function line.
22567 */
22568 void
22569func_line_exec(cookie)
22570 void *cookie;
22571{
22572 funccall_T *fcp = (funccall_T *)cookie;
22573 ufunc_T *fp = fcp->func;
22574
22575 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22576 fp->uf_tml_execed = TRUE;
22577}
22578
22579/*
22580 * Called when done with a function line.
22581 */
22582 void
22583func_line_end(cookie)
22584 void *cookie;
22585{
22586 funccall_T *fcp = (funccall_T *)cookie;
22587 ufunc_T *fp = fcp->func;
22588
22589 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22590 {
22591 if (fp->uf_tml_execed)
22592 {
22593 ++fp->uf_tml_count[fp->uf_tml_idx];
22594 profile_end(&fp->uf_tml_start);
22595 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022596 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022597 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22598 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022599 }
22600 fp->uf_tml_idx = -1;
22601 }
22602}
22603#endif
22604
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605/*
22606 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022607 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 */
22609 int
22610func_has_ended(cookie)
22611 void *cookie;
22612{
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614
22615 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22616 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022617 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 || fcp->returned);
22619}
22620
22621/*
22622 * return TRUE if cookie indicates a function which "abort"s on errors.
22623 */
22624 int
22625func_has_abort(cookie)
22626 void *cookie;
22627{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022628 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629}
22630
22631#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22632typedef enum
22633{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022634 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22635 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22636 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637} var_flavour_T;
22638
22639static var_flavour_T var_flavour __ARGS((char_u *varname));
22640
22641 static var_flavour_T
22642var_flavour(varname)
22643 char_u *varname;
22644{
22645 char_u *p = varname;
22646
22647 if (ASCII_ISUPPER(*p))
22648 {
22649 while (*(++p))
22650 if (ASCII_ISLOWER(*p))
22651 return VAR_FLAVOUR_SESSION;
22652 return VAR_FLAVOUR_VIMINFO;
22653 }
22654 else
22655 return VAR_FLAVOUR_DEFAULT;
22656}
22657#endif
22658
22659#if defined(FEAT_VIMINFO) || defined(PROTO)
22660/*
22661 * Restore global vars that start with a capital from the viminfo file
22662 */
22663 int
22664read_viminfo_varlist(virp, writing)
22665 vir_T *virp;
22666 int writing;
22667{
22668 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022669 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671
22672 if (!writing && (find_viminfo_parameter('!') != NULL))
22673 {
22674 tab = vim_strchr(virp->vir_line + 1, '\t');
22675 if (tab != NULL)
22676 {
22677 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022678 switch (*tab)
22679 {
22680 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022681#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022682 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022683#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022684 case 'D': type = VAR_DICT; break;
22685 case 'L': type = VAR_LIST; break;
22686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687
22688 tab = vim_strchr(tab, '\t');
22689 if (tab != NULL)
22690 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022691 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022692 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022693 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022695#ifdef FEAT_FLOAT
22696 else if (type == VAR_FLOAT)
22697 (void)string2float(tab + 1, &tv.vval.v_float);
22698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022700 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022701 if (type == VAR_DICT || type == VAR_LIST)
22702 {
22703 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22704
22705 if (etv == NULL)
22706 /* Failed to parse back the dict or list, use it as a
22707 * string. */
22708 tv.v_type = VAR_STRING;
22709 else
22710 {
22711 vim_free(tv.vval.v_string);
22712 tv = *etv;
22713 }
22714 }
22715
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022716 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022717
22718 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022719 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022720 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22721 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 }
22723 }
22724 }
22725
22726 return viminfo_readline(virp);
22727}
22728
22729/*
22730 * Write global vars that start with a capital to the viminfo file
22731 */
22732 void
22733write_viminfo_varlist(fp)
22734 FILE *fp;
22735{
Bram Moolenaar33570922005-01-25 22:26:29 +000022736 hashitem_T *hi;
22737 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022738 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022739 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022740 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022741 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022742 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743
22744 if (find_viminfo_parameter('!') == NULL)
22745 return;
22746
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022747 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022748
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022749 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022752 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022754 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022755 this_var = HI2DI(hi);
22756 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022757 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022758 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022759 {
22760 case VAR_STRING: s = "STR"; break;
22761 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022762#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022763 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022764#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022765 case VAR_DICT: s = "DIC"; break;
22766 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022767 default: continue;
22768 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022769 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022770 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022771 if (p != NULL)
22772 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022773 vim_free(tofree);
22774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 }
22776 }
22777}
22778#endif
22779
22780#if defined(FEAT_SESSION) || defined(PROTO)
22781 int
22782store_session_globals(fd)
22783 FILE *fd;
22784{
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 hashitem_T *hi;
22786 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022787 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 char_u *p, *t;
22789
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022790 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022791 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022793 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022795 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022796 this_var = HI2DI(hi);
22797 if ((this_var->di_tv.v_type == VAR_NUMBER
22798 || this_var->di_tv.v_type == VAR_STRING)
22799 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022800 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022801 /* Escape special characters with a backslash. Turn a LF and
22802 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022803 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022804 (char_u *)"\\\"\n\r");
22805 if (p == NULL) /* out of memory */
22806 break;
22807 for (t = p; *t != NUL; ++t)
22808 if (*t == '\n')
22809 *t = 'n';
22810 else if (*t == '\r')
22811 *t = 'r';
22812 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022813 this_var->di_key,
22814 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22815 : ' ',
22816 p,
22817 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22818 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022819 || put_eol(fd) == FAIL)
22820 {
22821 vim_free(p);
22822 return FAIL;
22823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 vim_free(p);
22825 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022826#ifdef FEAT_FLOAT
22827 else if (this_var->di_tv.v_type == VAR_FLOAT
22828 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22829 {
22830 float_T f = this_var->di_tv.vval.v_float;
22831 int sign = ' ';
22832
22833 if (f < 0)
22834 {
22835 f = -f;
22836 sign = '-';
22837 }
22838 if ((fprintf(fd, "let %s = %c&%f",
22839 this_var->di_key, sign, f) < 0)
22840 || put_eol(fd) == FAIL)
22841 return FAIL;
22842 }
22843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 }
22845 }
22846 return OK;
22847}
22848#endif
22849
Bram Moolenaar661b1822005-07-28 22:36:45 +000022850/*
22851 * Display script name where an item was last set.
22852 * Should only be invoked when 'verbose' is non-zero.
22853 */
22854 void
22855last_set_msg(scriptID)
22856 scid_T scriptID;
22857{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022858 char_u *p;
22859
Bram Moolenaar661b1822005-07-28 22:36:45 +000022860 if (scriptID != 0)
22861 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022862 p = home_replace_save(NULL, get_scriptname(scriptID));
22863 if (p != NULL)
22864 {
22865 verbose_enter();
22866 MSG_PUTS(_("\n\tLast set from "));
22867 MSG_PUTS(p);
22868 vim_free(p);
22869 verbose_leave();
22870 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022871 }
22872}
22873
Bram Moolenaard812df62008-11-09 12:46:09 +000022874/*
22875 * List v:oldfiles in a nice way.
22876 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022877 void
22878ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022879 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022880{
22881 list_T *l = vimvars[VV_OLDFILES].vv_list;
22882 listitem_T *li;
22883 int nr = 0;
22884
22885 if (l == NULL)
22886 msg((char_u *)_("No old files"));
22887 else
22888 {
22889 msg_start();
22890 msg_scroll = TRUE;
22891 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22892 {
22893 msg_outnum((long)++nr);
22894 MSG_PUTS(": ");
22895 msg_outtrans(get_tv_string(&li->li_tv));
22896 msg_putchar('\n');
22897 out_flush(); /* output one line at a time */
22898 ui_breakcheck();
22899 }
22900 /* Assume "got_int" was set to truncate the listing. */
22901 got_int = FALSE;
22902
22903#ifdef FEAT_BROWSE_CMD
22904 if (cmdmod.browse)
22905 {
22906 quit_more = FALSE;
22907 nr = prompt_for_number(FALSE);
22908 msg_starthere();
22909 if (nr > 0)
22910 {
22911 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22912 (long)nr);
22913
22914 if (p != NULL)
22915 {
22916 p = expand_env_save(p);
22917 eap->arg = p;
22918 eap->cmdidx = CMD_edit;
22919 cmdmod.browse = FALSE;
22920 do_exedit(eap, NULL);
22921 vim_free(p);
22922 }
22923 }
22924 }
22925#endif
22926 }
22927}
22928
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929#endif /* FEAT_EVAL */
22930
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022932#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933
22934#ifdef WIN3264
22935/*
22936 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22937 */
22938static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22939static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22940static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22941
22942/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022943 * Get the short path (8.3) for the filename in "fnamep".
22944 * Only works for a valid file name.
22945 * When the path gets longer "fnamep" is changed and the allocated buffer
22946 * is put in "bufp".
22947 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22948 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 */
22950 static int
22951get_short_pathname(fnamep, bufp, fnamelen)
22952 char_u **fnamep;
22953 char_u **bufp;
22954 int *fnamelen;
22955{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022956 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 char_u *newbuf;
22958
22959 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 l = GetShortPathName(*fnamep, *fnamep, len);
22961 if (l > len - 1)
22962 {
22963 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022964 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 newbuf = vim_strnsave(*fnamep, l);
22966 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968
22969 vim_free(*bufp);
22970 *fnamep = *bufp = newbuf;
22971
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022972 /* Really should always succeed, as the buffer is big enough. */
22973 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 }
22975
22976 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022977 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978}
22979
22980/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022981 * Get the short path (8.3) for the filename in "fname". The converted
22982 * path is returned in "bufp".
22983 *
22984 * Some of the directories specified in "fname" may not exist. This function
22985 * will shorten the existing directories at the beginning of the path and then
22986 * append the remaining non-existing path.
22987 *
22988 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022989 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022990 * bufp - Pointer to an allocated buffer for the filename.
22991 * fnamelen - Length of the filename pointed to by fname
22992 *
22993 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 */
22995 static int
22996shortpath_for_invalid_fname(fname, bufp, fnamelen)
22997 char_u **fname;
22998 char_u **bufp;
22999 int *fnamelen;
23000{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023001 char_u *short_fname, *save_fname, *pbuf_unused;
23002 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023004 int old_len, len;
23005 int new_len, sfx_len;
23006 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007
23008 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023009 old_len = *fnamelen;
23010 save_fname = vim_strnsave(*fname, old_len);
23011 pbuf_unused = NULL;
23012 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023014 endp = save_fname + old_len - 1; /* Find the end of the copy */
23015 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023017 /*
23018 * Try shortening the supplied path till it succeeds by removing one
23019 * directory at a time from the tail of the path.
23020 */
23021 len = 0;
23022 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023024 /* go back one path-separator */
23025 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23026 --endp;
23027 if (endp <= save_fname)
23028 break; /* processed the complete path */
23029
23030 /*
23031 * Replace the path separator with a NUL and try to shorten the
23032 * resulting path.
23033 */
23034 ch = *endp;
23035 *endp = 0;
23036 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023037 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023038 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23039 {
23040 retval = FAIL;
23041 goto theend;
23042 }
23043 *endp = ch; /* preserve the string */
23044
23045 if (len > 0)
23046 break; /* successfully shortened the path */
23047
23048 /* failed to shorten the path. Skip the path separator */
23049 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 }
23051
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023052 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023054 /*
23055 * Succeeded in shortening the path. Now concatenate the shortened
23056 * path with the remaining path at the tail.
23057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023059 /* Compute the length of the new path. */
23060 sfx_len = (int)(save_endp - endp) + 1;
23061 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023063 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023065 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023067 /* There is not enough space in the currently allocated string,
23068 * copy it to a buffer big enough. */
23069 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023071 {
23072 retval = FAIL;
23073 goto theend;
23074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 }
23076 else
23077 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023078 /* Transfer short_fname to the main buffer (it's big enough),
23079 * unless get_short_pathname() did its work in-place. */
23080 *fname = *bufp = save_fname;
23081 if (short_fname != save_fname)
23082 vim_strncpy(save_fname, short_fname, len);
23083 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023085
23086 /* concat the not-shortened part of the path */
23087 vim_strncpy(*fname + len, endp, sfx_len);
23088 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023090
23091theend:
23092 vim_free(pbuf_unused);
23093 vim_free(save_fname);
23094
23095 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096}
23097
23098/*
23099 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023100 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 */
23102 static int
23103shortpath_for_partial(fnamep, bufp, fnamelen)
23104 char_u **fnamep;
23105 char_u **bufp;
23106 int *fnamelen;
23107{
23108 int sepcount, len, tflen;
23109 char_u *p;
23110 char_u *pbuf, *tfname;
23111 int hasTilde;
23112
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023113 /* Count up the path separators from the RHS.. so we know which part
23114 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023116 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 if (vim_ispathsep(*p))
23118 ++sepcount;
23119
23120 /* Need full path first (use expand_env() to remove a "~/") */
23121 hasTilde = (**fnamep == '~');
23122 if (hasTilde)
23123 pbuf = tfname = expand_env_save(*fnamep);
23124 else
23125 pbuf = tfname = FullName_save(*fnamep, FALSE);
23126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023127 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023129 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23130 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131
23132 if (len == 0)
23133 {
23134 /* Don't have a valid filename, so shorten the rest of the
23135 * path if we can. This CAN give us invalid 8.3 filenames, but
23136 * there's not a lot of point in guessing what it might be.
23137 */
23138 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023139 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23140 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 }
23142
23143 /* Count the paths backward to find the beginning of the desired string. */
23144 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023145 {
23146#ifdef FEAT_MBYTE
23147 if (has_mbyte)
23148 p -= mb_head_off(tfname, p);
23149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 if (vim_ispathsep(*p))
23151 {
23152 if (sepcount == 0 || (hasTilde && sepcount == 1))
23153 break;
23154 else
23155 sepcount --;
23156 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 if (hasTilde)
23159 {
23160 --p;
23161 if (p >= tfname)
23162 *p = '~';
23163 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 }
23166 else
23167 ++p;
23168
23169 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23170 vim_free(*bufp);
23171 *fnamelen = (int)STRLEN(p);
23172 *bufp = pbuf;
23173 *fnamep = p;
23174
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023175 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176}
23177#endif /* WIN3264 */
23178
23179/*
23180 * Adjust a filename, according to a string of modifiers.
23181 * *fnamep must be NUL terminated when called. When returning, the length is
23182 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023183 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 * When there is an error, *fnamep is set to NULL.
23185 */
23186 int
23187modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23188 char_u *src; /* string with modifiers */
23189 int *usedlen; /* characters after src that are used */
23190 char_u **fnamep; /* file name so far */
23191 char_u **bufp; /* buffer for allocated file name or NULL */
23192 int *fnamelen; /* length of fnamep */
23193{
23194 int valid = 0;
23195 char_u *tail;
23196 char_u *s, *p, *pbuf;
23197 char_u dirname[MAXPATHL];
23198 int c;
23199 int has_fullname = 0;
23200#ifdef WIN3264
23201 int has_shortname = 0;
23202#endif
23203
23204repeat:
23205 /* ":p" - full path/file_name */
23206 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23207 {
23208 has_fullname = 1;
23209
23210 valid |= VALID_PATH;
23211 *usedlen += 2;
23212
23213 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23214 if ((*fnamep)[0] == '~'
23215#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23216 && ((*fnamep)[1] == '/'
23217# ifdef BACKSLASH_IN_FILENAME
23218 || (*fnamep)[1] == '\\'
23219# endif
23220 || (*fnamep)[1] == NUL)
23221
23222#endif
23223 )
23224 {
23225 *fnamep = expand_env_save(*fnamep);
23226 vim_free(*bufp); /* free any allocated file name */
23227 *bufp = *fnamep;
23228 if (*fnamep == NULL)
23229 return -1;
23230 }
23231
23232 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023233 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 {
23235 if (vim_ispathsep(*p)
23236 && p[1] == '.'
23237 && (p[2] == NUL
23238 || vim_ispathsep(p[2])
23239 || (p[2] == '.'
23240 && (p[3] == NUL || vim_ispathsep(p[3])))))
23241 break;
23242 }
23243
23244 /* FullName_save() is slow, don't use it when not needed. */
23245 if (*p != NUL || !vim_isAbsName(*fnamep))
23246 {
23247 *fnamep = FullName_save(*fnamep, *p != NUL);
23248 vim_free(*bufp); /* free any allocated file name */
23249 *bufp = *fnamep;
23250 if (*fnamep == NULL)
23251 return -1;
23252 }
23253
23254 /* Append a path separator to a directory. */
23255 if (mch_isdir(*fnamep))
23256 {
23257 /* Make room for one or two extra characters. */
23258 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23259 vim_free(*bufp); /* free any allocated file name */
23260 *bufp = *fnamep;
23261 if (*fnamep == NULL)
23262 return -1;
23263 add_pathsep(*fnamep);
23264 }
23265 }
23266
23267 /* ":." - path relative to the current directory */
23268 /* ":~" - path relative to the home directory */
23269 /* ":8" - shortname path - postponed till after */
23270 while (src[*usedlen] == ':'
23271 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23272 {
23273 *usedlen += 2;
23274 if (c == '8')
23275 {
23276#ifdef WIN3264
23277 has_shortname = 1; /* Postpone this. */
23278#endif
23279 continue;
23280 }
23281 pbuf = NULL;
23282 /* Need full path first (use expand_env() to remove a "~/") */
23283 if (!has_fullname)
23284 {
23285 if (c == '.' && **fnamep == '~')
23286 p = pbuf = expand_env_save(*fnamep);
23287 else
23288 p = pbuf = FullName_save(*fnamep, FALSE);
23289 }
23290 else
23291 p = *fnamep;
23292
23293 has_fullname = 0;
23294
23295 if (p != NULL)
23296 {
23297 if (c == '.')
23298 {
23299 mch_dirname(dirname, MAXPATHL);
23300 s = shorten_fname(p, dirname);
23301 if (s != NULL)
23302 {
23303 *fnamep = s;
23304 if (pbuf != NULL)
23305 {
23306 vim_free(*bufp); /* free any allocated file name */
23307 *bufp = pbuf;
23308 pbuf = NULL;
23309 }
23310 }
23311 }
23312 else
23313 {
23314 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23315 /* Only replace it when it starts with '~' */
23316 if (*dirname == '~')
23317 {
23318 s = vim_strsave(dirname);
23319 if (s != NULL)
23320 {
23321 *fnamep = s;
23322 vim_free(*bufp);
23323 *bufp = s;
23324 }
23325 }
23326 }
23327 vim_free(pbuf);
23328 }
23329 }
23330
23331 tail = gettail(*fnamep);
23332 *fnamelen = (int)STRLEN(*fnamep);
23333
23334 /* ":h" - head, remove "/file_name", can be repeated */
23335 /* Don't remove the first "/" or "c:\" */
23336 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23337 {
23338 valid |= VALID_HEAD;
23339 *usedlen += 2;
23340 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023341 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023342 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 *fnamelen = (int)(tail - *fnamep);
23344#ifdef VMS
23345 if (*fnamelen > 0)
23346 *fnamelen += 1; /* the path separator is part of the path */
23347#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023348 if (*fnamelen == 0)
23349 {
23350 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23351 p = vim_strsave((char_u *)".");
23352 if (p == NULL)
23353 return -1;
23354 vim_free(*bufp);
23355 *bufp = *fnamep = tail = p;
23356 *fnamelen = 1;
23357 }
23358 else
23359 {
23360 while (tail > s && !after_pathsep(s, tail))
23361 mb_ptr_back(*fnamep, tail);
23362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 }
23364
23365 /* ":8" - shortname */
23366 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23367 {
23368 *usedlen += 2;
23369#ifdef WIN3264
23370 has_shortname = 1;
23371#endif
23372 }
23373
23374#ifdef WIN3264
23375 /* Check shortname after we have done 'heads' and before we do 'tails'
23376 */
23377 if (has_shortname)
23378 {
23379 pbuf = NULL;
23380 /* Copy the string if it is shortened by :h */
23381 if (*fnamelen < (int)STRLEN(*fnamep))
23382 {
23383 p = vim_strnsave(*fnamep, *fnamelen);
23384 if (p == 0)
23385 return -1;
23386 vim_free(*bufp);
23387 *bufp = *fnamep = p;
23388 }
23389
23390 /* Split into two implementations - makes it easier. First is where
23391 * there isn't a full name already, second is where there is.
23392 */
23393 if (!has_fullname && !vim_isAbsName(*fnamep))
23394 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023395 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 return -1;
23397 }
23398 else
23399 {
23400 int l;
23401
23402 /* Simple case, already have the full-name
23403 * Nearly always shorter, so try first time. */
23404 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023405 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 return -1;
23407
23408 if (l == 0)
23409 {
23410 /* Couldn't find the filename.. search the paths.
23411 */
23412 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023413 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 return -1;
23415 }
23416 *fnamelen = l;
23417 }
23418 }
23419#endif /* WIN3264 */
23420
23421 /* ":t" - tail, just the basename */
23422 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23423 {
23424 *usedlen += 2;
23425 *fnamelen -= (int)(tail - *fnamep);
23426 *fnamep = tail;
23427 }
23428
23429 /* ":e" - extension, can be repeated */
23430 /* ":r" - root, without extension, can be repeated */
23431 while (src[*usedlen] == ':'
23432 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23433 {
23434 /* find a '.' in the tail:
23435 * - for second :e: before the current fname
23436 * - otherwise: The last '.'
23437 */
23438 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23439 s = *fnamep - 2;
23440 else
23441 s = *fnamep + *fnamelen - 1;
23442 for ( ; s > tail; --s)
23443 if (s[0] == '.')
23444 break;
23445 if (src[*usedlen + 1] == 'e') /* :e */
23446 {
23447 if (s > tail)
23448 {
23449 *fnamelen += (int)(*fnamep - (s + 1));
23450 *fnamep = s + 1;
23451#ifdef VMS
23452 /* cut version from the extension */
23453 s = *fnamep + *fnamelen - 1;
23454 for ( ; s > *fnamep; --s)
23455 if (s[0] == ';')
23456 break;
23457 if (s > *fnamep)
23458 *fnamelen = s - *fnamep;
23459#endif
23460 }
23461 else if (*fnamep <= tail)
23462 *fnamelen = 0;
23463 }
23464 else /* :r */
23465 {
23466 if (s > tail) /* remove one extension */
23467 *fnamelen = (int)(s - *fnamep);
23468 }
23469 *usedlen += 2;
23470 }
23471
23472 /* ":s?pat?foo?" - substitute */
23473 /* ":gs?pat?foo?" - global substitute */
23474 if (src[*usedlen] == ':'
23475 && (src[*usedlen + 1] == 's'
23476 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23477 {
23478 char_u *str;
23479 char_u *pat;
23480 char_u *sub;
23481 int sep;
23482 char_u *flags;
23483 int didit = FALSE;
23484
23485 flags = (char_u *)"";
23486 s = src + *usedlen + 2;
23487 if (src[*usedlen + 1] == 'g')
23488 {
23489 flags = (char_u *)"g";
23490 ++s;
23491 }
23492
23493 sep = *s++;
23494 if (sep)
23495 {
23496 /* find end of pattern */
23497 p = vim_strchr(s, sep);
23498 if (p != NULL)
23499 {
23500 pat = vim_strnsave(s, (int)(p - s));
23501 if (pat != NULL)
23502 {
23503 s = p + 1;
23504 /* find end of substitution */
23505 p = vim_strchr(s, sep);
23506 if (p != NULL)
23507 {
23508 sub = vim_strnsave(s, (int)(p - s));
23509 str = vim_strnsave(*fnamep, *fnamelen);
23510 if (sub != NULL && str != NULL)
23511 {
23512 *usedlen = (int)(p + 1 - src);
23513 s = do_string_sub(str, pat, sub, flags);
23514 if (s != NULL)
23515 {
23516 *fnamep = s;
23517 *fnamelen = (int)STRLEN(s);
23518 vim_free(*bufp);
23519 *bufp = s;
23520 didit = TRUE;
23521 }
23522 }
23523 vim_free(sub);
23524 vim_free(str);
23525 }
23526 vim_free(pat);
23527 }
23528 }
23529 /* after using ":s", repeat all the modifiers */
23530 if (didit)
23531 goto repeat;
23532 }
23533 }
23534
23535 return valid;
23536}
23537
23538/*
23539 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23540 * "flags" can be "g" to do a global substitute.
23541 * Returns an allocated string, NULL for error.
23542 */
23543 char_u *
23544do_string_sub(str, pat, sub, flags)
23545 char_u *str;
23546 char_u *pat;
23547 char_u *sub;
23548 char_u *flags;
23549{
23550 int sublen;
23551 regmatch_T regmatch;
23552 int i;
23553 int do_all;
23554 char_u *tail;
23555 garray_T ga;
23556 char_u *ret;
23557 char_u *save_cpo;
23558
23559 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23560 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023561 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562
23563 ga_init2(&ga, 1, 200);
23564
23565 do_all = (flags[0] == 'g');
23566
23567 regmatch.rm_ic = p_ic;
23568 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23569 if (regmatch.regprog != NULL)
23570 {
23571 tail = str;
23572 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23573 {
23574 /*
23575 * Get some space for a temporary buffer to do the substitution
23576 * into. It will contain:
23577 * - The text up to where the match is.
23578 * - The substituted text.
23579 * - The text after the match.
23580 */
23581 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23582 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23583 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23584 {
23585 ga_clear(&ga);
23586 break;
23587 }
23588
23589 /* copy the text up to where the match is */
23590 i = (int)(regmatch.startp[0] - tail);
23591 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23592 /* add the substituted text */
23593 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23594 + ga.ga_len + i, TRUE, TRUE, FALSE);
23595 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 /* avoid getting stuck on a match with an empty string */
23597 if (tail == regmatch.endp[0])
23598 {
23599 if (*tail == NUL)
23600 break;
23601 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23602 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 }
23604 else
23605 {
23606 tail = regmatch.endp[0];
23607 if (*tail == NUL)
23608 break;
23609 }
23610 if (!do_all)
23611 break;
23612 }
23613
23614 if (ga.ga_data != NULL)
23615 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23616
23617 vim_free(regmatch.regprog);
23618 }
23619
23620 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23621 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023622 if (p_cpo == empty_option)
23623 p_cpo = save_cpo;
23624 else
23625 /* Darn, evaluating {sub} expression changed the value. */
23626 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627
23628 return ret;
23629}
23630
23631#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */