blob: 3994b17274e8384651b8a493c9e0e7ae3e4b690a [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 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000111
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000112/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000113 * All user-defined global variables are stored in dictionary "globvardict".
114 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000116static dict_T globvardict;
117static dictitem_T globvars_var;
118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
126/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
129 */
130static int current_copyID = 0;
131
132/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000133 * Array to hold the hashtab with variables local to each sourced script.
134 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000136typedef struct
137{
138 dictitem_T sv_var;
139 dict_T sv_dict;
140} scriptvar_T;
141
142static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
143#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
144#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146static int echo_attr = 0; /* attributes used for ":echo" */
147
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000148/* Values for trans_function_name() argument: */
149#define TFN_INT 1 /* internal function name OK */
150#define TFN_QUIET 2 /* no error messages */
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Structure to hold info for a user function.
154 */
155typedef struct ufunc ufunc_T;
156
157struct ufunc
158{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000159 int uf_varargs; /* variable nr of arguments */
160 int uf_flags;
161 int uf_calls; /* nr of active calls */
162 garray_T uf_args; /* arguments */
163 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000164#ifdef FEAT_PROFILE
165 int uf_profiling; /* TRUE when func is being profiled */
166 /* profiling the function as a whole */
167 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000168 proftime_T uf_tm_total; /* time spent in function + children */
169 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T *uf_tml_total; /* time spent in a line + children */
174 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000195 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000197static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000199/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000200static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000202/* list heads for garbage collection */
203static dict_T *first_dict = NULL; /* list of all dicts */
204static list_T *first_list = NULL; /* list of all lists */
205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000206/* From user function to hashitem and back. */
207static ufunc_T dumuf;
208#define UF2HIKEY(fp) ((fp)->uf_name)
209#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211
212#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar33570922005-01-25 22:26:29 +0000215#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216#define VAR_SHORT_LEN 20 /* short variable name length */
217#define FIXVAR_CNT 12 /* number of fixed variables */
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000220typedef struct funccall_S funccall_T;
221
222struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 ufunc_T *func; /* function being called */
225 int linenr; /* next line to be executed */
226 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000227 struct /* fixed variables for arguments */
228 {
229 dictitem_T var; /* variable (without room for name) */
230 char_u room[VAR_SHORT_LEN]; /* room for the name */
231 } fixvar[FIXVAR_CNT];
232 dict_T l_vars; /* l: local function variables */
233 dictitem_T l_vars_var; /* variable for l: scope */
234 dict_T l_avars; /* a: argument variables */
235 dictitem_T l_avars_var; /* variable for a: scope */
236 list_T l_varlist; /* list for a:000 */
237 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
238 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 linenr_T breakpoint; /* next line with breakpoint or zero */
240 int dbg_tick; /* debug_tick when breakpoint was set */
241 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242#ifdef FEAT_PROFILE
243 proftime_T prof_child; /* time spent in a child */
244#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000245 funccall_T *caller; /* calling function or NULL */
246};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000249 * Info used by a ":for" loop.
250 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000251typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252{
253 int fi_semicolon; /* TRUE if ending in '; var]' */
254 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 listwatch_T fi_lw; /* keep an eye on the item used. */
256 list_T *fi_list; /* list being used */
257} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000260 * Struct used by trans_function_name()
261 */
262typedef struct
263{
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000265 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dictitem_T *fd_di; /* Dictionary item used */
267} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268
Bram Moolenaara7043832005-01-21 11:56:39 +0000269
270/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
275 */
276#include "version.h"
277
278/* values for vv_flags: */
279#define VV_COMPAT 1 /* compatible, also used without "v:" */
280#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000281#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
285static struct vimvar
286{
287 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288 dictitem_T vv_di; /* value and name for key */
289 char vv_filler[16]; /* space for LONGEST name below!!! */
290 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
291} vimvars[VV_LEN] =
292{
293 /*
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 */
297 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
298 {VV_NAME("count1", VAR_NUMBER), VV_RO},
299 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
300 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
301 {VV_NAME("warningmsg", VAR_STRING), 0},
302 {VV_NAME("statusmsg", VAR_STRING), 0},
303 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
305 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("termresponse", VAR_STRING), VV_RO},
308 {VV_NAME("fname", VAR_STRING), VV_RO},
309 {VV_NAME("lang", VAR_STRING), VV_RO},
310 {VV_NAME("lc_time", VAR_STRING), VV_RO},
311 {VV_NAME("ctype", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
313 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
314 {VV_NAME("fname_in", VAR_STRING), VV_RO},
315 {VV_NAME("fname_out", VAR_STRING), VV_RO},
316 {VV_NAME("fname_new", VAR_STRING), VV_RO},
317 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
318 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
319 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
322 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("progname", VAR_STRING), VV_RO},
324 {VV_NAME("servername", VAR_STRING), VV_RO},
325 {VV_NAME("dying", VAR_NUMBER), VV_RO},
326 {VV_NAME("exception", VAR_STRING), VV_RO},
327 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
328 {VV_NAME("register", VAR_STRING), VV_RO},
329 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
330 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000331 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
332 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000333 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000334 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
335 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000336 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000341 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000342 {VV_NAME("swapname", VAR_STRING), VV_RO},
343 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000344 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000346 {VV_NAME("mouse_win", VAR_NUMBER), 0},
347 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
348 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000349 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000350 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000351};
352
353/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000354#define vv_type vv_di.di_tv.v_type
355#define vv_nr vv_di.di_tv.vval.v_number
356#define vv_float vv_di.di_tv.vval.v_float
357#define vv_str vv_di.di_tv.vval.v_string
358#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000359
360/*
361 * The v: variables are stored in dictionary "vimvardict".
362 * "vimvars_var" is the variable that is used for the "l:" scope.
363 */
364static dict_T vimvardict;
365static dictitem_T vimvars_var;
366#define vimvarht vimvardict.dv_hashtab
367
Bram Moolenaara40058a2005-07-11 22:42:07 +0000368static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
369static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
370#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
371static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
372#endif
373static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
374static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
375static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000376static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
377static void list_glob_vars __ARGS((int *first));
378static void list_buf_vars __ARGS((int *first));
379static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_vim_vars __ARGS((int *first));
384static void list_script_vars __ARGS((int *first));
385static void list_func_vars __ARGS((int *first));
386static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
388static int check_changedtick __ARGS((char_u *arg));
389static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
390static void clear_lval __ARGS((lval_T *lp));
391static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
392static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
393static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
394static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
395static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
396static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
397static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
398static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
399static void item_lock __ARGS((typval_T *tv, int deep, int lock));
400static int tv_islocked __ARGS((typval_T *tv));
401
Bram Moolenaar33570922005-01-25 22:26:29 +0000402static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
403static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000411static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000416static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static listitem_T *listitem_alloc __ARGS((void));
418static void listitem_free __ARGS((listitem_T *item));
419static void listitem_remove __ARGS((list_T *l, listitem_T *item));
420static long list_len __ARGS((list_T *l));
421static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
422static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
423static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void list_append __ARGS((list_T *l, listitem_T *item));
428static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000429static int list_append_string __ARGS((list_T *l, char_u *str, int len));
430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *list2string __ARGS((typval_T *tv, int copyID));
437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000438static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
439static void set_ref_in_list __ARGS((list_T *l, int copyID));
440static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000442static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static dictitem_T *dictitem_alloc __ARGS((char_u *key));
444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
446static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int dict_add __ARGS((dict_T *d, dictitem_T *item));
449static long dict_len __ARGS((dict_T *d));
450static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000453static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
454static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static int string2float __ARGS((char_u *text, float_T *value));
458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
460static int find_internal_func __ARGS((char_u *name));
461static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
462static 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));
463static int call_func __ARGS((char_u *name, 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 +0000464static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
467static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
475static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
509static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000532static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000541static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000543static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000549static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000557static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000558static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000559static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000560static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000563static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000571static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000585static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000591static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603#ifdef FEAT_FLOAT
604static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
605#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000610static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000611static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000614static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618#ifdef vim_mkdir
619static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000625#ifdef FEAT_FLOAT
626static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000629static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000630static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000633static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000649static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000651static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000658static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000659static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000660static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000661static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000663static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000665static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
668static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000671static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000675#ifdef FEAT_FLOAT
676static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
678#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000679static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680#ifdef HAVE_STRFTIME
681static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
682#endif
683static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000694static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000696static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000697static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000698static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000699static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000700static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000702static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
707static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000719static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000722static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000724static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000725static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static int get_env_len __ARGS((char_u **arg));
727static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000728static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000729static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
730#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
731#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
732 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000733static 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 +0000734static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000735static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000736static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
737static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static typval_T *alloc_tv __ARGS((void));
739static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void init_tv __ARGS((typval_T *varp));
741static long get_tv_number __ARGS((typval_T *varp));
742static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000743static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static char_u *get_tv_string __ARGS((typval_T *varp));
745static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000746static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000748static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
750static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
751static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000752static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
753static 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 +0000754static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
755static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000756static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000757static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000759static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
761static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
762static int eval_fname_script __ARGS((char_u *p));
763static int eval_fname_sid __ARGS((char_u *p));
764static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static ufunc_T *find_func __ARGS((char_u *name));
766static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000767static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_PROFILE
769static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000770static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
771static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
772static int
773# ifdef __BORLANDC__
774 _RTLENTRYF
775# endif
776 prof_total_cmp __ARGS((const void *s1, const void *s2));
777static int
778# ifdef __BORLANDC__
779 _RTLENTRYF
780# endif
781 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000783static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000785static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static void func_free __ARGS((ufunc_T *fp));
787static void func_unref __ARGS((char_u *name));
788static void func_ref __ARGS((char_u *name));
789static 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));
790static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000791static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
792static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000793static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000794static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000795static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000797/* Character used as separated in autoload function/variable names. */
798#define AUTOLOAD_CHAR '#'
799
Bram Moolenaar33570922005-01-25 22:26:29 +0000800/*
801 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000802 */
803 void
804eval_init()
805{
Bram Moolenaar33570922005-01-25 22:26:29 +0000806 int i;
807 struct vimvar *p;
808
809 init_var_dict(&globvardict, &globvars_var);
810 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000811 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000812 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000813
814 for (i = 0; i < VV_LEN; ++i)
815 {
816 p = &vimvars[i];
817 STRCPY(p->vv_di.di_key, p->vv_name);
818 if (p->vv_flags & VV_RO)
819 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
820 else if (p->vv_flags & VV_RO_SBX)
821 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
822 else
823 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000824
825 /* add to v: scope dict, unless the value is not always available */
826 if (p->vv_type != VAR_UNKNOWN)
827 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000828 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000829 /* add to compat scope dict */
830 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000833}
834
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000835#if defined(EXITFREE) || defined(PROTO)
836 void
837eval_clear()
838{
839 int i;
840 struct vimvar *p;
841
842 for (i = 0; i < VV_LEN; ++i)
843 {
844 p = &vimvars[i];
845 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000846 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000847 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000848 p->vv_di.di_tv.vval.v_string = NULL;
849 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000850 }
851 hash_clear(&vimvarht);
852 hash_clear(&compat_hashtab);
853
854 /* script-local variables */
855 for (i = 1; i <= ga_scripts.ga_len; ++i)
856 vars_clear(&SCRIPT_VARS(i));
857 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000858 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000859
860 /* global variables */
861 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000862
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000863 /* autoloaded script names */
864 ga_clear_strings(&ga_loaded);
865
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000866 /* unreferenced lists and dicts */
867 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000868
869 /* functions */
870 free_all_functions();
871 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000872}
873#endif
874
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Return the name of the executed function.
877 */
878 char_u *
879func_name(cookie)
880 void *cookie;
881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000882 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884
885/*
886 * Return the address holding the next breakpoint line for a funccall cookie.
887 */
888 linenr_T *
889func_breakpoint(cookie)
890 void *cookie;
891{
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893}
894
895/*
896 * Return the address holding the debug tick for a funccall cookie.
897 */
898 int *
899func_dbg_tick(cookie)
900 void *cookie;
901{
Bram Moolenaar33570922005-01-25 22:26:29 +0000902 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903}
904
905/*
906 * Return the nesting level for a funccall cookie.
907 */
908 int
909func_level(cookie)
910 void *cookie;
911{
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913}
914
915/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000916funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918/*
919 * Return TRUE when a function was ended by a ":return" command.
920 */
921 int
922current_func_returned()
923{
924 return current_funccal->returned;
925}
926
927
928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * Set an internal variable to a string value. Creates the variable if it does
930 * not already exist.
931 */
932 void
933set_internal_string_var(name, value)
934 char_u *name;
935 char_u *value;
936{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000937 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 val = vim_strsave(value);
941 if (val != NULL)
942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000944 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000946 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949 }
950}
951
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000953static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954static char_u *redir_endp = NULL;
955static char_u *redir_varname = NULL;
956
957/*
958 * Start recording command output to a variable
959 * Returns OK if successfully completed the setup. FAIL otherwise.
960 */
961 int
962var_redir_start(name, append)
963 char_u *name;
964 int append; /* append to an existing variable */
965{
966 int save_emsg;
967 int err;
968 typval_T tv;
969
970 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000971 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000972 {
973 EMSG(_(e_invarg));
974 return FAIL;
975 }
976
977 redir_varname = vim_strsave(name);
978 if (redir_varname == NULL)
979 return FAIL;
980
981 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
982 if (redir_lval == NULL)
983 {
984 var_redir_stop();
985 return FAIL;
986 }
987
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000988 /* The output is stored in growarray "redir_ga" until redirection ends. */
989 ga_init2(&redir_ga, (int)sizeof(char), 500);
990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000991 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000992 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
993 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
995 {
996 if (redir_endp != NULL && *redir_endp != NUL)
997 /* Trailing characters are present after the variable name */
998 EMSG(_(e_trailing));
999 else
1000 EMSG(_(e_invarg));
1001 var_redir_stop();
1002 return FAIL;
1003 }
1004
1005 /* check if we can write to the variable: set it to or append an empty
1006 * string */
1007 save_emsg = did_emsg;
1008 did_emsg = FALSE;
1009 tv.v_type = VAR_STRING;
1010 tv.vval.v_string = (char_u *)"";
1011 if (append)
1012 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1013 else
1014 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1015 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001016 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 if (err)
1018 {
1019 var_redir_stop();
1020 return FAIL;
1021 }
1022 if (redir_lval->ll_newkey != NULL)
1023 {
1024 /* Dictionary item was created, don't do it again. */
1025 vim_free(redir_lval->ll_newkey);
1026 redir_lval->ll_newkey = NULL;
1027 }
1028
1029 return OK;
1030}
1031
1032/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001033 * Append "value[value_len]" to the variable set by var_redir_start().
1034 * The actual appending is postponed until redirection ends, because the value
1035 * appended may in fact be the string we write to, changing it may cause freed
1036 * memory to be used:
1037 * :redir => foo
1038 * :let foo
1039 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 */
1041 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001042var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001044 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001046 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047
1048 if (redir_lval == NULL)
1049 return;
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001052 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001054 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001056 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 {
1058 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001059 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 }
1061 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063}
1064
1065/*
1066 * Stop redirecting command output to a variable.
1067 */
1068 void
1069var_redir_stop()
1070{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001071 typval_T tv;
1072
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (redir_lval != NULL)
1074 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001075 /* Append the trailing NUL. */
1076 ga_append(&redir_ga, NUL);
1077
1078 /* Assign the text to the variable. */
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = redir_ga.ga_data;
1081 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1082 vim_free(tv.vval.v_string);
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 clear_lval(redir_lval);
1085 vim_free(redir_lval);
1086 redir_lval = NULL;
1087 }
1088 vim_free(redir_varname);
1089 redir_varname = NULL;
1090}
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092# if defined(FEAT_MBYTE) || defined(PROTO)
1093 int
1094eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1095 char_u *enc_from;
1096 char_u *enc_to;
1097 char_u *fname_from;
1098 char_u *fname_to;
1099{
1100 int err = FALSE;
1101
1102 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1103 set_vim_var_string(VV_CC_TO, enc_to, -1);
1104 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1105 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1106 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1107 err = TRUE;
1108 set_vim_var_string(VV_CC_FROM, NULL, -1);
1109 set_vim_var_string(VV_CC_TO, NULL, -1);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1112
1113 if (err)
1114 return FAIL;
1115 return OK;
1116}
1117# endif
1118
1119# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1120 int
1121eval_printexpr(fname, args)
1122 char_u *fname;
1123 char_u *args;
1124{
1125 int err = FALSE;
1126
1127 set_vim_var_string(VV_FNAME_IN, fname, -1);
1128 set_vim_var_string(VV_CMDARG, args, -1);
1129 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1130 err = TRUE;
1131 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1132 set_vim_var_string(VV_CMDARG, NULL, -1);
1133
1134 if (err)
1135 {
1136 mch_remove(fname);
1137 return FAIL;
1138 }
1139 return OK;
1140}
1141# endif
1142
1143# if defined(FEAT_DIFF) || defined(PROTO)
1144 void
1145eval_diff(origfile, newfile, outfile)
1146 char_u *origfile;
1147 char_u *newfile;
1148 char_u *outfile;
1149{
1150 int err = FALSE;
1151
1152 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1153 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1154 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1155 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1156 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1157 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1158 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1159}
1160
1161 void
1162eval_patch(origfile, difffile, outfile)
1163 char_u *origfile;
1164 char_u *difffile;
1165 char_u *outfile;
1166{
1167 int err;
1168
1169 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1170 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1171 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1172 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1173 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1174 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1175 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1176}
1177# endif
1178
1179/*
1180 * Top level evaluation function, returning a boolean.
1181 * Sets "error" to TRUE if there was an error.
1182 * Return TRUE or FALSE.
1183 */
1184 int
1185eval_to_bool(arg, error, nextcmd, skip)
1186 char_u *arg;
1187 int *error;
1188 char_u **nextcmd;
1189 int skip; /* only parse, don't execute */
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval = FALSE;
1193
1194 if (skip)
1195 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else
1199 {
1200 *error = FALSE;
1201 if (!skip)
1202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001203 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207 if (skip)
1208 --emsg_skip;
1209
1210 return retval;
1211}
1212
1213/*
1214 * Top level evaluation function, returning a string. If "skip" is TRUE,
1215 * only parsing to "nextcmd" is done, without reporting errors. Return
1216 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1217 */
1218 char_u *
1219eval_to_string_skip(arg, nextcmd, skip)
1220 char_u *arg;
1221 char_u **nextcmd;
1222 int skip; /* only parse, don't execute */
1223{
Bram Moolenaar33570922005-01-25 22:26:29 +00001224 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 char_u *retval;
1226
1227 if (skip)
1228 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 retval = NULL;
1231 else
1232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 retval = vim_strsave(get_tv_string(&tv));
1234 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236 if (skip)
1237 --emsg_skip;
1238
1239 return retval;
1240}
1241
1242/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001243 * Skip over an expression at "*pp".
1244 * Return FAIL for an error, OK otherwise.
1245 */
1246 int
1247skip_expr(pp)
1248 char_u **pp;
1249{
Bram Moolenaar33570922005-01-25 22:26:29 +00001250 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001251
1252 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001253 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001254}
1255
1256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * Top level evaluation function, returning a string.
1258 * Return pointer to allocated memory, or NULL for failure.
1259 */
1260 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 char_u *arg;
1263 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001264 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001268 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 retval = NULL;
1272 else
1273 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001274 if (dolist && tv.v_type == VAR_LIST)
1275 {
1276 ga_init2(&ga, (int)sizeof(char), 80);
1277 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1278 ga_append(&ga, NUL);
1279 retval = (char_u *)ga.ga_data;
1280 }
1281 else
1282 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285
1286 return retval;
1287}
1288
1289/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001290 * Call eval_to_string() without using current local variables and using
1291 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 */
1293 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001294eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 char_u *arg;
1296 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001297 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 char_u *retval;
1300 void *save_funccalp;
1301
1302 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001303 if (use_sandbox)
1304 ++sandbox;
1305 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001306 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001307 if (use_sandbox)
1308 --sandbox;
1309 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 restore_funccal(save_funccalp);
1311 return retval;
1312}
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314/*
1315 * Top level evaluation function, returning a number.
1316 * Evaluates "expr" silently.
1317 * Returns -1 for an error.
1318 */
1319 int
1320eval_to_number(expr)
1321 char_u *expr;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001325 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327 ++emsg_off;
1328
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 retval = -1;
1331 else
1332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001333 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 --emsg_off;
1337
1338 return retval;
1339}
1340
Bram Moolenaara40058a2005-07-11 22:42:07 +00001341/*
1342 * Prepare v: variable "idx" to be used.
1343 * Save the current typeval in "save_tv".
1344 * When not used yet add the variable to the v: hashtable.
1345 */
1346 static void
1347prepare_vimvar(idx, save_tv)
1348 int idx;
1349 typval_T *save_tv;
1350{
1351 *save_tv = vimvars[idx].vv_tv;
1352 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1353 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1354}
1355
1356/*
1357 * Restore v: variable "idx" to typeval "save_tv".
1358 * When no longer defined, remove the variable from the v: hashtable.
1359 */
1360 static void
1361restore_vimvar(idx, save_tv)
1362 int idx;
1363 typval_T *save_tv;
1364{
1365 hashitem_T *hi;
1366
Bram Moolenaara40058a2005-07-11 22:42:07 +00001367 vimvars[idx].vv_tv = *save_tv;
1368 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1369 {
1370 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1371 if (HASHITEM_EMPTY(hi))
1372 EMSG2(_(e_intern2), "restore_vimvar()");
1373 else
1374 hash_remove(&vimvarht, hi);
1375 }
1376}
1377
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001378#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001379/*
1380 * Evaluate an expression to a list with suggestions.
1381 * For the "expr:" part of 'spellsuggest'.
1382 */
1383 list_T *
1384eval_spell_expr(badword, expr)
1385 char_u *badword;
1386 char_u *expr;
1387{
1388 typval_T save_val;
1389 typval_T rettv;
1390 list_T *list = NULL;
1391 char_u *p = skipwhite(expr);
1392
1393 /* Set "v:val" to the bad word. */
1394 prepare_vimvar(VV_VAL, &save_val);
1395 vimvars[VV_VAL].vv_type = VAR_STRING;
1396 vimvars[VV_VAL].vv_str = badword;
1397 if (p_verbose == 0)
1398 ++emsg_off;
1399
1400 if (eval1(&p, &rettv, TRUE) == OK)
1401 {
1402 if (rettv.v_type != VAR_LIST)
1403 clear_tv(&rettv);
1404 else
1405 list = rettv.vval.v_list;
1406 }
1407
1408 if (p_verbose == 0)
1409 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001410 restore_vimvar(VV_VAL, &save_val);
1411
1412 return list;
1413}
1414
1415/*
1416 * "list" is supposed to contain two items: a word and a number. Return the
1417 * word in "pp" and the number as the return value.
1418 * Return -1 if anything isn't right.
1419 * Used to get the good word and score from the eval_spell_expr() result.
1420 */
1421 int
1422get_spellword(list, pp)
1423 list_T *list;
1424 char_u **pp;
1425{
1426 listitem_T *li;
1427
1428 li = list->lv_first;
1429 if (li == NULL)
1430 return -1;
1431 *pp = get_tv_string(&li->li_tv);
1432
1433 li = li->li_next;
1434 if (li == NULL)
1435 return -1;
1436 return get_tv_number(&li->li_tv);
1437}
1438#endif
1439
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001440/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001441 * Top level evaluation function.
1442 * Returns an allocated typval_T with the result.
1443 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001444 */
1445 typval_T *
1446eval_expr(arg, nextcmd)
1447 char_u *arg;
1448 char_u **nextcmd;
1449{
1450 typval_T *tv;
1451
1452 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001453 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001454 {
1455 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001456 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001457 }
1458
1459 return tv;
1460}
1461
1462
Bram Moolenaar4f688582007-07-24 12:34:30 +00001463#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1464 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001466 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001467 * Uses argv[argc] for the function arguments. Only Number and String
1468 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001471 static int
1472call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 char_u *func;
1474 int argc;
1475 char_u **argv;
1476 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar33570922005-01-25 22:26:29 +00001479 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 long n;
1481 int len;
1482 int i;
1483 int doesrange;
1484 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001487 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 for (i = 0; i < argc; i++)
1492 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001493 /* Pass a NULL or empty argument as an empty string */
1494 if (argv[i] == NULL || *argv[i] == NUL)
1495 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001496 argvars[i].v_type = VAR_STRING;
1497 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001498 continue;
1499 }
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 /* Recognize a number argument, the others must be strings. */
1502 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1503 if (len != 0 && len == (int)STRLEN(argv[i]))
1504 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001505 argvars[i].v_type = VAR_NUMBER;
1506 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 else
1509 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001510 argvars[i].v_type = VAR_STRING;
1511 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 }
1514
1515 if (safe)
1516 {
1517 save_funccalp = save_funccal();
1518 ++sandbox;
1519 }
1520
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001521 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1522 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001524 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (safe)
1526 {
1527 --sandbox;
1528 restore_funccal(save_funccalp);
1529 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001530 vim_free(argvars);
1531
1532 if (ret == FAIL)
1533 clear_tv(rettv);
1534
1535 return ret;
1536}
1537
Bram Moolenaar4f688582007-07-24 12:34:30 +00001538# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001539/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001540 * Call vimL function "func" and return the result as a string.
1541 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001542 * Uses argv[argc] for the function arguments.
1543 */
1544 void *
1545call_func_retstr(func, argc, argv, safe)
1546 char_u *func;
1547 int argc;
1548 char_u **argv;
1549 int safe; /* use the sandbox */
1550{
1551 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001552 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553
1554 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1555 return NULL;
1556
1557 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return retval;
1560}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001561# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562
Bram Moolenaar4f688582007-07-24 12:34:30 +00001563# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001565 * Call vimL function "func" and return the result as a number.
1566 * Returns -1 when calling the function fails.
1567 * Uses argv[argc] for the function arguments.
1568 */
1569 long
1570call_func_retnr(func, argc, argv, safe)
1571 char_u *func;
1572 int argc;
1573 char_u **argv;
1574 int safe; /* use the sandbox */
1575{
1576 typval_T rettv;
1577 long retval;
1578
1579 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1580 return -1;
1581
1582 retval = get_tv_number_chk(&rettv, NULL);
1583 clear_tv(&rettv);
1584 return retval;
1585}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001586# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001587
1588/*
1589 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Uses argv[argc] for the function arguments.
1591 */
1592 void *
1593call_func_retlist(func, argc, argv, safe)
1594 char_u *func;
1595 int argc;
1596 char_u **argv;
1597 int safe; /* use the sandbox */
1598{
1599 typval_T rettv;
1600
1601 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1602 return NULL;
1603
1604 if (rettv.v_type != VAR_LIST)
1605 {
1606 clear_tv(&rettv);
1607 return NULL;
1608 }
1609
1610 return rettv.vval.v_list;
1611}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#endif
1613
Bram Moolenaar4f688582007-07-24 12:34:30 +00001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
1616 * Save the current function call pointer, and set it to NULL.
1617 * Used when executing autocommands and for ":source".
1618 */
1619 void *
1620save_funccal()
1621{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001622 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 current_funccal = NULL;
1625 return (void *)fc;
1626}
1627
1628 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001629restore_funccal(vfc)
1630 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001632 funccall_T *fc = (funccall_T *)vfc;
1633
1634 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635}
1636
Bram Moolenaar05159a02005-02-26 23:04:13 +00001637#if defined(FEAT_PROFILE) || defined(PROTO)
1638/*
1639 * Prepare profiling for entering a child or something else that is not
1640 * counted for the script/function itself.
1641 * Should always be called in pair with prof_child_exit().
1642 */
1643 void
1644prof_child_enter(tm)
1645 proftime_T *tm; /* place to store waittime */
1646{
1647 funccall_T *fc = current_funccal;
1648
1649 if (fc != NULL && fc->func->uf_profiling)
1650 profile_start(&fc->prof_child);
1651 script_prof_save(tm);
1652}
1653
1654/*
1655 * Take care of time spent in a child.
1656 * Should always be called after prof_child_enter().
1657 */
1658 void
1659prof_child_exit(tm)
1660 proftime_T *tm; /* where waittime was stored */
1661{
1662 funccall_T *fc = current_funccal;
1663
1664 if (fc != NULL && fc->func->uf_profiling)
1665 {
1666 profile_end(&fc->prof_child);
1667 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1668 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1669 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1670 }
1671 script_prof_restore(tm);
1672}
1673#endif
1674
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#ifdef FEAT_FOLDING
1677/*
1678 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1679 * it in "*cp". Doesn't give error messages.
1680 */
1681 int
1682eval_foldexpr(arg, cp)
1683 char_u *arg;
1684 int *cp;
1685{
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 int retval;
1688 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001689 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1690 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001693 if (use_sandbox)
1694 ++sandbox;
1695 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 retval = 0;
1699 else
1700 {
1701 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702 if (tv.v_type == VAR_NUMBER)
1703 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001704 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 retval = 0;
1706 else
1707 {
1708 /* If the result is a string, check if there is a non-digit before
1709 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001710 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (!VIM_ISDIGIT(*s) && *s != '-')
1712 *cp = *s++;
1713 retval = atol((char *)s);
1714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001715 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001718 if (use_sandbox)
1719 --sandbox;
1720 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 return retval;
1723}
1724#endif
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 * ":let" list all variable values
1728 * ":let var1 var2" list variable values
1729 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 * ":let var += expr" assignment command.
1731 * ":let var -= expr" assignment command.
1732 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 void
1736ex_let(eap)
1737 exarg_T *eap;
1738{
1739 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001741 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 int var_count = 0;
1744 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001746 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001747 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaardb552d602006-03-23 22:59:57 +00001749 argend = skip_var_list(arg, &var_count, &semicolon);
1750 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001752 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1753 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001754 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001757 /*
1758 * ":let" without "=": list variables
1759 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 if (*arg == '[')
1761 EMSG(_(e_invarg));
1762 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001764 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001768 list_glob_vars(&first);
1769 list_buf_vars(&first);
1770 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001771#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001772 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001773#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001774 list_script_vars(&first);
1775 list_func_vars(&first);
1776 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 eap->nextcmd = check_nextcmd(arg);
1779 }
1780 else
1781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 op[0] = '=';
1783 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001784 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 {
1786 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1787 op[0] = expr[-1]; /* +=, -= or .= */
1788 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (eap->skip)
1792 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (eap->skip)
1795 {
1796 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 --emsg_skip;
1799 }
1800 else if (i != FAIL)
1801 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 }
1807}
1808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809/*
1810 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1811 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 * When "nextchars" is not NULL it points to a string with characters that
1813 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1814 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 * Returns OK or FAIL;
1816 */
1817 static int
1818ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1819 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 int copy; /* copy values from "tv", don't move */
1822 int semicolon; /* from skip_var_list() */
1823 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825{
1826 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 listitem_T *item;
1830 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831
1832 if (*arg != '[')
1833 {
1834 /*
1835 * ":let var = expr" or ":for var in list"
1836 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 return FAIL;
1839 return OK;
1840 }
1841
1842 /*
1843 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1844 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001845 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 {
1847 EMSG(_(e_listreq));
1848 return FAIL;
1849 }
1850
1851 i = list_len(l);
1852 if (semicolon == 0 && var_count < i)
1853 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001854 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return FAIL;
1856 }
1857 if (var_count - semicolon > i)
1858 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001859 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 return FAIL;
1861 }
1862
1863 item = l->lv_first;
1864 while (*arg != ']')
1865 {
1866 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 item = item->li_next;
1869 if (arg == NULL)
1870 return FAIL;
1871
1872 arg = skipwhite(arg);
1873 if (*arg == ';')
1874 {
1875 /* Put the rest of the list (may be empty) in the var after ';'.
1876 * Create a new list for this. */
1877 l = list_alloc();
1878 if (l == NULL)
1879 return FAIL;
1880 while (item != NULL)
1881 {
1882 list_append_tv(l, &item->li_tv);
1883 item = item->li_next;
1884 }
1885
1886 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001887 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 ltv.vval.v_list = l;
1889 l->lv_refcount = 1;
1890
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1892 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 clear_tv(&ltv);
1894 if (arg == NULL)
1895 return FAIL;
1896 break;
1897 }
1898 else if (*arg != ',' && *arg != ']')
1899 {
1900 EMSG2(_(e_intern2), "ex_let_vars()");
1901 return FAIL;
1902 }
1903 }
1904
1905 return OK;
1906}
1907
1908/*
1909 * Skip over assignable variable "var" or list of variables "[var, var]".
1910 * Used for ":let varvar = expr" and ":for varvar in expr".
1911 * For "[var, var]" increment "*var_count" for each variable.
1912 * for "[var, var; var]" set "semicolon".
1913 * Return NULL for an error.
1914 */
1915 static char_u *
1916skip_var_list(arg, var_count, semicolon)
1917 char_u *arg;
1918 int *var_count;
1919 int *semicolon;
1920{
1921 char_u *p, *s;
1922
1923 if (*arg == '[')
1924 {
1925 /* "[var, var]": find the matching ']'. */
1926 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001927 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 {
1929 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1930 s = skip_var_one(p);
1931 if (s == p)
1932 {
1933 EMSG2(_(e_invarg2), p);
1934 return NULL;
1935 }
1936 ++*var_count;
1937
1938 p = skipwhite(s);
1939 if (*p == ']')
1940 break;
1941 else if (*p == ';')
1942 {
1943 if (*semicolon == 1)
1944 {
1945 EMSG(_("Double ; in list of variables"));
1946 return NULL;
1947 }
1948 *semicolon = 1;
1949 }
1950 else if (*p != ',')
1951 {
1952 EMSG2(_(e_invarg2), p);
1953 return NULL;
1954 }
1955 }
1956 return p + 1;
1957 }
1958 else
1959 return skip_var_one(arg);
1960}
1961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001963 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001964 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 static char_u *
1967skip_var_one(arg)
1968 char_u *arg;
1969{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001970 if (*arg == '@' && arg[1] != NUL)
1971 return arg + 2;
1972 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1973 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974}
1975
Bram Moolenaara7043832005-01-21 11:56:39 +00001976/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001977 * List variables for hashtab "ht" with prefix "prefix".
1978 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001985 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001986{
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 hashitem_T *hi;
1988 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 int todo;
1990
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001991 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001992 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1993 {
1994 if (!HASHITEM_EMPTY(hi))
1995 {
1996 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001997 di = HI2DI(hi);
1998 if (empty || di->di_tv.v_type != VAR_STRING
1999 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002000 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002001 }
2002 }
2003}
2004
2005/*
2006 * List global variables.
2007 */
2008 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002009list_glob_vars(first)
2010 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002011{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002012 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002013}
2014
2015/*
2016 * List buffer variables.
2017 */
2018 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002019list_buf_vars(first)
2020 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002021{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 char_u numbuf[NUMBUFLEN];
2023
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002024 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2025 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002026
2027 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002028 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2029 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002030}
2031
2032/*
2033 * List window variables.
2034 */
2035 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002036list_win_vars(first)
2037 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002038{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002039 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2040 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002041}
2042
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002043#ifdef FEAT_WINDOWS
2044/*
2045 * List tab page variables.
2046 */
2047 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048list_tab_vars(first)
2049 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002050{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2052 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002053}
2054#endif
2055
Bram Moolenaara7043832005-01-21 11:56:39 +00002056/*
2057 * List Vim variables.
2058 */
2059 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002060list_vim_vars(first)
2061 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002063 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064}
2065
2066/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002067 * List script-local variables, if there is a script.
2068 */
2069 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002070list_script_vars(first)
2071 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002072{
2073 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002074 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2075 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002076}
2077
2078/*
2079 * List function variables, if there is a function.
2080 */
2081 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082list_func_vars(first)
2083 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002084{
2085 if (current_funccal != NULL)
2086 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002088}
2089
2090/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 * List variables in "arg".
2092 */
2093 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 exarg_T *eap;
2096 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098{
2099 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002102 char_u *name_start;
2103 char_u *arg_subsc;
2104 char_u *tofree;
2105 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106
2107 while (!ends_excmd(*arg) && !got_int)
2108 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002111 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2113 {
2114 emsg_severe = TRUE;
2115 EMSG(_(e_trailing));
2116 break;
2117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 /* get_name_len() takes care of expanding curly braces */
2122 name_start = name = arg;
2123 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2124 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 /* This is mainly to keep test 49 working: when expanding
2127 * curly braces fails overrule the exception error message. */
2128 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 emsg_severe = TRUE;
2131 EMSG2(_(e_invarg2), arg);
2132 break;
2133 }
2134 error = TRUE;
2135 }
2136 else
2137 {
2138 if (tofree != NULL)
2139 name = tofree;
2140 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 else
2143 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144 /* handle d.key, l[idx], f(expr) */
2145 arg_subsc = arg;
2146 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002152 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 case 'g': list_glob_vars(first); break;
2155 case 'b': list_buf_vars(first); break;
2156 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 case 'v': list_vim_vars(first); break;
2161 case 's': list_script_vars(first); break;
2162 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 default:
2164 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002165 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002166 }
2167 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168 {
2169 char_u numbuf[NUMBUFLEN];
2170 char_u *tf;
2171 int c;
2172 char_u *s;
2173
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002174 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 c = *arg;
2176 *arg = NUL;
2177 list_one_var_a((char_u *)"",
2178 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 tv.v_type,
2180 s == NULL ? (char_u *)"" : s,
2181 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 *arg = c;
2183 vim_free(tf);
2184 }
2185 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002189
2190 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192
2193 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 }
2195
2196 return arg;
2197}
2198
2199/*
2200 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2201 * Returns a pointer to the char just after the var name.
2202 * Returns NULL if there is an error.
2203 */
2204 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002207 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002209 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int c1;
2213 char_u *name;
2214 char_u *p;
2215 char_u *arg_end = NULL;
2216 int len;
2217 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002218 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 /*
2221 * ":let $VAR = expr": Set environment variable.
2222 */
2223 if (*arg == '$')
2224 {
2225 /* Find the end of the name. */
2226 ++arg;
2227 name = arg;
2228 len = get_env_len(&arg);
2229 if (len == 0)
2230 EMSG2(_(e_invarg2), name - 1);
2231 else
2232 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002233 if (op != NULL && (*op == '+' || *op == '-'))
2234 EMSG2(_(e_letwrong), op);
2235 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002236 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 EMSG(_(e_letunexp));
2238 else
2239 {
2240 c1 = name[len];
2241 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 p = get_tv_string_chk(tv);
2243 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 {
2245 int mustfree = FALSE;
2246 char_u *s = vim_getenv(name, &mustfree);
2247
2248 if (s != NULL)
2249 {
2250 p = tofree = concat_str(s, p);
2251 if (mustfree)
2252 vim_free(s);
2253 }
2254 }
2255 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002256 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002258 if (STRICMP(name, "HOME") == 0)
2259 init_homedir();
2260 else if (didset_vim && STRICMP(name, "VIM") == 0)
2261 didset_vim = FALSE;
2262 else if (didset_vimruntime
2263 && STRICMP(name, "VIMRUNTIME") == 0)
2264 didset_vimruntime = FALSE;
2265 arg_end = arg;
2266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
2270 }
2271 }
2272
2273 /*
2274 * ":let &option = expr": Set option value.
2275 * ":let &l:option = expr": Set local option value.
2276 * ":let &g:option = expr": Set global option value.
2277 */
2278 else if (*arg == '&')
2279 {
2280 /* Find the end of the name. */
2281 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282 if (p == NULL || (endchars != NULL
2283 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 EMSG(_(e_letunexp));
2285 else
2286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002287 long n;
2288 int opt_type;
2289 long numval;
2290 char_u *stringval = NULL;
2291 char_u *s;
2292
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 c1 = *p;
2294 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295
2296 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 s = get_tv_string_chk(tv); /* != NULL if number or string */
2298 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 {
2300 opt_type = get_option_value(arg, &numval,
2301 &stringval, opt_flags);
2302 if ((opt_type == 1 && *op == '.')
2303 || (opt_type == 0 && *op != '.'))
2304 EMSG2(_(e_letwrong), op);
2305 else
2306 {
2307 if (opt_type == 1) /* number */
2308 {
2309 if (*op == '+')
2310 n = numval + n;
2311 else
2312 n = numval - n;
2313 }
2314 else if (opt_type == 0 && stringval != NULL) /* string */
2315 {
2316 s = concat_str(stringval, s);
2317 vim_free(stringval);
2318 stringval = s;
2319 }
2320 }
2321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002322 if (s != NULL)
2323 {
2324 set_option_value(arg, n, s, opt_flags);
2325 arg_end = p;
2326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330 }
2331
2332 /*
2333 * ":let @r = expr": Set register contents.
2334 */
2335 else if (*arg == '@')
2336 {
2337 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 if (op != NULL && (*op == '+' || *op == '-'))
2339 EMSG2(_(e_letwrong), op);
2340 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002341 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 EMSG(_(e_letunexp));
2343 else
2344 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002345 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *s;
2347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 p = get_tv_string_chk(tv);
2349 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002351 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 if (s != NULL)
2353 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002354 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 vim_free(s);
2356 }
2357 }
2358 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002361 arg_end = arg + 1;
2362 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002363 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
2365 }
2366
2367 /*
2368 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002371 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002373 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002375 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2379 EMSG(_(e_letunexp));
2380 else
2381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 arg_end = p;
2384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 }
2388
2389 else
2390 EMSG2(_(e_invarg2), arg);
2391
2392 return arg_end;
2393}
2394
2395/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2397 */
2398 static int
2399check_changedtick(arg)
2400 char_u *arg;
2401{
2402 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2403 {
2404 EMSG2(_(e_readonlyvar), arg);
2405 return TRUE;
2406 }
2407 return FALSE;
2408}
2409
2410/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 * Get an lval: variable, Dict item or List item that can be assigned a value
2412 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2413 * "name.key", "name.key[expr]" etc.
2414 * Indexing only works if "name" is an existing List or Dictionary.
2415 * "name" points to the start of the name.
2416 * If "rettv" is not NULL it points to the value to be assigned.
2417 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2418 * wrong; must end in space or cmd separator.
2419 *
2420 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002421 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 */
2424 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002425get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002427 typval_T *rettv;
2428 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 int unlet;
2430 int skip;
2431 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002432 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 char_u *expr_start, *expr_end;
2436 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002437 dictitem_T *v;
2438 typval_T var1;
2439 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002441 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002444 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448
2449 if (skip)
2450 {
2451 /* When skipping just find the end of the name. */
2452 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002453 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 }
2455
2456 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (expr_start != NULL)
2459 {
2460 /* Don't expand the name when we already know there is an error. */
2461 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2462 && *p != '[' && *p != '.')
2463 {
2464 EMSG(_(e_trailing));
2465 return NULL;
2466 }
2467
2468 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2469 if (lp->ll_exp_name == NULL)
2470 {
2471 /* Report an invalid expression in braces, unless the
2472 * expression evaluation has been cancelled due to an
2473 * aborting error, an interrupt, or an exception. */
2474 if (!aborting() && !quiet)
2475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002476 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 EMSG2(_(e_invarg2), name);
2478 return NULL;
2479 }
2480 }
2481 lp->ll_name = lp->ll_exp_name;
2482 }
2483 else
2484 lp->ll_name = name;
2485
2486 /* Without [idx] or .key we are done. */
2487 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2488 return p;
2489
2490 cc = *p;
2491 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002492 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (v == NULL && !quiet)
2494 EMSG2(_(e_undefvar), lp->ll_name);
2495 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 if (v == NULL)
2497 return NULL;
2498
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 /*
2500 * Loop until no more [idx] or .key is following.
2501 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002502 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2506 && !(lp->ll_tv->v_type == VAR_DICT
2507 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (!quiet)
2510 EMSG(_("E689: Can only index a List or Dictionary"));
2511 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (!quiet)
2516 EMSG(_("E708: [:] must come last"));
2517 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 len = -1;
2521 if (*p == '.')
2522 {
2523 key = p + 1;
2524 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2525 ;
2526 if (len == 0)
2527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (!quiet)
2529 EMSG(_(e_emptykey));
2530 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 }
2532 p = key + len;
2533 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 else
2535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002537 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 if (*p == ':')
2539 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002540 else
2541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 empty1 = FALSE;
2543 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 if (get_tv_string_chk(&var1) == NULL)
2546 {
2547 /* not a number or string */
2548 clear_tv(&var1);
2549 return NULL;
2550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 }
2552
2553 /* Optionally get the second index [ :expr]. */
2554 if (*p == ':')
2555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 if (!empty1)
2561 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002563 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (rettv != NULL && (rettv->v_type != VAR_LIST
2565 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (!quiet)
2568 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 if (!empty1)
2570 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 }
2573 p = skipwhite(p + 1);
2574 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 else
2577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2580 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 if (!empty1)
2582 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 if (get_tv_string_chk(&var2) == NULL)
2586 {
2587 /* not a number or string */
2588 if (!empty1)
2589 clear_tv(&var1);
2590 clear_tv(&var2);
2591 return NULL;
2592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002598
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 if (!empty1)
2604 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609
2610 /* Skip to past ']'. */
2611 ++p;
2612 }
2613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 {
2616 if (len == -1)
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 if (*key == NUL)
2621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (!quiet)
2623 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 }
2627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002628 lp->ll_list = NULL;
2629 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002630 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002633 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002637 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 if (len == -1)
2639 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 if (len == -1)
2647 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 p = NULL;
2650 break;
2651 }
2652 if (len == -1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 else
2657 {
2658 /*
2659 * Get the number and item for the only or first index of the List.
2660 */
2661 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 else
2664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002665 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 clear_tv(&var1);
2667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_list = lp->ll_tv->vval.v_list;
2670 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2671 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002673 if (lp->ll_n1 < 0)
2674 {
2675 lp->ll_n1 = 0;
2676 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2677 }
2678 }
2679 if (lp->ll_li == NULL)
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /*
2687 * May need to find the item or absolute index for the second
2688 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 * When no index given: "lp->ll_empty2" is TRUE.
2690 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2705 if (lp->ll_n1 < 0)
2706 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2707 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713 }
2714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return p;
2716}
2717
2718/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002719 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 */
2721 static void
2722clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002723 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724{
2725 vim_free(lp->ll_exp_name);
2726 vim_free(lp->ll_newkey);
2727}
2728
2729/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002730 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002732 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 */
2734 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002736 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002738 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002740 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741{
2742 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 listitem_T *ri;
2744 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745
2746 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 cc = *endp;
2751 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 if (op != NULL && *op != '=')
2753 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002756 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002757 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002758 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 {
2760 if (tv_op(&tv, rettv, op) == OK)
2761 set_var(lp->ll_name, &tv, FALSE);
2762 clear_tv(&tv);
2763 }
2764 }
2765 else
2766 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002768 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002770 else if (tv_check_lock(lp->ll_newkey == NULL
2771 ? lp->ll_tv->v_lock
2772 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2773 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 else if (lp->ll_range)
2775 {
2776 /*
2777 * Assign the List values to the list items.
2778 */
2779 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002781 if (op != NULL && *op != '=')
2782 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2783 else
2784 {
2785 clear_tv(&lp->ll_li->li_tv);
2786 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2787 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 ri = ri->li_next;
2789 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2790 break;
2791 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002794 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 ri = NULL;
2797 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_li = lp->ll_li->li_next;
2801 ++lp->ll_n1;
2802 }
2803 if (ri != NULL)
2804 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002805 else if (lp->ll_empty2
2806 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 : lp->ll_n1 != lp->ll_n2)
2808 EMSG(_("E711: List value has not enough items"));
2809 }
2810 else
2811 {
2812 /*
2813 * Assign to a List or Dictionary item.
2814 */
2815 if (lp->ll_newkey != NULL)
2816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 if (op != NULL && *op != '=')
2818 {
2819 EMSG2(_(e_letwrong), op);
2820 return;
2821 }
2822
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002824 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (di == NULL)
2826 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002827 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2828 {
2829 vim_free(di);
2830 return;
2831 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002833 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002834 else if (op != NULL && *op != '=')
2835 {
2836 tv_op(lp->ll_tv, rettv, op);
2837 return;
2838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002839 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 /*
2843 * Assign the value to the variable or list item.
2844 */
2845 if (copy)
2846 copy_tv(rettv, lp->ll_tv);
2847 else
2848 {
2849 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002850 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002852 }
2853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854}
2855
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2858 * Returns OK or FAIL.
2859 */
2860 static int
2861tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 typval_T *tv1;
2863 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864 char_u *op;
2865{
2866 long n;
2867 char_u numbuf[NUMBUFLEN];
2868 char_u *s;
2869
2870 /* Can't do anything with a Funcref or a Dict on the right. */
2871 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2872 {
2873 switch (tv1->v_type)
2874 {
2875 case VAR_DICT:
2876 case VAR_FUNC:
2877 break;
2878
2879 case VAR_LIST:
2880 if (*op != '+' || tv2->v_type != VAR_LIST)
2881 break;
2882 /* List += List */
2883 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2884 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2885 return OK;
2886
2887 case VAR_NUMBER:
2888 case VAR_STRING:
2889 if (tv2->v_type == VAR_LIST)
2890 break;
2891 if (*op == '+' || *op == '-')
2892 {
2893 /* nr += nr or nr -= nr*/
2894 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002895#ifdef FEAT_FLOAT
2896 if (tv2->v_type == VAR_FLOAT)
2897 {
2898 float_T f = n;
2899
2900 if (*op == '+')
2901 f += tv2->vval.v_float;
2902 else
2903 f -= tv2->vval.v_float;
2904 clear_tv(tv1);
2905 tv1->v_type = VAR_FLOAT;
2906 tv1->vval.v_float = f;
2907 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002909#endif
2910 {
2911 if (*op == '+')
2912 n += get_tv_number(tv2);
2913 else
2914 n -= get_tv_number(tv2);
2915 clear_tv(tv1);
2916 tv1->v_type = VAR_NUMBER;
2917 tv1->vval.v_number = n;
2918 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 }
2920 else
2921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002922 if (tv2->v_type == VAR_FLOAT)
2923 break;
2924
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 /* str .= str */
2926 s = get_tv_string(tv1);
2927 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2928 clear_tv(tv1);
2929 tv1->v_type = VAR_STRING;
2930 tv1->vval.v_string = s;
2931 }
2932 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002933
2934#ifdef FEAT_FLOAT
2935 case VAR_FLOAT:
2936 {
2937 float_T f;
2938
2939 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2940 && tv2->v_type != VAR_NUMBER
2941 && tv2->v_type != VAR_STRING))
2942 break;
2943 if (tv2->v_type == VAR_FLOAT)
2944 f = tv2->vval.v_float;
2945 else
2946 f = get_tv_number(tv2);
2947 if (*op == '+')
2948 tv1->vval.v_float += f;
2949 else
2950 tv1->vval.v_float -= f;
2951 }
2952 return OK;
2953#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 }
2955 }
2956
2957 EMSG2(_(e_letwrong), op);
2958 return FAIL;
2959}
2960
2961/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002962 * Add a watcher to a list.
2963 */
2964 static void
2965list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 list_T *l;
2967 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968{
2969 lw->lw_next = l->lv_watch;
2970 l->lv_watch = lw;
2971}
2972
2973/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002974 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 * No warning when it isn't found...
2976 */
2977 static void
2978list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002979 list_T *l;
2980 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981{
Bram Moolenaar33570922005-01-25 22:26:29 +00002982 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002983
2984 lwp = &l->lv_watch;
2985 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2986 {
2987 if (lw == lwrem)
2988 {
2989 *lwp = lw->lw_next;
2990 break;
2991 }
2992 lwp = &lw->lw_next;
2993 }
2994}
2995
2996/*
2997 * Just before removing an item from a list: advance watchers to the next
2998 * item.
2999 */
3000 static void
3001list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 list_T *l;
3003 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004{
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006
3007 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3008 if (lw->lw_item == item)
3009 lw->lw_item = item->li_next;
3010}
3011
3012/*
3013 * Evaluate the expression used in a ":for var in expr" command.
3014 * "arg" points to "var".
3015 * Set "*errp" to TRUE for an error, FALSE otherwise;
3016 * Return a pointer that holds the info. Null when there is an error.
3017 */
3018 void *
3019eval_for_line(arg, errp, nextcmdp, skip)
3020 char_u *arg;
3021 int *errp;
3022 char_u **nextcmdp;
3023 int skip;
3024{
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T tv;
3028 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029
3030 *errp = TRUE; /* default: there is an error */
3031
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003033 if (fi == NULL)
3034 return NULL;
3035
3036 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3037 if (expr == NULL)
3038 return fi;
3039
3040 expr = skipwhite(expr);
3041 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3042 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003043 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 return fi;
3045 }
3046
3047 if (skip)
3048 ++emsg_skip;
3049 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3050 {
3051 *errp = FALSE;
3052 if (!skip)
3053 {
3054 l = tv.vval.v_list;
3055 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003056 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003057 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003058 clear_tv(&tv);
3059 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003060 else
3061 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003062 /* No need to increment the refcount, it's already set for the
3063 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 fi->fi_list = l;
3065 list_add_watch(l, &fi->fi_lw);
3066 fi->fi_lw.lw_item = l->lv_first;
3067 }
3068 }
3069 }
3070 if (skip)
3071 --emsg_skip;
3072
3073 return fi;
3074}
3075
3076/*
3077 * Use the first item in a ":for" list. Advance to the next.
3078 * Assign the values to the variable (list). "arg" points to the first one.
3079 * Return TRUE when a valid item was found, FALSE when at end of list or
3080 * something wrong.
3081 */
3082 int
3083next_for_item(fi_void, arg)
3084 void *fi_void;
3085 char_u *arg;
3086{
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090
3091 item = fi->fi_lw.lw_item;
3092 if (item == NULL)
3093 result = FALSE;
3094 else
3095 {
3096 fi->fi_lw.lw_item = item->li_next;
3097 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3098 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3099 }
3100 return result;
3101}
3102
3103/*
3104 * Free the structure used to store info used by ":for".
3105 */
3106 void
3107free_for_info(fi_void)
3108 void *fi_void;
3109{
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003112 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003113 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003115 list_unref(fi->fi_list);
3116 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 vim_free(fi);
3118}
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3121
3122 void
3123set_context_for_expression(xp, arg, cmdidx)
3124 expand_T *xp;
3125 char_u *arg;
3126 cmdidx_T cmdidx;
3127{
3128 int got_eq = FALSE;
3129 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 if (cmdidx == CMD_let)
3133 {
3134 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003135 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136 {
3137 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003138 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139 {
3140 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142 if (vim_iswhite(*p))
3143 break;
3144 }
3145 return;
3146 }
3147 }
3148 else
3149 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3150 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 while ((xp->xp_pattern = vim_strpbrk(arg,
3152 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3153 {
3154 c = *xp->xp_pattern;
3155 if (c == '&')
3156 {
3157 c = xp->xp_pattern[1];
3158 if (c == '&')
3159 {
3160 ++xp->xp_pattern;
3161 xp->xp_context = cmdidx != CMD_let || got_eq
3162 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3163 }
3164 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003167 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3168 xp->xp_pattern += 2;
3169
3170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 else if (c == '$')
3173 {
3174 /* environment variable */
3175 xp->xp_context = EXPAND_ENV_VARS;
3176 }
3177 else if (c == '=')
3178 {
3179 got_eq = TRUE;
3180 xp->xp_context = EXPAND_EXPRESSION;
3181 }
3182 else if (c == '<'
3183 && xp->xp_context == EXPAND_FUNCTIONS
3184 && vim_strchr(xp->xp_pattern, '(') == NULL)
3185 {
3186 /* Function name can start with "<SNR>" */
3187 break;
3188 }
3189 else if (cmdidx != CMD_let || got_eq)
3190 {
3191 if (c == '"') /* string */
3192 {
3193 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3194 if (c == '\\' && xp->xp_pattern[1] != NUL)
3195 ++xp->xp_pattern;
3196 xp->xp_context = EXPAND_NOTHING;
3197 }
3198 else if (c == '\'') /* literal string */
3199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003200 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3202 /* skip */ ;
3203 xp->xp_context = EXPAND_NOTHING;
3204 }
3205 else if (c == '|')
3206 {
3207 if (xp->xp_pattern[1] == '|')
3208 {
3209 ++xp->xp_pattern;
3210 xp->xp_context = EXPAND_EXPRESSION;
3211 }
3212 else
3213 xp->xp_context = EXPAND_COMMANDS;
3214 }
3215 else
3216 xp->xp_context = EXPAND_EXPRESSION;
3217 }
3218 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 /* Doesn't look like something valid, expand as an expression
3220 * anyway. */
3221 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 arg = xp->xp_pattern;
3223 if (*arg != NUL)
3224 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3225 /* skip */ ;
3226 }
3227 xp->xp_pattern = arg;
3228}
3229
3230#endif /* FEAT_CMDL_COMPL */
3231
3232/*
3233 * ":1,25call func(arg1, arg2)" function call.
3234 */
3235 void
3236ex_call(eap)
3237 exarg_T *eap;
3238{
3239 char_u *arg = eap->arg;
3240 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003242 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 linenr_T lnum;
3246 int doesrange;
3247 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003250 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003251 if (fudi.fd_newkey != NULL)
3252 {
3253 /* Still need to give an error message for missing key. */
3254 EMSG2(_(e_dictkey), fudi.fd_newkey);
3255 vim_free(fudi.fd_newkey);
3256 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003257 if (tofree == NULL)
3258 return;
3259
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003260 /* Increase refcount on dictionary, it could get deleted when evaluating
3261 * the arguments. */
3262 if (fudi.fd_dict != NULL)
3263 ++fudi.fd_dict->dv_refcount;
3264
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003265 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003266 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003267 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar532c7802005-01-27 14:44:31 +00003269 /* Skip white space to allow ":call func ()". Not good, but required for
3270 * backward compatibility. */
3271 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 if (*startarg != '(')
3275 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003276 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 goto end;
3278 }
3279
3280 /*
3281 * When skipping, evaluate the function once, to find the end of the
3282 * arguments.
3283 * When the function takes a range, this is discovered after the first
3284 * call, and the loop is broken.
3285 */
3286 if (eap->skip)
3287 {
3288 ++emsg_skip;
3289 lnum = eap->line2; /* do it once, also with an invalid range */
3290 }
3291 else
3292 lnum = eap->line1;
3293 for ( ; lnum <= eap->line2; ++lnum)
3294 {
3295 if (!eap->skip && eap->addr_count > 0)
3296 {
3297 curwin->w_cursor.lnum = lnum;
3298 curwin->w_cursor.col = 0;
3299 }
3300 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003301 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003302 eap->line1, eap->line2, &doesrange,
3303 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 failed = TRUE;
3306 break;
3307 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003308
3309 /* Handle a function returning a Funcref, Dictionary or List. */
3310 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3311 {
3312 failed = TRUE;
3313 break;
3314 }
3315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003316 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (doesrange || eap->skip)
3318 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003321 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003323 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (aborting())
3325 break;
3326 }
3327 if (eap->skip)
3328 --emsg_skip;
3329
3330 if (!failed)
3331 {
3332 /* Check for trailing illegal characters and a following command. */
3333 if (!ends_excmd(*arg))
3334 {
3335 emsg_severe = TRUE;
3336 EMSG(_(e_trailing));
3337 }
3338 else
3339 eap->nextcmd = check_nextcmd(arg);
3340 }
3341
3342end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003343 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003344 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
3348 * ":unlet[!] var1 ... " command.
3349 */
3350 void
3351ex_unlet(eap)
3352 exarg_T *eap;
3353{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003354 ex_unletlock(eap, eap->arg, 0);
3355}
3356
3357/*
3358 * ":lockvar" and ":unlockvar" commands
3359 */
3360 void
3361ex_lockvar(eap)
3362 exarg_T *eap;
3363{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365 int deep = 2;
3366
3367 if (eap->forceit)
3368 deep = -1;
3369 else if (vim_isdigit(*arg))
3370 {
3371 deep = getdigits(&arg);
3372 arg = skipwhite(arg);
3373 }
3374
3375 ex_unletlock(eap, arg, deep);
3376}
3377
3378/*
3379 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3380 */
3381 static void
3382ex_unletlock(eap, argstart, deep)
3383 exarg_T *eap;
3384 char_u *argstart;
3385 int deep;
3386{
3387 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003390 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
3392 do
3393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003394 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003395 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3396 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003397 if (lv.ll_name == NULL)
3398 error = TRUE; /* error but continue parsing */
3399 if (name_end == NULL || (!vim_iswhite(*name_end)
3400 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003402 if (name_end != NULL)
3403 {
3404 emsg_severe = TRUE;
3405 EMSG(_(e_trailing));
3406 }
3407 if (!(eap->skip || error))
3408 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 break;
3410 }
3411
3412 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003413 {
3414 if (eap->cmdidx == CMD_unlet)
3415 {
3416 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3417 error = TRUE;
3418 }
3419 else
3420 {
3421 if (do_lock_var(&lv, name_end, deep,
3422 eap->cmdidx == CMD_lockvar) == FAIL)
3423 error = TRUE;
3424 }
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003427 if (!eap->skip)
3428 clear_lval(&lv);
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 arg = skipwhite(name_end);
3431 } while (!ends_excmd(*arg));
3432
3433 eap->nextcmd = check_nextcmd(arg);
3434}
3435
Bram Moolenaar8c711452005-01-14 21:53:12 +00003436 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003437do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003438 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003439 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 int forceit;
3441{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003442 int ret = OK;
3443 int cc;
3444
3445 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003447 cc = *name_end;
3448 *name_end = NUL;
3449
3450 /* Normal name or expanded name. */
3451 if (check_changedtick(lp->ll_name))
3452 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003453 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003454 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003455 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003457 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3458 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 else if (lp->ll_range)
3460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003462
3463 /* Delete a range of List items. */
3464 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3465 {
3466 li = lp->ll_li->li_next;
3467 listitem_remove(lp->ll_list, lp->ll_li);
3468 lp->ll_li = li;
3469 ++lp->ll_n1;
3470 }
3471 }
3472 else
3473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 /* unlet a List item. */
3476 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003477 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003480 }
3481
3482 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003483}
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485/*
3486 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 */
3489 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003490do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003492 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
Bram Moolenaar33570922005-01-25 22:26:29 +00003494 hashtab_T *ht;
3495 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003496 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003497 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar33570922005-01-25 22:26:29 +00003499 ht = find_var_ht(name, &varname);
3500 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 hi = hash_find(ht, varname);
3503 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003505 di = HI2DI(hi);
3506 if (var_check_fixed(di->di_flags, name)
3507 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003508 return FAIL;
3509 delete_var(ht, hi);
3510 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 if (forceit)
3514 return OK;
3515 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 return FAIL;
3517}
3518
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519/*
3520 * Lock or unlock variable indicated by "lp".
3521 * "deep" is the levels to go (-1 for unlimited);
3522 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3523 */
3524 static int
3525do_lock_var(lp, name_end, deep, lock)
3526 lval_T *lp;
3527 char_u *name_end;
3528 int deep;
3529 int lock;
3530{
3531 int ret = OK;
3532 int cc;
3533 dictitem_T *di;
3534
3535 if (deep == 0) /* nothing to do */
3536 return OK;
3537
3538 if (lp->ll_tv == NULL)
3539 {
3540 cc = *name_end;
3541 *name_end = NUL;
3542
3543 /* Normal name or expanded name. */
3544 if (check_changedtick(lp->ll_name))
3545 ret = FAIL;
3546 else
3547 {
3548 di = find_var(lp->ll_name, NULL);
3549 if (di == NULL)
3550 ret = FAIL;
3551 else
3552 {
3553 if (lock)
3554 di->di_flags |= DI_FLAGS_LOCK;
3555 else
3556 di->di_flags &= ~DI_FLAGS_LOCK;
3557 item_lock(&di->di_tv, deep, lock);
3558 }
3559 }
3560 *name_end = cc;
3561 }
3562 else if (lp->ll_range)
3563 {
3564 listitem_T *li = lp->ll_li;
3565
3566 /* (un)lock a range of List items. */
3567 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3568 {
3569 item_lock(&li->li_tv, deep, lock);
3570 li = li->li_next;
3571 ++lp->ll_n1;
3572 }
3573 }
3574 else if (lp->ll_list != NULL)
3575 /* (un)lock a List item. */
3576 item_lock(&lp->ll_li->li_tv, deep, lock);
3577 else
3578 /* un(lock) a Dictionary item. */
3579 item_lock(&lp->ll_di->di_tv, deep, lock);
3580
3581 return ret;
3582}
3583
3584/*
3585 * Lock or unlock an item. "deep" is nr of levels to go.
3586 */
3587 static void
3588item_lock(tv, deep, lock)
3589 typval_T *tv;
3590 int deep;
3591 int lock;
3592{
3593 static int recurse = 0;
3594 list_T *l;
3595 listitem_T *li;
3596 dict_T *d;
3597 hashitem_T *hi;
3598 int todo;
3599
3600 if (recurse >= DICT_MAXNEST)
3601 {
3602 EMSG(_("E743: variable nested too deep for (un)lock"));
3603 return;
3604 }
3605 if (deep == 0)
3606 return;
3607 ++recurse;
3608
3609 /* lock/unlock the item itself */
3610 if (lock)
3611 tv->v_lock |= VAR_LOCKED;
3612 else
3613 tv->v_lock &= ~VAR_LOCKED;
3614
3615 switch (tv->v_type)
3616 {
3617 case VAR_LIST:
3618 if ((l = tv->vval.v_list) != NULL)
3619 {
3620 if (lock)
3621 l->lv_lock |= VAR_LOCKED;
3622 else
3623 l->lv_lock &= ~VAR_LOCKED;
3624 if (deep < 0 || deep > 1)
3625 /* recursive: lock/unlock the items the List contains */
3626 for (li = l->lv_first; li != NULL; li = li->li_next)
3627 item_lock(&li->li_tv, deep - 1, lock);
3628 }
3629 break;
3630 case VAR_DICT:
3631 if ((d = tv->vval.v_dict) != NULL)
3632 {
3633 if (lock)
3634 d->dv_lock |= VAR_LOCKED;
3635 else
3636 d->dv_lock &= ~VAR_LOCKED;
3637 if (deep < 0 || deep > 1)
3638 {
3639 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3642 {
3643 if (!HASHITEM_EMPTY(hi))
3644 {
3645 --todo;
3646 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3647 }
3648 }
3649 }
3650 }
3651 }
3652 --recurse;
3653}
3654
Bram Moolenaara40058a2005-07-11 22:42:07 +00003655/*
3656 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3657 * it refers to a List or Dictionary that is locked.
3658 */
3659 static int
3660tv_islocked(tv)
3661 typval_T *tv;
3662{
3663 return (tv->v_lock & VAR_LOCKED)
3664 || (tv->v_type == VAR_LIST
3665 && tv->vval.v_list != NULL
3666 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3667 || (tv->v_type == VAR_DICT
3668 && tv->vval.v_dict != NULL
3669 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3670}
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3673/*
3674 * Delete all "menutrans_" variables.
3675 */
3676 void
3677del_menutrans_vars()
3678{
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003680 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003683 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 {
3686 if (!HASHITEM_EMPTY(hi))
3687 {
3688 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3690 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
3692 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003693 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694}
3695#endif
3696
3697#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3698
3699/*
3700 * Local string buffer for the next two functions to store a variable name
3701 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3702 * get_user_var_name().
3703 */
3704
3705static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3706
3707static char_u *varnamebuf = NULL;
3708static int varnamebuflen = 0;
3709
3710/*
3711 * Function to concatenate a prefix and a variable name.
3712 */
3713 static char_u *
3714cat_prefix_varname(prefix, name)
3715 int prefix;
3716 char_u *name;
3717{
3718 int len;
3719
3720 len = (int)STRLEN(name) + 3;
3721 if (len > varnamebuflen)
3722 {
3723 vim_free(varnamebuf);
3724 len += 10; /* some additional space */
3725 varnamebuf = alloc(len);
3726 if (varnamebuf == NULL)
3727 {
3728 varnamebuflen = 0;
3729 return NULL;
3730 }
3731 varnamebuflen = len;
3732 }
3733 *varnamebuf = prefix;
3734 varnamebuf[1] = ':';
3735 STRCPY(varnamebuf + 2, name);
3736 return varnamebuf;
3737}
3738
3739/*
3740 * Function given to ExpandGeneric() to obtain the list of user defined
3741 * (global/buffer/window/built-in) variable names.
3742 */
3743/*ARGSUSED*/
3744 char_u *
3745get_user_var_name(xp, idx)
3746 expand_T *xp;
3747 int idx;
3748{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003749 static long_u gdone;
3750 static long_u bdone;
3751 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003752#ifdef FEAT_WINDOWS
3753 static long_u tdone;
3754#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003755 static int vidx;
3756 static hashitem_T *hi;
3757 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003760 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003762#ifdef FEAT_WINDOWS
3763 tdone = 0;
3764#endif
3765 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003766
3767 /* Global variables */
3768 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003772 else
3773 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003774 while (HASHITEM_EMPTY(hi))
3775 ++hi;
3776 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3777 return cat_prefix_varname('g', hi->hi_key);
3778 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003780
3781 /* b: variables */
3782 ht = &curbuf->b_vars.dv_hashtab;
3783 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003785 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003787 else
3788 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003789 while (HASHITEM_EMPTY(hi))
3790 ++hi;
3791 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003793 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003795 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return (char_u *)"b:changedtick";
3797 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003798
3799 /* w: variables */
3800 ht = &curwin->w_vars.dv_hashtab;
3801 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003803 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003805 else
3806 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 while (HASHITEM_EMPTY(hi))
3808 ++hi;
3809 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003811
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003812#ifdef FEAT_WINDOWS
3813 /* t: variables */
3814 ht = &curtab->tp_vars.dv_hashtab;
3815 if (tdone < ht->ht_used)
3816 {
3817 if (tdone++ == 0)
3818 hi = ht->ht_array;
3819 else
3820 ++hi;
3821 while (HASHITEM_EMPTY(hi))
3822 ++hi;
3823 return cat_prefix_varname('t', hi->hi_key);
3824 }
3825#endif
3826
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 /* v: variables */
3828 if (vidx < VV_LEN)
3829 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 vim_free(varnamebuf);
3832 varnamebuf = NULL;
3833 varnamebuflen = 0;
3834 return NULL;
3835}
3836
3837#endif /* FEAT_CMDL_COMPL */
3838
3839/*
3840 * types for expressions.
3841 */
3842typedef enum
3843{
3844 TYPE_UNKNOWN = 0
3845 , TYPE_EQUAL /* == */
3846 , TYPE_NEQUAL /* != */
3847 , TYPE_GREATER /* > */
3848 , TYPE_GEQUAL /* >= */
3849 , TYPE_SMALLER /* < */
3850 , TYPE_SEQUAL /* <= */
3851 , TYPE_MATCH /* =~ */
3852 , TYPE_NOMATCH /* !~ */
3853} exptype_T;
3854
3855/*
3856 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3859 */
3860
3861/*
3862 * Handle zero level expression.
3863 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003865 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * Return OK or FAIL.
3867 */
3868 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003869eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 char_u **nextcmd;
3873 int evaluate;
3874{
3875 int ret;
3876 char_u *p;
3877
3878 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (ret == FAIL || !ends_excmd(*p))
3881 {
3882 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 /*
3885 * Report the invalid expression unless the expression evaluation has
3886 * been cancelled due to an aborting error, an interrupt, or an
3887 * exception.
3888 */
3889 if (!aborting())
3890 EMSG2(_(e_invexpr2), arg);
3891 ret = FAIL;
3892 }
3893 if (nextcmd != NULL)
3894 *nextcmd = check_nextcmd(p);
3895
3896 return ret;
3897}
3898
3899/*
3900 * Handle top level expression:
3901 * expr1 ? expr0 : expr0
3902 *
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3905 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003906 * Note: "rettv.v_lock" is not set.
3907 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * Return OK or FAIL.
3909 */
3910 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 int evaluate;
3915{
3916 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 /*
3920 * Get the first variable.
3921 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 return FAIL;
3924
3925 if ((*arg)[0] == '?')
3926 {
3927 result = FALSE;
3928 if (evaluate)
3929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 int error = FALSE;
3931
3932 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003934 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003935 if (error)
3936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938
3939 /*
3940 * Get the second variable.
3941 */
3942 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return FAIL;
3945
3946 /*
3947 * Check for the ":".
3948 */
3949 if ((*arg)[0] != ':')
3950 {
3951 EMSG(_("E109: Missing ':' after '?'"));
3952 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return FAIL;
3955 }
3956
3957 /*
3958 * Get the third variable.
3959 */
3960 *arg = skipwhite(*arg + 1);
3961 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3962 {
3963 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return FAIL;
3966 }
3967 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970
3971 return OK;
3972}
3973
3974/*
3975 * Handle first level expression:
3976 * expr2 || expr2 || expr2 logical OR
3977 *
3978 * "arg" must point to the first non-white of the expression.
3979 * "arg" is advanced to the next non-white after the recognized expression.
3980 *
3981 * Return OK or FAIL.
3982 */
3983 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 int evaluate;
3988{
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 long result;
3991 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 /*
3995 * Get the first variable.
3996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 return FAIL;
3999
4000 /*
4001 * Repeat until there is no following "||".
4002 */
4003 first = TRUE;
4004 result = FALSE;
4005 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4006 {
4007 if (evaluate && first)
4008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 if (error)
4013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 first = FALSE;
4015 }
4016
4017 /*
4018 * Get the second variable.
4019 */
4020 *arg = skipwhite(*arg + 2);
4021 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4022 return FAIL;
4023
4024 /*
4025 * Compute the result.
4026 */
4027 if (evaluate && !result)
4028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004029 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 if (error)
4033 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 if (evaluate)
4036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 rettv->v_type = VAR_NUMBER;
4038 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040 }
4041
4042 return OK;
4043}
4044
4045/*
4046 * Handle second level expression:
4047 * expr3 && expr3 && expr3 logical AND
4048 *
4049 * "arg" must point to the first non-white of the expression.
4050 * "arg" is advanced to the next non-white after the recognized expression.
4051 *
4052 * Return OK or FAIL.
4053 */
4054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int evaluate;
4059{
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 long result;
4062 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 /*
4066 * Get the first variable.
4067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 return FAIL;
4070
4071 /*
4072 * Repeat until there is no following "&&".
4073 */
4074 first = TRUE;
4075 result = TRUE;
4076 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4077 {
4078 if (evaluate && first)
4079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004080 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 if (error)
4084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 first = FALSE;
4086 }
4087
4088 /*
4089 * Get the second variable.
4090 */
4091 *arg = skipwhite(*arg + 2);
4092 if (eval4(arg, &var2, evaluate && result) == FAIL)
4093 return FAIL;
4094
4095 /*
4096 * Compute the result.
4097 */
4098 if (evaluate && result)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 if (error)
4104 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 if (evaluate)
4107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 rettv->v_type = VAR_NUMBER;
4109 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 }
4112
4113 return OK;
4114}
4115
4116/*
4117 * Handle third level expression:
4118 * var1 == var2
4119 * var1 =~ var2
4120 * var1 != var2
4121 * var1 !~ var2
4122 * var1 > var2
4123 * var1 >= var2
4124 * var1 < var2
4125 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004126 * var1 is var2
4127 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 *
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4131 *
4132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 int evaluate;
4139{
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u *p;
4142 int i;
4143 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004144 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int len = 2;
4146 long n1, n2;
4147 char_u *s1, *s2;
4148 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4149 regmatch_T regmatch;
4150 int ic;
4151 char_u *save_cpo;
4152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 p = *arg;
4160 switch (p[0])
4161 {
4162 case '=': if (p[1] == '=')
4163 type = TYPE_EQUAL;
4164 else if (p[1] == '~')
4165 type = TYPE_MATCH;
4166 break;
4167 case '!': if (p[1] == '=')
4168 type = TYPE_NEQUAL;
4169 else if (p[1] == '~')
4170 type = TYPE_NOMATCH;
4171 break;
4172 case '>': if (p[1] != '=')
4173 {
4174 type = TYPE_GREATER;
4175 len = 1;
4176 }
4177 else
4178 type = TYPE_GEQUAL;
4179 break;
4180 case '<': if (p[1] != '=')
4181 {
4182 type = TYPE_SMALLER;
4183 len = 1;
4184 }
4185 else
4186 type = TYPE_SEQUAL;
4187 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004188 case 'i': if (p[1] == 's')
4189 {
4190 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4191 len = 5;
4192 if (!vim_isIDc(p[len]))
4193 {
4194 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4195 type_is = TRUE;
4196 }
4197 }
4198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200
4201 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004202 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
4204 if (type != TYPE_UNKNOWN)
4205 {
4206 /* extra question mark appended: ignore case */
4207 if (p[len] == '?')
4208 {
4209 ic = TRUE;
4210 ++len;
4211 }
4212 /* extra '#' appended: match case */
4213 else if (p[len] == '#')
4214 {
4215 ic = FALSE;
4216 ++len;
4217 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004218 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 else
4220 ic = p_ic;
4221
4222 /*
4223 * Get the second variable.
4224 */
4225 *arg = skipwhite(p + len);
4226 if (eval5(arg, &var2, evaluate) == FAIL)
4227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230 }
4231
4232 if (evaluate)
4233 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004234 if (type_is && rettv->v_type != var2.v_type)
4235 {
4236 /* For "is" a different type always means FALSE, for "notis"
4237 * it means TRUE. */
4238 n1 = (type == TYPE_NEQUAL);
4239 }
4240 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4241 {
4242 if (type_is)
4243 {
4244 n1 = (rettv->v_type == var2.v_type
4245 && rettv->vval.v_list == var2.vval.v_list);
4246 if (type == TYPE_NEQUAL)
4247 n1 = !n1;
4248 }
4249 else if (rettv->v_type != var2.v_type
4250 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4251 {
4252 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004253 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004254 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004255 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004256 clear_tv(rettv);
4257 clear_tv(&var2);
4258 return FAIL;
4259 }
4260 else
4261 {
4262 /* Compare two Lists for being equal or unequal. */
4263 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4264 if (type == TYPE_NEQUAL)
4265 n1 = !n1;
4266 }
4267 }
4268
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004269 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4270 {
4271 if (type_is)
4272 {
4273 n1 = (rettv->v_type == var2.v_type
4274 && rettv->vval.v_dict == var2.vval.v_dict);
4275 if (type == TYPE_NEQUAL)
4276 n1 = !n1;
4277 }
4278 else if (rettv->v_type != var2.v_type
4279 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4280 {
4281 if (rettv->v_type != var2.v_type)
4282 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4283 else
4284 EMSG(_("E736: Invalid operation for Dictionary"));
4285 clear_tv(rettv);
4286 clear_tv(&var2);
4287 return FAIL;
4288 }
4289 else
4290 {
4291 /* Compare two Dictionaries for being equal or unequal. */
4292 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4293 if (type == TYPE_NEQUAL)
4294 n1 = !n1;
4295 }
4296 }
4297
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004298 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4299 {
4300 if (rettv->v_type != var2.v_type
4301 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4302 {
4303 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004304 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004306 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004307 clear_tv(rettv);
4308 clear_tv(&var2);
4309 return FAIL;
4310 }
4311 else
4312 {
4313 /* Compare two Funcrefs for being equal or unequal. */
4314 if (rettv->vval.v_string == NULL
4315 || var2.vval.v_string == NULL)
4316 n1 = FALSE;
4317 else
4318 n1 = STRCMP(rettv->vval.v_string,
4319 var2.vval.v_string) == 0;
4320 if (type == TYPE_NEQUAL)
4321 n1 = !n1;
4322 }
4323 }
4324
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004325#ifdef FEAT_FLOAT
4326 /*
4327 * If one of the two variables is a float, compare as a float.
4328 * When using "=~" or "!~", always compare as string.
4329 */
4330 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4331 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4332 {
4333 float_T f1, f2;
4334
4335 if (rettv->v_type == VAR_FLOAT)
4336 f1 = rettv->vval.v_float;
4337 else
4338 f1 = get_tv_number(rettv);
4339 if (var2.v_type == VAR_FLOAT)
4340 f2 = var2.vval.v_float;
4341 else
4342 f2 = get_tv_number(&var2);
4343 n1 = FALSE;
4344 switch (type)
4345 {
4346 case TYPE_EQUAL: n1 = (f1 == f2); break;
4347 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4348 case TYPE_GREATER: n1 = (f1 > f2); break;
4349 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4350 case TYPE_SMALLER: n1 = (f1 < f2); break;
4351 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4352 case TYPE_UNKNOWN:
4353 case TYPE_MATCH:
4354 case TYPE_NOMATCH: break; /* avoid gcc warning */
4355 }
4356 }
4357#endif
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 /*
4360 * If one of the two variables is a number, compare as a number.
4361 * When using "=~" or "!~", always compare as string.
4362 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004363 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 n1 = get_tv_number(rettv);
4367 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 switch (type)
4369 {
4370 case TYPE_EQUAL: n1 = (n1 == n2); break;
4371 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4372 case TYPE_GREATER: n1 = (n1 > n2); break;
4373 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4374 case TYPE_SMALLER: n1 = (n1 < n2); break;
4375 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4376 case TYPE_UNKNOWN:
4377 case TYPE_MATCH:
4378 case TYPE_NOMATCH: break; /* avoid gcc warning */
4379 }
4380 }
4381 else
4382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 s1 = get_tv_string_buf(rettv, buf1);
4384 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4386 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4387 else
4388 i = 0;
4389 n1 = FALSE;
4390 switch (type)
4391 {
4392 case TYPE_EQUAL: n1 = (i == 0); break;
4393 case TYPE_NEQUAL: n1 = (i != 0); break;
4394 case TYPE_GREATER: n1 = (i > 0); break;
4395 case TYPE_GEQUAL: n1 = (i >= 0); break;
4396 case TYPE_SMALLER: n1 = (i < 0); break;
4397 case TYPE_SEQUAL: n1 = (i <= 0); break;
4398
4399 case TYPE_MATCH:
4400 case TYPE_NOMATCH:
4401 /* avoid 'l' flag in 'cpoptions' */
4402 save_cpo = p_cpo;
4403 p_cpo = (char_u *)"";
4404 regmatch.regprog = vim_regcomp(s2,
4405 RE_MAGIC + RE_STRING);
4406 regmatch.rm_ic = ic;
4407 if (regmatch.regprog != NULL)
4408 {
4409 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4410 vim_free(regmatch.regprog);
4411 if (type == TYPE_NOMATCH)
4412 n1 = !n1;
4413 }
4414 p_cpo = save_cpo;
4415 break;
4416
4417 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4418 }
4419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(rettv);
4421 clear_tv(&var2);
4422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 }
4426
4427 return OK;
4428}
4429
4430/*
4431 * Handle fourth level expression:
4432 * + number addition
4433 * - number subtraction
4434 * . string concatenation
4435 *
4436 * "arg" must point to the first non-white of the expression.
4437 * "arg" is advanced to the next non-white after the recognized expression.
4438 *
4439 * Return OK or FAIL.
4440 */
4441 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 int evaluate;
4446{
Bram Moolenaar33570922005-01-25 22:26:29 +00004447 typval_T var2;
4448 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 int op;
4450 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004451#ifdef FEAT_FLOAT
4452 float_T f1 = 0, f2 = 0;
4453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_u *s1, *s2;
4455 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4456 char_u *p;
4457
4458 /*
4459 * Get the first variable.
4460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 return FAIL;
4463
4464 /*
4465 * Repeat computing, until no '+', '-' or '.' is following.
4466 */
4467 for (;;)
4468 {
4469 op = **arg;
4470 if (op != '+' && op != '-' && op != '.')
4471 break;
4472
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 if ((op != '+' || rettv->v_type != VAR_LIST)
4474#ifdef FEAT_FLOAT
4475 && (op == '.' || rettv->v_type != VAR_FLOAT)
4476#endif
4477 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004478 {
4479 /* For "list + ...", an illegal use of the first operand as
4480 * a number cannot be determined before evaluating the 2nd
4481 * operand: if this is also a list, all is ok.
4482 * For "something . ...", "something - ..." or "non-list + ...",
4483 * we know that the first operand needs to be a string or number
4484 * without evaluating the 2nd operand. So check before to avoid
4485 * side effects after an error. */
4486 if (evaluate && get_tv_string_chk(rettv) == NULL)
4487 {
4488 clear_tv(rettv);
4489 return FAIL;
4490 }
4491 }
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /*
4494 * Get the second variable.
4495 */
4496 *arg = skipwhite(*arg + 1);
4497 if (eval6(arg, &var2, evaluate) == FAIL)
4498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 return FAIL;
4501 }
4502
4503 if (evaluate)
4504 {
4505 /*
4506 * Compute the result.
4507 */
4508 if (op == '.')
4509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004510 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4511 s2 = get_tv_string_buf_chk(&var2, buf2);
4512 if (s2 == NULL) /* type error ? */
4513 {
4514 clear_tv(rettv);
4515 clear_tv(&var2);
4516 return FAIL;
4517 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 clear_tv(rettv);
4520 rettv->v_type = VAR_STRING;
4521 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 else if (op == '+' && rettv->v_type == VAR_LIST
4524 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 {
4526 /* concatenate Lists */
4527 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4528 &var3) == FAIL)
4529 {
4530 clear_tv(rettv);
4531 clear_tv(&var2);
4532 return FAIL;
4533 }
4534 clear_tv(rettv);
4535 *rettv = var3;
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else
4538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004539 int error = FALSE;
4540
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541#ifdef FEAT_FLOAT
4542 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004543 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004544 f1 = rettv->vval.v_float;
4545 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004546 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004547 else
4548#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004549 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004550 n1 = get_tv_number_chk(rettv, &error);
4551 if (error)
4552 {
4553 /* This can only happen for "list + non-list". For
4554 * "non-list + ..." or "something - ...", we returned
4555 * before evaluating the 2nd operand. */
4556 clear_tv(rettv);
4557 return FAIL;
4558 }
4559#ifdef FEAT_FLOAT
4560 if (var2.v_type == VAR_FLOAT)
4561 f1 = n1;
4562#endif
4563 }
4564#ifdef FEAT_FLOAT
4565 if (var2.v_type == VAR_FLOAT)
4566 {
4567 f2 = var2.vval.v_float;
4568 n2 = 0;
4569 }
4570 else
4571#endif
4572 {
4573 n2 = get_tv_number_chk(&var2, &error);
4574 if (error)
4575 {
4576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 return FAIL;
4579 }
4580#ifdef FEAT_FLOAT
4581 if (rettv->v_type == VAR_FLOAT)
4582 f2 = n2;
4583#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586
4587#ifdef FEAT_FLOAT
4588 /* If there is a float on either side the result is a float. */
4589 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4590 {
4591 if (op == '+')
4592 f1 = f1 + f2;
4593 else
4594 f1 = f1 - f2;
4595 rettv->v_type = VAR_FLOAT;
4596 rettv->vval.v_float = f1;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004599#endif
4600 {
4601 if (op == '+')
4602 n1 = n1 + n2;
4603 else
4604 n1 = n1 - n2;
4605 rettv->v_type = VAR_NUMBER;
4606 rettv->vval.v_number = n1;
4607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 }
4612 return OK;
4613}
4614
4615/*
4616 * Handle fifth level expression:
4617 * * number multiplication
4618 * / number division
4619 * % number modulo
4620 *
4621 * "arg" must point to the first non-white of the expression.
4622 * "arg" is advanced to the next non-white after the recognized expression.
4623 *
4624 * Return OK or FAIL.
4625 */
4626 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int evaluate;
4631{
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 int op;
4634 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635#ifdef FEAT_FLOAT
4636 int use_float = FALSE;
4637 float_T f1 = 0, f2;
4638#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
4641 /*
4642 * Get the first variable.
4643 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return FAIL;
4646
4647 /*
4648 * Repeat computing, until no '*', '/' or '%' is following.
4649 */
4650 for (;;)
4651 {
4652 op = **arg;
4653 if (op != '*' && op != '/' && op != '%')
4654 break;
4655
4656 if (evaluate)
4657 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004658#ifdef FEAT_FLOAT
4659 if (rettv->v_type == VAR_FLOAT)
4660 {
4661 f1 = rettv->vval.v_float;
4662 use_float = TRUE;
4663 n1 = 0;
4664 }
4665 else
4666#endif
4667 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 if (error)
4670 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 else
4673 n1 = 0;
4674
4675 /*
4676 * Get the second variable.
4677 */
4678 *arg = skipwhite(*arg + 1);
4679 if (eval7(arg, &var2, evaluate) == FAIL)
4680 return FAIL;
4681
4682 if (evaluate)
4683 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684#ifdef FEAT_FLOAT
4685 if (var2.v_type == VAR_FLOAT)
4686 {
4687 if (!use_float)
4688 {
4689 f1 = n1;
4690 use_float = TRUE;
4691 }
4692 f2 = var2.vval.v_float;
4693 n2 = 0;
4694 }
4695 else
4696#endif
4697 {
4698 n2 = get_tv_number_chk(&var2, &error);
4699 clear_tv(&var2);
4700 if (error)
4701 return FAIL;
4702#ifdef FEAT_FLOAT
4703 if (use_float)
4704 f2 = n2;
4705#endif
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 /*
4709 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if (op == '*')
4716 f1 = f1 * f2;
4717 else if (op == '/')
4718 {
4719 /* We rely on the floating point library to handle divide
4720 * by zero to result in "inf" and not a crash. */
4721 f1 = f1 / f2;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 {
4725 EMSG(_("E804: Cannot use % with float"));
4726 return FAIL;
4727 }
4728 rettv->v_type = VAR_FLOAT;
4729 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if (op == '*')
4735 n1 = n1 * n2;
4736 else if (op == '/')
4737 {
4738 if (n2 == 0) /* give an error message? */
4739 {
4740 if (n1 == 0)
4741 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4742 else if (n1 < 0)
4743 n1 = -0x7fffffffL;
4744 else
4745 n1 = 0x7fffffffL;
4746 }
4747 else
4748 n1 = n1 / n2;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751 {
4752 if (n2 == 0) /* give an error message? */
4753 n1 = 0;
4754 else
4755 n1 = n1 % n2;
4756 }
4757 rettv->v_type = VAR_NUMBER;
4758 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762
4763 return OK;
4764}
4765
4766/*
4767 * Handle sixth level expression:
4768 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004769 * "string" string constant
4770 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 * &option-name option value
4772 * @r register contents
4773 * identifier variable value
4774 * function() function call
4775 * $VAR environment variable
4776 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004777 * [expr, expr] List
4778 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 *
4780 * Also handle:
4781 * ! in front logical NOT
4782 * - in front unary minus
4783 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784 * trailing [] subscript in String or List
4785 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 *
4787 * "arg" must point to the first non-white of the expression.
4788 * "arg" is advanced to the next non-white after the recognized expression.
4789 *
4790 * Return OK or FAIL.
4791 */
4792 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 int evaluate;
4797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 long n;
4799 int len;
4800 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 char_u *start_leader, *end_leader;
4802 int ret = OK;
4803 char_u *alias;
4804
4805 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
4811 /*
4812 * Skip '!' and '-' characters. They are handled later.
4813 */
4814 start_leader = *arg;
4815 while (**arg == '!' || **arg == '-' || **arg == '+')
4816 *arg = skipwhite(*arg + 1);
4817 end_leader = *arg;
4818
4819 switch (**arg)
4820 {
4821 /*
4822 * Number constant.
4823 */
4824 case '0':
4825 case '1':
4826 case '2':
4827 case '3':
4828 case '4':
4829 case '5':
4830 case '6':
4831 case '7':
4832 case '8':
4833 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004834 {
4835#ifdef FEAT_FLOAT
4836 char_u *p = skipdigits(*arg + 1);
4837 int get_float = FALSE;
4838
4839 /* We accept a float when the format matches
4840 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4841 * strict to avoid backwards compatibility problems. */
4842 if (p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844 get_float = TRUE;
4845 p = skipdigits(p + 2);
4846 if (*p == 'e' || *p == 'E')
4847 {
4848 ++p;
4849 if (*p == '-' || *p == '+')
4850 ++p;
4851 if (!vim_isdigit(*p))
4852 get_float = FALSE;
4853 else
4854 p = skipdigits(p + 1);
4855 }
4856 if (ASCII_ISALPHA(*p) || *p == '.')
4857 get_float = FALSE;
4858 }
4859 if (get_float)
4860 {
4861 float_T f;
4862
4863 *arg += string2float(*arg, &f);
4864 if (evaluate)
4865 {
4866 rettv->v_type = VAR_FLOAT;
4867 rettv->vval.v_float = f;
4868 }
4869 }
4870 else
4871#endif
4872 {
4873 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4874 *arg += len;
4875 if (evaluate)
4876 {
4877 rettv->v_type = VAR_NUMBER;
4878 rettv->vval.v_number = n;
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * String constant: "string".
4886 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 break;
4889
4890 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004891 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 break;
4895
4896 /*
4897 * List: [expr, expr]
4898 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 break;
4901
4902 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 * Dictionary: {key: val, key: val}
4904 */
4905 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4906 break;
4907
4908 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004909 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004911 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 break;
4913
4914 /*
4915 * Environment variable: $VAR.
4916 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919
4920 /*
4921 * Register contents: @r.
4922 */
4923 case '@': ++*arg;
4924 if (evaluate)
4925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004927 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 if (**arg != NUL)
4930 ++*arg;
4931 break;
4932
4933 /*
4934 * nested expression: (expression).
4935 */
4936 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (**arg == ')')
4939 ++*arg;
4940 else if (ret == OK)
4941 {
4942 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004943 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 ret = FAIL;
4945 }
4946 break;
4947
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 break;
4950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951
4952 if (ret == NOTDONE)
4953 {
4954 /*
4955 * Must be a variable or function name.
4956 * Can also be a curly-braces kind of name: {expr}.
4957 */
4958 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004959 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 if (alias != NULL)
4961 s = alias;
4962
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004963 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 ret = FAIL;
4965 else
4966 {
4967 if (**arg == '(') /* recursive! */
4968 {
4969 /* If "s" is the name of a variable of type VAR_FUNC
4970 * use its contents. */
4971 s = deref_func_name(s, &len);
4972
4973 /* Invoke the function. */
4974 ret = get_func_tv(s, len, rettv, arg,
4975 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 /* Stop the expression evaluation when immediately
4978 * aborting on error, or when an interrupt occurred or
4979 * an exception was thrown but not caught. */
4980 if (aborting())
4981 {
4982 if (ret == OK)
4983 clear_tv(rettv);
4984 ret = FAIL;
4985 }
4986 }
4987 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004988 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004989 else
4990 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 }
4992
4993 if (alias != NULL)
4994 vim_free(alias);
4995 }
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 *arg = skipwhite(*arg);
4998
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004999 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5000 * expr(expr). */
5001 if (ret == OK)
5002 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5006 */
5007 if (ret == OK && evaluate && end_leader > start_leader)
5008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005009 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 int val = 0;
5011#ifdef FEAT_FLOAT
5012 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 if (rettv->v_type == VAR_FLOAT)
5015 f = rettv->vval.v_float;
5016 else
5017#endif
5018 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005019 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005021 clear_tv(rettv);
5022 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005024 else
5025 {
5026 while (end_leader > start_leader)
5027 {
5028 --end_leader;
5029 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 {
5031#ifdef FEAT_FLOAT
5032 if (rettv->v_type == VAR_FLOAT)
5033 f = !f;
5034 else
5035#endif
5036 val = !val;
5037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005038 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 {
5040#ifdef FEAT_FLOAT
5041 if (rettv->v_type == VAR_FLOAT)
5042 f = -f;
5043 else
5044#endif
5045 val = -val;
5046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005047 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005048#ifdef FEAT_FLOAT
5049 if (rettv->v_type == VAR_FLOAT)
5050 {
5051 clear_tv(rettv);
5052 rettv->vval.v_float = f;
5053 }
5054 else
5055#endif
5056 {
5057 clear_tv(rettv);
5058 rettv->v_type = VAR_NUMBER;
5059 rettv->vval.v_number = val;
5060 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063
5064 return ret;
5065}
5066
5067/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005068 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5069 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5071 */
5072 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005073eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005077 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078{
5079 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 long len = -1;
5083 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 if (rettv->v_type == VAR_FUNC
5088#ifdef FEAT_FLOAT
5089 || rettv->v_type == VAR_FLOAT
5090#endif
5091 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005093 if (verbose)
5094 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095 return FAIL;
5096 }
5097
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 /*
5101 * dict.name
5102 */
5103 key = *arg + 1;
5104 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5105 ;
5106 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 }
5110 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 /*
5113 * something[idx]
5114 *
5115 * Get the (first) variable from inside the [].
5116 */
5117 *arg = skipwhite(*arg + 1);
5118 if (**arg == ':')
5119 empty1 = TRUE;
5120 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5121 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5123 {
5124 /* not a number or string */
5125 clear_tv(&var1);
5126 return FAIL;
5127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128
5129 /*
5130 * Get the second variable from inside the [:].
5131 */
5132 if (**arg == ':')
5133 {
5134 range = TRUE;
5135 *arg = skipwhite(*arg + 1);
5136 if (**arg == ']')
5137 empty2 = TRUE;
5138 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140 if (!empty1)
5141 clear_tv(&var1);
5142 return FAIL;
5143 }
5144 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5145 {
5146 /* not a number or string */
5147 if (!empty1)
5148 clear_tv(&var1);
5149 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 return FAIL;
5151 }
5152 }
5153
5154 /* Check for the ']'. */
5155 if (**arg != ']')
5156 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 if (verbose)
5158 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 clear_tv(&var1);
5160 if (range)
5161 clear_tv(&var2);
5162 return FAIL;
5163 }
5164 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 }
5166
5167 if (evaluate)
5168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 n1 = 0;
5170 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 n1 = get_tv_number(&var1);
5173 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 }
5175 if (range)
5176 {
5177 if (empty2)
5178 n2 = -1;
5179 else
5180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 n2 = get_tv_number(&var2);
5182 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 }
5184 }
5185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 {
5188 case VAR_NUMBER:
5189 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 len = (long)STRLEN(s);
5192 if (range)
5193 {
5194 /* The resulting variable is a substring. If the indexes
5195 * are out of range the result is empty. */
5196 if (n1 < 0)
5197 {
5198 n1 = len + n1;
5199 if (n1 < 0)
5200 n1 = 0;
5201 }
5202 if (n2 < 0)
5203 n2 = len + n2;
5204 else if (n2 >= len)
5205 n2 = len;
5206 if (n1 >= len || n2 < 0 || n1 > n2)
5207 s = NULL;
5208 else
5209 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5210 }
5211 else
5212 {
5213 /* The resulting variable is a string of a single
5214 * character. If the index is too big or negative the
5215 * result is empty. */
5216 if (n1 >= len || n1 < 0)
5217 s = NULL;
5218 else
5219 s = vim_strnsave(s + n1, 1);
5220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 clear_tv(rettv);
5222 rettv->v_type = VAR_STRING;
5223 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 break;
5225
5226 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 if (n1 < 0)
5229 n1 = len + n1;
5230 if (!empty1 && (n1 < 0 || n1 >= len))
5231 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005232 /* For a range we allow invalid values and return an empty
5233 * list. A list index out of range is an error. */
5234 if (!range)
5235 {
5236 if (verbose)
5237 EMSGN(_(e_listidx), n1);
5238 return FAIL;
5239 }
5240 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 }
5242 if (range)
5243 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 list_T *l;
5245 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246
5247 if (n2 < 0)
5248 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005249 else if (n2 >= len)
5250 n2 = len - 1;
5251 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005252 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 l = list_alloc();
5254 if (l == NULL)
5255 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 n1 <= n2; ++n1)
5258 {
5259 if (list_append_tv(l, &item->li_tv) == FAIL)
5260 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005261 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 return FAIL;
5263 }
5264 item = item->li_next;
5265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 clear_tv(rettv);
5267 rettv->v_type = VAR_LIST;
5268 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005269 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 }
5271 else
5272 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005273 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 clear_tv(rettv);
5275 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 }
5277 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278
5279 case VAR_DICT:
5280 if (range)
5281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282 if (verbose)
5283 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 if (len == -1)
5285 clear_tv(&var1);
5286 return FAIL;
5287 }
5288 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 if (len == -1)
5292 {
5293 key = get_tv_string(&var1);
5294 if (*key == NUL)
5295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 if (verbose)
5297 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 clear_tv(&var1);
5299 return FAIL;
5300 }
5301 }
5302
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005303 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005305 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005306 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 if (len == -1)
5308 clear_tv(&var1);
5309 if (item == NULL)
5310 return FAIL;
5311
5312 copy_tv(&item->di_tv, &var1);
5313 clear_tv(rettv);
5314 *rettv = var1;
5315 }
5316 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 }
5318 }
5319
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return OK;
5321}
5322
5323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 * Get an option value.
5325 * "arg" points to the '&' or '+' before the option name.
5326 * "arg" is advanced to character after the option name.
5327 * Return OK or FAIL.
5328 */
5329 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005330get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005332 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 int evaluate;
5334{
5335 char_u *option_end;
5336 long numval;
5337 char_u *stringval;
5338 int opt_type;
5339 int c;
5340 int working = (**arg == '+'); /* has("+option") */
5341 int ret = OK;
5342 int opt_flags;
5343
5344 /*
5345 * Isolate the option name and find its value.
5346 */
5347 option_end = find_option_end(arg, &opt_flags);
5348 if (option_end == NULL)
5349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005350 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 EMSG2(_("E112: Option name missing: %s"), *arg);
5352 return FAIL;
5353 }
5354
5355 if (!evaluate)
5356 {
5357 *arg = option_end;
5358 return OK;
5359 }
5360
5361 c = *option_end;
5362 *option_end = NUL;
5363 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 if (opt_type == -3) /* invalid name */
5367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 EMSG2(_("E113: Unknown option: %s"), *arg);
5370 ret = FAIL;
5371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 if (opt_type == -2) /* hidden string option */
5375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 rettv->v_type = VAR_STRING;
5377 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379 else if (opt_type == -1) /* hidden number option */
5380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 rettv->v_type = VAR_NUMBER;
5382 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 else if (opt_type == 1) /* number option */
5385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 rettv->v_type = VAR_NUMBER;
5387 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 else /* string option */
5390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 rettv->v_type = VAR_STRING;
5392 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 }
5395 else if (working && (opt_type == -2 || opt_type == -1))
5396 ret = FAIL;
5397
5398 *option_end = c; /* put back for error messages */
5399 *arg = option_end;
5400
5401 return ret;
5402}
5403
5404/*
5405 * Allocate a variable for a string constant.
5406 * Return OK or FAIL.
5407 */
5408 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 int evaluate;
5413{
5414 char_u *p;
5415 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 int extra = 0;
5417
5418 /*
5419 * Find the end of the string, skipping backslashed characters.
5420 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005421 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 if (*p == '\\' && p[1] != NUL)
5424 {
5425 ++p;
5426 /* A "\<x>" form occupies at least 4 characters, and produces up
5427 * to 6 characters: reserve space for 2 extra */
5428 if (*p == '<')
5429 extra += 2;
5430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432
5433 if (*p != '"')
5434 {
5435 EMSG2(_("E114: Missing quote: %s"), *arg);
5436 return FAIL;
5437 }
5438
5439 /* If only parsing, set *arg and return here */
5440 if (!evaluate)
5441 {
5442 *arg = p + 1;
5443 return OK;
5444 }
5445
5446 /*
5447 * Copy the string into allocated memory, handling backslashed
5448 * characters.
5449 */
5450 name = alloc((unsigned)(p - *arg + extra));
5451 if (name == NULL)
5452 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 rettv->v_type = VAR_STRING;
5454 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 if (*p == '\\')
5459 {
5460 switch (*++p)
5461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462 case 'b': *name++ = BS; ++p; break;
5463 case 'e': *name++ = ESC; ++p; break;
5464 case 'f': *name++ = FF; ++p; break;
5465 case 'n': *name++ = NL; ++p; break;
5466 case 'r': *name++ = CAR; ++p; break;
5467 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
5469 case 'X': /* hex: "\x1", "\x12" */
5470 case 'x':
5471 case 'u': /* Unicode: "\u0023" */
5472 case 'U':
5473 if (vim_isxdigit(p[1]))
5474 {
5475 int n, nr;
5476 int c = toupper(*p);
5477
5478 if (c == 'X')
5479 n = 2;
5480 else
5481 n = 4;
5482 nr = 0;
5483 while (--n >= 0 && vim_isxdigit(p[1]))
5484 {
5485 ++p;
5486 nr = (nr << 4) + hex2nr(*p);
5487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489#ifdef FEAT_MBYTE
5490 /* For "\u" store the number according to
5491 * 'encoding'. */
5492 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 else
5495#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 break;
5499
5500 /* octal: "\1", "\12", "\123" */
5501 case '0':
5502 case '1':
5503 case '2':
5504 case '3':
5505 case '4':
5506 case '5':
5507 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005508 case '7': *name = *p++ - '0';
5509 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 *name = (*name << 3) + *p++ - '0';
5512 if (*p >= '0' && *p <= '7')
5513 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 break;
5517
5518 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (extra != 0)
5521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 break;
5524 }
5525 /* FALLTHROUGH */
5526
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 break;
5529 }
5530 }
5531 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 *arg = p + 1;
5537
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return OK;
5539}
5540
5541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 * Return OK or FAIL.
5544 */
5545 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 int evaluate;
5550{
5551 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005552 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005553 int reduce = 0;
5554
5555 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005556 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005557 */
5558 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5559 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005563 break;
5564 ++reduce;
5565 ++p;
5566 }
5567 }
5568
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005572 return FAIL;
5573 }
5574
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 if (!evaluate)
5577 {
5578 *arg = p + 1;
5579 return OK;
5580 }
5581
5582 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 */
5585 str = alloc((unsigned)((p - *arg) - reduce));
5586 if (str == NULL)
5587 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 rettv->v_type = VAR_STRING;
5589 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005590
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 break;
5597 ++p;
5598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 *arg = p + 1;
5603
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 * Allocate a variable for a List and fill it from "*arg".
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 int evaluate;
5616{
Bram Moolenaar33570922005-01-25 22:26:29 +00005617 list_T *l = NULL;
5618 typval_T tv;
5619 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620
5621 if (evaluate)
5622 {
5623 l = list_alloc();
5624 if (l == NULL)
5625 return FAIL;
5626 }
5627
5628 *arg = skipwhite(*arg + 1);
5629 while (**arg != ']' && **arg != NUL)
5630 {
5631 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5632 goto failret;
5633 if (evaluate)
5634 {
5635 item = listitem_alloc();
5636 if (item != NULL)
5637 {
5638 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005639 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 list_append(l, item);
5641 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005642 else
5643 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 }
5645
5646 if (**arg == ']')
5647 break;
5648 if (**arg != ',')
5649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 goto failret;
5652 }
5653 *arg = skipwhite(*arg + 1);
5654 }
5655
5656 if (**arg != ']')
5657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659failret:
5660 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005661 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 return FAIL;
5663 }
5664
5665 *arg = skipwhite(*arg + 1);
5666 if (evaluate)
5667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 rettv->v_type = VAR_LIST;
5669 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 ++l->lv_refcount;
5671 }
5672
5673 return OK;
5674}
5675
5676/*
5677 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005678 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005680 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681list_alloc()
5682{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005683 list_T *l;
5684
5685 l = (list_T *)alloc_clear(sizeof(list_T));
5686 if (l != NULL)
5687 {
5688 /* Prepend the list to the list of lists for garbage collection. */
5689 if (first_list != NULL)
5690 first_list->lv_used_prev = l;
5691 l->lv_used_prev = NULL;
5692 l->lv_used_next = first_list;
5693 first_list = l;
5694 }
5695 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696}
5697
5698/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005699 * Allocate an empty list for a return value.
5700 * Returns OK or FAIL.
5701 */
5702 static int
5703rettv_list_alloc(rettv)
5704 typval_T *rettv;
5705{
5706 list_T *l = list_alloc();
5707
5708 if (l == NULL)
5709 return FAIL;
5710
5711 rettv->vval.v_list = l;
5712 rettv->v_type = VAR_LIST;
5713 ++l->lv_refcount;
5714 return OK;
5715}
5716
5717/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 * Unreference a list: decrement the reference count and free it when it
5719 * becomes zero.
5720 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005721 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005725 if (l != NULL && --l->lv_refcount <= 0)
5726 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727}
5728
5729/*
5730 * Free a list, including all items it points to.
5731 * Ignores the reference count.
5732 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005733 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005734list_free(l, recurse)
5735 list_T *l;
5736 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737{
Bram Moolenaar33570922005-01-25 22:26:29 +00005738 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005740 /* Remove the list from the list of lists for garbage collection. */
5741 if (l->lv_used_prev == NULL)
5742 first_list = l->lv_used_next;
5743 else
5744 l->lv_used_prev->lv_used_next = l->lv_used_next;
5745 if (l->lv_used_next != NULL)
5746 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5747
Bram Moolenaard9fba312005-06-26 22:34:35 +00005748 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005750 /* Remove the item before deleting it. */
5751 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005752 if (recurse || (item->li_tv.v_type != VAR_LIST
5753 && item->li_tv.v_type != VAR_DICT))
5754 clear_tv(&item->li_tv);
5755 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 }
5757 vim_free(l);
5758}
5759
5760/*
5761 * Allocate a list item.
5762 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005763 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764listitem_alloc()
5765{
Bram Moolenaar33570922005-01-25 22:26:29 +00005766 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767}
5768
5769/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005770 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 */
5772 static void
5773listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005776 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 vim_free(item);
5778}
5779
5780/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005781 * Remove a list item from a List and free it. Also clears the value.
5782 */
5783 static void
5784listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 list_T *l;
5786 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005787{
5788 list_remove(l, item, item);
5789 listitem_free(item);
5790}
5791
5792/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 * Get the number of items in a list.
5794 */
5795 static long
5796list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 if (l == NULL)
5800 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005801 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802}
5803
5804/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005805 * Return TRUE when two lists have exactly the same values.
5806 */
5807 static int
5808list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l1;
5810 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811 int ic; /* ignore case for strings */
5812{
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005814
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005815 if (l1 == l2)
5816 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005817 if (list_len(l1) != list_len(l2))
5818 return FALSE;
5819
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005820 for (item1 = l1->lv_first, item2 = l2->lv_first;
5821 item1 != NULL && item2 != NULL;
5822 item1 = item1->li_next, item2 = item2->li_next)
5823 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5824 return FALSE;
5825 return item1 == NULL && item2 == NULL;
5826}
5827
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005828#if defined(FEAT_PYTHON) || defined(PROTO)
5829/*
5830 * Return the dictitem that an entry in a hashtable points to.
5831 */
5832 dictitem_T *
5833dict_lookup(hi)
5834 hashitem_T *hi;
5835{
5836 return HI2DI(hi);
5837}
5838#endif
5839
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005841 * Return TRUE when two dictionaries have exactly the same key/values.
5842 */
5843 static int
5844dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 dict_T *d1;
5846 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005847 int ic; /* ignore case for strings */
5848{
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 hashitem_T *hi;
5850 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005852
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005853 if (d1 == d2)
5854 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005855 if (dict_len(d1) != dict_len(d2))
5856 return FALSE;
5857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005858 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005860 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005861 if (!HASHITEM_EMPTY(hi))
5862 {
5863 item2 = dict_find(d2, hi->hi_key, -1);
5864 if (item2 == NULL)
5865 return FALSE;
5866 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5867 return FALSE;
5868 --todo;
5869 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005870 }
5871 return TRUE;
5872}
5873
5874/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005875 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005876 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005877 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005878 */
5879 static int
5880tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 typval_T *tv1;
5882 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 int ic; /* ignore case */
5884{
5885 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005886 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005887 static int recursive = 0; /* cach recursive loops */
5888 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005890 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005891 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005892 /* Catch lists and dicts that have an endless loop by limiting
5893 * recursiveness to 1000. We guess they are equal then. */
5894 if (recursive >= 1000)
5895 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005896
5897 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005899 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005900 ++recursive;
5901 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5902 --recursive;
5903 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005904
5905 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005906 ++recursive;
5907 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5908 --recursive;
5909 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005910
5911 case VAR_FUNC:
5912 return (tv1->vval.v_string != NULL
5913 && tv2->vval.v_string != NULL
5914 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5915
5916 case VAR_NUMBER:
5917 return tv1->vval.v_number == tv2->vval.v_number;
5918
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005919#ifdef FEAT_FLOAT
5920 case VAR_FLOAT:
5921 return tv1->vval.v_float == tv2->vval.v_float;
5922#endif
5923
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005924 case VAR_STRING:
5925 s1 = get_tv_string_buf(tv1, buf1);
5926 s2 = get_tv_string_buf(tv2, buf2);
5927 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005929
5930 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 return TRUE;
5932}
5933
5934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 * Locate item with index "n" in list "l" and return it.
5936 * A negative index is counted from the end; -1 is the last item.
5937 * Returns NULL when "n" is out of range.
5938 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 long n;
5943{
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 long idx;
5946
5947 if (l == NULL)
5948 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005949
5950 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005952 n = l->lv_len + n;
5953
5954 /* Check for index out of range. */
5955 if (n < 0 || n >= l->lv_len)
5956 return NULL;
5957
5958 /* When there is a cached index may start search from there. */
5959 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005961 if (n < l->lv_idx / 2)
5962 {
5963 /* closest to the start of the list */
5964 item = l->lv_first;
5965 idx = 0;
5966 }
5967 else if (n > (l->lv_idx + l->lv_len) / 2)
5968 {
5969 /* closest to the end of the list */
5970 item = l->lv_last;
5971 idx = l->lv_len - 1;
5972 }
5973 else
5974 {
5975 /* closest to the cached index */
5976 item = l->lv_idx_item;
5977 idx = l->lv_idx;
5978 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 }
5980 else
5981 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005982 if (n < l->lv_len / 2)
5983 {
5984 /* closest to the start of the list */
5985 item = l->lv_first;
5986 idx = 0;
5987 }
5988 else
5989 {
5990 /* closest to the end of the list */
5991 item = l->lv_last;
5992 idx = l->lv_len - 1;
5993 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005995
5996 while (n > idx)
5997 {
5998 /* search forward */
5999 item = item->li_next;
6000 ++idx;
6001 }
6002 while (n < idx)
6003 {
6004 /* search backward */
6005 item = item->li_prev;
6006 --idx;
6007 }
6008
6009 /* cache the used index */
6010 l->lv_idx = idx;
6011 l->lv_idx_item = item;
6012
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 return item;
6014}
6015
6016/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006017 * Get list item "l[idx]" as a number.
6018 */
6019 static long
6020list_find_nr(l, idx, errorp)
6021 list_T *l;
6022 long idx;
6023 int *errorp; /* set to TRUE when something wrong */
6024{
6025 listitem_T *li;
6026
6027 li = list_find(l, idx);
6028 if (li == NULL)
6029 {
6030 if (errorp != NULL)
6031 *errorp = TRUE;
6032 return -1L;
6033 }
6034 return get_tv_number_chk(&li->li_tv, errorp);
6035}
6036
6037/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006038 * Locate "item" list "l" and return its index.
6039 * Returns -1 when "item" is not in the list.
6040 */
6041 static long
6042list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 list_T *l;
6044 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006045{
6046 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006048
6049 if (l == NULL)
6050 return -1;
6051 idx = 0;
6052 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6053 ++idx;
6054 if (li == NULL)
6055 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006056 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006057}
6058
6059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 * Append item "item" to the end of list "l".
6061 */
6062 static void
6063list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 list_T *l;
6065 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
6067 if (l->lv_last == NULL)
6068 {
6069 /* empty list */
6070 l->lv_first = item;
6071 l->lv_last = item;
6072 item->li_prev = NULL;
6073 }
6074 else
6075 {
6076 l->lv_last->li_next = item;
6077 item->li_prev = l->lv_last;
6078 l->lv_last = item;
6079 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 item->li_next = NULL;
6082}
6083
6084/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 */
6088 static int
6089list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 list_T *l;
6091 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006093 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094
Bram Moolenaar05159a02005-02-26 23:04:13 +00006095 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006097 copy_tv(tv, &li->li_tv);
6098 list_append(l, li);
6099 return OK;
6100}
6101
6102/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006103 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006104 * Return FAIL when out of memory.
6105 */
6106 int
6107list_append_dict(list, dict)
6108 list_T *list;
6109 dict_T *dict;
6110{
6111 listitem_T *li = listitem_alloc();
6112
6113 if (li == NULL)
6114 return FAIL;
6115 li->li_tv.v_type = VAR_DICT;
6116 li->li_tv.v_lock = 0;
6117 li->li_tv.vval.v_dict = dict;
6118 list_append(list, li);
6119 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 return OK;
6121}
6122
6123/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006124 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006125 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006126 * Returns FAIL when out of memory.
6127 */
6128 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006129list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006130 list_T *l;
6131 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006132 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006133{
6134 listitem_T *li = listitem_alloc();
6135
6136 if (li == NULL)
6137 return FAIL;
6138 list_append(l, li);
6139 li->li_tv.v_type = VAR_STRING;
6140 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006141 if (str == NULL)
6142 li->li_tv.vval.v_string = NULL;
6143 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006144 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006145 return FAIL;
6146 return OK;
6147}
6148
6149/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006150 * Append "n" to list "l".
6151 * Returns FAIL when out of memory.
6152 */
6153 static int
6154list_append_number(l, n)
6155 list_T *l;
6156 varnumber_T n;
6157{
6158 listitem_T *li;
6159
6160 li = listitem_alloc();
6161 if (li == NULL)
6162 return FAIL;
6163 li->li_tv.v_type = VAR_NUMBER;
6164 li->li_tv.v_lock = 0;
6165 li->li_tv.vval.v_number = n;
6166 list_append(l, li);
6167 return OK;
6168}
6169
6170/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006172 * If "item" is NULL append at the end.
6173 * Return FAIL when out of memory.
6174 */
6175 static int
6176list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 list_T *l;
6178 typval_T *tv;
6179 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182
6183 if (ni == NULL)
6184 return FAIL;
6185 copy_tv(tv, &ni->li_tv);
6186 if (item == NULL)
6187 /* Append new item at end of list. */
6188 list_append(l, ni);
6189 else
6190 {
6191 /* Insert new item before existing item. */
6192 ni->li_prev = item->li_prev;
6193 ni->li_next = item;
6194 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006195 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197 ++l->lv_idx;
6198 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006202 l->lv_idx_item = NULL;
6203 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 }
6207 return OK;
6208}
6209
6210/*
6211 * Extend "l1" with "l2".
6212 * If "bef" is NULL append at the end, otherwise insert before this item.
6213 * Returns FAIL when out of memory.
6214 */
6215 static int
6216list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 list_T *l1;
6218 list_T *l2;
6219 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220{
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222
6223 for (item = l2->lv_first; item != NULL; item = item->li_next)
6224 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6225 return FAIL;
6226 return OK;
6227}
6228
6229/*
6230 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6231 * Return FAIL when out of memory.
6232 */
6233 static int
6234list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 list_T *l1;
6236 list_T *l2;
6237 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238{
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240
6241 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006242 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243 if (l == NULL)
6244 return FAIL;
6245 tv->v_type = VAR_LIST;
6246 tv->vval.v_list = l;
6247
6248 /* append all items from the second list */
6249 return list_extend(l, l2, NULL);
6250}
6251
6252/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006255 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * Returns NULL when out of memory.
6257 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006259list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006262 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263{
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *copy;
6265 listitem_T *item;
6266 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267
6268 if (orig == NULL)
6269 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270
6271 copy = list_alloc();
6272 if (copy != NULL)
6273 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006274 if (copyID != 0)
6275 {
6276 /* Do this before adding the items, because one of the items may
6277 * refer back to this list. */
6278 orig->lv_copyID = copyID;
6279 orig->lv_copylist = copy;
6280 }
6281 for (item = orig->lv_first; item != NULL && !got_int;
6282 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 {
6284 ni = listitem_alloc();
6285 if (ni == NULL)
6286 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006288 {
6289 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6290 {
6291 vim_free(ni);
6292 break;
6293 }
6294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006296 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 list_append(copy, ni);
6298 }
6299 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006300 if (item != NULL)
6301 {
6302 list_unref(copy);
6303 copy = NULL;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
6306
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 return copy;
6308}
6309
6310/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006312 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006315list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
6318 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006319{
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006322 /* notify watchers */
6323 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006325 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 list_fix_watch(l, ip);
6327 if (ip == item2)
6328 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330
6331 if (item2->li_next == NULL)
6332 l->lv_last = item->li_prev;
6333 else
6334 item2->li_next->li_prev = item->li_prev;
6335 if (item->li_prev == NULL)
6336 l->lv_first = item2->li_next;
6337 else
6338 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006339 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340}
6341
6342/*
6343 * Return an allocated string with the string representation of a list.
6344 * May return NULL.
6345 */
6346 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006347list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006348 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006349 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350{
6351 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352
6353 if (tv->vval.v_list == NULL)
6354 return NULL;
6355 ga_init2(&ga, (int)sizeof(char), 80);
6356 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006357 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 {
6359 vim_free(ga.ga_data);
6360 return NULL;
6361 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 ga_append(&ga, ']');
6363 ga_append(&ga, NUL);
6364 return (char_u *)ga.ga_data;
6365}
6366
6367/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006368 * Join list "l" into a string in "*gap", using separator "sep".
6369 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006370 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006371 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006372 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006373list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006374 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006376 char_u *sep;
6377 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006378 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006379{
6380 int first = TRUE;
6381 char_u *tofree;
6382 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006384 char_u *s;
6385
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006386 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006387 {
6388 if (first)
6389 first = FALSE;
6390 else
6391 ga_concat(gap, sep);
6392
6393 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006394 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006395 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006396 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006397 if (s != NULL)
6398 ga_concat(gap, s);
6399 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (s == NULL)
6401 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006402 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006403 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006404}
6405
6406/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006407 * Garbage collection for lists and dictionaries.
6408 *
6409 * We use reference counts to be able to free most items right away when they
6410 * are no longer used. But for composite items it's possible that it becomes
6411 * unused while the reference count is > 0: When there is a recursive
6412 * reference. Example:
6413 * :let l = [1, 2, 3]
6414 * :let d = {9: l}
6415 * :let l[1] = d
6416 *
6417 * Since this is quite unusual we handle this with garbage collection: every
6418 * once in a while find out which lists and dicts are not referenced from any
6419 * variable.
6420 *
6421 * Here is a good reference text about garbage collection (refers to Python
6422 * but it applies to all reference-counting mechanisms):
6423 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006424 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006425
6426/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006427 * Do garbage collection for lists and dicts.
6428 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006429 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006430 int
6431garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006432{
6433 dict_T *dd;
6434 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006435 int copyID = ++current_copyID;
6436 buf_T *buf;
6437 win_T *wp;
6438 int i;
6439 funccall_T *fc;
6440 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006441#ifdef FEAT_WINDOWS
6442 tabpage_T *tp;
6443#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006444
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006445 /* Only do this once. */
6446 want_garbage_collect = FALSE;
6447 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006448 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006449
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006450 /*
6451 * 1. Go through all accessible variables and mark all lists and dicts
6452 * with copyID.
6453 */
6454 /* script-local variables */
6455 for (i = 1; i <= ga_scripts.ga_len; ++i)
6456 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6457
6458 /* buffer-local variables */
6459 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6460 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6461
6462 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006463 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006464 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6465
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466#ifdef FEAT_WINDOWS
6467 /* tabpage-local variables */
6468 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6469 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6470#endif
6471
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006472 /* global variables */
6473 set_ref_in_ht(&globvarht, copyID);
6474
6475 /* function-local variables */
6476 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6477 {
6478 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6479 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6480 }
6481
6482 /*
6483 * 2. Go through the list of dicts and free items without the copyID.
6484 */
6485 for (dd = first_dict; dd != NULL; )
6486 if (dd->dv_copyID != copyID)
6487 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006488 /* Free the Dictionary and ordinary items it contains, but don't
6489 * recurse into Lists and Dictionaries, they will be in the list
6490 * of dicts or list of lists. */
6491 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006492 did_free = TRUE;
6493
6494 /* restart, next dict may also have been freed */
6495 dd = first_dict;
6496 }
6497 else
6498 dd = dd->dv_used_next;
6499
6500 /*
6501 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006502 * But don't free a list that has a watcher (used in a for loop), these
6503 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006504 */
6505 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006506 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006507 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006508 /* Free the List and ordinary items it contains, but don't recurse
6509 * into Lists and Dictionaries, they will be in the list of dicts
6510 * or list of lists. */
6511 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006512 did_free = TRUE;
6513
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006514 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006515 ll = first_list;
6516 }
6517 else
6518 ll = ll->lv_used_next;
6519
6520 return did_free;
6521}
6522
6523/*
6524 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6525 */
6526 static void
6527set_ref_in_ht(ht, copyID)
6528 hashtab_T *ht;
6529 int copyID;
6530{
6531 int todo;
6532 hashitem_T *hi;
6533
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006534 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006535 for (hi = ht->ht_array; todo > 0; ++hi)
6536 if (!HASHITEM_EMPTY(hi))
6537 {
6538 --todo;
6539 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6540 }
6541}
6542
6543/*
6544 * Mark all lists and dicts referenced through list "l" with "copyID".
6545 */
6546 static void
6547set_ref_in_list(l, copyID)
6548 list_T *l;
6549 int copyID;
6550{
6551 listitem_T *li;
6552
6553 for (li = l->lv_first; li != NULL; li = li->li_next)
6554 set_ref_in_item(&li->li_tv, copyID);
6555}
6556
6557/*
6558 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6559 */
6560 static void
6561set_ref_in_item(tv, copyID)
6562 typval_T *tv;
6563 int copyID;
6564{
6565 dict_T *dd;
6566 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006567
6568 switch (tv->v_type)
6569 {
6570 case VAR_DICT:
6571 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006572 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006573 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 /* Didn't see this dict yet. */
6575 dd->dv_copyID = copyID;
6576 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006577 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006578 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006579
6580 case VAR_LIST:
6581 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006583 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006584 /* Didn't see this list yet. */
6585 ll->lv_copyID = copyID;
6586 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006587 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006588 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006589 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006590 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006591}
6592
6593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 * Allocate an empty header for a dictionary.
6595 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006596 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597dict_alloc()
6598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006600
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006602 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006603 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006604 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006605 if (first_dict != NULL)
6606 first_dict->dv_used_prev = d;
6607 d->dv_used_next = first_dict;
6608 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006609 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006610
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006612 d->dv_lock = 0;
6613 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006615 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006616 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617}
6618
6619/*
6620 * Unreference a Dictionary: decrement the reference count and free it when it
6621 * becomes zero.
6622 */
6623 static void
6624dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006627 if (d != NULL && --d->dv_refcount <= 0)
6628 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629}
6630
6631/*
6632 * Free a Dictionary, including all items it contains.
6633 * Ignores the reference count.
6634 */
6635 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006636dict_free(d, recurse)
6637 dict_T *d;
6638 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006640 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006642 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006644 /* Remove the dict from the list of dicts for garbage collection. */
6645 if (d->dv_used_prev == NULL)
6646 first_dict = d->dv_used_next;
6647 else
6648 d->dv_used_prev->dv_used_next = d->dv_used_next;
6649 if (d->dv_used_next != NULL)
6650 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6651
6652 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006653 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006654 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006657 if (!HASHITEM_EMPTY(hi))
6658 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006659 /* Remove the item before deleting it, just in case there is
6660 * something recursive causing trouble. */
6661 di = HI2DI(hi);
6662 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006663 if (recurse || (di->di_tv.v_type != VAR_LIST
6664 && di->di_tv.v_type != VAR_DICT))
6665 clear_tv(&di->di_tv);
6666 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006667 --todo;
6668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 vim_free(d);
6672}
6673
6674/*
6675 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006676 * The "key" is copied to the new item.
6677 * Note that the value of the item "di_tv" still needs to be initialized!
6678 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681dictitem_alloc(key)
6682 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683{
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006685
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006686 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006687 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006690 di->di_flags = 0;
6691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006692 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693}
6694
6695/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 * Make a copy of a Dictionary item.
6697 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006698 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006700 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701{
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006704 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6705 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706 if (di != NULL)
6707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006708 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006709 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 copy_tv(&org->di_tv, &di->di_tv);
6711 }
6712 return di;
6713}
6714
6715/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 * Remove item "item" from Dictionary "dict" and free it.
6717 */
6718 static void
6719dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 dict_T *dict;
6721 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006722{
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006724
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006726 if (HASHITEM_EMPTY(hi))
6727 EMSG2(_(e_intern2), "dictitem_remove()");
6728 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006730 dictitem_free(item);
6731}
6732
6733/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006734 * Free a dict item. Also clears the value.
6735 */
6736 static void
6737dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006738 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 clear_tv(&item->di_tv);
6741 vim_free(item);
6742}
6743
6744/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6746 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006747 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748 * Returns NULL when out of memory.
6749 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006750 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006751dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006752 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006753 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006754 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755{
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 dict_T *copy;
6757 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006758 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760
6761 if (orig == NULL)
6762 return NULL;
6763
6764 copy = dict_alloc();
6765 if (copy != NULL)
6766 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 if (copyID != 0)
6768 {
6769 orig->dv_copyID = copyID;
6770 orig->dv_copydict = copy;
6771 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006772 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006773 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777 --todo;
6778
6779 di = dictitem_alloc(hi->hi_key);
6780 if (di == NULL)
6781 break;
6782 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 {
6784 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6785 copyID) == FAIL)
6786 {
6787 vim_free(di);
6788 break;
6789 }
6790 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006791 else
6792 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6793 if (dict_add(copy, di) == FAIL)
6794 {
6795 dictitem_free(di);
6796 break;
6797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006799 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006800
Bram Moolenaare9a41262005-01-15 22:18:47 +00006801 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 if (todo > 0)
6803 {
6804 dict_unref(copy);
6805 copy = NULL;
6806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006807 }
6808
6809 return copy;
6810}
6811
6812/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006814 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006816 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 dict_T *d;
6819 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820{
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822}
6823
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006825 * Add a number or string entry to dictionary "d".
6826 * When "str" is NULL use number "nr", otherwise use "str".
6827 * Returns FAIL when out of memory and when key already exists.
6828 */
6829 int
6830dict_add_nr_str(d, key, nr, str)
6831 dict_T *d;
6832 char *key;
6833 long nr;
6834 char_u *str;
6835{
6836 dictitem_T *item;
6837
6838 item = dictitem_alloc((char_u *)key);
6839 if (item == NULL)
6840 return FAIL;
6841 item->di_tv.v_lock = 0;
6842 if (str == NULL)
6843 {
6844 item->di_tv.v_type = VAR_NUMBER;
6845 item->di_tv.vval.v_number = nr;
6846 }
6847 else
6848 {
6849 item->di_tv.v_type = VAR_STRING;
6850 item->di_tv.vval.v_string = vim_strsave(str);
6851 }
6852 if (dict_add(d, item) == FAIL)
6853 {
6854 dictitem_free(item);
6855 return FAIL;
6856 }
6857 return OK;
6858}
6859
6860/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006861 * Get the number of items in a Dictionary.
6862 */
6863 static long
6864dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006865 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006866{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867 if (d == NULL)
6868 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006869 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870}
6871
6872/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 * Find item "key[len]" in Dictionary "d".
6874 * If "len" is negative use strlen(key).
6875 * Returns NULL when not found.
6876 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880 char_u *key;
6881 int len;
6882{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006883#define AKEYLEN 200
6884 char_u buf[AKEYLEN];
6885 char_u *akey;
6886 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006889 if (len < 0)
6890 akey = key;
6891 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006892 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893 tofree = akey = vim_strnsave(key, len);
6894 if (akey == NULL)
6895 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006896 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006897 else
6898 {
6899 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006900 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901 akey = buf;
6902 }
6903
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 vim_free(tofree);
6906 if (HASHITEM_EMPTY(hi))
6907 return NULL;
6908 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909}
6910
6911/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006912 * Get a string item from a dictionary.
6913 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006914 * Returns NULL if the entry doesn't exist or out of memory.
6915 */
6916 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006917get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006918 dict_T *d;
6919 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006920 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006921{
6922 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006923 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006924
6925 di = dict_find(d, key, -1);
6926 if (di == NULL)
6927 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006928 s = get_tv_string(&di->di_tv);
6929 if (save && s != NULL)
6930 s = vim_strsave(s);
6931 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006932}
6933
6934/*
6935 * Get a number item from a dictionary.
6936 * Returns 0 if the entry doesn't exist or out of memory.
6937 */
6938 long
6939get_dict_number(d, key)
6940 dict_T *d;
6941 char_u *key;
6942{
6943 dictitem_T *di;
6944
6945 di = dict_find(d, key, -1);
6946 if (di == NULL)
6947 return 0;
6948 return get_tv_number(&di->di_tv);
6949}
6950
6951/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006952 * Return an allocated string with the string representation of a Dictionary.
6953 * May return NULL.
6954 */
6955 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006956dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006958 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006959{
6960 garray_T ga;
6961 int first = TRUE;
6962 char_u *tofree;
6963 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 return NULL;
6971 ga_init2(&ga, (int)sizeof(char), 80);
6972 ga_append(&ga, '{');
6973
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006974 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006975 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006977 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006979 --todo;
6980
6981 if (first)
6982 first = FALSE;
6983 else
6984 ga_concat(&ga, (char_u *)", ");
6985
6986 tofree = string_quote(hi->hi_key, FALSE);
6987 if (tofree != NULL)
6988 {
6989 ga_concat(&ga, tofree);
6990 vim_free(tofree);
6991 }
6992 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006993 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006994 if (s != NULL)
6995 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006997 if (s == NULL)
6998 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007000 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 if (todo > 0)
7002 {
7003 vim_free(ga.ga_data);
7004 return NULL;
7005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006
7007 ga_append(&ga, '}');
7008 ga_append(&ga, NUL);
7009 return (char_u *)ga.ga_data;
7010}
7011
7012/*
7013 * Allocate a variable for a Dictionary and fill it from "*arg".
7014 * Return OK or FAIL. Returns NOTDONE for {expr}.
7015 */
7016 static int
7017get_dict_tv(arg, rettv, evaluate)
7018 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020 int evaluate;
7021{
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d = NULL;
7023 typval_T tvkey;
7024 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007025 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007028 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029
7030 /*
7031 * First check if it's not a curly-braces thing: {expr}.
7032 * Must do this without evaluating, otherwise a function may be called
7033 * twice. Unfortunately this means we need to call eval1() twice for the
7034 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037 if (*start != '}')
7038 {
7039 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7040 return FAIL;
7041 if (*start == '}')
7042 return NOTDONE;
7043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044
7045 if (evaluate)
7046 {
7047 d = dict_alloc();
7048 if (d == NULL)
7049 return FAIL;
7050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 tvkey.v_type = VAR_UNKNOWN;
7052 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053
7054 *arg = skipwhite(*arg + 1);
7055 while (**arg != '}' && **arg != NUL)
7056 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 goto failret;
7059 if (**arg != ':')
7060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007061 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063 goto failret;
7064 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007065 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007067 key = get_tv_string_buf_chk(&tvkey, buf);
7068 if (key == NULL || *key == NUL)
7069 {
7070 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7071 if (key != NULL)
7072 EMSG(_(e_emptykey));
7073 clear_tv(&tvkey);
7074 goto failret;
7075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077
7078 *arg = skipwhite(*arg + 1);
7079 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7080 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007081 if (evaluate)
7082 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 goto failret;
7084 }
7085 if (evaluate)
7086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 if (item != NULL)
7089 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007090 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 clear_tv(&tv);
7093 goto failret;
7094 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007095 item = dictitem_alloc(key);
7096 clear_tv(&tvkey);
7097 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007100 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 if (dict_add(d, item) == FAIL)
7102 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 }
7104 }
7105
7106 if (**arg == '}')
7107 break;
7108 if (**arg != ',')
7109 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007110 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111 goto failret;
7112 }
7113 *arg = skipwhite(*arg + 1);
7114 }
7115
7116 if (**arg != '}')
7117 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007118 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119failret:
7120 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007121 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122 return FAIL;
7123 }
7124
7125 *arg = skipwhite(*arg + 1);
7126 if (evaluate)
7127 {
7128 rettv->v_type = VAR_DICT;
7129 rettv->vval.v_dict = d;
7130 ++d->dv_refcount;
7131 }
7132
7133 return OK;
7134}
7135
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007137 * Return a string with the string representation of a variable.
7138 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007139 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007140 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007141 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007142 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007143 */
7144 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007145echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007147 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007148 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007149 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007150{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151 static int recurse = 0;
7152 char_u *r = NULL;
7153
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007156 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 *tofree = NULL;
7158 return NULL;
7159 }
7160 ++recurse;
7161
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007162 switch (tv->v_type)
7163 {
7164 case VAR_FUNC:
7165 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 r = tv->vval.v_string;
7167 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007168
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007170 if (tv->vval.v_list == NULL)
7171 {
7172 *tofree = NULL;
7173 r = NULL;
7174 }
7175 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7176 {
7177 *tofree = NULL;
7178 r = (char_u *)"[...]";
7179 }
7180 else
7181 {
7182 tv->vval.v_list->lv_copyID = copyID;
7183 *tofree = list2string(tv, copyID);
7184 r = *tofree;
7185 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007187
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007189 if (tv->vval.v_dict == NULL)
7190 {
7191 *tofree = NULL;
7192 r = NULL;
7193 }
7194 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7195 {
7196 *tofree = NULL;
7197 r = (char_u *)"{...}";
7198 }
7199 else
7200 {
7201 tv->vval.v_dict->dv_copyID = copyID;
7202 *tofree = dict2string(tv, copyID);
7203 r = *tofree;
7204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007207 case VAR_STRING:
7208 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 *tofree = NULL;
7210 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007211 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007213#ifdef FEAT_FLOAT
7214 case VAR_FLOAT:
7215 *tofree = NULL;
7216 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7217 r = numbuf;
7218 break;
7219#endif
7220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007222 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007224 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225
7226 --recurse;
7227 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007228}
7229
7230/*
7231 * Return a string with the string representation of a variable.
7232 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7233 * "numbuf" is used for a number.
7234 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007235 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007236 */
7237 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007238tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007240 char_u **tofree;
7241 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007242 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007243{
7244 switch (tv->v_type)
7245 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007246 case VAR_FUNC:
7247 *tofree = string_quote(tv->vval.v_string, TRUE);
7248 return *tofree;
7249 case VAR_STRING:
7250 *tofree = string_quote(tv->vval.v_string, FALSE);
7251 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007252#ifdef FEAT_FLOAT
7253 case VAR_FLOAT:
7254 *tofree = NULL;
7255 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7256 return numbuf;
7257#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007259 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007262 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007263 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007264 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007265 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007266}
7267
7268/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 * Return string "str" in ' quotes, doubling ' characters.
7270 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007272 */
7273 static char_u *
7274string_quote(str, function)
7275 char_u *str;
7276 int function;
7277{
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007279 char_u *p, *r, *s;
7280
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 len = (function ? 13 : 3);
7282 if (str != NULL)
7283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007284 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 for (p = str; *p != NUL; mb_ptr_adv(p))
7286 if (*p == '\'')
7287 ++len;
7288 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007289 s = r = alloc(len);
7290 if (r != NULL)
7291 {
7292 if (function)
7293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007295 r += 10;
7296 }
7297 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 if (str != NULL)
7300 for (p = str; *p != NUL; )
7301 {
7302 if (*p == '\'')
7303 *r++ = '\'';
7304 MB_COPY_CHAR(p, r);
7305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007307 if (function)
7308 *r++ = ')';
7309 *r++ = NUL;
7310 }
7311 return s;
7312}
7313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007314#ifdef FEAT_FLOAT
7315/*
7316 * Convert the string "text" to a floating point number.
7317 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7318 * this always uses a decimal point.
7319 * Returns the length of the text that was consumed.
7320 */
7321 static int
7322string2float(text, value)
7323 char_u *text;
7324 float_T *value; /* result stored here */
7325{
7326 char *s = (char *)text;
7327 float_T f;
7328
7329 f = strtod(s, &s);
7330 *value = f;
7331 return (int)((char_u *)s - text);
7332}
7333#endif
7334
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 * Get the value of an environment variable.
7337 * "arg" is pointing to the '$'. It is advanced to after the name.
7338 * If the environment variable was not set, silently assume it is empty.
7339 * Always return OK.
7340 */
7341 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 int evaluate;
7346{
7347 char_u *string = NULL;
7348 int len;
7349 int cc;
7350 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007351 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
7353 ++*arg;
7354 name = *arg;
7355 len = get_env_len(arg);
7356 if (evaluate)
7357 {
7358 if (len != 0)
7359 {
7360 cc = name[len];
7361 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007362 /* first try vim_getenv(), fast for normal environment vars */
7363 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007365 {
7366 if (!mustfree)
7367 string = vim_strsave(string);
7368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 else
7370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007371 if (mustfree)
7372 vim_free(string);
7373
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 /* next try expanding things like $VIM and ${HOME} */
7375 string = expand_env_save(name - 1);
7376 if (string != NULL && *string == '$')
7377 {
7378 vim_free(string);
7379 string = NULL;
7380 }
7381 }
7382 name[len] = cc;
7383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007384 rettv->v_type = VAR_STRING;
7385 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 }
7387
7388 return OK;
7389}
7390
7391/*
7392 * Array with names and number of arguments of all internal functions
7393 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7394 */
7395static struct fst
7396{
7397 char *f_name; /* function name */
7398 char f_min_argc; /* minimal number of arguments */
7399 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007401 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402} functions[] =
7403{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007404#ifdef FEAT_FLOAT
7405 {"abs", 1, 1, f_abs},
7406#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007407 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {"append", 2, 2, f_append},
7409 {"argc", 0, 0, f_argc},
7410 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007411 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007412#ifdef FEAT_FLOAT
7413 {"atan", 1, 1, f_atan},
7414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007416 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {"bufexists", 1, 1, f_bufexists},
7418 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7419 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7420 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7421 {"buflisted", 1, 1, f_buflisted},
7422 {"bufloaded", 1, 1, f_bufloaded},
7423 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007424 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 {"bufwinnr", 1, 1, f_bufwinnr},
7426 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007427 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007429#ifdef FEAT_FLOAT
7430 {"ceil", 1, 1, f_ceil},
7431#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007432 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {"char2nr", 1, 1, f_char2nr},
7434 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007435 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007437#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007438 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007439 {"complete_add", 1, 1, f_complete_add},
7440 {"complete_check", 0, 0, f_complete_check},
7441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007443 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007444#ifdef FEAT_FLOAT
7445 {"cos", 1, 1, f_cos},
7446#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007449 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {"delete", 1, 1, f_delete},
7452 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007453 {"diff_filler", 1, 1, f_diff_filler},
7454 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007455 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007457 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {"eventhandler", 0, 0, f_eventhandler},
7459 {"executable", 1, 1, f_executable},
7460 {"exists", 1, 1, f_exists},
7461 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007462 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007463 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7465 {"filereadable", 1, 1, f_filereadable},
7466 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007467 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007468 {"finddir", 1, 3, f_finddir},
7469 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007470#ifdef FEAT_FLOAT
7471 {"float2nr", 1, 1, f_float2nr},
7472 {"floor", 1, 1, f_floor},
7473#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007474 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {"fnamemodify", 2, 2, f_fnamemodify},
7476 {"foldclosed", 1, 1, f_foldclosed},
7477 {"foldclosedend", 1, 1, f_foldclosedend},
7478 {"foldlevel", 1, 1, f_foldlevel},
7479 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007480 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007483 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007484 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007485 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {"getbufvar", 2, 2, f_getbufvar},
7487 {"getchar", 0, 1, f_getchar},
7488 {"getcharmod", 0, 0, f_getcharmod},
7489 {"getcmdline", 0, 0, f_getcmdline},
7490 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007491 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007493 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007494 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {"getfsize", 1, 1, f_getfsize},
7496 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007497 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007498 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007499 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007500 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007501 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007502 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007503 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007504 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007506 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {"getwinposx", 0, 0, f_getwinposx},
7508 {"getwinposy", 0, 0, f_getwinposy},
7509 {"getwinvar", 2, 2, f_getwinvar},
7510 {"glob", 1, 1, f_glob},
7511 {"globpath", 2, 2, f_globpath},
7512 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007513 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007514 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007515 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7517 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7518 {"histadd", 2, 2, f_histadd},
7519 {"histdel", 1, 2, f_histdel},
7520 {"histget", 1, 2, f_histget},
7521 {"histnr", 1, 1, f_histnr},
7522 {"hlID", 1, 1, f_hlID},
7523 {"hlexists", 1, 1, f_hlexists},
7524 {"hostname", 0, 0, f_hostname},
7525 {"iconv", 3, 3, f_iconv},
7526 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007528 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007530 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {"inputrestore", 0, 0, f_inputrestore},
7532 {"inputsave", 0, 0, f_inputsave},
7533 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007536 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007541 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {"libcall", 3, 3, f_libcall},
7543 {"libcallnr", 3, 3, f_libcallnr},
7544 {"line", 1, 1, f_line},
7545 {"line2byte", 1, 1, f_line2byte},
7546 {"lispindent", 1, 1, f_lispindent},
7547 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007548#ifdef FEAT_FLOAT
7549 {"log10", 1, 1, f_log10},
7550#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007551 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007552 {"maparg", 1, 3, f_maparg},
7553 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007554 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007555 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007556 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007557 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007558 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007559 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007560 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007561 {"max", 1, 1, f_max},
7562 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007563#ifdef vim_mkdir
7564 {"mkdir", 1, 3, f_mkdir},
7565#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007566 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {"nextnonblank", 1, 1, f_nextnonblank},
7568 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007569 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570#ifdef FEAT_FLOAT
7571 {"pow", 2, 2, f_pow},
7572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007574 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007575 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007577 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007578 {"reltime", 0, 2, f_reltime},
7579 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {"remote_expr", 2, 3, f_remote_expr},
7581 {"remote_foreground", 1, 1, f_remote_foreground},
7582 {"remote_peek", 1, 2, f_remote_peek},
7583 {"remote_read", 1, 1, f_remote_read},
7584 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007585 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007587 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007589 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007590#ifdef FEAT_FLOAT
7591 {"round", 1, 1, f_round},
7592#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007593 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007594 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007595 {"searchpair", 3, 7, f_searchpair},
7596 {"searchpairpos", 3, 7, f_searchpairpos},
7597 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {"server2client", 2, 2, f_server2client},
7599 {"serverlist", 0, 0, f_serverlist},
7600 {"setbufvar", 3, 3, f_setbufvar},
7601 {"setcmdpos", 1, 1, f_setcmdpos},
7602 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007603 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007604 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007605 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007606 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007608 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007610 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007612#ifdef FEAT_FLOAT
7613 {"sin", 1, 1, f_sin},
7614#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007615 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007616 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007617 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007618 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007619 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007620#ifdef FEAT_FLOAT
7621 {"sqrt", 1, 1, f_sqrt},
7622 {"str2float", 1, 1, f_str2float},
7623#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007624 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625#ifdef HAVE_STRFTIME
7626 {"strftime", 1, 2, f_strftime},
7627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {"strlen", 1, 1, f_strlen},
7631 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007632 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {"strtrans", 1, 1, f_strtrans},
7634 {"submatch", 1, 1, f_submatch},
7635 {"substitute", 4, 4, f_substitute},
7636 {"synID", 3, 3, f_synID},
7637 {"synIDattr", 2, 3, f_synIDattr},
7638 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007639 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007640 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007641 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007642 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007643 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007644 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007645 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007647 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"tolower", 1, 1, f_tolower},
7649 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007650 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#ifdef FEAT_FLOAT
7652 {"trunc", 1, 1, f_trunc},
7653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {"virtcol", 1, 1, f_virtcol},
7657 {"visualmode", 0, 1, f_visualmode},
7658 {"winbufnr", 1, 1, f_winbufnr},
7659 {"wincol", 0, 0, f_wincol},
7660 {"winheight", 1, 1, f_winheight},
7661 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007662 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007664 {"winrestview", 1, 1, f_winrestview},
7665 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007667 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668};
7669
7670#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7671
7672/*
7673 * Function given to ExpandGeneric() to obtain the list of internal
7674 * or user defined function names.
7675 */
7676 char_u *
7677get_function_name(xp, idx)
7678 expand_T *xp;
7679 int idx;
7680{
7681 static int intidx = -1;
7682 char_u *name;
7683
7684 if (idx == 0)
7685 intidx = -1;
7686 if (intidx < 0)
7687 {
7688 name = get_user_func_name(xp, idx);
7689 if (name != NULL)
7690 return name;
7691 }
7692 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7693 {
7694 STRCPY(IObuff, functions[intidx].f_name);
7695 STRCAT(IObuff, "(");
7696 if (functions[intidx].f_max_argc == 0)
7697 STRCAT(IObuff, ")");
7698 return IObuff;
7699 }
7700
7701 return NULL;
7702}
7703
7704/*
7705 * Function given to ExpandGeneric() to obtain the list of internal or
7706 * user defined variable or function names.
7707 */
7708/*ARGSUSED*/
7709 char_u *
7710get_expr_name(xp, idx)
7711 expand_T *xp;
7712 int idx;
7713{
7714 static int intidx = -1;
7715 char_u *name;
7716
7717 if (idx == 0)
7718 intidx = -1;
7719 if (intidx < 0)
7720 {
7721 name = get_function_name(xp, idx);
7722 if (name != NULL)
7723 return name;
7724 }
7725 return get_user_var_name(xp, ++intidx);
7726}
7727
7728#endif /* FEAT_CMDL_COMPL */
7729
7730/*
7731 * Find internal function in table above.
7732 * Return index, or -1 if not found
7733 */
7734 static int
7735find_internal_func(name)
7736 char_u *name; /* name of the function */
7737{
7738 int first = 0;
7739 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7740 int cmp;
7741 int x;
7742
7743 /*
7744 * Find the function name in the table. Binary search.
7745 */
7746 while (first <= last)
7747 {
7748 x = first + ((unsigned)(last - first) >> 1);
7749 cmp = STRCMP(name, functions[x].f_name);
7750 if (cmp < 0)
7751 last = x - 1;
7752 else if (cmp > 0)
7753 first = x + 1;
7754 else
7755 return x;
7756 }
7757 return -1;
7758}
7759
7760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7762 * name it contains, otherwise return "name".
7763 */
7764 static char_u *
7765deref_func_name(name, lenp)
7766 char_u *name;
7767 int *lenp;
7768{
Bram Moolenaar33570922005-01-25 22:26:29 +00007769 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770 int cc;
7771
7772 cc = name[*lenp];
7773 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007774 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 {
7780 *lenp = 0;
7781 return (char_u *)""; /* just in case */
7782 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007783 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007785 }
7786
7787 return name;
7788}
7789
7790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 * Allocate a variable for the result of a function.
7792 * Return OK or FAIL.
7793 */
7794 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7796 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 char_u *name; /* name of the function */
7798 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 char_u **arg; /* argument, pointing to the '(' */
7801 linenr_T firstline; /* first line of range */
7802 linenr_T lastline; /* last line of range */
7803 int *doesrange; /* return: function handled range */
7804 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 char_u *argp;
7808 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007809 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 int argcount = 0; /* number of arguments found */
7811
7812 /*
7813 * Get the arguments.
7814 */
7815 argp = *arg;
7816 while (argcount < MAX_FUNC_ARGS)
7817 {
7818 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7819 if (*argp == ')' || *argp == ',' || *argp == NUL)
7820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7822 {
7823 ret = FAIL;
7824 break;
7825 }
7826 ++argcount;
7827 if (*argp != ',')
7828 break;
7829 }
7830 if (*argp == ')')
7831 ++argp;
7832 else
7833 ret = FAIL;
7834
7835 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 {
7840 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007841 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007843 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 *arg = skipwhite(argp);
7850 return ret;
7851}
7852
7853
7854/*
7855 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007856 * Return OK when the function can't be called, FAIL otherwise.
7857 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 */
7859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 char_u *name; /* name of the function */
7863 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007866 typval_T *argvars; /* vars for arguments, must have "argcount"
7867 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 linenr_T firstline; /* first line of range */
7869 linenr_T lastline; /* last line of range */
7870 int *doesrange; /* return: function handled range */
7871 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875#define ERROR_UNKNOWN 0
7876#define ERROR_TOOMANY 1
7877#define ERROR_TOOFEW 2
7878#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879#define ERROR_DICT 4
7880#define ERROR_NONE 5
7881#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 int error = ERROR_NONE;
7883 int i;
7884 int llen;
7885 ufunc_T *fp;
7886 int cc;
7887#define FLEN_FIXED 40
7888 char_u fname_buf[FLEN_FIXED + 1];
7889 char_u *fname;
7890
7891 /*
7892 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7893 * Change <SNR>123_name() to K_SNR 123_name().
7894 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7895 */
7896 cc = name[len];
7897 name[len] = NUL;
7898 llen = eval_fname_script(name);
7899 if (llen > 0)
7900 {
7901 fname_buf[0] = K_SPECIAL;
7902 fname_buf[1] = KS_EXTRA;
7903 fname_buf[2] = (int)KE_SNR;
7904 i = 3;
7905 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7906 {
7907 if (current_SID <= 0)
7908 error = ERROR_SCRIPT;
7909 else
7910 {
7911 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7912 i = (int)STRLEN(fname_buf);
7913 }
7914 }
7915 if (i + STRLEN(name + llen) < FLEN_FIXED)
7916 {
7917 STRCPY(fname_buf + i, name + llen);
7918 fname = fname_buf;
7919 }
7920 else
7921 {
7922 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7923 if (fname == NULL)
7924 error = ERROR_OTHER;
7925 else
7926 {
7927 mch_memmove(fname, fname_buf, (size_t)i);
7928 STRCPY(fname + i, name + llen);
7929 }
7930 }
7931 }
7932 else
7933 fname = name;
7934
7935 *doesrange = FALSE;
7936
7937
7938 /* execute the function if no errors detected and executing */
7939 if (evaluate && error == ERROR_NONE)
7940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 error = ERROR_UNKNOWN;
7943
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007944 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 /*
7947 * User defined function.
7948 */
7949 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007950
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007952 /* Trigger FuncUndefined event, may load the function. */
7953 if (fp == NULL
7954 && apply_autocmds(EVENT_FUNCUNDEFINED,
7955 fname, fname, TRUE, NULL)
7956 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007958 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 fp = find_func(fname);
7960 }
7961#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007962 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007963 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007964 {
7965 /* loaded a package, search for the function again */
7966 fp = find_func(fname);
7967 }
7968
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (fp != NULL)
7970 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007971 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007973 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007975 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007977 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 else
7980 {
7981 /*
7982 * Call the user function.
7983 * Save and restore search patterns, script variables and
7984 * redo buffer.
7985 */
7986 save_search_patterns();
7987 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007988 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007991 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7992 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7993 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007994 /* Function was unreferenced while being used, free it
7995 * now. */
7996 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 restoreRedobuff();
7998 restore_search_patterns();
7999 error = ERROR_NONE;
8000 }
8001 }
8002 }
8003 else
8004 {
8005 /*
8006 * Find the function name in the table, call its implementation.
8007 */
8008 i = find_internal_func(fname);
8009 if (i >= 0)
8010 {
8011 if (argcount < functions[i].f_min_argc)
8012 error = ERROR_TOOFEW;
8013 else if (argcount > functions[i].f_max_argc)
8014 error = ERROR_TOOMANY;
8015 else
8016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 error = ERROR_NONE;
8020 }
8021 }
8022 }
8023 /*
8024 * The function call (or "FuncUndefined" autocommand sequence) might
8025 * have been aborted by an error, an interrupt, or an explicitly thrown
8026 * exception that has not been caught so far. This situation can be
8027 * tested for by calling aborting(). For an error in an internal
8028 * function or for the "E132" error in call_user_func(), however, the
8029 * throw point at which the "force_abort" flag (temporarily reset by
8030 * emsg()) is normally updated has not been reached yet. We need to
8031 * update that flag first to make aborting() reliable.
8032 */
8033 update_force_abort();
8034 }
8035 if (error == ERROR_NONE)
8036 ret = OK;
8037
8038 /*
8039 * Report an error unless the argument evaluation or function call has been
8040 * cancelled due to an aborting error, an interrupt, or an exception.
8041 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 if (!aborting())
8043 {
8044 switch (error)
8045 {
8046 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008048 break;
8049 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008050 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 break;
8052 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008053 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 name);
8055 break;
8056 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008057 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008058 name);
8059 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008060 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008061 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 name);
8063 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008064 }
8065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 name[len] = cc;
8068 if (fname != name && fname != fname_buf)
8069 vim_free(fname);
8070
8071 return ret;
8072}
8073
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008074/*
8075 * Give an error message with a function name. Handle <SNR> things.
8076 */
8077 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008078emsg_funcname(ermsg, name)
8079 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008080 char_u *name;
8081{
8082 char_u *p;
8083
8084 if (*name == K_SPECIAL)
8085 p = concat_str((char_u *)"<SNR>", name + 3);
8086 else
8087 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008088 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008089 if (p != name)
8090 vim_free(p);
8091}
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093/*********************************************
8094 * Implementation of the built-in functions
8095 */
8096
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097#ifdef FEAT_FLOAT
8098/*
8099 * "abs(expr)" function
8100 */
8101 static void
8102f_abs(argvars, rettv)
8103 typval_T *argvars;
8104 typval_T *rettv;
8105{
8106 if (argvars[0].v_type == VAR_FLOAT)
8107 {
8108 rettv->v_type = VAR_FLOAT;
8109 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8110 }
8111 else
8112 {
8113 varnumber_T n;
8114 int error = FALSE;
8115
8116 n = get_tv_number_chk(&argvars[0], &error);
8117 if (error)
8118 rettv->vval.v_number = -1;
8119 else if (n > 0)
8120 rettv->vval.v_number = n;
8121 else
8122 rettv->vval.v_number = -n;
8123 }
8124}
8125#endif
8126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008128 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 */
8130 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008131f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 typval_T *argvars;
8133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008140 if ((l = argvars[0].vval.v_list) != NULL
8141 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8142 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008143 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144 }
8145 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008146 EMSG(_(e_listreq));
8147}
8148
8149/*
8150 * "append(lnum, string/list)" function
8151 */
8152 static void
8153f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 typval_T *argvars;
8155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008156{
8157 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008158 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 list_T *l = NULL;
8160 listitem_T *li = NULL;
8161 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008162 long added = 0;
8163
Bram Moolenaar0d660222005-01-07 21:51:51 +00008164 lnum = get_tv_lnum(argvars);
8165 if (lnum >= 0
8166 && lnum <= curbuf->b_ml.ml_line_count
8167 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008168 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008171 l = argvars[1].vval.v_list;
8172 if (l == NULL)
8173 return;
8174 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008177 for (;;)
8178 {
8179 if (l == NULL)
8180 tv = &argvars[1]; /* append a string */
8181 else if (li == NULL)
8182 break; /* end of list */
8183 else
8184 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008185 line = get_tv_string_chk(tv);
8186 if (line == NULL) /* type error */
8187 {
8188 rettv->vval.v_number = 1; /* Failed */
8189 break;
8190 }
8191 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008192 ++added;
8193 if (l == NULL)
8194 break;
8195 li = li->li_next;
8196 }
8197
8198 appended_lines_mark(lnum, added);
8199 if (curwin->w_cursor.lnum > lnum)
8200 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008202 else
8203 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205
8206/*
8207 * "argc()" function
8208 */
8209/* ARGSUSED */
8210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 typval_T *argvars;
8213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216}
8217
8218/*
8219 * "argidx()" function
8220 */
8221/* ARGSUSED */
8222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008223f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 typval_T *argvars;
8225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
8231 * "argv(nr)" function
8232 */
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238 int idx;
8239
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008240 if (argvars[0].v_type != VAR_UNKNOWN)
8241 {
8242 idx = get_tv_number_chk(&argvars[0], NULL);
8243 if (idx >= 0 && idx < ARGCOUNT)
8244 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8245 else
8246 rettv->vval.v_string = NULL;
8247 rettv->v_type = VAR_STRING;
8248 }
8249 else if (rettv_list_alloc(rettv) == OK)
8250 for (idx = 0; idx < ARGCOUNT; ++idx)
8251 list_append_string(rettv->vval.v_list,
8252 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255#ifdef FEAT_FLOAT
8256static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8257
8258/*
8259 * Get the float value of "argvars[0]" into "f".
8260 * Returns FAIL when the argument is not a Number or Float.
8261 */
8262 static int
8263get_float_arg(argvars, f)
8264 typval_T *argvars;
8265 float_T *f;
8266{
8267 if (argvars[0].v_type == VAR_FLOAT)
8268 {
8269 *f = argvars[0].vval.v_float;
8270 return OK;
8271 }
8272 if (argvars[0].v_type == VAR_NUMBER)
8273 {
8274 *f = (float_T)argvars[0].vval.v_number;
8275 return OK;
8276 }
8277 EMSG(_("E808: Number or Float required"));
8278 return FAIL;
8279}
8280
8281/*
8282 * "atan()" function
8283 */
8284 static void
8285f_atan(argvars, rettv)
8286 typval_T *argvars;
8287 typval_T *rettv;
8288{
8289 float_T f;
8290
8291 rettv->v_type = VAR_FLOAT;
8292 if (get_float_arg(argvars, &f) == OK)
8293 rettv->vval.v_float = atan(f);
8294 else
8295 rettv->vval.v_float = 0.0;
8296}
8297#endif
8298
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299/*
8300 * "browse(save, title, initdir, default)" function
8301 */
8302/* ARGSUSED */
8303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 typval_T *argvars;
8306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307{
8308#ifdef FEAT_BROWSE
8309 int save;
8310 char_u *title;
8311 char_u *initdir;
8312 char_u *defname;
8313 char_u buf[NUMBUFLEN];
8314 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008315 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317 save = get_tv_number_chk(&argvars[0], &error);
8318 title = get_tv_string_chk(&argvars[1]);
8319 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8320 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 if (error || title == NULL || initdir == NULL || defname == NULL)
8323 rettv->vval.v_string = NULL;
8324 else
8325 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008326 do_browse(save ? BROWSE_SAVE : 0,
8327 title, defname, NULL, initdir, NULL, curbuf);
8328#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008332}
8333
8334/*
8335 * "browsedir(title, initdir)" function
8336 */
8337/* ARGSUSED */
8338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 typval_T *argvars;
8341 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008342{
8343#ifdef FEAT_BROWSE
8344 char_u *title;
8345 char_u *initdir;
8346 char_u buf[NUMBUFLEN];
8347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008348 title = get_tv_string_chk(&argvars[0]);
8349 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008351 if (title == NULL || initdir == NULL)
8352 rettv->vval.v_string = NULL;
8353 else
8354 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008355 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360}
8361
Bram Moolenaar33570922005-01-25 22:26:29 +00008362static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008363
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364/*
8365 * Find a buffer by number or exact name.
8366 */
8367 static buf_T *
8368find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008373 if (avar->v_type == VAR_NUMBER)
8374 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008375 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008378 if (buf == NULL)
8379 {
8380 /* No full path name match, try a match with a URL or a "nofile"
8381 * buffer, these don't use the full path. */
8382 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8383 if (buf->b_fname != NULL
8384 && (path_with_url(buf->b_fname)
8385#ifdef FEAT_QUICKFIX
8386 || bt_nofile(buf)
8387#endif
8388 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008389 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008390 break;
8391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 }
8393 return buf;
8394}
8395
8396/*
8397 * "bufexists(expr)" function
8398 */
8399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 typval_T *argvars;
8402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405}
8406
8407/*
8408 * "buflisted(expr)" function
8409 */
8410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
8415 buf_T *buf;
8416
8417 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421/*
8422 * "bufloaded(expr)" function
8423 */
8424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 typval_T *argvars;
8427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 buf_T *buf;
8430
8431 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008432 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433}
8434
Bram Moolenaar33570922005-01-25 22:26:29 +00008435static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437/*
8438 * Get buffer by number or pattern.
8439 */
8440 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 int save_magic;
8446 char_u *save_cpo;
8447 buf_T *buf;
8448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449 if (tv->v_type == VAR_NUMBER)
8450 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008451 if (tv->v_type != VAR_STRING)
8452 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (name == NULL || *name == NUL)
8454 return curbuf;
8455 if (name[0] == '$' && name[1] == NUL)
8456 return lastbuf;
8457
8458 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8459 save_magic = p_magic;
8460 p_magic = TRUE;
8461 save_cpo = p_cpo;
8462 p_cpo = (char_u *)"";
8463
8464 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8465 TRUE, FALSE));
8466
8467 p_magic = save_magic;
8468 p_cpo = save_cpo;
8469
8470 /* If not found, try expanding the name, like done for bufexists(). */
8471 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008472 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
8474 return buf;
8475}
8476
8477/*
8478 * "bufname(expr)" function
8479 */
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 buf_T *buf;
8486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489 buf = get_buf_tv(&argvars[0]);
8490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 --emsg_off;
8496}
8497
8498/*
8499 * "bufnr(expr)" function
8500 */
8501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *argvars;
8504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008507 int error = FALSE;
8508 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 --emsg_off;
8514
8515 /* If the buffer isn't found and the second argument is not zero create a
8516 * new buffer. */
8517 if (buf == NULL
8518 && argvars[1].v_type != VAR_UNKNOWN
8519 && get_tv_number_chk(&argvars[1], &error) != 0
8520 && !error
8521 && (name = get_tv_string_chk(&argvars[0])) != NULL
8522 && !error)
8523 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531/*
8532 * "bufwinnr(nr)" function
8533 */
8534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *argvars;
8537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539#ifdef FEAT_WINDOWS
8540 win_T *wp;
8541 int winnr = 0;
8542#endif
8543 buf_T *buf;
8544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008545 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548#ifdef FEAT_WINDOWS
8549 for (wp = firstwin; wp; wp = wp->w_next)
8550 {
8551 ++winnr;
8552 if (wp->w_buffer == buf)
8553 break;
8554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558#endif
8559 --emsg_off;
8560}
8561
8562/*
8563 * "byte2line(byte)" function
8564 */
8565/*ARGSUSED*/
8566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 typval_T *argvars;
8569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#else
8574 long boff = 0;
8575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 (linenr_T)0, &boff);
8582#endif
8583}
8584
8585/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008586 * "byteidx()" function
8587 */
8588/*ARGSUSED*/
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008593{
8594#ifdef FEAT_MBYTE
8595 char_u *t;
8596#endif
8597 char_u *str;
8598 long idx;
8599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008600 str = get_tv_string_chk(&argvars[0]);
8601 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008604 return;
8605
8606#ifdef FEAT_MBYTE
8607 t = str;
8608 for ( ; idx > 0; idx--)
8609 {
8610 if (*t == NUL) /* EOL reached */
8611 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008612 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008613 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008614 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008615#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008616 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008618#endif
8619}
8620
8621/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008622 * "call(func, arglist)" function
8623 */
8624 static void
8625f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 typval_T *argvars;
8627 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008628{
8629 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008630 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008631 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008633 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008635
8636 rettv->vval.v_number = 0;
8637 if (argvars[1].v_type != VAR_LIST)
8638 {
8639 EMSG(_(e_listreq));
8640 return;
8641 }
8642 if (argvars[1].vval.v_list == NULL)
8643 return;
8644
8645 if (argvars[0].v_type == VAR_FUNC)
8646 func = argvars[0].vval.v_string;
8647 else
8648 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 if (*func == NUL)
8650 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008651
Bram Moolenaare9a41262005-01-15 22:18:47 +00008652 if (argvars[2].v_type != VAR_UNKNOWN)
8653 {
8654 if (argvars[2].v_type != VAR_DICT)
8655 {
8656 EMSG(_(e_dictreq));
8657 return;
8658 }
8659 selfdict = argvars[2].vval.v_dict;
8660 }
8661
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008662 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8663 item = item->li_next)
8664 {
8665 if (argc == MAX_FUNC_ARGS)
8666 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008667 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008668 break;
8669 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008670 /* Make a copy of each argument. This is needed to be able to set
8671 * v_lock to VAR_FIXED in the copy without changing the original list.
8672 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008673 copy_tv(&item->li_tv, &argv[argc++]);
8674 }
8675
8676 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008677 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8679 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008680
8681 /* Free the arguments. */
8682 while (argc > 0)
8683 clear_tv(&argv[--argc]);
8684}
8685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686#ifdef FEAT_FLOAT
8687/*
8688 * "ceil({float})" function
8689 */
8690 static void
8691f_ceil(argvars, rettv)
8692 typval_T *argvars;
8693 typval_T *rettv;
8694{
8695 float_T f;
8696
8697 rettv->v_type = VAR_FLOAT;
8698 if (get_float_arg(argvars, &f) == OK)
8699 rettv->vval.v_float = ceil(f);
8700 else
8701 rettv->vval.v_float = 0.0;
8702}
8703#endif
8704
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008705/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008706 * "changenr()" function
8707 */
8708/*ARGSUSED*/
8709 static void
8710f_changenr(argvars, rettv)
8711 typval_T *argvars;
8712 typval_T *rettv;
8713{
8714 rettv->vval.v_number = curbuf->b_u_seq_cur;
8715}
8716
8717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 * "char2nr(string)" function
8719 */
8720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008722 typval_T *argvars;
8723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725#ifdef FEAT_MBYTE
8726 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008727 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else
8729#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731}
8732
8733/*
8734 * "cindent(lnum)" function
8735 */
8736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 typval_T *argvars;
8739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741#ifdef FEAT_CINDENT
8742 pos_T pos;
8743 linenr_T lnum;
8744
8745 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8748 {
8749 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 curwin->w_cursor = pos;
8752 }
8753 else
8754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
8757
8758/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008759 * "clearmatches()" function
8760 */
8761/*ARGSUSED*/
8762 static void
8763f_clearmatches(argvars, rettv)
8764 typval_T *argvars;
8765 typval_T *rettv;
8766{
8767#ifdef FEAT_SEARCH_EXTRA
8768 clear_matches(curwin);
8769#endif
8770}
8771
8772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 * "col(string)" function
8774 */
8775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 typval_T *argvars;
8778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 colnr_T col = 0;
8781 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008782 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008784 fp = var2fpos(&argvars[0], FALSE, &fnum);
8785 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {
8787 if (fp->col == MAXCOL)
8788 {
8789 /* '> can be MAXCOL, get the length of the line then */
8790 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008791 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 else
8793 col = MAXCOL;
8794 }
8795 else
8796 {
8797 col = fp->col + 1;
8798#ifdef FEAT_VIRTUALEDIT
8799 /* col(".") when the cursor is on the NUL at the end of the line
8800 * because of "coladd" can be seen as an extra column. */
8801 if (virtual_active() && fp == &curwin->w_cursor)
8802 {
8803 char_u *p = ml_get_cursor();
8804
8805 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8806 curwin->w_virtcol - curwin->w_cursor.coladd))
8807 {
8808# ifdef FEAT_MBYTE
8809 int l;
8810
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008811 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 col += l;
8813# else
8814 if (*p != NUL && p[1] == NUL)
8815 ++col;
8816# endif
8817 }
8818 }
8819#endif
8820 }
8821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
Bram Moolenaar572cb562005-08-05 21:35:02 +00008825#if defined(FEAT_INS_EXPAND)
8826/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008827 * "complete()" function
8828 */
8829/*ARGSUSED*/
8830 static void
8831f_complete(argvars, rettv)
8832 typval_T *argvars;
8833 typval_T *rettv;
8834{
8835 int startcol;
8836
8837 if ((State & INSERT) == 0)
8838 {
8839 EMSG(_("E785: complete() can only be used in Insert mode"));
8840 return;
8841 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008842
8843 /* Check for undo allowed here, because if something was already inserted
8844 * the line was already saved for undo and this check isn't done. */
8845 if (!undo_allowed())
8846 return;
8847
Bram Moolenaarade00832006-03-10 21:46:58 +00008848 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8849 {
8850 EMSG(_(e_invarg));
8851 return;
8852 }
8853
8854 startcol = get_tv_number_chk(&argvars[0], NULL);
8855 if (startcol <= 0)
8856 return;
8857
8858 set_completion(startcol - 1, argvars[1].vval.v_list);
8859}
8860
8861/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008862 * "complete_add()" function
8863 */
8864/*ARGSUSED*/
8865 static void
8866f_complete_add(argvars, rettv)
8867 typval_T *argvars;
8868 typval_T *rettv;
8869{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008870 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008871}
8872
8873/*
8874 * "complete_check()" function
8875 */
8876/*ARGSUSED*/
8877 static void
8878f_complete_check(argvars, rettv)
8879 typval_T *argvars;
8880 typval_T *rettv;
8881{
8882 int saved = RedrawingDisabled;
8883
8884 RedrawingDisabled = 0;
8885 ins_compl_check_keys(0);
8886 rettv->vval.v_number = compl_interrupted;
8887 RedrawingDisabled = saved;
8888}
8889#endif
8890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891/*
8892 * "confirm(message, buttons[, default [, type]])" function
8893 */
8894/*ARGSUSED*/
8895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008897 typval_T *argvars;
8898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8901 char_u *message;
8902 char_u *buttons = NULL;
8903 char_u buf[NUMBUFLEN];
8904 char_u buf2[NUMBUFLEN];
8905 int def = 1;
8906 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 char_u *typestr;
8908 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 message = get_tv_string_chk(&argvars[0]);
8911 if (message == NULL)
8912 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008913 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008915 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8916 if (buttons == NULL)
8917 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008918 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008921 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8924 if (typestr == NULL)
8925 error = TRUE;
8926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 switch (TOUPPER_ASC(*typestr))
8929 {
8930 case 'E': type = VIM_ERROR; break;
8931 case 'Q': type = VIM_QUESTION; break;
8932 case 'I': type = VIM_INFO; break;
8933 case 'W': type = VIM_WARNING; break;
8934 case 'G': type = VIM_GENERIC; break;
8935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937 }
8938 }
8939 }
8940
8941 if (buttons == NULL || *buttons == NUL)
8942 buttons = (char_u *)_("&Ok");
8943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944 if (error)
8945 rettv->vval.v_number = 0;
8946 else
8947 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 def, NULL);
8949#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#endif
8952}
8953
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954/*
8955 * "copy()" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008961{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008962 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008965#ifdef FEAT_FLOAT
8966/*
8967 * "cos()" function
8968 */
8969 static void
8970f_cos(argvars, rettv)
8971 typval_T *argvars;
8972 typval_T *rettv;
8973{
8974 float_T f;
8975
8976 rettv->v_type = VAR_FLOAT;
8977 if (get_float_arg(argvars, &f) == OK)
8978 rettv->vval.v_float = cos(f);
8979 else
8980 rettv->vval.v_float = 0.0;
8981}
8982#endif
8983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008985 * "count()" function
8986 */
8987 static void
8988f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008991{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008992 long n = 0;
8993 int ic = FALSE;
8994
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008996 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 listitem_T *li;
8998 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009000
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 if ((l = argvars[0].vval.v_list) != NULL)
9002 {
9003 li = l->lv_first;
9004 if (argvars[2].v_type != VAR_UNKNOWN)
9005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 int error = FALSE;
9007
9008 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 if (argvars[3].v_type != VAR_UNKNOWN)
9010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 idx = get_tv_number_chk(&argvars[3], &error);
9012 if (!error)
9013 {
9014 li = list_find(l, idx);
9015 if (li == NULL)
9016 EMSGN(_(e_listidx), idx);
9017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 if (error)
9020 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 }
9022
9023 for ( ; li != NULL; li = li->li_next)
9024 if (tv_equal(&li->li_tv, &argvars[1], ic))
9025 ++n;
9026 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009027 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009028 else if (argvars[0].v_type == VAR_DICT)
9029 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 int todo;
9031 dict_T *d;
9032 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009033
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009034 if ((d = argvars[0].vval.v_dict) != NULL)
9035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 int error = FALSE;
9037
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 if (argvars[2].v_type != VAR_UNKNOWN)
9039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009040 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 if (argvars[3].v_type != VAR_UNKNOWN)
9042 EMSG(_(e_invarg));
9043 }
9044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009045 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009047 {
9048 if (!HASHITEM_EMPTY(hi))
9049 {
9050 --todo;
9051 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9052 ++n;
9053 }
9054 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009055 }
9056 }
9057 else
9058 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009059 rettv->vval.v_number = n;
9060}
9061
9062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9064 *
9065 * Checks the existence of a cscope connection.
9066 */
9067/*ARGSUSED*/
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 typval_T *argvars;
9071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073#ifdef FEAT_CSCOPE
9074 int num = 0;
9075 char_u *dbpath = NULL;
9076 char_u *prepend = NULL;
9077 char_u buf[NUMBUFLEN];
9078
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009079 if (argvars[0].v_type != VAR_UNKNOWN
9080 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 num = (int)get_tv_number(&argvars[0]);
9083 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009084 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
9087
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091#endif
9092}
9093
9094/*
9095 * "cursor(lnum, col)" function
9096 *
9097 * Moves the cursor to the specified line and column
9098 */
9099/*ARGSUSED*/
9100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *argvars;
9103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009106#ifdef FEAT_VIRTUALEDIT
9107 long coladd = 0;
9108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
Bram Moolenaara5525202006-03-02 22:52:09 +00009110 if (argvars[1].v_type == VAR_UNKNOWN)
9111 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009112 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009113
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009114 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009115 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009116 line = pos.lnum;
9117 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009118#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009119 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009120#endif
9121 }
9122 else
9123 {
9124 line = get_tv_lnum(argvars);
9125 col = get_tv_number_chk(&argvars[1], NULL);
9126#ifdef FEAT_VIRTUALEDIT
9127 if (argvars[2].v_type != VAR_UNKNOWN)
9128 coladd = get_tv_number_chk(&argvars[2], NULL);
9129#endif
9130 }
9131 if (line < 0 || col < 0
9132#ifdef FEAT_VIRTUALEDIT
9133 || coladd < 0
9134#endif
9135 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 if (line > 0)
9138 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (col > 0)
9140 curwin->w_cursor.col = col - 1;
9141#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009142 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#endif
9144
9145 /* Make sure the cursor is in a valid position. */
9146 check_cursor();
9147#ifdef FEAT_MBYTE
9148 /* Correct cursor for multi-byte character. */
9149 if (has_mbyte)
9150 mb_adjust_cursor();
9151#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009152
9153 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
9156/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009157 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 */
9159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009161 typval_T *argvars;
9162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009164 int noref = 0;
9165
9166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009168 if (noref < 0 || noref > 1)
9169 EMSG(_(e_invarg));
9170 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009171 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
9175 * "delete()" function
9176 */
9177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009179 typval_T *argvars;
9180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186}
9187
9188/*
9189 * "did_filetype()" function
9190 */
9191/*ARGSUSED*/
9192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009194 typval_T *argvars;
9195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
9197#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201#endif
9202}
9203
9204/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009205 * "diff_filler()" function
9206 */
9207/*ARGSUSED*/
9208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009212{
9213#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009215#endif
9216}
9217
9218/*
9219 * "diff_hlID()" function
9220 */
9221/*ARGSUSED*/
9222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009224 typval_T *argvars;
9225 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009226{
9227#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009228 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009229 static linenr_T prev_lnum = 0;
9230 static int changedtick = 0;
9231 static int fnum = 0;
9232 static int change_start = 0;
9233 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009234 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009235 int filler_lines;
9236 int col;
9237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 if (lnum < 0) /* ignore type error in {lnum} arg */
9239 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009240 if (lnum != prev_lnum
9241 || changedtick != curbuf->b_changedtick
9242 || fnum != curbuf->b_fnum)
9243 {
9244 /* New line, buffer, change: need to get the values. */
9245 filler_lines = diff_check(curwin, lnum);
9246 if (filler_lines < 0)
9247 {
9248 if (filler_lines == -1)
9249 {
9250 change_start = MAXCOL;
9251 change_end = -1;
9252 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9253 hlID = HLF_ADD; /* added line */
9254 else
9255 hlID = HLF_CHD; /* changed line */
9256 }
9257 else
9258 hlID = HLF_ADD; /* added line */
9259 }
9260 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009261 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009262 prev_lnum = lnum;
9263 changedtick = curbuf->b_changedtick;
9264 fnum = curbuf->b_fnum;
9265 }
9266
9267 if (hlID == HLF_CHD || hlID == HLF_TXD)
9268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009270 if (col >= change_start && col <= change_end)
9271 hlID = HLF_TXD; /* changed text */
9272 else
9273 hlID = HLF_CHD; /* changed line */
9274 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009275 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009276#endif
9277}
9278
9279/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009280 * "empty({expr})" function
9281 */
9282 static void
9283f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T *argvars;
9285 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009286{
9287 int n;
9288
9289 switch (argvars[0].v_type)
9290 {
9291 case VAR_STRING:
9292 case VAR_FUNC:
9293 n = argvars[0].vval.v_string == NULL
9294 || *argvars[0].vval.v_string == NUL;
9295 break;
9296 case VAR_NUMBER:
9297 n = argvars[0].vval.v_number == 0;
9298 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009299#ifdef FEAT_FLOAT
9300 case VAR_FLOAT:
9301 n = argvars[0].vval.v_float == 0.0;
9302 break;
9303#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009304 case VAR_LIST:
9305 n = argvars[0].vval.v_list == NULL
9306 || argvars[0].vval.v_list->lv_first == NULL;
9307 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 case VAR_DICT:
9309 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009312 default:
9313 EMSG2(_(e_intern2), "f_empty()");
9314 n = 0;
9315 }
9316
9317 rettv->vval.v_number = n;
9318}
9319
9320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 * "escape({string}, {chars})" function
9322 */
9323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 typval_T *argvars;
9326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 char_u buf[NUMBUFLEN];
9329
Bram Moolenaar758711c2005-02-02 23:11:38 +00009330 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9331 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009336 * "eval()" function
9337 */
9338/*ARGSUSED*/
9339 static void
9340f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009341 typval_T *argvars;
9342 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009343{
9344 char_u *s;
9345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 s = get_tv_string_chk(&argvars[0]);
9347 if (s != NULL)
9348 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009350 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9351 {
9352 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009353 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009355 else if (*s != NUL)
9356 EMSG(_(e_trailing));
9357}
9358
9359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 * "eventhandler()" function
9361 */
9362/*ARGSUSED*/
9363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 typval_T *argvars;
9366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369}
9370
9371/*
9372 * "executable()" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
9381
9382/*
9383 * "exists()" function
9384 */
9385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 typval_T *argvars;
9388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 char_u *p;
9391 char_u *name;
9392 int n = FALSE;
9393 int len = 0;
9394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 if (*p == '$') /* environment variable */
9397 {
9398 /* first try "normal" environment variables (fast) */
9399 if (mch_getenv(p + 1) != NULL)
9400 n = TRUE;
9401 else
9402 {
9403 /* try expanding things like $VIM and ${HOME} */
9404 p = expand_env_save(p);
9405 if (p != NULL && *p != '$')
9406 n = TRUE;
9407 vim_free(p);
9408 }
9409 }
9410 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009413 if (*skipwhite(p) != NUL)
9414 n = FALSE; /* trailing garbage */
9415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 else if (*p == '*') /* internal or user defined function */
9417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 }
9420 else if (*p == ':')
9421 {
9422 n = cmd_exists(p + 1);
9423 }
9424 else if (*p == '#')
9425 {
9426#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009427 if (p[1] == '#')
9428 n = autocmd_supported(p + 2);
9429 else
9430 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#endif
9432 }
9433 else /* internal variable */
9434 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009435 char_u *tofree;
9436 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009438 /* get_name_len() takes care of expanding curly braces */
9439 name = p;
9440 len = get_name_len(&p, &tofree, TRUE, FALSE);
9441 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009443 if (tofree != NULL)
9444 name = tofree;
9445 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9446 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009448 /* handle d.key, l[idx], f(expr) */
9449 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9450 if (n)
9451 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 }
9453 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009454 if (*p != NUL)
9455 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009457 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 }
9459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463/*
9464 * "expand()" function
9465 */
9466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *argvars;
9469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 char_u *s;
9472 int len;
9473 char_u *errormsg;
9474 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9475 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->v_type = VAR_STRING;
9479 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (*s == '%' || *s == '#' || *s == '<')
9481 {
9482 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009483 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 --emsg_off;
9485 }
9486 else
9487 {
9488 /* When the optional second argument is non-zero, don't remove matches
9489 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009490 if (argvars[1].v_type != VAR_UNKNOWN
9491 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 if (!error)
9494 {
9495 ExpandInit(&xpc);
9496 xpc.xp_context = EXPAND_FILES;
9497 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 }
9499 else
9500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 }
9502}
9503
9504/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009505 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009506 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009507 */
9508 static void
9509f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009510 typval_T *argvars;
9511 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009512{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009513 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009514 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009515 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 list_T *l1, *l2;
9517 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009518 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009520
Bram Moolenaare9a41262005-01-15 22:18:47 +00009521 l1 = argvars[0].vval.v_list;
9522 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009523 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9524 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009525 {
9526 if (argvars[2].v_type != VAR_UNKNOWN)
9527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009528 before = get_tv_number_chk(&argvars[2], &error);
9529 if (error)
9530 return; /* type error; errmsg already given */
9531
Bram Moolenaar758711c2005-02-02 23:11:38 +00009532 if (before == l1->lv_len)
9533 item = NULL;
9534 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009536 item = list_find(l1, before);
9537 if (item == NULL)
9538 {
9539 EMSGN(_(e_listidx), before);
9540 return;
9541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009542 }
9543 }
9544 else
9545 item = NULL;
9546 list_extend(l1, l2, item);
9547
Bram Moolenaare9a41262005-01-15 22:18:47 +00009548 copy_tv(&argvars[0], rettv);
9549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009550 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009551 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9552 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 dict_T *d1, *d2;
9554 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 char_u *action;
9556 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009558 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009559
9560 d1 = argvars[0].vval.v_dict;
9561 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009562 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9563 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009564 {
9565 /* Check the third argument. */
9566 if (argvars[2].v_type != VAR_UNKNOWN)
9567 {
9568 static char *(av[]) = {"keep", "force", "error"};
9569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 action = get_tv_string_chk(&argvars[2]);
9571 if (action == NULL)
9572 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009573 for (i = 0; i < 3; ++i)
9574 if (STRCMP(action, av[i]) == 0)
9575 break;
9576 if (i == 3)
9577 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009578 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009579 return;
9580 }
9581 }
9582 else
9583 action = (char_u *)"force";
9584
9585 /* Go over all entries in the second dict and add them to the
9586 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009587 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009588 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009590 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009592 --todo;
9593 di1 = dict_find(d1, hi2->hi_key, -1);
9594 if (di1 == NULL)
9595 {
9596 di1 = dictitem_copy(HI2DI(hi2));
9597 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9598 dictitem_free(di1);
9599 }
9600 else if (*action == 'e')
9601 {
9602 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9603 break;
9604 }
9605 else if (*action == 'f')
9606 {
9607 clear_tv(&di1->di_tv);
9608 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009610 }
9611 }
9612
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 copy_tv(&argvars[0], rettv);
9614 }
9615 }
9616 else
9617 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009618}
9619
9620/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009621 * "feedkeys()" function
9622 */
9623/*ARGSUSED*/
9624 static void
9625f_feedkeys(argvars, rettv)
9626 typval_T *argvars;
9627 typval_T *rettv;
9628{
9629 int remap = TRUE;
9630 char_u *keys, *flags;
9631 char_u nbuf[NUMBUFLEN];
9632 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009633 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009634
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009635 /* This is not allowed in the sandbox. If the commands would still be
9636 * executed in the sandbox it would be OK, but it probably happens later,
9637 * when "sandbox" is no longer set. */
9638 if (check_secure())
9639 return;
9640
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009641 rettv->vval.v_number = 0;
9642 keys = get_tv_string(&argvars[0]);
9643 if (*keys != NUL)
9644 {
9645 if (argvars[1].v_type != VAR_UNKNOWN)
9646 {
9647 flags = get_tv_string_buf(&argvars[1], nbuf);
9648 for ( ; *flags != NUL; ++flags)
9649 {
9650 switch (*flags)
9651 {
9652 case 'n': remap = FALSE; break;
9653 case 'm': remap = TRUE; break;
9654 case 't': typed = TRUE; break;
9655 }
9656 }
9657 }
9658
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009659 /* Need to escape K_SPECIAL and CSI before putting the string in the
9660 * typeahead buffer. */
9661 keys_esc = vim_strsave_escape_csi(keys);
9662 if (keys_esc != NULL)
9663 {
9664 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009665 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009666 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009667 if (vgetc_busy)
9668 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009669 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009670 }
9671}
9672
9673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 * "filereadable()" function
9675 */
9676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009678 typval_T *argvars;
9679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 FILE *fd;
9682 char_u *p;
9683 int n;
9684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9687 {
9688 n = TRUE;
9689 fclose(fd);
9690 }
9691 else
9692 n = FALSE;
9693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
9697/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009698 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 * rights to write into.
9700 */
9701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009703 typval_T *argvars;
9704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009706 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707}
9708
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009709static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009710
9711 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009712findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *argvars;
9714 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009715 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009716{
9717#ifdef FEAT_SEARCHPATH
9718 char_u *fname;
9719 char_u *fresult = NULL;
9720 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9721 char_u *p;
9722 char_u pathbuf[NUMBUFLEN];
9723 int count = 1;
9724 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009725 int error = FALSE;
9726#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009727
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009728 rettv->vval.v_string = NULL;
9729 rettv->v_type = VAR_STRING;
9730
9731#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009733
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009734 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9737 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009738 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 else
9740 {
9741 if (*p != NUL)
9742 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009745 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009747 }
9748
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009749 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9750 error = TRUE;
9751
9752 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 do
9755 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009756 if (rettv->v_type == VAR_STRING)
9757 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 fresult = find_file_in_path_option(first ? fname : NULL,
9759 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009760 0, first, path,
9761 find_what,
9762 curbuf->b_ffname,
9763 find_what == FINDFILE_DIR
9764 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009765 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009766
9767 if (fresult != NULL && rettv->v_type == VAR_LIST)
9768 list_append_string(rettv->vval.v_list, fresult, -1);
9769
9770 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009772
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009773 if (rettv->v_type == VAR_STRING)
9774 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009775#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009776}
9777
Bram Moolenaar33570922005-01-25 22:26:29 +00009778static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9779static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009780
9781/*
9782 * Implementation of map() and filter().
9783 */
9784 static void
9785filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 typval_T *argvars;
9787 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009788 int map;
9789{
9790 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009791 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009792 listitem_T *li, *nli;
9793 list_T *l = NULL;
9794 dictitem_T *di;
9795 hashtab_T *ht;
9796 hashitem_T *hi;
9797 dict_T *d = NULL;
9798 typval_T save_val;
9799 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009800 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009801 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009802 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009803 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009804
9805 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009806 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009807 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009808 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009809 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009810 return;
9811 }
9812 else if (argvars[0].v_type == VAR_DICT)
9813 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009814 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009815 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 return;
9817 }
9818 else
9819 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009820 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009821 return;
9822 }
9823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 expr = get_tv_string_buf_chk(&argvars[1], buf);
9825 /* On type errors, the preceding call has already displayed an error
9826 * message. Avoid a misleading error message for an empty string that
9827 * was not passed as argument. */
9828 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 prepare_vimvar(VV_VAL, &save_val);
9831 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009832
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009833 /* We reset "did_emsg" to be able to detect whether an error
9834 * occurred during evaluation of the expression. */
9835 save_did_emsg = did_emsg;
9836 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009838 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 prepare_vimvar(VV_KEY, &save_key);
9841 vimvars[VV_KEY].vv_type = VAR_STRING;
9842
9843 ht = &d->dv_hashtab;
9844 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009845 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009846 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 if (!HASHITEM_EMPTY(hi))
9849 {
9850 --todo;
9851 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009852 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 break;
9854 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009855 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009856 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 break;
9858 if (!map && rem)
9859 dictitem_remove(d, di);
9860 clear_tv(&vimvars[VV_KEY].vv_tv);
9861 }
9862 }
9863 hash_unlock(ht);
9864
9865 restore_vimvar(VV_KEY, &save_key);
9866 }
9867 else
9868 {
9869 for (li = l->lv_first; li != NULL; li = nli)
9870 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009871 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009872 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009873 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009874 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009875 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009876 break;
9877 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009879 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009880 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009883
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009884 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009885 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886
9887 copy_tv(&argvars[0], rettv);
9888}
9889
9890 static int
9891filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009893 char_u *expr;
9894 int map;
9895 int *remp;
9896{
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009898 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009899 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009900
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 s = expr;
9903 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009904 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 if (*s != NUL) /* check for trailing chars after expr */
9906 {
9907 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009908 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 }
9910 if (map)
9911 {
9912 /* map(): replace the list item value */
9913 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009914 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 *tv = rettv;
9916 }
9917 else
9918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009919 int error = FALSE;
9920
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 /* On type error, nothing has been removed; return FAIL to stop the
9925 * loop. The error message was given by get_tv_number_chk(). */
9926 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009927 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009929 retval = OK;
9930theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009932 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009933}
9934
9935/*
9936 * "filter()" function
9937 */
9938 static void
9939f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009942{
9943 filter_map(argvars, rettv, FALSE);
9944}
9945
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009947 * "finddir({fname}[, {path}[, {count}]])" function
9948 */
9949 static void
9950f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009951 typval_T *argvars;
9952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009953{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009954 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009955}
9956
9957/*
9958 * "findfile({fname}[, {path}[, {count}]])" function
9959 */
9960 static void
9961f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 typval_T *argvars;
9963 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009964{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009965 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009966}
9967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009968#ifdef FEAT_FLOAT
9969/*
9970 * "float2nr({float})" function
9971 */
9972 static void
9973f_float2nr(argvars, rettv)
9974 typval_T *argvars;
9975 typval_T *rettv;
9976{
9977 float_T f;
9978
9979 if (get_float_arg(argvars, &f) == OK)
9980 {
9981 if (f < -0x7fffffff)
9982 rettv->vval.v_number = -0x7fffffff;
9983 else if (f > 0x7fffffff)
9984 rettv->vval.v_number = 0x7fffffff;
9985 else
9986 rettv->vval.v_number = (varnumber_T)f;
9987 }
9988 else
9989 rettv->vval.v_number = 0;
9990}
9991
9992/*
9993 * "floor({float})" function
9994 */
9995 static void
9996f_floor(argvars, rettv)
9997 typval_T *argvars;
9998 typval_T *rettv;
9999{
10000 float_T f;
10001
10002 rettv->v_type = VAR_FLOAT;
10003 if (get_float_arg(argvars, &f) == OK)
10004 rettv->vval.v_float = floor(f);
10005 else
10006 rettv->vval.v_float = 0.0;
10007}
10008#endif
10009
Bram Moolenaar0d660222005-01-07 21:51:51 +000010010/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010011 * "fnameescape({string})" function
10012 */
10013 static void
10014f_fnameescape(argvars, rettv)
10015 typval_T *argvars;
10016 typval_T *rettv;
10017{
10018 rettv->vval.v_string = vim_strsave_fnameescape(
10019 get_tv_string(&argvars[0]), FALSE);
10020 rettv->v_type = VAR_STRING;
10021}
10022
10023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 * "fnamemodify({fname}, {mods})" function
10025 */
10026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 char_u *fname;
10032 char_u *mods;
10033 int usedlen = 0;
10034 int len;
10035 char_u *fbuf = NULL;
10036 char_u buf[NUMBUFLEN];
10037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 fname = get_tv_string_chk(&argvars[0]);
10039 mods = get_tv_string_buf_chk(&argvars[1], buf);
10040 if (fname == NULL || mods == NULL)
10041 fname = NULL;
10042 else
10043 {
10044 len = (int)STRLEN(fname);
10045 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 vim_free(fbuf);
10054}
10055
Bram Moolenaar33570922005-01-25 22:26:29 +000010056static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
10058/*
10059 * "foldclosed()" function
10060 */
10061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010063 typval_T *argvars;
10064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 int end;
10066{
10067#ifdef FEAT_FOLDING
10068 linenr_T lnum;
10069 linenr_T first, last;
10070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10073 {
10074 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10075 {
10076 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 return;
10081 }
10082 }
10083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085}
10086
10087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010088 * "foldclosed()" function
10089 */
10090 static void
10091f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010094{
10095 foldclosed_both(argvars, rettv, FALSE);
10096}
10097
10098/*
10099 * "foldclosedend()" function
10100 */
10101 static void
10102f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010103 typval_T *argvars;
10104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010105{
10106 foldclosed_both(argvars, rettv, TRUE);
10107}
10108
10109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 * "foldlevel()" function
10111 */
10112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010114 typval_T *argvars;
10115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117#ifdef FEAT_FOLDING
10118 linenr_T lnum;
10119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 else
10124#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126}
10127
10128/*
10129 * "foldtext()" function
10130 */
10131/*ARGSUSED*/
10132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010134 typval_T *argvars;
10135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136{
10137#ifdef FEAT_FOLDING
10138 linenr_T lnum;
10139 char_u *s;
10140 char_u *r;
10141 int len;
10142 char *txt;
10143#endif
10144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->v_type = VAR_STRING;
10146 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010148 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10149 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10150 <= curbuf->b_ml.ml_line_count
10151 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 {
10153 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010154 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10155 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 {
10157 if (!linewhite(lnum))
10158 break;
10159 ++lnum;
10160 }
10161
10162 /* Find interesting text in this line. */
10163 s = skipwhite(ml_get(lnum));
10164 /* skip C comment-start */
10165 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010168 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010170 {
10171 s = skipwhite(ml_get(lnum + 1));
10172 if (*s == '*')
10173 s = skipwhite(s + 1);
10174 }
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 txt = _("+-%s%3ld lines: ");
10177 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 + 20 /* for %3ld */
10180 + STRLEN(s))); /* concatenated */
10181 if (r != NULL)
10182 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10184 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10185 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 len = (int)STRLEN(r);
10187 STRCAT(r, s);
10188 /* remove 'foldmarker' and 'commentstring' */
10189 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 }
10192 }
10193#endif
10194}
10195
10196/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010197 * "foldtextresult(lnum)" function
10198 */
10199/*ARGSUSED*/
10200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010202 typval_T *argvars;
10203 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010204{
10205#ifdef FEAT_FOLDING
10206 linenr_T lnum;
10207 char_u *text;
10208 char_u buf[51];
10209 foldinfo_T foldinfo;
10210 int fold_count;
10211#endif
10212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->v_type = VAR_STRING;
10214 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010215#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 /* treat illegal types and illegal string values for {lnum} the same */
10218 if (lnum < 0)
10219 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010220 fold_count = foldedCount(curwin, lnum, &foldinfo);
10221 if (fold_count > 0)
10222 {
10223 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10224 &foldinfo, buf);
10225 if (text == buf)
10226 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010227 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010228 }
10229#endif
10230}
10231
10232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 * "foreground()" function
10234 */
10235/*ARGSUSED*/
10236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 typval_T *argvars;
10239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#ifdef FEAT_GUI
10243 if (gui.in_use)
10244 gui_mch_set_foreground();
10245#else
10246# ifdef WIN32
10247 win32_set_foreground();
10248# endif
10249#endif
10250}
10251
10252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010253 * "function()" function
10254 */
10255/*ARGSUSED*/
10256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 typval_T *argvars;
10259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010260{
10261 char_u *s;
10262
Bram Moolenaara7043832005-01-21 11:56:39 +000010263 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010266 EMSG2(_(e_invarg2), s);
10267 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010268 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010269 else
10270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010271 rettv->vval.v_string = vim_strsave(s);
10272 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010273 }
10274}
10275
10276/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010277 * "garbagecollect()" function
10278 */
10279/*ARGSUSED*/
10280 static void
10281f_garbagecollect(argvars, rettv)
10282 typval_T *argvars;
10283 typval_T *rettv;
10284{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010285 /* This is postponed until we are back at the toplevel, because we may be
10286 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10287 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010288
10289 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10290 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010291}
10292
10293/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010294 * "get()" function
10295 */
10296 static void
10297f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010298 typval_T *argvars;
10299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010300{
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 listitem_T *li;
10302 list_T *l;
10303 dictitem_T *di;
10304 dict_T *d;
10305 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010306
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010308 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 int error = FALSE;
10312
10313 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10314 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010316 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 else if (argvars[0].v_type == VAR_DICT)
10319 {
10320 if ((d = argvars[0].vval.v_dict) != NULL)
10321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010322 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 if (di != NULL)
10324 tv = &di->di_tv;
10325 }
10326 }
10327 else
10328 EMSG2(_(e_listdictarg), "get()");
10329
10330 if (tv == NULL)
10331 {
10332 if (argvars[2].v_type == VAR_UNKNOWN)
10333 rettv->vval.v_number = 0;
10334 else
10335 copy_tv(&argvars[2], rettv);
10336 }
10337 else
10338 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010339}
10340
Bram Moolenaar342337a2005-07-21 21:11:17 +000010341static 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 +000010342
10343/*
10344 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010345 * Return a range (from start to end) of lines in rettv from the specified
10346 * buffer.
10347 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010348 */
10349 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010350get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010351 buf_T *buf;
10352 linenr_T start;
10353 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010354 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010355 typval_T *rettv;
10356{
10357 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010358
Bram Moolenaar342337a2005-07-21 21:11:17 +000010359 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010360 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010361 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010362 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010363 }
10364 else
10365 rettv->vval.v_number = 0;
10366
10367 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10368 return;
10369
10370 if (!retlist)
10371 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010372 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10373 p = ml_get_buf(buf, start, FALSE);
10374 else
10375 p = (char_u *)"";
10376
10377 rettv->v_type = VAR_STRING;
10378 rettv->vval.v_string = vim_strsave(p);
10379 }
10380 else
10381 {
10382 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010383 return;
10384
10385 if (start < 1)
10386 start = 1;
10387 if (end > buf->b_ml.ml_line_count)
10388 end = buf->b_ml.ml_line_count;
10389 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010390 if (list_append_string(rettv->vval.v_list,
10391 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010392 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010393 }
10394}
10395
10396/*
10397 * "getbufline()" function
10398 */
10399 static void
10400f_getbufline(argvars, rettv)
10401 typval_T *argvars;
10402 typval_T *rettv;
10403{
10404 linenr_T lnum;
10405 linenr_T end;
10406 buf_T *buf;
10407
10408 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10409 ++emsg_off;
10410 buf = get_buf_tv(&argvars[0]);
10411 --emsg_off;
10412
Bram Moolenaar661b1822005-07-28 22:36:45 +000010413 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010414 if (argvars[2].v_type == VAR_UNKNOWN)
10415 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010416 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010417 end = get_tv_lnum_buf(&argvars[2], buf);
10418
Bram Moolenaar342337a2005-07-21 21:11:17 +000010419 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010420}
10421
Bram Moolenaar0d660222005-01-07 21:51:51 +000010422/*
10423 * "getbufvar()" function
10424 */
10425 static void
10426f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010427 typval_T *argvars;
10428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010429{
10430 buf_T *buf;
10431 buf_T *save_curbuf;
10432 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10436 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010437 ++emsg_off;
10438 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010439
10440 rettv->v_type = VAR_STRING;
10441 rettv->vval.v_string = NULL;
10442
10443 if (buf != NULL && varname != NULL)
10444 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010445 /* set curbuf to be our buf, temporarily */
10446 save_curbuf = curbuf;
10447 curbuf = buf;
10448
Bram Moolenaar0d660222005-01-07 21:51:51 +000010449 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010450 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010451 else
10452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 if (*varname == NUL)
10454 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10455 * scope prefix before the NUL byte is required by
10456 * find_var_in_ht(). */
10457 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010458 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010459 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010460 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010462 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010463
10464 /* restore previous notion of curbuf */
10465 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010466 }
10467
10468 --emsg_off;
10469}
10470
10471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 * "getchar()" function
10473 */
10474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 typval_T *argvars;
10477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010482 /* Position the cursor. Needed after a message that ends in a space. */
10483 windgoto(msg_row, msg_col);
10484
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 ++no_mapping;
10486 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010487 for (;;)
10488 {
10489 if (argvars[0].v_type == VAR_UNKNOWN)
10490 /* getchar(): blocking wait. */
10491 n = safe_vgetc();
10492 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10493 /* getchar(1): only check if char avail */
10494 n = vpeekc();
10495 else if (error || vpeekc() == NUL)
10496 /* illegal argument or getchar(0) and no char avail: return zero */
10497 n = 0;
10498 else
10499 /* getchar(0) and char avail: return char */
10500 n = safe_vgetc();
10501 if (n == K_IGNORE)
10502 continue;
10503 break;
10504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 --no_mapping;
10506 --allow_keys;
10507
Bram Moolenaar219b8702006-11-01 14:32:36 +000010508 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10509 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10510 vimvars[VV_MOUSE_COL].vv_nr = 0;
10511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 if (IS_SPECIAL(n) || mod_mask != 0)
10514 {
10515 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10516 int i = 0;
10517
10518 /* Turn a special key into three bytes, plus modifier. */
10519 if (mod_mask != 0)
10520 {
10521 temp[i++] = K_SPECIAL;
10522 temp[i++] = KS_MODIFIER;
10523 temp[i++] = mod_mask;
10524 }
10525 if (IS_SPECIAL(n))
10526 {
10527 temp[i++] = K_SPECIAL;
10528 temp[i++] = K_SECOND(n);
10529 temp[i++] = K_THIRD(n);
10530 }
10531#ifdef FEAT_MBYTE
10532 else if (has_mbyte)
10533 i += (*mb_char2bytes)(n, temp + i);
10534#endif
10535 else
10536 temp[i++] = n;
10537 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 rettv->v_type = VAR_STRING;
10539 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010540
10541#ifdef FEAT_MOUSE
10542 if (n == K_LEFTMOUSE
10543 || n == K_LEFTMOUSE_NM
10544 || n == K_LEFTDRAG
10545 || n == K_LEFTRELEASE
10546 || n == K_LEFTRELEASE_NM
10547 || n == K_MIDDLEMOUSE
10548 || n == K_MIDDLEDRAG
10549 || n == K_MIDDLERELEASE
10550 || n == K_RIGHTMOUSE
10551 || n == K_RIGHTDRAG
10552 || n == K_RIGHTRELEASE
10553 || n == K_X1MOUSE
10554 || n == K_X1DRAG
10555 || n == K_X1RELEASE
10556 || n == K_X2MOUSE
10557 || n == K_X2DRAG
10558 || n == K_X2RELEASE
10559 || n == K_MOUSEDOWN
10560 || n == K_MOUSEUP)
10561 {
10562 int row = mouse_row;
10563 int col = mouse_col;
10564 win_T *win;
10565 linenr_T lnum;
10566# ifdef FEAT_WINDOWS
10567 win_T *wp;
10568# endif
10569 int n = 1;
10570
10571 if (row >= 0 && col >= 0)
10572 {
10573 /* Find the window at the mouse coordinates and compute the
10574 * text position. */
10575 win = mouse_find_win(&row, &col);
10576 (void)mouse_comp_pos(win, &row, &col, &lnum);
10577# ifdef FEAT_WINDOWS
10578 for (wp = firstwin; wp != win; wp = wp->w_next)
10579 ++n;
10580# endif
10581 vimvars[VV_MOUSE_WIN].vv_nr = n;
10582 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10583 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10584 }
10585 }
10586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 }
10588}
10589
10590/*
10591 * "getcharmod()" function
10592 */
10593/*ARGSUSED*/
10594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010596 typval_T *argvars;
10597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600}
10601
10602/*
10603 * "getcmdline()" function
10604 */
10605/*ARGSUSED*/
10606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 typval_T *argvars;
10609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611 rettv->v_type = VAR_STRING;
10612 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613}
10614
10615/*
10616 * "getcmdpos()" function
10617 */
10618/*ARGSUSED*/
10619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010621 typval_T *argvars;
10622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
10627/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010628 * "getcmdtype()" function
10629 */
10630/*ARGSUSED*/
10631 static void
10632f_getcmdtype(argvars, rettv)
10633 typval_T *argvars;
10634 typval_T *rettv;
10635{
10636 rettv->v_type = VAR_STRING;
10637 rettv->vval.v_string = alloc(2);
10638 if (rettv->vval.v_string != NULL)
10639 {
10640 rettv->vval.v_string[0] = get_cmdline_type();
10641 rettv->vval.v_string[1] = NUL;
10642 }
10643}
10644
10645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 * "getcwd()" function
10647 */
10648/*ARGSUSED*/
10649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010651 typval_T *argvars;
10652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
10654 char_u cwd[MAXPATHL];
10655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 else
10660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010663 if (rettv->vval.v_string != NULL)
10664 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665#endif
10666 }
10667}
10668
10669/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010670 * "getfontname()" function
10671 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010672/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 typval_T *argvars;
10676 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->v_type = VAR_STRING;
10679 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010680#ifdef FEAT_GUI
10681 if (gui.in_use)
10682 {
10683 GuiFont font;
10684 char_u *name = NULL;
10685
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010686 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010687 {
10688 /* Get the "Normal" font. Either the name saved by
10689 * hl_set_font_name() or from the font ID. */
10690 font = gui.norm_font;
10691 name = hl_get_font_name();
10692 }
10693 else
10694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010696 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10697 return;
10698 font = gui_mch_get_font(name, FALSE);
10699 if (font == NOFONT)
10700 return; /* Invalid font name, return empty string. */
10701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010703 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010704 gui_mch_free_font(font);
10705 }
10706#endif
10707}
10708
10709/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010710 * "getfperm({fname})" function
10711 */
10712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010716{
10717 char_u *fname;
10718 struct stat st;
10719 char_u *perm = NULL;
10720 char_u flags[] = "rwx";
10721 int i;
10722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010726 if (mch_stat((char *)fname, &st) >= 0)
10727 {
10728 perm = vim_strsave((char_u *)"---------");
10729 if (perm != NULL)
10730 {
10731 for (i = 0; i < 9; i++)
10732 {
10733 if (st.st_mode & (1 << (8 - i)))
10734 perm[i] = flags[i % 3];
10735 }
10736 }
10737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010739}
10740
10741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 * "getfsize({fname})" function
10743 */
10744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T *argvars;
10747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
10749 char_u *fname;
10750 struct stat st;
10751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
10756 if (mch_stat((char *)fname, &st) >= 0)
10757 {
10758 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010763
10764 /* non-perfect check for overflow */
10765 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10766 rettv->vval.v_number = -2;
10767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 }
10769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771}
10772
10773/*
10774 * "getftime({fname})" function
10775 */
10776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010778 typval_T *argvars;
10779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780{
10781 char_u *fname;
10782 struct stat st;
10783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785
10786 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790}
10791
10792/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010793 * "getftype({fname})" function
10794 */
10795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010797 typval_T *argvars;
10798 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010799{
10800 char_u *fname;
10801 struct stat st;
10802 char_u *type = NULL;
10803 char *t;
10804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010808 if (mch_lstat((char *)fname, &st) >= 0)
10809 {
10810#ifdef S_ISREG
10811 if (S_ISREG(st.st_mode))
10812 t = "file";
10813 else if (S_ISDIR(st.st_mode))
10814 t = "dir";
10815# ifdef S_ISLNK
10816 else if (S_ISLNK(st.st_mode))
10817 t = "link";
10818# endif
10819# ifdef S_ISBLK
10820 else if (S_ISBLK(st.st_mode))
10821 t = "bdev";
10822# endif
10823# ifdef S_ISCHR
10824 else if (S_ISCHR(st.st_mode))
10825 t = "cdev";
10826# endif
10827# ifdef S_ISFIFO
10828 else if (S_ISFIFO(st.st_mode))
10829 t = "fifo";
10830# endif
10831# ifdef S_ISSOCK
10832 else if (S_ISSOCK(st.st_mode))
10833 t = "fifo";
10834# endif
10835 else
10836 t = "other";
10837#else
10838# ifdef S_IFMT
10839 switch (st.st_mode & S_IFMT)
10840 {
10841 case S_IFREG: t = "file"; break;
10842 case S_IFDIR: t = "dir"; break;
10843# ifdef S_IFLNK
10844 case S_IFLNK: t = "link"; break;
10845# endif
10846# ifdef S_IFBLK
10847 case S_IFBLK: t = "bdev"; break;
10848# endif
10849# ifdef S_IFCHR
10850 case S_IFCHR: t = "cdev"; break;
10851# endif
10852# ifdef S_IFIFO
10853 case S_IFIFO: t = "fifo"; break;
10854# endif
10855# ifdef S_IFSOCK
10856 case S_IFSOCK: t = "socket"; break;
10857# endif
10858 default: t = "other";
10859 }
10860# else
10861 if (mch_isdir(fname))
10862 t = "dir";
10863 else
10864 t = "file";
10865# endif
10866#endif
10867 type = vim_strsave((char_u *)t);
10868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010870}
10871
10872/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010873 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010874 */
10875 static void
10876f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010879{
10880 linenr_T lnum;
10881 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010882 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883
10884 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010885 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010886 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010887 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010888 retlist = FALSE;
10889 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010890 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010891 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010892 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010893 retlist = TRUE;
10894 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010895
Bram Moolenaar342337a2005-07-21 21:11:17 +000010896 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897}
10898
10899/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010900 * "getmatches()" function
10901 */
10902/*ARGSUSED*/
10903 static void
10904f_getmatches(argvars, rettv)
10905 typval_T *argvars;
10906 typval_T *rettv;
10907{
10908#ifdef FEAT_SEARCH_EXTRA
10909 dict_T *dict;
10910 matchitem_T *cur = curwin->w_match_head;
10911
10912 rettv->vval.v_number = 0;
10913
10914 if (rettv_list_alloc(rettv) == OK)
10915 {
10916 while (cur != NULL)
10917 {
10918 dict = dict_alloc();
10919 if (dict == NULL)
10920 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010921 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10922 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10923 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10924 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10925 list_append_dict(rettv->vval.v_list, dict);
10926 cur = cur->next;
10927 }
10928 }
10929#endif
10930}
10931
10932/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010933 * "getpid()" function
10934 */
10935/*ARGSUSED*/
10936 static void
10937f_getpid(argvars, rettv)
10938 typval_T *argvars;
10939 typval_T *rettv;
10940{
10941 rettv->vval.v_number = mch_get_pid();
10942}
10943
10944/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010945 * "getpos(string)" function
10946 */
10947 static void
10948f_getpos(argvars, rettv)
10949 typval_T *argvars;
10950 typval_T *rettv;
10951{
10952 pos_T *fp;
10953 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010954 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010955
10956 if (rettv_list_alloc(rettv) == OK)
10957 {
10958 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010959 fp = var2fpos(&argvars[0], TRUE, &fnum);
10960 if (fnum != -1)
10961 list_append_number(l, (varnumber_T)fnum);
10962 else
10963 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010964 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10965 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010966 list_append_number(l, (fp != NULL)
10967 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010968 : (varnumber_T)0);
10969 list_append_number(l,
10970#ifdef FEAT_VIRTUALEDIT
10971 (fp != NULL) ? (varnumber_T)fp->coladd :
10972#endif
10973 (varnumber_T)0);
10974 }
10975 else
10976 rettv->vval.v_number = FALSE;
10977}
10978
10979/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010980 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010981 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010982/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010983 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010984f_getqflist(argvars, rettv)
10985 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010986 typval_T *rettv;
10987{
10988#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010989 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010990#endif
10991
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010992 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010993#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010994 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010995 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010996 wp = NULL;
10997 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10998 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010999 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011000 if (wp == NULL)
11001 return;
11002 }
11003
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011004 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011005 }
11006#endif
11007}
11008
11009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 * "getreg()" function
11011 */
11012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016{
11017 char_u *strregname;
11018 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011019 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 strregname = get_tv_string_chk(&argvars[0]);
11025 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011026 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 regname = (strregname == NULL ? '"' : *strregname);
11032 if (regname == 0)
11033 regname = '"';
11034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 rettv->vval.v_string = error ? NULL :
11037 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
11040/*
11041 * "getregtype()" function
11042 */
11043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011045 typval_T *argvars;
11046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047{
11048 char_u *strregname;
11049 int regname;
11050 char_u buf[NUMBUFLEN + 2];
11051 long reglen = 0;
11052
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 {
11055 strregname = get_tv_string_chk(&argvars[0]);
11056 if (strregname == NULL) /* type error; errmsg already given */
11057 {
11058 rettv->v_type = VAR_STRING;
11059 rettv->vval.v_string = NULL;
11060 return;
11061 }
11062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 else
11064 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011065 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
11067 regname = (strregname == NULL ? '"' : *strregname);
11068 if (regname == 0)
11069 regname = '"';
11070
11071 buf[0] = NUL;
11072 buf[1] = NUL;
11073 switch (get_reg_type(regname, &reglen))
11074 {
11075 case MLINE: buf[0] = 'V'; break;
11076 case MCHAR: buf[0] = 'v'; break;
11077#ifdef FEAT_VISUAL
11078 case MBLOCK:
11079 buf[0] = Ctrl_V;
11080 sprintf((char *)buf + 1, "%ld", reglen + 1);
11081 break;
11082#endif
11083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 rettv->v_type = VAR_STRING;
11085 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086}
11087
11088/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011089 * "gettabwinvar()" function
11090 */
11091 static void
11092f_gettabwinvar(argvars, rettv)
11093 typval_T *argvars;
11094 typval_T *rettv;
11095{
11096 getwinvar(argvars, rettv, 1);
11097}
11098
11099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 * "getwinposx()" function
11101 */
11102/*ARGSUSED*/
11103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011105 typval_T *argvars;
11106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109#ifdef FEAT_GUI
11110 if (gui.in_use)
11111 {
11112 int x, y;
11113
11114 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 }
11117#endif
11118}
11119
11120/*
11121 * "getwinposy()" function
11122 */
11123/*ARGSUSED*/
11124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011126 typval_T *argvars;
11127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130#ifdef FEAT_GUI
11131 if (gui.in_use)
11132 {
11133 int x, y;
11134
11135 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 }
11138#endif
11139}
11140
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011141/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011142 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011143 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011144 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011145find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011146 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011147 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011148{
11149#ifdef FEAT_WINDOWS
11150 win_T *wp;
11151#endif
11152 int nr;
11153
11154 nr = get_tv_number_chk(vp, NULL);
11155
11156#ifdef FEAT_WINDOWS
11157 if (nr < 0)
11158 return NULL;
11159 if (nr == 0)
11160 return curwin;
11161
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011162 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11163 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011164 if (--nr <= 0)
11165 break;
11166 return wp;
11167#else
11168 if (nr == 0 || nr == 1)
11169 return curwin;
11170 return NULL;
11171#endif
11172}
11173
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174/*
11175 * "getwinvar()" function
11176 */
11177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011179 typval_T *argvars;
11180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011182 getwinvar(argvars, rettv, 0);
11183}
11184
11185/*
11186 * getwinvar() and gettabwinvar()
11187 */
11188 static void
11189getwinvar(argvars, rettv, off)
11190 typval_T *argvars;
11191 typval_T *rettv;
11192 int off; /* 1 for gettabwinvar() */
11193{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 win_T *win, *oldcurwin;
11195 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011197 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011199#ifdef FEAT_WINDOWS
11200 if (off == 1)
11201 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11202 else
11203 tp = curtab;
11204#endif
11205 win = find_win_by_nr(&argvars[off], tp);
11206 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->v_type = VAR_STRING;
11210 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211
11212 if (win != NULL && varname != NULL)
11213 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011214 /* Set curwin to be our win, temporarily. Also set curbuf, so
11215 * that we can get buffer-local options. */
11216 oldcurwin = curwin;
11217 curwin = win;
11218 curbuf = win->w_buffer;
11219
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 else
11223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 if (*varname == NUL)
11225 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11226 * scope prefix before the NUL byte is required by
11227 * find_var_in_ht(). */
11228 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011230 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011234
11235 /* restore previous notion of curwin */
11236 curwin = oldcurwin;
11237 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 }
11239
11240 --emsg_off;
11241}
11242
11243/*
11244 * "glob()" function
11245 */
11246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011248 typval_T *argvars;
11249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
11251 expand_T xpc;
11252
11253 ExpandInit(&xpc);
11254 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->v_type = VAR_STRING;
11256 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258}
11259
11260/*
11261 * "globpath()" function
11262 */
11263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
11268 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 if (file == NULL)
11273 rettv->vval.v_string = NULL;
11274 else
11275 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
11279 * "has()" function
11280 */
11281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *argvars;
11284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
11286 int i;
11287 char_u *name;
11288 int n = FALSE;
11289 static char *(has_list[]) =
11290 {
11291#ifdef AMIGA
11292 "amiga",
11293# ifdef FEAT_ARP
11294 "arp",
11295# endif
11296#endif
11297#ifdef __BEOS__
11298 "beos",
11299#endif
11300#ifdef MSDOS
11301# ifdef DJGPP
11302 "dos32",
11303# else
11304 "dos16",
11305# endif
11306#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011307#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 "mac",
11309#endif
11310#if defined(MACOS_X_UNIX)
11311 "macunix",
11312#endif
11313#ifdef OS2
11314 "os2",
11315#endif
11316#ifdef __QNX__
11317 "qnx",
11318#endif
11319#ifdef RISCOS
11320 "riscos",
11321#endif
11322#ifdef UNIX
11323 "unix",
11324#endif
11325#ifdef VMS
11326 "vms",
11327#endif
11328#ifdef WIN16
11329 "win16",
11330#endif
11331#ifdef WIN32
11332 "win32",
11333#endif
11334#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11335 "win32unix",
11336#endif
11337#ifdef WIN64
11338 "win64",
11339#endif
11340#ifdef EBCDIC
11341 "ebcdic",
11342#endif
11343#ifndef CASE_INSENSITIVE_FILENAME
11344 "fname_case",
11345#endif
11346#ifdef FEAT_ARABIC
11347 "arabic",
11348#endif
11349#ifdef FEAT_AUTOCMD
11350 "autocmd",
11351#endif
11352#ifdef FEAT_BEVAL
11353 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011354# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11355 "balloon_multiline",
11356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357#endif
11358#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11359 "builtin_terms",
11360# ifdef ALL_BUILTIN_TCAPS
11361 "all_builtin_terms",
11362# endif
11363#endif
11364#ifdef FEAT_BYTEOFF
11365 "byte_offset",
11366#endif
11367#ifdef FEAT_CINDENT
11368 "cindent",
11369#endif
11370#ifdef FEAT_CLIENTSERVER
11371 "clientserver",
11372#endif
11373#ifdef FEAT_CLIPBOARD
11374 "clipboard",
11375#endif
11376#ifdef FEAT_CMDL_COMPL
11377 "cmdline_compl",
11378#endif
11379#ifdef FEAT_CMDHIST
11380 "cmdline_hist",
11381#endif
11382#ifdef FEAT_COMMENTS
11383 "comments",
11384#endif
11385#ifdef FEAT_CRYPT
11386 "cryptv",
11387#endif
11388#ifdef FEAT_CSCOPE
11389 "cscope",
11390#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011391#ifdef CURSOR_SHAPE
11392 "cursorshape",
11393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394#ifdef DEBUG
11395 "debug",
11396#endif
11397#ifdef FEAT_CON_DIALOG
11398 "dialog_con",
11399#endif
11400#ifdef FEAT_GUI_DIALOG
11401 "dialog_gui",
11402#endif
11403#ifdef FEAT_DIFF
11404 "diff",
11405#endif
11406#ifdef FEAT_DIGRAPHS
11407 "digraphs",
11408#endif
11409#ifdef FEAT_DND
11410 "dnd",
11411#endif
11412#ifdef FEAT_EMACS_TAGS
11413 "emacs_tags",
11414#endif
11415 "eval", /* always present, of course! */
11416#ifdef FEAT_EX_EXTRA
11417 "ex_extra",
11418#endif
11419#ifdef FEAT_SEARCH_EXTRA
11420 "extra_search",
11421#endif
11422#ifdef FEAT_FKMAP
11423 "farsi",
11424#endif
11425#ifdef FEAT_SEARCHPATH
11426 "file_in_path",
11427#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011428#if defined(UNIX) && !defined(USE_SYSTEM)
11429 "filterpipe",
11430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431#ifdef FEAT_FIND_ID
11432 "find_in_path",
11433#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011434#ifdef FEAT_FLOAT
11435 "float",
11436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437#ifdef FEAT_FOLDING
11438 "folding",
11439#endif
11440#ifdef FEAT_FOOTER
11441 "footer",
11442#endif
11443#if !defined(USE_SYSTEM) && defined(UNIX)
11444 "fork",
11445#endif
11446#ifdef FEAT_GETTEXT
11447 "gettext",
11448#endif
11449#ifdef FEAT_GUI
11450 "gui",
11451#endif
11452#ifdef FEAT_GUI_ATHENA
11453# ifdef FEAT_GUI_NEXTAW
11454 "gui_neXtaw",
11455# else
11456 "gui_athena",
11457# endif
11458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459#ifdef FEAT_GUI_GTK
11460 "gui_gtk",
11461# ifdef HAVE_GTK2
11462 "gui_gtk2",
11463# endif
11464#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011465#ifdef FEAT_GUI_GNOME
11466 "gui_gnome",
11467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468#ifdef FEAT_GUI_MAC
11469 "gui_mac",
11470#endif
11471#ifdef FEAT_GUI_MOTIF
11472 "gui_motif",
11473#endif
11474#ifdef FEAT_GUI_PHOTON
11475 "gui_photon",
11476#endif
11477#ifdef FEAT_GUI_W16
11478 "gui_win16",
11479#endif
11480#ifdef FEAT_GUI_W32
11481 "gui_win32",
11482#endif
11483#ifdef FEAT_HANGULIN
11484 "hangul_input",
11485#endif
11486#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11487 "iconv",
11488#endif
11489#ifdef FEAT_INS_EXPAND
11490 "insert_expand",
11491#endif
11492#ifdef FEAT_JUMPLIST
11493 "jumplist",
11494#endif
11495#ifdef FEAT_KEYMAP
11496 "keymap",
11497#endif
11498#ifdef FEAT_LANGMAP
11499 "langmap",
11500#endif
11501#ifdef FEAT_LIBCALL
11502 "libcall",
11503#endif
11504#ifdef FEAT_LINEBREAK
11505 "linebreak",
11506#endif
11507#ifdef FEAT_LISP
11508 "lispindent",
11509#endif
11510#ifdef FEAT_LISTCMDS
11511 "listcmds",
11512#endif
11513#ifdef FEAT_LOCALMAP
11514 "localmap",
11515#endif
11516#ifdef FEAT_MENU
11517 "menu",
11518#endif
11519#ifdef FEAT_SESSION
11520 "mksession",
11521#endif
11522#ifdef FEAT_MODIFY_FNAME
11523 "modify_fname",
11524#endif
11525#ifdef FEAT_MOUSE
11526 "mouse",
11527#endif
11528#ifdef FEAT_MOUSESHAPE
11529 "mouseshape",
11530#endif
11531#if defined(UNIX) || defined(VMS)
11532# ifdef FEAT_MOUSE_DEC
11533 "mouse_dec",
11534# endif
11535# ifdef FEAT_MOUSE_GPM
11536 "mouse_gpm",
11537# endif
11538# ifdef FEAT_MOUSE_JSB
11539 "mouse_jsbterm",
11540# endif
11541# ifdef FEAT_MOUSE_NET
11542 "mouse_netterm",
11543# endif
11544# ifdef FEAT_MOUSE_PTERM
11545 "mouse_pterm",
11546# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011547# ifdef FEAT_SYSMOUSE
11548 "mouse_sysmouse",
11549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550# ifdef FEAT_MOUSE_XTERM
11551 "mouse_xterm",
11552# endif
11553#endif
11554#ifdef FEAT_MBYTE
11555 "multi_byte",
11556#endif
11557#ifdef FEAT_MBYTE_IME
11558 "multi_byte_ime",
11559#endif
11560#ifdef FEAT_MULTI_LANG
11561 "multi_lang",
11562#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011563#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011564#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011565 "mzscheme",
11566#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568#ifdef FEAT_OLE
11569 "ole",
11570#endif
11571#ifdef FEAT_OSFILETYPE
11572 "osfiletype",
11573#endif
11574#ifdef FEAT_PATH_EXTRA
11575 "path_extra",
11576#endif
11577#ifdef FEAT_PERL
11578#ifndef DYNAMIC_PERL
11579 "perl",
11580#endif
11581#endif
11582#ifdef FEAT_PYTHON
11583#ifndef DYNAMIC_PYTHON
11584 "python",
11585#endif
11586#endif
11587#ifdef FEAT_POSTSCRIPT
11588 "postscript",
11589#endif
11590#ifdef FEAT_PRINTER
11591 "printer",
11592#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011593#ifdef FEAT_PROFILE
11594 "profile",
11595#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011596#ifdef FEAT_RELTIME
11597 "reltime",
11598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599#ifdef FEAT_QUICKFIX
11600 "quickfix",
11601#endif
11602#ifdef FEAT_RIGHTLEFT
11603 "rightleft",
11604#endif
11605#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11606 "ruby",
11607#endif
11608#ifdef FEAT_SCROLLBIND
11609 "scrollbind",
11610#endif
11611#ifdef FEAT_CMDL_INFO
11612 "showcmd",
11613 "cmdline_info",
11614#endif
11615#ifdef FEAT_SIGNS
11616 "signs",
11617#endif
11618#ifdef FEAT_SMARTINDENT
11619 "smartindent",
11620#endif
11621#ifdef FEAT_SNIFF
11622 "sniff",
11623#endif
11624#ifdef FEAT_STL_OPT
11625 "statusline",
11626#endif
11627#ifdef FEAT_SUN_WORKSHOP
11628 "sun_workshop",
11629#endif
11630#ifdef FEAT_NETBEANS_INTG
11631 "netbeans_intg",
11632#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011633#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011634 "spell",
11635#endif
11636#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 "syntax",
11638#endif
11639#if defined(USE_SYSTEM) || !defined(UNIX)
11640 "system",
11641#endif
11642#ifdef FEAT_TAG_BINS
11643 "tag_binary",
11644#endif
11645#ifdef FEAT_TAG_OLDSTATIC
11646 "tag_old_static",
11647#endif
11648#ifdef FEAT_TAG_ANYWHITE
11649 "tag_any_white",
11650#endif
11651#ifdef FEAT_TCL
11652# ifndef DYNAMIC_TCL
11653 "tcl",
11654# endif
11655#endif
11656#ifdef TERMINFO
11657 "terminfo",
11658#endif
11659#ifdef FEAT_TERMRESPONSE
11660 "termresponse",
11661#endif
11662#ifdef FEAT_TEXTOBJ
11663 "textobjects",
11664#endif
11665#ifdef HAVE_TGETENT
11666 "tgetent",
11667#endif
11668#ifdef FEAT_TITLE
11669 "title",
11670#endif
11671#ifdef FEAT_TOOLBAR
11672 "toolbar",
11673#endif
11674#ifdef FEAT_USR_CMDS
11675 "user-commands", /* was accidentally included in 5.4 */
11676 "user_commands",
11677#endif
11678#ifdef FEAT_VIMINFO
11679 "viminfo",
11680#endif
11681#ifdef FEAT_VERTSPLIT
11682 "vertsplit",
11683#endif
11684#ifdef FEAT_VIRTUALEDIT
11685 "virtualedit",
11686#endif
11687#ifdef FEAT_VISUAL
11688 "visual",
11689#endif
11690#ifdef FEAT_VISUALEXTRA
11691 "visualextra",
11692#endif
11693#ifdef FEAT_VREPLACE
11694 "vreplace",
11695#endif
11696#ifdef FEAT_WILDIGN
11697 "wildignore",
11698#endif
11699#ifdef FEAT_WILDMENU
11700 "wildmenu",
11701#endif
11702#ifdef FEAT_WINDOWS
11703 "windows",
11704#endif
11705#ifdef FEAT_WAK
11706 "winaltkeys",
11707#endif
11708#ifdef FEAT_WRITEBACKUP
11709 "writebackup",
11710#endif
11711#ifdef FEAT_XIM
11712 "xim",
11713#endif
11714#ifdef FEAT_XFONTSET
11715 "xfontset",
11716#endif
11717#ifdef USE_XSMP
11718 "xsmp",
11719#endif
11720#ifdef USE_XSMP_INTERACT
11721 "xsmp_interact",
11722#endif
11723#ifdef FEAT_XCLIPBOARD
11724 "xterm_clipboard",
11725#endif
11726#ifdef FEAT_XTERM_SAVE
11727 "xterm_save",
11728#endif
11729#if defined(UNIX) && defined(FEAT_X11)
11730 "X11",
11731#endif
11732 NULL
11733 };
11734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 for (i = 0; has_list[i] != NULL; ++i)
11737 if (STRICMP(name, has_list[i]) == 0)
11738 {
11739 n = TRUE;
11740 break;
11741 }
11742
11743 if (n == FALSE)
11744 {
11745 if (STRNICMP(name, "patch", 5) == 0)
11746 n = has_patch(atoi((char *)name + 5));
11747 else if (STRICMP(name, "vim_starting") == 0)
11748 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011749#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11750 else if (STRICMP(name, "balloon_multiline") == 0)
11751 n = multiline_balloon_available();
11752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753#ifdef DYNAMIC_TCL
11754 else if (STRICMP(name, "tcl") == 0)
11755 n = tcl_enabled(FALSE);
11756#endif
11757#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11758 else if (STRICMP(name, "iconv") == 0)
11759 n = iconv_enabled(FALSE);
11760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011761#ifdef DYNAMIC_MZSCHEME
11762 else if (STRICMP(name, "mzscheme") == 0)
11763 n = mzscheme_enabled(FALSE);
11764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765#ifdef DYNAMIC_RUBY
11766 else if (STRICMP(name, "ruby") == 0)
11767 n = ruby_enabled(FALSE);
11768#endif
11769#ifdef DYNAMIC_PYTHON
11770 else if (STRICMP(name, "python") == 0)
11771 n = python_enabled(FALSE);
11772#endif
11773#ifdef DYNAMIC_PERL
11774 else if (STRICMP(name, "perl") == 0)
11775 n = perl_enabled(FALSE);
11776#endif
11777#ifdef FEAT_GUI
11778 else if (STRICMP(name, "gui_running") == 0)
11779 n = (gui.in_use || gui.starting);
11780# ifdef FEAT_GUI_W32
11781 else if (STRICMP(name, "gui_win32s") == 0)
11782 n = gui_is_win32s();
11783# endif
11784# ifdef FEAT_BROWSE
11785 else if (STRICMP(name, "browse") == 0)
11786 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11787# endif
11788#endif
11789#ifdef FEAT_SYN_HL
11790 else if (STRICMP(name, "syntax_items") == 0)
11791 n = syntax_present(curbuf);
11792#endif
11793#if defined(WIN3264)
11794 else if (STRICMP(name, "win95") == 0)
11795 n = mch_windows95();
11796#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011797#ifdef FEAT_NETBEANS_INTG
11798 else if (STRICMP(name, "netbeans_enabled") == 0)
11799 n = usingNetbeans;
11800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 }
11802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011807 * "has_key()" function
11808 */
11809 static void
11810f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 typval_T *argvars;
11812 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011813{
11814 rettv->vval.v_number = 0;
11815 if (argvars[0].v_type != VAR_DICT)
11816 {
11817 EMSG(_(e_dictreq));
11818 return;
11819 }
11820 if (argvars[0].vval.v_dict == NULL)
11821 return;
11822
11823 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011824 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011825}
11826
11827/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011828 * "haslocaldir()" function
11829 */
11830/*ARGSUSED*/
11831 static void
11832f_haslocaldir(argvars, rettv)
11833 typval_T *argvars;
11834 typval_T *rettv;
11835{
11836 rettv->vval.v_number = (curwin->w_localdir != NULL);
11837}
11838
11839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 * "hasmapto()" function
11841 */
11842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 typval_T *argvars;
11845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846{
11847 char_u *name;
11848 char_u *mode;
11849 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011850 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 mode = (char_u *)"nvo";
11855 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011858 if (argvars[2].v_type != VAR_UNKNOWN)
11859 abbr = get_tv_number(&argvars[2]);
11860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
Bram Moolenaar2c932302006-03-18 21:42:09 +000011862 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
11869 * "histadd()" function
11870 */
11871/*ARGSUSED*/
11872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 typval_T *argvars;
11875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
11877#ifdef FEAT_CMDHIST
11878 int histype;
11879 char_u *str;
11880 char_u buf[NUMBUFLEN];
11881#endif
11882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011883 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 if (check_restricted() || check_secure())
11885 return;
11886#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011887 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11888 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 if (histype >= 0)
11890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011891 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 if (*str != NUL)
11893 {
11894 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 return;
11897 }
11898 }
11899#endif
11900}
11901
11902/*
11903 * "histdel()" function
11904 */
11905/*ARGSUSED*/
11906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910{
11911#ifdef FEAT_CMDHIST
11912 int n;
11913 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11917 if (str == NULL)
11918 n = 0;
11919 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011921 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011922 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 else
11927 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011929 get_tv_string_buf(&argvars[1], buf));
11930 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933#endif
11934}
11935
11936/*
11937 * "histget()" function
11938 */
11939/*ARGSUSED*/
11940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011942 typval_T *argvars;
11943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944{
11945#ifdef FEAT_CMDHIST
11946 int type;
11947 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11951 if (str == NULL)
11952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 {
11955 type = get_histtype(str);
11956 if (argvars[1].v_type == VAR_UNKNOWN)
11957 idx = get_history_idx(type);
11958 else
11959 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11960 /* -1 on type error */
11961 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967}
11968
11969/*
11970 * "histnr()" function
11971 */
11972/*ARGSUSED*/
11973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *argvars;
11976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
11978 int i;
11979
11980#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 char_u *history = get_tv_string_chk(&argvars[0]);
11982
11983 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 if (i >= HIST_CMD && i < HIST_COUNT)
11985 i = get_history_idx(i);
11986 else
11987#endif
11988 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 * "highlightID(name)" function
11994 */
11995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 typval_T *argvars;
11998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001}
12002
12003/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012004 * "highlight_exists()" function
12005 */
12006 static void
12007f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010{
12011 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12012}
12013
12014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 * "hostname()" function
12016 */
12017/*ARGSUSED*/
12018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022{
12023 char_u hostname[256];
12024
12025 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->v_type = VAR_STRING;
12027 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028}
12029
12030/*
12031 * iconv() function
12032 */
12033/*ARGSUSED*/
12034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012036 typval_T *argvars;
12037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038{
12039#ifdef FEAT_MBYTE
12040 char_u buf1[NUMBUFLEN];
12041 char_u buf2[NUMBUFLEN];
12042 char_u *from, *to, *str;
12043 vimconv_T vimconv;
12044#endif
12045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 rettv->v_type = VAR_STRING;
12047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048
12049#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 str = get_tv_string(&argvars[0]);
12051 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12052 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 vimconv.vc_type = CONV_NONE;
12054 convert_setup(&vimconv, from, to);
12055
12056 /* If the encodings are equal, no conversion needed. */
12057 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061
12062 convert_setup(&vimconv, NULL, NULL);
12063 vim_free(from);
12064 vim_free(to);
12065#endif
12066}
12067
12068/*
12069 * "indent()" function
12070 */
12071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 typval_T *argvars;
12074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076 linenr_T lnum;
12077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083}
12084
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012085/*
12086 * "index()" function
12087 */
12088 static void
12089f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012090 typval_T *argvars;
12091 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012092{
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 list_T *l;
12094 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012095 long idx = 0;
12096 int ic = FALSE;
12097
12098 rettv->vval.v_number = -1;
12099 if (argvars[0].v_type != VAR_LIST)
12100 {
12101 EMSG(_(e_listreq));
12102 return;
12103 }
12104 l = argvars[0].vval.v_list;
12105 if (l != NULL)
12106 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012107 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012108 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012110 int error = FALSE;
12111
Bram Moolenaar758711c2005-02-02 23:11:38 +000012112 /* Start at specified item. Use the cached index that list_find()
12113 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012115 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012116 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012117 ic = get_tv_number_chk(&argvars[3], &error);
12118 if (error)
12119 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012120 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012121
Bram Moolenaar758711c2005-02-02 23:11:38 +000012122 for ( ; item != NULL; item = item->li_next, ++idx)
12123 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012124 {
12125 rettv->vval.v_number = idx;
12126 break;
12127 }
12128 }
12129}
12130
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131static int inputsecret_flag = 0;
12132
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012133static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12134
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012136 * This function is used by f_input() and f_inputdialog() functions. The third
12137 * argument to f_input() specifies the type of completion to use at the
12138 * prompt. The third argument to f_inputdialog() specifies the value to return
12139 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 */
12141 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012142get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012145 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 char_u *p = NULL;
12149 int c;
12150 char_u buf[NUMBUFLEN];
12151 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012153 int xp_type = EXPAND_NOTHING;
12154 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012157 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158
12159#ifdef NO_CONSOLE_INPUT
12160 /* While starting up, there is no place to enter text. */
12161 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163#endif
12164
12165 cmd_silent = FALSE; /* Want to see the prompt. */
12166 if (prompt != NULL)
12167 {
12168 /* Only the part of the message after the last NL is considered as
12169 * prompt for the command line */
12170 p = vim_strrchr(prompt, '\n');
12171 if (p == NULL)
12172 p = prompt;
12173 else
12174 {
12175 ++p;
12176 c = *p;
12177 *p = NUL;
12178 msg_start();
12179 msg_clr_eos();
12180 msg_puts_attr(prompt, echo_attr);
12181 msg_didout = FALSE;
12182 msg_starthere();
12183 *p = c;
12184 }
12185 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012187 if (argvars[1].v_type != VAR_UNKNOWN)
12188 {
12189 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12190 if (defstr != NULL)
12191 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012193 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012194 {
12195 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012196 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012197 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012198
Bram Moolenaar4463f292005-09-25 22:20:24 +000012199 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012200
Bram Moolenaar4463f292005-09-25 22:20:24 +000012201 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12202 if (xp_name == NULL)
12203 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012204
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012205 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012206
Bram Moolenaar4463f292005-09-25 22:20:24 +000012207 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12208 &xp_arg) == FAIL)
12209 return;
12210 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012211 }
12212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012213 if (defstr != NULL)
12214 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012215 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12216 xp_type, xp_arg);
12217
12218 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012220 /* since the user typed this, no need to wait for return */
12221 need_wait_return = FALSE;
12222 msg_didout = FALSE;
12223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 cmd_silent = cmd_silent_save;
12225}
12226
12227/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012228 * "input()" function
12229 * Also handles inputsecret() when inputsecret is set.
12230 */
12231 static void
12232f_input(argvars, rettv)
12233 typval_T *argvars;
12234 typval_T *rettv;
12235{
12236 get_user_input(argvars, rettv, FALSE);
12237}
12238
12239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 * "inputdialog()" function
12241 */
12242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 typval_T *argvars;
12245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246{
12247#if defined(FEAT_GUI_TEXTDIALOG)
12248 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12249 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12250 {
12251 char_u *message;
12252 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 message = get_tv_string_chk(&argvars[0]);
12256 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012257 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012258 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 else
12260 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012261 if (message != NULL && defstr != NULL
12262 && do_dialog(VIM_QUESTION, NULL, message,
12263 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 else
12266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012267 if (message != NULL && defstr != NULL
12268 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012269 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 rettv->vval.v_string = vim_strsave(
12271 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 }
12277 else
12278#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012279 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280}
12281
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012282/*
12283 * "inputlist()" function
12284 */
12285 static void
12286f_inputlist(argvars, rettv)
12287 typval_T *argvars;
12288 typval_T *rettv;
12289{
12290 listitem_T *li;
12291 int selected;
12292 int mouse_used;
12293
12294 rettv->vval.v_number = 0;
12295#ifdef NO_CONSOLE_INPUT
12296 /* While starting up, there is no place to enter text. */
12297 if (no_console_input())
12298 return;
12299#endif
12300 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12301 {
12302 EMSG2(_(e_listarg), "inputlist()");
12303 return;
12304 }
12305
12306 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012307 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012308 lines_left = Rows; /* avoid more prompt */
12309 msg_scroll = TRUE;
12310 msg_clr_eos();
12311
12312 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12313 {
12314 msg_puts(get_tv_string(&li->li_tv));
12315 msg_putchar('\n');
12316 }
12317
12318 /* Ask for choice. */
12319 selected = prompt_for_number(&mouse_used);
12320 if (mouse_used)
12321 selected -= lines_left;
12322
12323 rettv->vval.v_number = selected;
12324}
12325
12326
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12328
12329/*
12330 * "inputrestore()" function
12331 */
12332/*ARGSUSED*/
12333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 typval_T *argvars;
12336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
12338 if (ga_userinput.ga_len > 0)
12339 {
12340 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12342 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 }
12345 else if (p_verbose > 1)
12346 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012347 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 }
12350}
12351
12352/*
12353 * "inputsave()" function
12354 */
12355/*ARGSUSED*/
12356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012358 typval_T *argvars;
12359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012361 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 if (ga_grow(&ga_userinput, 1) == OK)
12363 {
12364 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12365 + ga_userinput.ga_len);
12366 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 }
12369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371}
12372
12373/*
12374 * "inputsecret()" function
12375 */
12376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377f_inputsecret(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 ++cmdline_star;
12382 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 --cmdline_star;
12385 --inputsecret_flag;
12386}
12387
12388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012389 * "insert()" function
12390 */
12391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *argvars;
12394 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012395{
12396 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 listitem_T *item;
12398 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012399 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012400
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012401 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012402 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012403 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012404 else if ((l = argvars[0].vval.v_list) != NULL
12405 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012406 {
12407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012408 before = get_tv_number_chk(&argvars[2], &error);
12409 if (error)
12410 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012411
Bram Moolenaar758711c2005-02-02 23:11:38 +000012412 if (before == l->lv_len)
12413 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012414 else
12415 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012416 item = list_find(l, before);
12417 if (item == NULL)
12418 {
12419 EMSGN(_(e_listidx), before);
12420 l = NULL;
12421 }
12422 }
12423 if (l != NULL)
12424 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012425 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012426 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012427 }
12428 }
12429}
12430
12431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 * "isdirectory()" function
12433 */
12434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 typval_T *argvars;
12437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
12441
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012442/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012443 * "islocked()" function
12444 */
12445 static void
12446f_islocked(argvars, rettv)
12447 typval_T *argvars;
12448 typval_T *rettv;
12449{
12450 lval_T lv;
12451 char_u *end;
12452 dictitem_T *di;
12453
12454 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012455 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12456 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012457 if (end != NULL && lv.ll_name != NULL)
12458 {
12459 if (*end != NUL)
12460 EMSG(_(e_trailing));
12461 else
12462 {
12463 if (lv.ll_tv == NULL)
12464 {
12465 if (check_changedtick(lv.ll_name))
12466 rettv->vval.v_number = 1; /* always locked */
12467 else
12468 {
12469 di = find_var(lv.ll_name, NULL);
12470 if (di != NULL)
12471 {
12472 /* Consider a variable locked when:
12473 * 1. the variable itself is locked
12474 * 2. the value of the variable is locked.
12475 * 3. the List or Dict value is locked.
12476 */
12477 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12478 || tv_islocked(&di->di_tv));
12479 }
12480 }
12481 }
12482 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012483 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012484 else if (lv.ll_newkey != NULL)
12485 EMSG2(_(e_dictkey), lv.ll_newkey);
12486 else if (lv.ll_list != NULL)
12487 /* List item. */
12488 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12489 else
12490 /* Dictionary item. */
12491 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12492 }
12493 }
12494
12495 clear_lval(&lv);
12496}
12497
Bram Moolenaar33570922005-01-25 22:26:29 +000012498static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012499
12500/*
12501 * Turn a dict into a list:
12502 * "what" == 0: list of keys
12503 * "what" == 1: list of values
12504 * "what" == 2: list of items
12505 */
12506 static void
12507dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *argvars;
12509 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012510 int what;
12511{
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 list_T *l2;
12513 dictitem_T *di;
12514 hashitem_T *hi;
12515 listitem_T *li;
12516 listitem_T *li2;
12517 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012518 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012519
12520 rettv->vval.v_number = 0;
12521 if (argvars[0].v_type != VAR_DICT)
12522 {
12523 EMSG(_(e_dictreq));
12524 return;
12525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012526 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012527 return;
12528
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012529 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012530 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012531
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012532 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012533 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012534 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012535 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012536 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012537 --todo;
12538 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012539
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012540 li = listitem_alloc();
12541 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012542 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012543 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012544
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012545 if (what == 0)
12546 {
12547 /* keys() */
12548 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012549 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012550 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12551 }
12552 else if (what == 1)
12553 {
12554 /* values() */
12555 copy_tv(&di->di_tv, &li->li_tv);
12556 }
12557 else
12558 {
12559 /* items() */
12560 l2 = list_alloc();
12561 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012562 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012563 li->li_tv.vval.v_list = l2;
12564 if (l2 == NULL)
12565 break;
12566 ++l2->lv_refcount;
12567
12568 li2 = listitem_alloc();
12569 if (li2 == NULL)
12570 break;
12571 list_append(l2, li2);
12572 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012573 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012574 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12575
12576 li2 = listitem_alloc();
12577 if (li2 == NULL)
12578 break;
12579 list_append(l2, li2);
12580 copy_tv(&di->di_tv, &li2->li_tv);
12581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012582 }
12583 }
12584}
12585
12586/*
12587 * "items(dict)" function
12588 */
12589 static void
12590f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012591 typval_T *argvars;
12592 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012593{
12594 dict_list(argvars, rettv, 2);
12595}
12596
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012598 * "join()" function
12599 */
12600 static void
12601f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 typval_T *argvars;
12603 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012604{
12605 garray_T ga;
12606 char_u *sep;
12607
12608 rettv->vval.v_number = 0;
12609 if (argvars[0].v_type != VAR_LIST)
12610 {
12611 EMSG(_(e_listreq));
12612 return;
12613 }
12614 if (argvars[0].vval.v_list == NULL)
12615 return;
12616 if (argvars[1].v_type == VAR_UNKNOWN)
12617 sep = (char_u *)" ";
12618 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012620
12621 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012622
12623 if (sep != NULL)
12624 {
12625 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012626 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627 ga_append(&ga, NUL);
12628 rettv->vval.v_string = (char_u *)ga.ga_data;
12629 }
12630 else
12631 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012632}
12633
12634/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012635 * "keys()" function
12636 */
12637 static void
12638f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012641{
12642 dict_list(argvars, rettv, 0);
12643}
12644
12645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 * "last_buffer_nr()" function.
12647 */
12648/*ARGSUSED*/
12649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 typval_T *argvars;
12652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653{
12654 int n = 0;
12655 buf_T *buf;
12656
12657 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12658 if (n < buf->b_fnum)
12659 n = buf->b_fnum;
12660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012662}
12663
12664/*
12665 * "len()" function
12666 */
12667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *argvars;
12670 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012671{
12672 switch (argvars[0].v_type)
12673 {
12674 case VAR_STRING:
12675 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = (varnumber_T)STRLEN(
12677 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012678 break;
12679 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012681 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012682 case VAR_DICT:
12683 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12684 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012686 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012687 break;
12688 }
12689}
12690
Bram Moolenaar33570922005-01-25 22:26:29 +000012691static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012692
12693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012695 typval_T *argvars;
12696 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012697 int type;
12698{
12699#ifdef FEAT_LIBCALL
12700 char_u *string_in;
12701 char_u **string_result;
12702 int nr_result;
12703#endif
12704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012706 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710
12711 if (check_restricted() || check_secure())
12712 return;
12713
12714#ifdef FEAT_LIBCALL
12715 /* The first two args must be strings, otherwise its meaningless */
12716 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12717 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012718 string_in = NULL;
12719 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012720 string_in = argvars[2].vval.v_string;
12721 if (type == VAR_NUMBER)
12722 string_result = NULL;
12723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012725 if (mch_libcall(argvars[0].vval.v_string,
12726 argvars[1].vval.v_string,
12727 string_in,
12728 argvars[2].vval.v_number,
12729 string_result,
12730 &nr_result) == OK
12731 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733 }
12734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735}
12736
12737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012738 * "libcall()" function
12739 */
12740 static void
12741f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012742 typval_T *argvars;
12743 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012744{
12745 libcall_common(argvars, rettv, VAR_STRING);
12746}
12747
12748/*
12749 * "libcallnr()" function
12750 */
12751 static void
12752f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *argvars;
12754 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755{
12756 libcall_common(argvars, rettv, VAR_NUMBER);
12757}
12758
12759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 * "line(string)" function
12761 */
12762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766{
12767 linenr_T lnum = 0;
12768 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012769 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012771 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 if (fp != NULL)
12773 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775}
12776
12777/*
12778 * "line2byte(lnum)" function
12779 */
12780/*ARGSUSED*/
12781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *argvars;
12784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785{
12786#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788#else
12789 linenr_T lnum;
12790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12796 if (rettv->vval.v_number >= 0)
12797 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#endif
12799}
12800
12801/*
12802 * "lispindent(lnum)" function
12803 */
12804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808{
12809#ifdef FEAT_LISP
12810 pos_T pos;
12811 linenr_T lnum;
12812
12813 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12816 {
12817 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 curwin->w_cursor = pos;
12820 }
12821 else
12822#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
12827 * "localtime()" function
12828 */
12829/*ARGSUSED*/
12830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *argvars;
12833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
Bram Moolenaar33570922005-01-25 22:26:29 +000012838static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839
12840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 typval_T *argvars;
12843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 int exact;
12845{
12846 char_u *keys;
12847 char_u *which;
12848 char_u buf[NUMBUFLEN];
12849 char_u *keys_buf = NULL;
12850 char_u *rhs;
12851 int mode;
12852 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012853 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854
12855 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 rettv->v_type = VAR_STRING;
12857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 if (*keys == NUL)
12861 return;
12862
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012863 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012866 if (argvars[2].v_type != VAR_UNKNOWN)
12867 abbr = get_tv_number(&argvars[2]);
12868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 else
12870 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 if (which == NULL)
12872 return;
12873
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 mode = get_map_mode(&which, 0);
12875
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012876 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012877 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 vim_free(keys_buf);
12879 if (rhs != NULL)
12880 {
12881 ga_init(&ga);
12882 ga.ga_itemsize = 1;
12883 ga.ga_growsize = 40;
12884
12885 while (*rhs != NUL)
12886 ga_concat(&ga, str2special(&rhs, FALSE));
12887
12888 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 }
12891}
12892
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012893#ifdef FEAT_FLOAT
12894/*
12895 * "log10()" function
12896 */
12897 static void
12898f_log10(argvars, rettv)
12899 typval_T *argvars;
12900 typval_T *rettv;
12901{
12902 float_T f;
12903
12904 rettv->v_type = VAR_FLOAT;
12905 if (get_float_arg(argvars, &f) == OK)
12906 rettv->vval.v_float = log10(f);
12907 else
12908 rettv->vval.v_float = 0.0;
12909}
12910#endif
12911
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012913 * "map()" function
12914 */
12915 static void
12916f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012917 typval_T *argvars;
12918 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012919{
12920 filter_map(argvars, rettv, TRUE);
12921}
12922
12923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012924 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 */
12926 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012927f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012928 typval_T *argvars;
12929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012935 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 */
12937 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012938f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012939 typval_T *argvars;
12940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012942 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943}
12944
Bram Moolenaar33570922005-01-25 22:26:29 +000012945static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
12947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012949 typval_T *argvars;
12950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 int type;
12952{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012953 char_u *str = NULL;
12954 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 char_u *pat;
12956 regmatch_T regmatch;
12957 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012958 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 char_u *save_cpo;
12960 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012961 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012962 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012963 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012964 list_T *l = NULL;
12965 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012966 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012967 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968
12969 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12970 save_cpo = p_cpo;
12971 p_cpo = (char_u *)"";
12972
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012973 rettv->vval.v_number = -1;
12974 if (type == 3)
12975 {
12976 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012977 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012978 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012979 }
12980 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->v_type = VAR_STRING;
12983 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012986 if (argvars[0].v_type == VAR_LIST)
12987 {
12988 if ((l = argvars[0].vval.v_list) == NULL)
12989 goto theend;
12990 li = l->lv_first;
12991 }
12992 else
12993 expr = str = get_tv_string(&argvars[0]);
12994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012995 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12996 if (pat == NULL)
12997 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012998
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013001 int error = FALSE;
13002
13003 start = get_tv_number_chk(&argvars[2], &error);
13004 if (error)
13005 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013006 if (l != NULL)
13007 {
13008 li = list_find(l, start);
13009 if (li == NULL)
13010 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013011 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013012 }
13013 else
13014 {
13015 if (start < 0)
13016 start = 0;
13017 if (start > (long)STRLEN(str))
13018 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013019 /* When "count" argument is there ignore matches before "start",
13020 * otherwise skip part of the string. Differs when pattern is "^"
13021 * or "\<". */
13022 if (argvars[3].v_type != VAR_UNKNOWN)
13023 startcol = start;
13024 else
13025 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013026 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013027
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013028 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 nth = get_tv_number_chk(&argvars[3], &error);
13030 if (error)
13031 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 }
13033
13034 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13035 if (regmatch.regprog != NULL)
13036 {
13037 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013038
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013039 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013040 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013041 if (l != NULL)
13042 {
13043 if (li == NULL)
13044 {
13045 match = FALSE;
13046 break;
13047 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013048 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013049 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013050 if (str == NULL)
13051 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013052 }
13053
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013054 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013055
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013056 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013057 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013058 if (l == NULL && !match)
13059 break;
13060
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013061 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013062 if (l != NULL)
13063 {
13064 li = li->li_next;
13065 ++idx;
13066 }
13067 else
13068 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013069#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013070 startcol = (colnr_T)(regmatch.startp[0]
13071 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013072#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013073 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013074#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013075 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013076 }
13077
13078 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013080 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013082 int i;
13083
13084 /* return list with matched string and submatches */
13085 for (i = 0; i < NSUBEXP; ++i)
13086 {
13087 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013088 {
13089 if (list_append_string(rettv->vval.v_list,
13090 (char_u *)"", 0) == FAIL)
13091 break;
13092 }
13093 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013094 regmatch.startp[i],
13095 (int)(regmatch.endp[i] - regmatch.startp[i]))
13096 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013097 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013098 }
13099 }
13100 else if (type == 2)
13101 {
13102 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013103 if (l != NULL)
13104 copy_tv(&li->li_tv, rettv);
13105 else
13106 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013108 }
13109 else if (l != NULL)
13110 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 else
13112 {
13113 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 (varnumber_T)(regmatch.startp[0] - str);
13116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013119 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 }
13121 }
13122 vim_free(regmatch.regprog);
13123 }
13124
13125theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013126 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 p_cpo = save_cpo;
13128}
13129
13130/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131 * "match()" function
13132 */
13133 static void
13134f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013135 typval_T *argvars;
13136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013137{
13138 find_some_match(argvars, rettv, 1);
13139}
13140
13141/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013142 * "matchadd()" function
13143 */
13144 static void
13145f_matchadd(argvars, rettv)
13146 typval_T *argvars;
13147 typval_T *rettv;
13148{
13149#ifdef FEAT_SEARCH_EXTRA
13150 char_u buf[NUMBUFLEN];
13151 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13152 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13153 int prio = 10; /* default priority */
13154 int id = -1;
13155 int error = FALSE;
13156
13157 rettv->vval.v_number = -1;
13158
13159 if (grp == NULL || pat == NULL)
13160 return;
13161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013162 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013163 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013164 if (argvars[3].v_type != VAR_UNKNOWN)
13165 id = get_tv_number_chk(&argvars[3], &error);
13166 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013167 if (error == TRUE)
13168 return;
13169 if (id >= 1 && id <= 3)
13170 {
13171 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13172 return;
13173 }
13174
13175 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13176#endif
13177}
13178
13179/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013180 * "matcharg()" function
13181 */
13182 static void
13183f_matcharg(argvars, rettv)
13184 typval_T *argvars;
13185 typval_T *rettv;
13186{
13187 if (rettv_list_alloc(rettv) == OK)
13188 {
13189#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013190 int id = get_tv_number(&argvars[0]);
13191 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013192
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013193 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013194 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013195 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13196 {
13197 list_append_string(rettv->vval.v_list,
13198 syn_id2name(m->hlg_id), -1);
13199 list_append_string(rettv->vval.v_list, m->pattern, -1);
13200 }
13201 else
13202 {
13203 list_append_string(rettv->vval.v_list, NUL, -1);
13204 list_append_string(rettv->vval.v_list, NUL, -1);
13205 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013206 }
13207#endif
13208 }
13209}
13210
13211/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013212 * "matchdelete()" function
13213 */
13214 static void
13215f_matchdelete(argvars, rettv)
13216 typval_T *argvars;
13217 typval_T *rettv;
13218{
13219#ifdef FEAT_SEARCH_EXTRA
13220 rettv->vval.v_number = match_delete(curwin,
13221 (int)get_tv_number(&argvars[0]), TRUE);
13222#endif
13223}
13224
13225/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013226 * "matchend()" function
13227 */
13228 static void
13229f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013230 typval_T *argvars;
13231 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232{
13233 find_some_match(argvars, rettv, 0);
13234}
13235
13236/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013237 * "matchlist()" function
13238 */
13239 static void
13240f_matchlist(argvars, rettv)
13241 typval_T *argvars;
13242 typval_T *rettv;
13243{
13244 find_some_match(argvars, rettv, 3);
13245}
13246
13247/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013248 * "matchstr()" function
13249 */
13250 static void
13251f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013252 typval_T *argvars;
13253 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013254{
13255 find_some_match(argvars, rettv, 2);
13256}
13257
Bram Moolenaar33570922005-01-25 22:26:29 +000013258static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013259
13260 static void
13261max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013262 typval_T *argvars;
13263 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013264 int domax;
13265{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013266 long n = 0;
13267 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013269
13270 if (argvars[0].v_type == VAR_LIST)
13271 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013272 list_T *l;
13273 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013274
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013275 l = argvars[0].vval.v_list;
13276 if (l != NULL)
13277 {
13278 li = l->lv_first;
13279 if (li != NULL)
13280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013282 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013283 {
13284 li = li->li_next;
13285 if (li == NULL)
13286 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013288 if (domax ? i > n : i < n)
13289 n = i;
13290 }
13291 }
13292 }
13293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013294 else if (argvars[0].v_type == VAR_DICT)
13295 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013296 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013297 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013299 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013300
13301 d = argvars[0].vval.v_dict;
13302 if (d != NULL)
13303 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013304 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013305 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013307 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013309 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 if (first)
13312 {
13313 n = i;
13314 first = FALSE;
13315 }
13316 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013317 n = i;
13318 }
13319 }
13320 }
13321 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013322 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013323 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013325}
13326
13327/*
13328 * "max()" function
13329 */
13330 static void
13331f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 typval_T *argvars;
13333 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013334{
13335 max_min(argvars, rettv, TRUE);
13336}
13337
13338/*
13339 * "min()" function
13340 */
13341 static void
13342f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013343 typval_T *argvars;
13344 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013345{
13346 max_min(argvars, rettv, FALSE);
13347}
13348
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013349static int mkdir_recurse __ARGS((char_u *dir, int prot));
13350
13351/*
13352 * Create the directory in which "dir" is located, and higher levels when
13353 * needed.
13354 */
13355 static int
13356mkdir_recurse(dir, prot)
13357 char_u *dir;
13358 int prot;
13359{
13360 char_u *p;
13361 char_u *updir;
13362 int r = FAIL;
13363
13364 /* Get end of directory name in "dir".
13365 * We're done when it's "/" or "c:/". */
13366 p = gettail_sep(dir);
13367 if (p <= get_past_head(dir))
13368 return OK;
13369
13370 /* If the directory exists we're done. Otherwise: create it.*/
13371 updir = vim_strnsave(dir, (int)(p - dir));
13372 if (updir == NULL)
13373 return FAIL;
13374 if (mch_isdir(updir))
13375 r = OK;
13376 else if (mkdir_recurse(updir, prot) == OK)
13377 r = vim_mkdir_emsg(updir, prot);
13378 vim_free(updir);
13379 return r;
13380}
13381
13382#ifdef vim_mkdir
13383/*
13384 * "mkdir()" function
13385 */
13386 static void
13387f_mkdir(argvars, rettv)
13388 typval_T *argvars;
13389 typval_T *rettv;
13390{
13391 char_u *dir;
13392 char_u buf[NUMBUFLEN];
13393 int prot = 0755;
13394
13395 rettv->vval.v_number = FAIL;
13396 if (check_restricted() || check_secure())
13397 return;
13398
13399 dir = get_tv_string_buf(&argvars[0], buf);
13400 if (argvars[1].v_type != VAR_UNKNOWN)
13401 {
13402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 prot = get_tv_number_chk(&argvars[2], NULL);
13404 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013405 mkdir_recurse(dir, prot);
13406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013408}
13409#endif
13410
Bram Moolenaar0d660222005-01-07 21:51:51 +000013411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 * "mode()" function
13413 */
13414/*ARGSUSED*/
13415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013420 char_u buf[3];
13421
13422 buf[1] = NUL;
13423 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424
13425#ifdef FEAT_VISUAL
13426 if (VIsual_active)
13427 {
13428 if (VIsual_select)
13429 buf[0] = VIsual_mode + 's' - 'v';
13430 else
13431 buf[0] = VIsual_mode;
13432 }
13433 else
13434#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013435 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13436 || State == CONFIRM)
13437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013439 if (State == ASKMORE)
13440 buf[1] = 'm';
13441 else if (State == CONFIRM)
13442 buf[1] = '?';
13443 }
13444 else if (State == EXTERNCMD)
13445 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 else if (State & INSERT)
13447 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013448#ifdef FEAT_VREPLACE
13449 if (State & VREPLACE_FLAG)
13450 {
13451 buf[0] = 'R';
13452 buf[1] = 'v';
13453 }
13454 else
13455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 if (State & REPLACE_FLAG)
13457 buf[0] = 'R';
13458 else
13459 buf[0] = 'i';
13460 }
13461 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013464 if (exmode_active)
13465 buf[1] = 'v';
13466 }
13467 else if (exmode_active)
13468 {
13469 buf[0] = 'c';
13470 buf[1] = 'e';
13471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013475 if (finish_op)
13476 buf[1] = 'o';
13477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013479 /* A zero number or empty string argument: return only major mode. */
13480 if (!(argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number != 0)
13481 && !(argvars[0].v_type == VAR_STRING
13482 && *get_tv_string(&argvars[0]) != NUL))
13483 buf[1] = NUL;
13484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 rettv->vval.v_string = vim_strsave(buf);
13486 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487}
13488
13489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490 * "nextnonblank()" function
13491 */
13492 static void
13493f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496{
13497 linenr_T lnum;
13498
13499 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502 {
13503 lnum = 0;
13504 break;
13505 }
13506 if (*skipwhite(ml_get(lnum)) != NUL)
13507 break;
13508 }
13509 rettv->vval.v_number = lnum;
13510}
13511
13512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 * "nr2char()" function
13514 */
13515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013517 typval_T *argvars;
13518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519{
13520 char_u buf[NUMBUFLEN];
13521
13522#ifdef FEAT_MBYTE
13523 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 else
13526#endif
13527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 buf[1] = NUL;
13530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->v_type = VAR_STRING;
13532 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013533}
13534
13535/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013536 * "pathshorten()" function
13537 */
13538 static void
13539f_pathshorten(argvars, rettv)
13540 typval_T *argvars;
13541 typval_T *rettv;
13542{
13543 char_u *p;
13544
13545 rettv->v_type = VAR_STRING;
13546 p = get_tv_string_chk(&argvars[0]);
13547 if (p == NULL)
13548 rettv->vval.v_string = NULL;
13549 else
13550 {
13551 p = vim_strsave(p);
13552 rettv->vval.v_string = p;
13553 if (p != NULL)
13554 shorten_dir(p);
13555 }
13556}
13557
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013558#ifdef FEAT_FLOAT
13559/*
13560 * "pow()" function
13561 */
13562 static void
13563f_pow(argvars, rettv)
13564 typval_T *argvars;
13565 typval_T *rettv;
13566{
13567 float_T fx, fy;
13568
13569 rettv->v_type = VAR_FLOAT;
13570 if (get_float_arg(argvars, &fx) == OK
13571 && get_float_arg(&argvars[1], &fy) == OK)
13572 rettv->vval.v_float = pow(fx, fy);
13573 else
13574 rettv->vval.v_float = 0.0;
13575}
13576#endif
13577
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579 * "prevnonblank()" function
13580 */
13581 static void
13582f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013583 typval_T *argvars;
13584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013585{
13586 linenr_T lnum;
13587
13588 lnum = get_tv_lnum(argvars);
13589 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13590 lnum = 0;
13591 else
13592 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13593 --lnum;
13594 rettv->vval.v_number = lnum;
13595}
13596
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013597#ifdef HAVE_STDARG_H
13598/* This dummy va_list is here because:
13599 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13600 * - locally in the function results in a "used before set" warning
13601 * - using va_start() to initialize it gives "function with fixed args" error */
13602static va_list ap;
13603#endif
13604
Bram Moolenaar8c711452005-01-14 21:53:12 +000013605/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013606 * "printf()" function
13607 */
13608 static void
13609f_printf(argvars, rettv)
13610 typval_T *argvars;
13611 typval_T *rettv;
13612{
13613 rettv->v_type = VAR_STRING;
13614 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013615#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013616 {
13617 char_u buf[NUMBUFLEN];
13618 int len;
13619 char_u *s;
13620 int saved_did_emsg = did_emsg;
13621 char *fmt;
13622
13623 /* Get the required length, allocate the buffer and do it for real. */
13624 did_emsg = FALSE;
13625 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013626 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013627 if (!did_emsg)
13628 {
13629 s = alloc(len + 1);
13630 if (s != NULL)
13631 {
13632 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013633 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013634 }
13635 }
13636 did_emsg |= saved_did_emsg;
13637 }
13638#endif
13639}
13640
13641/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013642 * "pumvisible()" function
13643 */
13644/*ARGSUSED*/
13645 static void
13646f_pumvisible(argvars, rettv)
13647 typval_T *argvars;
13648 typval_T *rettv;
13649{
13650 rettv->vval.v_number = 0;
13651#ifdef FEAT_INS_EXPAND
13652 if (pum_visible())
13653 rettv->vval.v_number = 1;
13654#endif
13655}
13656
13657/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013658 * "range()" function
13659 */
13660 static void
13661f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013662 typval_T *argvars;
13663 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013664{
13665 long start;
13666 long end;
13667 long stride = 1;
13668 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013672 if (argvars[1].v_type == VAR_UNKNOWN)
13673 {
13674 end = start - 1;
13675 start = 0;
13676 }
13677 else
13678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013679 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013681 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013682 }
13683
13684 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 if (error)
13686 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013687 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013688 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013689 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013690 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013691 else
13692 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013693 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013694 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013695 if (list_append_number(rettv->vval.v_list,
13696 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013697 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013698 }
13699}
13700
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013701/*
13702 * "readfile()" function
13703 */
13704 static void
13705f_readfile(argvars, rettv)
13706 typval_T *argvars;
13707 typval_T *rettv;
13708{
13709 int binary = FALSE;
13710 char_u *fname;
13711 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013712 listitem_T *li;
13713#define FREAD_SIZE 200 /* optimized for text lines */
13714 char_u buf[FREAD_SIZE];
13715 int readlen; /* size of last fread() */
13716 int buflen; /* nr of valid chars in buf[] */
13717 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13718 int tolist; /* first byte in buf[] still to be put in list */
13719 int chop; /* how many CR to chop off */
13720 char_u *prev = NULL; /* previously read bytes, if any */
13721 int prevlen = 0; /* length of "prev" if not NULL */
13722 char_u *s;
13723 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013724 long maxline = MAXLNUM;
13725 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013726
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013727 if (argvars[1].v_type != VAR_UNKNOWN)
13728 {
13729 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13730 binary = TRUE;
13731 if (argvars[2].v_type != VAR_UNKNOWN)
13732 maxline = get_tv_number(&argvars[2]);
13733 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013734
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013735 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013736 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013737
13738 /* Always open the file in binary mode, library functions have a mind of
13739 * their own about CR-LF conversion. */
13740 fname = get_tv_string(&argvars[0]);
13741 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13742 {
13743 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13744 return;
13745 }
13746
13747 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013748 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013749 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013750 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013751 buflen = filtd + readlen;
13752 tolist = 0;
13753 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13754 {
13755 if (buf[filtd] == '\n' || readlen <= 0)
13756 {
13757 /* Only when in binary mode add an empty list item when the
13758 * last line ends in a '\n'. */
13759 if (!binary && readlen == 0 && filtd == 0)
13760 break;
13761
13762 /* Found end-of-line or end-of-file: add a text line to the
13763 * list. */
13764 chop = 0;
13765 if (!binary)
13766 while (filtd - chop - 1 >= tolist
13767 && buf[filtd - chop - 1] == '\r')
13768 ++chop;
13769 len = filtd - tolist - chop;
13770 if (prev == NULL)
13771 s = vim_strnsave(buf + tolist, len);
13772 else
13773 {
13774 s = alloc((unsigned)(prevlen + len + 1));
13775 if (s != NULL)
13776 {
13777 mch_memmove(s, prev, prevlen);
13778 vim_free(prev);
13779 prev = NULL;
13780 mch_memmove(s + prevlen, buf + tolist, len);
13781 s[prevlen + len] = NUL;
13782 }
13783 }
13784 tolist = filtd + 1;
13785
13786 li = listitem_alloc();
13787 if (li == NULL)
13788 {
13789 vim_free(s);
13790 break;
13791 }
13792 li->li_tv.v_type = VAR_STRING;
13793 li->li_tv.v_lock = 0;
13794 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013795 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013796
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013797 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013798 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013799 if (readlen <= 0)
13800 break;
13801 }
13802 else if (buf[filtd] == NUL)
13803 buf[filtd] = '\n';
13804 }
13805 if (readlen <= 0)
13806 break;
13807
13808 if (tolist == 0)
13809 {
13810 /* "buf" is full, need to move text to an allocated buffer */
13811 if (prev == NULL)
13812 {
13813 prev = vim_strnsave(buf, buflen);
13814 prevlen = buflen;
13815 }
13816 else
13817 {
13818 s = alloc((unsigned)(prevlen + buflen));
13819 if (s != NULL)
13820 {
13821 mch_memmove(s, prev, prevlen);
13822 mch_memmove(s + prevlen, buf, buflen);
13823 vim_free(prev);
13824 prev = s;
13825 prevlen += buflen;
13826 }
13827 }
13828 filtd = 0;
13829 }
13830 else
13831 {
13832 mch_memmove(buf, buf + tolist, buflen - tolist);
13833 filtd -= tolist;
13834 }
13835 }
13836
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013837 /*
13838 * For a negative line count use only the lines at the end of the file,
13839 * free the rest.
13840 */
13841 if (maxline < 0)
13842 while (cnt > -maxline)
13843 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013844 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013845 --cnt;
13846 }
13847
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013848 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013849 fclose(fd);
13850}
13851
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013852#if defined(FEAT_RELTIME)
13853static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13854
13855/*
13856 * Convert a List to proftime_T.
13857 * Return FAIL when there is something wrong.
13858 */
13859 static int
13860list2proftime(arg, tm)
13861 typval_T *arg;
13862 proftime_T *tm;
13863{
13864 long n1, n2;
13865 int error = FALSE;
13866
13867 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13868 || arg->vval.v_list->lv_len != 2)
13869 return FAIL;
13870 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13871 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13872# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013873 tm->HighPart = n1;
13874 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013875# else
13876 tm->tv_sec = n1;
13877 tm->tv_usec = n2;
13878# endif
13879 return error ? FAIL : OK;
13880}
13881#endif /* FEAT_RELTIME */
13882
13883/*
13884 * "reltime()" function
13885 */
13886 static void
13887f_reltime(argvars, rettv)
13888 typval_T *argvars;
13889 typval_T *rettv;
13890{
13891#ifdef FEAT_RELTIME
13892 proftime_T res;
13893 proftime_T start;
13894
13895 if (argvars[0].v_type == VAR_UNKNOWN)
13896 {
13897 /* No arguments: get current time. */
13898 profile_start(&res);
13899 }
13900 else if (argvars[1].v_type == VAR_UNKNOWN)
13901 {
13902 if (list2proftime(&argvars[0], &res) == FAIL)
13903 return;
13904 profile_end(&res);
13905 }
13906 else
13907 {
13908 /* Two arguments: compute the difference. */
13909 if (list2proftime(&argvars[0], &start) == FAIL
13910 || list2proftime(&argvars[1], &res) == FAIL)
13911 return;
13912 profile_sub(&res, &start);
13913 }
13914
13915 if (rettv_list_alloc(rettv) == OK)
13916 {
13917 long n1, n2;
13918
13919# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013920 n1 = res.HighPart;
13921 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013922# else
13923 n1 = res.tv_sec;
13924 n2 = res.tv_usec;
13925# endif
13926 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13927 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13928 }
13929#endif
13930}
13931
13932/*
13933 * "reltimestr()" function
13934 */
13935 static void
13936f_reltimestr(argvars, rettv)
13937 typval_T *argvars;
13938 typval_T *rettv;
13939{
13940#ifdef FEAT_RELTIME
13941 proftime_T tm;
13942#endif
13943
13944 rettv->v_type = VAR_STRING;
13945 rettv->vval.v_string = NULL;
13946#ifdef FEAT_RELTIME
13947 if (list2proftime(&argvars[0], &tm) == OK)
13948 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13949#endif
13950}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013951
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13953static void make_connection __ARGS((void));
13954static int check_connection __ARGS((void));
13955
13956 static void
13957make_connection()
13958{
13959 if (X_DISPLAY == NULL
13960# ifdef FEAT_GUI
13961 && !gui.in_use
13962# endif
13963 )
13964 {
13965 x_force_connect = TRUE;
13966 setup_term_clip();
13967 x_force_connect = FALSE;
13968 }
13969}
13970
13971 static int
13972check_connection()
13973{
13974 make_connection();
13975 if (X_DISPLAY == NULL)
13976 {
13977 EMSG(_("E240: No connection to Vim server"));
13978 return FAIL;
13979 }
13980 return OK;
13981}
13982#endif
13983
13984#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013985static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986
13987 static void
13988remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013989 typval_T *argvars;
13990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 int expr;
13992{
13993 char_u *server_name;
13994 char_u *keys;
13995 char_u *r = NULL;
13996 char_u buf[NUMBUFLEN];
13997# ifdef WIN32
13998 HWND w;
13999# else
14000 Window w;
14001# endif
14002
14003 if (check_restricted() || check_secure())
14004 return;
14005
14006# ifdef FEAT_X11
14007 if (check_connection() == FAIL)
14008 return;
14009# endif
14010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 server_name = get_tv_string_chk(&argvars[0]);
14012 if (server_name == NULL)
14013 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014014 keys = get_tv_string_buf(&argvars[1], buf);
14015# ifdef WIN32
14016 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14017# else
14018 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14019 < 0)
14020# endif
14021 {
14022 if (r != NULL)
14023 EMSG(r); /* sending worked but evaluation failed */
14024 else
14025 EMSG2(_("E241: Unable to send to %s"), server_name);
14026 return;
14027 }
14028
14029 rettv->vval.v_string = r;
14030
14031 if (argvars[2].v_type != VAR_UNKNOWN)
14032 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014033 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014034 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014036
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014037 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014038 v.di_tv.v_type = VAR_STRING;
14039 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 idvar = get_tv_string_chk(&argvars[2]);
14041 if (idvar != NULL)
14042 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014043 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044 }
14045}
14046#endif
14047
14048/*
14049 * "remote_expr()" function
14050 */
14051/*ARGSUSED*/
14052 static void
14053f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T *argvars;
14055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056{
14057 rettv->v_type = VAR_STRING;
14058 rettv->vval.v_string = NULL;
14059#ifdef FEAT_CLIENTSERVER
14060 remote_common(argvars, rettv, TRUE);
14061#endif
14062}
14063
14064/*
14065 * "remote_foreground()" function
14066 */
14067/*ARGSUSED*/
14068 static void
14069f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 typval_T *argvars;
14071 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014072{
14073 rettv->vval.v_number = 0;
14074#ifdef FEAT_CLIENTSERVER
14075# ifdef WIN32
14076 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 {
14078 char_u *server_name = get_tv_string_chk(&argvars[0]);
14079
14080 if (server_name != NULL)
14081 serverForeground(server_name);
14082 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014083# else
14084 /* Send a foreground() expression to the server. */
14085 argvars[1].v_type = VAR_STRING;
14086 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14087 argvars[2].v_type = VAR_UNKNOWN;
14088 remote_common(argvars, rettv, TRUE);
14089 vim_free(argvars[1].vval.v_string);
14090# endif
14091#endif
14092}
14093
14094/*ARGSUSED*/
14095 static void
14096f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014097 typval_T *argvars;
14098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014099{
14100#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014102 char_u *s = NULL;
14103# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014104 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107
14108 if (check_restricted() || check_secure())
14109 {
14110 rettv->vval.v_number = -1;
14111 return;
14112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 serverid = get_tv_string_chk(&argvars[0]);
14114 if (serverid == NULL)
14115 {
14116 rettv->vval.v_number = -1;
14117 return; /* type error; errmsg already given */
14118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014120 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121 if (n == 0)
14122 rettv->vval.v_number = -1;
14123 else
14124 {
14125 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14126 rettv->vval.v_number = (s != NULL);
14127 }
14128# else
14129 rettv->vval.v_number = 0;
14130 if (check_connection() == FAIL)
14131 return;
14132
14133 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014134 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135# endif
14136
14137 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 char_u *retvar;
14140
Bram Moolenaar33570922005-01-25 22:26:29 +000014141 v.di_tv.v_type = VAR_STRING;
14142 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 retvar = get_tv_string_chk(&argvars[1]);
14144 if (retvar != NULL)
14145 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 }
14148#else
14149 rettv->vval.v_number = -1;
14150#endif
14151}
14152
14153/*ARGSUSED*/
14154 static void
14155f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *argvars;
14157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158{
14159 char_u *r = NULL;
14160
14161#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 char_u *serverid = get_tv_string_chk(&argvars[0]);
14163
14164 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165 {
14166# ifdef WIN32
14167 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014168 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014169
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014170 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014171 if (n != 0)
14172 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14173 if (r == NULL)
14174# else
14175 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014177# endif
14178 EMSG(_("E277: Unable to read a server reply"));
14179 }
14180#endif
14181 rettv->v_type = VAR_STRING;
14182 rettv->vval.v_string = r;
14183}
14184
14185/*
14186 * "remote_send()" function
14187 */
14188/*ARGSUSED*/
14189 static void
14190f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193{
14194 rettv->v_type = VAR_STRING;
14195 rettv->vval.v_string = NULL;
14196#ifdef FEAT_CLIENTSERVER
14197 remote_common(argvars, rettv, FALSE);
14198#endif
14199}
14200
14201/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014202 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203 */
14204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014205f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *argvars;
14207 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014208{
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 list_T *l;
14210 listitem_T *item, *item2;
14211 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014212 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014213 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014214 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014215 dict_T *d;
14216 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014217
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014218 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014219 if (argvars[0].v_type == VAR_DICT)
14220 {
14221 if (argvars[2].v_type != VAR_UNKNOWN)
14222 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014223 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014224 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 key = get_tv_string_chk(&argvars[1]);
14227 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 di = dict_find(d, key, -1);
14230 if (di == NULL)
14231 EMSG2(_(e_dictkey), key);
14232 else
14233 {
14234 *rettv = di->di_tv;
14235 init_tv(&di->di_tv);
14236 dictitem_remove(d, di);
14237 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014239 }
14240 }
14241 else if (argvars[0].v_type != VAR_LIST)
14242 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014243 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014244 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 int error = FALSE;
14247
14248 idx = get_tv_number_chk(&argvars[1], &error);
14249 if (error)
14250 ; /* type error: do nothing, errmsg already given */
14251 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014252 EMSGN(_(e_listidx), idx);
14253 else
14254 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014255 if (argvars[2].v_type == VAR_UNKNOWN)
14256 {
14257 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014258 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014259 *rettv = item->li_tv;
14260 vim_free(item);
14261 }
14262 else
14263 {
14264 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 end = get_tv_number_chk(&argvars[2], &error);
14266 if (error)
14267 ; /* type error: do nothing */
14268 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014269 EMSGN(_(e_listidx), end);
14270 else
14271 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014272 int cnt = 0;
14273
14274 for (li = item; li != NULL; li = li->li_next)
14275 {
14276 ++cnt;
14277 if (li == item2)
14278 break;
14279 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014280 if (li == NULL) /* didn't find "item2" after "item" */
14281 EMSG(_(e_invrange));
14282 else
14283 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014284 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014285 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014286 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014287 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014288 l->lv_first = item;
14289 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014290 item->li_prev = NULL;
14291 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014292 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014293 }
14294 }
14295 }
14296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014297 }
14298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299}
14300
14301/*
14302 * "rename({from}, {to})" function
14303 */
14304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014305f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014306 typval_T *argvars;
14307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308{
14309 char_u buf[NUMBUFLEN];
14310
14311 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14315 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316}
14317
14318/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014319 * "repeat()" function
14320 */
14321/*ARGSUSED*/
14322 static void
14323f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014324 typval_T *argvars;
14325 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014326{
14327 char_u *p;
14328 int n;
14329 int slen;
14330 int len;
14331 char_u *r;
14332 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014333
14334 n = get_tv_number(&argvars[1]);
14335 if (argvars[0].v_type == VAR_LIST)
14336 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014337 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014338 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014339 if (list_extend(rettv->vval.v_list,
14340 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014341 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014342 }
14343 else
14344 {
14345 p = get_tv_string(&argvars[0]);
14346 rettv->v_type = VAR_STRING;
14347 rettv->vval.v_string = NULL;
14348
14349 slen = (int)STRLEN(p);
14350 len = slen * n;
14351 if (len <= 0)
14352 return;
14353
14354 r = alloc(len + 1);
14355 if (r != NULL)
14356 {
14357 for (i = 0; i < n; i++)
14358 mch_memmove(r + i * slen, p, (size_t)slen);
14359 r[len] = NUL;
14360 }
14361
14362 rettv->vval.v_string = r;
14363 }
14364}
14365
14366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 * "resolve()" function
14368 */
14369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373{
14374 char_u *p;
14375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377#ifdef FEAT_SHORTCUT
14378 {
14379 char_u *v = NULL;
14380
14381 v = mch_resolve_shortcut(p);
14382 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 }
14387#else
14388# ifdef HAVE_READLINK
14389 {
14390 char_u buf[MAXPATHL + 1];
14391 char_u *cpy;
14392 int len;
14393 char_u *remain = NULL;
14394 char_u *q;
14395 int is_relative_to_current = FALSE;
14396 int has_trailing_pathsep = FALSE;
14397 int limit = 100;
14398
14399 p = vim_strsave(p);
14400
14401 if (p[0] == '.' && (vim_ispathsep(p[1])
14402 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14403 is_relative_to_current = TRUE;
14404
14405 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014406 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 has_trailing_pathsep = TRUE;
14408
14409 q = getnextcomp(p);
14410 if (*q != NUL)
14411 {
14412 /* Separate the first path component in "p", and keep the
14413 * remainder (beginning with the path separator). */
14414 remain = vim_strsave(q - 1);
14415 q[-1] = NUL;
14416 }
14417
14418 for (;;)
14419 {
14420 for (;;)
14421 {
14422 len = readlink((char *)p, (char *)buf, MAXPATHL);
14423 if (len <= 0)
14424 break;
14425 buf[len] = NUL;
14426
14427 if (limit-- == 0)
14428 {
14429 vim_free(p);
14430 vim_free(remain);
14431 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 goto fail;
14434 }
14435
14436 /* Ensure that the result will have a trailing path separator
14437 * if the argument has one. */
14438 if (remain == NULL && has_trailing_pathsep)
14439 add_pathsep(buf);
14440
14441 /* Separate the first path component in the link value and
14442 * concatenate the remainders. */
14443 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14444 if (*q != NUL)
14445 {
14446 if (remain == NULL)
14447 remain = vim_strsave(q - 1);
14448 else
14449 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014450 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 if (cpy != NULL)
14452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 vim_free(remain);
14454 remain = cpy;
14455 }
14456 }
14457 q[-1] = NUL;
14458 }
14459
14460 q = gettail(p);
14461 if (q > p && *q == NUL)
14462 {
14463 /* Ignore trailing path separator. */
14464 q[-1] = NUL;
14465 q = gettail(p);
14466 }
14467 if (q > p && !mch_isFullName(buf))
14468 {
14469 /* symlink is relative to directory of argument */
14470 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14471 if (cpy != NULL)
14472 {
14473 STRCPY(cpy, p);
14474 STRCPY(gettail(cpy), buf);
14475 vim_free(p);
14476 p = cpy;
14477 }
14478 }
14479 else
14480 {
14481 vim_free(p);
14482 p = vim_strsave(buf);
14483 }
14484 }
14485
14486 if (remain == NULL)
14487 break;
14488
14489 /* Append the first path component of "remain" to "p". */
14490 q = getnextcomp(remain + 1);
14491 len = q - remain - (*q != NUL);
14492 cpy = vim_strnsave(p, STRLEN(p) + len);
14493 if (cpy != NULL)
14494 {
14495 STRNCAT(cpy, remain, len);
14496 vim_free(p);
14497 p = cpy;
14498 }
14499 /* Shorten "remain". */
14500 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014501 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 else
14503 {
14504 vim_free(remain);
14505 remain = NULL;
14506 }
14507 }
14508
14509 /* If the result is a relative path name, make it explicitly relative to
14510 * the current directory if and only if the argument had this form. */
14511 if (!vim_ispathsep(*p))
14512 {
14513 if (is_relative_to_current
14514 && *p != NUL
14515 && !(p[0] == '.'
14516 && (p[1] == NUL
14517 || vim_ispathsep(p[1])
14518 || (p[1] == '.'
14519 && (p[2] == NUL
14520 || vim_ispathsep(p[2]))))))
14521 {
14522 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014523 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 if (cpy != NULL)
14525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 vim_free(p);
14527 p = cpy;
14528 }
14529 }
14530 else if (!is_relative_to_current)
14531 {
14532 /* Strip leading "./". */
14533 q = p;
14534 while (q[0] == '.' && vim_ispathsep(q[1]))
14535 q += 2;
14536 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014537 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 }
14539 }
14540
14541 /* Ensure that the result will have no trailing path separator
14542 * if the argument had none. But keep "/" or "//". */
14543 if (!has_trailing_pathsep)
14544 {
14545 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014546 if (after_pathsep(p, q))
14547 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 }
14549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 }
14552# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554# endif
14555#endif
14556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558
14559#ifdef HAVE_READLINK
14560fail:
14561#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563}
14564
14565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 */
14568 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014569f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014570 typval_T *argvars;
14571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572{
Bram Moolenaar33570922005-01-25 22:26:29 +000014573 list_T *l;
14574 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 rettv->vval.v_number = 0;
14577 if (argvars[0].v_type != VAR_LIST)
14578 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014579 else if ((l = argvars[0].vval.v_list) != NULL
14580 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 {
14582 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014583 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014584 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014585 while (li != NULL)
14586 {
14587 ni = li->li_prev;
14588 list_append(l, li);
14589 li = ni;
14590 }
14591 rettv->vval.v_list = l;
14592 rettv->v_type = VAR_LIST;
14593 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014594 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596}
14597
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014598#define SP_NOMOVE 0x01 /* don't move cursor */
14599#define SP_REPEAT 0x02 /* repeat to find outer pair */
14600#define SP_RETCOUNT 0x04 /* return matchcount */
14601#define SP_SETPCMARK 0x08 /* set previous context mark */
14602#define SP_START 0x10 /* accept match at start position */
14603#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14604#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014605
Bram Moolenaar33570922005-01-25 22:26:29 +000014606static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607
14608/*
14609 * Get flags for a search function.
14610 * Possibly sets "p_ws".
14611 * Returns BACKWARD, FORWARD or zero (for an error).
14612 */
14613 static int
14614get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014615 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616 int *flagsp;
14617{
14618 int dir = FORWARD;
14619 char_u *flags;
14620 char_u nbuf[NUMBUFLEN];
14621 int mask;
14622
14623 if (varp->v_type != VAR_UNKNOWN)
14624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 flags = get_tv_string_buf_chk(varp, nbuf);
14626 if (flags == NULL)
14627 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 while (*flags != NUL)
14629 {
14630 switch (*flags)
14631 {
14632 case 'b': dir = BACKWARD; break;
14633 case 'w': p_ws = TRUE; break;
14634 case 'W': p_ws = FALSE; break;
14635 default: mask = 0;
14636 if (flagsp != NULL)
14637 switch (*flags)
14638 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014639 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014640 case 'e': mask = SP_END; break;
14641 case 'm': mask = SP_RETCOUNT; break;
14642 case 'n': mask = SP_NOMOVE; break;
14643 case 'p': mask = SP_SUBPAT; break;
14644 case 'r': mask = SP_REPEAT; break;
14645 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014646 }
14647 if (mask == 0)
14648 {
14649 EMSG2(_(e_invarg2), flags);
14650 dir = 0;
14651 }
14652 else
14653 *flagsp |= mask;
14654 }
14655 if (dir == 0)
14656 break;
14657 ++flags;
14658 }
14659 }
14660 return dir;
14661}
14662
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014664 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014666 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014667search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014669 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014670 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014672 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 char_u *pat;
14674 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014675 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676 int save_p_ws = p_ws;
14677 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014678 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014679 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014680 proftime_T tm;
14681#ifdef FEAT_RELTIME
14682 long time_limit = 0;
14683#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014684 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014685 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014688 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014689 if (dir == 0)
14690 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014691 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014692 if (flags & SP_START)
14693 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014694 if (flags & SP_END)
14695 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014696
Bram Moolenaar76929292008-01-06 19:07:36 +000014697 /* Optional arguments: line number to stop searching and timeout. */
14698 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014699 {
14700 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14701 if (lnum_stop < 0)
14702 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014703#ifdef FEAT_RELTIME
14704 if (argvars[3].v_type != VAR_UNKNOWN)
14705 {
14706 time_limit = get_tv_number_chk(&argvars[3], NULL);
14707 if (time_limit < 0)
14708 goto theend;
14709 }
14710#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014711 }
14712
Bram Moolenaar76929292008-01-06 19:07:36 +000014713#ifdef FEAT_RELTIME
14714 /* Set the time limit, if there is one. */
14715 profile_setlimit(time_limit, &tm);
14716#endif
14717
Bram Moolenaar231334e2005-07-25 20:46:57 +000014718 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014719 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014720 * Check to make sure only those flags are set.
14721 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14722 * flags cannot be set. Check for that condition also.
14723 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014724 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014725 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014728 goto theend;
14729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014731 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014732 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014733 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014734 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014736 if (flags & SP_SUBPAT)
14737 retval = subpatnum;
14738 else
14739 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014740 if (flags & SP_SETPCMARK)
14741 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014743 if (match_pos != NULL)
14744 {
14745 /* Store the match cursor position */
14746 match_pos->lnum = pos.lnum;
14747 match_pos->col = pos.col + 1;
14748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 /* "/$" will put the cursor after the end of the line, may need to
14750 * correct that here */
14751 check_cursor();
14752 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014753
14754 /* If 'n' flag is used: restore cursor position. */
14755 if (flags & SP_NOMOVE)
14756 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014757 else
14758 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014759theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014761
14762 return retval;
14763}
14764
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014765#ifdef FEAT_FLOAT
14766/*
14767 * "round({float})" function
14768 */
14769 static void
14770f_round(argvars, rettv)
14771 typval_T *argvars;
14772 typval_T *rettv;
14773{
14774 float_T f;
14775
14776 rettv->v_type = VAR_FLOAT;
14777 if (get_float_arg(argvars, &f) == OK)
14778 /* round() is not in C90, use ceil() or floor() instead. */
14779 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14780 else
14781 rettv->vval.v_float = 0.0;
14782}
14783#endif
14784
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014785/*
14786 * "search()" function
14787 */
14788 static void
14789f_search(argvars, rettv)
14790 typval_T *argvars;
14791 typval_T *rettv;
14792{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014793 int flags = 0;
14794
14795 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796}
14797
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014799 * "searchdecl()" function
14800 */
14801 static void
14802f_searchdecl(argvars, rettv)
14803 typval_T *argvars;
14804 typval_T *rettv;
14805{
14806 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014807 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014808 int error = FALSE;
14809 char_u *name;
14810
14811 rettv->vval.v_number = 1; /* default: FAIL */
14812
14813 name = get_tv_string_chk(&argvars[0]);
14814 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014815 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014816 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014817 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14818 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14819 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014820 if (!error && name != NULL)
14821 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014822 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014823}
14824
14825/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014826 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014828 static int
14829searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014830 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014831 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832{
14833 char_u *spat, *mpat, *epat;
14834 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 int dir;
14837 int flags = 0;
14838 char_u nbuf1[NUMBUFLEN];
14839 char_u nbuf2[NUMBUFLEN];
14840 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014841 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014842 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014843 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014846 spat = get_tv_string_chk(&argvars[0]);
14847 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14848 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14849 if (spat == NULL || mpat == NULL || epat == NULL)
14850 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 /* Handle the optional fourth argument: flags */
14853 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014854 if (dir == 0)
14855 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014856
14857 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014858 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14859 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014860 if ((flags & (SP_END | SP_SUBPAT)) != 0
14861 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014862 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014863 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014864 goto theend;
14865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014867 /* Using 'r' implies 'W', otherwise it doesn't work. */
14868 if (flags & SP_REPEAT)
14869 p_ws = FALSE;
14870
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014871 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014872 if (argvars[3].v_type == VAR_UNKNOWN
14873 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 skip = (char_u *)"";
14875 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014878 if (argvars[5].v_type != VAR_UNKNOWN)
14879 {
14880 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14881 if (lnum_stop < 0)
14882 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014883#ifdef FEAT_RELTIME
14884 if (argvars[6].v_type != VAR_UNKNOWN)
14885 {
14886 time_limit = get_tv_number_chk(&argvars[6], NULL);
14887 if (time_limit < 0)
14888 goto theend;
14889 }
14890#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014891 }
14892 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014893 if (skip == NULL)
14894 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014896 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014897 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014898
14899theend:
14900 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014901
14902 return retval;
14903}
14904
14905/*
14906 * "searchpair()" function
14907 */
14908 static void
14909f_searchpair(argvars, rettv)
14910 typval_T *argvars;
14911 typval_T *rettv;
14912{
14913 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14914}
14915
14916/*
14917 * "searchpairpos()" function
14918 */
14919 static void
14920f_searchpairpos(argvars, rettv)
14921 typval_T *argvars;
14922 typval_T *rettv;
14923{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014924 pos_T match_pos;
14925 int lnum = 0;
14926 int col = 0;
14927
14928 rettv->vval.v_number = 0;
14929
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014930 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014931 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014932
14933 if (searchpair_cmn(argvars, &match_pos) > 0)
14934 {
14935 lnum = match_pos.lnum;
14936 col = match_pos.col;
14937 }
14938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014939 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14940 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014941}
14942
14943/*
14944 * Search for a start/middle/end thing.
14945 * Used by searchpair(), see its documentation for the details.
14946 * Returns 0 or -1 for no match,
14947 */
14948 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014949do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14950 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014951 char_u *spat; /* start pattern */
14952 char_u *mpat; /* middle pattern */
14953 char_u *epat; /* end pattern */
14954 int dir; /* BACKWARD or FORWARD */
14955 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014956 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014957 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014958 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014959 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014960{
14961 char_u *save_cpo;
14962 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14963 long retval = 0;
14964 pos_T pos;
14965 pos_T firstpos;
14966 pos_T foundpos;
14967 pos_T save_cursor;
14968 pos_T save_pos;
14969 int n;
14970 int r;
14971 int nest = 1;
14972 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014973 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014974 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014975
14976 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14977 save_cpo = p_cpo;
14978 p_cpo = (char_u *)"";
14979
Bram Moolenaar76929292008-01-06 19:07:36 +000014980#ifdef FEAT_RELTIME
14981 /* Set the time limit, if there is one. */
14982 profile_setlimit(time_limit, &tm);
14983#endif
14984
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014985 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14986 * start/middle/end (pat3, for the top pair). */
14987 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14988 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14989 if (pat2 == NULL || pat3 == NULL)
14990 goto theend;
14991 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14992 if (*mpat == NUL)
14993 STRCPY(pat3, pat2);
14994 else
14995 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14996 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014997 if (flags & SP_START)
14998 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014999
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 save_cursor = curwin->w_cursor;
15001 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015002 clearpos(&firstpos);
15003 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 pat = pat3;
15005 for (;;)
15006 {
15007 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015008 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15010 /* didn't find it or found the first match again: FAIL */
15011 break;
15012
15013 if (firstpos.lnum == 0)
15014 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015015 if (equalpos(pos, foundpos))
15016 {
15017 /* Found the same position again. Can happen with a pattern that
15018 * has "\zs" at the end and searching backwards. Advance one
15019 * character and try again. */
15020 if (dir == BACKWARD)
15021 decl(&pos);
15022 else
15023 incl(&pos);
15024 }
15025 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015027 /* clear the start flag to avoid getting stuck here */
15028 options &= ~SEARCH_START;
15029
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 /* If the skip pattern matches, ignore this match. */
15031 if (*skip != NUL)
15032 {
15033 save_pos = curwin->w_cursor;
15034 curwin->w_cursor = pos;
15035 r = eval_to_bool(skip, &err, NULL, FALSE);
15036 curwin->w_cursor = save_pos;
15037 if (err)
15038 {
15039 /* Evaluating {skip} caused an error, break here. */
15040 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015041 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 break;
15043 }
15044 if (r)
15045 continue;
15046 }
15047
15048 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15049 {
15050 /* Found end when searching backwards or start when searching
15051 * forward: nested pair. */
15052 ++nest;
15053 pat = pat2; /* nested, don't search for middle */
15054 }
15055 else
15056 {
15057 /* Found end when searching forward or start when searching
15058 * backward: end of (nested) pair; or found middle in outer pair. */
15059 if (--nest == 1)
15060 pat = pat3; /* outer level, search for middle */
15061 }
15062
15063 if (nest == 0)
15064 {
15065 /* Found the match: return matchcount or line number. */
15066 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015067 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015069 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015070 if (flags & SP_SETPCMARK)
15071 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072 curwin->w_cursor = pos;
15073 if (!(flags & SP_REPEAT))
15074 break;
15075 nest = 1; /* search for next unmatched */
15076 }
15077 }
15078
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015079 if (match_pos != NULL)
15080 {
15081 /* Store the match cursor position */
15082 match_pos->lnum = curwin->w_cursor.lnum;
15083 match_pos->col = curwin->w_cursor.col + 1;
15084 }
15085
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015087 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 curwin->w_cursor = save_cursor;
15089
15090theend:
15091 vim_free(pat2);
15092 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015094
15095 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096}
15097
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015098/*
15099 * "searchpos()" function
15100 */
15101 static void
15102f_searchpos(argvars, rettv)
15103 typval_T *argvars;
15104 typval_T *rettv;
15105{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015106 pos_T match_pos;
15107 int lnum = 0;
15108 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015109 int n;
15110 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015111
15112 rettv->vval.v_number = 0;
15113
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015114 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015115 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015116
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015117 n = search_cmn(argvars, &match_pos, &flags);
15118 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015119 {
15120 lnum = match_pos.lnum;
15121 col = match_pos.col;
15122 }
15123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015124 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15125 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015126 if (flags & SP_SUBPAT)
15127 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015128}
15129
15130
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131/*ARGSUSED*/
15132 static void
15133f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015134 typval_T *argvars;
15135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015137#ifdef FEAT_CLIENTSERVER
15138 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 char_u *server = get_tv_string_chk(&argvars[0]);
15140 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 if (server == NULL || reply == NULL)
15144 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 if (check_restricted() || check_secure())
15146 return;
15147# ifdef FEAT_X11
15148 if (check_connection() == FAIL)
15149 return;
15150# endif
15151
15152 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154 EMSG(_("E258: Unable to send to client"));
15155 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157 rettv->vval.v_number = 0;
15158#else
15159 rettv->vval.v_number = -1;
15160#endif
15161}
15162
15163/*ARGSUSED*/
15164 static void
15165f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015166 typval_T *argvars;
15167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168{
15169 char_u *r = NULL;
15170
15171#ifdef FEAT_CLIENTSERVER
15172# ifdef WIN32
15173 r = serverGetVimNames();
15174# else
15175 make_connection();
15176 if (X_DISPLAY != NULL)
15177 r = serverGetVimNames(X_DISPLAY);
15178# endif
15179#endif
15180 rettv->v_type = VAR_STRING;
15181 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182}
15183
15184/*
15185 * "setbufvar()" function
15186 */
15187/*ARGSUSED*/
15188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015189f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 typval_T *argvars;
15191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192{
15193 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015196 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 char_u nbuf[NUMBUFLEN];
15198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015199 rettv->vval.v_number = 0;
15200
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 if (check_restricted() || check_secure())
15202 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15204 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 varp = &argvars[2];
15207
15208 if (buf != NULL && varname != NULL && varp != NULL)
15209 {
15210 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212
15213 if (*varname == '&')
15214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 long numval;
15216 char_u *strval;
15217 int error = FALSE;
15218
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 numval = get_tv_number_chk(varp, &error);
15221 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 if (!error && strval != NULL)
15223 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224 }
15225 else
15226 {
15227 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15228 if (bufvarname != NULL)
15229 {
15230 STRCPY(bufvarname, "b:");
15231 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015232 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 vim_free(bufvarname);
15234 }
15235 }
15236
15237 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240}
15241
15242/*
15243 * "setcmdpos()" function
15244 */
15245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015246f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015247 typval_T *argvars;
15248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 int pos = (int)get_tv_number(&argvars[0]) - 1;
15251
15252 if (pos >= 0)
15253 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254}
15255
15256/*
15257 * "setline()" function
15258 */
15259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015260f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015261 typval_T *argvars;
15262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263{
15264 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015265 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015266 list_T *l = NULL;
15267 listitem_T *li = NULL;
15268 long added = 0;
15269 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015271 lnum = get_tv_lnum(&argvars[0]);
15272 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015274 l = argvars[1].vval.v_list;
15275 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015277 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015279
15280 rettv->vval.v_number = 0; /* OK */
15281 for (;;)
15282 {
15283 if (l != NULL)
15284 {
15285 /* list argument, get next string */
15286 if (li == NULL)
15287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015288 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015289 li = li->li_next;
15290 }
15291
15292 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015293 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015294 break;
15295 if (lnum <= curbuf->b_ml.ml_line_count)
15296 {
15297 /* existing line, replace it */
15298 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15299 {
15300 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015301 if (lnum == curwin->w_cursor.lnum)
15302 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015303 rettv->vval.v_number = 0; /* OK */
15304 }
15305 }
15306 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15307 {
15308 /* lnum is one past the last line, append the line */
15309 ++added;
15310 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15311 rettv->vval.v_number = 0; /* OK */
15312 }
15313
15314 if (l == NULL) /* only one string argument */
15315 break;
15316 ++lnum;
15317 }
15318
15319 if (added > 0)
15320 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321}
15322
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015323static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15324
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015326 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015327 */
15328/*ARGSUSED*/
15329 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015330set_qf_ll_list(wp, list_arg, action_arg, rettv)
15331 win_T *wp;
15332 typval_T *list_arg;
15333 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015334 typval_T *rettv;
15335{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015336#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015337 char_u *act;
15338 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015339#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015340
Bram Moolenaar2641f772005-03-25 21:58:17 +000015341 rettv->vval.v_number = -1;
15342
15343#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015344 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015345 EMSG(_(e_listreq));
15346 else
15347 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015348 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015349
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015350 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015351 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015352 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015353 if (act == NULL)
15354 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015355 if (*act == 'a' || *act == 'r')
15356 action = *act;
15357 }
15358
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015359 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015360 rettv->vval.v_number = 0;
15361 }
15362#endif
15363}
15364
15365/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015366 * "setloclist()" function
15367 */
15368/*ARGSUSED*/
15369 static void
15370f_setloclist(argvars, rettv)
15371 typval_T *argvars;
15372 typval_T *rettv;
15373{
15374 win_T *win;
15375
15376 rettv->vval.v_number = -1;
15377
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015378 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015379 if (win != NULL)
15380 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15381}
15382
15383/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015384 * "setmatches()" function
15385 */
15386 static void
15387f_setmatches(argvars, rettv)
15388 typval_T *argvars;
15389 typval_T *rettv;
15390{
15391#ifdef FEAT_SEARCH_EXTRA
15392 list_T *l;
15393 listitem_T *li;
15394 dict_T *d;
15395
15396 rettv->vval.v_number = -1;
15397 if (argvars[0].v_type != VAR_LIST)
15398 {
15399 EMSG(_(e_listreq));
15400 return;
15401 }
15402 if ((l = argvars[0].vval.v_list) != NULL)
15403 {
15404
15405 /* To some extent make sure that we are dealing with a list from
15406 * "getmatches()". */
15407 li = l->lv_first;
15408 while (li != NULL)
15409 {
15410 if (li->li_tv.v_type != VAR_DICT
15411 || (d = li->li_tv.vval.v_dict) == NULL)
15412 {
15413 EMSG(_(e_invarg));
15414 return;
15415 }
15416 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15417 && dict_find(d, (char_u *)"pattern", -1) != NULL
15418 && dict_find(d, (char_u *)"priority", -1) != NULL
15419 && dict_find(d, (char_u *)"id", -1) != NULL))
15420 {
15421 EMSG(_(e_invarg));
15422 return;
15423 }
15424 li = li->li_next;
15425 }
15426
15427 clear_matches(curwin);
15428 li = l->lv_first;
15429 while (li != NULL)
15430 {
15431 d = li->li_tv.vval.v_dict;
15432 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15433 get_dict_string(d, (char_u *)"pattern", FALSE),
15434 (int)get_dict_number(d, (char_u *)"priority"),
15435 (int)get_dict_number(d, (char_u *)"id"));
15436 li = li->li_next;
15437 }
15438 rettv->vval.v_number = 0;
15439 }
15440#endif
15441}
15442
15443/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015444 * "setpos()" function
15445 */
15446/*ARGSUSED*/
15447 static void
15448f_setpos(argvars, rettv)
15449 typval_T *argvars;
15450 typval_T *rettv;
15451{
15452 pos_T pos;
15453 int fnum;
15454 char_u *name;
15455
Bram Moolenaar08250432008-02-13 11:42:46 +000015456 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015457 name = get_tv_string_chk(argvars);
15458 if (name != NULL)
15459 {
15460 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15461 {
15462 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015463 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015464 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015465 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015466 if (fnum == curbuf->b_fnum)
15467 {
15468 curwin->w_cursor = pos;
15469 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015470 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015471 }
15472 else
15473 EMSG(_(e_invarg));
15474 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015475 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15476 {
15477 /* set mark */
15478 if (setmark_pos(name[1], &pos, fnum) == OK)
15479 rettv->vval.v_number = 0;
15480 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015481 else
15482 EMSG(_(e_invarg));
15483 }
15484 }
15485}
15486
15487/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015488 * "setqflist()" function
15489 */
15490/*ARGSUSED*/
15491 static void
15492f_setqflist(argvars, rettv)
15493 typval_T *argvars;
15494 typval_T *rettv;
15495{
15496 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15497}
15498
15499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 * "setreg()" function
15501 */
15502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015504 typval_T *argvars;
15505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506{
15507 int regname;
15508 char_u *strregname;
15509 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015510 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 int append;
15512 char_u yank_type;
15513 long block_len;
15514
15515 block_len = -1;
15516 yank_type = MAUTO;
15517 append = FALSE;
15518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015519 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 if (strregname == NULL)
15523 return; /* type error; errmsg already given */
15524 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 if (regname == 0 || regname == '@')
15526 regname = '"';
15527 else if (regname == '=')
15528 return;
15529
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015530 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015532 stropt = get_tv_string_chk(&argvars[2]);
15533 if (stropt == NULL)
15534 return; /* type error */
15535 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 switch (*stropt)
15537 {
15538 case 'a': case 'A': /* append */
15539 append = TRUE;
15540 break;
15541 case 'v': case 'c': /* character-wise selection */
15542 yank_type = MCHAR;
15543 break;
15544 case 'V': case 'l': /* line-wise selection */
15545 yank_type = MLINE;
15546 break;
15547#ifdef FEAT_VISUAL
15548 case 'b': case Ctrl_V: /* block-wise selection */
15549 yank_type = MBLOCK;
15550 if (VIM_ISDIGIT(stropt[1]))
15551 {
15552 ++stropt;
15553 block_len = getdigits(&stropt) - 1;
15554 --stropt;
15555 }
15556 break;
15557#endif
15558 }
15559 }
15560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015561 strval = get_tv_string_chk(&argvars[1]);
15562 if (strval != NULL)
15563 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566}
15567
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015568/*
15569 * "settabwinvar()" function
15570 */
15571 static void
15572f_settabwinvar(argvars, rettv)
15573 typval_T *argvars;
15574 typval_T *rettv;
15575{
15576 setwinvar(argvars, rettv, 1);
15577}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578
15579/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015580 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015584 typval_T *argvars;
15585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015587 setwinvar(argvars, rettv, 0);
15588}
15589
15590/*
15591 * "setwinvar()" and "settabwinvar()" functions
15592 */
15593 static void
15594setwinvar(argvars, rettv, off)
15595 typval_T *argvars;
15596 typval_T *rettv;
15597 int off;
15598{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 win_T *win;
15600#ifdef FEAT_WINDOWS
15601 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015602 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603#endif
15604 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015607 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 rettv->vval.v_number = 0;
15610
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 if (check_restricted() || check_secure())
15612 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015613
15614#ifdef FEAT_WINDOWS
15615 if (off == 1)
15616 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15617 else
15618 tp = curtab;
15619#endif
15620 win = find_win_by_nr(&argvars[off], tp);
15621 varname = get_tv_string_chk(&argvars[off + 1]);
15622 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623
15624 if (win != NULL && varname != NULL && varp != NULL)
15625 {
15626#ifdef FEAT_WINDOWS
15627 /* set curwin to be our win, temporarily */
15628 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015629 save_curtab = curtab;
15630 goto_tabpage_tp(tp);
15631 if (!win_valid(win))
15632 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 curwin = win;
15634 curbuf = curwin->w_buffer;
15635#endif
15636
15637 if (*varname == '&')
15638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 long numval;
15640 char_u *strval;
15641 int error = FALSE;
15642
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015644 numval = get_tv_number_chk(varp, &error);
15645 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 if (!error && strval != NULL)
15647 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 }
15649 else
15650 {
15651 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15652 if (winvarname != NULL)
15653 {
15654 STRCPY(winvarname, "w:");
15655 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015656 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 vim_free(winvarname);
15658 }
15659 }
15660
15661#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015662 /* Restore current tabpage and window, if still valid (autocomands can
15663 * make them invalid). */
15664 if (valid_tabpage(save_curtab))
15665 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 if (win_valid(save_curwin))
15667 {
15668 curwin = save_curwin;
15669 curbuf = curwin->w_buffer;
15670 }
15671#endif
15672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673}
15674
15675/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015676 * "shellescape({string})" function
15677 */
15678 static void
15679f_shellescape(argvars, rettv)
15680 typval_T *argvars;
15681 typval_T *rettv;
15682{
15683 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15684 rettv->v_type = VAR_STRING;
15685}
15686
15687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 */
15690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015691f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 typval_T *argvars;
15693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697 p = get_tv_string(&argvars[0]);
15698 rettv->vval.v_string = vim_strsave(p);
15699 simplify_filename(rettv->vval.v_string); /* simplify in place */
15700 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701}
15702
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015703#ifdef FEAT_FLOAT
15704/*
15705 * "sin()" function
15706 */
15707 static void
15708f_sin(argvars, rettv)
15709 typval_T *argvars;
15710 typval_T *rettv;
15711{
15712 float_T f;
15713
15714 rettv->v_type = VAR_FLOAT;
15715 if (get_float_arg(argvars, &f) == OK)
15716 rettv->vval.v_float = sin(f);
15717 else
15718 rettv->vval.v_float = 0.0;
15719}
15720#endif
15721
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722static int
15723#ifdef __BORLANDC__
15724 _RTLENTRYF
15725#endif
15726 item_compare __ARGS((const void *s1, const void *s2));
15727static int
15728#ifdef __BORLANDC__
15729 _RTLENTRYF
15730#endif
15731 item_compare2 __ARGS((const void *s1, const void *s2));
15732
15733static int item_compare_ic;
15734static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015736#define ITEM_COMPARE_FAIL 999
15737
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015741 static int
15742#ifdef __BORLANDC__
15743_RTLENTRYF
15744#endif
15745item_compare(s1, s2)
15746 const void *s1;
15747 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 char_u *p1, *p2;
15750 char_u *tofree1, *tofree2;
15751 int res;
15752 char_u numbuf1[NUMBUFLEN];
15753 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015755 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15756 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015757 if (p1 == NULL)
15758 p1 = (char_u *)"";
15759 if (p2 == NULL)
15760 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761 if (item_compare_ic)
15762 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015764 res = STRCMP(p1, p2);
15765 vim_free(tofree1);
15766 vim_free(tofree2);
15767 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768}
15769
15770 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015771#ifdef __BORLANDC__
15772_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774item_compare2(s1, s2)
15775 const void *s1;
15776 const void *s2;
15777{
15778 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015779 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015780 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015783 /* shortcut after failure in previous call; compare all items equal */
15784 if (item_compare_func_err)
15785 return 0;
15786
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015787 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15788 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015789 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15790 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791
15792 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015793 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015794 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795 clear_tv(&argv[0]);
15796 clear_tv(&argv[1]);
15797
15798 if (res == FAIL)
15799 res = ITEM_COMPARE_FAIL;
15800 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 /* return value has wrong type */
15802 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15803 if (item_compare_func_err)
15804 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805 clear_tv(&rettv);
15806 return res;
15807}
15808
15809/*
15810 * "sort({list})" function
15811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 typval_T *argvars;
15815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816{
Bram Moolenaar33570922005-01-25 22:26:29 +000015817 list_T *l;
15818 listitem_T *li;
15819 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015820 long len;
15821 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaar0d660222005-01-07 21:51:51 +000015823 rettv->vval.v_number = 0;
15824 if (argvars[0].v_type != VAR_LIST)
15825 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 else
15827 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015828 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015829 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015830 return;
15831 rettv->vval.v_list = l;
15832 rettv->v_type = VAR_LIST;
15833 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835 len = list_len(l);
15836 if (len <= 1)
15837 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 item_compare_ic = FALSE;
15840 item_compare_func = NULL;
15841 if (argvars[1].v_type != VAR_UNKNOWN)
15842 {
15843 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015844 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845 else
15846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 int error = FALSE;
15848
15849 i = get_tv_number_chk(&argvars[1], &error);
15850 if (error)
15851 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852 if (i == 1)
15853 item_compare_ic = TRUE;
15854 else
15855 item_compare_func = get_tv_string(&argvars[1]);
15856 }
15857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015860 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 if (ptrs == NULL)
15862 return;
15863 i = 0;
15864 for (li = l->lv_first; li != NULL; li = li->li_next)
15865 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015867 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868 /* test the compare function */
15869 if (item_compare_func != NULL
15870 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15871 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015872 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874 {
15875 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877 item_compare_func == NULL ? item_compare : item_compare2);
15878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 if (!item_compare_func_err)
15880 {
15881 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015882 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015883 l->lv_len = 0;
15884 for (i = 0; i < len; ++i)
15885 list_append(l, ptrs[i]);
15886 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015887 }
15888
15889 vim_free(ptrs);
15890 }
15891}
15892
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015893/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015894 * "soundfold({word})" function
15895 */
15896 static void
15897f_soundfold(argvars, rettv)
15898 typval_T *argvars;
15899 typval_T *rettv;
15900{
15901 char_u *s;
15902
15903 rettv->v_type = VAR_STRING;
15904 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015905#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015906 rettv->vval.v_string = eval_soundfold(s);
15907#else
15908 rettv->vval.v_string = vim_strsave(s);
15909#endif
15910}
15911
15912/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015913 * "spellbadword()" function
15914 */
15915/* ARGSUSED */
15916 static void
15917f_spellbadword(argvars, rettv)
15918 typval_T *argvars;
15919 typval_T *rettv;
15920{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015921 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015922 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015923 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015924
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015925 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015926 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015927
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015928#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015929 if (argvars[0].v_type == VAR_UNKNOWN)
15930 {
15931 /* Find the start and length of the badly spelled word. */
15932 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15933 if (len != 0)
15934 word = ml_get_cursor();
15935 }
15936 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15937 {
15938 char_u *str = get_tv_string_chk(&argvars[0]);
15939 int capcol = -1;
15940
15941 if (str != NULL)
15942 {
15943 /* Check the argument for spelling. */
15944 while (*str != NUL)
15945 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015946 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015947 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015948 {
15949 word = str;
15950 break;
15951 }
15952 str += len;
15953 }
15954 }
15955 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015956#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015957
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 list_append_string(rettv->vval.v_list, word, len);
15959 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015960 attr == HLF_SPB ? "bad" :
15961 attr == HLF_SPR ? "rare" :
15962 attr == HLF_SPL ? "local" :
15963 attr == HLF_SPC ? "caps" :
15964 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015965}
15966
15967/*
15968 * "spellsuggest()" function
15969 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015970/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015971 static void
15972f_spellsuggest(argvars, rettv)
15973 typval_T *argvars;
15974 typval_T *rettv;
15975{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015976#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015977 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015978 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015979 int maxcount;
15980 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015981 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015982 listitem_T *li;
15983 int need_capital = FALSE;
15984#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015985
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015986 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015987 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015988
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015989#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015990 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15991 {
15992 str = get_tv_string(&argvars[0]);
15993 if (argvars[1].v_type != VAR_UNKNOWN)
15994 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015995 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015996 if (maxcount <= 0)
15997 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015998 if (argvars[2].v_type != VAR_UNKNOWN)
15999 {
16000 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16001 if (typeerr)
16002 return;
16003 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016004 }
16005 else
16006 maxcount = 25;
16007
Bram Moolenaar4770d092006-01-12 23:22:24 +000016008 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016009
16010 for (i = 0; i < ga.ga_len; ++i)
16011 {
16012 str = ((char_u **)ga.ga_data)[i];
16013
16014 li = listitem_alloc();
16015 if (li == NULL)
16016 vim_free(str);
16017 else
16018 {
16019 li->li_tv.v_type = VAR_STRING;
16020 li->li_tv.v_lock = 0;
16021 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016022 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016023 }
16024 }
16025 ga_clear(&ga);
16026 }
16027#endif
16028}
16029
Bram Moolenaar0d660222005-01-07 21:51:51 +000016030 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016031f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 typval_T *argvars;
16033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016034{
16035 char_u *str;
16036 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016037 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016038 regmatch_T regmatch;
16039 char_u patbuf[NUMBUFLEN];
16040 char_u *save_cpo;
16041 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016042 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016043 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016044 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016045
16046 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16047 save_cpo = p_cpo;
16048 p_cpo = (char_u *)"";
16049
16050 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016051 if (argvars[1].v_type != VAR_UNKNOWN)
16052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016053 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16054 if (pat == NULL)
16055 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016057 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016058 }
16059 if (pat == NULL || *pat == NUL)
16060 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016062 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016064 if (typeerr)
16065 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066
Bram Moolenaar0d660222005-01-07 21:51:51 +000016067 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16068 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016071 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016072 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016073 if (*str == NUL)
16074 match = FALSE; /* empty item at the end */
16075 else
16076 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016077 if (match)
16078 end = regmatch.startp[0];
16079 else
16080 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016081 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16082 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016084 if (list_append_string(rettv->vval.v_list, str,
16085 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016086 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 }
16088 if (!match)
16089 break;
16090 /* Advance to just after the match. */
16091 if (regmatch.endp[0] > str)
16092 col = 0;
16093 else
16094 {
16095 /* Don't get stuck at the same match. */
16096#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016097 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098#else
16099 col = 1;
16100#endif
16101 }
16102 str = regmatch.endp[0];
16103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104
Bram Moolenaar0d660222005-01-07 21:51:51 +000016105 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107
Bram Moolenaar0d660222005-01-07 21:51:51 +000016108 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109}
16110
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016111#ifdef FEAT_FLOAT
16112/*
16113 * "sqrt()" function
16114 */
16115 static void
16116f_sqrt(argvars, rettv)
16117 typval_T *argvars;
16118 typval_T *rettv;
16119{
16120 float_T f;
16121
16122 rettv->v_type = VAR_FLOAT;
16123 if (get_float_arg(argvars, &f) == OK)
16124 rettv->vval.v_float = sqrt(f);
16125 else
16126 rettv->vval.v_float = 0.0;
16127}
16128
16129/*
16130 * "str2float()" function
16131 */
16132 static void
16133f_str2float(argvars, rettv)
16134 typval_T *argvars;
16135 typval_T *rettv;
16136{
16137 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16138
16139 if (*p == '+')
16140 p = skipwhite(p + 1);
16141 (void)string2float(p, &rettv->vval.v_float);
16142 rettv->v_type = VAR_FLOAT;
16143}
16144#endif
16145
Bram Moolenaar2c932302006-03-18 21:42:09 +000016146/*
16147 * "str2nr()" function
16148 */
16149 static void
16150f_str2nr(argvars, rettv)
16151 typval_T *argvars;
16152 typval_T *rettv;
16153{
16154 int base = 10;
16155 char_u *p;
16156 long n;
16157
16158 if (argvars[1].v_type != VAR_UNKNOWN)
16159 {
16160 base = get_tv_number(&argvars[1]);
16161 if (base != 8 && base != 10 && base != 16)
16162 {
16163 EMSG(_(e_invarg));
16164 return;
16165 }
16166 }
16167
16168 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016169 if (*p == '+')
16170 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016171 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16172 rettv->vval.v_number = n;
16173}
16174
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175#ifdef HAVE_STRFTIME
16176/*
16177 * "strftime({format}[, {time}])" function
16178 */
16179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 typval_T *argvars;
16182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183{
16184 char_u result_buf[256];
16185 struct tm *curtime;
16186 time_t seconds;
16187 char_u *p;
16188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016189 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016192 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 seconds = time(NULL);
16194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016195 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 curtime = localtime(&seconds);
16197 /* MSVC returns NULL for an invalid value of seconds. */
16198 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016199 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 else
16201 {
16202# ifdef FEAT_MBYTE
16203 vimconv_T conv;
16204 char_u *enc;
16205
16206 conv.vc_type = CONV_NONE;
16207 enc = enc_locale();
16208 convert_setup(&conv, p_enc, enc);
16209 if (conv.vc_type != CONV_NONE)
16210 p = string_convert(&conv, p, NULL);
16211# endif
16212 if (p != NULL)
16213 (void)strftime((char *)result_buf, sizeof(result_buf),
16214 (char *)p, curtime);
16215 else
16216 result_buf[0] = NUL;
16217
16218# ifdef FEAT_MBYTE
16219 if (conv.vc_type != CONV_NONE)
16220 vim_free(p);
16221 convert_setup(&conv, enc, p_enc);
16222 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016223 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 else
16225# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227
16228# ifdef FEAT_MBYTE
16229 /* Release conversion descriptors */
16230 convert_setup(&conv, NULL, NULL);
16231 vim_free(enc);
16232# endif
16233 }
16234}
16235#endif
16236
16237/*
16238 * "stridx()" function
16239 */
16240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016241f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 typval_T *argvars;
16243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244{
16245 char_u buf[NUMBUFLEN];
16246 char_u *needle;
16247 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016248 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016250 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016252 needle = get_tv_string_chk(&argvars[1]);
16253 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 if (needle == NULL || haystack == NULL)
16256 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 if (argvars[2].v_type != VAR_UNKNOWN)
16259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016260 int error = FALSE;
16261
16262 start_idx = get_tv_number_chk(&argvars[2], &error);
16263 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016264 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016265 if (start_idx >= 0)
16266 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 }
16268
16269 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16270 if (pos != NULL)
16271 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272}
16273
16274/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275 * "string()" function
16276 */
16277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016278f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 typval_T *argvars;
16280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016281{
16282 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016283 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016286 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016287 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016288 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016289 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290}
16291
16292/*
16293 * "strlen()" function
16294 */
16295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016296f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 typval_T *argvars;
16298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 rettv->vval.v_number = (varnumber_T)(STRLEN(
16301 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302}
16303
16304/*
16305 * "strpart()" function
16306 */
16307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308f_strpart(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{
16312 char_u *p;
16313 int n;
16314 int len;
16315 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016316 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319 slen = (int)STRLEN(p);
16320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016321 n = get_tv_number_chk(&argvars[1], &error);
16322 if (error)
16323 len = 0;
16324 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 else
16327 len = slen - n; /* default len: all bytes that are available. */
16328
16329 /*
16330 * Only return the overlap between the specified part and the actual
16331 * string.
16332 */
16333 if (n < 0)
16334 {
16335 len += n;
16336 n = 0;
16337 }
16338 else if (n > slen)
16339 n = slen;
16340 if (len < 0)
16341 len = 0;
16342 else if (n + len > slen)
16343 len = slen - n;
16344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016345 rettv->v_type = VAR_STRING;
16346 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347}
16348
16349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350 * "strridx()" function
16351 */
16352 static void
16353f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016354 typval_T *argvars;
16355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356{
16357 char_u buf[NUMBUFLEN];
16358 char_u *needle;
16359 char_u *haystack;
16360 char_u *rest;
16361 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016362 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016364 needle = get_tv_string_chk(&argvars[1]);
16365 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366
16367 rettv->vval.v_number = -1;
16368 if (needle == NULL || haystack == NULL)
16369 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016370
16371 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016372 if (argvars[2].v_type != VAR_UNKNOWN)
16373 {
16374 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016375 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016376 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016377 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016378 }
16379 else
16380 end_idx = haystack_len;
16381
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016383 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016385 lastmatch = haystack + end_idx;
16386 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016388 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 for (rest = haystack; *rest != '\0'; ++rest)
16390 {
16391 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016392 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016393 break;
16394 lastmatch = rest;
16395 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016396 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397
16398 if (lastmatch == NULL)
16399 rettv->vval.v_number = -1;
16400 else
16401 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16402}
16403
16404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 * "strtrans()" function
16406 */
16407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016408f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016409 typval_T *argvars;
16410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->v_type = VAR_STRING;
16413 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414}
16415
16416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417 * "submatch()" function
16418 */
16419 static void
16420f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016421 typval_T *argvars;
16422 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423{
16424 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 rettv->vval.v_string =
16426 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427}
16428
16429/*
16430 * "substitute()" function
16431 */
16432 static void
16433f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 typval_T *argvars;
16435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436{
16437 char_u patbuf[NUMBUFLEN];
16438 char_u subbuf[NUMBUFLEN];
16439 char_u flagsbuf[NUMBUFLEN];
16440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 char_u *str = get_tv_string_chk(&argvars[0]);
16442 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16443 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16444 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16445
Bram Moolenaar0d660222005-01-07 21:51:51 +000016446 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16448 rettv->vval.v_string = NULL;
16449 else
16450 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451}
16452
16453/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016454 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 */
16456/*ARGSUSED*/
16457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016458f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016459 typval_T *argvars;
16460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461{
16462 int id = 0;
16463#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016464 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 long col;
16466 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016467 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016469 lnum = get_tv_lnum(argvars); /* -1 on type error */
16470 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16471 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016474 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016475 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476#endif
16477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016478 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479}
16480
16481/*
16482 * "synIDattr(id, what [, mode])" function
16483 */
16484/*ARGSUSED*/
16485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016486f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016487 typval_T *argvars;
16488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489{
16490 char_u *p = NULL;
16491#ifdef FEAT_SYN_HL
16492 int id;
16493 char_u *what;
16494 char_u *mode;
16495 char_u modebuf[NUMBUFLEN];
16496 int modec;
16497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016498 id = get_tv_number(&argvars[0]);
16499 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 modec = TOLOWER_ASC(mode[0]);
16504 if (modec != 't' && modec != 'c'
16505#ifdef FEAT_GUI
16506 && modec != 'g'
16507#endif
16508 )
16509 modec = 0; /* replace invalid with current */
16510 }
16511 else
16512 {
16513#ifdef FEAT_GUI
16514 if (gui.in_use)
16515 modec = 'g';
16516 else
16517#endif
16518 if (t_colors > 1)
16519 modec = 'c';
16520 else
16521 modec = 't';
16522 }
16523
16524
16525 switch (TOLOWER_ASC(what[0]))
16526 {
16527 case 'b':
16528 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16529 p = highlight_color(id, what, modec);
16530 else /* bold */
16531 p = highlight_has_attr(id, HL_BOLD, modec);
16532 break;
16533
16534 case 'f': /* fg[#] */
16535 p = highlight_color(id, what, modec);
16536 break;
16537
16538 case 'i':
16539 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16540 p = highlight_has_attr(id, HL_INVERSE, modec);
16541 else /* italic */
16542 p = highlight_has_attr(id, HL_ITALIC, modec);
16543 break;
16544
16545 case 'n': /* name */
16546 p = get_highlight_name(NULL, id - 1);
16547 break;
16548
16549 case 'r': /* reverse */
16550 p = highlight_has_attr(id, HL_INVERSE, modec);
16551 break;
16552
16553 case 's': /* standout */
16554 p = highlight_has_attr(id, HL_STANDOUT, modec);
16555 break;
16556
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016557 case 'u':
16558 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16559 /* underline */
16560 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16561 else
16562 /* undercurl */
16563 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 break;
16565 }
16566
16567 if (p != NULL)
16568 p = vim_strsave(p);
16569#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016570 rettv->v_type = VAR_STRING;
16571 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572}
16573
16574/*
16575 * "synIDtrans(id)" function
16576 */
16577/*ARGSUSED*/
16578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016580 typval_T *argvars;
16581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582{
16583 int id;
16584
16585#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587
16588 if (id > 0)
16589 id = syn_get_final_id(id);
16590 else
16591#endif
16592 id = 0;
16593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595}
16596
16597/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016598 * "synstack(lnum, col)" function
16599 */
16600/*ARGSUSED*/
16601 static void
16602f_synstack(argvars, rettv)
16603 typval_T *argvars;
16604 typval_T *rettv;
16605{
16606#ifdef FEAT_SYN_HL
16607 long lnum;
16608 long col;
16609 int i;
16610 int id;
16611#endif
16612
16613 rettv->v_type = VAR_LIST;
16614 rettv->vval.v_list = NULL;
16615
16616#ifdef FEAT_SYN_HL
16617 lnum = get_tv_lnum(argvars); /* -1 on type error */
16618 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16619
16620 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
16621 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
16622 && rettv_list_alloc(rettv) != FAIL)
16623 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016624 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016625 for (i = 0; ; ++i)
16626 {
16627 id = syn_get_stack_item(i);
16628 if (id < 0)
16629 break;
16630 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16631 break;
16632 }
16633 }
16634#endif
16635}
16636
16637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 * "system()" function
16639 */
16640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016642 typval_T *argvars;
16643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016645 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016647 char_u *infile = NULL;
16648 char_u buf[NUMBUFLEN];
16649 int err = FALSE;
16650 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016652 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016653 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016654
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016655 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016656 {
16657 /*
16658 * Write the string to a temp file, to be used for input of the shell
16659 * command.
16660 */
16661 if ((infile = vim_tempname('i')) == NULL)
16662 {
16663 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016664 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016665 }
16666
16667 fd = mch_fopen((char *)infile, WRITEBIN);
16668 if (fd == NULL)
16669 {
16670 EMSG2(_(e_notopen), infile);
16671 goto done;
16672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016673 p = get_tv_string_buf_chk(&argvars[1], buf);
16674 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016675 {
16676 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016677 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016678 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016679 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16680 err = TRUE;
16681 if (fclose(fd) != 0)
16682 err = TRUE;
16683 if (err)
16684 {
16685 EMSG(_("E677: Error writing temp file"));
16686 goto done;
16687 }
16688 }
16689
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016690 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16691 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016692
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693#ifdef USE_CR
16694 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016695 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 {
16697 char_u *s;
16698
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016699 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 {
16701 if (*s == CAR)
16702 *s = NL;
16703 }
16704 }
16705#else
16706# ifdef USE_CRNL
16707 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016708 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 {
16710 char_u *s, *d;
16711
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016712 d = res;
16713 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 {
16715 if (s[0] == CAR && s[1] == NL)
16716 ++s;
16717 *d++ = *s;
16718 }
16719 *d = NUL;
16720 }
16721# endif
16722#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016723
16724done:
16725 if (infile != NULL)
16726 {
16727 mch_remove(infile);
16728 vim_free(infile);
16729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016730 rettv->v_type = VAR_STRING;
16731 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732}
16733
16734/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016735 * "tabpagebuflist()" function
16736 */
16737/* ARGSUSED */
16738 static void
16739f_tabpagebuflist(argvars, rettv)
16740 typval_T *argvars;
16741 typval_T *rettv;
16742{
16743#ifndef FEAT_WINDOWS
16744 rettv->vval.v_number = 0;
16745#else
16746 tabpage_T *tp;
16747 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016748
16749 if (argvars[0].v_type == VAR_UNKNOWN)
16750 wp = firstwin;
16751 else
16752 {
16753 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16754 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016755 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016756 }
16757 if (wp == NULL)
16758 rettv->vval.v_number = 0;
16759 else
16760 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016761 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016762 rettv->vval.v_number = 0;
16763 else
16764 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016765 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016766 if (list_append_number(rettv->vval.v_list,
16767 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016768 break;
16769 }
16770 }
16771#endif
16772}
16773
16774
16775/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016776 * "tabpagenr()" function
16777 */
16778/* ARGSUSED */
16779 static void
16780f_tabpagenr(argvars, rettv)
16781 typval_T *argvars;
16782 typval_T *rettv;
16783{
16784 int nr = 1;
16785#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016786 char_u *arg;
16787
16788 if (argvars[0].v_type != VAR_UNKNOWN)
16789 {
16790 arg = get_tv_string_chk(&argvars[0]);
16791 nr = 0;
16792 if (arg != NULL)
16793 {
16794 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016795 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016796 else
16797 EMSG2(_(e_invexpr2), arg);
16798 }
16799 }
16800 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016801 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016802#endif
16803 rettv->vval.v_number = nr;
16804}
16805
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016806
16807#ifdef FEAT_WINDOWS
16808static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16809
16810/*
16811 * Common code for tabpagewinnr() and winnr().
16812 */
16813 static int
16814get_winnr(tp, argvar)
16815 tabpage_T *tp;
16816 typval_T *argvar;
16817{
16818 win_T *twin;
16819 int nr = 1;
16820 win_T *wp;
16821 char_u *arg;
16822
16823 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16824 if (argvar->v_type != VAR_UNKNOWN)
16825 {
16826 arg = get_tv_string_chk(argvar);
16827 if (arg == NULL)
16828 nr = 0; /* type error; errmsg already given */
16829 else if (STRCMP(arg, "$") == 0)
16830 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16831 else if (STRCMP(arg, "#") == 0)
16832 {
16833 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16834 if (twin == NULL)
16835 nr = 0;
16836 }
16837 else
16838 {
16839 EMSG2(_(e_invexpr2), arg);
16840 nr = 0;
16841 }
16842 }
16843
16844 if (nr > 0)
16845 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16846 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016847 {
16848 if (wp == NULL)
16849 {
16850 /* didn't find it in this tabpage */
16851 nr = 0;
16852 break;
16853 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016854 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016855 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016856 return nr;
16857}
16858#endif
16859
16860/*
16861 * "tabpagewinnr()" function
16862 */
16863/* ARGSUSED */
16864 static void
16865f_tabpagewinnr(argvars, rettv)
16866 typval_T *argvars;
16867 typval_T *rettv;
16868{
16869 int nr = 1;
16870#ifdef FEAT_WINDOWS
16871 tabpage_T *tp;
16872
16873 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16874 if (tp == NULL)
16875 nr = 0;
16876 else
16877 nr = get_winnr(tp, &argvars[1]);
16878#endif
16879 rettv->vval.v_number = nr;
16880}
16881
16882
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016883/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016884 * "tagfiles()" function
16885 */
16886/*ARGSUSED*/
16887 static void
16888f_tagfiles(argvars, rettv)
16889 typval_T *argvars;
16890 typval_T *rettv;
16891{
16892 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016893 tagname_T tn;
16894 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016895
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016896 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016897 {
16898 rettv->vval.v_number = 0;
16899 return;
16900 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016901
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016902 for (first = TRUE; ; first = FALSE)
16903 if (get_tagfname(&tn, first, fname) == FAIL
16904 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016905 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016906 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016907}
16908
16909/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016910 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016911 */
16912 static void
16913f_taglist(argvars, rettv)
16914 typval_T *argvars;
16915 typval_T *rettv;
16916{
16917 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016918
16919 tag_pattern = get_tv_string(&argvars[0]);
16920
16921 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 if (*tag_pattern == NUL)
16923 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016924
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016925 if (rettv_list_alloc(rettv) == OK)
16926 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016927}
16928
16929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 * "tempname()" function
16931 */
16932/*ARGSUSED*/
16933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016934f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 typval_T *argvars;
16936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937{
16938 static int x = 'A';
16939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940 rettv->v_type = VAR_STRING;
16941 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942
16943 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16944 * names. Skip 'I' and 'O', they are used for shell redirection. */
16945 do
16946 {
16947 if (x == 'Z')
16948 x = '0';
16949 else if (x == '9')
16950 x = 'A';
16951 else
16952 {
16953#ifdef EBCDIC
16954 if (x == 'I')
16955 x = 'J';
16956 else if (x == 'R')
16957 x = 'S';
16958 else
16959#endif
16960 ++x;
16961 }
16962 } while (x == 'I' || x == 'O');
16963}
16964
16965/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016966 * "test(list)" function: Just checking the walls...
16967 */
16968/*ARGSUSED*/
16969 static void
16970f_test(argvars, rettv)
16971 typval_T *argvars;
16972 typval_T *rettv;
16973{
16974 /* Used for unit testing. Change the code below to your liking. */
16975#if 0
16976 listitem_T *li;
16977 list_T *l;
16978 char_u *bad, *good;
16979
16980 if (argvars[0].v_type != VAR_LIST)
16981 return;
16982 l = argvars[0].vval.v_list;
16983 if (l == NULL)
16984 return;
16985 li = l->lv_first;
16986 if (li == NULL)
16987 return;
16988 bad = get_tv_string(&li->li_tv);
16989 li = li->li_next;
16990 if (li == NULL)
16991 return;
16992 good = get_tv_string(&li->li_tv);
16993 rettv->vval.v_number = test_edit_score(bad, good);
16994#endif
16995}
16996
16997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 * "tolower(string)" function
16999 */
17000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 typval_T *argvars;
17003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004{
17005 char_u *p;
17006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017007 p = vim_strsave(get_tv_string(&argvars[0]));
17008 rettv->v_type = VAR_STRING;
17009 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010
17011 if (p != NULL)
17012 while (*p != NUL)
17013 {
17014#ifdef FEAT_MBYTE
17015 int l;
17016
17017 if (enc_utf8)
17018 {
17019 int c, lc;
17020
17021 c = utf_ptr2char(p);
17022 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017023 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 /* TODO: reallocate string when byte count changes. */
17025 if (utf_char2len(lc) == l)
17026 utf_char2bytes(lc, p);
17027 p += l;
17028 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017029 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 p += l; /* skip multi-byte character */
17031 else
17032#endif
17033 {
17034 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17035 ++p;
17036 }
17037 }
17038}
17039
17040/*
17041 * "toupper(string)" function
17042 */
17043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017044f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017045 typval_T *argvars;
17046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017048 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017049 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050}
17051
17052/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017053 * "tr(string, fromstr, tostr)" function
17054 */
17055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017057 typval_T *argvars;
17058 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017059{
17060 char_u *instr;
17061 char_u *fromstr;
17062 char_u *tostr;
17063 char_u *p;
17064#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017065 int inlen;
17066 int fromlen;
17067 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017068 int idx;
17069 char_u *cpstr;
17070 int cplen;
17071 int first = TRUE;
17072#endif
17073 char_u buf[NUMBUFLEN];
17074 char_u buf2[NUMBUFLEN];
17075 garray_T ga;
17076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017077 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17079 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017080
17081 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017082 rettv->v_type = VAR_STRING;
17083 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 if (fromstr == NULL || tostr == NULL)
17085 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017086 ga_init2(&ga, (int)sizeof(char), 80);
17087
17088#ifdef FEAT_MBYTE
17089 if (!has_mbyte)
17090#endif
17091 /* not multi-byte: fromstr and tostr must be the same length */
17092 if (STRLEN(fromstr) != STRLEN(tostr))
17093 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017094#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017095error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017096#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017097 EMSG2(_(e_invarg2), fromstr);
17098 ga_clear(&ga);
17099 return;
17100 }
17101
17102 /* fromstr and tostr have to contain the same number of chars */
17103 while (*instr != NUL)
17104 {
17105#ifdef FEAT_MBYTE
17106 if (has_mbyte)
17107 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017108 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017109 cpstr = instr;
17110 cplen = inlen;
17111 idx = 0;
17112 for (p = fromstr; *p != NUL; p += fromlen)
17113 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017114 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017115 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17116 {
17117 for (p = tostr; *p != NUL; p += tolen)
17118 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017119 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017120 if (idx-- == 0)
17121 {
17122 cplen = tolen;
17123 cpstr = p;
17124 break;
17125 }
17126 }
17127 if (*p == NUL) /* tostr is shorter than fromstr */
17128 goto error;
17129 break;
17130 }
17131 ++idx;
17132 }
17133
17134 if (first && cpstr == instr)
17135 {
17136 /* Check that fromstr and tostr have the same number of
17137 * (multi-byte) characters. Done only once when a character
17138 * of instr doesn't appear in fromstr. */
17139 first = FALSE;
17140 for (p = tostr; *p != NUL; p += tolen)
17141 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017142 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017143 --idx;
17144 }
17145 if (idx != 0)
17146 goto error;
17147 }
17148
17149 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017150 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017151 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017152
17153 instr += inlen;
17154 }
17155 else
17156#endif
17157 {
17158 /* When not using multi-byte chars we can do it faster. */
17159 p = vim_strchr(fromstr, *instr);
17160 if (p != NULL)
17161 ga_append(&ga, tostr[p - fromstr]);
17162 else
17163 ga_append(&ga, *instr);
17164 ++instr;
17165 }
17166 }
17167
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017168 /* add a terminating NUL */
17169 ga_grow(&ga, 1);
17170 ga_append(&ga, NUL);
17171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017172 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017173}
17174
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017175#ifdef FEAT_FLOAT
17176/*
17177 * "trunc({float})" function
17178 */
17179 static void
17180f_trunc(argvars, rettv)
17181 typval_T *argvars;
17182 typval_T *rettv;
17183{
17184 float_T f;
17185
17186 rettv->v_type = VAR_FLOAT;
17187 if (get_float_arg(argvars, &f) == OK)
17188 /* trunc() is not in C90, use floor() or ceil() instead. */
17189 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17190 else
17191 rettv->vval.v_float = 0.0;
17192}
17193#endif
17194
Bram Moolenaar8299df92004-07-10 09:47:34 +000017195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 * "type(expr)" function
17197 */
17198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 typval_T *argvars;
17201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017203 int n;
17204
17205 switch (argvars[0].v_type)
17206 {
17207 case VAR_NUMBER: n = 0; break;
17208 case VAR_STRING: n = 1; break;
17209 case VAR_FUNC: n = 2; break;
17210 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017211 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017212#ifdef FEAT_FLOAT
17213 case VAR_FLOAT: n = 5; break;
17214#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017215 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17216 }
17217 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218}
17219
17220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017221 * "values(dict)" function
17222 */
17223 static void
17224f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 typval_T *argvars;
17226 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017227{
17228 dict_list(argvars, rettv, 1);
17229}
17230
17231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 * "virtcol(string)" function
17233 */
17234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017235f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017236 typval_T *argvars;
17237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238{
17239 colnr_T vcol = 0;
17240 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017241 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017243 fp = var2fpos(&argvars[0], FALSE, &fnum);
17244 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17245 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 {
17247 getvvcol(curwin, fp, NULL, NULL, &vcol);
17248 ++vcol;
17249 }
17250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017251 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252}
17253
17254/*
17255 * "visualmode()" function
17256 */
17257/*ARGSUSED*/
17258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *argvars;
17261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
17263#ifdef FEAT_VISUAL
17264 char_u str[2];
17265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017266 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 str[0] = curbuf->b_visual_mode_eval;
17268 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270
17271 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017272 if ((argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number != 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 || (argvars[0].v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017274 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 curbuf->b_visual_mode_eval = NUL;
17276#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017277 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278#endif
17279}
17280
17281/*
17282 * "winbufnr(nr)" function
17283 */
17284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017285f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 typval_T *argvars;
17287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288{
17289 win_T *wp;
17290
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017291 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017295 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
17299 * "wincol()" function
17300 */
17301/*ARGSUSED*/
17302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017304 typval_T *argvars;
17305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306{
17307 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017308 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309}
17310
17311/*
17312 * "winheight(nr)" function
17313 */
17314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017316 typval_T *argvars;
17317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318{
17319 win_T *wp;
17320
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017321 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017323 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326}
17327
17328/*
17329 * "winline()" function
17330 */
17331/*ARGSUSED*/
17332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *argvars;
17335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336{
17337 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339}
17340
17341/*
17342 * "winnr()" function
17343 */
17344/* ARGSUSED */
17345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017346f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017347 typval_T *argvars;
17348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349{
17350 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017351
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017353 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017355 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356}
17357
17358/*
17359 * "winrestcmd()" function
17360 */
17361/* ARGSUSED */
17362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 typval_T *argvars;
17365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
17367#ifdef FEAT_WINDOWS
17368 win_T *wp;
17369 int winnr = 1;
17370 garray_T ga;
17371 char_u buf[50];
17372
17373 ga_init2(&ga, (int)sizeof(char), 70);
17374 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17375 {
17376 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17377 ga_concat(&ga, buf);
17378# ifdef FEAT_VERTSPLIT
17379 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17380 ga_concat(&ga, buf);
17381# endif
17382 ++winnr;
17383 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017384 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017386 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391}
17392
17393/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017394 * "winrestview()" function
17395 */
17396/* ARGSUSED */
17397 static void
17398f_winrestview(argvars, rettv)
17399 typval_T *argvars;
17400 typval_T *rettv;
17401{
17402 dict_T *dict;
17403
17404 if (argvars[0].v_type != VAR_DICT
17405 || (dict = argvars[0].vval.v_dict) == NULL)
17406 EMSG(_(e_invarg));
17407 else
17408 {
17409 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17410 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17411#ifdef FEAT_VIRTUALEDIT
17412 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17413#endif
17414 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017415 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017416
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017417 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017418#ifdef FEAT_DIFF
17419 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17420#endif
17421 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17422 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17423
17424 check_cursor();
17425 changed_cline_bef_curs();
17426 invalidate_botline();
17427 redraw_later(VALID);
17428
17429 if (curwin->w_topline == 0)
17430 curwin->w_topline = 1;
17431 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17432 curwin->w_topline = curbuf->b_ml.ml_line_count;
17433#ifdef FEAT_DIFF
17434 check_topfill(curwin, TRUE);
17435#endif
17436 }
17437}
17438
17439/*
17440 * "winsaveview()" function
17441 */
17442/* ARGSUSED */
17443 static void
17444f_winsaveview(argvars, rettv)
17445 typval_T *argvars;
17446 typval_T *rettv;
17447{
17448 dict_T *dict;
17449
17450 dict = dict_alloc();
17451 if (dict == NULL)
17452 return;
17453 rettv->v_type = VAR_DICT;
17454 rettv->vval.v_dict = dict;
17455 ++dict->dv_refcount;
17456
17457 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17458 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17459#ifdef FEAT_VIRTUALEDIT
17460 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17461#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017462 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017463 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17464
17465 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17466#ifdef FEAT_DIFF
17467 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17468#endif
17469 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17470 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17471}
17472
17473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 * "winwidth(nr)" function
17475 */
17476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017477f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 typval_T *argvars;
17479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480{
17481 win_T *wp;
17482
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017483 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 else
17487#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491#endif
17492}
17493
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017495 * "writefile()" function
17496 */
17497 static void
17498f_writefile(argvars, rettv)
17499 typval_T *argvars;
17500 typval_T *rettv;
17501{
17502 int binary = FALSE;
17503 char_u *fname;
17504 FILE *fd;
17505 listitem_T *li;
17506 char_u *s;
17507 int ret = 0;
17508 int c;
17509
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017510 if (check_restricted() || check_secure())
17511 return;
17512
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017513 if (argvars[0].v_type != VAR_LIST)
17514 {
17515 EMSG2(_(e_listarg), "writefile()");
17516 return;
17517 }
17518 if (argvars[0].vval.v_list == NULL)
17519 return;
17520
17521 if (argvars[2].v_type != VAR_UNKNOWN
17522 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17523 binary = TRUE;
17524
17525 /* Always open the file in binary mode, library functions have a mind of
17526 * their own about CR-LF conversion. */
17527 fname = get_tv_string(&argvars[1]);
17528 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17529 {
17530 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17531 ret = -1;
17532 }
17533 else
17534 {
17535 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17536 li = li->li_next)
17537 {
17538 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17539 {
17540 if (*s == '\n')
17541 c = putc(NUL, fd);
17542 else
17543 c = putc(*s, fd);
17544 if (c == EOF)
17545 {
17546 ret = -1;
17547 break;
17548 }
17549 }
17550 if (!binary || li->li_next != NULL)
17551 if (putc('\n', fd) == EOF)
17552 {
17553 ret = -1;
17554 break;
17555 }
17556 if (ret < 0)
17557 {
17558 EMSG(_(e_write));
17559 break;
17560 }
17561 }
17562 fclose(fd);
17563 }
17564
17565 rettv->vval.v_number = ret;
17566}
17567
17568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017570 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 */
17572 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017573var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017574 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017575 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017576 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017578 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017580 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581
Bram Moolenaara5525202006-03-02 22:52:09 +000017582 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017583 if (varp->v_type == VAR_LIST)
17584 {
17585 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017586 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017587 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017588 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017589
17590 l = varp->vval.v_list;
17591 if (l == NULL)
17592 return NULL;
17593
17594 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017595 pos.lnum = list_find_nr(l, 0L, &error);
17596 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017597 return NULL; /* invalid line number */
17598
17599 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017600 pos.col = list_find_nr(l, 1L, &error);
17601 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017602 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017603 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017604
17605 /* We accept "$" for the column number: last column. */
17606 li = list_find(l, 1L);
17607 if (li != NULL && li->li_tv.v_type == VAR_STRING
17608 && li->li_tv.vval.v_string != NULL
17609 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17610 pos.col = len + 1;
17611
Bram Moolenaara5525202006-03-02 22:52:09 +000017612 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017613 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017614 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017615 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017616
Bram Moolenaara5525202006-03-02 22:52:09 +000017617#ifdef FEAT_VIRTUALEDIT
17618 /* Get the virtual offset. Defaults to zero. */
17619 pos.coladd = list_find_nr(l, 2L, &error);
17620 if (error)
17621 pos.coladd = 0;
17622#endif
17623
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017624 return &pos;
17625 }
17626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 name = get_tv_string_chk(varp);
17628 if (name == NULL)
17629 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017630 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017632#ifdef FEAT_VISUAL
17633 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17634 {
17635 if (VIsual_active)
17636 return &VIsual;
17637 return &curwin->w_cursor;
17638 }
17639#endif
17640 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017642 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17644 return NULL;
17645 return pp;
17646 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017647
17648#ifdef FEAT_VIRTUALEDIT
17649 pos.coladd = 0;
17650#endif
17651
Bram Moolenaar477933c2007-07-17 14:32:23 +000017652 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017653 {
17654 pos.col = 0;
17655 if (name[1] == '0') /* "w0": first visible line */
17656 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017657 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017658 pos.lnum = curwin->w_topline;
17659 return &pos;
17660 }
17661 else if (name[1] == '$') /* "w$": last visible line */
17662 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017663 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017664 pos.lnum = curwin->w_botline - 1;
17665 return &pos;
17666 }
17667 }
17668 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017670 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 {
17672 pos.lnum = curbuf->b_ml.ml_line_count;
17673 pos.col = 0;
17674 }
17675 else
17676 {
17677 pos.lnum = curwin->w_cursor.lnum;
17678 pos.col = (colnr_T)STRLEN(ml_get_curline());
17679 }
17680 return &pos;
17681 }
17682 return NULL;
17683}
17684
17685/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017686 * Convert list in "arg" into a position and optional file number.
17687 * When "fnump" is NULL there is no file number, only 3 items.
17688 * Note that the column is passed on as-is, the caller may want to decrement
17689 * it to use 1 for the first column.
17690 * Return FAIL when conversion is not possible, doesn't check the position for
17691 * validity.
17692 */
17693 static int
17694list2fpos(arg, posp, fnump)
17695 typval_T *arg;
17696 pos_T *posp;
17697 int *fnump;
17698{
17699 list_T *l = arg->vval.v_list;
17700 long i = 0;
17701 long n;
17702
Bram Moolenaarbde35262006-07-23 20:12:24 +000017703 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17704 * when "fnump" isn't NULL and "coladd" is optional. */
17705 if (arg->v_type != VAR_LIST
17706 || l == NULL
17707 || l->lv_len < (fnump == NULL ? 2 : 3)
17708 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017709 return FAIL;
17710
17711 if (fnump != NULL)
17712 {
17713 n = list_find_nr(l, i++, NULL); /* fnum */
17714 if (n < 0)
17715 return FAIL;
17716 if (n == 0)
17717 n = curbuf->b_fnum; /* current buffer */
17718 *fnump = n;
17719 }
17720
17721 n = list_find_nr(l, i++, NULL); /* lnum */
17722 if (n < 0)
17723 return FAIL;
17724 posp->lnum = n;
17725
17726 n = list_find_nr(l, i++, NULL); /* col */
17727 if (n < 0)
17728 return FAIL;
17729 posp->col = n;
17730
17731#ifdef FEAT_VIRTUALEDIT
17732 n = list_find_nr(l, i, NULL);
17733 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017734 posp->coladd = 0;
17735 else
17736 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017737#endif
17738
17739 return OK;
17740}
17741
17742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 * Get the length of an environment variable name.
17744 * Advance "arg" to the first character after the name.
17745 * Return 0 for error.
17746 */
17747 static int
17748get_env_len(arg)
17749 char_u **arg;
17750{
17751 char_u *p;
17752 int len;
17753
17754 for (p = *arg; vim_isIDc(*p); ++p)
17755 ;
17756 if (p == *arg) /* no name found */
17757 return 0;
17758
17759 len = (int)(p - *arg);
17760 *arg = p;
17761 return len;
17762}
17763
17764/*
17765 * Get the length of the name of a function or internal variable.
17766 * "arg" is advanced to the first non-white character after the name.
17767 * Return 0 if something is wrong.
17768 */
17769 static int
17770get_id_len(arg)
17771 char_u **arg;
17772{
17773 char_u *p;
17774 int len;
17775
17776 /* Find the end of the name. */
17777 for (p = *arg; eval_isnamec(*p); ++p)
17778 ;
17779 if (p == *arg) /* no name found */
17780 return 0;
17781
17782 len = (int)(p - *arg);
17783 *arg = skipwhite(p);
17784
17785 return len;
17786}
17787
17788/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017789 * Get the length of the name of a variable or function.
17790 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017792 * Return -1 if curly braces expansion failed.
17793 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 * If the name contains 'magic' {}'s, expand them and return the
17795 * expanded name in an allocated string via 'alias' - caller must free.
17796 */
17797 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017798get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 char_u **arg;
17800 char_u **alias;
17801 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017802 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803{
17804 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 char_u *p;
17806 char_u *expr_start;
17807 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
17809 *alias = NULL; /* default to no alias */
17810
17811 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17812 && (*arg)[2] == (int)KE_SNR)
17813 {
17814 /* hard coded <SNR>, already translated */
17815 *arg += 3;
17816 return get_id_len(arg) + 3;
17817 }
17818 len = eval_fname_script(*arg);
17819 if (len > 0)
17820 {
17821 /* literal "<SID>", "s:" or "<SNR>" */
17822 *arg += len;
17823 }
17824
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017828 p = find_name_end(*arg, &expr_start, &expr_end,
17829 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 if (expr_start != NULL)
17831 {
17832 char_u *temp_string;
17833
17834 if (!evaluate)
17835 {
17836 len += (int)(p - *arg);
17837 *arg = skipwhite(p);
17838 return len;
17839 }
17840
17841 /*
17842 * Include any <SID> etc in the expanded string:
17843 * Thus the -len here.
17844 */
17845 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17846 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017847 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 *alias = temp_string;
17849 *arg = skipwhite(p);
17850 return (int)STRLEN(temp_string);
17851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852
17853 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017854 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 EMSG2(_(e_invexpr2), *arg);
17856
17857 return len;
17858}
17859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860/*
17861 * Find the end of a variable or function name, taking care of magic braces.
17862 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17863 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017864 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017865 * Return a pointer to just after the name. Equal to "arg" if there is no
17866 * valid name.
17867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017869find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 char_u *arg;
17871 char_u **expr_start;
17872 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017873 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017875 int mb_nest = 0;
17876 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 char_u *p;
17878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017879 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881 *expr_start = NULL;
17882 *expr_end = NULL;
17883 }
17884
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017885 /* Quick check for valid starting character. */
17886 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17887 return arg;
17888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017889 for (p = arg; *p != NUL
17890 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017891 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017892 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017894 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017896 if (*p == '\'')
17897 {
17898 /* skip over 'string' to avoid counting [ and ] inside it. */
17899 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17900 ;
17901 if (*p == NUL)
17902 break;
17903 }
17904 else if (*p == '"')
17905 {
17906 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17907 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17908 if (*p == '\\' && p[1] != NUL)
17909 ++p;
17910 if (*p == NUL)
17911 break;
17912 }
17913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017914 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017916 if (*p == '[')
17917 ++br_nest;
17918 else if (*p == ']')
17919 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017922 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017924 if (*p == '{')
17925 {
17926 mb_nest++;
17927 if (expr_start != NULL && *expr_start == NULL)
17928 *expr_start = p;
17929 }
17930 else if (*p == '}')
17931 {
17932 mb_nest--;
17933 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17934 *expr_end = p;
17935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 }
17938
17939 return p;
17940}
17941
17942/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017943 * Expands out the 'magic' {}'s in a variable/function name.
17944 * Note that this can call itself recursively, to deal with
17945 * constructs like foo{bar}{baz}{bam}
17946 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17947 * "in_start" ^
17948 * "expr_start" ^
17949 * "expr_end" ^
17950 * "in_end" ^
17951 *
17952 * Returns a new allocated string, which the caller must free.
17953 * Returns NULL for failure.
17954 */
17955 static char_u *
17956make_expanded_name(in_start, expr_start, expr_end, in_end)
17957 char_u *in_start;
17958 char_u *expr_start;
17959 char_u *expr_end;
17960 char_u *in_end;
17961{
17962 char_u c1;
17963 char_u *retval = NULL;
17964 char_u *temp_result;
17965 char_u *nextcmd = NULL;
17966
17967 if (expr_end == NULL || in_end == NULL)
17968 return NULL;
17969 *expr_start = NUL;
17970 *expr_end = NUL;
17971 c1 = *in_end;
17972 *in_end = NUL;
17973
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017974 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017975 if (temp_result != NULL && nextcmd == NULL)
17976 {
17977 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17978 + (in_end - expr_end) + 1));
17979 if (retval != NULL)
17980 {
17981 STRCPY(retval, in_start);
17982 STRCAT(retval, temp_result);
17983 STRCAT(retval, expr_end + 1);
17984 }
17985 }
17986 vim_free(temp_result);
17987
17988 *in_end = c1; /* put char back for error messages */
17989 *expr_start = '{';
17990 *expr_end = '}';
17991
17992 if (retval != NULL)
17993 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017994 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017995 if (expr_start != NULL)
17996 {
17997 /* Further expansion! */
17998 temp_result = make_expanded_name(retval, expr_start,
17999 expr_end, temp_result);
18000 vim_free(retval);
18001 retval = temp_result;
18002 }
18003 }
18004
18005 return retval;
18006}
18007
18008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018010 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 */
18012 static int
18013eval_isnamec(c)
18014 int c;
18015{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018016 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18017}
18018
18019/*
18020 * Return TRUE if character "c" can be used as the first character in a
18021 * variable or function name (excluding '{' and '}').
18022 */
18023 static int
18024eval_isnamec1(c)
18025 int c;
18026{
18027 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028}
18029
18030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 * Set number v: variable to "val".
18032 */
18033 void
18034set_vim_var_nr(idx, val)
18035 int idx;
18036 long val;
18037{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018038 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039}
18040
18041/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018042 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 */
18044 long
18045get_vim_var_nr(idx)
18046 int idx;
18047{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018048 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049}
18050
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018051#if defined(FEAT_AUTOCMD) || defined(PROTO)
18052/*
18053 * Get string v: variable value. Uses a static buffer, can only be used once.
18054 */
18055 char_u *
18056get_vim_var_str(idx)
18057 int idx;
18058{
18059 return get_tv_string(&vimvars[idx].vv_tv);
18060}
18061#endif
18062
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063/*
18064 * Set v:count, v:count1 and v:prevcount.
18065 */
18066 void
18067set_vcount(count, count1)
18068 long count;
18069 long count1;
18070{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018071 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
18072 vimvars[VV_COUNT].vv_nr = count;
18073 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074}
18075
18076/*
18077 * Set string v: variable to a copy of "val".
18078 */
18079 void
18080set_vim_var_string(idx, val, len)
18081 int idx;
18082 char_u *val;
18083 int len; /* length of "val" to use or -1 (whole string) */
18084{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018085 /* Need to do this (at least) once, since we can't initialize a union.
18086 * Will always be invoked when "v:progname" is set. */
18087 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18088
Bram Moolenaare9a41262005-01-15 22:18:47 +000018089 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018091 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018093 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018095 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096}
18097
18098/*
18099 * Set v:register if needed.
18100 */
18101 void
18102set_reg_var(c)
18103 int c;
18104{
18105 char_u regname;
18106
18107 if (c == 0 || c == ' ')
18108 regname = '"';
18109 else
18110 regname = c;
18111 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018112 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 set_vim_var_string(VV_REG, &regname, 1);
18114}
18115
18116/*
18117 * Get or set v:exception. If "oldval" == NULL, return the current value.
18118 * Otherwise, restore the value to "oldval" and return NULL.
18119 * Must always be called in pairs to save and restore v:exception! Does not
18120 * take care of memory allocations.
18121 */
18122 char_u *
18123v_exception(oldval)
18124 char_u *oldval;
18125{
18126 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018127 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128
Bram Moolenaare9a41262005-01-15 22:18:47 +000018129 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 return NULL;
18131}
18132
18133/*
18134 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18135 * Otherwise, restore the value to "oldval" and return NULL.
18136 * Must always be called in pairs to save and restore v:throwpoint! Does not
18137 * take care of memory allocations.
18138 */
18139 char_u *
18140v_throwpoint(oldval)
18141 char_u *oldval;
18142{
18143 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018144 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145
Bram Moolenaare9a41262005-01-15 22:18:47 +000018146 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 return NULL;
18148}
18149
18150#if defined(FEAT_AUTOCMD) || defined(PROTO)
18151/*
18152 * Set v:cmdarg.
18153 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18154 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18155 * Must always be called in pairs!
18156 */
18157 char_u *
18158set_cmdarg(eap, oldarg)
18159 exarg_T *eap;
18160 char_u *oldarg;
18161{
18162 char_u *oldval;
18163 char_u *newval;
18164 unsigned len;
18165
Bram Moolenaare9a41262005-01-15 22:18:47 +000018166 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018167 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018169 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018170 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018171 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 }
18173
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018174 if (eap->force_bin == FORCE_BIN)
18175 len = 6;
18176 else if (eap->force_bin == FORCE_NOBIN)
18177 len = 8;
18178 else
18179 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018180
18181 if (eap->read_edit)
18182 len += 7;
18183
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018184 if (eap->force_ff != 0)
18185 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18186# ifdef FEAT_MBYTE
18187 if (eap->force_enc != 0)
18188 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018189 if (eap->bad_char != 0)
18190 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018191# endif
18192
18193 newval = alloc(len + 1);
18194 if (newval == NULL)
18195 return NULL;
18196
18197 if (eap->force_bin == FORCE_BIN)
18198 sprintf((char *)newval, " ++bin");
18199 else if (eap->force_bin == FORCE_NOBIN)
18200 sprintf((char *)newval, " ++nobin");
18201 else
18202 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018203
18204 if (eap->read_edit)
18205 STRCAT(newval, " ++edit");
18206
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018207 if (eap->force_ff != 0)
18208 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18209 eap->cmd + eap->force_ff);
18210# ifdef FEAT_MBYTE
18211 if (eap->force_enc != 0)
18212 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18213 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018214 if (eap->bad_char != 0)
18215 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18216 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018217# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018218 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018219 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220}
18221#endif
18222
18223/*
18224 * Get the value of internal variable "name".
18225 * Return OK or FAIL.
18226 */
18227 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018228get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 char_u *name;
18230 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018231 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018232 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233{
18234 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018235 typval_T *tv = NULL;
18236 typval_T atv;
18237 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239
18240 /* truncate the name, so that we can use strcmp() */
18241 cc = name[len];
18242 name[len] = NUL;
18243
18244 /*
18245 * Check for "b:changedtick".
18246 */
18247 if (STRCMP(name, "b:changedtick") == 0)
18248 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018249 atv.v_type = VAR_NUMBER;
18250 atv.vval.v_number = curbuf->b_changedtick;
18251 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 }
18253
18254 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 * Check for user-defined variables.
18256 */
18257 else
18258 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018259 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018261 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 }
18263
Bram Moolenaare9a41262005-01-15 22:18:47 +000018264 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018266 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018267 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 ret = FAIL;
18269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018271 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
18273 name[len] = cc;
18274
18275 return ret;
18276}
18277
18278/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018279 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18280 * Also handle function call with Funcref variable: func(expr)
18281 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18282 */
18283 static int
18284handle_subscript(arg, rettv, evaluate, verbose)
18285 char_u **arg;
18286 typval_T *rettv;
18287 int evaluate; /* do more than finding the end */
18288 int verbose; /* give error messages */
18289{
18290 int ret = OK;
18291 dict_T *selfdict = NULL;
18292 char_u *s;
18293 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018294 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018295
18296 while (ret == OK
18297 && (**arg == '['
18298 || (**arg == '.' && rettv->v_type == VAR_DICT)
18299 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18300 && !vim_iswhite(*(*arg - 1)))
18301 {
18302 if (**arg == '(')
18303 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018304 /* need to copy the funcref so that we can clear rettv */
18305 functv = *rettv;
18306 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018307
18308 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018309 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018310 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018311 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18312 &len, evaluate, selfdict);
18313
18314 /* Clear the funcref afterwards, so that deleting it while
18315 * evaluating the arguments is possible (see test55). */
18316 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018317
18318 /* Stop the expression evaluation when immediately aborting on
18319 * error, or when an interrupt occurred or an exception was thrown
18320 * but not caught. */
18321 if (aborting())
18322 {
18323 if (ret == OK)
18324 clear_tv(rettv);
18325 ret = FAIL;
18326 }
18327 dict_unref(selfdict);
18328 selfdict = NULL;
18329 }
18330 else /* **arg == '[' || **arg == '.' */
18331 {
18332 dict_unref(selfdict);
18333 if (rettv->v_type == VAR_DICT)
18334 {
18335 selfdict = rettv->vval.v_dict;
18336 if (selfdict != NULL)
18337 ++selfdict->dv_refcount;
18338 }
18339 else
18340 selfdict = NULL;
18341 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18342 {
18343 clear_tv(rettv);
18344 ret = FAIL;
18345 }
18346 }
18347 }
18348 dict_unref(selfdict);
18349 return ret;
18350}
18351
18352/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018353 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018354 * value).
18355 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018356 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018357alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018358{
Bram Moolenaar33570922005-01-25 22:26:29 +000018359 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018360}
18361
18362/*
18363 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 * The string "s" must have been allocated, it is consumed.
18365 * Return NULL for out of memory, the variable otherwise.
18366 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018367 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018368alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 char_u *s;
18370{
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018373 rettv = alloc_tv();
18374 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018376 rettv->v_type = VAR_STRING;
18377 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 }
18379 else
18380 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382}
18383
18384/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018385 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018387 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018388free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018389 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390{
18391 if (varp != NULL)
18392 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018393 switch (varp->v_type)
18394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018395 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018396 func_unref(varp->vval.v_string);
18397 /*FALLTHROUGH*/
18398 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018399 vim_free(varp->vval.v_string);
18400 break;
18401 case VAR_LIST:
18402 list_unref(varp->vval.v_list);
18403 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018404 case VAR_DICT:
18405 dict_unref(varp->vval.v_dict);
18406 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018407 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018408#ifdef FEAT_FLOAT
18409 case VAR_FLOAT:
18410#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018411 case VAR_UNKNOWN:
18412 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018413 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018414 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018415 break;
18416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 vim_free(varp);
18418 }
18419}
18420
18421/*
18422 * Free the memory for a variable value and set the value to NULL or 0.
18423 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018424 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018425clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018426 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427{
18428 if (varp != NULL)
18429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018430 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018432 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018433 func_unref(varp->vval.v_string);
18434 /*FALLTHROUGH*/
18435 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018436 vim_free(varp->vval.v_string);
18437 varp->vval.v_string = NULL;
18438 break;
18439 case VAR_LIST:
18440 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018441 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018442 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018443 case VAR_DICT:
18444 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018445 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018446 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018447 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018448 varp->vval.v_number = 0;
18449 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018450#ifdef FEAT_FLOAT
18451 case VAR_FLOAT:
18452 varp->vval.v_float = 0.0;
18453 break;
18454#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018455 case VAR_UNKNOWN:
18456 break;
18457 default:
18458 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018460 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 }
18462}
18463
18464/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018465 * Set the value of a variable to NULL without freeing items.
18466 */
18467 static void
18468init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018469 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470{
18471 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018472 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473}
18474
18475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 * Get the number value of a variable.
18477 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018478 * For incompatible types, return 0.
18479 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18480 * caller of incompatible types: it sets *denote to TRUE if "denote"
18481 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 */
18483 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018485 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018487 int error = FALSE;
18488
18489 return get_tv_number_chk(varp, &error); /* return 0L on error */
18490}
18491
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018492 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018493get_tv_number_chk(varp, denote)
18494 typval_T *varp;
18495 int *denote;
18496{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018497 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018499 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018501 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018502 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018503#ifdef FEAT_FLOAT
18504 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018505 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018506 break;
18507#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018508 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018509 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018510 break;
18511 case VAR_STRING:
18512 if (varp->vval.v_string != NULL)
18513 vim_str2nr(varp->vval.v_string, NULL, NULL,
18514 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018515 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018516 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018517 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018518 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018519 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018520 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018521 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018522 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018523 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 if (denote == NULL) /* useful for values that must be unsigned */
18527 n = -1;
18528 else
18529 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018530 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531}
18532
18533/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018534 * Get the lnum from the first argument.
18535 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018536 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 */
18538 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018539get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018540 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541{
Bram Moolenaar33570922005-01-25 22:26:29 +000018542 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 linenr_T lnum;
18544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018545 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 if (lnum == 0) /* no valid number, try using line() */
18547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548 rettv.v_type = VAR_NUMBER;
18549 f_line(argvars, &rettv);
18550 lnum = rettv.vval.v_number;
18551 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 }
18553 return lnum;
18554}
18555
18556/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018557 * Get the lnum from the first argument.
18558 * Also accepts "$", then "buf" is used.
18559 * Returns 0 on error.
18560 */
18561 static linenr_T
18562get_tv_lnum_buf(argvars, buf)
18563 typval_T *argvars;
18564 buf_T *buf;
18565{
18566 if (argvars[0].v_type == VAR_STRING
18567 && argvars[0].vval.v_string != NULL
18568 && argvars[0].vval.v_string[0] == '$'
18569 && buf != NULL)
18570 return buf->b_ml.ml_line_count;
18571 return get_tv_number_chk(&argvars[0], NULL);
18572}
18573
18574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 * Get the string value of a variable.
18576 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018577 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18578 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 * If the String variable has never been set, return an empty string.
18580 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018581 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18582 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 */
18584 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018585get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018586 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587{
18588 static char_u mybuf[NUMBUFLEN];
18589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591}
18592
18593 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018595 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018596 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018598 char_u *res = get_tv_string_buf_chk(varp, buf);
18599
18600 return res != NULL ? res : (char_u *)"";
18601}
18602
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018603 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604get_tv_string_chk(varp)
18605 typval_T *varp;
18606{
18607 static char_u mybuf[NUMBUFLEN];
18608
18609 return get_tv_string_buf_chk(varp, mybuf);
18610}
18611
18612 static char_u *
18613get_tv_string_buf_chk(varp, buf)
18614 typval_T *varp;
18615 char_u *buf;
18616{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018617 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018619 case VAR_NUMBER:
18620 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18621 return buf;
18622 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018623 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018624 break;
18625 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018626 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018627 break;
18628 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018629 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018630 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018631#ifdef FEAT_FLOAT
18632 case VAR_FLOAT:
18633 EMSG(_("E806: using Float as a String"));
18634 break;
18635#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018636 case VAR_STRING:
18637 if (varp->vval.v_string != NULL)
18638 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018639 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018640 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018641 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018642 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018644 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645}
18646
18647/*
18648 * Find variable "name" in the list of variables.
18649 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018650 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018651 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018652 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018654 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018655find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018657 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018660 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661
Bram Moolenaara7043832005-01-21 11:56:39 +000018662 ht = find_var_ht(name, &varname);
18663 if (htp != NULL)
18664 *htp = ht;
18665 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018667 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668}
18669
18670/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018671 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018672 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018674 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018675find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018677 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018678 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018679{
Bram Moolenaar33570922005-01-25 22:26:29 +000018680 hashitem_T *hi;
18681
18682 if (*varname == NUL)
18683 {
18684 /* Must be something like "s:", otherwise "ht" would be NULL. */
18685 switch (varname[-2])
18686 {
18687 case 's': return &SCRIPT_SV(current_SID).sv_var;
18688 case 'g': return &globvars_var;
18689 case 'v': return &vimvars_var;
18690 case 'b': return &curbuf->b_bufvar;
18691 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018692#ifdef FEAT_WINDOWS
18693 case 't': return &curtab->tp_winvar;
18694#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018695 case 'l': return current_funccal == NULL
18696 ? NULL : &current_funccal->l_vars_var;
18697 case 'a': return current_funccal == NULL
18698 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018699 }
18700 return NULL;
18701 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018702
18703 hi = hash_find(ht, varname);
18704 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018705 {
18706 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018707 * worked find the variable again. Don't auto-load a script if it was
18708 * loaded already, otherwise it would be loaded every time when
18709 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018710 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018711 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018712 hi = hash_find(ht, varname);
18713 if (HASHITEM_EMPTY(hi))
18714 return NULL;
18715 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018716 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018717}
18718
18719/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018721 * Set "varname" to the start of name without ':'.
18722 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018723 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018724find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 char_u *name;
18726 char_u **varname;
18727{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018728 hashitem_T *hi;
18729
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 if (name[1] != ':')
18731 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018732 /* The name must not start with a colon or #. */
18733 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 return NULL;
18735 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018736
18737 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018738 hi = hash_find(&compat_hashtab, name);
18739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018740 return &compat_hashtab;
18741
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018743 return &globvarht; /* global variable */
18744 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 }
18746 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018747 if (*name == 'g') /* global variable */
18748 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018749 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18750 */
18751 if (vim_strchr(name + 2, ':') != NULL
18752 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018753 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018755 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018758#ifdef FEAT_WINDOWS
18759 if (*name == 't') /* tab page variable */
18760 return &curtab->tp_vars.dv_hashtab;
18761#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 if (*name == 'v') /* v: variable */
18763 return &vimvarht;
18764 if (*name == 'a' && current_funccal != NULL) /* function argument */
18765 return &current_funccal->l_avars.dv_hashtab;
18766 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18767 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 if (*name == 's' /* script variable */
18769 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18770 return &SCRIPT_VARS(current_SID);
18771 return NULL;
18772}
18773
18774/*
18775 * Get the string value of a (global/local) variable.
18776 * Returns NULL when it doesn't exist.
18777 */
18778 char_u *
18779get_var_value(name)
18780 char_u *name;
18781{
Bram Moolenaar33570922005-01-25 22:26:29 +000018782 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783
Bram Moolenaara7043832005-01-21 11:56:39 +000018784 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 if (v == NULL)
18786 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788}
18789
18790/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 * sourcing this script and when executing functions defined in the script.
18793 */
18794 void
18795new_script_vars(id)
18796 scid_T id;
18797{
Bram Moolenaara7043832005-01-21 11:56:39 +000018798 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 hashtab_T *ht;
18800 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018801
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18803 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018804 /* Re-allocating ga_data means that an ht_array pointing to
18805 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018806 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018807 for (i = 1; i <= ga_scripts.ga_len; ++i)
18808 {
18809 ht = &SCRIPT_VARS(i);
18810 if (ht->ht_mask == HT_INIT_SIZE - 1)
18811 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018812 sv = &SCRIPT_SV(i);
18813 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018814 }
18815
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 while (ga_scripts.ga_len < id)
18817 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018818 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18819 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 }
18822 }
18823}
18824
18825/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018826 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18827 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 */
18829 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018830init_var_dict(dict, dict_var)
18831 dict_T *dict;
18832 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833{
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 hash_init(&dict->dv_hashtab);
18835 dict->dv_refcount = 99999;
18836 dict_var->di_tv.vval.v_dict = dict;
18837 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018838 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018839 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18840 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841}
18842
18843/*
18844 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018845 * Frees all allocated variables and the value they contain.
18846 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 */
18848 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018849vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018850 hashtab_T *ht;
18851{
18852 vars_clear_ext(ht, TRUE);
18853}
18854
18855/*
18856 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18857 */
18858 static void
18859vars_clear_ext(ht, free_val)
18860 hashtab_T *ht;
18861 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862{
Bram Moolenaara7043832005-01-21 11:56:39 +000018863 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018864 hashitem_T *hi;
18865 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866
Bram Moolenaar33570922005-01-25 22:26:29 +000018867 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018868 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018869 for (hi = ht->ht_array; todo > 0; ++hi)
18870 {
18871 if (!HASHITEM_EMPTY(hi))
18872 {
18873 --todo;
18874
Bram Moolenaar33570922005-01-25 22:26:29 +000018875 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018876 * ht_array might change then. hash_clear() takes care of it
18877 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018878 v = HI2DI(hi);
18879 if (free_val)
18880 clear_tv(&v->di_tv);
18881 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18882 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018883 }
18884 }
18885 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018886 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887}
18888
Bram Moolenaara7043832005-01-21 11:56:39 +000018889/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 * Delete a variable from hashtab "ht" at item "hi".
18891 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018894delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018895 hashtab_T *ht;
18896 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897{
Bram Moolenaar33570922005-01-25 22:26:29 +000018898 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018899
18900 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 clear_tv(&di->di_tv);
18902 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903}
18904
18905/*
18906 * List the value of one internal variable.
18907 */
18908 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018909list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018912 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018914 char_u *tofree;
18915 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018917
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018918 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018919 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018920 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018921 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922}
18923
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018925list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 char_u *prefix;
18927 char_u *name;
18928 int type;
18929 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018930 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931{
Bram Moolenaar31859182007-08-14 20:41:13 +000018932 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18933 msg_start();
18934 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 if (name != NULL) /* "a:" vars don't have a name stored */
18936 msg_puts(name);
18937 msg_putchar(' ');
18938 msg_advance(22);
18939 if (type == VAR_NUMBER)
18940 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018941 else if (type == VAR_FUNC)
18942 msg_putchar('*');
18943 else if (type == VAR_LIST)
18944 {
18945 msg_putchar('[');
18946 if (*string == '[')
18947 ++string;
18948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018949 else if (type == VAR_DICT)
18950 {
18951 msg_putchar('{');
18952 if (*string == '{')
18953 ++string;
18954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 else
18956 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018957
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018959
18960 if (type == VAR_FUNC)
18961 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018962 if (*first)
18963 {
18964 msg_clr_eos();
18965 *first = FALSE;
18966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967}
18968
18969/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018970 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 * If the variable already exists, the value is updated.
18972 * Otherwise the variable is created.
18973 */
18974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018977 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018978 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979{
Bram Moolenaar33570922005-01-25 22:26:29 +000018980 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018983 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018985 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018986 {
18987 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18988 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18989 ? name[2] : name[0]))
18990 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018991 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018992 return;
18993 }
18994 if (function_exists(name))
18995 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018996 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018997 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018998 return;
18999 }
19000 }
19001
Bram Moolenaara7043832005-01-21 11:56:39 +000019002 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019004 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019005 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019006 return;
19007 }
19008
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019009 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019010 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019013 if (var_check_ro(v->di_flags, name)
19014 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019015 return;
19016 if (v->di_tv.v_type != tv->v_type
19017 && !((v->di_tv.v_type == VAR_STRING
19018 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019019 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019020 || tv->v_type == VAR_NUMBER))
19021#ifdef FEAT_FLOAT
19022 && !((v->di_tv.v_type == VAR_NUMBER
19023 || v->di_tv.v_type == VAR_FLOAT)
19024 && (tv->v_type == VAR_NUMBER
19025 || tv->v_type == VAR_FLOAT))
19026#endif
19027 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019028 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019029 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019030 return;
19031 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019032
19033 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019034 * Handle setting internal v: variables separately: we don't change
19035 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019036 */
19037 if (ht == &vimvarht)
19038 {
19039 if (v->di_tv.v_type == VAR_STRING)
19040 {
19041 vim_free(v->di_tv.vval.v_string);
19042 if (copy || tv->v_type != VAR_STRING)
19043 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19044 else
19045 {
19046 /* Take over the string to avoid an extra alloc/free. */
19047 v->di_tv.vval.v_string = tv->vval.v_string;
19048 tv->vval.v_string = NULL;
19049 }
19050 }
19051 else if (v->di_tv.v_type != VAR_NUMBER)
19052 EMSG2(_(e_intern2), "set_var()");
19053 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019054 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019055 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019056 if (STRCMP(varname, "searchforward") == 0)
19057 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19058 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 return;
19060 }
19061
19062 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 }
19064 else /* add a new variable */
19065 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019066 /* Can't add "v:" variable. */
19067 if (ht == &vimvarht)
19068 {
19069 EMSG2(_(e_illvar), name);
19070 return;
19071 }
19072
Bram Moolenaar92124a32005-06-17 22:03:40 +000019073 /* Make sure the variable name is valid. */
19074 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019075 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19076 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019077 {
19078 EMSG2(_(e_illvar), varname);
19079 return;
19080 }
19081
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019082 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19083 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019084 if (v == NULL)
19085 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019089 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 return;
19091 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019092 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019095 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019097 else
19098 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019100 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103}
19104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019105/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019106 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 * Also give an error message.
19108 */
19109 static int
19110var_check_ro(flags, name)
19111 int flags;
19112 char_u *name;
19113{
19114 if (flags & DI_FLAGS_RO)
19115 {
19116 EMSG2(_(e_readonlyvar), name);
19117 return TRUE;
19118 }
19119 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19120 {
19121 EMSG2(_(e_readonlysbx), name);
19122 return TRUE;
19123 }
19124 return FALSE;
19125}
19126
19127/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019128 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19129 * Also give an error message.
19130 */
19131 static int
19132var_check_fixed(flags, name)
19133 int flags;
19134 char_u *name;
19135{
19136 if (flags & DI_FLAGS_FIX)
19137 {
19138 EMSG2(_("E795: Cannot delete variable %s"), name);
19139 return TRUE;
19140 }
19141 return FALSE;
19142}
19143
19144/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019145 * Return TRUE if typeval "tv" is set to be locked (immutable).
19146 * Also give an error message, using "name".
19147 */
19148 static int
19149tv_check_lock(lock, name)
19150 int lock;
19151 char_u *name;
19152{
19153 if (lock & VAR_LOCKED)
19154 {
19155 EMSG2(_("E741: Value is locked: %s"),
19156 name == NULL ? (char_u *)_("Unknown") : name);
19157 return TRUE;
19158 }
19159 if (lock & VAR_FIXED)
19160 {
19161 EMSG2(_("E742: Cannot change value of %s"),
19162 name == NULL ? (char_u *)_("Unknown") : name);
19163 return TRUE;
19164 }
19165 return FALSE;
19166}
19167
19168/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019169 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019170 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019171 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019175 typval_T *from;
19176 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019178 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019179 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019180 switch (from->v_type)
19181 {
19182 case VAR_NUMBER:
19183 to->vval.v_number = from->vval.v_number;
19184 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019185#ifdef FEAT_FLOAT
19186 case VAR_FLOAT:
19187 to->vval.v_float = from->vval.v_float;
19188 break;
19189#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019190 case VAR_STRING:
19191 case VAR_FUNC:
19192 if (from->vval.v_string == NULL)
19193 to->vval.v_string = NULL;
19194 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019195 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019196 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019197 if (from->v_type == VAR_FUNC)
19198 func_ref(to->vval.v_string);
19199 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019200 break;
19201 case VAR_LIST:
19202 if (from->vval.v_list == NULL)
19203 to->vval.v_list = NULL;
19204 else
19205 {
19206 to->vval.v_list = from->vval.v_list;
19207 ++to->vval.v_list->lv_refcount;
19208 }
19209 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019210 case VAR_DICT:
19211 if (from->vval.v_dict == NULL)
19212 to->vval.v_dict = NULL;
19213 else
19214 {
19215 to->vval.v_dict = from->vval.v_dict;
19216 ++to->vval.v_dict->dv_refcount;
19217 }
19218 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019219 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221 break;
19222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223}
19224
19225/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019226 * Make a copy of an item.
19227 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019228 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19229 * reference to an already copied list/dict can be used.
19230 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019231 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019232 static int
19233item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 typval_T *from;
19235 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019236 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019237 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019238{
19239 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019240 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019241
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019243 {
19244 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019245 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019246 }
19247 ++recurse;
19248
19249 switch (from->v_type)
19250 {
19251 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019252#ifdef FEAT_FLOAT
19253 case VAR_FLOAT:
19254#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019255 case VAR_STRING:
19256 case VAR_FUNC:
19257 copy_tv(from, to);
19258 break;
19259 case VAR_LIST:
19260 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019261 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019262 if (from->vval.v_list == NULL)
19263 to->vval.v_list = NULL;
19264 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19265 {
19266 /* use the copy made earlier */
19267 to->vval.v_list = from->vval.v_list->lv_copylist;
19268 ++to->vval.v_list->lv_refcount;
19269 }
19270 else
19271 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19272 if (to->vval.v_list == NULL)
19273 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019274 break;
19275 case VAR_DICT:
19276 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019277 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019278 if (from->vval.v_dict == NULL)
19279 to->vval.v_dict = NULL;
19280 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19281 {
19282 /* use the copy made earlier */
19283 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19284 ++to->vval.v_dict->dv_refcount;
19285 }
19286 else
19287 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19288 if (to->vval.v_dict == NULL)
19289 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019290 break;
19291 default:
19292 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019293 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019294 }
19295 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019296 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019297}
19298
19299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 * ":echo expr1 ..." print each argument separated with a space, add a
19301 * newline at the end.
19302 * ":echon expr1 ..." print each argument plain.
19303 */
19304 void
19305ex_echo(eap)
19306 exarg_T *eap;
19307{
19308 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019309 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019310 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 char_u *p;
19312 int needclr = TRUE;
19313 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019314 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315
19316 if (eap->skip)
19317 ++emsg_skip;
19318 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19319 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019320 /* If eval1() causes an error message the text from the command may
19321 * still need to be cleared. E.g., "echo 22,44". */
19322 need_clr_eos = needclr;
19323
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 {
19327 /*
19328 * Report the invalid expression unless the expression evaluation
19329 * has been cancelled due to an aborting error, an interrupt, or an
19330 * exception.
19331 */
19332 if (!aborting())
19333 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019334 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 break;
19336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019337 need_clr_eos = FALSE;
19338
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 if (!eap->skip)
19340 {
19341 if (atstart)
19342 {
19343 atstart = FALSE;
19344 /* Call msg_start() after eval1(), evaluating the expression
19345 * may cause a message to appear. */
19346 if (eap->cmdidx == CMD_echo)
19347 msg_start();
19348 }
19349 else if (eap->cmdidx == CMD_echo)
19350 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019351 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019352 if (p != NULL)
19353 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019355 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019357 if (*p != TAB && needclr)
19358 {
19359 /* remove any text still there from the command */
19360 msg_clr_eos();
19361 needclr = FALSE;
19362 }
19363 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 }
19365 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019366 {
19367#ifdef FEAT_MBYTE
19368 if (has_mbyte)
19369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019370 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019371
19372 (void)msg_outtrans_len_attr(p, i, echo_attr);
19373 p += i - 1;
19374 }
19375 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019377 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019380 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019382 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 arg = skipwhite(arg);
19384 }
19385 eap->nextcmd = check_nextcmd(arg);
19386
19387 if (eap->skip)
19388 --emsg_skip;
19389 else
19390 {
19391 /* remove text that may still be there from the command */
19392 if (needclr)
19393 msg_clr_eos();
19394 if (eap->cmdidx == CMD_echo)
19395 msg_end();
19396 }
19397}
19398
19399/*
19400 * ":echohl {name}".
19401 */
19402 void
19403ex_echohl(eap)
19404 exarg_T *eap;
19405{
19406 int id;
19407
19408 id = syn_name2id(eap->arg);
19409 if (id == 0)
19410 echo_attr = 0;
19411 else
19412 echo_attr = syn_id2attr(id);
19413}
19414
19415/*
19416 * ":execute expr1 ..." execute the result of an expression.
19417 * ":echomsg expr1 ..." Print a message
19418 * ":echoerr expr1 ..." Print an error
19419 * Each gets spaces around each argument and a newline at the end for
19420 * echo commands
19421 */
19422 void
19423ex_execute(eap)
19424 exarg_T *eap;
19425{
19426 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019427 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 int ret = OK;
19429 char_u *p;
19430 garray_T ga;
19431 int len;
19432 int save_did_emsg;
19433
19434 ga_init2(&ga, 1, 80);
19435
19436 if (eap->skip)
19437 ++emsg_skip;
19438 while (*arg != NUL && *arg != '|' && *arg != '\n')
19439 {
19440 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019441 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 {
19443 /*
19444 * Report the invalid expression unless the expression evaluation
19445 * has been cancelled due to an aborting error, an interrupt, or an
19446 * exception.
19447 */
19448 if (!aborting())
19449 EMSG2(_(e_invexpr2), p);
19450 ret = FAIL;
19451 break;
19452 }
19453
19454 if (!eap->skip)
19455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019456 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 len = (int)STRLEN(p);
19458 if (ga_grow(&ga, len + 2) == FAIL)
19459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 ret = FAIL;
19462 break;
19463 }
19464 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 ga.ga_len += len;
19468 }
19469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019470 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 arg = skipwhite(arg);
19472 }
19473
19474 if (ret != FAIL && ga.ga_data != NULL)
19475 {
19476 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019479 out_flush();
19480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 else if (eap->cmdidx == CMD_echoerr)
19482 {
19483 /* We don't want to abort following commands, restore did_emsg. */
19484 save_did_emsg = did_emsg;
19485 EMSG((char_u *)ga.ga_data);
19486 if (!force_abort)
19487 did_emsg = save_did_emsg;
19488 }
19489 else if (eap->cmdidx == CMD_execute)
19490 do_cmdline((char_u *)ga.ga_data,
19491 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19492 }
19493
19494 ga_clear(&ga);
19495
19496 if (eap->skip)
19497 --emsg_skip;
19498
19499 eap->nextcmd = check_nextcmd(arg);
19500}
19501
19502/*
19503 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19504 * "arg" points to the "&" or '+' when called, to "option" when returning.
19505 * Returns NULL when no option name found. Otherwise pointer to the char
19506 * after the option name.
19507 */
19508 static char_u *
19509find_option_end(arg, opt_flags)
19510 char_u **arg;
19511 int *opt_flags;
19512{
19513 char_u *p = *arg;
19514
19515 ++p;
19516 if (*p == 'g' && p[1] == ':')
19517 {
19518 *opt_flags = OPT_GLOBAL;
19519 p += 2;
19520 }
19521 else if (*p == 'l' && p[1] == ':')
19522 {
19523 *opt_flags = OPT_LOCAL;
19524 p += 2;
19525 }
19526 else
19527 *opt_flags = 0;
19528
19529 if (!ASCII_ISALPHA(*p))
19530 return NULL;
19531 *arg = p;
19532
19533 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19534 p += 4; /* termcap option */
19535 else
19536 while (ASCII_ISALPHA(*p))
19537 ++p;
19538 return p;
19539}
19540
19541/*
19542 * ":function"
19543 */
19544 void
19545ex_function(eap)
19546 exarg_T *eap;
19547{
19548 char_u *theline;
19549 int j;
19550 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 char_u *name = NULL;
19553 char_u *p;
19554 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019555 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 garray_T newargs;
19557 garray_T newlines;
19558 int varargs = FALSE;
19559 int mustend = FALSE;
19560 int flags = 0;
19561 ufunc_T *fp;
19562 int indent;
19563 int nesting;
19564 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 dictitem_T *v;
19566 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019567 static int func_nr = 0; /* number for nameless function */
19568 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019569 hashtab_T *ht;
19570 int todo;
19571 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019572 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573
19574 /*
19575 * ":function" without argument: list functions.
19576 */
19577 if (ends_excmd(*eap->arg))
19578 {
19579 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019580 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019581 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019582 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019583 {
19584 if (!HASHITEM_EMPTY(hi))
19585 {
19586 --todo;
19587 fp = HI2UF(hi);
19588 if (!isdigit(*fp->uf_name))
19589 list_func_head(fp, FALSE);
19590 }
19591 }
19592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 eap->nextcmd = check_nextcmd(eap->arg);
19594 return;
19595 }
19596
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019597 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019598 * ":function /pat": list functions matching pattern.
19599 */
19600 if (*eap->arg == '/')
19601 {
19602 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19603 if (!eap->skip)
19604 {
19605 regmatch_T regmatch;
19606
19607 c = *p;
19608 *p = NUL;
19609 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19610 *p = c;
19611 if (regmatch.regprog != NULL)
19612 {
19613 regmatch.rm_ic = p_ic;
19614
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019615 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019616 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19617 {
19618 if (!HASHITEM_EMPTY(hi))
19619 {
19620 --todo;
19621 fp = HI2UF(hi);
19622 if (!isdigit(*fp->uf_name)
19623 && vim_regexec(&regmatch, fp->uf_name, 0))
19624 list_func_head(fp, FALSE);
19625 }
19626 }
19627 }
19628 }
19629 if (*p == '/')
19630 ++p;
19631 eap->nextcmd = check_nextcmd(p);
19632 return;
19633 }
19634
19635 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019636 * Get the function name. There are these situations:
19637 * func normal function name
19638 * "name" == func, "fudi.fd_dict" == NULL
19639 * dict.func new dictionary entry
19640 * "name" == NULL, "fudi.fd_dict" set,
19641 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19642 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019643 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019644 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19645 * dict.func existing dict entry that's not a Funcref
19646 * "name" == NULL, "fudi.fd_dict" set,
19647 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019650 name = trans_function_name(&p, eap->skip, 0, &fudi);
19651 paren = (vim_strchr(p, '(') != NULL);
19652 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 {
19654 /*
19655 * Return on an invalid expression in braces, unless the expression
19656 * evaluation has been cancelled due to an aborting error, an
19657 * interrupt, or an exception.
19658 */
19659 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019661 if (!eap->skip && fudi.fd_newkey != NULL)
19662 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019663 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 else
19667 eap->skip = TRUE;
19668 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019669
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 /* An error in a function call during evaluation of an expression in magic
19671 * braces should not cause the function not to be defined. */
19672 saved_did_emsg = did_emsg;
19673 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674
19675 /*
19676 * ":function func" with only function name: list function.
19677 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019678 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 {
19680 if (!ends_excmd(*skipwhite(p)))
19681 {
19682 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019683 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 }
19685 eap->nextcmd = check_nextcmd(p);
19686 if (eap->nextcmd != NULL)
19687 *p = NUL;
19688 if (!eap->skip && !got_int)
19689 {
19690 fp = find_func(name);
19691 if (fp != NULL)
19692 {
19693 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019694 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019696 if (FUNCLINE(fp, j) == NULL)
19697 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 msg_putchar('\n');
19699 msg_outnum((long)(j + 1));
19700 if (j < 9)
19701 msg_putchar(' ');
19702 if (j < 99)
19703 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019704 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 out_flush(); /* show a line at a time */
19706 ui_breakcheck();
19707 }
19708 if (!got_int)
19709 {
19710 msg_putchar('\n');
19711 msg_puts((char_u *)" endfunction");
19712 }
19713 }
19714 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019715 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019717 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 }
19719
19720 /*
19721 * ":function name(arg1, arg2)" Define function.
19722 */
19723 p = skipwhite(p);
19724 if (*p != '(')
19725 {
19726 if (!eap->skip)
19727 {
19728 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019729 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 }
19731 /* attempt to continue by skipping some text */
19732 if (vim_strchr(p, '(') != NULL)
19733 p = vim_strchr(p, '(');
19734 }
19735 p = skipwhite(p + 1);
19736
19737 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19738 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19739
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019740 if (!eap->skip)
19741 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019742 /* Check the name of the function. Unless it's a dictionary function
19743 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019744 if (name != NULL)
19745 arg = name;
19746 else
19747 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019748 if (arg != NULL && (fudi.fd_di == NULL
19749 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019750 {
19751 if (*arg == K_SPECIAL)
19752 j = 3;
19753 else
19754 j = 0;
19755 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19756 : eval_isnamec(arg[j])))
19757 ++j;
19758 if (arg[j] != NUL)
19759 emsg_funcname(_(e_invarg2), arg);
19760 }
19761 }
19762
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 /*
19764 * Isolate the arguments: "arg1, arg2, ...)"
19765 */
19766 while (*p != ')')
19767 {
19768 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19769 {
19770 varargs = TRUE;
19771 p += 3;
19772 mustend = TRUE;
19773 }
19774 else
19775 {
19776 arg = p;
19777 while (ASCII_ISALNUM(*p) || *p == '_')
19778 ++p;
19779 if (arg == p || isdigit(*arg)
19780 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19781 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19782 {
19783 if (!eap->skip)
19784 EMSG2(_("E125: Illegal argument: %s"), arg);
19785 break;
19786 }
19787 if (ga_grow(&newargs, 1) == FAIL)
19788 goto erret;
19789 c = *p;
19790 *p = NUL;
19791 arg = vim_strsave(arg);
19792 if (arg == NULL)
19793 goto erret;
19794 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19795 *p = c;
19796 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 if (*p == ',')
19798 ++p;
19799 else
19800 mustend = TRUE;
19801 }
19802 p = skipwhite(p);
19803 if (mustend && *p != ')')
19804 {
19805 if (!eap->skip)
19806 EMSG2(_(e_invarg2), eap->arg);
19807 break;
19808 }
19809 }
19810 ++p; /* skip the ')' */
19811
Bram Moolenaare9a41262005-01-15 22:18:47 +000019812 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 for (;;)
19814 {
19815 p = skipwhite(p);
19816 if (STRNCMP(p, "range", 5) == 0)
19817 {
19818 flags |= FC_RANGE;
19819 p += 5;
19820 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019821 else if (STRNCMP(p, "dict", 4) == 0)
19822 {
19823 flags |= FC_DICT;
19824 p += 4;
19825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 else if (STRNCMP(p, "abort", 5) == 0)
19827 {
19828 flags |= FC_ABORT;
19829 p += 5;
19830 }
19831 else
19832 break;
19833 }
19834
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019835 /* When there is a line break use what follows for the function body.
19836 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19837 if (*p == '\n')
19838 line_arg = p + 1;
19839 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 EMSG(_(e_trailing));
19841
19842 /*
19843 * Read the body of the function, until ":endfunction" is found.
19844 */
19845 if (KeyTyped)
19846 {
19847 /* Check if the function already exists, don't let the user type the
19848 * whole function before telling him it doesn't work! For a script we
19849 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019850 if (!eap->skip && !eap->forceit)
19851 {
19852 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19853 EMSG(_(e_funcdict));
19854 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019855 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019858 if (!eap->skip && did_emsg)
19859 goto erret;
19860
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 msg_putchar('\n'); /* don't overwrite the function name */
19862 cmdline_row = msg_row;
19863 }
19864
19865 indent = 2;
19866 nesting = 0;
19867 for (;;)
19868 {
19869 msg_scroll = TRUE;
19870 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019871 sourcing_lnum_off = sourcing_lnum;
19872
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019873 if (line_arg != NULL)
19874 {
19875 /* Use eap->arg, split up in parts by line breaks. */
19876 theline = line_arg;
19877 p = vim_strchr(theline, '\n');
19878 if (p == NULL)
19879 line_arg += STRLEN(line_arg);
19880 else
19881 {
19882 *p = NUL;
19883 line_arg = p + 1;
19884 }
19885 }
19886 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 theline = getcmdline(':', 0L, indent);
19888 else
19889 theline = eap->getline(':', eap->cookie, indent);
19890 if (KeyTyped)
19891 lines_left = Rows - 1;
19892 if (theline == NULL)
19893 {
19894 EMSG(_("E126: Missing :endfunction"));
19895 goto erret;
19896 }
19897
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019898 /* Detect line continuation: sourcing_lnum increased more than one. */
19899 if (sourcing_lnum > sourcing_lnum_off + 1)
19900 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19901 else
19902 sourcing_lnum_off = 0;
19903
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 if (skip_until != NULL)
19905 {
19906 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19907 * don't check for ":endfunc". */
19908 if (STRCMP(theline, skip_until) == 0)
19909 {
19910 vim_free(skip_until);
19911 skip_until = NULL;
19912 }
19913 }
19914 else
19915 {
19916 /* skip ':' and blanks*/
19917 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19918 ;
19919
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019920 /* Check for "endfunction". */
19921 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019923 if (line_arg == NULL)
19924 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 break;
19926 }
19927
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019928 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 * at "end". */
19930 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19931 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019932 else if (STRNCMP(p, "if", 2) == 0
19933 || STRNCMP(p, "wh", 2) == 0
19934 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 || STRNCMP(p, "try", 3) == 0)
19936 indent += 2;
19937
19938 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019939 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019941 if (*p == '!')
19942 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 p += eval_fname_script(p);
19944 if (ASCII_ISALPHA(*p))
19945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019946 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 if (*skipwhite(p) == '(')
19948 {
19949 ++nesting;
19950 indent += 2;
19951 }
19952 }
19953 }
19954
19955 /* Check for ":append" or ":insert". */
19956 p = skip_range(p, NULL);
19957 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19958 || (p[0] == 'i'
19959 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19960 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19961 skip_until = vim_strsave((char_u *)".");
19962
19963 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19964 arg = skipwhite(skiptowhite(p));
19965 if (arg[0] == '<' && arg[1] =='<'
19966 && ((p[0] == 'p' && p[1] == 'y'
19967 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19968 || (p[0] == 'p' && p[1] == 'e'
19969 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19970 || (p[0] == 't' && p[1] == 'c'
19971 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19972 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19973 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019974 || (p[0] == 'm' && p[1] == 'z'
19975 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 ))
19977 {
19978 /* ":python <<" continues until a dot, like ":append" */
19979 p = skipwhite(arg + 2);
19980 if (*p == NUL)
19981 skip_until = vim_strsave((char_u *)".");
19982 else
19983 skip_until = vim_strsave(p);
19984 }
19985 }
19986
19987 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019988 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019989 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019990 if (line_arg == NULL)
19991 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019993 }
19994
19995 /* Copy the line to newly allocated memory. get_one_sourceline()
19996 * allocates 250 bytes per line, this saves 80% on average. The cost
19997 * is an extra alloc/free. */
19998 p = vim_strsave(theline);
19999 if (p != NULL)
20000 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020001 if (line_arg == NULL)
20002 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020003 theline = p;
20004 }
20005
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020006 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20007
20008 /* Add NULL lines for continuation lines, so that the line count is
20009 * equal to the index in the growarray. */
20010 while (sourcing_lnum_off-- > 0)
20011 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020012
20013 /* Check for end of eap->arg. */
20014 if (line_arg != NULL && *line_arg == NUL)
20015 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 }
20017
20018 /* Don't define the function when skipping commands or when an error was
20019 * detected. */
20020 if (eap->skip || did_emsg)
20021 goto erret;
20022
20023 /*
20024 * If there are no errors, add the function
20025 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020026 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020027 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020028 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020030 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020031 emsg_funcname("E707: Function name conflicts with variable: %s",
20032 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020033 goto erret;
20034 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020035
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020036 fp = find_func(name);
20037 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020039 if (!eap->forceit)
20040 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020041 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020042 goto erret;
20043 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020044 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020046 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020047 name);
20048 goto erret;
20049 }
20050 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020051 ga_clear_strings(&(fp->uf_args));
20052 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020053 vim_free(name);
20054 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 }
20057 else
20058 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020059 char numbuf[20];
20060
20061 fp = NULL;
20062 if (fudi.fd_newkey == NULL && !eap->forceit)
20063 {
20064 EMSG(_(e_funcdict));
20065 goto erret;
20066 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020067 if (fudi.fd_di == NULL)
20068 {
20069 /* Can't add a function to a locked dictionary */
20070 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20071 goto erret;
20072 }
20073 /* Can't change an existing function if it is locked */
20074 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20075 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020076
20077 /* Give the function a sequential number. Can only be used with a
20078 * Funcref! */
20079 vim_free(name);
20080 sprintf(numbuf, "%d", ++func_nr);
20081 name = vim_strsave((char_u *)numbuf);
20082 if (name == NULL)
20083 goto erret;
20084 }
20085
20086 if (fp == NULL)
20087 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020088 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020089 {
20090 int slen, plen;
20091 char_u *scriptname;
20092
20093 /* Check that the autoload name matches the script name. */
20094 j = FAIL;
20095 if (sourcing_name != NULL)
20096 {
20097 scriptname = autoload_name(name);
20098 if (scriptname != NULL)
20099 {
20100 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020101 plen = (int)STRLEN(p);
20102 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020103 if (slen > plen && fnamecmp(p,
20104 sourcing_name + slen - plen) == 0)
20105 j = OK;
20106 vim_free(scriptname);
20107 }
20108 }
20109 if (j == FAIL)
20110 {
20111 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20112 goto erret;
20113 }
20114 }
20115
20116 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 if (fp == NULL)
20118 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020119
20120 if (fudi.fd_dict != NULL)
20121 {
20122 if (fudi.fd_di == NULL)
20123 {
20124 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020125 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020126 if (fudi.fd_di == NULL)
20127 {
20128 vim_free(fp);
20129 goto erret;
20130 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020131 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20132 {
20133 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020134 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020135 goto erret;
20136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020137 }
20138 else
20139 /* overwrite existing dict entry */
20140 clear_tv(&fudi.fd_di->di_tv);
20141 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020142 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020143 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020144 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020145
20146 /* behave like "dict" was used */
20147 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020148 }
20149
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020151 STRCPY(fp->uf_name, name);
20152 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020154 fp->uf_args = newargs;
20155 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020156#ifdef FEAT_PROFILE
20157 fp->uf_tml_count = NULL;
20158 fp->uf_tml_total = NULL;
20159 fp->uf_tml_self = NULL;
20160 fp->uf_profiling = FALSE;
20161 if (prof_def_func())
20162 func_do_profile(fp);
20163#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020164 fp->uf_varargs = varargs;
20165 fp->uf_flags = flags;
20166 fp->uf_calls = 0;
20167 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020168 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169
20170erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 ga_clear_strings(&newargs);
20172 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020173ret_free:
20174 vim_free(skip_until);
20175 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178}
20179
20180/*
20181 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020182 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020184 * flags:
20185 * TFN_INT: internal function name OK
20186 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 * Advances "pp" to just after the function name (if no error).
20188 */
20189 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020190trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 char_u **pp;
20192 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020193 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020196 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 char_u *start;
20198 char_u *end;
20199 int lead;
20200 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020203
20204 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020205 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020207
20208 /* Check for hard coded <SNR>: already translated function ID (from a user
20209 * command). */
20210 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20211 && (*pp)[2] == (int)KE_SNR)
20212 {
20213 *pp += 3;
20214 len = get_id_len(pp) + 3;
20215 return vim_strnsave(start, len);
20216 }
20217
20218 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20219 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020221 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020223
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020224 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20225 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020226 if (end == start)
20227 {
20228 if (!skip)
20229 EMSG(_("E129: Function name required"));
20230 goto theend;
20231 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020232 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020233 {
20234 /*
20235 * Report an invalid expression in braces, unless the expression
20236 * evaluation has been cancelled due to an aborting error, an
20237 * interrupt, or an exception.
20238 */
20239 if (!aborting())
20240 {
20241 if (end != NULL)
20242 EMSG2(_(e_invarg2), start);
20243 }
20244 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020245 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020246 goto theend;
20247 }
20248
20249 if (lv.ll_tv != NULL)
20250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020251 if (fdp != NULL)
20252 {
20253 fdp->fd_dict = lv.ll_dict;
20254 fdp->fd_newkey = lv.ll_newkey;
20255 lv.ll_newkey = NULL;
20256 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020257 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020258 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20259 {
20260 name = vim_strsave(lv.ll_tv->vval.v_string);
20261 *pp = end;
20262 }
20263 else
20264 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020265 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20266 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020267 EMSG(_(e_funcref));
20268 else
20269 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020270 name = NULL;
20271 }
20272 goto theend;
20273 }
20274
20275 if (lv.ll_name == NULL)
20276 {
20277 /* Error found, but continue after the function name. */
20278 *pp = end;
20279 goto theend;
20280 }
20281
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020282 /* Check if the name is a Funcref. If so, use the value. */
20283 if (lv.ll_exp_name != NULL)
20284 {
20285 len = (int)STRLEN(lv.ll_exp_name);
20286 name = deref_func_name(lv.ll_exp_name, &len);
20287 if (name == lv.ll_exp_name)
20288 name = NULL;
20289 }
20290 else
20291 {
20292 len = (int)(end - *pp);
20293 name = deref_func_name(*pp, &len);
20294 if (name == *pp)
20295 name = NULL;
20296 }
20297 if (name != NULL)
20298 {
20299 name = vim_strsave(name);
20300 *pp = end;
20301 goto theend;
20302 }
20303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020304 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020305 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020306 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020307 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20308 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20309 {
20310 /* When there was "s:" already or the name expanded to get a
20311 * leading "s:" then remove it. */
20312 lv.ll_name += 2;
20313 len -= 2;
20314 lead = 2;
20315 }
20316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020317 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020318 {
20319 if (lead == 2) /* skip over "s:" */
20320 lv.ll_name += 2;
20321 len = (int)(end - lv.ll_name);
20322 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020323
20324 /*
20325 * Copy the function name to allocated memory.
20326 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20327 * Accept <SNR>123_name() outside a script.
20328 */
20329 if (skip)
20330 lead = 0; /* do nothing */
20331 else if (lead > 0)
20332 {
20333 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020334 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20335 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020336 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020337 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020338 if (current_SID <= 0)
20339 {
20340 EMSG(_(e_usingsid));
20341 goto theend;
20342 }
20343 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20344 lead += (int)STRLEN(sid_buf);
20345 }
20346 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020347 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020348 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020349 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020350 goto theend;
20351 }
20352 name = alloc((unsigned)(len + lead + 1));
20353 if (name != NULL)
20354 {
20355 if (lead > 0)
20356 {
20357 name[0] = K_SPECIAL;
20358 name[1] = KS_EXTRA;
20359 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020360 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020361 STRCPY(name + 3, sid_buf);
20362 }
20363 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20364 name[len + lead] = NUL;
20365 }
20366 *pp = end;
20367
20368theend:
20369 clear_lval(&lv);
20370 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371}
20372
20373/*
20374 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20375 * Return 2 if "p" starts with "s:".
20376 * Return 0 otherwise.
20377 */
20378 static int
20379eval_fname_script(p)
20380 char_u *p;
20381{
20382 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20383 || STRNICMP(p + 1, "SNR>", 4) == 0))
20384 return 5;
20385 if (p[0] == 's' && p[1] == ':')
20386 return 2;
20387 return 0;
20388}
20389
20390/*
20391 * Return TRUE if "p" starts with "<SID>" or "s:".
20392 * Only works if eval_fname_script() returned non-zero for "p"!
20393 */
20394 static int
20395eval_fname_sid(p)
20396 char_u *p;
20397{
20398 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20399}
20400
20401/*
20402 * List the head of the function: "name(arg1, arg2)".
20403 */
20404 static void
20405list_func_head(fp, indent)
20406 ufunc_T *fp;
20407 int indent;
20408{
20409 int j;
20410
20411 msg_start();
20412 if (indent)
20413 MSG_PUTS(" ");
20414 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020415 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 {
20417 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020418 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 }
20420 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020421 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020423 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
20425 if (j)
20426 MSG_PUTS(", ");
20427 msg_puts(FUNCARG(fp, j));
20428 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020429 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 {
20431 if (j)
20432 MSG_PUTS(", ");
20433 MSG_PUTS("...");
20434 }
20435 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020436 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020437 if (p_verbose > 0)
20438 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439}
20440
20441/*
20442 * Find a function by name, return pointer to it in ufuncs.
20443 * Return NULL for unknown function.
20444 */
20445 static ufunc_T *
20446find_func(name)
20447 char_u *name;
20448{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020449 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020451 hi = hash_find(&func_hashtab, name);
20452 if (!HASHITEM_EMPTY(hi))
20453 return HI2UF(hi);
20454 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455}
20456
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020457#if defined(EXITFREE) || defined(PROTO)
20458 void
20459free_all_functions()
20460{
20461 hashitem_T *hi;
20462
20463 /* Need to start all over every time, because func_free() may change the
20464 * hash table. */
20465 while (func_hashtab.ht_used > 0)
20466 for (hi = func_hashtab.ht_array; ; ++hi)
20467 if (!HASHITEM_EMPTY(hi))
20468 {
20469 func_free(HI2UF(hi));
20470 break;
20471 }
20472}
20473#endif
20474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020475/*
20476 * Return TRUE if a function "name" exists.
20477 */
20478 static int
20479function_exists(name)
20480 char_u *name;
20481{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020482 char_u *nm = name;
20483 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020484 int n = FALSE;
20485
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020486 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020487 nm = skipwhite(nm);
20488
20489 /* Only accept "funcname", "funcname ", "funcname (..." and
20490 * "funcname(...", not "funcname!...". */
20491 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020492 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020493 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020495 else
20496 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020497 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020498 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020499 return n;
20500}
20501
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020502/*
20503 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020504 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020505 */
20506 static int
20507builtin_function(name)
20508 char_u *name;
20509{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020510 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20511 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020512}
20513
Bram Moolenaar05159a02005-02-26 23:04:13 +000020514#if defined(FEAT_PROFILE) || defined(PROTO)
20515/*
20516 * Start profiling function "fp".
20517 */
20518 static void
20519func_do_profile(fp)
20520 ufunc_T *fp;
20521{
20522 fp->uf_tm_count = 0;
20523 profile_zero(&fp->uf_tm_self);
20524 profile_zero(&fp->uf_tm_total);
20525 if (fp->uf_tml_count == NULL)
20526 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20527 (sizeof(int) * fp->uf_lines.ga_len));
20528 if (fp->uf_tml_total == NULL)
20529 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20530 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20531 if (fp->uf_tml_self == NULL)
20532 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20533 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20534 fp->uf_tml_idx = -1;
20535 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20536 || fp->uf_tml_self == NULL)
20537 return; /* out of memory */
20538
20539 fp->uf_profiling = TRUE;
20540}
20541
20542/*
20543 * Dump the profiling results for all functions in file "fd".
20544 */
20545 void
20546func_dump_profile(fd)
20547 FILE *fd;
20548{
20549 hashitem_T *hi;
20550 int todo;
20551 ufunc_T *fp;
20552 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020553 ufunc_T **sorttab;
20554 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020555
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020556 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000020557 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20558
Bram Moolenaar05159a02005-02-26 23:04:13 +000020559 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20560 {
20561 if (!HASHITEM_EMPTY(hi))
20562 {
20563 --todo;
20564 fp = HI2UF(hi);
20565 if (fp->uf_profiling)
20566 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020567 if (sorttab != NULL)
20568 sorttab[st_len++] = fp;
20569
Bram Moolenaar05159a02005-02-26 23:04:13 +000020570 if (fp->uf_name[0] == K_SPECIAL)
20571 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20572 else
20573 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20574 if (fp->uf_tm_count == 1)
20575 fprintf(fd, "Called 1 time\n");
20576 else
20577 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20578 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20579 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20580 fprintf(fd, "\n");
20581 fprintf(fd, "count total (s) self (s)\n");
20582
20583 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20584 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020585 if (FUNCLINE(fp, i) == NULL)
20586 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020587 prof_func_line(fd, fp->uf_tml_count[i],
20588 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020589 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20590 }
20591 fprintf(fd, "\n");
20592 }
20593 }
20594 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020595
20596 if (sorttab != NULL && st_len > 0)
20597 {
20598 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20599 prof_total_cmp);
20600 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20601 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20602 prof_self_cmp);
20603 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20604 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020605}
Bram Moolenaar73830342005-02-28 22:48:19 +000020606
20607 static void
20608prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20609 FILE *fd;
20610 ufunc_T **sorttab;
20611 int st_len;
20612 char *title;
20613 int prefer_self; /* when equal print only self time */
20614{
20615 int i;
20616 ufunc_T *fp;
20617
20618 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20619 fprintf(fd, "count total (s) self (s) function\n");
20620 for (i = 0; i < 20 && i < st_len; ++i)
20621 {
20622 fp = sorttab[i];
20623 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20624 prefer_self);
20625 if (fp->uf_name[0] == K_SPECIAL)
20626 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20627 else
20628 fprintf(fd, " %s()\n", fp->uf_name);
20629 }
20630 fprintf(fd, "\n");
20631}
20632
20633/*
20634 * Print the count and times for one function or function line.
20635 */
20636 static void
20637prof_func_line(fd, count, total, self, prefer_self)
20638 FILE *fd;
20639 int count;
20640 proftime_T *total;
20641 proftime_T *self;
20642 int prefer_self; /* when equal print only self time */
20643{
20644 if (count > 0)
20645 {
20646 fprintf(fd, "%5d ", count);
20647 if (prefer_self && profile_equal(total, self))
20648 fprintf(fd, " ");
20649 else
20650 fprintf(fd, "%s ", profile_msg(total));
20651 if (!prefer_self && profile_equal(total, self))
20652 fprintf(fd, " ");
20653 else
20654 fprintf(fd, "%s ", profile_msg(self));
20655 }
20656 else
20657 fprintf(fd, " ");
20658}
20659
20660/*
20661 * Compare function for total time sorting.
20662 */
20663 static int
20664#ifdef __BORLANDC__
20665_RTLENTRYF
20666#endif
20667prof_total_cmp(s1, s2)
20668 const void *s1;
20669 const void *s2;
20670{
20671 ufunc_T *p1, *p2;
20672
20673 p1 = *(ufunc_T **)s1;
20674 p2 = *(ufunc_T **)s2;
20675 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20676}
20677
20678/*
20679 * Compare function for self time sorting.
20680 */
20681 static int
20682#ifdef __BORLANDC__
20683_RTLENTRYF
20684#endif
20685prof_self_cmp(s1, s2)
20686 const void *s1;
20687 const void *s2;
20688{
20689 ufunc_T *p1, *p2;
20690
20691 p1 = *(ufunc_T **)s1;
20692 p2 = *(ufunc_T **)s2;
20693 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20694}
20695
Bram Moolenaar05159a02005-02-26 23:04:13 +000020696#endif
20697
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020698/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020699 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020700 * Return TRUE if a package was loaded.
20701 */
20702 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020703script_autoload(name, reload)
20704 char_u *name;
20705 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020706{
20707 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020708 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020709 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020710 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020712 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020713 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020714 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020715 return FALSE;
20716
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020717 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020718
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020719 /* Find the name in the list of previously loaded package names. Skip
20720 * "autoload/", it's always the same. */
20721 for (i = 0; i < ga_loaded.ga_len; ++i)
20722 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20723 break;
20724 if (!reload && i < ga_loaded.ga_len)
20725 ret = FALSE; /* was loaded already */
20726 else
20727 {
20728 /* Remember the name if it wasn't loaded already. */
20729 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20730 {
20731 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20732 tofree = NULL;
20733 }
20734
20735 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020736 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020737 ret = TRUE;
20738 }
20739
20740 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020741 return ret;
20742}
20743
20744/*
20745 * Return the autoload script name for a function or variable name.
20746 * Returns NULL when out of memory.
20747 */
20748 static char_u *
20749autoload_name(name)
20750 char_u *name;
20751{
20752 char_u *p;
20753 char_u *scriptname;
20754
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020755 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020756 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20757 if (scriptname == NULL)
20758 return FALSE;
20759 STRCPY(scriptname, "autoload/");
20760 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020761 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020762 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020763 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020764 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020765 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020766}
20767
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20769
20770/*
20771 * Function given to ExpandGeneric() to obtain the list of user defined
20772 * function names.
20773 */
20774 char_u *
20775get_user_func_name(xp, idx)
20776 expand_T *xp;
20777 int idx;
20778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020779 static long_u done;
20780 static hashitem_T *hi;
20781 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782
20783 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020785 done = 0;
20786 hi = func_hashtab.ht_array;
20787 }
20788 if (done < func_hashtab.ht_used)
20789 {
20790 if (done++ > 0)
20791 ++hi;
20792 while (HASHITEM_EMPTY(hi))
20793 ++hi;
20794 fp = HI2UF(hi);
20795
20796 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20797 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798
20799 cat_func_name(IObuff, fp);
20800 if (xp->xp_context != EXPAND_USER_FUNC)
20801 {
20802 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020803 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 STRCAT(IObuff, ")");
20805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 return IObuff;
20807 }
20808 return NULL;
20809}
20810
20811#endif /* FEAT_CMDL_COMPL */
20812
20813/*
20814 * Copy the function name of "fp" to buffer "buf".
20815 * "buf" must be able to hold the function name plus three bytes.
20816 * Takes care of script-local function names.
20817 */
20818 static void
20819cat_func_name(buf, fp)
20820 char_u *buf;
20821 ufunc_T *fp;
20822{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020823 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 {
20825 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020826 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 }
20828 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020829 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830}
20831
20832/*
20833 * ":delfunction {name}"
20834 */
20835 void
20836ex_delfunction(eap)
20837 exarg_T *eap;
20838{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020839 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 char_u *p;
20841 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020842 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843
20844 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020845 name = trans_function_name(&p, eap->skip, 0, &fudi);
20846 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020848 {
20849 if (fudi.fd_dict != NULL && !eap->skip)
20850 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 if (!ends_excmd(*skipwhite(p)))
20854 {
20855 vim_free(name);
20856 EMSG(_(e_trailing));
20857 return;
20858 }
20859 eap->nextcmd = check_nextcmd(p);
20860 if (eap->nextcmd != NULL)
20861 *p = NUL;
20862
20863 if (!eap->skip)
20864 fp = find_func(name);
20865 vim_free(name);
20866
20867 if (!eap->skip)
20868 {
20869 if (fp == NULL)
20870 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020871 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 return;
20873 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020874 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 {
20876 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20877 return;
20878 }
20879
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020880 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020882 /* Delete the dict item that refers to the function, it will
20883 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020884 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020886 else
20887 func_free(fp);
20888 }
20889}
20890
20891/*
20892 * Free a function and remove it from the list of functions.
20893 */
20894 static void
20895func_free(fp)
20896 ufunc_T *fp;
20897{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020898 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020899
20900 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020901 ga_clear_strings(&(fp->uf_args));
20902 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020903#ifdef FEAT_PROFILE
20904 vim_free(fp->uf_tml_count);
20905 vim_free(fp->uf_tml_total);
20906 vim_free(fp->uf_tml_self);
20907#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020909 /* remove the function from the function hashtable */
20910 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20911 if (HASHITEM_EMPTY(hi))
20912 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020913 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020914 hash_remove(&func_hashtab, hi);
20915
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020916 vim_free(fp);
20917}
20918
20919/*
20920 * Unreference a Function: decrement the reference count and free it when it
20921 * becomes zero. Only for numbered functions.
20922 */
20923 static void
20924func_unref(name)
20925 char_u *name;
20926{
20927 ufunc_T *fp;
20928
20929 if (name != NULL && isdigit(*name))
20930 {
20931 fp = find_func(name);
20932 if (fp == NULL)
20933 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020934 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020935 {
20936 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020937 * when "uf_calls" becomes zero. */
20938 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 func_free(fp);
20940 }
20941 }
20942}
20943
20944/*
20945 * Count a reference to a Function.
20946 */
20947 static void
20948func_ref(name)
20949 char_u *name;
20950{
20951 ufunc_T *fp;
20952
20953 if (name != NULL && isdigit(*name))
20954 {
20955 fp = find_func(name);
20956 if (fp == NULL)
20957 EMSG2(_(e_intern2), "func_ref()");
20958 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020959 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 }
20961}
20962
20963/*
20964 * Call a user function.
20965 */
20966 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020967call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 ufunc_T *fp; /* pointer to function */
20969 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020970 typval_T *argvars; /* arguments */
20971 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 linenr_T firstline; /* first line of range */
20973 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020974 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975{
Bram Moolenaar33570922005-01-25 22:26:29 +000020976 char_u *save_sourcing_name;
20977 linenr_T save_sourcing_lnum;
20978 scid_T save_current_SID;
20979 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020980 int save_did_emsg;
20981 static int depth = 0;
20982 dictitem_T *v;
20983 int fixvar_idx = 0; /* index in fixvar[] */
20984 int i;
20985 int ai;
20986 char_u numbuf[NUMBUFLEN];
20987 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020988#ifdef FEAT_PROFILE
20989 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020990 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992
20993 /* If depth of calling is getting too high, don't execute the function */
20994 if (depth >= p_mfd)
20995 {
20996 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020997 rettv->v_type = VAR_NUMBER;
20998 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 return;
21000 }
21001 ++depth;
21002
21003 line_breakcheck(); /* check for CTRL-C hit */
21004
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021005 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000021006 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021008 fc.rettv = rettv;
21009 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 fc.linenr = 0;
21011 fc.returned = FALSE;
21012 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021014 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 fc.dbg_tick = debug_tick;
21016
Bram Moolenaar33570922005-01-25 22:26:29 +000021017 /*
21018 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21019 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21020 * each argument variable and saves a lot of time.
21021 */
21022 /*
21023 * Init l: variables.
21024 */
21025 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021026 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021027 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021028 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21029 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021030 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021031 name = v->di_key;
21032 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
21034 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
21035 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021036 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021037 v->di_tv.vval.v_dict = selfdict;
21038 ++selfdict->dv_refcount;
21039 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021040
Bram Moolenaar33570922005-01-25 22:26:29 +000021041 /*
21042 * Init a: variables.
21043 * Set a:0 to "argcount".
21044 * Set a:000 to a list with room for the "..." arguments.
21045 */
21046 init_var_dict(&fc.l_avars, &fc.l_avars_var);
21047 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021048 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000021049 v = &fc.fixvar[fixvar_idx++].var;
21050 STRCPY(v->di_key, "000");
21051 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21052 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21053 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021054 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021055 v->di_tv.vval.v_list = &fc.l_varlist;
21056 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
21057 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000021058 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021059
21060 /*
21061 * Set a:firstline to "firstline" and a:lastline to "lastline".
21062 * Set a:name to named arguments.
21063 * Set a:N to the "..." arguments.
21064 */
21065 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
21066 (varnumber_T)firstline);
21067 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
21068 (varnumber_T)lastline);
21069 for (i = 0; i < argcount; ++i)
21070 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021071 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021072 if (ai < 0)
21073 /* named argument a:name */
21074 name = FUNCARG(fp, i);
21075 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021076 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 /* "..." argument a:1, a:2, etc. */
21078 sprintf((char *)numbuf, "%d", ai + 1);
21079 name = numbuf;
21080 }
21081 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21082 {
21083 v = &fc.fixvar[fixvar_idx++].var;
21084 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21085 }
21086 else
21087 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021088 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21089 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021090 if (v == NULL)
21091 break;
21092 v->di_flags = DI_FLAGS_RO;
21093 }
21094 STRCPY(v->di_key, name);
21095 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21096
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021097 /* Note: the values are copied directly to avoid alloc/free.
21098 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021099 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021100 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021101
21102 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21103 {
21104 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
21105 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021106 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021107 }
21108 }
21109
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 /* Don't redraw while executing the function. */
21111 ++RedrawingDisabled;
21112 save_sourcing_name = sourcing_name;
21113 save_sourcing_lnum = sourcing_lnum;
21114 sourcing_lnum = 1;
21115 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021116 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 if (sourcing_name != NULL)
21118 {
21119 if (save_sourcing_name != NULL
21120 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21121 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21122 else
21123 STRCPY(sourcing_name, "function ");
21124 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21125
21126 if (p_verbose >= 12)
21127 {
21128 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021129 verbose_enter_scroll();
21130
Bram Moolenaar555b2802005-05-19 21:08:39 +000021131 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 if (p_verbose >= 14)
21133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021135 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021136 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021137 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138
21139 msg_puts((char_u *)"(");
21140 for (i = 0; i < argcount; ++i)
21141 {
21142 if (i > 0)
21143 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021144 if (argvars[i].v_type == VAR_NUMBER)
21145 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 else
21147 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021148 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21149 if (s != NULL)
21150 {
21151 trunc_string(s, buf, MSG_BUF_CLEN);
21152 msg_puts(buf);
21153 vim_free(tofree);
21154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 }
21156 }
21157 msg_puts((char_u *)")");
21158 }
21159 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021160
21161 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 --no_wait_return;
21163 }
21164 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021165#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021166 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021167 {
21168 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21169 func_do_profile(fp);
21170 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021171 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021172 {
21173 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021174 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021175 profile_zero(&fp->uf_tm_children);
21176 }
21177 script_prof_save(&wait_start);
21178 }
21179#endif
21180
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021182 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 save_did_emsg = did_emsg;
21184 did_emsg = FALSE;
21185
21186 /* call do_cmdline() to execute the lines */
21187 do_cmdline(NULL, get_func_line, (void *)&fc,
21188 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21189
21190 --RedrawingDisabled;
21191
21192 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021193 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 clear_tv(rettv);
21196 rettv->v_type = VAR_NUMBER;
21197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 }
21199
Bram Moolenaar05159a02005-02-26 23:04:13 +000021200#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021201 if (do_profiling == PROF_YES && (fp->uf_profiling
21202 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021203 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021204 profile_end(&call_start);
21205 profile_sub_wait(&wait_start, &call_start);
21206 profile_add(&fp->uf_tm_total, &call_start);
21207 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021208 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021209 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021210 profile_add(&fc.caller->func->uf_tm_children, &call_start);
21211 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021212 }
21213 }
21214#endif
21215
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 /* when being verbose, mention the return value */
21217 if (p_verbose >= 12)
21218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021220 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021223 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021224 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021225 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
21226 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021229 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021230 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021231 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021232 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021233
Bram Moolenaar555b2802005-05-19 21:08:39 +000021234 /* The value may be very long. Skip the middle part, so that we
21235 * have some idea how it starts and ends. smsg() would always
21236 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021237 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
21238 if (s != NULL)
21239 {
21240 trunc_string(s, buf, MSG_BUF_CLEN);
21241 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21242 vim_free(tofree);
21243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 }
21245 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021246
21247 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 --no_wait_return;
21249 }
21250
21251 vim_free(sourcing_name);
21252 sourcing_name = save_sourcing_name;
21253 sourcing_lnum = save_sourcing_lnum;
21254 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021255#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021256 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021257 script_prof_restore(&wait_start);
21258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259
21260 if (p_verbose >= 12 && sourcing_name != NULL)
21261 {
21262 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021263 verbose_enter_scroll();
21264
Bram Moolenaar555b2802005-05-19 21:08:39 +000021265 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021267
21268 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 --no_wait_return;
21270 }
21271
21272 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021273 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021275 /* The a: variables typevals were not allocated, only free the allocated
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 * variables. */
21277 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
21278
21279 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 --depth;
21281}
21282
21283/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 * Add a number variable "name" to dict "dp" with value "nr".
21285 */
21286 static void
21287add_nr_var(dp, v, name, nr)
21288 dict_T *dp;
21289 dictitem_T *v;
21290 char *name;
21291 varnumber_T nr;
21292{
21293 STRCPY(v->di_key, name);
21294 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21295 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21296 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021297 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 v->di_tv.vval.v_number = nr;
21299}
21300
21301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 * ":return [expr]"
21303 */
21304 void
21305ex_return(eap)
21306 exarg_T *eap;
21307{
21308 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021309 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 int returning = FALSE;
21311
21312 if (current_funccal == NULL)
21313 {
21314 EMSG(_("E133: :return not inside a function"));
21315 return;
21316 }
21317
21318 if (eap->skip)
21319 ++emsg_skip;
21320
21321 eap->nextcmd = NULL;
21322 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021323 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 {
21325 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021326 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 }
21330 /* It's safer to return also on error. */
21331 else if (!eap->skip)
21332 {
21333 /*
21334 * Return unless the expression evaluation has been cancelled due to an
21335 * aborting error, an interrupt, or an exception.
21336 */
21337 if (!aborting())
21338 returning = do_return(eap, FALSE, TRUE, NULL);
21339 }
21340
21341 /* When skipping or the return gets pending, advance to the next command
21342 * in this line (!returning). Otherwise, ignore the rest of the line.
21343 * Following lines will be ignored by get_func_line(). */
21344 if (returning)
21345 eap->nextcmd = NULL;
21346 else if (eap->nextcmd == NULL) /* no argument */
21347 eap->nextcmd = check_nextcmd(arg);
21348
21349 if (eap->skip)
21350 --emsg_skip;
21351}
21352
21353/*
21354 * Return from a function. Possibly makes the return pending. Also called
21355 * for a pending return at the ":endtry" or after returning from an extra
21356 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021357 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021358 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 * FALSE when the return gets pending.
21360 */
21361 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 exarg_T *eap;
21364 int reanimate;
21365 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021366 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367{
21368 int idx;
21369 struct condstack *cstack = eap->cstack;
21370
21371 if (reanimate)
21372 /* Undo the return. */
21373 current_funccal->returned = FALSE;
21374
21375 /*
21376 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21377 * not in its finally clause (which then is to be executed next) is found.
21378 * In this case, make the ":return" pending for execution at the ":endtry".
21379 * Otherwise, return normally.
21380 */
21381 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21382 if (idx >= 0)
21383 {
21384 cstack->cs_pending[idx] = CSTP_RETURN;
21385
21386 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021387 /* A pending return again gets pending. "rettv" points to an
21388 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021390 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 else
21392 {
21393 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021394 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 {
21400 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021401 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 else
21404 EMSG(_(e_outofmem));
21405 }
21406 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408
21409 if (reanimate)
21410 {
21411 /* The pending return value could be overwritten by a ":return"
21412 * without argument in a finally clause; reset the default
21413 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021414 current_funccal->rettv->v_type = VAR_NUMBER;
21415 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 }
21417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 }
21420 else
21421 {
21422 current_funccal->returned = TRUE;
21423
21424 /* If the return is carried out now, store the return value. For
21425 * a return immediately after reanimation, the value is already
21426 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021427 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021430 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021432 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 }
21434 }
21435
21436 return idx < 0;
21437}
21438
21439/*
21440 * Free the variable with a pending return value.
21441 */
21442 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021443discard_pending_return(rettv)
21444 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445{
Bram Moolenaar33570922005-01-25 22:26:29 +000021446 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447}
21448
21449/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 * is an allocated string. Used by report_pending() for verbose messages.
21452 */
21453 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454get_return_cmd(rettv)
21455 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021457 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021458 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021459 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021461 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021462 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021463 if (s == NULL)
21464 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465
21466 STRCPY(IObuff, ":return ");
21467 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21468 if (STRLEN(s) + 8 >= IOSIZE)
21469 STRCPY(IObuff + IOSIZE - 4, "...");
21470 vim_free(tofree);
21471 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472}
21473
21474/*
21475 * Get next function line.
21476 * Called by do_cmdline() to get the next line.
21477 * Returns allocated string, or NULL for end of function.
21478 */
21479/* ARGSUSED */
21480 char_u *
21481get_func_line(c, cookie, indent)
21482 int c; /* not used */
21483 void *cookie;
21484 int indent; /* not used */
21485{
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021487 ufunc_T *fp = fcp->func;
21488 char_u *retval;
21489 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490
21491 /* If breakpoints have been added/deleted need to check for it. */
21492 if (fcp->dbg_tick != debug_tick)
21493 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021494 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 sourcing_lnum);
21496 fcp->dbg_tick = debug_tick;
21497 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021498#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021499 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021500 func_line_end(cookie);
21501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502
Bram Moolenaar05159a02005-02-26 23:04:13 +000021503 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021504 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21505 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 retval = NULL;
21507 else
21508 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021509 /* Skip NULL lines (continuation lines). */
21510 while (fcp->linenr < gap->ga_len
21511 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21512 ++fcp->linenr;
21513 if (fcp->linenr >= gap->ga_len)
21514 retval = NULL;
21515 else
21516 {
21517 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21518 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021519#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021520 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021521 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021522#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 }
21525
21526 /* Did we encounter a breakpoint? */
21527 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21528 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021529 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021531 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 sourcing_lnum);
21533 fcp->dbg_tick = debug_tick;
21534 }
21535
21536 return retval;
21537}
21538
Bram Moolenaar05159a02005-02-26 23:04:13 +000021539#if defined(FEAT_PROFILE) || defined(PROTO)
21540/*
21541 * Called when starting to read a function line.
21542 * "sourcing_lnum" must be correct!
21543 * When skipping lines it may not actually be executed, but we won't find out
21544 * until later and we need to store the time now.
21545 */
21546 void
21547func_line_start(cookie)
21548 void *cookie;
21549{
21550 funccall_T *fcp = (funccall_T *)cookie;
21551 ufunc_T *fp = fcp->func;
21552
21553 if (fp->uf_profiling && sourcing_lnum >= 1
21554 && sourcing_lnum <= fp->uf_lines.ga_len)
21555 {
21556 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021557 /* Skip continuation lines. */
21558 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21559 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021560 fp->uf_tml_execed = FALSE;
21561 profile_start(&fp->uf_tml_start);
21562 profile_zero(&fp->uf_tml_children);
21563 profile_get_wait(&fp->uf_tml_wait);
21564 }
21565}
21566
21567/*
21568 * Called when actually executing a function line.
21569 */
21570 void
21571func_line_exec(cookie)
21572 void *cookie;
21573{
21574 funccall_T *fcp = (funccall_T *)cookie;
21575 ufunc_T *fp = fcp->func;
21576
21577 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21578 fp->uf_tml_execed = TRUE;
21579}
21580
21581/*
21582 * Called when done with a function line.
21583 */
21584 void
21585func_line_end(cookie)
21586 void *cookie;
21587{
21588 funccall_T *fcp = (funccall_T *)cookie;
21589 ufunc_T *fp = fcp->func;
21590
21591 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21592 {
21593 if (fp->uf_tml_execed)
21594 {
21595 ++fp->uf_tml_count[fp->uf_tml_idx];
21596 profile_end(&fp->uf_tml_start);
21597 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021598 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021599 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21600 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021601 }
21602 fp->uf_tml_idx = -1;
21603 }
21604}
21605#endif
21606
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607/*
21608 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021609 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 */
21611 int
21612func_has_ended(cookie)
21613 void *cookie;
21614{
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616
21617 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21618 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021619 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 || fcp->returned);
21621}
21622
21623/*
21624 * return TRUE if cookie indicates a function which "abort"s on errors.
21625 */
21626 int
21627func_has_abort(cookie)
21628 void *cookie;
21629{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021630 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631}
21632
21633#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21634typedef enum
21635{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021636 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21637 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21638 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639} var_flavour_T;
21640
21641static var_flavour_T var_flavour __ARGS((char_u *varname));
21642
21643 static var_flavour_T
21644var_flavour(varname)
21645 char_u *varname;
21646{
21647 char_u *p = varname;
21648
21649 if (ASCII_ISUPPER(*p))
21650 {
21651 while (*(++p))
21652 if (ASCII_ISLOWER(*p))
21653 return VAR_FLAVOUR_SESSION;
21654 return VAR_FLAVOUR_VIMINFO;
21655 }
21656 else
21657 return VAR_FLAVOUR_DEFAULT;
21658}
21659#endif
21660
21661#if defined(FEAT_VIMINFO) || defined(PROTO)
21662/*
21663 * Restore global vars that start with a capital from the viminfo file
21664 */
21665 int
21666read_viminfo_varlist(virp, writing)
21667 vir_T *virp;
21668 int writing;
21669{
21670 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021671 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673
21674 if (!writing && (find_viminfo_parameter('!') != NULL))
21675 {
21676 tab = vim_strchr(virp->vir_line + 1, '\t');
21677 if (tab != NULL)
21678 {
21679 *tab++ = '\0'; /* isolate the variable name */
21680 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021681 type = VAR_STRING;
21682#ifdef FEAT_FLOAT
21683 else if (*tab == 'F')
21684 type = VAR_FLOAT;
21685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686
21687 tab = vim_strchr(tab, '\t');
21688 if (tab != NULL)
21689 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021690 tv.v_type = type;
21691 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021692 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021694#ifdef FEAT_FLOAT
21695 else if (type == VAR_FLOAT)
21696 (void)string2float(tab + 1, &tv.vval.v_float);
21697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021699 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021700 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021701 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021702 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 }
21704 }
21705 }
21706
21707 return viminfo_readline(virp);
21708}
21709
21710/*
21711 * Write global vars that start with a capital to the viminfo file
21712 */
21713 void
21714write_viminfo_varlist(fp)
21715 FILE *fp;
21716{
Bram Moolenaar33570922005-01-25 22:26:29 +000021717 hashitem_T *hi;
21718 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021719 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021720 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021721 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021722 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021723 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724
21725 if (find_viminfo_parameter('!') == NULL)
21726 return;
21727
21728 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021729
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021730 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021731 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021733 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021735 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 this_var = HI2DI(hi);
21737 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021738 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021739 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021740 {
21741 case VAR_STRING: s = "STR"; break;
21742 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021743#ifdef FEAT_FLOAT
21744 case VAR_FLOAT: s = "FLO"; break;
21745#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021746 default: continue;
21747 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021749 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021750 if (p != NULL)
21751 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021752 vim_free(tofree);
21753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 }
21755 }
21756}
21757#endif
21758
21759#if defined(FEAT_SESSION) || defined(PROTO)
21760 int
21761store_session_globals(fd)
21762 FILE *fd;
21763{
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 hashitem_T *hi;
21765 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021766 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 char_u *p, *t;
21768
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021769 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021770 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021772 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021774 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 this_var = HI2DI(hi);
21776 if ((this_var->di_tv.v_type == VAR_NUMBER
21777 || this_var->di_tv.v_type == VAR_STRING)
21778 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021779 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021780 /* Escape special characters with a backslash. Turn a LF and
21781 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021783 (char_u *)"\\\"\n\r");
21784 if (p == NULL) /* out of memory */
21785 break;
21786 for (t = p; *t != NUL; ++t)
21787 if (*t == '\n')
21788 *t = 'n';
21789 else if (*t == '\r')
21790 *t = 'r';
21791 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 this_var->di_key,
21793 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21794 : ' ',
21795 p,
21796 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21797 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021798 || put_eol(fd) == FAIL)
21799 {
21800 vim_free(p);
21801 return FAIL;
21802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 vim_free(p);
21804 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021805#ifdef FEAT_FLOAT
21806 else if (this_var->di_tv.v_type == VAR_FLOAT
21807 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21808 {
21809 float_T f = this_var->di_tv.vval.v_float;
21810 int sign = ' ';
21811
21812 if (f < 0)
21813 {
21814 f = -f;
21815 sign = '-';
21816 }
21817 if ((fprintf(fd, "let %s = %c&%f",
21818 this_var->di_key, sign, f) < 0)
21819 || put_eol(fd) == FAIL)
21820 return FAIL;
21821 }
21822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 }
21824 }
21825 return OK;
21826}
21827#endif
21828
Bram Moolenaar661b1822005-07-28 22:36:45 +000021829/*
21830 * Display script name where an item was last set.
21831 * Should only be invoked when 'verbose' is non-zero.
21832 */
21833 void
21834last_set_msg(scriptID)
21835 scid_T scriptID;
21836{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021837 char_u *p;
21838
Bram Moolenaar661b1822005-07-28 22:36:45 +000021839 if (scriptID != 0)
21840 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021841 p = home_replace_save(NULL, get_scriptname(scriptID));
21842 if (p != NULL)
21843 {
21844 verbose_enter();
21845 MSG_PUTS(_("\n\tLast set from "));
21846 MSG_PUTS(p);
21847 vim_free(p);
21848 verbose_leave();
21849 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021850 }
21851}
21852
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853#endif /* FEAT_EVAL */
21854
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021856#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857
21858#ifdef WIN3264
21859/*
21860 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21861 */
21862static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21863static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21864static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21865
21866/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021867 * Get the short path (8.3) for the filename in "fnamep".
21868 * Only works for a valid file name.
21869 * When the path gets longer "fnamep" is changed and the allocated buffer
21870 * is put in "bufp".
21871 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21872 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 */
21874 static int
21875get_short_pathname(fnamep, bufp, fnamelen)
21876 char_u **fnamep;
21877 char_u **bufp;
21878 int *fnamelen;
21879{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021880 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 char_u *newbuf;
21882
21883 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 l = GetShortPathName(*fnamep, *fnamep, len);
21885 if (l > len - 1)
21886 {
21887 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021888 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 newbuf = vim_strnsave(*fnamep, l);
21890 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892
21893 vim_free(*bufp);
21894 *fnamep = *bufp = newbuf;
21895
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021896 /* Really should always succeed, as the buffer is big enough. */
21897 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 }
21899
21900 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021901 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902}
21903
21904/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021905 * Get the short path (8.3) for the filename in "fname". The converted
21906 * path is returned in "bufp".
21907 *
21908 * Some of the directories specified in "fname" may not exist. This function
21909 * will shorten the existing directories at the beginning of the path and then
21910 * append the remaining non-existing path.
21911 *
21912 * fname - Pointer to the filename to shorten. On return, contains the
21913 * pointer to the shortened pathname
21914 * bufp - Pointer to an allocated buffer for the filename.
21915 * fnamelen - Length of the filename pointed to by fname
21916 *
21917 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 */
21919 static int
21920shortpath_for_invalid_fname(fname, bufp, fnamelen)
21921 char_u **fname;
21922 char_u **bufp;
21923 int *fnamelen;
21924{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021925 char_u *short_fname, *save_fname, *pbuf_unused;
21926 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021928 int old_len, len;
21929 int new_len, sfx_len;
21930 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931
21932 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021933 old_len = *fnamelen;
21934 save_fname = vim_strnsave(*fname, old_len);
21935 pbuf_unused = NULL;
21936 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021938 endp = save_fname + old_len - 1; /* Find the end of the copy */
21939 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021941 /*
21942 * Try shortening the supplied path till it succeeds by removing one
21943 * directory at a time from the tail of the path.
21944 */
21945 len = 0;
21946 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021948 /* go back one path-separator */
21949 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
21950 --endp;
21951 if (endp <= save_fname)
21952 break; /* processed the complete path */
21953
21954 /*
21955 * Replace the path separator with a NUL and try to shorten the
21956 * resulting path.
21957 */
21958 ch = *endp;
21959 *endp = 0;
21960 short_fname = save_fname;
21961 len = STRLEN(short_fname) + 1;
21962 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
21963 {
21964 retval = FAIL;
21965 goto theend;
21966 }
21967 *endp = ch; /* preserve the string */
21968
21969 if (len > 0)
21970 break; /* successfully shortened the path */
21971
21972 /* failed to shorten the path. Skip the path separator */
21973 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 }
21975
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021976 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021978 /*
21979 * Succeeded in shortening the path. Now concatenate the shortened
21980 * path with the remaining path at the tail.
21981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021983 /* Compute the length of the new path. */
21984 sfx_len = (int)(save_endp - endp) + 1;
21985 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021987 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021989 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021991 /* There is not enough space in the currently allocated string,
21992 * copy it to a buffer big enough. */
21993 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021995 {
21996 retval = FAIL;
21997 goto theend;
21998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 }
22000 else
22001 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022002 /* Transfer short_fname to the main buffer (it's big enough),
22003 * unless get_short_pathname() did its work in-place. */
22004 *fname = *bufp = save_fname;
22005 if (short_fname != save_fname)
22006 vim_strncpy(save_fname, short_fname, len);
22007 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022009
22010 /* concat the not-shortened part of the path */
22011 vim_strncpy(*fname + len, endp, sfx_len);
22012 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022014
22015theend:
22016 vim_free(pbuf_unused);
22017 vim_free(save_fname);
22018
22019 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020}
22021
22022/*
22023 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022024 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 */
22026 static int
22027shortpath_for_partial(fnamep, bufp, fnamelen)
22028 char_u **fnamep;
22029 char_u **bufp;
22030 int *fnamelen;
22031{
22032 int sepcount, len, tflen;
22033 char_u *p;
22034 char_u *pbuf, *tfname;
22035 int hasTilde;
22036
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022037 /* Count up the path separators from the RHS.. so we know which part
22038 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022040 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 if (vim_ispathsep(*p))
22042 ++sepcount;
22043
22044 /* Need full path first (use expand_env() to remove a "~/") */
22045 hasTilde = (**fnamep == '~');
22046 if (hasTilde)
22047 pbuf = tfname = expand_env_save(*fnamep);
22048 else
22049 pbuf = tfname = FullName_save(*fnamep, FALSE);
22050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022051 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022053 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22054 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055
22056 if (len == 0)
22057 {
22058 /* Don't have a valid filename, so shorten the rest of the
22059 * path if we can. This CAN give us invalid 8.3 filenames, but
22060 * there's not a lot of point in guessing what it might be.
22061 */
22062 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022063 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 }
22066
22067 /* Count the paths backward to find the beginning of the desired string. */
22068 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022069 {
22070#ifdef FEAT_MBYTE
22071 if (has_mbyte)
22072 p -= mb_head_off(tfname, p);
22073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 if (vim_ispathsep(*p))
22075 {
22076 if (sepcount == 0 || (hasTilde && sepcount == 1))
22077 break;
22078 else
22079 sepcount --;
22080 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 if (hasTilde)
22083 {
22084 --p;
22085 if (p >= tfname)
22086 *p = '~';
22087 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 }
22090 else
22091 ++p;
22092
22093 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22094 vim_free(*bufp);
22095 *fnamelen = (int)STRLEN(p);
22096 *bufp = pbuf;
22097 *fnamep = p;
22098
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022099 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100}
22101#endif /* WIN3264 */
22102
22103/*
22104 * Adjust a filename, according to a string of modifiers.
22105 * *fnamep must be NUL terminated when called. When returning, the length is
22106 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022107 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 * When there is an error, *fnamep is set to NULL.
22109 */
22110 int
22111modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22112 char_u *src; /* string with modifiers */
22113 int *usedlen; /* characters after src that are used */
22114 char_u **fnamep; /* file name so far */
22115 char_u **bufp; /* buffer for allocated file name or NULL */
22116 int *fnamelen; /* length of fnamep */
22117{
22118 int valid = 0;
22119 char_u *tail;
22120 char_u *s, *p, *pbuf;
22121 char_u dirname[MAXPATHL];
22122 int c;
22123 int has_fullname = 0;
22124#ifdef WIN3264
22125 int has_shortname = 0;
22126#endif
22127
22128repeat:
22129 /* ":p" - full path/file_name */
22130 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22131 {
22132 has_fullname = 1;
22133
22134 valid |= VALID_PATH;
22135 *usedlen += 2;
22136
22137 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22138 if ((*fnamep)[0] == '~'
22139#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22140 && ((*fnamep)[1] == '/'
22141# ifdef BACKSLASH_IN_FILENAME
22142 || (*fnamep)[1] == '\\'
22143# endif
22144 || (*fnamep)[1] == NUL)
22145
22146#endif
22147 )
22148 {
22149 *fnamep = expand_env_save(*fnamep);
22150 vim_free(*bufp); /* free any allocated file name */
22151 *bufp = *fnamep;
22152 if (*fnamep == NULL)
22153 return -1;
22154 }
22155
22156 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022157 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 {
22159 if (vim_ispathsep(*p)
22160 && p[1] == '.'
22161 && (p[2] == NUL
22162 || vim_ispathsep(p[2])
22163 || (p[2] == '.'
22164 && (p[3] == NUL || vim_ispathsep(p[3])))))
22165 break;
22166 }
22167
22168 /* FullName_save() is slow, don't use it when not needed. */
22169 if (*p != NUL || !vim_isAbsName(*fnamep))
22170 {
22171 *fnamep = FullName_save(*fnamep, *p != NUL);
22172 vim_free(*bufp); /* free any allocated file name */
22173 *bufp = *fnamep;
22174 if (*fnamep == NULL)
22175 return -1;
22176 }
22177
22178 /* Append a path separator to a directory. */
22179 if (mch_isdir(*fnamep))
22180 {
22181 /* Make room for one or two extra characters. */
22182 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22183 vim_free(*bufp); /* free any allocated file name */
22184 *bufp = *fnamep;
22185 if (*fnamep == NULL)
22186 return -1;
22187 add_pathsep(*fnamep);
22188 }
22189 }
22190
22191 /* ":." - path relative to the current directory */
22192 /* ":~" - path relative to the home directory */
22193 /* ":8" - shortname path - postponed till after */
22194 while (src[*usedlen] == ':'
22195 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22196 {
22197 *usedlen += 2;
22198 if (c == '8')
22199 {
22200#ifdef WIN3264
22201 has_shortname = 1; /* Postpone this. */
22202#endif
22203 continue;
22204 }
22205 pbuf = NULL;
22206 /* Need full path first (use expand_env() to remove a "~/") */
22207 if (!has_fullname)
22208 {
22209 if (c == '.' && **fnamep == '~')
22210 p = pbuf = expand_env_save(*fnamep);
22211 else
22212 p = pbuf = FullName_save(*fnamep, FALSE);
22213 }
22214 else
22215 p = *fnamep;
22216
22217 has_fullname = 0;
22218
22219 if (p != NULL)
22220 {
22221 if (c == '.')
22222 {
22223 mch_dirname(dirname, MAXPATHL);
22224 s = shorten_fname(p, dirname);
22225 if (s != NULL)
22226 {
22227 *fnamep = s;
22228 if (pbuf != NULL)
22229 {
22230 vim_free(*bufp); /* free any allocated file name */
22231 *bufp = pbuf;
22232 pbuf = NULL;
22233 }
22234 }
22235 }
22236 else
22237 {
22238 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22239 /* Only replace it when it starts with '~' */
22240 if (*dirname == '~')
22241 {
22242 s = vim_strsave(dirname);
22243 if (s != NULL)
22244 {
22245 *fnamep = s;
22246 vim_free(*bufp);
22247 *bufp = s;
22248 }
22249 }
22250 }
22251 vim_free(pbuf);
22252 }
22253 }
22254
22255 tail = gettail(*fnamep);
22256 *fnamelen = (int)STRLEN(*fnamep);
22257
22258 /* ":h" - head, remove "/file_name", can be repeated */
22259 /* Don't remove the first "/" or "c:\" */
22260 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22261 {
22262 valid |= VALID_HEAD;
22263 *usedlen += 2;
22264 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022265 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022266 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 *fnamelen = (int)(tail - *fnamep);
22268#ifdef VMS
22269 if (*fnamelen > 0)
22270 *fnamelen += 1; /* the path separator is part of the path */
22271#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022272 if (*fnamelen == 0)
22273 {
22274 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22275 p = vim_strsave((char_u *)".");
22276 if (p == NULL)
22277 return -1;
22278 vim_free(*bufp);
22279 *bufp = *fnamep = tail = p;
22280 *fnamelen = 1;
22281 }
22282 else
22283 {
22284 while (tail > s && !after_pathsep(s, tail))
22285 mb_ptr_back(*fnamep, tail);
22286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 }
22288
22289 /* ":8" - shortname */
22290 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22291 {
22292 *usedlen += 2;
22293#ifdef WIN3264
22294 has_shortname = 1;
22295#endif
22296 }
22297
22298#ifdef WIN3264
22299 /* Check shortname after we have done 'heads' and before we do 'tails'
22300 */
22301 if (has_shortname)
22302 {
22303 pbuf = NULL;
22304 /* Copy the string if it is shortened by :h */
22305 if (*fnamelen < (int)STRLEN(*fnamep))
22306 {
22307 p = vim_strnsave(*fnamep, *fnamelen);
22308 if (p == 0)
22309 return -1;
22310 vim_free(*bufp);
22311 *bufp = *fnamep = p;
22312 }
22313
22314 /* Split into two implementations - makes it easier. First is where
22315 * there isn't a full name already, second is where there is.
22316 */
22317 if (!has_fullname && !vim_isAbsName(*fnamep))
22318 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022319 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 return -1;
22321 }
22322 else
22323 {
22324 int l;
22325
22326 /* Simple case, already have the full-name
22327 * Nearly always shorter, so try first time. */
22328 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022329 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 return -1;
22331
22332 if (l == 0)
22333 {
22334 /* Couldn't find the filename.. search the paths.
22335 */
22336 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022337 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 return -1;
22339 }
22340 *fnamelen = l;
22341 }
22342 }
22343#endif /* WIN3264 */
22344
22345 /* ":t" - tail, just the basename */
22346 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22347 {
22348 *usedlen += 2;
22349 *fnamelen -= (int)(tail - *fnamep);
22350 *fnamep = tail;
22351 }
22352
22353 /* ":e" - extension, can be repeated */
22354 /* ":r" - root, without extension, can be repeated */
22355 while (src[*usedlen] == ':'
22356 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22357 {
22358 /* find a '.' in the tail:
22359 * - for second :e: before the current fname
22360 * - otherwise: The last '.'
22361 */
22362 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22363 s = *fnamep - 2;
22364 else
22365 s = *fnamep + *fnamelen - 1;
22366 for ( ; s > tail; --s)
22367 if (s[0] == '.')
22368 break;
22369 if (src[*usedlen + 1] == 'e') /* :e */
22370 {
22371 if (s > tail)
22372 {
22373 *fnamelen += (int)(*fnamep - (s + 1));
22374 *fnamep = s + 1;
22375#ifdef VMS
22376 /* cut version from the extension */
22377 s = *fnamep + *fnamelen - 1;
22378 for ( ; s > *fnamep; --s)
22379 if (s[0] == ';')
22380 break;
22381 if (s > *fnamep)
22382 *fnamelen = s - *fnamep;
22383#endif
22384 }
22385 else if (*fnamep <= tail)
22386 *fnamelen = 0;
22387 }
22388 else /* :r */
22389 {
22390 if (s > tail) /* remove one extension */
22391 *fnamelen = (int)(s - *fnamep);
22392 }
22393 *usedlen += 2;
22394 }
22395
22396 /* ":s?pat?foo?" - substitute */
22397 /* ":gs?pat?foo?" - global substitute */
22398 if (src[*usedlen] == ':'
22399 && (src[*usedlen + 1] == 's'
22400 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22401 {
22402 char_u *str;
22403 char_u *pat;
22404 char_u *sub;
22405 int sep;
22406 char_u *flags;
22407 int didit = FALSE;
22408
22409 flags = (char_u *)"";
22410 s = src + *usedlen + 2;
22411 if (src[*usedlen + 1] == 'g')
22412 {
22413 flags = (char_u *)"g";
22414 ++s;
22415 }
22416
22417 sep = *s++;
22418 if (sep)
22419 {
22420 /* find end of pattern */
22421 p = vim_strchr(s, sep);
22422 if (p != NULL)
22423 {
22424 pat = vim_strnsave(s, (int)(p - s));
22425 if (pat != NULL)
22426 {
22427 s = p + 1;
22428 /* find end of substitution */
22429 p = vim_strchr(s, sep);
22430 if (p != NULL)
22431 {
22432 sub = vim_strnsave(s, (int)(p - s));
22433 str = vim_strnsave(*fnamep, *fnamelen);
22434 if (sub != NULL && str != NULL)
22435 {
22436 *usedlen = (int)(p + 1 - src);
22437 s = do_string_sub(str, pat, sub, flags);
22438 if (s != NULL)
22439 {
22440 *fnamep = s;
22441 *fnamelen = (int)STRLEN(s);
22442 vim_free(*bufp);
22443 *bufp = s;
22444 didit = TRUE;
22445 }
22446 }
22447 vim_free(sub);
22448 vim_free(str);
22449 }
22450 vim_free(pat);
22451 }
22452 }
22453 /* after using ":s", repeat all the modifiers */
22454 if (didit)
22455 goto repeat;
22456 }
22457 }
22458
22459 return valid;
22460}
22461
22462/*
22463 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22464 * "flags" can be "g" to do a global substitute.
22465 * Returns an allocated string, NULL for error.
22466 */
22467 char_u *
22468do_string_sub(str, pat, sub, flags)
22469 char_u *str;
22470 char_u *pat;
22471 char_u *sub;
22472 char_u *flags;
22473{
22474 int sublen;
22475 regmatch_T regmatch;
22476 int i;
22477 int do_all;
22478 char_u *tail;
22479 garray_T ga;
22480 char_u *ret;
22481 char_u *save_cpo;
22482
22483 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22484 save_cpo = p_cpo;
22485 p_cpo = (char_u *)"";
22486
22487 ga_init2(&ga, 1, 200);
22488
22489 do_all = (flags[0] == 'g');
22490
22491 regmatch.rm_ic = p_ic;
22492 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22493 if (regmatch.regprog != NULL)
22494 {
22495 tail = str;
22496 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22497 {
22498 /*
22499 * Get some space for a temporary buffer to do the substitution
22500 * into. It will contain:
22501 * - The text up to where the match is.
22502 * - The substituted text.
22503 * - The text after the match.
22504 */
22505 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22506 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22507 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22508 {
22509 ga_clear(&ga);
22510 break;
22511 }
22512
22513 /* copy the text up to where the match is */
22514 i = (int)(regmatch.startp[0] - tail);
22515 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22516 /* add the substituted text */
22517 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22518 + ga.ga_len + i, TRUE, TRUE, FALSE);
22519 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 /* avoid getting stuck on a match with an empty string */
22521 if (tail == regmatch.endp[0])
22522 {
22523 if (*tail == NUL)
22524 break;
22525 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22526 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 }
22528 else
22529 {
22530 tail = regmatch.endp[0];
22531 if (*tail == NUL)
22532 break;
22533 }
22534 if (!do_all)
22535 break;
22536 }
22537
22538 if (ga.ga_data != NULL)
22539 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22540
22541 vim_free(regmatch.regprog);
22542 }
22543
22544 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22545 ga_clear(&ga);
22546 p_cpo = save_cpo;
22547
22548 return ret;
22549}
22550
22551#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */