blob: d32efaa7b66aef51e57774702f74f38cef85798b [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
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
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 Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 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 */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 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
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000201/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000202static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
203
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204/* list heads for garbage collection */
205static dict_T *first_dict = NULL; /* list of all dicts */
206static list_T *first_list = NULL; /* list of all lists */
207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000208/* From user function to hashitem and back. */
209static ufunc_T dumuf;
210#define UF2HIKEY(fp) ((fp)->uf_name)
211#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
212#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
213
214#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
215#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
218#define VAR_SHORT_LEN 20 /* short variable name length */
219#define FIXVAR_CNT 12 /* number of fixed variables */
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000222typedef struct funccall_S funccall_T;
223
224struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 ufunc_T *func; /* function being called */
227 int linenr; /* next line to be executed */
228 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000229 struct /* fixed variables for arguments */
230 {
231 dictitem_T var; /* variable (without room for name) */
232 char_u room[VAR_SHORT_LEN]; /* room for the name */
233 } fixvar[FIXVAR_CNT];
234 dict_T l_vars; /* l: local function variables */
235 dictitem_T l_vars_var; /* variable for l: scope */
236 dict_T l_avars; /* a: argument variables */
237 dictitem_T l_avars_var; /* variable for a: scope */
238 list_T l_varlist; /* list for a:000 */
239 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
240 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 linenr_T breakpoint; /* next line with breakpoint or zero */
242 int dbg_tick; /* debug_tick when breakpoint was set */
243 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000244#ifdef FEAT_PROFILE
245 proftime_T prof_child; /* time spent in a child */
246#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000247 funccall_T *caller; /* calling function or NULL */
248};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251 * Info used by a ":for" loop.
252 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000253typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254{
255 int fi_semicolon; /* TRUE if ending in '; var]' */
256 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257 listwatch_T fi_lw; /* keep an eye on the item used. */
258 list_T *fi_list; /* list being used */
259} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000262 * Struct used by trans_function_name()
263 */
264typedef struct
265{
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000267 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 dictitem_T *fd_di; /* Dictionary item used */
269} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270
Bram Moolenaara7043832005-01-21 11:56:39 +0000271
272/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 * Array to hold the value of v: variables.
274 * The value is in a dictitem, so that it can also be used in the v: scope.
275 * The reason to use this table anyway is for very quick access to the
276 * variables with the VV_ defines.
277 */
278#include "version.h"
279
280/* values for vv_flags: */
281#define VV_COMPAT 1 /* compatible, also used without "v:" */
282#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000283#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000286
287static struct vimvar
288{
289 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 dictitem_T vv_di; /* value and name for key */
291 char vv_filler[16]; /* space for LONGEST name below!!! */
292 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
293} vimvars[VV_LEN] =
294{
295 /*
296 * The order here must match the VV_ defines in vim.h!
297 * Initializing a union does not work, leave tv.vval empty to get zero's.
298 */
299 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
300 {VV_NAME("count1", VAR_NUMBER), VV_RO},
301 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
302 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
303 {VV_NAME("warningmsg", VAR_STRING), 0},
304 {VV_NAME("statusmsg", VAR_STRING), 0},
305 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
307 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
309 {VV_NAME("termresponse", VAR_STRING), VV_RO},
310 {VV_NAME("fname", VAR_STRING), VV_RO},
311 {VV_NAME("lang", VAR_STRING), VV_RO},
312 {VV_NAME("lc_time", VAR_STRING), VV_RO},
313 {VV_NAME("ctype", VAR_STRING), VV_RO},
314 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
315 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
316 {VV_NAME("fname_in", VAR_STRING), VV_RO},
317 {VV_NAME("fname_out", VAR_STRING), VV_RO},
318 {VV_NAME("fname_new", VAR_STRING), VV_RO},
319 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
320 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
321 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
324 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("progname", VAR_STRING), VV_RO},
326 {VV_NAME("servername", VAR_STRING), VV_RO},
327 {VV_NAME("dying", VAR_NUMBER), VV_RO},
328 {VV_NAME("exception", VAR_STRING), VV_RO},
329 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
330 {VV_NAME("register", VAR_STRING), VV_RO},
331 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
332 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000333 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
334 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000335 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000336 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
337 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000338 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
341 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000343 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000344 {VV_NAME("swapname", VAR_STRING), VV_RO},
345 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000346 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000347 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
438static void dict_free __ARGS((dict_T *d));
439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static 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));
456static 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 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000557static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585#ifdef vim_mkdir
586static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000591static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000593static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000594static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000597static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000610static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000612static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000619static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000620static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000621static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000623static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000625static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000628static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000629static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000632static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633#ifdef HAVE_STRFTIME
634static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
636static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000648static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000649static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000650static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000651static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000652static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000654static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000668static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000671static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000673static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
674static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static int get_env_len __ARGS((char_u **arg));
676static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000678static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
679#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
680#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
681 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000682static 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 +0000683static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000685static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
686static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static typval_T *alloc_tv __ARGS((void));
688static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void init_tv __ARGS((typval_T *varp));
690static long get_tv_number __ARGS((typval_T *varp));
691static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000692static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static char_u *get_tv_string __ARGS((typval_T *varp));
694static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000695static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
699static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
700static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
701static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
702static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
703static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
704static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000705static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000706static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000708static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
710static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
711static int eval_fname_script __ARGS((char_u *p));
712static int eval_fname_sid __ARGS((char_u *p));
713static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static ufunc_T *find_func __ARGS((char_u *name));
715static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000716static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717#ifdef FEAT_PROFILE
718static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000719static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
720static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
721static int
722# ifdef __BORLANDC__
723 _RTLENTRYF
724# endif
725 prof_total_cmp __ARGS((const void *s1, const void *s2));
726static int
727# ifdef __BORLANDC__
728 _RTLENTRYF
729# endif
730 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000731#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000732static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000733static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000734static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void func_free __ARGS((ufunc_T *fp));
736static void func_unref __ARGS((char_u *name));
737static void func_ref __ARGS((char_u *name));
738static 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));
739static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000740static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
741static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000742static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000743static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000744static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000746/* Character used as separated in autoload function/variable names. */
747#define AUTOLOAD_CHAR '#'
748
Bram Moolenaar33570922005-01-25 22:26:29 +0000749/*
750 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000751 */
752 void
753eval_init()
754{
Bram Moolenaar33570922005-01-25 22:26:29 +0000755 int i;
756 struct vimvar *p;
757
758 init_var_dict(&globvardict, &globvars_var);
759 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000760 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000761 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
763 for (i = 0; i < VV_LEN; ++i)
764 {
765 p = &vimvars[i];
766 STRCPY(p->vv_di.di_key, p->vv_name);
767 if (p->vv_flags & VV_RO)
768 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
769 else if (p->vv_flags & VV_RO_SBX)
770 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
771 else
772 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773
774 /* add to v: scope dict, unless the value is not always available */
775 if (p->vv_type != VAR_UNKNOWN)
776 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000777 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000778 /* add to compat scope dict */
779 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000781}
782
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000783#if defined(EXITFREE) || defined(PROTO)
784 void
785eval_clear()
786{
787 int i;
788 struct vimvar *p;
789
790 for (i = 0; i < VV_LEN; ++i)
791 {
792 p = &vimvars[i];
793 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000795 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000796 p->vv_di.di_tv.vval.v_string = NULL;
797 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000798 }
799 hash_clear(&vimvarht);
800 hash_clear(&compat_hashtab);
801
802 /* script-local variables */
803 for (i = 1; i <= ga_scripts.ga_len; ++i)
804 vars_clear(&SCRIPT_VARS(i));
805 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000806 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000807
808 /* global variables */
809 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000810
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000811 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000813 hash_clear(&func_hashtab);
814
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000815 /* autoloaded script names */
816 ga_clear_strings(&ga_loaded);
817
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000818 /* unreferenced lists and dicts */
819 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000820}
821#endif
822
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * Return the name of the executed function.
825 */
826 char_u *
827func_name(cookie)
828 void *cookie;
829{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000830 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832
833/*
834 * Return the address holding the next breakpoint line for a funccall cookie.
835 */
836 linenr_T *
837func_breakpoint(cookie)
838 void *cookie;
839{
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
843/*
844 * Return the address holding the debug tick for a funccall cookie.
845 */
846 int *
847func_dbg_tick(cookie)
848 void *cookie;
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853/*
854 * Return the nesting level for a funccall cookie.
855 */
856 int
857func_level(cookie)
858 void *cookie;
859{
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
863/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000864funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866/*
867 * Return TRUE when a function was ended by a ":return" command.
868 */
869 int
870current_func_returned()
871{
872 return current_funccal->returned;
873}
874
875
876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * Set an internal variable to a string value. Creates the variable if it does
878 * not already exist.
879 */
880 void
881set_internal_string_var(name, value)
882 char_u *name;
883 char_u *value;
884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000885 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 val = vim_strsave(value);
889 if (val != NULL)
890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000894 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 }
898}
899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900static lval_T *redir_lval = NULL;
901static char_u *redir_endp = NULL;
902static char_u *redir_varname = NULL;
903
904/*
905 * Start recording command output to a variable
906 * Returns OK if successfully completed the setup. FAIL otherwise.
907 */
908 int
909var_redir_start(name, append)
910 char_u *name;
911 int append; /* append to an existing variable */
912{
913 int save_emsg;
914 int err;
915 typval_T tv;
916
917 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000918 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 {
920 EMSG(_(e_invarg));
921 return FAIL;
922 }
923
924 redir_varname = vim_strsave(name);
925 if (redir_varname == NULL)
926 return FAIL;
927
928 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
929 if (redir_lval == NULL)
930 {
931 var_redir_stop();
932 return FAIL;
933 }
934
935 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000936 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
937 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000938 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
939 {
940 if (redir_endp != NULL && *redir_endp != NUL)
941 /* Trailing characters are present after the variable name */
942 EMSG(_(e_trailing));
943 else
944 EMSG(_(e_invarg));
945 var_redir_stop();
946 return FAIL;
947 }
948
949 /* check if we can write to the variable: set it to or append an empty
950 * string */
951 save_emsg = did_emsg;
952 did_emsg = FALSE;
953 tv.v_type = VAR_STRING;
954 tv.vval.v_string = (char_u *)"";
955 if (append)
956 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
957 else
958 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
959 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000960 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 if (err)
962 {
963 var_redir_stop();
964 return FAIL;
965 }
966 if (redir_lval->ll_newkey != NULL)
967 {
968 /* Dictionary item was created, don't do it again. */
969 vim_free(redir_lval->ll_newkey);
970 redir_lval->ll_newkey = NULL;
971 }
972
973 return OK;
974}
975
976/*
977 * Append "value[len]" to the variable set by var_redir_start().
978 */
979 void
980var_redir_str(value, len)
981 char_u *value;
982 int len;
983{
984 char_u *val;
985 typval_T tv;
986 int save_emsg;
987 int err;
988
989 if (redir_lval == NULL)
990 return;
991
992 if (len == -1)
993 /* Append the entire string */
994 val = vim_strsave(value);
995 else
996 /* Append only the specified number of characters */
997 val = vim_strnsave(value, len);
998 if (val == NULL)
999 return;
1000
1001 tv.v_type = VAR_STRING;
1002 tv.vval.v_string = val;
1003
1004 save_emsg = did_emsg;
1005 did_emsg = FALSE;
1006 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1007 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001008 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 if (err)
1010 var_redir_stop();
1011
1012 vim_free(tv.vval.v_string);
1013}
1014
1015/*
1016 * Stop redirecting command output to a variable.
1017 */
1018 void
1019var_redir_stop()
1020{
1021 if (redir_lval != NULL)
1022 {
1023 clear_lval(redir_lval);
1024 vim_free(redir_lval);
1025 redir_lval = NULL;
1026 }
1027 vim_free(redir_varname);
1028 redir_varname = NULL;
1029}
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031# if defined(FEAT_MBYTE) || defined(PROTO)
1032 int
1033eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1034 char_u *enc_from;
1035 char_u *enc_to;
1036 char_u *fname_from;
1037 char_u *fname_to;
1038{
1039 int err = FALSE;
1040
1041 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1042 set_vim_var_string(VV_CC_TO, enc_to, -1);
1043 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1044 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1045 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1046 err = TRUE;
1047 set_vim_var_string(VV_CC_FROM, NULL, -1);
1048 set_vim_var_string(VV_CC_TO, NULL, -1);
1049 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1050 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1051
1052 if (err)
1053 return FAIL;
1054 return OK;
1055}
1056# endif
1057
1058# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1059 int
1060eval_printexpr(fname, args)
1061 char_u *fname;
1062 char_u *args;
1063{
1064 int err = FALSE;
1065
1066 set_vim_var_string(VV_FNAME_IN, fname, -1);
1067 set_vim_var_string(VV_CMDARG, args, -1);
1068 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1069 err = TRUE;
1070 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1071 set_vim_var_string(VV_CMDARG, NULL, -1);
1072
1073 if (err)
1074 {
1075 mch_remove(fname);
1076 return FAIL;
1077 }
1078 return OK;
1079}
1080# endif
1081
1082# if defined(FEAT_DIFF) || defined(PROTO)
1083 void
1084eval_diff(origfile, newfile, outfile)
1085 char_u *origfile;
1086 char_u *newfile;
1087 char_u *outfile;
1088{
1089 int err = FALSE;
1090
1091 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1092 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1093 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1094 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1095 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1096 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1097 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1098}
1099
1100 void
1101eval_patch(origfile, difffile, outfile)
1102 char_u *origfile;
1103 char_u *difffile;
1104 char_u *outfile;
1105{
1106 int err;
1107
1108 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1109 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1110 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1111 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1112 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1113 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1114 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1115}
1116# endif
1117
1118/*
1119 * Top level evaluation function, returning a boolean.
1120 * Sets "error" to TRUE if there was an error.
1121 * Return TRUE or FALSE.
1122 */
1123 int
1124eval_to_bool(arg, error, nextcmd, skip)
1125 char_u *arg;
1126 int *error;
1127 char_u **nextcmd;
1128 int skip; /* only parse, don't execute */
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int retval = FALSE;
1132
1133 if (skip)
1134 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 else
1138 {
1139 *error = FALSE;
1140 if (!skip)
1141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001142 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 }
1146 if (skip)
1147 --emsg_skip;
1148
1149 return retval;
1150}
1151
1152/*
1153 * Top level evaluation function, returning a string. If "skip" is TRUE,
1154 * only parsing to "nextcmd" is done, without reporting errors. Return
1155 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1156 */
1157 char_u *
1158eval_to_string_skip(arg, nextcmd, skip)
1159 char_u *arg;
1160 char_u **nextcmd;
1161 int skip; /* only parse, don't execute */
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 char_u *retval;
1165
1166 if (skip)
1167 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 retval = NULL;
1170 else
1171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 retval = vim_strsave(get_tv_string(&tv));
1173 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 if (skip)
1176 --emsg_skip;
1177
1178 return retval;
1179}
1180
1181/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001182 * Skip over an expression at "*pp".
1183 * Return FAIL for an error, OK otherwise.
1184 */
1185 int
1186skip_expr(pp)
1187 char_u **pp;
1188{
Bram Moolenaar33570922005-01-25 22:26:29 +00001189 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001190
1191 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001192 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001193}
1194
1195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 * Top level evaluation function, returning a string.
1197 * Return pointer to allocated memory, or NULL for failure.
1198 */
1199 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001200eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u *arg;
1202 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001203 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001207 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = NULL;
1211 else
1212 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001213 if (dolist && tv.v_type == VAR_LIST)
1214 {
1215 ga_init2(&ga, (int)sizeof(char), 80);
1216 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1217 ga_append(&ga, NUL);
1218 retval = (char_u *)ga.ga_data;
1219 }
1220 else
1221 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224
1225 return retval;
1226}
1227
1228/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001229 * Call eval_to_string() without using current local variables and using
1230 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 */
1232 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001233eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 char_u *arg;
1235 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001236 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 char_u *retval;
1239 void *save_funccalp;
1240
1241 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001242 if (use_sandbox)
1243 ++sandbox;
1244 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001245 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 if (use_sandbox)
1247 --sandbox;
1248 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 restore_funccal(save_funccalp);
1250 return retval;
1251}
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253/*
1254 * Top level evaluation function, returning a number.
1255 * Evaluates "expr" silently.
1256 * Returns -1 for an error.
1257 */
1258 int
1259eval_to_number(expr)
1260 char_u *expr;
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001264 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 ++emsg_off;
1267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 retval = -1;
1270 else
1271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 --emsg_off;
1276
1277 return retval;
1278}
1279
Bram Moolenaara40058a2005-07-11 22:42:07 +00001280/*
1281 * Prepare v: variable "idx" to be used.
1282 * Save the current typeval in "save_tv".
1283 * When not used yet add the variable to the v: hashtable.
1284 */
1285 static void
1286prepare_vimvar(idx, save_tv)
1287 int idx;
1288 typval_T *save_tv;
1289{
1290 *save_tv = vimvars[idx].vv_tv;
1291 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1292 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1293}
1294
1295/*
1296 * Restore v: variable "idx" to typeval "save_tv".
1297 * When no longer defined, remove the variable from the v: hashtable.
1298 */
1299 static void
1300restore_vimvar(idx, save_tv)
1301 int idx;
1302 typval_T *save_tv;
1303{
1304 hashitem_T *hi;
1305
1306 clear_tv(&vimvars[idx].vv_tv);
1307 vimvars[idx].vv_tv = *save_tv;
1308 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1309 {
1310 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1311 if (HASHITEM_EMPTY(hi))
1312 EMSG2(_(e_intern2), "restore_vimvar()");
1313 else
1314 hash_remove(&vimvarht, hi);
1315 }
1316}
1317
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001318#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001319/*
1320 * Evaluate an expression to a list with suggestions.
1321 * For the "expr:" part of 'spellsuggest'.
1322 */
1323 list_T *
1324eval_spell_expr(badword, expr)
1325 char_u *badword;
1326 char_u *expr;
1327{
1328 typval_T save_val;
1329 typval_T rettv;
1330 list_T *list = NULL;
1331 char_u *p = skipwhite(expr);
1332
1333 /* Set "v:val" to the bad word. */
1334 prepare_vimvar(VV_VAL, &save_val);
1335 vimvars[VV_VAL].vv_type = VAR_STRING;
1336 vimvars[VV_VAL].vv_str = badword;
1337 if (p_verbose == 0)
1338 ++emsg_off;
1339
1340 if (eval1(&p, &rettv, TRUE) == OK)
1341 {
1342 if (rettv.v_type != VAR_LIST)
1343 clear_tv(&rettv);
1344 else
1345 list = rettv.vval.v_list;
1346 }
1347
1348 if (p_verbose == 0)
1349 --emsg_off;
1350 vimvars[VV_VAL].vv_str = NULL;
1351 restore_vimvar(VV_VAL, &save_val);
1352
1353 return list;
1354}
1355
1356/*
1357 * "list" is supposed to contain two items: a word and a number. Return the
1358 * word in "pp" and the number as the return value.
1359 * Return -1 if anything isn't right.
1360 * Used to get the good word and score from the eval_spell_expr() result.
1361 */
1362 int
1363get_spellword(list, pp)
1364 list_T *list;
1365 char_u **pp;
1366{
1367 listitem_T *li;
1368
1369 li = list->lv_first;
1370 if (li == NULL)
1371 return -1;
1372 *pp = get_tv_string(&li->li_tv);
1373
1374 li = li->li_next;
1375 if (li == NULL)
1376 return -1;
1377 return get_tv_number(&li->li_tv);
1378}
1379#endif
1380
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001381/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001382 * Top level evaluation function.
1383 * Returns an allocated typval_T with the result.
1384 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001385 */
1386 typval_T *
1387eval_expr(arg, nextcmd)
1388 char_u *arg;
1389 char_u **nextcmd;
1390{
1391 typval_T *tv;
1392
1393 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001394 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395 {
1396 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398 }
1399
1400 return tv;
1401}
1402
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1405/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001410 static int
1411call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *func;
1413 int argc;
1414 char_u **argv;
1415 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 long n;
1420 int len;
1421 int i;
1422 int doesrange;
1423 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001426 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 for (i = 0; i < argc; i++)
1431 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001432 /* Pass a NULL or empty argument as an empty string */
1433 if (argv[i] == NULL || *argv[i] == NUL)
1434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001435 argvars[i].v_type = VAR_STRING;
1436 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001437 continue;
1438 }
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /* Recognize a number argument, the others must be strings. */
1441 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1442 if (len != 0 && len == (int)STRLEN(argv[i]))
1443 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001444 argvars[i].v_type = VAR_NUMBER;
1445 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 else
1448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001449 argvars[i].v_type = VAR_STRING;
1450 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 }
1453
1454 if (safe)
1455 {
1456 save_funccalp = save_funccal();
1457 ++sandbox;
1458 }
1459
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001460 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1461 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001463 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (safe)
1465 {
1466 --sandbox;
1467 restore_funccal(save_funccalp);
1468 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 vim_free(argvars);
1470
1471 if (ret == FAIL)
1472 clear_tv(rettv);
1473
1474 return ret;
1475}
1476
1477/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001478 * Call vimL function "func" and return the result as a string.
1479 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480 * Uses argv[argc] for the function arguments.
1481 */
1482 void *
1483call_func_retstr(func, argc, argv, safe)
1484 char_u *func;
1485 int argc;
1486 char_u **argv;
1487 int safe; /* use the sandbox */
1488{
1489 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001490 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001491
1492 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1493 return NULL;
1494
1495 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return retval;
1498}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001502 * Call vimL function "func" and return the result as a number.
1503 * Returns -1 when calling the function fails.
1504 * Uses argv[argc] for the function arguments.
1505 */
1506 long
1507call_func_retnr(func, argc, argv, safe)
1508 char_u *func;
1509 int argc;
1510 char_u **argv;
1511 int safe; /* use the sandbox */
1512{
1513 typval_T rettv;
1514 long retval;
1515
1516 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1517 return -1;
1518
1519 retval = get_tv_number_chk(&rettv, NULL);
1520 clear_tv(&rettv);
1521 return retval;
1522}
1523#endif
1524
1525/*
1526 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001527 * Uses argv[argc] for the function arguments.
1528 */
1529 void *
1530call_func_retlist(func, argc, argv, safe)
1531 char_u *func;
1532 int argc;
1533 char_u **argv;
1534 int safe; /* use the sandbox */
1535{
1536 typval_T rettv;
1537
1538 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1539 return NULL;
1540
1541 if (rettv.v_type != VAR_LIST)
1542 {
1543 clear_tv(&rettv);
1544 return NULL;
1545 }
1546
1547 return rettv.vval.v_list;
1548}
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551
1552/*
1553 * Save the current function call pointer, and set it to NULL.
1554 * Used when executing autocommands and for ":source".
1555 */
1556 void *
1557save_funccal()
1558{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001559 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 current_funccal = NULL;
1562 return (void *)fc;
1563}
1564
1565 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001566restore_funccal(vfc)
1567 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001569 funccall_T *fc = (funccall_T *)vfc;
1570
1571 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572}
1573
Bram Moolenaar05159a02005-02-26 23:04:13 +00001574#if defined(FEAT_PROFILE) || defined(PROTO)
1575/*
1576 * Prepare profiling for entering a child or something else that is not
1577 * counted for the script/function itself.
1578 * Should always be called in pair with prof_child_exit().
1579 */
1580 void
1581prof_child_enter(tm)
1582 proftime_T *tm; /* place to store waittime */
1583{
1584 funccall_T *fc = current_funccal;
1585
1586 if (fc != NULL && fc->func->uf_profiling)
1587 profile_start(&fc->prof_child);
1588 script_prof_save(tm);
1589}
1590
1591/*
1592 * Take care of time spent in a child.
1593 * Should always be called after prof_child_enter().
1594 */
1595 void
1596prof_child_exit(tm)
1597 proftime_T *tm; /* where waittime was stored */
1598{
1599 funccall_T *fc = current_funccal;
1600
1601 if (fc != NULL && fc->func->uf_profiling)
1602 {
1603 profile_end(&fc->prof_child);
1604 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1605 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1606 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1607 }
1608 script_prof_restore(tm);
1609}
1610#endif
1611
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#ifdef FEAT_FOLDING
1614/*
1615 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1616 * it in "*cp". Doesn't give error messages.
1617 */
1618 int
1619eval_foldexpr(arg, cp)
1620 char_u *arg;
1621 int *cp;
1622{
Bram Moolenaar33570922005-01-25 22:26:29 +00001623 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 int retval;
1625 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001626 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1627 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001630 if (use_sandbox)
1631 ++sandbox;
1632 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 retval = 0;
1636 else
1637 {
1638 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 if (tv.v_type == VAR_NUMBER)
1640 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001641 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 retval = 0;
1643 else
1644 {
1645 /* If the result is a string, check if there is a non-digit before
1646 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (!VIM_ISDIGIT(*s) && *s != '-')
1649 *cp = *s++;
1650 retval = atol((char *)s);
1651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001655 if (use_sandbox)
1656 --sandbox;
1657 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 return retval;
1660}
1661#endif
1662
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 * ":let" list all variable values
1665 * ":let var1 var2" list variable values
1666 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 * ":let var += expr" assignment command.
1668 * ":let var -= expr" assignment command.
1669 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 */
1672 void
1673ex_let(eap)
1674 exarg_T *eap;
1675{
1676 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001678 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 int var_count = 0;
1681 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001682 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001683 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaardb552d602006-03-23 22:59:57 +00001685 argend = skip_var_list(arg, &var_count, &semicolon);
1686 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001688 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1689 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001690 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001693 /*
1694 * ":let" without "=": list variables
1695 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (*arg == '[')
1697 EMSG(_(e_invarg));
1698 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 /* ":let var1 var2" */
1700 arg = list_arg_vars(eap, arg);
1701 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001704 list_glob_vars();
1705 list_buf_vars();
1706 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001707#ifdef FEAT_WINDOWS
1708 list_tab_vars();
1709#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001710 list_script_vars();
1711 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001712 list_vim_vars();
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 eap->nextcmd = check_nextcmd(arg);
1715 }
1716 else
1717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001718 op[0] = '=';
1719 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001720 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 {
1722 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1723 op[0] = expr[-1]; /* +=, -= or .= */
1724 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (eap->skip)
1728 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (eap->skip)
1731 {
1732 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 --emsg_skip;
1735 }
1736 else if (i != FAIL)
1737 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742 }
1743}
1744
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745/*
1746 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1747 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 * When "nextchars" is not NULL it points to a string with characters that
1749 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1750 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 * Returns OK or FAIL;
1752 */
1753 static int
1754ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1755 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001756 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 int copy; /* copy values from "tv", don't move */
1758 int semicolon; /* from skip_var_list() */
1759 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
1762 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001765 listitem_T *item;
1766 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767
1768 if (*arg != '[')
1769 {
1770 /*
1771 * ":let var = expr" or ":for var in list"
1772 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 return FAIL;
1775 return OK;
1776 }
1777
1778 /*
1779 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1780 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001781 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 {
1783 EMSG(_(e_listreq));
1784 return FAIL;
1785 }
1786
1787 i = list_len(l);
1788 if (semicolon == 0 && var_count < i)
1789 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001790 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 return FAIL;
1792 }
1793 if (var_count - semicolon > i)
1794 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001795 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 return FAIL;
1797 }
1798
1799 item = l->lv_first;
1800 while (*arg != ']')
1801 {
1802 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 item = item->li_next;
1805 if (arg == NULL)
1806 return FAIL;
1807
1808 arg = skipwhite(arg);
1809 if (*arg == ';')
1810 {
1811 /* Put the rest of the list (may be empty) in the var after ';'.
1812 * Create a new list for this. */
1813 l = list_alloc();
1814 if (l == NULL)
1815 return FAIL;
1816 while (item != NULL)
1817 {
1818 list_append_tv(l, &item->li_tv);
1819 item = item->li_next;
1820 }
1821
1822 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001823 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 ltv.vval.v_list = l;
1825 l->lv_refcount = 1;
1826
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1828 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 clear_tv(&ltv);
1830 if (arg == NULL)
1831 return FAIL;
1832 break;
1833 }
1834 else if (*arg != ',' && *arg != ']')
1835 {
1836 EMSG2(_(e_intern2), "ex_let_vars()");
1837 return FAIL;
1838 }
1839 }
1840
1841 return OK;
1842}
1843
1844/*
1845 * Skip over assignable variable "var" or list of variables "[var, var]".
1846 * Used for ":let varvar = expr" and ":for varvar in expr".
1847 * For "[var, var]" increment "*var_count" for each variable.
1848 * for "[var, var; var]" set "semicolon".
1849 * Return NULL for an error.
1850 */
1851 static char_u *
1852skip_var_list(arg, var_count, semicolon)
1853 char_u *arg;
1854 int *var_count;
1855 int *semicolon;
1856{
1857 char_u *p, *s;
1858
1859 if (*arg == '[')
1860 {
1861 /* "[var, var]": find the matching ']'. */
1862 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001863 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 {
1865 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1866 s = skip_var_one(p);
1867 if (s == p)
1868 {
1869 EMSG2(_(e_invarg2), p);
1870 return NULL;
1871 }
1872 ++*var_count;
1873
1874 p = skipwhite(s);
1875 if (*p == ']')
1876 break;
1877 else if (*p == ';')
1878 {
1879 if (*semicolon == 1)
1880 {
1881 EMSG(_("Double ; in list of variables"));
1882 return NULL;
1883 }
1884 *semicolon = 1;
1885 }
1886 else if (*p != ',')
1887 {
1888 EMSG2(_(e_invarg2), p);
1889 return NULL;
1890 }
1891 }
1892 return p + 1;
1893 }
1894 else
1895 return skip_var_one(arg);
1896}
1897
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001898/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001899 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1900 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 static char_u *
1903skip_var_one(arg)
1904 char_u *arg;
1905{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001906 if (*arg == '@' && arg[1] != NUL)
1907 return arg + 2;
1908 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1909 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910}
1911
Bram Moolenaara7043832005-01-21 11:56:39 +00001912/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 * List variables for hashtab "ht" with prefix "prefix".
1914 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001917list_hashtable_vars(ht, prefix, empty)
1918 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001921{
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 hashitem_T *hi;
1923 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001924 int todo;
1925
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001926 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001927 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1928 {
1929 if (!HASHITEM_EMPTY(hi))
1930 {
1931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 di = HI2DI(hi);
1933 if (empty || di->di_tv.v_type != VAR_STRING
1934 || di->di_tv.vval.v_string != NULL)
1935 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 }
1937 }
1938}
1939
1940/*
1941 * List global variables.
1942 */
1943 static void
1944list_glob_vars()
1945{
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001947}
1948
1949/*
1950 * List buffer variables.
1951 */
1952 static void
1953list_buf_vars()
1954{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001955 char_u numbuf[NUMBUFLEN];
1956
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958
1959 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1960 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001961}
1962
1963/*
1964 * List window variables.
1965 */
1966 static void
1967list_win_vars()
1968{
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970}
1971
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001972#ifdef FEAT_WINDOWS
1973/*
1974 * List tab page variables.
1975 */
1976 static void
1977list_tab_vars()
1978{
1979 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1980}
1981#endif
1982
Bram Moolenaara7043832005-01-21 11:56:39 +00001983/*
1984 * List Vim variables.
1985 */
1986 static void
1987list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988{
Bram Moolenaar33570922005-01-25 22:26:29 +00001989 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990}
1991
1992/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001993 * List script-local variables, if there is a script.
1994 */
1995 static void
1996list_script_vars()
1997{
1998 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1999 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2000}
2001
2002/*
2003 * List function variables, if there is a function.
2004 */
2005 static void
2006list_func_vars()
2007{
2008 if (current_funccal != NULL)
2009 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2010 (char_u *)"l:", FALSE);
2011}
2012
2013/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 * List variables in "arg".
2015 */
2016 static char_u *
2017list_arg_vars(eap, arg)
2018 exarg_T *eap;
2019 char_u *arg;
2020{
2021 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024 char_u *name_start;
2025 char_u *arg_subsc;
2026 char_u *tofree;
2027 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028
2029 while (!ends_excmd(*arg) && !got_int)
2030 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002033 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2035 {
2036 emsg_severe = TRUE;
2037 EMSG(_(e_trailing));
2038 break;
2039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 /* get_name_len() takes care of expanding curly braces */
2044 name_start = name = arg;
2045 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2046 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002048 /* This is mainly to keep test 49 working: when expanding
2049 * curly braces fails overrule the exception error message. */
2050 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 emsg_severe = TRUE;
2053 EMSG2(_(e_invarg2), arg);
2054 break;
2055 }
2056 error = TRUE;
2057 }
2058 else
2059 {
2060 if (tofree != NULL)
2061 name = tofree;
2062 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 else
2065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 /* handle d.key, l[idx], f(expr) */
2067 arg_subsc = arg;
2068 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 case 'g': list_glob_vars(); break;
2077 case 'b': list_buf_vars(); break;
2078 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002079#ifdef FEAT_WINDOWS
2080 case 't': list_tab_vars(); break;
2081#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002083 case 's': list_script_vars(); break;
2084 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 default:
2086 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 }
2089 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090 {
2091 char_u numbuf[NUMBUFLEN];
2092 char_u *tf;
2093 int c;
2094 char_u *s;
2095
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002096 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 c = *arg;
2098 *arg = NUL;
2099 list_one_var_a((char_u *)"",
2100 arg == arg_subsc ? name : name_start,
2101 tv.v_type, s == NULL ? (char_u *)"" : s);
2102 *arg = c;
2103 vim_free(tf);
2104 }
2105 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 }
2108 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109
2110 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112
2113 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 }
2115
2116 return arg;
2117}
2118
2119/*
2120 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2121 * Returns a pointer to the char just after the var name.
2122 * Returns NULL if there is an error.
2123 */
2124 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002125ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131{
2132 int c1;
2133 char_u *name;
2134 char_u *p;
2135 char_u *arg_end = NULL;
2136 int len;
2137 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139
2140 /*
2141 * ":let $VAR = expr": Set environment variable.
2142 */
2143 if (*arg == '$')
2144 {
2145 /* Find the end of the name. */
2146 ++arg;
2147 name = arg;
2148 len = get_env_len(&arg);
2149 if (len == 0)
2150 EMSG2(_(e_invarg2), name - 1);
2151 else
2152 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002153 if (op != NULL && (*op == '+' || *op == '-'))
2154 EMSG2(_(e_letwrong), op);
2155 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 EMSG(_(e_letunexp));
2158 else
2159 {
2160 c1 = name[len];
2161 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 p = get_tv_string_chk(tv);
2163 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 {
2165 int mustfree = FALSE;
2166 char_u *s = vim_getenv(name, &mustfree);
2167
2168 if (s != NULL)
2169 {
2170 p = tofree = concat_str(s, p);
2171 if (mustfree)
2172 vim_free(s);
2173 }
2174 }
2175 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002178 if (STRICMP(name, "HOME") == 0)
2179 init_homedir();
2180 else if (didset_vim && STRICMP(name, "VIM") == 0)
2181 didset_vim = FALSE;
2182 else if (didset_vimruntime
2183 && STRICMP(name, "VIMRUNTIME") == 0)
2184 didset_vimruntime = FALSE;
2185 arg_end = arg;
2186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 }
2190 }
2191 }
2192
2193 /*
2194 * ":let &option = expr": Set option value.
2195 * ":let &l:option = expr": Set local option value.
2196 * ":let &g:option = expr": Set global option value.
2197 */
2198 else if (*arg == '&')
2199 {
2200 /* Find the end of the name. */
2201 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002202 if (p == NULL || (endchars != NULL
2203 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 EMSG(_(e_letunexp));
2205 else
2206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 long n;
2208 int opt_type;
2209 long numval;
2210 char_u *stringval = NULL;
2211 char_u *s;
2212
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 c1 = *p;
2214 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002215
2216 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002217 s = get_tv_string_chk(tv); /* != NULL if number or string */
2218 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002219 {
2220 opt_type = get_option_value(arg, &numval,
2221 &stringval, opt_flags);
2222 if ((opt_type == 1 && *op == '.')
2223 || (opt_type == 0 && *op != '.'))
2224 EMSG2(_(e_letwrong), op);
2225 else
2226 {
2227 if (opt_type == 1) /* number */
2228 {
2229 if (*op == '+')
2230 n = numval + n;
2231 else
2232 n = numval - n;
2233 }
2234 else if (opt_type == 0 && stringval != NULL) /* string */
2235 {
2236 s = concat_str(stringval, s);
2237 vim_free(stringval);
2238 stringval = s;
2239 }
2240 }
2241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 if (s != NULL)
2243 {
2244 set_option_value(arg, n, s, opt_flags);
2245 arg_end = p;
2246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 }
2250 }
2251
2252 /*
2253 * ":let @r = expr": Set register contents.
2254 */
2255 else if (*arg == '@')
2256 {
2257 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 if (op != NULL && (*op == '+' || *op == '-'))
2259 EMSG2(_(e_letwrong), op);
2260 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002261 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 EMSG(_(e_letunexp));
2263 else
2264 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002265 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 char_u *s;
2267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002268 p = get_tv_string_chk(tv);
2269 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002271 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 if (s != NULL)
2273 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002274 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 vim_free(s);
2276 }
2277 }
2278 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002281 arg_end = arg + 1;
2282 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002283 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
2286
2287 /*
2288 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002291 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002295 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2299 EMSG(_(e_letunexp));
2300 else
2301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 arg_end = p;
2304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 else
2310 EMSG2(_(e_invarg2), arg);
2311
2312 return arg_end;
2313}
2314
2315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002316 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2317 */
2318 static int
2319check_changedtick(arg)
2320 char_u *arg;
2321{
2322 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2323 {
2324 EMSG2(_(e_readonlyvar), arg);
2325 return TRUE;
2326 }
2327 return FALSE;
2328}
2329
2330/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 * Get an lval: variable, Dict item or List item that can be assigned a value
2332 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2333 * "name.key", "name.key[expr]" etc.
2334 * Indexing only works if "name" is an existing List or Dictionary.
2335 * "name" points to the start of the name.
2336 * If "rettv" is not NULL it points to the value to be assigned.
2337 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2338 * wrong; must end in space or cmd separator.
2339 *
2340 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002341 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 */
2344 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002345get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002347 typval_T *rettv;
2348 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 int unlet;
2350 int skip;
2351 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002352 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 char_u *expr_start, *expr_end;
2356 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002357 dictitem_T *v;
2358 typval_T var1;
2359 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002361 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002364 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002367 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368
2369 if (skip)
2370 {
2371 /* When skipping just find the end of the name. */
2372 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002373 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 }
2375
2376 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002377 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (expr_start != NULL)
2379 {
2380 /* Don't expand the name when we already know there is an error. */
2381 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2382 && *p != '[' && *p != '.')
2383 {
2384 EMSG(_(e_trailing));
2385 return NULL;
2386 }
2387
2388 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2389 if (lp->ll_exp_name == NULL)
2390 {
2391 /* Report an invalid expression in braces, unless the
2392 * expression evaluation has been cancelled due to an
2393 * aborting error, an interrupt, or an exception. */
2394 if (!aborting() && !quiet)
2395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002396 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 EMSG2(_(e_invarg2), name);
2398 return NULL;
2399 }
2400 }
2401 lp->ll_name = lp->ll_exp_name;
2402 }
2403 else
2404 lp->ll_name = name;
2405
2406 /* Without [idx] or .key we are done. */
2407 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2408 return p;
2409
2410 cc = *p;
2411 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002412 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (v == NULL && !quiet)
2414 EMSG2(_(e_undefvar), lp->ll_name);
2415 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 if (v == NULL)
2417 return NULL;
2418
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 /*
2420 * Loop until no more [idx] or .key is following.
2421 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002422 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2426 && !(lp->ll_tv->v_type == VAR_DICT
2427 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 if (!quiet)
2430 EMSG(_("E689: Can only index a List or Dictionary"));
2431 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (!quiet)
2436 EMSG(_("E708: [:] must come last"));
2437 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002439
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 len = -1;
2441 if (*p == '.')
2442 {
2443 key = p + 1;
2444 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2445 ;
2446 if (len == 0)
2447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
2449 EMSG(_(e_emptykey));
2450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 p = key + len;
2453 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002454 else
2455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002457 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 if (*p == ':')
2459 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002460 else
2461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 empty1 = FALSE;
2463 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 if (get_tv_string_chk(&var1) == NULL)
2466 {
2467 /* not a number or string */
2468 clear_tv(&var1);
2469 return NULL;
2470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 }
2472
2473 /* Optionally get the second index [ :expr]. */
2474 if (*p == ':')
2475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002480 if (!empty1)
2481 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (rettv != NULL && (rettv->v_type != VAR_LIST
2485 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (!quiet)
2488 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 if (!empty1)
2490 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493 p = skipwhite(p + 1);
2494 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 else
2497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2500 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 if (!empty1)
2502 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 if (get_tv_string_chk(&var2) == NULL)
2506 {
2507 /* not a number or string */
2508 if (!empty1)
2509 clear_tv(&var1);
2510 clear_tv(&var2);
2511 return NULL;
2512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 if (!empty1)
2524 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529
2530 /* Skip to past ']'. */
2531 ++p;
2532 }
2533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 {
2536 if (len == -1)
2537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002539 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 if (*key == NUL)
2541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (!quiet)
2543 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
2547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 lp->ll_list = NULL;
2549 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002550 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002553 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (len == -1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 if (len == -1)
2567 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 p = NULL;
2570 break;
2571 }
2572 if (len == -1)
2573 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 }
2576 else
2577 {
2578 /*
2579 * Get the number and item for the only or first index of the List.
2580 */
2581 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 else
2584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 clear_tv(&var1);
2587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 lp->ll_list = lp->ll_tv->vval.v_list;
2590 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2591 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002593 if (lp->ll_n1 < 0)
2594 {
2595 lp->ll_n1 = 0;
2596 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2597 }
2598 }
2599 if (lp->ll_li == NULL)
2600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
2605
2606 /*
2607 * May need to find the item or absolute index for the second
2608 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * When no index given: "lp->ll_empty2" is TRUE.
2610 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002614 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2625 if (lp->ll_n1 < 0)
2626 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2627 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 }
2630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 }
2634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return p;
2636}
2637
2638/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 */
2641 static void
2642clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644{
2645 vim_free(lp->ll_exp_name);
2646 vim_free(lp->ll_newkey);
2647}
2648
2649/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002650 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 */
2654 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002658 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661{
2662 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002663 listitem_T *ri;
2664 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665
2666 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 cc = *endp;
2671 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 if (op != NULL && *op != '=')
2673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002676 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002677 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002678 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 {
2680 if (tv_op(&tv, rettv, op) == OK)
2681 set_var(lp->ll_name, &tv, FALSE);
2682 clear_tv(&tv);
2683 }
2684 }
2685 else
2686 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002690 else if (tv_check_lock(lp->ll_newkey == NULL
2691 ? lp->ll_tv->v_lock
2692 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2693 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 else if (lp->ll_range)
2695 {
2696 /*
2697 * Assign the List values to the list items.
2698 */
2699 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 if (op != NULL && *op != '=')
2702 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2703 else
2704 {
2705 clear_tv(&lp->ll_li->li_tv);
2706 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 ri = ri->li_next;
2709 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2710 break;
2711 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002714 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 ri = NULL;
2717 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_li = lp->ll_li->li_next;
2721 ++lp->ll_n1;
2722 }
2723 if (ri != NULL)
2724 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 else if (lp->ll_empty2
2726 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 : lp->ll_n1 != lp->ll_n2)
2728 EMSG(_("E711: List value has not enough items"));
2729 }
2730 else
2731 {
2732 /*
2733 * Assign to a List or Dictionary item.
2734 */
2735 if (lp->ll_newkey != NULL)
2736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 if (op != NULL && *op != '=')
2738 {
2739 EMSG2(_(e_letwrong), op);
2740 return;
2741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (di == NULL)
2746 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002747 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2748 {
2749 vim_free(di);
2750 return;
2751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002754 else if (op != NULL && *op != '=')
2755 {
2756 tv_op(lp->ll_tv, rettv, op);
2757 return;
2758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /*
2763 * Assign the value to the variable or list item.
2764 */
2765 if (copy)
2766 copy_tv(rettv, lp->ll_tv);
2767 else
2768 {
2769 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002770 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772 }
2773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002774}
2775
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2778 * Returns OK or FAIL.
2779 */
2780 static int
2781tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002782 typval_T *tv1;
2783 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 char_u *op;
2785{
2786 long n;
2787 char_u numbuf[NUMBUFLEN];
2788 char_u *s;
2789
2790 /* Can't do anything with a Funcref or a Dict on the right. */
2791 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2792 {
2793 switch (tv1->v_type)
2794 {
2795 case VAR_DICT:
2796 case VAR_FUNC:
2797 break;
2798
2799 case VAR_LIST:
2800 if (*op != '+' || tv2->v_type != VAR_LIST)
2801 break;
2802 /* List += List */
2803 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2804 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2805 return OK;
2806
2807 case VAR_NUMBER:
2808 case VAR_STRING:
2809 if (tv2->v_type == VAR_LIST)
2810 break;
2811 if (*op == '+' || *op == '-')
2812 {
2813 /* nr += nr or nr -= nr*/
2814 n = get_tv_number(tv1);
2815 if (*op == '+')
2816 n += get_tv_number(tv2);
2817 else
2818 n -= get_tv_number(tv2);
2819 clear_tv(tv1);
2820 tv1->v_type = VAR_NUMBER;
2821 tv1->vval.v_number = n;
2822 }
2823 else
2824 {
2825 /* str .= str */
2826 s = get_tv_string(tv1);
2827 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2828 clear_tv(tv1);
2829 tv1->v_type = VAR_STRING;
2830 tv1->vval.v_string = s;
2831 }
2832 return OK;
2833 }
2834 }
2835
2836 EMSG2(_(e_letwrong), op);
2837 return FAIL;
2838}
2839
2840/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002841 * Add a watcher to a list.
2842 */
2843 static void
2844list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 list_T *l;
2846 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847{
2848 lw->lw_next = l->lv_watch;
2849 l->lv_watch = lw;
2850}
2851
2852/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002853 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 * No warning when it isn't found...
2855 */
2856 static void
2857list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 list_T *l;
2859 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860{
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862
2863 lwp = &l->lv_watch;
2864 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2865 {
2866 if (lw == lwrem)
2867 {
2868 *lwp = lw->lw_next;
2869 break;
2870 }
2871 lwp = &lw->lw_next;
2872 }
2873}
2874
2875/*
2876 * Just before removing an item from a list: advance watchers to the next
2877 * item.
2878 */
2879 static void
2880list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 list_T *l;
2882 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883{
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885
2886 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2887 if (lw->lw_item == item)
2888 lw->lw_item = item->li_next;
2889}
2890
2891/*
2892 * Evaluate the expression used in a ":for var in expr" command.
2893 * "arg" points to "var".
2894 * Set "*errp" to TRUE for an error, FALSE otherwise;
2895 * Return a pointer that holds the info. Null when there is an error.
2896 */
2897 void *
2898eval_for_line(arg, errp, nextcmdp, skip)
2899 char_u *arg;
2900 int *errp;
2901 char_u **nextcmdp;
2902 int skip;
2903{
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 typval_T tv;
2907 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908
2909 *errp = TRUE; /* default: there is an error */
2910
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 if (fi == NULL)
2913 return NULL;
2914
2915 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2916 if (expr == NULL)
2917 return fi;
2918
2919 expr = skipwhite(expr);
2920 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2921 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002922 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 return fi;
2924 }
2925
2926 if (skip)
2927 ++emsg_skip;
2928 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2929 {
2930 *errp = FALSE;
2931 if (!skip)
2932 {
2933 l = tv.vval.v_list;
2934 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002937 clear_tv(&tv);
2938 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002939 else
2940 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002941 /* No need to increment the refcount, it's already set for the
2942 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943 fi->fi_list = l;
2944 list_add_watch(l, &fi->fi_lw);
2945 fi->fi_lw.lw_item = l->lv_first;
2946 }
2947 }
2948 }
2949 if (skip)
2950 --emsg_skip;
2951
2952 return fi;
2953}
2954
2955/*
2956 * Use the first item in a ":for" list. Advance to the next.
2957 * Assign the values to the variable (list). "arg" points to the first one.
2958 * Return TRUE when a valid item was found, FALSE when at end of list or
2959 * something wrong.
2960 */
2961 int
2962next_for_item(fi_void, arg)
2963 void *fi_void;
2964 char_u *arg;
2965{
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002968 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969
2970 item = fi->fi_lw.lw_item;
2971 if (item == NULL)
2972 result = FALSE;
2973 else
2974 {
2975 fi->fi_lw.lw_item = item->li_next;
2976 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2977 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2978 }
2979 return result;
2980}
2981
2982/*
2983 * Free the structure used to store info used by ":for".
2984 */
2985 void
2986free_for_info(fi_void)
2987 void *fi_void;
2988{
Bram Moolenaar33570922005-01-25 22:26:29 +00002989 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002990
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002991 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002992 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002993 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002994 list_unref(fi->fi_list);
2995 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002996 vim_free(fi);
2997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3000
3001 void
3002set_context_for_expression(xp, arg, cmdidx)
3003 expand_T *xp;
3004 char_u *arg;
3005 cmdidx_T cmdidx;
3006{
3007 int got_eq = FALSE;
3008 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 if (cmdidx == CMD_let)
3012 {
3013 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003014 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015 {
3016 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003017 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018 {
3019 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021 if (vim_iswhite(*p))
3022 break;
3023 }
3024 return;
3025 }
3026 }
3027 else
3028 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3029 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 while ((xp->xp_pattern = vim_strpbrk(arg,
3031 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3032 {
3033 c = *xp->xp_pattern;
3034 if (c == '&')
3035 {
3036 c = xp->xp_pattern[1];
3037 if (c == '&')
3038 {
3039 ++xp->xp_pattern;
3040 xp->xp_context = cmdidx != CMD_let || got_eq
3041 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3042 }
3043 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003046 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3047 xp->xp_pattern += 2;
3048
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 else if (c == '$')
3052 {
3053 /* environment variable */
3054 xp->xp_context = EXPAND_ENV_VARS;
3055 }
3056 else if (c == '=')
3057 {
3058 got_eq = TRUE;
3059 xp->xp_context = EXPAND_EXPRESSION;
3060 }
3061 else if (c == '<'
3062 && xp->xp_context == EXPAND_FUNCTIONS
3063 && vim_strchr(xp->xp_pattern, '(') == NULL)
3064 {
3065 /* Function name can start with "<SNR>" */
3066 break;
3067 }
3068 else if (cmdidx != CMD_let || got_eq)
3069 {
3070 if (c == '"') /* string */
3071 {
3072 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3073 if (c == '\\' && xp->xp_pattern[1] != NUL)
3074 ++xp->xp_pattern;
3075 xp->xp_context = EXPAND_NOTHING;
3076 }
3077 else if (c == '\'') /* literal string */
3078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003079 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3081 /* skip */ ;
3082 xp->xp_context = EXPAND_NOTHING;
3083 }
3084 else if (c == '|')
3085 {
3086 if (xp->xp_pattern[1] == '|')
3087 {
3088 ++xp->xp_pattern;
3089 xp->xp_context = EXPAND_EXPRESSION;
3090 }
3091 else
3092 xp->xp_context = EXPAND_COMMANDS;
3093 }
3094 else
3095 xp->xp_context = EXPAND_EXPRESSION;
3096 }
3097 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003098 /* Doesn't look like something valid, expand as an expression
3099 * anyway. */
3100 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 arg = xp->xp_pattern;
3102 if (*arg != NUL)
3103 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3104 /* skip */ ;
3105 }
3106 xp->xp_pattern = arg;
3107}
3108
3109#endif /* FEAT_CMDL_COMPL */
3110
3111/*
3112 * ":1,25call func(arg1, arg2)" function call.
3113 */
3114 void
3115ex_call(eap)
3116 exarg_T *eap;
3117{
3118 char_u *arg = eap->arg;
3119 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 linenr_T lnum;
3125 int doesrange;
3126 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003129 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3130 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 if (tofree == NULL)
3132 return;
3133
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003134 /* Increase refcount on dictionary, it could get deleted when evaluating
3135 * the arguments. */
3136 if (fudi.fd_dict != NULL)
3137 ++fudi.fd_dict->dv_refcount;
3138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003140 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaar532c7802005-01-27 14:44:31 +00003143 /* Skip white space to allow ":call func ()". Not good, but required for
3144 * backward compatibility. */
3145 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 if (*startarg != '(')
3149 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003150 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 goto end;
3152 }
3153
3154 /*
3155 * When skipping, evaluate the function once, to find the end of the
3156 * arguments.
3157 * When the function takes a range, this is discovered after the first
3158 * call, and the loop is broken.
3159 */
3160 if (eap->skip)
3161 {
3162 ++emsg_skip;
3163 lnum = eap->line2; /* do it once, also with an invalid range */
3164 }
3165 else
3166 lnum = eap->line1;
3167 for ( ; lnum <= eap->line2; ++lnum)
3168 {
3169 if (!eap->skip && eap->addr_count > 0)
3170 {
3171 curwin->w_cursor.lnum = lnum;
3172 curwin->w_cursor.col = 0;
3173 }
3174 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003175 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003176 eap->line1, eap->line2, &doesrange,
3177 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 failed = TRUE;
3180 break;
3181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (doesrange || eap->skip)
3184 break;
3185 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003186 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003188 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (aborting())
3190 break;
3191 }
3192 if (eap->skip)
3193 --emsg_skip;
3194
3195 if (!failed)
3196 {
3197 /* Check for trailing illegal characters and a following command. */
3198 if (!ends_excmd(*arg))
3199 {
3200 emsg_severe = TRUE;
3201 EMSG(_(e_trailing));
3202 }
3203 else
3204 eap->nextcmd = check_nextcmd(arg);
3205 }
3206
3207end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003208 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003209 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210}
3211
3212/*
3213 * ":unlet[!] var1 ... " command.
3214 */
3215 void
3216ex_unlet(eap)
3217 exarg_T *eap;
3218{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003219 ex_unletlock(eap, eap->arg, 0);
3220}
3221
3222/*
3223 * ":lockvar" and ":unlockvar" commands
3224 */
3225 void
3226ex_lockvar(eap)
3227 exarg_T *eap;
3228{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003230 int deep = 2;
3231
3232 if (eap->forceit)
3233 deep = -1;
3234 else if (vim_isdigit(*arg))
3235 {
3236 deep = getdigits(&arg);
3237 arg = skipwhite(arg);
3238 }
3239
3240 ex_unletlock(eap, arg, deep);
3241}
3242
3243/*
3244 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3245 */
3246 static void
3247ex_unletlock(eap, argstart, deep)
3248 exarg_T *eap;
3249 char_u *argstart;
3250 int deep;
3251{
3252 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003255 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
3257 do
3258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003260 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3261 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 if (lv.ll_name == NULL)
3263 error = TRUE; /* error but continue parsing */
3264 if (name_end == NULL || (!vim_iswhite(*name_end)
3265 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003267 if (name_end != NULL)
3268 {
3269 emsg_severe = TRUE;
3270 EMSG(_(e_trailing));
3271 }
3272 if (!(eap->skip || error))
3273 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 break;
3275 }
3276
3277 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278 {
3279 if (eap->cmdidx == CMD_unlet)
3280 {
3281 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3282 error = TRUE;
3283 }
3284 else
3285 {
3286 if (do_lock_var(&lv, name_end, deep,
3287 eap->cmdidx == CMD_lockvar) == FAIL)
3288 error = TRUE;
3289 }
3290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003292 if (!eap->skip)
3293 clear_lval(&lv);
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 arg = skipwhite(name_end);
3296 } while (!ends_excmd(*arg));
3297
3298 eap->nextcmd = check_nextcmd(arg);
3299}
3300
Bram Moolenaar8c711452005-01-14 21:53:12 +00003301 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003302do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003304 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003305 int forceit;
3306{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307 int ret = OK;
3308 int cc;
3309
3310 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003311 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 cc = *name_end;
3313 *name_end = NUL;
3314
3315 /* Normal name or expanded name. */
3316 if (check_changedtick(lp->ll_name))
3317 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003319 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003320 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003321 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003322 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3323 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003324 else if (lp->ll_range)
3325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003327
3328 /* Delete a range of List items. */
3329 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3330 {
3331 li = lp->ll_li->li_next;
3332 listitem_remove(lp->ll_list, lp->ll_li);
3333 lp->ll_li = li;
3334 ++lp->ll_n1;
3335 }
3336 }
3337 else
3338 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003340 /* unlet a List item. */
3341 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003344 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003345 }
3346
3347 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348}
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350/*
3351 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003352 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 */
3354 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003357 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
Bram Moolenaar33570922005-01-25 22:26:29 +00003359 hashtab_T *ht;
3360 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003361 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
Bram Moolenaar33570922005-01-25 22:26:29 +00003363 ht = find_var_ht(name, &varname);
3364 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003366 hi = hash_find(ht, varname);
3367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003368 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003369 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3370 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 if (var_check_ro(HI2DI(hi)->di_flags, name))
3372 return FAIL;
3373 delete_var(ht, hi);
3374 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003377 if (forceit)
3378 return OK;
3379 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 return FAIL;
3381}
3382
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003383/*
3384 * Lock or unlock variable indicated by "lp".
3385 * "deep" is the levels to go (-1 for unlimited);
3386 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3387 */
3388 static int
3389do_lock_var(lp, name_end, deep, lock)
3390 lval_T *lp;
3391 char_u *name_end;
3392 int deep;
3393 int lock;
3394{
3395 int ret = OK;
3396 int cc;
3397 dictitem_T *di;
3398
3399 if (deep == 0) /* nothing to do */
3400 return OK;
3401
3402 if (lp->ll_tv == NULL)
3403 {
3404 cc = *name_end;
3405 *name_end = NUL;
3406
3407 /* Normal name or expanded name. */
3408 if (check_changedtick(lp->ll_name))
3409 ret = FAIL;
3410 else
3411 {
3412 di = find_var(lp->ll_name, NULL);
3413 if (di == NULL)
3414 ret = FAIL;
3415 else
3416 {
3417 if (lock)
3418 di->di_flags |= DI_FLAGS_LOCK;
3419 else
3420 di->di_flags &= ~DI_FLAGS_LOCK;
3421 item_lock(&di->di_tv, deep, lock);
3422 }
3423 }
3424 *name_end = cc;
3425 }
3426 else if (lp->ll_range)
3427 {
3428 listitem_T *li = lp->ll_li;
3429
3430 /* (un)lock a range of List items. */
3431 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3432 {
3433 item_lock(&li->li_tv, deep, lock);
3434 li = li->li_next;
3435 ++lp->ll_n1;
3436 }
3437 }
3438 else if (lp->ll_list != NULL)
3439 /* (un)lock a List item. */
3440 item_lock(&lp->ll_li->li_tv, deep, lock);
3441 else
3442 /* un(lock) a Dictionary item. */
3443 item_lock(&lp->ll_di->di_tv, deep, lock);
3444
3445 return ret;
3446}
3447
3448/*
3449 * Lock or unlock an item. "deep" is nr of levels to go.
3450 */
3451 static void
3452item_lock(tv, deep, lock)
3453 typval_T *tv;
3454 int deep;
3455 int lock;
3456{
3457 static int recurse = 0;
3458 list_T *l;
3459 listitem_T *li;
3460 dict_T *d;
3461 hashitem_T *hi;
3462 int todo;
3463
3464 if (recurse >= DICT_MAXNEST)
3465 {
3466 EMSG(_("E743: variable nested too deep for (un)lock"));
3467 return;
3468 }
3469 if (deep == 0)
3470 return;
3471 ++recurse;
3472
3473 /* lock/unlock the item itself */
3474 if (lock)
3475 tv->v_lock |= VAR_LOCKED;
3476 else
3477 tv->v_lock &= ~VAR_LOCKED;
3478
3479 switch (tv->v_type)
3480 {
3481 case VAR_LIST:
3482 if ((l = tv->vval.v_list) != NULL)
3483 {
3484 if (lock)
3485 l->lv_lock |= VAR_LOCKED;
3486 else
3487 l->lv_lock &= ~VAR_LOCKED;
3488 if (deep < 0 || deep > 1)
3489 /* recursive: lock/unlock the items the List contains */
3490 for (li = l->lv_first; li != NULL; li = li->li_next)
3491 item_lock(&li->li_tv, deep - 1, lock);
3492 }
3493 break;
3494 case VAR_DICT:
3495 if ((d = tv->vval.v_dict) != NULL)
3496 {
3497 if (lock)
3498 d->dv_lock |= VAR_LOCKED;
3499 else
3500 d->dv_lock &= ~VAR_LOCKED;
3501 if (deep < 0 || deep > 1)
3502 {
3503 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003504 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003505 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3506 {
3507 if (!HASHITEM_EMPTY(hi))
3508 {
3509 --todo;
3510 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3511 }
3512 }
3513 }
3514 }
3515 }
3516 --recurse;
3517}
3518
Bram Moolenaara40058a2005-07-11 22:42:07 +00003519/*
3520 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3521 * it refers to a List or Dictionary that is locked.
3522 */
3523 static int
3524tv_islocked(tv)
3525 typval_T *tv;
3526{
3527 return (tv->v_lock & VAR_LOCKED)
3528 || (tv->v_type == VAR_LIST
3529 && tv->vval.v_list != NULL
3530 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3531 || (tv->v_type == VAR_DICT
3532 && tv->vval.v_dict != NULL
3533 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3534}
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3537/*
3538 * Delete all "menutrans_" variables.
3539 */
3540 void
3541del_menutrans_vars()
3542{
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003544 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003547 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003548 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003549 {
3550 if (!HASHITEM_EMPTY(hi))
3551 {
3552 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3554 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003555 }
3556 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559#endif
3560
3561#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3562
3563/*
3564 * Local string buffer for the next two functions to store a variable name
3565 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3566 * get_user_var_name().
3567 */
3568
3569static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3570
3571static char_u *varnamebuf = NULL;
3572static int varnamebuflen = 0;
3573
3574/*
3575 * Function to concatenate a prefix and a variable name.
3576 */
3577 static char_u *
3578cat_prefix_varname(prefix, name)
3579 int prefix;
3580 char_u *name;
3581{
3582 int len;
3583
3584 len = (int)STRLEN(name) + 3;
3585 if (len > varnamebuflen)
3586 {
3587 vim_free(varnamebuf);
3588 len += 10; /* some additional space */
3589 varnamebuf = alloc(len);
3590 if (varnamebuf == NULL)
3591 {
3592 varnamebuflen = 0;
3593 return NULL;
3594 }
3595 varnamebuflen = len;
3596 }
3597 *varnamebuf = prefix;
3598 varnamebuf[1] = ':';
3599 STRCPY(varnamebuf + 2, name);
3600 return varnamebuf;
3601}
3602
3603/*
3604 * Function given to ExpandGeneric() to obtain the list of user defined
3605 * (global/buffer/window/built-in) variable names.
3606 */
3607/*ARGSUSED*/
3608 char_u *
3609get_user_var_name(xp, idx)
3610 expand_T *xp;
3611 int idx;
3612{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003613 static long_u gdone;
3614 static long_u bdone;
3615 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003616#ifdef FEAT_WINDOWS
3617 static long_u tdone;
3618#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003619 static int vidx;
3620 static hashitem_T *hi;
3621 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003624 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003625 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003626#ifdef FEAT_WINDOWS
3627 tdone = 0;
3628#endif
3629 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003630
3631 /* Global variables */
3632 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003634 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003635 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003636 else
3637 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003638 while (HASHITEM_EMPTY(hi))
3639 ++hi;
3640 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3641 return cat_prefix_varname('g', hi->hi_key);
3642 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003644
3645 /* b: variables */
3646 ht = &curbuf->b_vars.dv_hashtab;
3647 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003651 else
3652 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003653 while (HASHITEM_EMPTY(hi))
3654 ++hi;
3655 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003659 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return (char_u *)"b:changedtick";
3661 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003662
3663 /* w: variables */
3664 ht = &curwin->w_vars.dv_hashtab;
3665 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003667 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003669 else
3670 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003671 while (HASHITEM_EMPTY(hi))
3672 ++hi;
3673 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003675
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003676#ifdef FEAT_WINDOWS
3677 /* t: variables */
3678 ht = &curtab->tp_vars.dv_hashtab;
3679 if (tdone < ht->ht_used)
3680 {
3681 if (tdone++ == 0)
3682 hi = ht->ht_array;
3683 else
3684 ++hi;
3685 while (HASHITEM_EMPTY(hi))
3686 ++hi;
3687 return cat_prefix_varname('t', hi->hi_key);
3688 }
3689#endif
3690
Bram Moolenaar33570922005-01-25 22:26:29 +00003691 /* v: variables */
3692 if (vidx < VV_LEN)
3693 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
3695 vim_free(varnamebuf);
3696 varnamebuf = NULL;
3697 varnamebuflen = 0;
3698 return NULL;
3699}
3700
3701#endif /* FEAT_CMDL_COMPL */
3702
3703/*
3704 * types for expressions.
3705 */
3706typedef enum
3707{
3708 TYPE_UNKNOWN = 0
3709 , TYPE_EQUAL /* == */
3710 , TYPE_NEQUAL /* != */
3711 , TYPE_GREATER /* > */
3712 , TYPE_GEQUAL /* >= */
3713 , TYPE_SMALLER /* < */
3714 , TYPE_SEQUAL /* <= */
3715 , TYPE_MATCH /* =~ */
3716 , TYPE_NOMATCH /* !~ */
3717} exptype_T;
3718
3719/*
3720 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3723 */
3724
3725/*
3726 * Handle zero level expression.
3727 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003729 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * Return OK or FAIL.
3731 */
3732 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 char_u **nextcmd;
3737 int evaluate;
3738{
3739 int ret;
3740 char_u *p;
3741
3742 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (ret == FAIL || !ends_excmd(*p))
3745 {
3746 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003747 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 /*
3749 * Report the invalid expression unless the expression evaluation has
3750 * been cancelled due to an aborting error, an interrupt, or an
3751 * exception.
3752 */
3753 if (!aborting())
3754 EMSG2(_(e_invexpr2), arg);
3755 ret = FAIL;
3756 }
3757 if (nextcmd != NULL)
3758 *nextcmd = check_nextcmd(p);
3759
3760 return ret;
3761}
3762
3763/*
3764 * Handle top level expression:
3765 * expr1 ? expr0 : expr0
3766 *
3767 * "arg" must point to the first non-white of the expression.
3768 * "arg" is advanced to the next non-white after the recognized expression.
3769 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003770 * Note: "rettv.v_lock" is not set.
3771 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * Return OK or FAIL.
3773 */
3774 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 int evaluate;
3779{
3780 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783 /*
3784 * Get the first variable.
3785 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return FAIL;
3788
3789 if ((*arg)[0] == '?')
3790 {
3791 result = FALSE;
3792 if (evaluate)
3793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 int error = FALSE;
3795
3796 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003798 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003799 if (error)
3800 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802
3803 /*
3804 * Get the second variable.
3805 */
3806 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return FAIL;
3809
3810 /*
3811 * Check for the ":".
3812 */
3813 if ((*arg)[0] != ':')
3814 {
3815 EMSG(_("E109: Missing ':' after '?'"));
3816 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return FAIL;
3819 }
3820
3821 /*
3822 * Get the third variable.
3823 */
3824 *arg = skipwhite(*arg + 1);
3825 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3826 {
3827 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 return FAIL;
3830 }
3831 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003832 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835 return OK;
3836}
3837
3838/*
3839 * Handle first level expression:
3840 * expr2 || expr2 || expr2 logical OR
3841 *
3842 * "arg" must point to the first non-white of the expression.
3843 * "arg" is advanced to the next non-white after the recognized expression.
3844 *
3845 * Return OK or FAIL.
3846 */
3847 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 int evaluate;
3852{
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 long result;
3855 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
3858 /*
3859 * Get the first variable.
3860 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 return FAIL;
3863
3864 /*
3865 * Repeat until there is no following "||".
3866 */
3867 first = TRUE;
3868 result = FALSE;
3869 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3870 {
3871 if (evaluate && first)
3872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003876 if (error)
3877 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 first = FALSE;
3879 }
3880
3881 /*
3882 * Get the second variable.
3883 */
3884 *arg = skipwhite(*arg + 2);
3885 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3886 return FAIL;
3887
3888 /*
3889 * Compute the result.
3890 */
3891 if (evaluate && !result)
3892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003893 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003896 if (error)
3897 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 if (evaluate)
3900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 rettv->v_type = VAR_NUMBER;
3902 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 }
3905
3906 return OK;
3907}
3908
3909/*
3910 * Handle second level expression:
3911 * expr3 && expr3 && expr3 logical AND
3912 *
3913 * "arg" must point to the first non-white of the expression.
3914 * "arg" is advanced to the next non-white after the recognized expression.
3915 *
3916 * Return OK or FAIL.
3917 */
3918 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 int evaluate;
3923{
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 long result;
3926 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003927 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 /*
3930 * Get the first variable.
3931 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003932 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 return FAIL;
3934
3935 /*
3936 * Repeat until there is no following "&&".
3937 */
3938 first = TRUE;
3939 result = TRUE;
3940 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3941 {
3942 if (evaluate && first)
3943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003947 if (error)
3948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 first = FALSE;
3950 }
3951
3952 /*
3953 * Get the second variable.
3954 */
3955 *arg = skipwhite(*arg + 2);
3956 if (eval4(arg, &var2, evaluate && result) == FAIL)
3957 return FAIL;
3958
3959 /*
3960 * Compute the result.
3961 */
3962 if (evaluate && result)
3963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003964 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003967 if (error)
3968 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 if (evaluate)
3971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 rettv->v_type = VAR_NUMBER;
3973 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
3977 return OK;
3978}
3979
3980/*
3981 * Handle third level expression:
3982 * var1 == var2
3983 * var1 =~ var2
3984 * var1 != var2
3985 * var1 !~ var2
3986 * var1 > var2
3987 * var1 >= var2
3988 * var1 < var2
3989 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003990 * var1 is var2
3991 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * "arg" must point to the first non-white of the expression.
3994 * "arg" is advanced to the next non-white after the recognized expression.
3995 *
3996 * Return OK or FAIL.
3997 */
3998 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 int evaluate;
4003{
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u *p;
4006 int i;
4007 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004008 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 int len = 2;
4010 long n1, n2;
4011 char_u *s1, *s2;
4012 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4013 regmatch_T regmatch;
4014 int ic;
4015 char_u *save_cpo;
4016
4017 /*
4018 * Get the first variable.
4019 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return FAIL;
4022
4023 p = *arg;
4024 switch (p[0])
4025 {
4026 case '=': if (p[1] == '=')
4027 type = TYPE_EQUAL;
4028 else if (p[1] == '~')
4029 type = TYPE_MATCH;
4030 break;
4031 case '!': if (p[1] == '=')
4032 type = TYPE_NEQUAL;
4033 else if (p[1] == '~')
4034 type = TYPE_NOMATCH;
4035 break;
4036 case '>': if (p[1] != '=')
4037 {
4038 type = TYPE_GREATER;
4039 len = 1;
4040 }
4041 else
4042 type = TYPE_GEQUAL;
4043 break;
4044 case '<': if (p[1] != '=')
4045 {
4046 type = TYPE_SMALLER;
4047 len = 1;
4048 }
4049 else
4050 type = TYPE_SEQUAL;
4051 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004052 case 'i': if (p[1] == 's')
4053 {
4054 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4055 len = 5;
4056 if (!vim_isIDc(p[len]))
4057 {
4058 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4059 type_is = TRUE;
4060 }
4061 }
4062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064
4065 /*
4066 * If there is a comparitive operator, use it.
4067 */
4068 if (type != TYPE_UNKNOWN)
4069 {
4070 /* extra question mark appended: ignore case */
4071 if (p[len] == '?')
4072 {
4073 ic = TRUE;
4074 ++len;
4075 }
4076 /* extra '#' appended: match case */
4077 else if (p[len] == '#')
4078 {
4079 ic = FALSE;
4080 ++len;
4081 }
4082 /* nothing appened: use 'ignorecase' */
4083 else
4084 ic = p_ic;
4085
4086 /*
4087 * Get the second variable.
4088 */
4089 *arg = skipwhite(p + len);
4090 if (eval5(arg, &var2, evaluate) == FAIL)
4091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094 }
4095
4096 if (evaluate)
4097 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004098 if (type_is && rettv->v_type != var2.v_type)
4099 {
4100 /* For "is" a different type always means FALSE, for "notis"
4101 * it means TRUE. */
4102 n1 = (type == TYPE_NEQUAL);
4103 }
4104 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4105 {
4106 if (type_is)
4107 {
4108 n1 = (rettv->v_type == var2.v_type
4109 && rettv->vval.v_list == var2.vval.v_list);
4110 if (type == TYPE_NEQUAL)
4111 n1 = !n1;
4112 }
4113 else if (rettv->v_type != var2.v_type
4114 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4115 {
4116 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004117 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004118 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004119 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004120 clear_tv(rettv);
4121 clear_tv(&var2);
4122 return FAIL;
4123 }
4124 else
4125 {
4126 /* Compare two Lists for being equal or unequal. */
4127 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4128 if (type == TYPE_NEQUAL)
4129 n1 = !n1;
4130 }
4131 }
4132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004133 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4134 {
4135 if (type_is)
4136 {
4137 n1 = (rettv->v_type == var2.v_type
4138 && rettv->vval.v_dict == var2.vval.v_dict);
4139 if (type == TYPE_NEQUAL)
4140 n1 = !n1;
4141 }
4142 else if (rettv->v_type != var2.v_type
4143 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4144 {
4145 if (rettv->v_type != var2.v_type)
4146 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4147 else
4148 EMSG(_("E736: Invalid operation for Dictionary"));
4149 clear_tv(rettv);
4150 clear_tv(&var2);
4151 return FAIL;
4152 }
4153 else
4154 {
4155 /* Compare two Dictionaries for being equal or unequal. */
4156 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4157 if (type == TYPE_NEQUAL)
4158 n1 = !n1;
4159 }
4160 }
4161
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004162 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4163 {
4164 if (rettv->v_type != var2.v_type
4165 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4166 {
4167 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004170 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004171 clear_tv(rettv);
4172 clear_tv(&var2);
4173 return FAIL;
4174 }
4175 else
4176 {
4177 /* Compare two Funcrefs for being equal or unequal. */
4178 if (rettv->vval.v_string == NULL
4179 || var2.vval.v_string == NULL)
4180 n1 = FALSE;
4181 else
4182 n1 = STRCMP(rettv->vval.v_string,
4183 var2.vval.v_string) == 0;
4184 if (type == TYPE_NEQUAL)
4185 n1 = !n1;
4186 }
4187 }
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 /*
4190 * If one of the two variables is a number, compare as a number.
4191 * When using "=~" or "!~", always compare as string.
4192 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004193 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 n1 = get_tv_number(rettv);
4197 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 switch (type)
4199 {
4200 case TYPE_EQUAL: n1 = (n1 == n2); break;
4201 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4202 case TYPE_GREATER: n1 = (n1 > n2); break;
4203 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4204 case TYPE_SMALLER: n1 = (n1 < n2); break;
4205 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4206 case TYPE_UNKNOWN:
4207 case TYPE_MATCH:
4208 case TYPE_NOMATCH: break; /* avoid gcc warning */
4209 }
4210 }
4211 else
4212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 s1 = get_tv_string_buf(rettv, buf1);
4214 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4216 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4217 else
4218 i = 0;
4219 n1 = FALSE;
4220 switch (type)
4221 {
4222 case TYPE_EQUAL: n1 = (i == 0); break;
4223 case TYPE_NEQUAL: n1 = (i != 0); break;
4224 case TYPE_GREATER: n1 = (i > 0); break;
4225 case TYPE_GEQUAL: n1 = (i >= 0); break;
4226 case TYPE_SMALLER: n1 = (i < 0); break;
4227 case TYPE_SEQUAL: n1 = (i <= 0); break;
4228
4229 case TYPE_MATCH:
4230 case TYPE_NOMATCH:
4231 /* avoid 'l' flag in 'cpoptions' */
4232 save_cpo = p_cpo;
4233 p_cpo = (char_u *)"";
4234 regmatch.regprog = vim_regcomp(s2,
4235 RE_MAGIC + RE_STRING);
4236 regmatch.rm_ic = ic;
4237 if (regmatch.regprog != NULL)
4238 {
4239 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4240 vim_free(regmatch.regprog);
4241 if (type == TYPE_NOMATCH)
4242 n1 = !n1;
4243 }
4244 p_cpo = save_cpo;
4245 break;
4246
4247 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4248 }
4249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 clear_tv(rettv);
4251 clear_tv(&var2);
4252 rettv->v_type = VAR_NUMBER;
4253 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256
4257 return OK;
4258}
4259
4260/*
4261 * Handle fourth level expression:
4262 * + number addition
4263 * - number subtraction
4264 * . string concatenation
4265 *
4266 * "arg" must point to the first non-white of the expression.
4267 * "arg" is advanced to the next non-white after the recognized expression.
4268 *
4269 * Return OK or FAIL.
4270 */
4271 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 int evaluate;
4276{
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T var2;
4278 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 int op;
4280 long n1, n2;
4281 char_u *s1, *s2;
4282 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4283 char_u *p;
4284
4285 /*
4286 * Get the first variable.
4287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return FAIL;
4290
4291 /*
4292 * Repeat computing, until no '+', '-' or '.' is following.
4293 */
4294 for (;;)
4295 {
4296 op = **arg;
4297 if (op != '+' && op != '-' && op != '.')
4298 break;
4299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 if (op != '+' || rettv->v_type != VAR_LIST)
4301 {
4302 /* For "list + ...", an illegal use of the first operand as
4303 * a number cannot be determined before evaluating the 2nd
4304 * operand: if this is also a list, all is ok.
4305 * For "something . ...", "something - ..." or "non-list + ...",
4306 * we know that the first operand needs to be a string or number
4307 * without evaluating the 2nd operand. So check before to avoid
4308 * side effects after an error. */
4309 if (evaluate && get_tv_string_chk(rettv) == NULL)
4310 {
4311 clear_tv(rettv);
4312 return FAIL;
4313 }
4314 }
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 /*
4317 * Get the second variable.
4318 */
4319 *arg = skipwhite(*arg + 1);
4320 if (eval6(arg, &var2, evaluate) == FAIL)
4321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 return FAIL;
4324 }
4325
4326 if (evaluate)
4327 {
4328 /*
4329 * Compute the result.
4330 */
4331 if (op == '.')
4332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4334 s2 = get_tv_string_buf_chk(&var2, buf2);
4335 if (s2 == NULL) /* type error ? */
4336 {
4337 clear_tv(rettv);
4338 clear_tv(&var2);
4339 return FAIL;
4340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004341 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(rettv);
4343 rettv->v_type = VAR_STRING;
4344 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004346 else if (op == '+' && rettv->v_type == VAR_LIST
4347 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004348 {
4349 /* concatenate Lists */
4350 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4351 &var3) == FAIL)
4352 {
4353 clear_tv(rettv);
4354 clear_tv(&var2);
4355 return FAIL;
4356 }
4357 clear_tv(rettv);
4358 *rettv = var3;
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 else
4361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 int error = FALSE;
4363
4364 n1 = get_tv_number_chk(rettv, &error);
4365 if (error)
4366 {
4367 /* This can only happen for "list + non-list".
4368 * For "non-list + ..." or "something - ...", we returned
4369 * before evaluating the 2nd operand. */
4370 clear_tv(rettv);
4371 return FAIL;
4372 }
4373 n2 = get_tv_number_chk(&var2, &error);
4374 if (error)
4375 {
4376 clear_tv(rettv);
4377 clear_tv(&var2);
4378 return FAIL;
4379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (op == '+')
4382 n1 = n1 + n2;
4383 else
4384 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 rettv->v_type = VAR_NUMBER;
4386 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 }
4391 return OK;
4392}
4393
4394/*
4395 * Handle fifth level expression:
4396 * * number multiplication
4397 * / number division
4398 * % number modulo
4399 *
4400 * "arg" must point to the first non-white of the expression.
4401 * "arg" is advanced to the next non-white after the recognized expression.
4402 *
4403 * Return OK or FAIL.
4404 */
4405 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 int evaluate;
4410{
Bram Moolenaar33570922005-01-25 22:26:29 +00004411 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 int op;
4413 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 /*
4417 * Get the first variable.
4418 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004419 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 return FAIL;
4421
4422 /*
4423 * Repeat computing, until no '*', '/' or '%' is following.
4424 */
4425 for (;;)
4426 {
4427 op = **arg;
4428 if (op != '*' && op != '/' && op != '%')
4429 break;
4430
4431 if (evaluate)
4432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 if (error)
4436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 else
4439 n1 = 0;
4440
4441 /*
4442 * Get the second variable.
4443 */
4444 *arg = skipwhite(*arg + 1);
4445 if (eval7(arg, &var2, evaluate) == FAIL)
4446 return FAIL;
4447
4448 if (evaluate)
4449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004450 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 if (error)
4453 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455 /*
4456 * Compute the result.
4457 */
4458 if (op == '*')
4459 n1 = n1 * n2;
4460 else if (op == '/')
4461 {
4462 if (n2 == 0) /* give an error message? */
4463 n1 = 0x7fffffffL;
4464 else
4465 n1 = n1 / n2;
4466 }
4467 else
4468 {
4469 if (n2 == 0) /* give an error message? */
4470 n1 = 0;
4471 else
4472 n1 = n1 % n2;
4473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 rettv->v_type = VAR_NUMBER;
4475 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 }
4478
4479 return OK;
4480}
4481
4482/*
4483 * Handle sixth level expression:
4484 * number number constant
4485 * "string" string contstant
4486 * 'string' literal string contstant
4487 * &option-name option value
4488 * @r register contents
4489 * identifier variable value
4490 * function() function call
4491 * $VAR environment variable
4492 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004493 * [expr, expr] List
4494 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 *
4496 * Also handle:
4497 * ! in front logical NOT
4498 * - in front unary minus
4499 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 * trailing [] subscript in String or List
4501 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 *
4503 * "arg" must point to the first non-white of the expression.
4504 * "arg" is advanced to the next non-white after the recognized expression.
4505 *
4506 * Return OK or FAIL.
4507 */
4508 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 int evaluate;
4513{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 long n;
4515 int len;
4516 char_u *s;
4517 int val;
4518 char_u *start_leader, *end_leader;
4519 int ret = OK;
4520 char_u *alias;
4521
4522 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 /*
4529 * Skip '!' and '-' characters. They are handled later.
4530 */
4531 start_leader = *arg;
4532 while (**arg == '!' || **arg == '-' || **arg == '+')
4533 *arg = skipwhite(*arg + 1);
4534 end_leader = *arg;
4535
4536 switch (**arg)
4537 {
4538 /*
4539 * Number constant.
4540 */
4541 case '0':
4542 case '1':
4543 case '2':
4544 case '3':
4545 case '4':
4546 case '5':
4547 case '6':
4548 case '7':
4549 case '8':
4550 case '9':
4551 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4552 *arg += len;
4553 if (evaluate)
4554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 rettv->v_type = VAR_NUMBER;
4556 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558 break;
4559
4560 /*
4561 * String constant: "string".
4562 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004563 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 break;
4565
4566 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 break;
4571
4572 /*
4573 * List: [expr, expr]
4574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 break;
4577
4578 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004579 * Dictionary: {key: val, key: val}
4580 */
4581 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4582 break;
4583
4584 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004585 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004587 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 break;
4589
4590 /*
4591 * Environment variable: $VAR.
4592 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
4595
4596 /*
4597 * Register contents: @r.
4598 */
4599 case '@': ++*arg;
4600 if (evaluate)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004603 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 if (**arg != NUL)
4606 ++*arg;
4607 break;
4608
4609 /*
4610 * nested expression: (expression).
4611 */
4612 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (**arg == ')')
4615 ++*arg;
4616 else if (ret == OK)
4617 {
4618 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 ret = FAIL;
4621 }
4622 break;
4623
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627
4628 if (ret == NOTDONE)
4629 {
4630 /*
4631 * Must be a variable or function name.
4632 * Can also be a curly-braces kind of name: {expr}.
4633 */
4634 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004635 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 if (alias != NULL)
4637 s = alias;
4638
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004639 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 ret = FAIL;
4641 else
4642 {
4643 if (**arg == '(') /* recursive! */
4644 {
4645 /* If "s" is the name of a variable of type VAR_FUNC
4646 * use its contents. */
4647 s = deref_func_name(s, &len);
4648
4649 /* Invoke the function. */
4650 ret = get_func_tv(s, len, rettv, arg,
4651 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004652 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 /* Stop the expression evaluation when immediately
4654 * aborting on error, or when an interrupt occurred or
4655 * an exception was thrown but not caught. */
4656 if (aborting())
4657 {
4658 if (ret == OK)
4659 clear_tv(rettv);
4660 ret = FAIL;
4661 }
4662 }
4663 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004664 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004665 else
4666 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 }
4668
4669 if (alias != NULL)
4670 vim_free(alias);
4671 }
4672
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 *arg = skipwhite(*arg);
4674
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004675 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4676 * expr(expr). */
4677 if (ret == OK)
4678 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 /*
4681 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4682 */
4683 if (ret == OK && evaluate && end_leader > start_leader)
4684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 int error = FALSE;
4686
4687 val = get_tv_number_chk(rettv, &error);
4688 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 clear_tv(rettv);
4691 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004693 else
4694 {
4695 while (end_leader > start_leader)
4696 {
4697 --end_leader;
4698 if (*end_leader == '!')
4699 val = !val;
4700 else if (*end_leader == '-')
4701 val = -val;
4702 }
4703 clear_tv(rettv);
4704 rettv->v_type = VAR_NUMBER;
4705 rettv->vval.v_number = val;
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708
4709 return ret;
4710}
4711
4712/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004713 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4714 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4716 */
4717 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004718eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004720 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004722 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723{
4724 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004725 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 long len = -1;
4728 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004732 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004734 if (verbose)
4735 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 return FAIL;
4737 }
4738
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 /*
4742 * dict.name
4743 */
4744 key = *arg + 1;
4745 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4746 ;
4747 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 }
4751 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 /*
4754 * something[idx]
4755 *
4756 * Get the (first) variable from inside the [].
4757 */
4758 *arg = skipwhite(*arg + 1);
4759 if (**arg == ':')
4760 empty1 = TRUE;
4761 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4762 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4764 {
4765 /* not a number or string */
4766 clear_tv(&var1);
4767 return FAIL;
4768 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769
4770 /*
4771 * Get the second variable from inside the [:].
4772 */
4773 if (**arg == ':')
4774 {
4775 range = TRUE;
4776 *arg = skipwhite(*arg + 1);
4777 if (**arg == ']')
4778 empty2 = TRUE;
4779 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 if (!empty1)
4782 clear_tv(&var1);
4783 return FAIL;
4784 }
4785 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4786 {
4787 /* not a number or string */
4788 if (!empty1)
4789 clear_tv(&var1);
4790 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 return FAIL;
4792 }
4793 }
4794
4795 /* Check for the ']'. */
4796 if (**arg != ']')
4797 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004798 if (verbose)
4799 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 clear_tv(&var1);
4801 if (range)
4802 clear_tv(&var2);
4803 return FAIL;
4804 }
4805 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 }
4807
4808 if (evaluate)
4809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 n1 = 0;
4811 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 n1 = get_tv_number(&var1);
4814 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 }
4816 if (range)
4817 {
4818 if (empty2)
4819 n2 = -1;
4820 else
4821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004822 n2 = get_tv_number(&var2);
4823 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 }
4825 }
4826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 {
4829 case VAR_NUMBER:
4830 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832 len = (long)STRLEN(s);
4833 if (range)
4834 {
4835 /* The resulting variable is a substring. If the indexes
4836 * are out of range the result is empty. */
4837 if (n1 < 0)
4838 {
4839 n1 = len + n1;
4840 if (n1 < 0)
4841 n1 = 0;
4842 }
4843 if (n2 < 0)
4844 n2 = len + n2;
4845 else if (n2 >= len)
4846 n2 = len;
4847 if (n1 >= len || n2 < 0 || n1 > n2)
4848 s = NULL;
4849 else
4850 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4851 }
4852 else
4853 {
4854 /* The resulting variable is a string of a single
4855 * character. If the index is too big or negative the
4856 * result is empty. */
4857 if (n1 >= len || n1 < 0)
4858 s = NULL;
4859 else
4860 s = vim_strnsave(s + n1, 1);
4861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 clear_tv(rettv);
4863 rettv->v_type = VAR_STRING;
4864 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 break;
4866
4867 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004869 if (n1 < 0)
4870 n1 = len + n1;
4871 if (!empty1 && (n1 < 0 || n1 >= len))
4872 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004873 /* For a range we allow invalid values and return an empty
4874 * list. A list index out of range is an error. */
4875 if (!range)
4876 {
4877 if (verbose)
4878 EMSGN(_(e_listidx), n1);
4879 return FAIL;
4880 }
4881 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 }
4883 if (range)
4884 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004885 list_T *l;
4886 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887
4888 if (n2 < 0)
4889 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004890 else if (n2 >= len)
4891 n2 = len - 1;
4892 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004893 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 l = list_alloc();
4895 if (l == NULL)
4896 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 n1 <= n2; ++n1)
4899 {
4900 if (list_append_tv(l, &item->li_tv) == FAIL)
4901 {
4902 list_free(l);
4903 return FAIL;
4904 }
4905 item = item->li_next;
4906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 clear_tv(rettv);
4908 rettv->v_type = VAR_LIST;
4909 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004910 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911 }
4912 else
4913 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004914 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 clear_tv(rettv);
4916 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 }
4918 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004919
4920 case VAR_DICT:
4921 if (range)
4922 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004923 if (verbose)
4924 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 if (len == -1)
4926 clear_tv(&var1);
4927 return FAIL;
4928 }
4929 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004930 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004931
4932 if (len == -1)
4933 {
4934 key = get_tv_string(&var1);
4935 if (*key == NUL)
4936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004937 if (verbose)
4938 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939 clear_tv(&var1);
4940 return FAIL;
4941 }
4942 }
4943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004944 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004946 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004947 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 if (len == -1)
4949 clear_tv(&var1);
4950 if (item == NULL)
4951 return FAIL;
4952
4953 copy_tv(&item->di_tv, &var1);
4954 clear_tv(rettv);
4955 *rettv = var1;
4956 }
4957 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 }
4959 }
4960
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004961 return OK;
4962}
4963
4964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 * Get an option value.
4966 * "arg" points to the '&' or '+' before the option name.
4967 * "arg" is advanced to character after the option name.
4968 * Return OK or FAIL.
4969 */
4970 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004973 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 int evaluate;
4975{
4976 char_u *option_end;
4977 long numval;
4978 char_u *stringval;
4979 int opt_type;
4980 int c;
4981 int working = (**arg == '+'); /* has("+option") */
4982 int ret = OK;
4983 int opt_flags;
4984
4985 /*
4986 * Isolate the option name and find its value.
4987 */
4988 option_end = find_option_end(arg, &opt_flags);
4989 if (option_end == NULL)
4990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 EMSG2(_("E112: Option name missing: %s"), *arg);
4993 return FAIL;
4994 }
4995
4996 if (!evaluate)
4997 {
4998 *arg = option_end;
4999 return OK;
5000 }
5001
5002 c = *option_end;
5003 *option_end = NUL;
5004 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
5007 if (opt_type == -3) /* invalid name */
5008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 EMSG2(_("E113: Unknown option: %s"), *arg);
5011 ret = FAIL;
5012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
5015 if (opt_type == -2) /* hidden string option */
5016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 rettv->v_type = VAR_STRING;
5018 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else if (opt_type == -1) /* hidden number option */
5021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 rettv->v_type = VAR_NUMBER;
5023 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 else if (opt_type == 1) /* number option */
5026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 rettv->v_type = VAR_NUMBER;
5028 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 else /* string option */
5031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005032 rettv->v_type = VAR_STRING;
5033 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 }
5036 else if (working && (opt_type == -2 || opt_type == -1))
5037 ret = FAIL;
5038
5039 *option_end = c; /* put back for error messages */
5040 *arg = option_end;
5041
5042 return ret;
5043}
5044
5045/*
5046 * Allocate a variable for a string constant.
5047 * Return OK or FAIL.
5048 */
5049 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005050get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 int evaluate;
5054{
5055 char_u *p;
5056 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 int extra = 0;
5058
5059 /*
5060 * Find the end of the string, skipping backslashed characters.
5061 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 if (*p == '\\' && p[1] != NUL)
5065 {
5066 ++p;
5067 /* A "\<x>" form occupies at least 4 characters, and produces up
5068 * to 6 characters: reserve space for 2 extra */
5069 if (*p == '<')
5070 extra += 2;
5071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073
5074 if (*p != '"')
5075 {
5076 EMSG2(_("E114: Missing quote: %s"), *arg);
5077 return FAIL;
5078 }
5079
5080 /* If only parsing, set *arg and return here */
5081 if (!evaluate)
5082 {
5083 *arg = p + 1;
5084 return OK;
5085 }
5086
5087 /*
5088 * Copy the string into allocated memory, handling backslashed
5089 * characters.
5090 */
5091 name = alloc((unsigned)(p - *arg + extra));
5092 if (name == NULL)
5093 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 rettv->v_type = VAR_STRING;
5095 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099 if (*p == '\\')
5100 {
5101 switch (*++p)
5102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 case 'b': *name++ = BS; ++p; break;
5104 case 'e': *name++ = ESC; ++p; break;
5105 case 'f': *name++ = FF; ++p; break;
5106 case 'n': *name++ = NL; ++p; break;
5107 case 'r': *name++ = CAR; ++p; break;
5108 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110 case 'X': /* hex: "\x1", "\x12" */
5111 case 'x':
5112 case 'u': /* Unicode: "\u0023" */
5113 case 'U':
5114 if (vim_isxdigit(p[1]))
5115 {
5116 int n, nr;
5117 int c = toupper(*p);
5118
5119 if (c == 'X')
5120 n = 2;
5121 else
5122 n = 4;
5123 nr = 0;
5124 while (--n >= 0 && vim_isxdigit(p[1]))
5125 {
5126 ++p;
5127 nr = (nr << 4) + hex2nr(*p);
5128 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130#ifdef FEAT_MBYTE
5131 /* For "\u" store the number according to
5132 * 'encoding'. */
5133 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 else
5136#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140
5141 /* octal: "\1", "\12", "\123" */
5142 case '0':
5143 case '1':
5144 case '2':
5145 case '3':
5146 case '4':
5147 case '5':
5148 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 case '7': *name = *p++ - '0';
5150 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 *name = (*name << 3) + *p++ - '0';
5153 if (*p >= '0' && *p <= '7')
5154 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (extra != 0)
5162 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165 }
5166 /* FALLTHROUGH */
5167
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170 }
5171 }
5172 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 *arg = p + 1;
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 return OK;
5180}
5181
5182/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 * Return OK or FAIL.
5185 */
5186 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 int evaluate;
5191{
5192 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005193 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005194 int reduce = 0;
5195
5196 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005198 */
5199 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005202 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005204 break;
5205 ++reduce;
5206 ++p;
5207 }
5208 }
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005211 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005213 return FAIL;
5214 }
5215
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005217 if (!evaluate)
5218 {
5219 *arg = p + 1;
5220 return OK;
5221 }
5222
5223 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005225 */
5226 str = alloc((unsigned)((p - *arg) - reduce));
5227 if (str == NULL)
5228 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 rettv->v_type = VAR_STRING;
5230 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005235 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005237 break;
5238 ++p;
5239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 *arg = p + 1;
5244
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 return OK;
5246}
5247
5248/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 * Allocate a variable for a List and fill it from "*arg".
5250 * Return OK or FAIL.
5251 */
5252 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005255 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 int evaluate;
5257{
Bram Moolenaar33570922005-01-25 22:26:29 +00005258 list_T *l = NULL;
5259 typval_T tv;
5260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261
5262 if (evaluate)
5263 {
5264 l = list_alloc();
5265 if (l == NULL)
5266 return FAIL;
5267 }
5268
5269 *arg = skipwhite(*arg + 1);
5270 while (**arg != ']' && **arg != NUL)
5271 {
5272 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5273 goto failret;
5274 if (evaluate)
5275 {
5276 item = listitem_alloc();
5277 if (item != NULL)
5278 {
5279 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005280 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 list_append(l, item);
5282 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005283 else
5284 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 }
5286
5287 if (**arg == ']')
5288 break;
5289 if (**arg != ',')
5290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 goto failret;
5293 }
5294 *arg = skipwhite(*arg + 1);
5295 }
5296
5297 if (**arg != ']')
5298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300failret:
5301 if (evaluate)
5302 list_free(l);
5303 return FAIL;
5304 }
5305
5306 *arg = skipwhite(*arg + 1);
5307 if (evaluate)
5308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005309 rettv->v_type = VAR_LIST;
5310 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 ++l->lv_refcount;
5312 }
5313
5314 return OK;
5315}
5316
5317/*
5318 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005319 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005321 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322list_alloc()
5323{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005324 list_T *l;
5325
5326 l = (list_T *)alloc_clear(sizeof(list_T));
5327 if (l != NULL)
5328 {
5329 /* Prepend the list to the list of lists for garbage collection. */
5330 if (first_list != NULL)
5331 first_list->lv_used_prev = l;
5332 l->lv_used_prev = NULL;
5333 l->lv_used_next = first_list;
5334 first_list = l;
5335 }
5336 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337}
5338
5339/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005340 * Allocate an empty list for a return value.
5341 * Returns OK or FAIL.
5342 */
5343 static int
5344rettv_list_alloc(rettv)
5345 typval_T *rettv;
5346{
5347 list_T *l = list_alloc();
5348
5349 if (l == NULL)
5350 return FAIL;
5351
5352 rettv->vval.v_list = l;
5353 rettv->v_type = VAR_LIST;
5354 ++l->lv_refcount;
5355 return OK;
5356}
5357
5358/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 * Unreference a list: decrement the reference count and free it when it
5360 * becomes zero.
5361 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005362 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005366 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5367 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368}
5369
5370/*
5371 * Free a list, including all items it points to.
5372 * Ignores the reference count.
5373 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005374 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005376 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377{
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379
Bram Moolenaard9fba312005-06-26 22:34:35 +00005380 /* Avoid that recursive reference to the list frees us again. */
5381 l->lv_refcount = DEL_REFCOUNT;
5382
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383 /* Remove the list from the list of lists for garbage collection. */
5384 if (l->lv_used_prev == NULL)
5385 first_list = l->lv_used_next;
5386 else
5387 l->lv_used_prev->lv_used_next = l->lv_used_next;
5388 if (l->lv_used_next != NULL)
5389 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5390
Bram Moolenaard9fba312005-06-26 22:34:35 +00005391 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005393 /* Remove the item before deleting it. */
5394 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 listitem_free(item);
5396 }
5397 vim_free(l);
5398}
5399
5400/*
5401 * Allocate a list item.
5402 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005403 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404listitem_alloc()
5405{
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407}
5408
5409/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005410 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 */
5412 static void
5413listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005414 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005416 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 vim_free(item);
5418}
5419
5420/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005421 * Remove a list item from a List and free it. Also clears the value.
5422 */
5423 static void
5424listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 list_T *l;
5426 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005427{
5428 list_remove(l, item, item);
5429 listitem_free(item);
5430}
5431
5432/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 * Get the number of items in a list.
5434 */
5435 static long
5436list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005437 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 if (l == NULL)
5440 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005441 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442}
5443
5444/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005445 * Return TRUE when two lists have exactly the same values.
5446 */
5447 static int
5448list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 list_T *l1;
5450 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005451 int ic; /* ignore case for strings */
5452{
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005454
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005455 if (list_len(l1) != list_len(l2))
5456 return FALSE;
5457
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005458 for (item1 = l1->lv_first, item2 = l2->lv_first;
5459 item1 != NULL && item2 != NULL;
5460 item1 = item1->li_next, item2 = item2->li_next)
5461 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5462 return FALSE;
5463 return item1 == NULL && item2 == NULL;
5464}
5465
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005466#if defined(FEAT_PYTHON) || defined(PROTO)
5467/*
5468 * Return the dictitem that an entry in a hashtable points to.
5469 */
5470 dictitem_T *
5471dict_lookup(hi)
5472 hashitem_T *hi;
5473{
5474 return HI2DI(hi);
5475}
5476#endif
5477
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005478/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005479 * Return TRUE when two dictionaries have exactly the same key/values.
5480 */
5481 static int
5482dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 dict_T *d1;
5484 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005485 int ic; /* ignore case for strings */
5486{
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 hashitem_T *hi;
5488 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005489 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490
5491 if (dict_len(d1) != dict_len(d2))
5492 return FALSE;
5493
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005494 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005497 if (!HASHITEM_EMPTY(hi))
5498 {
5499 item2 = dict_find(d2, hi->hi_key, -1);
5500 if (item2 == NULL)
5501 return FALSE;
5502 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5503 return FALSE;
5504 --todo;
5505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005506 }
5507 return TRUE;
5508}
5509
5510/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005511 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005512 * Compares the items just like "==" would compare them, but strings and
5513 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005514 */
5515 static int
5516tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005517 typval_T *tv1;
5518 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005519 int ic; /* ignore case */
5520{
5521 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005522 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005524 if (tv1->v_type != tv2->v_type)
5525 return FALSE;
5526
5527 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005529 case VAR_LIST:
5530 /* recursive! */
5531 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5532
5533 case VAR_DICT:
5534 /* recursive! */
5535 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5536
5537 case VAR_FUNC:
5538 return (tv1->vval.v_string != NULL
5539 && tv2->vval.v_string != NULL
5540 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5541
5542 case VAR_NUMBER:
5543 return tv1->vval.v_number == tv2->vval.v_number;
5544
5545 case VAR_STRING:
5546 s1 = get_tv_string_buf(tv1, buf1);
5547 s2 = get_tv_string_buf(tv2, buf2);
5548 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005549 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005550
5551 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005552 return TRUE;
5553}
5554
5555/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 * Locate item with index "n" in list "l" and return it.
5557 * A negative index is counted from the end; -1 is the last item.
5558 * Returns NULL when "n" is out of range.
5559 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005560 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 long n;
5564{
Bram Moolenaar33570922005-01-25 22:26:29 +00005565 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 long idx;
5567
5568 if (l == NULL)
5569 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005570
5571 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005573 n = l->lv_len + n;
5574
5575 /* Check for index out of range. */
5576 if (n < 0 || n >= l->lv_len)
5577 return NULL;
5578
5579 /* When there is a cached index may start search from there. */
5580 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005582 if (n < l->lv_idx / 2)
5583 {
5584 /* closest to the start of the list */
5585 item = l->lv_first;
5586 idx = 0;
5587 }
5588 else if (n > (l->lv_idx + l->lv_len) / 2)
5589 {
5590 /* closest to the end of the list */
5591 item = l->lv_last;
5592 idx = l->lv_len - 1;
5593 }
5594 else
5595 {
5596 /* closest to the cached index */
5597 item = l->lv_idx_item;
5598 idx = l->lv_idx;
5599 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 }
5601 else
5602 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005603 if (n < l->lv_len / 2)
5604 {
5605 /* closest to the start of the list */
5606 item = l->lv_first;
5607 idx = 0;
5608 }
5609 else
5610 {
5611 /* closest to the end of the list */
5612 item = l->lv_last;
5613 idx = l->lv_len - 1;
5614 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005616
5617 while (n > idx)
5618 {
5619 /* search forward */
5620 item = item->li_next;
5621 ++idx;
5622 }
5623 while (n < idx)
5624 {
5625 /* search backward */
5626 item = item->li_prev;
5627 --idx;
5628 }
5629
5630 /* cache the used index */
5631 l->lv_idx = idx;
5632 l->lv_idx_item = item;
5633
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 return item;
5635}
5636
5637/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005638 * Get list item "l[idx]" as a number.
5639 */
5640 static long
5641list_find_nr(l, idx, errorp)
5642 list_T *l;
5643 long idx;
5644 int *errorp; /* set to TRUE when something wrong */
5645{
5646 listitem_T *li;
5647
5648 li = list_find(l, idx);
5649 if (li == NULL)
5650 {
5651 if (errorp != NULL)
5652 *errorp = TRUE;
5653 return -1L;
5654 }
5655 return get_tv_number_chk(&li->li_tv, errorp);
5656}
5657
5658/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005659 * Locate "item" list "l" and return its index.
5660 * Returns -1 when "item" is not in the list.
5661 */
5662 static long
5663list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005664 list_T *l;
5665 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005666{
5667 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005668 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005669
5670 if (l == NULL)
5671 return -1;
5672 idx = 0;
5673 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5674 ++idx;
5675 if (li == NULL)
5676 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005677 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005678}
5679
5680/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 * Append item "item" to the end of list "l".
5682 */
5683 static void
5684list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005685 list_T *l;
5686 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687{
5688 if (l->lv_last == NULL)
5689 {
5690 /* empty list */
5691 l->lv_first = item;
5692 l->lv_last = item;
5693 item->li_prev = NULL;
5694 }
5695 else
5696 {
5697 l->lv_last->li_next = item;
5698 item->li_prev = l->lv_last;
5699 l->lv_last = item;
5700 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005701 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 item->li_next = NULL;
5703}
5704
5705/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005707 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708 */
5709 static int
5710list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005711 list_T *l;
5712 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005714 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715
Bram Moolenaar05159a02005-02-26 23:04:13 +00005716 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005718 copy_tv(tv, &li->li_tv);
5719 list_append(l, li);
5720 return OK;
5721}
5722
5723/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005724 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005725 * Return FAIL when out of memory.
5726 */
5727 int
5728list_append_dict(list, dict)
5729 list_T *list;
5730 dict_T *dict;
5731{
5732 listitem_T *li = listitem_alloc();
5733
5734 if (li == NULL)
5735 return FAIL;
5736 li->li_tv.v_type = VAR_DICT;
5737 li->li_tv.v_lock = 0;
5738 li->li_tv.vval.v_dict = dict;
5739 list_append(list, li);
5740 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 return OK;
5742}
5743
5744/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005745 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005746 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005747 * Returns FAIL when out of memory.
5748 */
5749 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005750list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005751 list_T *l;
5752 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005753 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005754{
5755 listitem_T *li = listitem_alloc();
5756
5757 if (li == NULL)
5758 return FAIL;
5759 list_append(l, li);
5760 li->li_tv.v_type = VAR_STRING;
5761 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005762 if (str == NULL)
5763 li->li_tv.vval.v_string = NULL;
5764 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005765 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005766 return FAIL;
5767 return OK;
5768}
5769
5770/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005771 * Append "n" to list "l".
5772 * Returns FAIL when out of memory.
5773 */
5774 static int
5775list_append_number(l, n)
5776 list_T *l;
5777 varnumber_T n;
5778{
5779 listitem_T *li;
5780
5781 li = listitem_alloc();
5782 if (li == NULL)
5783 return FAIL;
5784 li->li_tv.v_type = VAR_NUMBER;
5785 li->li_tv.v_lock = 0;
5786 li->li_tv.vval.v_number = n;
5787 list_append(l, li);
5788 return OK;
5789}
5790
5791/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005793 * If "item" is NULL append at the end.
5794 * Return FAIL when out of memory.
5795 */
5796 static int
5797list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 list_T *l;
5799 typval_T *tv;
5800 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005801{
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005803
5804 if (ni == NULL)
5805 return FAIL;
5806 copy_tv(tv, &ni->li_tv);
5807 if (item == NULL)
5808 /* Append new item at end of list. */
5809 list_append(l, ni);
5810 else
5811 {
5812 /* Insert new item before existing item. */
5813 ni->li_prev = item->li_prev;
5814 ni->li_next = item;
5815 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005816 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005817 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005818 ++l->lv_idx;
5819 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005820 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005821 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005822 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005823 l->lv_idx_item = NULL;
5824 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005825 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005826 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005827 }
5828 return OK;
5829}
5830
5831/*
5832 * Extend "l1" with "l2".
5833 * If "bef" is NULL append at the end, otherwise insert before this item.
5834 * Returns FAIL when out of memory.
5835 */
5836 static int
5837list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 list_T *l1;
5839 list_T *l2;
5840 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005841{
Bram Moolenaar33570922005-01-25 22:26:29 +00005842 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005843
5844 for (item = l2->lv_first; item != NULL; item = item->li_next)
5845 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5846 return FAIL;
5847 return OK;
5848}
5849
5850/*
5851 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5852 * Return FAIL when out of memory.
5853 */
5854 static int
5855list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l1;
5857 list_T *l2;
5858 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005859{
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861
5862 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005863 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005864 if (l == NULL)
5865 return FAIL;
5866 tv->v_type = VAR_LIST;
5867 tv->vval.v_list = l;
5868
5869 /* append all items from the second list */
5870 return list_extend(l, l2, NULL);
5871}
5872
5873/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005876 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 * Returns NULL when out of memory.
5878 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005879 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005883 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884{
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *copy;
5886 listitem_T *item;
5887 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888
5889 if (orig == NULL)
5890 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891
5892 copy = list_alloc();
5893 if (copy != NULL)
5894 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005895 if (copyID != 0)
5896 {
5897 /* Do this before adding the items, because one of the items may
5898 * refer back to this list. */
5899 orig->lv_copyID = copyID;
5900 orig->lv_copylist = copy;
5901 }
5902 for (item = orig->lv_first; item != NULL && !got_int;
5903 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 {
5905 ni = listitem_alloc();
5906 if (ni == NULL)
5907 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005908 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005909 {
5910 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5911 {
5912 vim_free(ni);
5913 break;
5914 }
5915 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005917 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 list_append(copy, ni);
5919 }
5920 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005921 if (item != NULL)
5922 {
5923 list_unref(copy);
5924 copy = NULL;
5925 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 }
5927
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 return copy;
5929}
5930
5931/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005933 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005935 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005936list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 list_T *l;
5938 listitem_T *item;
5939 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005943 /* notify watchers */
5944 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005946 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005947 list_fix_watch(l, ip);
5948 if (ip == item2)
5949 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005951
5952 if (item2->li_next == NULL)
5953 l->lv_last = item->li_prev;
5954 else
5955 item2->li_next->li_prev = item->li_prev;
5956 if (item->li_prev == NULL)
5957 l->lv_first = item2->li_next;
5958 else
5959 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005960 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961}
5962
5963/*
5964 * Return an allocated string with the string representation of a list.
5965 * May return NULL.
5966 */
5967 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005968list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005970 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
5972 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973
5974 if (tv->vval.v_list == NULL)
5975 return NULL;
5976 ga_init2(&ga, (int)sizeof(char), 80);
5977 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005978 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005979 {
5980 vim_free(ga.ga_data);
5981 return NULL;
5982 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 ga_append(&ga, ']');
5984 ga_append(&ga, NUL);
5985 return (char_u *)ga.ga_data;
5986}
5987
5988/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 * Join list "l" into a string in "*gap", using separator "sep".
5990 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005991 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005992 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005993 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005994list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 char_u *sep;
5998 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005999 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006000{
6001 int first = TRUE;
6002 char_u *tofree;
6003 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005 char_u *s;
6006
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006007 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006008 {
6009 if (first)
6010 first = FALSE;
6011 else
6012 ga_concat(gap, sep);
6013
6014 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006016 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 if (s != NULL)
6019 ga_concat(gap, s);
6020 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006021 if (s == NULL)
6022 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006023 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006024 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025}
6026
6027/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 * Garbage collection for lists and dictionaries.
6029 *
6030 * We use reference counts to be able to free most items right away when they
6031 * are no longer used. But for composite items it's possible that it becomes
6032 * unused while the reference count is > 0: When there is a recursive
6033 * reference. Example:
6034 * :let l = [1, 2, 3]
6035 * :let d = {9: l}
6036 * :let l[1] = d
6037 *
6038 * Since this is quite unusual we handle this with garbage collection: every
6039 * once in a while find out which lists and dicts are not referenced from any
6040 * variable.
6041 *
6042 * Here is a good reference text about garbage collection (refers to Python
6043 * but it applies to all reference-counting mechanisms):
6044 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046
6047/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006048 * Do garbage collection for lists and dicts.
6049 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006050 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006051 int
6052garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006053{
6054 dict_T *dd;
6055 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 int copyID = ++current_copyID;
6057 buf_T *buf;
6058 win_T *wp;
6059 int i;
6060 funccall_T *fc;
6061 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006062#ifdef FEAT_WINDOWS
6063 tabpage_T *tp;
6064#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006065
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006066 /* Only do this once. */
6067 want_garbage_collect = FALSE;
6068 may_garbage_collect = FALSE;
6069
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006070 /*
6071 * 1. Go through all accessible variables and mark all lists and dicts
6072 * with copyID.
6073 */
6074 /* script-local variables */
6075 for (i = 1; i <= ga_scripts.ga_len; ++i)
6076 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6077
6078 /* buffer-local variables */
6079 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6080 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6081
6082 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006083 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006084 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6085
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006086#ifdef FEAT_WINDOWS
6087 /* tabpage-local variables */
6088 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6089 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6090#endif
6091
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 /* global variables */
6093 set_ref_in_ht(&globvarht, copyID);
6094
6095 /* function-local variables */
6096 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6097 {
6098 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6099 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6100 }
6101
6102 /*
6103 * 2. Go through the list of dicts and free items without the copyID.
6104 */
6105 for (dd = first_dict; dd != NULL; )
6106 if (dd->dv_copyID != copyID)
6107 {
6108 dict_free(dd);
6109 did_free = TRUE;
6110
6111 /* restart, next dict may also have been freed */
6112 dd = first_dict;
6113 }
6114 else
6115 dd = dd->dv_used_next;
6116
6117 /*
6118 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006119 * But don't free a list that has a watcher (used in a for loop), these
6120 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 */
6122 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006123 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006124 {
6125 list_free(ll);
6126 did_free = TRUE;
6127
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006128 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006129 ll = first_list;
6130 }
6131 else
6132 ll = ll->lv_used_next;
6133
6134 return did_free;
6135}
6136
6137/*
6138 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6139 */
6140 static void
6141set_ref_in_ht(ht, copyID)
6142 hashtab_T *ht;
6143 int copyID;
6144{
6145 int todo;
6146 hashitem_T *hi;
6147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006148 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006149 for (hi = ht->ht_array; todo > 0; ++hi)
6150 if (!HASHITEM_EMPTY(hi))
6151 {
6152 --todo;
6153 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6154 }
6155}
6156
6157/*
6158 * Mark all lists and dicts referenced through list "l" with "copyID".
6159 */
6160 static void
6161set_ref_in_list(l, copyID)
6162 list_T *l;
6163 int copyID;
6164{
6165 listitem_T *li;
6166
6167 for (li = l->lv_first; li != NULL; li = li->li_next)
6168 set_ref_in_item(&li->li_tv, copyID);
6169}
6170
6171/*
6172 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6173 */
6174 static void
6175set_ref_in_item(tv, copyID)
6176 typval_T *tv;
6177 int copyID;
6178{
6179 dict_T *dd;
6180 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006181
6182 switch (tv->v_type)
6183 {
6184 case VAR_DICT:
6185 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006186 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006187 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 /* Didn't see this dict yet. */
6189 dd->dv_copyID = copyID;
6190 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006191 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006192 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006193
6194 case VAR_LIST:
6195 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006197 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006198 /* Didn't see this list yet. */
6199 ll->lv_copyID = copyID;
6200 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006201 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006202 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006203 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006204 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006205}
6206
6207/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208 * Allocate an empty header for a dictionary.
6209 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006210 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006211dict_alloc()
6212{
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006217 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006218 /* Add the list to the hashtable for garbage collection. */
6219 if (first_dict != NULL)
6220 first_dict->dv_used_prev = d;
6221 d->dv_used_next = first_dict;
6222 d->dv_used_prev = NULL;
6223
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006225 d->dv_lock = 0;
6226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006230}
6231
6232/*
6233 * Unreference a Dictionary: decrement the reference count and free it when it
6234 * becomes zero.
6235 */
6236 static void
6237dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006239{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006240 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6241 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006242}
6243
6244/*
6245 * Free a Dictionary, including all items it contains.
6246 * Ignores the reference count.
6247 */
6248 static void
6249dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006251{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006254 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255
Bram Moolenaard9fba312005-06-26 22:34:35 +00006256 /* Avoid that recursive reference to the dict frees us again. */
6257 d->dv_refcount = DEL_REFCOUNT;
6258
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006259 /* Remove the dict from the list of dicts for garbage collection. */
6260 if (d->dv_used_prev == NULL)
6261 first_dict = d->dv_used_next;
6262 else
6263 d->dv_used_prev->dv_used_next = d->dv_used_next;
6264 if (d->dv_used_next != NULL)
6265 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6266
6267 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006268 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006269 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006271 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006272 if (!HASHITEM_EMPTY(hi))
6273 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006274 /* Remove the item before deleting it, just in case there is
6275 * something recursive causing trouble. */
6276 di = HI2DI(hi);
6277 hash_remove(&d->dv_hashtab, hi);
6278 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006279 --todo;
6280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 vim_free(d);
6284}
6285
6286/*
6287 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 * The "key" is copied to the new item.
6289 * Note that the value of the item "di_tv" still needs to be initialized!
6290 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006291 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006293dictitem_alloc(key)
6294 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006295{
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006301 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006302 di->di_flags = 0;
6303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006304 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006305}
6306
6307/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308 * Make a copy of a Dictionary item.
6309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006311dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006313{
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006316 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6317 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006318 if (di != NULL)
6319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006321 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006322 copy_tv(&org->di_tv, &di->di_tv);
6323 }
6324 return di;
6325}
6326
6327/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 * Remove item "item" from Dictionary "dict" and free it.
6329 */
6330 static void
6331dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 dict_T *dict;
6333 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334{
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006336
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006338 if (HASHITEM_EMPTY(hi))
6339 EMSG2(_(e_intern2), "dictitem_remove()");
6340 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006342 dictitem_free(item);
6343}
6344
6345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346 * Free a dict item. Also clears the value.
6347 */
6348 static void
6349dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352 clear_tv(&item->di_tv);
6353 vim_free(item);
6354}
6355
6356/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6358 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006359 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360 * Returns NULL when out of memory.
6361 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006363dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006366 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006367{
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 dict_T *copy;
6369 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372
6373 if (orig == NULL)
6374 return NULL;
6375
6376 copy = dict_alloc();
6377 if (copy != NULL)
6378 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006379 if (copyID != 0)
6380 {
6381 orig->dv_copyID = copyID;
6382 orig->dv_copydict = copy;
6383 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006384 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006385 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006387 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389 --todo;
6390
6391 di = dictitem_alloc(hi->hi_key);
6392 if (di == NULL)
6393 break;
6394 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006395 {
6396 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6397 copyID) == FAIL)
6398 {
6399 vim_free(di);
6400 break;
6401 }
6402 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006403 else
6404 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6405 if (dict_add(copy, di) == FAIL)
6406 {
6407 dictitem_free(di);
6408 break;
6409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006411 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412
Bram Moolenaare9a41262005-01-15 22:18:47 +00006413 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006414 if (todo > 0)
6415 {
6416 dict_unref(copy);
6417 copy = NULL;
6418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006419 }
6420
6421 return copy;
6422}
6423
6424/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006426 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006428 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 dict_T *d;
6431 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006434}
6435
Bram Moolenaar8c711452005-01-14 21:53:12 +00006436/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437 * Add a number or string entry to dictionary "d".
6438 * When "str" is NULL use number "nr", otherwise use "str".
6439 * Returns FAIL when out of memory and when key already exists.
6440 */
6441 int
6442dict_add_nr_str(d, key, nr, str)
6443 dict_T *d;
6444 char *key;
6445 long nr;
6446 char_u *str;
6447{
6448 dictitem_T *item;
6449
6450 item = dictitem_alloc((char_u *)key);
6451 if (item == NULL)
6452 return FAIL;
6453 item->di_tv.v_lock = 0;
6454 if (str == NULL)
6455 {
6456 item->di_tv.v_type = VAR_NUMBER;
6457 item->di_tv.vval.v_number = nr;
6458 }
6459 else
6460 {
6461 item->di_tv.v_type = VAR_STRING;
6462 item->di_tv.vval.v_string = vim_strsave(str);
6463 }
6464 if (dict_add(d, item) == FAIL)
6465 {
6466 dictitem_free(item);
6467 return FAIL;
6468 }
6469 return OK;
6470}
6471
6472/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473 * Get the number of items in a Dictionary.
6474 */
6475 static long
6476dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006478{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006479 if (d == NULL)
6480 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006481 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482}
6483
6484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485 * Find item "key[len]" in Dictionary "d".
6486 * If "len" is negative use strlen(key).
6487 * Returns NULL when not found.
6488 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006492 char_u *key;
6493 int len;
6494{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006495#define AKEYLEN 200
6496 char_u buf[AKEYLEN];
6497 char_u *akey;
6498 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 if (len < 0)
6502 akey = key;
6503 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006505 tofree = akey = vim_strnsave(key, len);
6506 if (akey == NULL)
6507 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006509 else
6510 {
6511 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006512 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 akey = buf;
6514 }
6515
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006517 vim_free(tofree);
6518 if (HASHITEM_EMPTY(hi))
6519 return NULL;
6520 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521}
6522
6523/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006524 * Get a string item from a dictionary.
6525 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006526 * Returns NULL if the entry doesn't exist or out of memory.
6527 */
6528 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006529get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006530 dict_T *d;
6531 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006532 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006533{
6534 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006535 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006536
6537 di = dict_find(d, key, -1);
6538 if (di == NULL)
6539 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006540 s = get_tv_string(&di->di_tv);
6541 if (save && s != NULL)
6542 s = vim_strsave(s);
6543 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006544}
6545
6546/*
6547 * Get a number item from a dictionary.
6548 * Returns 0 if the entry doesn't exist or out of memory.
6549 */
6550 long
6551get_dict_number(d, key)
6552 dict_T *d;
6553 char_u *key;
6554{
6555 dictitem_T *di;
6556
6557 di = dict_find(d, key, -1);
6558 if (di == NULL)
6559 return 0;
6560 return get_tv_number(&di->di_tv);
6561}
6562
6563/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 * Return an allocated string with the string representation of a Dictionary.
6565 * May return NULL.
6566 */
6567 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006568dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006570 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006571{
6572 garray_T ga;
6573 int first = TRUE;
6574 char_u *tofree;
6575 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006577 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006579 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006580
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582 return NULL;
6583 ga_init2(&ga, (int)sizeof(char), 80);
6584 ga_append(&ga, '{');
6585
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006586 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 --todo;
6592
6593 if (first)
6594 first = FALSE;
6595 else
6596 ga_concat(&ga, (char_u *)", ");
6597
6598 tofree = string_quote(hi->hi_key, FALSE);
6599 if (tofree != NULL)
6600 {
6601 ga_concat(&ga, tofree);
6602 vim_free(tofree);
6603 }
6604 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006605 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006606 if (s != NULL)
6607 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006609 if (s == NULL)
6610 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006612 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 if (todo > 0)
6614 {
6615 vim_free(ga.ga_data);
6616 return NULL;
6617 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618
6619 ga_append(&ga, '}');
6620 ga_append(&ga, NUL);
6621 return (char_u *)ga.ga_data;
6622}
6623
6624/*
6625 * Allocate a variable for a Dictionary and fill it from "*arg".
6626 * Return OK or FAIL. Returns NOTDONE for {expr}.
6627 */
6628 static int
6629get_dict_tv(arg, rettv, evaluate)
6630 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006631 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006632 int evaluate;
6633{
Bram Moolenaar33570922005-01-25 22:26:29 +00006634 dict_T *d = NULL;
6635 typval_T tvkey;
6636 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006638 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006640 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641
6642 /*
6643 * First check if it's not a curly-braces thing: {expr}.
6644 * Must do this without evaluating, otherwise a function may be called
6645 * twice. Unfortunately this means we need to call eval1() twice for the
6646 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006649 if (*start != '}')
6650 {
6651 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6652 return FAIL;
6653 if (*start == '}')
6654 return NOTDONE;
6655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656
6657 if (evaluate)
6658 {
6659 d = dict_alloc();
6660 if (d == NULL)
6661 return FAIL;
6662 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 tvkey.v_type = VAR_UNKNOWN;
6664 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665
6666 *arg = skipwhite(*arg + 1);
6667 while (**arg != '}' && **arg != NUL)
6668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006669 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006670 goto failret;
6671 if (**arg != ':')
6672 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006673 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006674 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006675 goto failret;
6676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006677 key = get_tv_string_buf_chk(&tvkey, buf);
6678 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006680 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6681 if (key != NULL)
6682 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006683 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 goto failret;
6685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686
6687 *arg = skipwhite(*arg + 1);
6688 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006690 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691 goto failret;
6692 }
6693 if (evaluate)
6694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006696 if (item != NULL)
6697 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006698 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006699 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006700 clear_tv(&tv);
6701 goto failret;
6702 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006703 item = dictitem_alloc(key);
6704 clear_tv(&tvkey);
6705 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006708 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006709 if (dict_add(d, item) == FAIL)
6710 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711 }
6712 }
6713
6714 if (**arg == '}')
6715 break;
6716 if (**arg != ',')
6717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006718 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719 goto failret;
6720 }
6721 *arg = skipwhite(*arg + 1);
6722 }
6723
6724 if (**arg != '}')
6725 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006726 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727failret:
6728 if (evaluate)
6729 dict_free(d);
6730 return FAIL;
6731 }
6732
6733 *arg = skipwhite(*arg + 1);
6734 if (evaluate)
6735 {
6736 rettv->v_type = VAR_DICT;
6737 rettv->vval.v_dict = d;
6738 ++d->dv_refcount;
6739 }
6740
6741 return OK;
6742}
6743
Bram Moolenaar8c711452005-01-14 21:53:12 +00006744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006745 * Return a string with the string representation of a variable.
6746 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006747 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006748 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006749 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750 * May return NULL;
6751 */
6752 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006753echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006754 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006755 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006757 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006758{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759 static int recurse = 0;
6760 char_u *r = NULL;
6761
Bram Moolenaar33570922005-01-25 22:26:29 +00006762 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006763 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006764 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006765 *tofree = NULL;
6766 return NULL;
6767 }
6768 ++recurse;
6769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770 switch (tv->v_type)
6771 {
6772 case VAR_FUNC:
6773 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 r = tv->vval.v_string;
6775 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006776
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006778 if (tv->vval.v_list == NULL)
6779 {
6780 *tofree = NULL;
6781 r = NULL;
6782 }
6783 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6784 {
6785 *tofree = NULL;
6786 r = (char_u *)"[...]";
6787 }
6788 else
6789 {
6790 tv->vval.v_list->lv_copyID = copyID;
6791 *tofree = list2string(tv, copyID);
6792 r = *tofree;
6793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006794 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006795
Bram Moolenaar8c711452005-01-14 21:53:12 +00006796 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006797 if (tv->vval.v_dict == NULL)
6798 {
6799 *tofree = NULL;
6800 r = NULL;
6801 }
6802 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6803 {
6804 *tofree = NULL;
6805 r = (char_u *)"{...}";
6806 }
6807 else
6808 {
6809 tv->vval.v_dict->dv_copyID = copyID;
6810 *tofree = dict2string(tv, copyID);
6811 r = *tofree;
6812 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 case VAR_STRING:
6816 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006817 *tofree = NULL;
6818 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006821 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006823 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825
6826 --recurse;
6827 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828}
6829
6830/*
6831 * Return a string with the string representation of a variable.
6832 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6833 * "numbuf" is used for a number.
6834 * Puts quotes around strings, so that they can be parsed back by eval().
6835 * May return NULL;
6836 */
6837 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006838tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006840 char_u **tofree;
6841 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006842 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006843{
6844 switch (tv->v_type)
6845 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006846 case VAR_FUNC:
6847 *tofree = string_quote(tv->vval.v_string, TRUE);
6848 return *tofree;
6849 case VAR_STRING:
6850 *tofree = string_quote(tv->vval.v_string, FALSE);
6851 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006852 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006853 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006854 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006856 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006858 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006859 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006860}
6861
6862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 * Return string "str" in ' quotes, doubling ' characters.
6864 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006865 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866 */
6867 static char_u *
6868string_quote(str, function)
6869 char_u *str;
6870 int function;
6871{
Bram Moolenaar33570922005-01-25 22:26:29 +00006872 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006873 char_u *p, *r, *s;
6874
Bram Moolenaar33570922005-01-25 22:26:29 +00006875 len = (function ? 13 : 3);
6876 if (str != NULL)
6877 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006878 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 for (p = str; *p != NUL; mb_ptr_adv(p))
6880 if (*p == '\'')
6881 ++len;
6882 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006883 s = r = alloc(len);
6884 if (r != NULL)
6885 {
6886 if (function)
6887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006889 r += 10;
6890 }
6891 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 if (str != NULL)
6894 for (p = str; *p != NUL; )
6895 {
6896 if (*p == '\'')
6897 *r++ = '\'';
6898 MB_COPY_CHAR(p, r);
6899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006900 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 if (function)
6902 *r++ = ')';
6903 *r++ = NUL;
6904 }
6905 return s;
6906}
6907
6908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 * Get the value of an environment variable.
6910 * "arg" is pointing to the '$'. It is advanced to after the name.
6911 * If the environment variable was not set, silently assume it is empty.
6912 * Always return OK.
6913 */
6914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006915get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 int evaluate;
6919{
6920 char_u *string = NULL;
6921 int len;
6922 int cc;
6923 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006924 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926 ++*arg;
6927 name = *arg;
6928 len = get_env_len(arg);
6929 if (evaluate)
6930 {
6931 if (len != 0)
6932 {
6933 cc = name[len];
6934 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006935 /* first try vim_getenv(), fast for normal environment vars */
6936 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006938 {
6939 if (!mustfree)
6940 string = vim_strsave(string);
6941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 else
6943 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006944 if (mustfree)
6945 vim_free(string);
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 /* next try expanding things like $VIM and ${HOME} */
6948 string = expand_env_save(name - 1);
6949 if (string != NULL && *string == '$')
6950 {
6951 vim_free(string);
6952 string = NULL;
6953 }
6954 }
6955 name[len] = cc;
6956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006957 rettv->v_type = VAR_STRING;
6958 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
6961 return OK;
6962}
6963
6964/*
6965 * Array with names and number of arguments of all internal functions
6966 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6967 */
6968static struct fst
6969{
6970 char *f_name; /* function name */
6971 char f_min_argc; /* minimal number of arguments */
6972 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006974 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975} functions[] =
6976{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006977 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {"append", 2, 2, f_append},
6979 {"argc", 0, 0, f_argc},
6980 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006981 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006983 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {"bufexists", 1, 1, f_bufexists},
6985 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6986 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6987 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6988 {"buflisted", 1, 1, f_buflisted},
6989 {"bufloaded", 1, 1, f_bufloaded},
6990 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006991 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {"bufwinnr", 1, 1, f_bufwinnr},
6993 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006994 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006995 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006996 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"char2nr", 1, 1, f_char2nr},
6998 {"cindent", 1, 1, f_cindent},
6999 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007000#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007001 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007002 {"complete_add", 1, 1, f_complete_add},
7003 {"complete_check", 0, 0, f_complete_check},
7004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007006 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007007 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007009 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007010 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {"delete", 1, 1, f_delete},
7012 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007013 {"diff_filler", 1, 1, f_diff_filler},
7014 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007015 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007017 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {"eventhandler", 0, 0, f_eventhandler},
7019 {"executable", 1, 1, f_executable},
7020 {"exists", 1, 1, f_exists},
7021 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007022 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007023 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7025 {"filereadable", 1, 1, f_filereadable},
7026 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007027 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007028 {"finddir", 1, 3, f_finddir},
7029 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 {"fnamemodify", 2, 2, f_fnamemodify},
7031 {"foldclosed", 1, 1, f_foldclosed},
7032 {"foldclosedend", 1, 1, f_foldclosedend},
7033 {"foldlevel", 1, 1, f_foldlevel},
7034 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007035 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007039 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007040 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {"getbufvar", 2, 2, f_getbufvar},
7042 {"getchar", 0, 1, f_getchar},
7043 {"getcharmod", 0, 0, f_getcharmod},
7044 {"getcmdline", 0, 0, f_getcmdline},
7045 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007046 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007048 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007049 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"getfsize", 1, 1, f_getfsize},
7051 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007052 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007053 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007054 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007055 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007056 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007057 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007059 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"getwinposx", 0, 0, f_getwinposx},
7061 {"getwinposy", 0, 0, f_getwinposy},
7062 {"getwinvar", 2, 2, f_getwinvar},
7063 {"glob", 1, 1, f_glob},
7064 {"globpath", 2, 2, f_globpath},
7065 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007066 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007067 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7069 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7070 {"histadd", 2, 2, f_histadd},
7071 {"histdel", 1, 2, f_histdel},
7072 {"histget", 1, 2, f_histget},
7073 {"histnr", 1, 1, f_histnr},
7074 {"hlID", 1, 1, f_hlID},
7075 {"hlexists", 1, 1, f_hlexists},
7076 {"hostname", 0, 0, f_hostname},
7077 {"iconv", 3, 3, f_iconv},
7078 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007079 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007080 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007082 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {"inputrestore", 0, 0, f_inputrestore},
7084 {"inputsave", 0, 0, f_inputsave},
7085 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007086 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007088 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007090 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007093 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"libcall", 3, 3, f_libcall},
7095 {"libcallnr", 3, 3, f_libcallnr},
7096 {"line", 1, 1, f_line},
7097 {"line2byte", 1, 1, f_line2byte},
7098 {"lispindent", 1, 1, f_lispindent},
7099 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007100 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007101 {"maparg", 1, 3, f_maparg},
7102 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007103 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007104 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007105 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007106 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007107 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007108 {"max", 1, 1, f_max},
7109 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007110#ifdef vim_mkdir
7111 {"mkdir", 1, 3, f_mkdir},
7112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {"mode", 0, 0, f_mode},
7114 {"nextnonblank", 1, 1, f_nextnonblank},
7115 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007116 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007118 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007119 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007121 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007122 {"reltime", 0, 2, f_reltime},
7123 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"remote_expr", 2, 3, f_remote_expr},
7125 {"remote_foreground", 1, 1, f_remote_foreground},
7126 {"remote_peek", 1, 2, f_remote_peek},
7127 {"remote_read", 1, 1, f_remote_read},
7128 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007129 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007131 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007133 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007134 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007135 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007136 {"searchpair", 3, 6, f_searchpair},
7137 {"searchpairpos", 3, 6, f_searchpairpos},
7138 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"server2client", 2, 2, f_server2client},
7140 {"serverlist", 0, 0, f_serverlist},
7141 {"setbufvar", 3, 3, f_setbufvar},
7142 {"setcmdpos", 1, 1, f_setcmdpos},
7143 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007144 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007145 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007146 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007148 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007150 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007152 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007153 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007154 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007155 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007156 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007157 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158#ifdef HAVE_STRFTIME
7159 {"strftime", 1, 2, f_strftime},
7160#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007162 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {"strlen", 1, 1, f_strlen},
7164 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007165 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"strtrans", 1, 1, f_strtrans},
7167 {"submatch", 1, 1, f_submatch},
7168 {"substitute", 4, 4, f_substitute},
7169 {"synID", 3, 3, f_synID},
7170 {"synIDattr", 2, 3, f_synIDattr},
7171 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007172 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007174 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007176 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007177 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007179 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {"tolower", 1, 1, f_tolower},
7181 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007182 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {"virtcol", 1, 1, f_virtcol},
7186 {"visualmode", 0, 1, f_visualmode},
7187 {"winbufnr", 1, 1, f_winbufnr},
7188 {"wincol", 0, 0, f_wincol},
7189 {"winheight", 1, 1, f_winheight},
7190 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007191 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007193 {"winrestview", 1, 1, f_winrestview},
7194 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007196 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197};
7198
7199#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7200
7201/*
7202 * Function given to ExpandGeneric() to obtain the list of internal
7203 * or user defined function names.
7204 */
7205 char_u *
7206get_function_name(xp, idx)
7207 expand_T *xp;
7208 int idx;
7209{
7210 static int intidx = -1;
7211 char_u *name;
7212
7213 if (idx == 0)
7214 intidx = -1;
7215 if (intidx < 0)
7216 {
7217 name = get_user_func_name(xp, idx);
7218 if (name != NULL)
7219 return name;
7220 }
7221 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7222 {
7223 STRCPY(IObuff, functions[intidx].f_name);
7224 STRCAT(IObuff, "(");
7225 if (functions[intidx].f_max_argc == 0)
7226 STRCAT(IObuff, ")");
7227 return IObuff;
7228 }
7229
7230 return NULL;
7231}
7232
7233/*
7234 * Function given to ExpandGeneric() to obtain the list of internal or
7235 * user defined variable or function names.
7236 */
7237/*ARGSUSED*/
7238 char_u *
7239get_expr_name(xp, idx)
7240 expand_T *xp;
7241 int idx;
7242{
7243 static int intidx = -1;
7244 char_u *name;
7245
7246 if (idx == 0)
7247 intidx = -1;
7248 if (intidx < 0)
7249 {
7250 name = get_function_name(xp, idx);
7251 if (name != NULL)
7252 return name;
7253 }
7254 return get_user_var_name(xp, ++intidx);
7255}
7256
7257#endif /* FEAT_CMDL_COMPL */
7258
7259/*
7260 * Find internal function in table above.
7261 * Return index, or -1 if not found
7262 */
7263 static int
7264find_internal_func(name)
7265 char_u *name; /* name of the function */
7266{
7267 int first = 0;
7268 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7269 int cmp;
7270 int x;
7271
7272 /*
7273 * Find the function name in the table. Binary search.
7274 */
7275 while (first <= last)
7276 {
7277 x = first + ((unsigned)(last - first) >> 1);
7278 cmp = STRCMP(name, functions[x].f_name);
7279 if (cmp < 0)
7280 last = x - 1;
7281 else if (cmp > 0)
7282 first = x + 1;
7283 else
7284 return x;
7285 }
7286 return -1;
7287}
7288
7289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7291 * name it contains, otherwise return "name".
7292 */
7293 static char_u *
7294deref_func_name(name, lenp)
7295 char_u *name;
7296 int *lenp;
7297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 int cc;
7300
7301 cc = name[*lenp];
7302 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007303 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007308 {
7309 *lenp = 0;
7310 return (char_u *)""; /* just in case */
7311 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007312 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 }
7315
7316 return name;
7317}
7318
7319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 * Allocate a variable for the result of a function.
7321 * Return OK or FAIL.
7322 */
7323 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7325 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 char_u *name; /* name of the function */
7327 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 char_u **arg; /* argument, pointing to the '(' */
7330 linenr_T firstline; /* first line of range */
7331 linenr_T lastline; /* last line of range */
7332 int *doesrange; /* return: function handled range */
7333 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
7336 char_u *argp;
7337 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007338 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 int argcount = 0; /* number of arguments found */
7340
7341 /*
7342 * Get the arguments.
7343 */
7344 argp = *arg;
7345 while (argcount < MAX_FUNC_ARGS)
7346 {
7347 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7348 if (*argp == ')' || *argp == ',' || *argp == NUL)
7349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7351 {
7352 ret = FAIL;
7353 break;
7354 }
7355 ++argcount;
7356 if (*argp != ',')
7357 break;
7358 }
7359 if (*argp == ')')
7360 ++argp;
7361 else
7362 ret = FAIL;
7363
7364 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007365 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 {
7369 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
7375 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378 *arg = skipwhite(argp);
7379 return ret;
7380}
7381
7382
7383/*
7384 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007385 * Return OK when the function can't be called, FAIL otherwise.
7386 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 */
7388 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 char_u *name; /* name of the function */
7392 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007395 typval_T *argvars; /* vars for arguments, must have "argcount"
7396 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 linenr_T firstline; /* first line of range */
7398 linenr_T lastline; /* last line of range */
7399 int *doesrange; /* return: function handled range */
7400 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404#define ERROR_UNKNOWN 0
7405#define ERROR_TOOMANY 1
7406#define ERROR_TOOFEW 2
7407#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408#define ERROR_DICT 4
7409#define ERROR_NONE 5
7410#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 int error = ERROR_NONE;
7412 int i;
7413 int llen;
7414 ufunc_T *fp;
7415 int cc;
7416#define FLEN_FIXED 40
7417 char_u fname_buf[FLEN_FIXED + 1];
7418 char_u *fname;
7419
7420 /*
7421 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7422 * Change <SNR>123_name() to K_SNR 123_name().
7423 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7424 */
7425 cc = name[len];
7426 name[len] = NUL;
7427 llen = eval_fname_script(name);
7428 if (llen > 0)
7429 {
7430 fname_buf[0] = K_SPECIAL;
7431 fname_buf[1] = KS_EXTRA;
7432 fname_buf[2] = (int)KE_SNR;
7433 i = 3;
7434 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7435 {
7436 if (current_SID <= 0)
7437 error = ERROR_SCRIPT;
7438 else
7439 {
7440 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7441 i = (int)STRLEN(fname_buf);
7442 }
7443 }
7444 if (i + STRLEN(name + llen) < FLEN_FIXED)
7445 {
7446 STRCPY(fname_buf + i, name + llen);
7447 fname = fname_buf;
7448 }
7449 else
7450 {
7451 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7452 if (fname == NULL)
7453 error = ERROR_OTHER;
7454 else
7455 {
7456 mch_memmove(fname, fname_buf, (size_t)i);
7457 STRCPY(fname + i, name + llen);
7458 }
7459 }
7460 }
7461 else
7462 fname = name;
7463
7464 *doesrange = FALSE;
7465
7466
7467 /* execute the function if no errors detected and executing */
7468 if (evaluate && error == ERROR_NONE)
7469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 error = ERROR_UNKNOWN;
7472
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007473 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
7475 /*
7476 * User defined function.
7477 */
7478 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007481 /* Trigger FuncUndefined event, may load the function. */
7482 if (fp == NULL
7483 && apply_autocmds(EVENT_FUNCUNDEFINED,
7484 fname, fname, TRUE, NULL)
7485 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007487 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 fp = find_func(fname);
7489 }
7490#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007491 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007492 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007493 {
7494 /* loaded a package, search for the function again */
7495 fp = find_func(fname);
7496 }
7497
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 if (fp != NULL)
7499 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007500 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007502 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007504 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007506 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 else
7509 {
7510 /*
7511 * Call the user function.
7512 * Save and restore search patterns, script variables and
7513 * redo buffer.
7514 */
7515 save_search_patterns();
7516 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007517 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007518 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007519 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007520 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7521 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7522 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007523 /* Function was unreferenced while being used, free it
7524 * now. */
7525 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 restoreRedobuff();
7527 restore_search_patterns();
7528 error = ERROR_NONE;
7529 }
7530 }
7531 }
7532 else
7533 {
7534 /*
7535 * Find the function name in the table, call its implementation.
7536 */
7537 i = find_internal_func(fname);
7538 if (i >= 0)
7539 {
7540 if (argcount < functions[i].f_min_argc)
7541 error = ERROR_TOOFEW;
7542 else if (argcount > functions[i].f_max_argc)
7543 error = ERROR_TOOMANY;
7544 else
7545 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007546 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 error = ERROR_NONE;
7549 }
7550 }
7551 }
7552 /*
7553 * The function call (or "FuncUndefined" autocommand sequence) might
7554 * have been aborted by an error, an interrupt, or an explicitly thrown
7555 * exception that has not been caught so far. This situation can be
7556 * tested for by calling aborting(). For an error in an internal
7557 * function or for the "E132" error in call_user_func(), however, the
7558 * throw point at which the "force_abort" flag (temporarily reset by
7559 * emsg()) is normally updated has not been reached yet. We need to
7560 * update that flag first to make aborting() reliable.
7561 */
7562 update_force_abort();
7563 }
7564 if (error == ERROR_NONE)
7565 ret = OK;
7566
7567 /*
7568 * Report an error unless the argument evaluation or function call has been
7569 * cancelled due to an aborting error, an interrupt, or an exception.
7570 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 if (!aborting())
7572 {
7573 switch (error)
7574 {
7575 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007576 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 break;
7578 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007579 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 break;
7581 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007582 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 name);
7584 break;
7585 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007586 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 name);
7588 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007590 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 name);
7592 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 }
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
7596 name[len] = cc;
7597 if (fname != name && fname != fname_buf)
7598 vim_free(fname);
7599
7600 return ret;
7601}
7602
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603/*
7604 * Give an error message with a function name. Handle <SNR> things.
7605 */
7606 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007607emsg_funcname(ermsg, name)
7608 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 char_u *name;
7610{
7611 char_u *p;
7612
7613 if (*name == K_SPECIAL)
7614 p = concat_str((char_u *)"<SNR>", name + 3);
7615 else
7616 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007617 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007618 if (p != name)
7619 vim_free(p);
7620}
7621
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622/*********************************************
7623 * Implementation of the built-in functions
7624 */
7625
7626/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007627 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 */
7629 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007630f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaar33570922005-01-25 22:26:29 +00007634 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007637 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007639 if ((l = argvars[0].vval.v_list) != NULL
7640 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7641 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007643 }
7644 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645 EMSG(_(e_listreq));
7646}
7647
7648/*
7649 * "append(lnum, string/list)" function
7650 */
7651 static void
7652f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 typval_T *argvars;
7654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007655{
7656 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 list_T *l = NULL;
7659 listitem_T *li = NULL;
7660 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 long added = 0;
7662
Bram Moolenaar0d660222005-01-07 21:51:51 +00007663 lnum = get_tv_lnum(argvars);
7664 if (lnum >= 0
7665 && lnum <= curbuf->b_ml.ml_line_count
7666 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007667 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007668 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007670 l = argvars[1].vval.v_list;
7671 if (l == NULL)
7672 return;
7673 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007675 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676 for (;;)
7677 {
7678 if (l == NULL)
7679 tv = &argvars[1]; /* append a string */
7680 else if (li == NULL)
7681 break; /* end of list */
7682 else
7683 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 line = get_tv_string_chk(tv);
7685 if (line == NULL) /* type error */
7686 {
7687 rettv->vval.v_number = 1; /* Failed */
7688 break;
7689 }
7690 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007691 ++added;
7692 if (l == NULL)
7693 break;
7694 li = li->li_next;
7695 }
7696
7697 appended_lines_mark(lnum, added);
7698 if (curwin->w_cursor.lnum > lnum)
7699 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007701 else
7702 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703}
7704
7705/*
7706 * "argc()" function
7707 */
7708/* ARGSUSED */
7709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 typval_T *argvars;
7712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715}
7716
7717/*
7718 * "argidx()" function
7719 */
7720/* ARGSUSED */
7721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 typval_T *argvars;
7724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727}
7728
7729/*
7730 * "argv(nr)" function
7731 */
7732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 typval_T *argvars;
7735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 int idx;
7738
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007739 if (argvars[0].v_type != VAR_UNKNOWN)
7740 {
7741 idx = get_tv_number_chk(&argvars[0], NULL);
7742 if (idx >= 0 && idx < ARGCOUNT)
7743 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7744 else
7745 rettv->vval.v_string = NULL;
7746 rettv->v_type = VAR_STRING;
7747 }
7748 else if (rettv_list_alloc(rettv) == OK)
7749 for (idx = 0; idx < ARGCOUNT; ++idx)
7750 list_append_string(rettv->vval.v_list,
7751 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
7753
7754/*
7755 * "browse(save, title, initdir, default)" function
7756 */
7757/* ARGSUSED */
7758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 typval_T *argvars;
7761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763#ifdef FEAT_BROWSE
7764 int save;
7765 char_u *title;
7766 char_u *initdir;
7767 char_u *defname;
7768 char_u buf[NUMBUFLEN];
7769 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772 save = get_tv_number_chk(&argvars[0], &error);
7773 title = get_tv_string_chk(&argvars[1]);
7774 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7775 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007777 if (error || title == NULL || initdir == NULL || defname == NULL)
7778 rettv->vval.v_string = NULL;
7779 else
7780 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007781 do_browse(save ? BROWSE_SAVE : 0,
7782 title, defname, NULL, initdir, NULL, curbuf);
7783#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007784 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007785#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007787}
7788
7789/*
7790 * "browsedir(title, initdir)" function
7791 */
7792/* ARGSUSED */
7793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007795 typval_T *argvars;
7796 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007797{
7798#ifdef FEAT_BROWSE
7799 char_u *title;
7800 char_u *initdir;
7801 char_u buf[NUMBUFLEN];
7802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007803 title = get_tv_string_chk(&argvars[0]);
7804 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007806 if (title == NULL || initdir == NULL)
7807 rettv->vval.v_string = NULL;
7808 else
7809 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007810 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815}
7816
Bram Moolenaar33570922005-01-25 22:26:29 +00007817static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007818
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819/*
7820 * Find a buffer by number or exact name.
7821 */
7822 static buf_T *
7823find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 if (avar->v_type == VAR_NUMBER)
7829 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007830 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007833 if (buf == NULL)
7834 {
7835 /* No full path name match, try a match with a URL or a "nofile"
7836 * buffer, these don't use the full path. */
7837 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7838 if (buf->b_fname != NULL
7839 && (path_with_url(buf->b_fname)
7840#ifdef FEAT_QUICKFIX
7841 || bt_nofile(buf)
7842#endif
7843 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007844 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007845 break;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848 return buf;
7849}
7850
7851/*
7852 * "bufexists(expr)" function
7853 */
7854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007859 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860}
7861
7862/*
7863 * "buflisted(expr)" function
7864 */
7865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 typval_T *argvars;
7868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
7870 buf_T *buf;
7871
7872 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874}
7875
7876/*
7877 * "bufloaded(expr)" function
7878 */
7879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 typval_T *argvars;
7882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 buf_T *buf;
7885
7886 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
Bram Moolenaar33570922005-01-25 22:26:29 +00007890static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007891
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892/*
7893 * Get buffer by number or pattern.
7894 */
7895 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 int save_magic;
7901 char_u *save_cpo;
7902 buf_T *buf;
7903
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007904 if (tv->v_type == VAR_NUMBER)
7905 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007906 if (tv->v_type != VAR_STRING)
7907 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (name == NULL || *name == NUL)
7909 return curbuf;
7910 if (name[0] == '$' && name[1] == NUL)
7911 return lastbuf;
7912
7913 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7914 save_magic = p_magic;
7915 p_magic = TRUE;
7916 save_cpo = p_cpo;
7917 p_cpo = (char_u *)"";
7918
7919 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7920 TRUE, FALSE));
7921
7922 p_magic = save_magic;
7923 p_cpo = save_cpo;
7924
7925 /* If not found, try expanding the name, like done for bufexists(). */
7926 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 return buf;
7930}
7931
7932/*
7933 * "bufname(expr)" function
7934 */
7935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 typval_T *argvars;
7938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 buf_T *buf;
7941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007942 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944 buf = get_buf_tv(&argvars[0]);
7945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 --emsg_off;
7951}
7952
7953/*
7954 * "bufnr(expr)" function
7955 */
7956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 typval_T *argvars;
7959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007962 int error = FALSE;
7963 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007965 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007968 --emsg_off;
7969
7970 /* If the buffer isn't found and the second argument is not zero create a
7971 * new buffer. */
7972 if (buf == NULL
7973 && argvars[1].v_type != VAR_UNKNOWN
7974 && get_tv_number_chk(&argvars[1], &error) != 0
7975 && !error
7976 && (name = get_tv_string_chk(&argvars[0])) != NULL
7977 && !error)
7978 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7979
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984}
7985
7986/*
7987 * "bufwinnr(nr)" function
7988 */
7989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007991 typval_T *argvars;
7992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994#ifdef FEAT_WINDOWS
7995 win_T *wp;
7996 int winnr = 0;
7997#endif
7998 buf_T *buf;
7999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008000 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003#ifdef FEAT_WINDOWS
8004 for (wp = firstwin; wp; wp = wp->w_next)
8005 {
8006 ++winnr;
8007 if (wp->w_buffer == buf)
8008 break;
8009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013#endif
8014 --emsg_off;
8015}
8016
8017/*
8018 * "byte2line(byte)" function
8019 */
8020/*ARGSUSED*/
8021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008023 typval_T *argvars;
8024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
8026#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028#else
8029 long boff = 0;
8030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008031 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 (linenr_T)0, &boff);
8037#endif
8038}
8039
8040/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008041 * "byteidx()" function
8042 */
8043/*ARGSUSED*/
8044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 typval_T *argvars;
8047 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008048{
8049#ifdef FEAT_MBYTE
8050 char_u *t;
8051#endif
8052 char_u *str;
8053 long idx;
8054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008055 str = get_tv_string_chk(&argvars[0]);
8056 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008057 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008058 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008059 return;
8060
8061#ifdef FEAT_MBYTE
8062 t = str;
8063 for ( ; idx > 0; idx--)
8064 {
8065 if (*t == NUL) /* EOL reached */
8066 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008067 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008068 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008069 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008070#else
8071 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008073#endif
8074}
8075
8076/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077 * "call(func, arglist)" function
8078 */
8079 static void
8080f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 typval_T *argvars;
8082 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008083{
8084 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008085 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008086 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008087 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008088 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008090
8091 rettv->vval.v_number = 0;
8092 if (argvars[1].v_type != VAR_LIST)
8093 {
8094 EMSG(_(e_listreq));
8095 return;
8096 }
8097 if (argvars[1].vval.v_list == NULL)
8098 return;
8099
8100 if (argvars[0].v_type == VAR_FUNC)
8101 func = argvars[0].vval.v_string;
8102 else
8103 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104 if (*func == NUL)
8105 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008106
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 if (argvars[2].v_type != VAR_UNKNOWN)
8108 {
8109 if (argvars[2].v_type != VAR_DICT)
8110 {
8111 EMSG(_(e_dictreq));
8112 return;
8113 }
8114 selfdict = argvars[2].vval.v_dict;
8115 }
8116
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008117 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8118 item = item->li_next)
8119 {
8120 if (argc == MAX_FUNC_ARGS)
8121 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008122 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008123 break;
8124 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008125 /* Make a copy of each argument. This is needed to be able to set
8126 * v_lock to VAR_FIXED in the copy without changing the original list.
8127 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008128 copy_tv(&item->li_tv, &argv[argc++]);
8129 }
8130
8131 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008132 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8134 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008135
8136 /* Free the arguments. */
8137 while (argc > 0)
8138 clear_tv(&argv[--argc]);
8139}
8140
8141/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008142 * "changenr()" function
8143 */
8144/*ARGSUSED*/
8145 static void
8146f_changenr(argvars, rettv)
8147 typval_T *argvars;
8148 typval_T *rettv;
8149{
8150 rettv->vval.v_number = curbuf->b_u_seq_cur;
8151}
8152
8153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 * "char2nr(string)" function
8155 */
8156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 typval_T *argvars;
8159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161#ifdef FEAT_MBYTE
8162 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008163 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 else
8165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167}
8168
8169/*
8170 * "cindent(lnum)" function
8171 */
8172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 typval_T *argvars;
8175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177#ifdef FEAT_CINDENT
8178 pos_T pos;
8179 linenr_T lnum;
8180
8181 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8184 {
8185 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008186 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 curwin->w_cursor = pos;
8188 }
8189 else
8190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008191 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192}
8193
8194/*
8195 * "col(string)" function
8196 */
8197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 typval_T *argvars;
8200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
8202 colnr_T col = 0;
8203 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008204 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008206 fp = var2fpos(&argvars[0], FALSE, &fnum);
8207 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
8209 if (fp->col == MAXCOL)
8210 {
8211 /* '> can be MAXCOL, get the length of the line then */
8212 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008213 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 else
8215 col = MAXCOL;
8216 }
8217 else
8218 {
8219 col = fp->col + 1;
8220#ifdef FEAT_VIRTUALEDIT
8221 /* col(".") when the cursor is on the NUL at the end of the line
8222 * because of "coladd" can be seen as an extra column. */
8223 if (virtual_active() && fp == &curwin->w_cursor)
8224 {
8225 char_u *p = ml_get_cursor();
8226
8227 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8228 curwin->w_virtcol - curwin->w_cursor.coladd))
8229 {
8230# ifdef FEAT_MBYTE
8231 int l;
8232
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008233 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 col += l;
8235# else
8236 if (*p != NUL && p[1] == NUL)
8237 ++col;
8238# endif
8239 }
8240 }
8241#endif
8242 }
8243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245}
8246
Bram Moolenaar572cb562005-08-05 21:35:02 +00008247#if defined(FEAT_INS_EXPAND)
8248/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008249 * "complete()" function
8250 */
8251/*ARGSUSED*/
8252 static void
8253f_complete(argvars, rettv)
8254 typval_T *argvars;
8255 typval_T *rettv;
8256{
8257 int startcol;
8258
8259 if ((State & INSERT) == 0)
8260 {
8261 EMSG(_("E785: complete() can only be used in Insert mode"));
8262 return;
8263 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008264
8265 /* Check for undo allowed here, because if something was already inserted
8266 * the line was already saved for undo and this check isn't done. */
8267 if (!undo_allowed())
8268 return;
8269
Bram Moolenaarade00832006-03-10 21:46:58 +00008270 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8271 {
8272 EMSG(_(e_invarg));
8273 return;
8274 }
8275
8276 startcol = get_tv_number_chk(&argvars[0], NULL);
8277 if (startcol <= 0)
8278 return;
8279
8280 set_completion(startcol - 1, argvars[1].vval.v_list);
8281}
8282
8283/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008284 * "complete_add()" function
8285 */
8286/*ARGSUSED*/
8287 static void
8288f_complete_add(argvars, rettv)
8289 typval_T *argvars;
8290 typval_T *rettv;
8291{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008292 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008293}
8294
8295/*
8296 * "complete_check()" function
8297 */
8298/*ARGSUSED*/
8299 static void
8300f_complete_check(argvars, rettv)
8301 typval_T *argvars;
8302 typval_T *rettv;
8303{
8304 int saved = RedrawingDisabled;
8305
8306 RedrawingDisabled = 0;
8307 ins_compl_check_keys(0);
8308 rettv->vval.v_number = compl_interrupted;
8309 RedrawingDisabled = saved;
8310}
8311#endif
8312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313/*
8314 * "confirm(message, buttons[, default [, type]])" function
8315 */
8316/*ARGSUSED*/
8317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 typval_T *argvars;
8320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8323 char_u *message;
8324 char_u *buttons = NULL;
8325 char_u buf[NUMBUFLEN];
8326 char_u buf2[NUMBUFLEN];
8327 int def = 1;
8328 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008329 char_u *typestr;
8330 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008332 message = get_tv_string_chk(&argvars[0]);
8333 if (message == NULL)
8334 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008337 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8338 if (buttons == NULL)
8339 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008342 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008343 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008345 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8346 if (typestr == NULL)
8347 error = TRUE;
8348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350 switch (TOUPPER_ASC(*typestr))
8351 {
8352 case 'E': type = VIM_ERROR; break;
8353 case 'Q': type = VIM_QUESTION; break;
8354 case 'I': type = VIM_INFO; break;
8355 case 'W': type = VIM_WARNING; break;
8356 case 'G': type = VIM_GENERIC; break;
8357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
8359 }
8360 }
8361 }
8362
8363 if (buttons == NULL || *buttons == NUL)
8364 buttons = (char_u *)_("&Ok");
8365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008366 if (error)
8367 rettv->vval.v_number = 0;
8368 else
8369 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 def, NULL);
8371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#endif
8374}
8375
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008376/*
8377 * "copy()" function
8378 */
8379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008381 typval_T *argvars;
8382 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008383{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008384 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008388 * "count()" function
8389 */
8390 static void
8391f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 typval_T *argvars;
8393 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008394{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008395 long n = 0;
8396 int ic = FALSE;
8397
Bram Moolenaare9a41262005-01-15 22:18:47 +00008398 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008399 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 listitem_T *li;
8401 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008402 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008403
Bram Moolenaare9a41262005-01-15 22:18:47 +00008404 if ((l = argvars[0].vval.v_list) != NULL)
8405 {
8406 li = l->lv_first;
8407 if (argvars[2].v_type != VAR_UNKNOWN)
8408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409 int error = FALSE;
8410
8411 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 if (argvars[3].v_type != VAR_UNKNOWN)
8413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 idx = get_tv_number_chk(&argvars[3], &error);
8415 if (!error)
8416 {
8417 li = list_find(l, idx);
8418 if (li == NULL)
8419 EMSGN(_(e_listidx), idx);
8420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422 if (error)
8423 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 }
8425
8426 for ( ; li != NULL; li = li->li_next)
8427 if (tv_equal(&li->li_tv, &argvars[1], ic))
8428 ++n;
8429 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 else if (argvars[0].v_type == VAR_DICT)
8432 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 int todo;
8434 dict_T *d;
8435 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008436
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008437 if ((d = argvars[0].vval.v_dict) != NULL)
8438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439 int error = FALSE;
8440
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441 if (argvars[2].v_type != VAR_UNKNOWN)
8442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008444 if (argvars[3].v_type != VAR_UNKNOWN)
8445 EMSG(_(e_invarg));
8446 }
8447
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008448 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008449 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008450 {
8451 if (!HASHITEM_EMPTY(hi))
8452 {
8453 --todo;
8454 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8455 ++n;
8456 }
8457 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008458 }
8459 }
8460 else
8461 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008462 rettv->vval.v_number = n;
8463}
8464
8465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8467 *
8468 * Checks the existence of a cscope connection.
8469 */
8470/*ARGSUSED*/
8471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008472f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008473 typval_T *argvars;
8474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476#ifdef FEAT_CSCOPE
8477 int num = 0;
8478 char_u *dbpath = NULL;
8479 char_u *prepend = NULL;
8480 char_u buf[NUMBUFLEN];
8481
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482 if (argvars[0].v_type != VAR_UNKNOWN
8483 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008485 num = (int)get_tv_number(&argvars[0]);
8486 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008487 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494#endif
8495}
8496
8497/*
8498 * "cursor(lnum, col)" function
8499 *
8500 * Moves the cursor to the specified line and column
8501 */
8502/*ARGSUSED*/
8503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 typval_T *argvars;
8506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507{
8508 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008509#ifdef FEAT_VIRTUALEDIT
8510 long coladd = 0;
8511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512
Bram Moolenaara5525202006-03-02 22:52:09 +00008513 if (argvars[1].v_type == VAR_UNKNOWN)
8514 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008515 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008516
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008517 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008518 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008519 line = pos.lnum;
8520 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008521#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008522 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008523#endif
8524 }
8525 else
8526 {
8527 line = get_tv_lnum(argvars);
8528 col = get_tv_number_chk(&argvars[1], NULL);
8529#ifdef FEAT_VIRTUALEDIT
8530 if (argvars[2].v_type != VAR_UNKNOWN)
8531 coladd = get_tv_number_chk(&argvars[2], NULL);
8532#endif
8533 }
8534 if (line < 0 || col < 0
8535#ifdef FEAT_VIRTUALEDIT
8536 || coladd < 0
8537#endif
8538 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (line > 0)
8541 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 if (col > 0)
8543 curwin->w_cursor.col = col - 1;
8544#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008545 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#endif
8547
8548 /* Make sure the cursor is in a valid position. */
8549 check_cursor();
8550#ifdef FEAT_MBYTE
8551 /* Correct cursor for multi-byte character. */
8552 if (has_mbyte)
8553 mb_adjust_cursor();
8554#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008555
8556 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557}
8558
8559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 */
8562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008564 typval_T *argvars;
8565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008567 int noref = 0;
8568
8569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008570 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008571 if (noref < 0 || noref > 1)
8572 EMSG(_(e_invarg));
8573 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008574 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575}
8576
8577/*
8578 * "delete()" function
8579 */
8580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *argvars;
8583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
8585 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591/*
8592 * "did_filetype()" function
8593 */
8594/*ARGSUSED*/
8595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 typval_T *argvars;
8598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604#endif
8605}
8606
8607/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008608 * "diff_filler()" function
8609 */
8610/*ARGSUSED*/
8611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008613 typval_T *argvars;
8614 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008615{
8616#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008618#endif
8619}
8620
8621/*
8622 * "diff_hlID()" function
8623 */
8624/*ARGSUSED*/
8625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008627 typval_T *argvars;
8628 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008629{
8630#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008632 static linenr_T prev_lnum = 0;
8633 static int changedtick = 0;
8634 static int fnum = 0;
8635 static int change_start = 0;
8636 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008637 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008638 int filler_lines;
8639 int col;
8640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 if (lnum < 0) /* ignore type error in {lnum} arg */
8642 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008643 if (lnum != prev_lnum
8644 || changedtick != curbuf->b_changedtick
8645 || fnum != curbuf->b_fnum)
8646 {
8647 /* New line, buffer, change: need to get the values. */
8648 filler_lines = diff_check(curwin, lnum);
8649 if (filler_lines < 0)
8650 {
8651 if (filler_lines == -1)
8652 {
8653 change_start = MAXCOL;
8654 change_end = -1;
8655 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8656 hlID = HLF_ADD; /* added line */
8657 else
8658 hlID = HLF_CHD; /* changed line */
8659 }
8660 else
8661 hlID = HLF_ADD; /* added line */
8662 }
8663 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008664 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008665 prev_lnum = lnum;
8666 changedtick = curbuf->b_changedtick;
8667 fnum = curbuf->b_fnum;
8668 }
8669
8670 if (hlID == HLF_CHD || hlID == HLF_TXD)
8671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008672 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008673 if (col >= change_start && col <= change_end)
8674 hlID = HLF_TXD; /* changed text */
8675 else
8676 hlID = HLF_CHD; /* changed line */
8677 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008678 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008679#endif
8680}
8681
8682/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008683 * "empty({expr})" function
8684 */
8685 static void
8686f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 typval_T *argvars;
8688 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008689{
8690 int n;
8691
8692 switch (argvars[0].v_type)
8693 {
8694 case VAR_STRING:
8695 case VAR_FUNC:
8696 n = argvars[0].vval.v_string == NULL
8697 || *argvars[0].vval.v_string == NUL;
8698 break;
8699 case VAR_NUMBER:
8700 n = argvars[0].vval.v_number == 0;
8701 break;
8702 case VAR_LIST:
8703 n = argvars[0].vval.v_list == NULL
8704 || argvars[0].vval.v_list->lv_first == NULL;
8705 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008706 case VAR_DICT:
8707 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008709 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008710 default:
8711 EMSG2(_(e_intern2), "f_empty()");
8712 n = 0;
8713 }
8714
8715 rettv->vval.v_number = n;
8716}
8717
8718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 * "escape({string}, {chars})" function
8720 */
8721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726 char_u buf[NUMBUFLEN];
8727
Bram Moolenaar758711c2005-02-02 23:11:38 +00008728 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8729 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731}
8732
8733/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008734 * "eval()" function
8735 */
8736/*ARGSUSED*/
8737 static void
8738f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008739 typval_T *argvars;
8740 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008741{
8742 char_u *s;
8743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 s = get_tv_string_chk(&argvars[0]);
8745 if (s != NULL)
8746 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8749 {
8750 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008751 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008753 else if (*s != NUL)
8754 EMSG(_(e_trailing));
8755}
8756
8757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 * "eventhandler()" function
8759 */
8760/*ARGSUSED*/
8761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 typval_T *argvars;
8764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767}
8768
8769/*
8770 * "executable()" function
8771 */
8772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008774 typval_T *argvars;
8775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
8780/*
8781 * "exists()" function
8782 */
8783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 typval_T *argvars;
8786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 char_u *p;
8789 char_u *name;
8790 int n = FALSE;
8791 int len = 0;
8792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (*p == '$') /* environment variable */
8795 {
8796 /* first try "normal" environment variables (fast) */
8797 if (mch_getenv(p + 1) != NULL)
8798 n = TRUE;
8799 else
8800 {
8801 /* try expanding things like $VIM and ${HOME} */
8802 p = expand_env_save(p);
8803 if (p != NULL && *p != '$')
8804 n = TRUE;
8805 vim_free(p);
8806 }
8807 }
8808 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008811 if (*skipwhite(p) != NUL)
8812 n = FALSE; /* trailing garbage */
8813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 else if (*p == '*') /* internal or user defined function */
8815 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008816 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 }
8818 else if (*p == ':')
8819 {
8820 n = cmd_exists(p + 1);
8821 }
8822 else if (*p == '#')
8823 {
8824#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008825 if (p[1] == '#')
8826 n = autocmd_supported(p + 2);
8827 else
8828 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829#endif
8830 }
8831 else /* internal variable */
8832 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008833 char_u *tofree;
8834 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008836 /* get_name_len() takes care of expanding curly braces */
8837 name = p;
8838 len = get_name_len(&p, &tofree, TRUE, FALSE);
8839 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008841 if (tofree != NULL)
8842 name = tofree;
8843 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8844 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008846 /* handle d.key, l[idx], f(expr) */
8847 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8848 if (n)
8849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008852 if (*p != NUL)
8853 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008855 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 }
8857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861/*
8862 * "expand()" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 char_u *s;
8870 int len;
8871 char_u *errormsg;
8872 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8873 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 rettv->v_type = VAR_STRING;
8877 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (*s == '%' || *s == '#' || *s == '<')
8879 {
8880 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 --emsg_off;
8883 }
8884 else
8885 {
8886 /* When the optional second argument is non-zero, don't remove matches
8887 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008888 if (argvars[1].v_type != VAR_UNKNOWN
8889 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 if (!error)
8892 {
8893 ExpandInit(&xpc);
8894 xpc.xp_context = EXPAND_FILES;
8895 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 }
8897 else
8898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900}
8901
8902/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008903 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008904 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008905 */
8906 static void
8907f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008908 typval_T *argvars;
8909 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008910{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008912 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008913 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 list_T *l1, *l2;
8915 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008916 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008918
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 l1 = argvars[0].vval.v_list;
8920 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008921 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8922 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008923 {
8924 if (argvars[2].v_type != VAR_UNKNOWN)
8925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008926 before = get_tv_number_chk(&argvars[2], &error);
8927 if (error)
8928 return; /* type error; errmsg already given */
8929
Bram Moolenaar758711c2005-02-02 23:11:38 +00008930 if (before == l1->lv_len)
8931 item = NULL;
8932 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008934 item = list_find(l1, before);
8935 if (item == NULL)
8936 {
8937 EMSGN(_(e_listidx), before);
8938 return;
8939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 }
8941 }
8942 else
8943 item = NULL;
8944 list_extend(l1, l2, item);
8945
Bram Moolenaare9a41262005-01-15 22:18:47 +00008946 copy_tv(&argvars[0], rettv);
8947 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008948 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 dict_T *d1, *d2;
8952 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008953 char_u *action;
8954 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008956 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957
8958 d1 = argvars[0].vval.v_dict;
8959 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008960 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8961 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 {
8963 /* Check the third argument. */
8964 if (argvars[2].v_type != VAR_UNKNOWN)
8965 {
8966 static char *(av[]) = {"keep", "force", "error"};
8967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 action = get_tv_string_chk(&argvars[2]);
8969 if (action == NULL)
8970 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 for (i = 0; i < 3; ++i)
8972 if (STRCMP(action, av[i]) == 0)
8973 break;
8974 if (i == 3)
8975 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008976 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008977 return;
8978 }
8979 }
8980 else
8981 action = (char_u *)"force";
8982
8983 /* Go over all entries in the second dict and add them to the
8984 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008985 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008987 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008988 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008990 --todo;
8991 di1 = dict_find(d1, hi2->hi_key, -1);
8992 if (di1 == NULL)
8993 {
8994 di1 = dictitem_copy(HI2DI(hi2));
8995 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8996 dictitem_free(di1);
8997 }
8998 else if (*action == 'e')
8999 {
9000 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9001 break;
9002 }
9003 else if (*action == 'f')
9004 {
9005 clear_tv(&di1->di_tv);
9006 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 }
9009 }
9010
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 copy_tv(&argvars[0], rettv);
9012 }
9013 }
9014 else
9015 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009016}
9017
9018/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009019 * "feedkeys()" function
9020 */
9021/*ARGSUSED*/
9022 static void
9023f_feedkeys(argvars, rettv)
9024 typval_T *argvars;
9025 typval_T *rettv;
9026{
9027 int remap = TRUE;
9028 char_u *keys, *flags;
9029 char_u nbuf[NUMBUFLEN];
9030 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009031 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009032
9033 rettv->vval.v_number = 0;
9034 keys = get_tv_string(&argvars[0]);
9035 if (*keys != NUL)
9036 {
9037 if (argvars[1].v_type != VAR_UNKNOWN)
9038 {
9039 flags = get_tv_string_buf(&argvars[1], nbuf);
9040 for ( ; *flags != NUL; ++flags)
9041 {
9042 switch (*flags)
9043 {
9044 case 'n': remap = FALSE; break;
9045 case 'm': remap = TRUE; break;
9046 case 't': typed = TRUE; break;
9047 }
9048 }
9049 }
9050
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009051 /* Need to escape K_SPECIAL and CSI before putting the string in the
9052 * typeahead buffer. */
9053 keys_esc = vim_strsave_escape_csi(keys);
9054 if (keys_esc != NULL)
9055 {
9056 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009057 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009058 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009059 if (vgetc_busy)
9060 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009061 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009062 }
9063}
9064
9065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 * "filereadable()" function
9067 */
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_filereadable(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 FILE *fd;
9074 char_u *p;
9075 int n;
9076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9079 {
9080 n = TRUE;
9081 fclose(fd);
9082 }
9083 else
9084 n = FALSE;
9085
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009090 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 * rights to write into.
9092 */
9093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 typval_T *argvars;
9096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009098 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099}
9100
Bram Moolenaar33570922005-01-25 22:26:29 +00009101static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009102
9103 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009104findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 typval_T *argvars;
9106 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009107 int dir;
9108{
9109#ifdef FEAT_SEARCHPATH
9110 char_u *fname;
9111 char_u *fresult = NULL;
9112 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9113 char_u *p;
9114 char_u pathbuf[NUMBUFLEN];
9115 int count = 1;
9116 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009117 int error = FALSE;
9118#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009119
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009120 rettv->vval.v_string = NULL;
9121 rettv->v_type = VAR_STRING;
9122
9123#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009125
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009126 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9129 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009130 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 else
9132 {
9133 if (*p != NUL)
9134 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009137 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009139 }
9140
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009141 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9142 error = TRUE;
9143
9144 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 do
9147 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009148 if (rettv->v_type == VAR_STRING)
9149 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 fresult = find_file_in_path_option(first ? fname : NULL,
9151 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009152 0, first, path, dir, NULL,
9153 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009155
9156 if (fresult != NULL && rettv->v_type == VAR_LIST)
9157 list_append_string(rettv->vval.v_list, fresult, -1);
9158
9159 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009161
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009162 if (rettv->v_type == VAR_STRING)
9163 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009164#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009165}
9166
Bram Moolenaar33570922005-01-25 22:26:29 +00009167static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9168static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009169
9170/*
9171 * Implementation of map() and filter().
9172 */
9173 static void
9174filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009175 typval_T *argvars;
9176 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009177 int map;
9178{
9179 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009180 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009181 listitem_T *li, *nli;
9182 list_T *l = NULL;
9183 dictitem_T *di;
9184 hashtab_T *ht;
9185 hashitem_T *hi;
9186 dict_T *d = NULL;
9187 typval_T save_val;
9188 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009189 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009190 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009191 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009192 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009193
9194 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009195 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009196 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009197 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009198 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199 return;
9200 }
9201 else if (argvars[0].v_type == VAR_DICT)
9202 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009203 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009204 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009205 return;
9206 }
9207 else
9208 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009209 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009210 return;
9211 }
9212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 expr = get_tv_string_buf_chk(&argvars[1], buf);
9214 /* On type errors, the preceding call has already displayed an error
9215 * message. Avoid a misleading error message for an empty string that
9216 * was not passed as argument. */
9217 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 prepare_vimvar(VV_VAL, &save_val);
9220 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009221
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009222 /* We reset "did_emsg" to be able to detect whether an error
9223 * occurred during evaluation of the expression. */
9224 save_did_emsg = did_emsg;
9225 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229 prepare_vimvar(VV_KEY, &save_key);
9230 vimvars[VV_KEY].vv_type = VAR_STRING;
9231
9232 ht = &d->dv_hashtab;
9233 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009234 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 if (!HASHITEM_EMPTY(hi))
9238 {
9239 --todo;
9240 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009241 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 break;
9243 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009244 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009245 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 break;
9247 if (!map && rem)
9248 dictitem_remove(d, di);
9249 clear_tv(&vimvars[VV_KEY].vv_tv);
9250 }
9251 }
9252 hash_unlock(ht);
9253
9254 restore_vimvar(VV_KEY, &save_key);
9255 }
9256 else
9257 {
9258 for (li = l->lv_first; li != NULL; li = nli)
9259 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009260 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009261 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009263 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009264 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009265 break;
9266 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009272
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009273 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009275
9276 copy_tv(&argvars[0], rettv);
9277}
9278
9279 static int
9280filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 char_u *expr;
9283 int map;
9284 int *remp;
9285{
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287 char_u *s;
9288
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 s = expr;
9291 if (eval1(&s, &rettv, TRUE) == FAIL)
9292 return FAIL;
9293 if (*s != NUL) /* check for trailing chars after expr */
9294 {
9295 EMSG2(_(e_invexpr2), s);
9296 return FAIL;
9297 }
9298 if (map)
9299 {
9300 /* map(): replace the list item value */
9301 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009302 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 *tv = rettv;
9304 }
9305 else
9306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009307 int error = FALSE;
9308
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009312 /* On type error, nothing has been removed; return FAIL to stop the
9313 * loop. The error message was given by get_tv_number_chk(). */
9314 if (error)
9315 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009318 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009319}
9320
9321/*
9322 * "filter()" function
9323 */
9324 static void
9325f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009328{
9329 filter_map(argvars, rettv, FALSE);
9330}
9331
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009333 * "finddir({fname}[, {path}[, {count}]])" function
9334 */
9335 static void
9336f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 typval_T *argvars;
9338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009339{
9340 findfilendir(argvars, rettv, TRUE);
9341}
9342
9343/*
9344 * "findfile({fname}[, {path}[, {count}]])" function
9345 */
9346 static void
9347f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009348 typval_T *argvars;
9349 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009350{
9351 findfilendir(argvars, rettv, FALSE);
9352}
9353
9354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 * "fnamemodify({fname}, {mods})" function
9356 */
9357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009359 typval_T *argvars;
9360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361{
9362 char_u *fname;
9363 char_u *mods;
9364 int usedlen = 0;
9365 int len;
9366 char_u *fbuf = NULL;
9367 char_u buf[NUMBUFLEN];
9368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 fname = get_tv_string_chk(&argvars[0]);
9370 mods = get_tv_string_buf_chk(&argvars[1], buf);
9371 if (fname == NULL || mods == NULL)
9372 fname = NULL;
9373 else
9374 {
9375 len = (int)STRLEN(fname);
9376 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 vim_free(fbuf);
9385}
9386
Bram Moolenaar33570922005-01-25 22:26:29 +00009387static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
9389/*
9390 * "foldclosed()" function
9391 */
9392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 typval_T *argvars;
9395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 int end;
9397{
9398#ifdef FEAT_FOLDING
9399 linenr_T lnum;
9400 linenr_T first, last;
9401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9404 {
9405 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9406 {
9407 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 return;
9412 }
9413 }
9414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416}
9417
9418/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419 * "foldclosed()" function
9420 */
9421 static void
9422f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 typval_T *argvars;
9424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009425{
9426 foldclosed_both(argvars, rettv, FALSE);
9427}
9428
9429/*
9430 * "foldclosedend()" function
9431 */
9432 static void
9433f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009434 typval_T *argvars;
9435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436{
9437 foldclosed_both(argvars, rettv, TRUE);
9438}
9439
9440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * "foldlevel()" function
9442 */
9443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448#ifdef FEAT_FOLDING
9449 linenr_T lnum;
9450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 else
9455#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457}
9458
9459/*
9460 * "foldtext()" function
9461 */
9462/*ARGSUSED*/
9463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009465 typval_T *argvars;
9466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468#ifdef FEAT_FOLDING
9469 linenr_T lnum;
9470 char_u *s;
9471 char_u *r;
9472 int len;
9473 char *txt;
9474#endif
9475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->v_type = VAR_STRING;
9477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009479 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9480 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9481 <= curbuf->b_ml.ml_line_count
9482 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 {
9484 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009485 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9486 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
9488 if (!linewhite(lnum))
9489 break;
9490 ++lnum;
9491 }
9492
9493 /* Find interesting text in this line. */
9494 s = skipwhite(ml_get(lnum));
9495 /* skip C comment-start */
9496 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009499 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009500 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009501 {
9502 s = skipwhite(ml_get(lnum + 1));
9503 if (*s == '*')
9504 s = skipwhite(s + 1);
9505 }
9506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 txt = _("+-%s%3ld lines: ");
9508 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009509 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 + 20 /* for %3ld */
9511 + STRLEN(s))); /* concatenated */
9512 if (r != NULL)
9513 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009514 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9515 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9516 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 len = (int)STRLEN(r);
9518 STRCAT(r, s);
9519 /* remove 'foldmarker' and 'commentstring' */
9520 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 }
9523 }
9524#endif
9525}
9526
9527/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009528 * "foldtextresult(lnum)" function
9529 */
9530/*ARGSUSED*/
9531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009533 typval_T *argvars;
9534 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009535{
9536#ifdef FEAT_FOLDING
9537 linenr_T lnum;
9538 char_u *text;
9539 char_u buf[51];
9540 foldinfo_T foldinfo;
9541 int fold_count;
9542#endif
9543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544 rettv->v_type = VAR_STRING;
9545 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009546#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548 /* treat illegal types and illegal string values for {lnum} the same */
9549 if (lnum < 0)
9550 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009551 fold_count = foldedCount(curwin, lnum, &foldinfo);
9552 if (fold_count > 0)
9553 {
9554 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9555 &foldinfo, buf);
9556 if (text == buf)
9557 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009559 }
9560#endif
9561}
9562
9563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 * "foreground()" function
9565 */
9566/*ARGSUSED*/
9567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009569 typval_T *argvars;
9570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573#ifdef FEAT_GUI
9574 if (gui.in_use)
9575 gui_mch_set_foreground();
9576#else
9577# ifdef WIN32
9578 win32_set_foreground();
9579# endif
9580#endif
9581}
9582
9583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009584 * "function()" function
9585 */
9586/*ARGSUSED*/
9587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009588f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009589 typval_T *argvars;
9590 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591{
9592 char_u *s;
9593
Bram Moolenaara7043832005-01-21 11:56:39 +00009594 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009596 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597 EMSG2(_(e_invarg2), s);
9598 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009599 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009600 else
9601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_string = vim_strsave(s);
9603 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009604 }
9605}
9606
9607/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009608 * "garbagecollect()" function
9609 */
9610/*ARGSUSED*/
9611 static void
9612f_garbagecollect(argvars, rettv)
9613 typval_T *argvars;
9614 typval_T *rettv;
9615{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009616 /* This is postponed until we are back at the toplevel, because we may be
9617 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9618 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009619}
9620
9621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009622 * "get()" function
9623 */
9624 static void
9625f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009628{
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 listitem_T *li;
9630 list_T *l;
9631 dictitem_T *di;
9632 dict_T *d;
9633 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009634
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009636 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 int error = FALSE;
9640
9641 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9642 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 else if (argvars[0].v_type == VAR_DICT)
9647 {
9648 if ((d = argvars[0].vval.v_dict) != NULL)
9649 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009650 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 if (di != NULL)
9652 tv = &di->di_tv;
9653 }
9654 }
9655 else
9656 EMSG2(_(e_listdictarg), "get()");
9657
9658 if (tv == NULL)
9659 {
9660 if (argvars[2].v_type == VAR_UNKNOWN)
9661 rettv->vval.v_number = 0;
9662 else
9663 copy_tv(&argvars[2], rettv);
9664 }
9665 else
9666 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009667}
9668
Bram Moolenaar342337a2005-07-21 21:11:17 +00009669static 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 +00009670
9671/*
9672 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009673 * Return a range (from start to end) of lines in rettv from the specified
9674 * buffer.
9675 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009676 */
9677 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009678get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009679 buf_T *buf;
9680 linenr_T start;
9681 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009682 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009683 typval_T *rettv;
9684{
9685 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009686
Bram Moolenaar342337a2005-07-21 21:11:17 +00009687 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009688 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009689 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009690 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009691 }
9692 else
9693 rettv->vval.v_number = 0;
9694
9695 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9696 return;
9697
9698 if (!retlist)
9699 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009700 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9701 p = ml_get_buf(buf, start, FALSE);
9702 else
9703 p = (char_u *)"";
9704
9705 rettv->v_type = VAR_STRING;
9706 rettv->vval.v_string = vim_strsave(p);
9707 }
9708 else
9709 {
9710 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009711 return;
9712
9713 if (start < 1)
9714 start = 1;
9715 if (end > buf->b_ml.ml_line_count)
9716 end = buf->b_ml.ml_line_count;
9717 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009718 if (list_append_string(rettv->vval.v_list,
9719 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009720 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009721 }
9722}
9723
9724/*
9725 * "getbufline()" function
9726 */
9727 static void
9728f_getbufline(argvars, rettv)
9729 typval_T *argvars;
9730 typval_T *rettv;
9731{
9732 linenr_T lnum;
9733 linenr_T end;
9734 buf_T *buf;
9735
9736 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9737 ++emsg_off;
9738 buf = get_buf_tv(&argvars[0]);
9739 --emsg_off;
9740
Bram Moolenaar661b1822005-07-28 22:36:45 +00009741 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009742 if (argvars[2].v_type == VAR_UNKNOWN)
9743 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009744 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009745 end = get_tv_lnum_buf(&argvars[2], buf);
9746
Bram Moolenaar342337a2005-07-21 21:11:17 +00009747 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009748}
9749
Bram Moolenaar0d660222005-01-07 21:51:51 +00009750/*
9751 * "getbufvar()" function
9752 */
9753 static void
9754f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009755 typval_T *argvars;
9756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009757{
9758 buf_T *buf;
9759 buf_T *save_curbuf;
9760 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009761 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9764 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009765 ++emsg_off;
9766 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009767
9768 rettv->v_type = VAR_STRING;
9769 rettv->vval.v_string = NULL;
9770
9771 if (buf != NULL && varname != NULL)
9772 {
9773 if (*varname == '&') /* buffer-local-option */
9774 {
9775 /* set curbuf to be our buf, temporarily */
9776 save_curbuf = curbuf;
9777 curbuf = buf;
9778
9779 get_option_tv(&varname, rettv, TRUE);
9780
9781 /* restore previous notion of curbuf */
9782 curbuf = save_curbuf;
9783 }
9784 else
9785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 if (*varname == NUL)
9787 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9788 * scope prefix before the NUL byte is required by
9789 * find_var_in_ht(). */
9790 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009791 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009792 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009793 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009794 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009795 }
9796 }
9797
9798 --emsg_off;
9799}
9800
9801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 * "getchar()" function
9803 */
9804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009810 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009812 /* Position the cursor. Needed after a message that ends in a space. */
9813 windgoto(msg_row, msg_col);
9814
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 ++no_mapping;
9816 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009817 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 /* getchar(): blocking wait. */
9819 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009820 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 /* getchar(1): only check if char avail */
9822 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 else if (error || vpeekc() == NUL)
9824 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 n = 0;
9826 else
9827 /* getchar(0) and char avail: return char */
9828 n = safe_vgetc();
9829 --no_mapping;
9830 --allow_keys;
9831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (IS_SPECIAL(n) || mod_mask != 0)
9834 {
9835 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9836 int i = 0;
9837
9838 /* Turn a special key into three bytes, plus modifier. */
9839 if (mod_mask != 0)
9840 {
9841 temp[i++] = K_SPECIAL;
9842 temp[i++] = KS_MODIFIER;
9843 temp[i++] = mod_mask;
9844 }
9845 if (IS_SPECIAL(n))
9846 {
9847 temp[i++] = K_SPECIAL;
9848 temp[i++] = K_SECOND(n);
9849 temp[i++] = K_THIRD(n);
9850 }
9851#ifdef FEAT_MBYTE
9852 else if (has_mbyte)
9853 i += (*mb_char2bytes)(n, temp + i);
9854#endif
9855 else
9856 temp[i++] = n;
9857 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->v_type = VAR_STRING;
9859 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 }
9861}
9862
9863/*
9864 * "getcharmod()" function
9865 */
9866/*ARGSUSED*/
9867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873}
9874
9875/*
9876 * "getcmdline()" function
9877 */
9878/*ARGSUSED*/
9879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009881 typval_T *argvars;
9882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 rettv->v_type = VAR_STRING;
9885 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886}
9887
9888/*
9889 * "getcmdpos()" function
9890 */
9891/*ARGSUSED*/
9892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009893f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009894 typval_T *argvars;
9895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898}
9899
9900/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009901 * "getcmdtype()" function
9902 */
9903/*ARGSUSED*/
9904 static void
9905f_getcmdtype(argvars, rettv)
9906 typval_T *argvars;
9907 typval_T *rettv;
9908{
9909 rettv->v_type = VAR_STRING;
9910 rettv->vval.v_string = alloc(2);
9911 if (rettv->vval.v_string != NULL)
9912 {
9913 rettv->vval.v_string[0] = get_cmdline_type();
9914 rettv->vval.v_string[1] = NUL;
9915 }
9916}
9917
9918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 * "getcwd()" function
9920 */
9921/*ARGSUSED*/
9922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009924 typval_T *argvars;
9925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
9927 char_u cwd[MAXPATHL];
9928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 else
9933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009936 if (rettv->vval.v_string != NULL)
9937 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938#endif
9939 }
9940}
9941
9942/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009943 * "getfontname()" function
9944 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009945/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009947f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009948 typval_T *argvars;
9949 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009950{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->v_type = VAR_STRING;
9952 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009953#ifdef FEAT_GUI
9954 if (gui.in_use)
9955 {
9956 GuiFont font;
9957 char_u *name = NULL;
9958
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009959 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009960 {
9961 /* Get the "Normal" font. Either the name saved by
9962 * hl_set_font_name() or from the font ID. */
9963 font = gui.norm_font;
9964 name = hl_get_font_name();
9965 }
9966 else
9967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009969 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9970 return;
9971 font = gui_mch_get_font(name, FALSE);
9972 if (font == NOFONT)
9973 return; /* Invalid font name, return empty string. */
9974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009977 gui_mch_free_font(font);
9978 }
9979#endif
9980}
9981
9982/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009983 * "getfperm({fname})" function
9984 */
9985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009989{
9990 char_u *fname;
9991 struct stat st;
9992 char_u *perm = NULL;
9993 char_u flags[] = "rwx";
9994 int i;
9995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009999 if (mch_stat((char *)fname, &st) >= 0)
10000 {
10001 perm = vim_strsave((char_u *)"---------");
10002 if (perm != NULL)
10003 {
10004 for (i = 0; i < 9; i++)
10005 {
10006 if (st.st_mode & (1 << (8 - i)))
10007 perm[i] = flags[i % 3];
10008 }
10009 }
10010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010012}
10013
10014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 * "getfsize({fname})" function
10016 */
10017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 typval_T *argvars;
10020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 char_u *fname;
10023 struct stat st;
10024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
10029 if (mch_stat((char *)fname, &st) >= 0)
10030 {
10031 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 }
10036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038}
10039
10040/*
10041 * "getftime({fname})" function
10042 */
10043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010045 typval_T *argvars;
10046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 char_u *fname;
10049 struct stat st;
10050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
10053 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
10059/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010060 * "getftype({fname})" function
10061 */
10062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 typval_T *argvars;
10065 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010066{
10067 char_u *fname;
10068 struct stat st;
10069 char_u *type = NULL;
10070 char *t;
10071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010075 if (mch_lstat((char *)fname, &st) >= 0)
10076 {
10077#ifdef S_ISREG
10078 if (S_ISREG(st.st_mode))
10079 t = "file";
10080 else if (S_ISDIR(st.st_mode))
10081 t = "dir";
10082# ifdef S_ISLNK
10083 else if (S_ISLNK(st.st_mode))
10084 t = "link";
10085# endif
10086# ifdef S_ISBLK
10087 else if (S_ISBLK(st.st_mode))
10088 t = "bdev";
10089# endif
10090# ifdef S_ISCHR
10091 else if (S_ISCHR(st.st_mode))
10092 t = "cdev";
10093# endif
10094# ifdef S_ISFIFO
10095 else if (S_ISFIFO(st.st_mode))
10096 t = "fifo";
10097# endif
10098# ifdef S_ISSOCK
10099 else if (S_ISSOCK(st.st_mode))
10100 t = "fifo";
10101# endif
10102 else
10103 t = "other";
10104#else
10105# ifdef S_IFMT
10106 switch (st.st_mode & S_IFMT)
10107 {
10108 case S_IFREG: t = "file"; break;
10109 case S_IFDIR: t = "dir"; break;
10110# ifdef S_IFLNK
10111 case S_IFLNK: t = "link"; break;
10112# endif
10113# ifdef S_IFBLK
10114 case S_IFBLK: t = "bdev"; break;
10115# endif
10116# ifdef S_IFCHR
10117 case S_IFCHR: t = "cdev"; break;
10118# endif
10119# ifdef S_IFIFO
10120 case S_IFIFO: t = "fifo"; break;
10121# endif
10122# ifdef S_IFSOCK
10123 case S_IFSOCK: t = "socket"; break;
10124# endif
10125 default: t = "other";
10126 }
10127# else
10128 if (mch_isdir(fname))
10129 t = "dir";
10130 else
10131 t = "file";
10132# endif
10133#endif
10134 type = vim_strsave((char_u *)t);
10135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010137}
10138
10139/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010140 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010141 */
10142 static void
10143f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010144 typval_T *argvars;
10145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010146{
10147 linenr_T lnum;
10148 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010149 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010150
10151 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010152 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010153 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010154 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010155 retlist = FALSE;
10156 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010157 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010158 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010159 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010160 retlist = TRUE;
10161 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010162
Bram Moolenaar342337a2005-07-21 21:11:17 +000010163 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010164}
10165
10166/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010167 * "getpos(string)" function
10168 */
10169 static void
10170f_getpos(argvars, rettv)
10171 typval_T *argvars;
10172 typval_T *rettv;
10173{
10174 pos_T *fp;
10175 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010176 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010177
10178 if (rettv_list_alloc(rettv) == OK)
10179 {
10180 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010181 fp = var2fpos(&argvars[0], TRUE, &fnum);
10182 if (fnum != -1)
10183 list_append_number(l, (varnumber_T)fnum);
10184 else
10185 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010186 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10187 : (varnumber_T)0);
10188 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10189 : (varnumber_T)0);
10190 list_append_number(l,
10191#ifdef FEAT_VIRTUALEDIT
10192 (fp != NULL) ? (varnumber_T)fp->coladd :
10193#endif
10194 (varnumber_T)0);
10195 }
10196 else
10197 rettv->vval.v_number = FALSE;
10198}
10199
10200/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010201 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010202 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010203/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010204 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010205f_getqflist(argvars, rettv)
10206 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010207 typval_T *rettv;
10208{
10209#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010210 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010211#endif
10212
10213 rettv->vval.v_number = FALSE;
10214#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010215 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010216 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010217 wp = NULL;
10218 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10219 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010220 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010221 if (wp == NULL)
10222 return;
10223 }
10224
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010225 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010226 }
10227#endif
10228}
10229
10230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 * "getreg()" function
10232 */
10233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010234f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010235 typval_T *argvars;
10236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237{
10238 char_u *strregname;
10239 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010240 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010243 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245 strregname = get_tv_string_chk(&argvars[0]);
10246 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010247 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 regname = (strregname == NULL ? '"' : *strregname);
10253 if (regname == 0)
10254 regname = '"';
10255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 rettv->vval.v_string = error ? NULL :
10258 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259}
10260
10261/*
10262 * "getregtype()" function
10263 */
10264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010265f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010266 typval_T *argvars;
10267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
10269 char_u *strregname;
10270 int regname;
10271 char_u buf[NUMBUFLEN + 2];
10272 long reglen = 0;
10273
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010274 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 {
10276 strregname = get_tv_string_chk(&argvars[0]);
10277 if (strregname == NULL) /* type error; errmsg already given */
10278 {
10279 rettv->v_type = VAR_STRING;
10280 rettv->vval.v_string = NULL;
10281 return;
10282 }
10283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 else
10285 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287
10288 regname = (strregname == NULL ? '"' : *strregname);
10289 if (regname == 0)
10290 regname = '"';
10291
10292 buf[0] = NUL;
10293 buf[1] = NUL;
10294 switch (get_reg_type(regname, &reglen))
10295 {
10296 case MLINE: buf[0] = 'V'; break;
10297 case MCHAR: buf[0] = 'v'; break;
10298#ifdef FEAT_VISUAL
10299 case MBLOCK:
10300 buf[0] = Ctrl_V;
10301 sprintf((char *)buf + 1, "%ld", reglen + 1);
10302 break;
10303#endif
10304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305 rettv->v_type = VAR_STRING;
10306 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307}
10308
10309/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010310 * "gettabwinvar()" function
10311 */
10312 static void
10313f_gettabwinvar(argvars, rettv)
10314 typval_T *argvars;
10315 typval_T *rettv;
10316{
10317 getwinvar(argvars, rettv, 1);
10318}
10319
10320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 * "getwinposx()" function
10322 */
10323/*ARGSUSED*/
10324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010326 typval_T *argvars;
10327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330#ifdef FEAT_GUI
10331 if (gui.in_use)
10332 {
10333 int x, y;
10334
10335 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 }
10338#endif
10339}
10340
10341/*
10342 * "getwinposy()" function
10343 */
10344/*ARGSUSED*/
10345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010347 typval_T *argvars;
10348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351#ifdef FEAT_GUI
10352 if (gui.in_use)
10353 {
10354 int x, y;
10355
10356 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359#endif
10360}
10361
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010362/*
10363 * Find window specifed by "vp" in tabpage "tp".
10364 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010365 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010366find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010367 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010368 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010369{
10370#ifdef FEAT_WINDOWS
10371 win_T *wp;
10372#endif
10373 int nr;
10374
10375 nr = get_tv_number_chk(vp, NULL);
10376
10377#ifdef FEAT_WINDOWS
10378 if (nr < 0)
10379 return NULL;
10380 if (nr == 0)
10381 return curwin;
10382
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010383 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10384 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010385 if (--nr <= 0)
10386 break;
10387 return wp;
10388#else
10389 if (nr == 0 || nr == 1)
10390 return curwin;
10391 return NULL;
10392#endif
10393}
10394
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395/*
10396 * "getwinvar()" function
10397 */
10398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 typval_T *argvars;
10401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010403 getwinvar(argvars, rettv, 0);
10404}
10405
10406/*
10407 * getwinvar() and gettabwinvar()
10408 */
10409 static void
10410getwinvar(argvars, rettv, off)
10411 typval_T *argvars;
10412 typval_T *rettv;
10413 int off; /* 1 for gettabwinvar() */
10414{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 win_T *win, *oldcurwin;
10416 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010417 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010418 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010420#ifdef FEAT_WINDOWS
10421 if (off == 1)
10422 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10423 else
10424 tp = curtab;
10425#endif
10426 win = find_win_by_nr(&argvars[off], tp);
10427 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->v_type = VAR_STRING;
10431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432
10433 if (win != NULL && varname != NULL)
10434 {
10435 if (*varname == '&') /* window-local-option */
10436 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010437 /* Set curwin to be our win, temporarily. Also set curbuf, so
10438 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 oldcurwin = curwin;
10440 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010441 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444
10445 /* restore previous notion of curwin */
10446 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010447 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 }
10449 else
10450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 if (*varname == NUL)
10452 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10453 * scope prefix before the NUL byte is required by
10454 * find_var_in_ht(). */
10455 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010457 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 }
10461 }
10462
10463 --emsg_off;
10464}
10465
10466/*
10467 * "glob()" function
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
10474 expand_T xpc;
10475
10476 ExpandInit(&xpc);
10477 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478 rettv->v_type = VAR_STRING;
10479 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481}
10482
10483/*
10484 * "globpath()" function
10485 */
10486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *argvars;
10489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490{
10491 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 if (file == NULL)
10496 rettv->vval.v_string = NULL;
10497 else
10498 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501/*
10502 * "has()" function
10503 */
10504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010506 typval_T *argvars;
10507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 int i;
10510 char_u *name;
10511 int n = FALSE;
10512 static char *(has_list[]) =
10513 {
10514#ifdef AMIGA
10515 "amiga",
10516# ifdef FEAT_ARP
10517 "arp",
10518# endif
10519#endif
10520#ifdef __BEOS__
10521 "beos",
10522#endif
10523#ifdef MSDOS
10524# ifdef DJGPP
10525 "dos32",
10526# else
10527 "dos16",
10528# endif
10529#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010530#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 "mac",
10532#endif
10533#if defined(MACOS_X_UNIX)
10534 "macunix",
10535#endif
10536#ifdef OS2
10537 "os2",
10538#endif
10539#ifdef __QNX__
10540 "qnx",
10541#endif
10542#ifdef RISCOS
10543 "riscos",
10544#endif
10545#ifdef UNIX
10546 "unix",
10547#endif
10548#ifdef VMS
10549 "vms",
10550#endif
10551#ifdef WIN16
10552 "win16",
10553#endif
10554#ifdef WIN32
10555 "win32",
10556#endif
10557#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10558 "win32unix",
10559#endif
10560#ifdef WIN64
10561 "win64",
10562#endif
10563#ifdef EBCDIC
10564 "ebcdic",
10565#endif
10566#ifndef CASE_INSENSITIVE_FILENAME
10567 "fname_case",
10568#endif
10569#ifdef FEAT_ARABIC
10570 "arabic",
10571#endif
10572#ifdef FEAT_AUTOCMD
10573 "autocmd",
10574#endif
10575#ifdef FEAT_BEVAL
10576 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010577# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10578 "balloon_multiline",
10579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580#endif
10581#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10582 "builtin_terms",
10583# ifdef ALL_BUILTIN_TCAPS
10584 "all_builtin_terms",
10585# endif
10586#endif
10587#ifdef FEAT_BYTEOFF
10588 "byte_offset",
10589#endif
10590#ifdef FEAT_CINDENT
10591 "cindent",
10592#endif
10593#ifdef FEAT_CLIENTSERVER
10594 "clientserver",
10595#endif
10596#ifdef FEAT_CLIPBOARD
10597 "clipboard",
10598#endif
10599#ifdef FEAT_CMDL_COMPL
10600 "cmdline_compl",
10601#endif
10602#ifdef FEAT_CMDHIST
10603 "cmdline_hist",
10604#endif
10605#ifdef FEAT_COMMENTS
10606 "comments",
10607#endif
10608#ifdef FEAT_CRYPT
10609 "cryptv",
10610#endif
10611#ifdef FEAT_CSCOPE
10612 "cscope",
10613#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010614#ifdef CURSOR_SHAPE
10615 "cursorshape",
10616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617#ifdef DEBUG
10618 "debug",
10619#endif
10620#ifdef FEAT_CON_DIALOG
10621 "dialog_con",
10622#endif
10623#ifdef FEAT_GUI_DIALOG
10624 "dialog_gui",
10625#endif
10626#ifdef FEAT_DIFF
10627 "diff",
10628#endif
10629#ifdef FEAT_DIGRAPHS
10630 "digraphs",
10631#endif
10632#ifdef FEAT_DND
10633 "dnd",
10634#endif
10635#ifdef FEAT_EMACS_TAGS
10636 "emacs_tags",
10637#endif
10638 "eval", /* always present, of course! */
10639#ifdef FEAT_EX_EXTRA
10640 "ex_extra",
10641#endif
10642#ifdef FEAT_SEARCH_EXTRA
10643 "extra_search",
10644#endif
10645#ifdef FEAT_FKMAP
10646 "farsi",
10647#endif
10648#ifdef FEAT_SEARCHPATH
10649 "file_in_path",
10650#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010651#if defined(UNIX) && !defined(USE_SYSTEM)
10652 "filterpipe",
10653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654#ifdef FEAT_FIND_ID
10655 "find_in_path",
10656#endif
10657#ifdef FEAT_FOLDING
10658 "folding",
10659#endif
10660#ifdef FEAT_FOOTER
10661 "footer",
10662#endif
10663#if !defined(USE_SYSTEM) && defined(UNIX)
10664 "fork",
10665#endif
10666#ifdef FEAT_GETTEXT
10667 "gettext",
10668#endif
10669#ifdef FEAT_GUI
10670 "gui",
10671#endif
10672#ifdef FEAT_GUI_ATHENA
10673# ifdef FEAT_GUI_NEXTAW
10674 "gui_neXtaw",
10675# else
10676 "gui_athena",
10677# endif
10678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679#ifdef FEAT_GUI_GTK
10680 "gui_gtk",
10681# ifdef HAVE_GTK2
10682 "gui_gtk2",
10683# endif
10684#endif
10685#ifdef FEAT_GUI_MAC
10686 "gui_mac",
10687#endif
10688#ifdef FEAT_GUI_MOTIF
10689 "gui_motif",
10690#endif
10691#ifdef FEAT_GUI_PHOTON
10692 "gui_photon",
10693#endif
10694#ifdef FEAT_GUI_W16
10695 "gui_win16",
10696#endif
10697#ifdef FEAT_GUI_W32
10698 "gui_win32",
10699#endif
10700#ifdef FEAT_HANGULIN
10701 "hangul_input",
10702#endif
10703#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10704 "iconv",
10705#endif
10706#ifdef FEAT_INS_EXPAND
10707 "insert_expand",
10708#endif
10709#ifdef FEAT_JUMPLIST
10710 "jumplist",
10711#endif
10712#ifdef FEAT_KEYMAP
10713 "keymap",
10714#endif
10715#ifdef FEAT_LANGMAP
10716 "langmap",
10717#endif
10718#ifdef FEAT_LIBCALL
10719 "libcall",
10720#endif
10721#ifdef FEAT_LINEBREAK
10722 "linebreak",
10723#endif
10724#ifdef FEAT_LISP
10725 "lispindent",
10726#endif
10727#ifdef FEAT_LISTCMDS
10728 "listcmds",
10729#endif
10730#ifdef FEAT_LOCALMAP
10731 "localmap",
10732#endif
10733#ifdef FEAT_MENU
10734 "menu",
10735#endif
10736#ifdef FEAT_SESSION
10737 "mksession",
10738#endif
10739#ifdef FEAT_MODIFY_FNAME
10740 "modify_fname",
10741#endif
10742#ifdef FEAT_MOUSE
10743 "mouse",
10744#endif
10745#ifdef FEAT_MOUSESHAPE
10746 "mouseshape",
10747#endif
10748#if defined(UNIX) || defined(VMS)
10749# ifdef FEAT_MOUSE_DEC
10750 "mouse_dec",
10751# endif
10752# ifdef FEAT_MOUSE_GPM
10753 "mouse_gpm",
10754# endif
10755# ifdef FEAT_MOUSE_JSB
10756 "mouse_jsbterm",
10757# endif
10758# ifdef FEAT_MOUSE_NET
10759 "mouse_netterm",
10760# endif
10761# ifdef FEAT_MOUSE_PTERM
10762 "mouse_pterm",
10763# endif
10764# ifdef FEAT_MOUSE_XTERM
10765 "mouse_xterm",
10766# endif
10767#endif
10768#ifdef FEAT_MBYTE
10769 "multi_byte",
10770#endif
10771#ifdef FEAT_MBYTE_IME
10772 "multi_byte_ime",
10773#endif
10774#ifdef FEAT_MULTI_LANG
10775 "multi_lang",
10776#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010777#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010778#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010779 "mzscheme",
10780#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782#ifdef FEAT_OLE
10783 "ole",
10784#endif
10785#ifdef FEAT_OSFILETYPE
10786 "osfiletype",
10787#endif
10788#ifdef FEAT_PATH_EXTRA
10789 "path_extra",
10790#endif
10791#ifdef FEAT_PERL
10792#ifndef DYNAMIC_PERL
10793 "perl",
10794#endif
10795#endif
10796#ifdef FEAT_PYTHON
10797#ifndef DYNAMIC_PYTHON
10798 "python",
10799#endif
10800#endif
10801#ifdef FEAT_POSTSCRIPT
10802 "postscript",
10803#endif
10804#ifdef FEAT_PRINTER
10805 "printer",
10806#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010807#ifdef FEAT_PROFILE
10808 "profile",
10809#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010810#ifdef FEAT_RELTIME
10811 "reltime",
10812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813#ifdef FEAT_QUICKFIX
10814 "quickfix",
10815#endif
10816#ifdef FEAT_RIGHTLEFT
10817 "rightleft",
10818#endif
10819#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10820 "ruby",
10821#endif
10822#ifdef FEAT_SCROLLBIND
10823 "scrollbind",
10824#endif
10825#ifdef FEAT_CMDL_INFO
10826 "showcmd",
10827 "cmdline_info",
10828#endif
10829#ifdef FEAT_SIGNS
10830 "signs",
10831#endif
10832#ifdef FEAT_SMARTINDENT
10833 "smartindent",
10834#endif
10835#ifdef FEAT_SNIFF
10836 "sniff",
10837#endif
10838#ifdef FEAT_STL_OPT
10839 "statusline",
10840#endif
10841#ifdef FEAT_SUN_WORKSHOP
10842 "sun_workshop",
10843#endif
10844#ifdef FEAT_NETBEANS_INTG
10845 "netbeans_intg",
10846#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010847#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010848 "spell",
10849#endif
10850#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 "syntax",
10852#endif
10853#if defined(USE_SYSTEM) || !defined(UNIX)
10854 "system",
10855#endif
10856#ifdef FEAT_TAG_BINS
10857 "tag_binary",
10858#endif
10859#ifdef FEAT_TAG_OLDSTATIC
10860 "tag_old_static",
10861#endif
10862#ifdef FEAT_TAG_ANYWHITE
10863 "tag_any_white",
10864#endif
10865#ifdef FEAT_TCL
10866# ifndef DYNAMIC_TCL
10867 "tcl",
10868# endif
10869#endif
10870#ifdef TERMINFO
10871 "terminfo",
10872#endif
10873#ifdef FEAT_TERMRESPONSE
10874 "termresponse",
10875#endif
10876#ifdef FEAT_TEXTOBJ
10877 "textobjects",
10878#endif
10879#ifdef HAVE_TGETENT
10880 "tgetent",
10881#endif
10882#ifdef FEAT_TITLE
10883 "title",
10884#endif
10885#ifdef FEAT_TOOLBAR
10886 "toolbar",
10887#endif
10888#ifdef FEAT_USR_CMDS
10889 "user-commands", /* was accidentally included in 5.4 */
10890 "user_commands",
10891#endif
10892#ifdef FEAT_VIMINFO
10893 "viminfo",
10894#endif
10895#ifdef FEAT_VERTSPLIT
10896 "vertsplit",
10897#endif
10898#ifdef FEAT_VIRTUALEDIT
10899 "virtualedit",
10900#endif
10901#ifdef FEAT_VISUAL
10902 "visual",
10903#endif
10904#ifdef FEAT_VISUALEXTRA
10905 "visualextra",
10906#endif
10907#ifdef FEAT_VREPLACE
10908 "vreplace",
10909#endif
10910#ifdef FEAT_WILDIGN
10911 "wildignore",
10912#endif
10913#ifdef FEAT_WILDMENU
10914 "wildmenu",
10915#endif
10916#ifdef FEAT_WINDOWS
10917 "windows",
10918#endif
10919#ifdef FEAT_WAK
10920 "winaltkeys",
10921#endif
10922#ifdef FEAT_WRITEBACKUP
10923 "writebackup",
10924#endif
10925#ifdef FEAT_XIM
10926 "xim",
10927#endif
10928#ifdef FEAT_XFONTSET
10929 "xfontset",
10930#endif
10931#ifdef USE_XSMP
10932 "xsmp",
10933#endif
10934#ifdef USE_XSMP_INTERACT
10935 "xsmp_interact",
10936#endif
10937#ifdef FEAT_XCLIPBOARD
10938 "xterm_clipboard",
10939#endif
10940#ifdef FEAT_XTERM_SAVE
10941 "xterm_save",
10942#endif
10943#if defined(UNIX) && defined(FEAT_X11)
10944 "X11",
10945#endif
10946 NULL
10947 };
10948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 for (i = 0; has_list[i] != NULL; ++i)
10951 if (STRICMP(name, has_list[i]) == 0)
10952 {
10953 n = TRUE;
10954 break;
10955 }
10956
10957 if (n == FALSE)
10958 {
10959 if (STRNICMP(name, "patch", 5) == 0)
10960 n = has_patch(atoi((char *)name + 5));
10961 else if (STRICMP(name, "vim_starting") == 0)
10962 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010963#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10964 else if (STRICMP(name, "balloon_multiline") == 0)
10965 n = multiline_balloon_available();
10966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967#ifdef DYNAMIC_TCL
10968 else if (STRICMP(name, "tcl") == 0)
10969 n = tcl_enabled(FALSE);
10970#endif
10971#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10972 else if (STRICMP(name, "iconv") == 0)
10973 n = iconv_enabled(FALSE);
10974#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010975#ifdef DYNAMIC_MZSCHEME
10976 else if (STRICMP(name, "mzscheme") == 0)
10977 n = mzscheme_enabled(FALSE);
10978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979#ifdef DYNAMIC_RUBY
10980 else if (STRICMP(name, "ruby") == 0)
10981 n = ruby_enabled(FALSE);
10982#endif
10983#ifdef DYNAMIC_PYTHON
10984 else if (STRICMP(name, "python") == 0)
10985 n = python_enabled(FALSE);
10986#endif
10987#ifdef DYNAMIC_PERL
10988 else if (STRICMP(name, "perl") == 0)
10989 n = perl_enabled(FALSE);
10990#endif
10991#ifdef FEAT_GUI
10992 else if (STRICMP(name, "gui_running") == 0)
10993 n = (gui.in_use || gui.starting);
10994# ifdef FEAT_GUI_W32
10995 else if (STRICMP(name, "gui_win32s") == 0)
10996 n = gui_is_win32s();
10997# endif
10998# ifdef FEAT_BROWSE
10999 else if (STRICMP(name, "browse") == 0)
11000 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11001# endif
11002#endif
11003#ifdef FEAT_SYN_HL
11004 else if (STRICMP(name, "syntax_items") == 0)
11005 n = syntax_present(curbuf);
11006#endif
11007#if defined(WIN3264)
11008 else if (STRICMP(name, "win95") == 0)
11009 n = mch_windows95();
11010#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011011#ifdef FEAT_NETBEANS_INTG
11012 else if (STRICMP(name, "netbeans_enabled") == 0)
11013 n = usingNetbeans;
11014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 }
11016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018}
11019
11020/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011021 * "has_key()" function
11022 */
11023 static void
11024f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011025 typval_T *argvars;
11026 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011027{
11028 rettv->vval.v_number = 0;
11029 if (argvars[0].v_type != VAR_DICT)
11030 {
11031 EMSG(_(e_dictreq));
11032 return;
11033 }
11034 if (argvars[0].vval.v_dict == NULL)
11035 return;
11036
11037 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011038 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011039}
11040
11041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 * "hasmapto()" function
11043 */
11044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011046 typval_T *argvars;
11047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049 char_u *name;
11050 char_u *mode;
11051 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011052 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 mode = (char_u *)"nvo";
11057 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011060 if (argvars[2].v_type != VAR_UNKNOWN)
11061 abbr = get_tv_number(&argvars[2]);
11062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063
Bram Moolenaar2c932302006-03-18 21:42:09 +000011064 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068}
11069
11070/*
11071 * "histadd()" function
11072 */
11073/*ARGSUSED*/
11074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *argvars;
11077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079#ifdef FEAT_CMDHIST
11080 int histype;
11081 char_u *str;
11082 char_u buf[NUMBUFLEN];
11083#endif
11084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 if (check_restricted() || check_secure())
11087 return;
11088#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11090 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 if (histype >= 0)
11092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 if (*str != NUL)
11095 {
11096 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011097 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 return;
11099 }
11100 }
11101#endif
11102}
11103
11104/*
11105 * "histdel()" function
11106 */
11107/*ARGSUSED*/
11108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 typval_T *argvars;
11111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
11113#ifdef FEAT_CMDHIST
11114 int n;
11115 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11119 if (str == NULL)
11120 n = 0;
11121 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011124 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 else
11129 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 get_tv_string_buf(&argvars[1], buf));
11132 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135#endif
11136}
11137
11138/*
11139 * "histget()" function
11140 */
11141/*ARGSUSED*/
11142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011144 typval_T *argvars;
11145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
11147#ifdef FEAT_CMDHIST
11148 int type;
11149 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11153 if (str == NULL)
11154 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 {
11157 type = get_histtype(str);
11158 if (argvars[1].v_type == VAR_UNKNOWN)
11159 idx = get_history_idx(type);
11160 else
11161 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11162 /* -1 on type error */
11163 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169}
11170
11171/*
11172 * "histnr()" function
11173 */
11174/*ARGSUSED*/
11175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 typval_T *argvars;
11178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179{
11180 int i;
11181
11182#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 char_u *history = get_tv_string_chk(&argvars[0]);
11184
11185 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 if (i >= HIST_CMD && i < HIST_COUNT)
11187 i = get_history_idx(i);
11188 else
11189#endif
11190 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192}
11193
11194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 * "highlightID(name)" function
11196 */
11197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011199 typval_T *argvars;
11200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203}
11204
11205/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206 * "highlight_exists()" function
11207 */
11208 static void
11209f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011210 typval_T *argvars;
11211 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011212{
11213 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11214}
11215
11216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 * "hostname()" function
11218 */
11219/*ARGSUSED*/
11220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011222 typval_T *argvars;
11223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224{
11225 char_u hostname[256];
11226
11227 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 rettv->v_type = VAR_STRING;
11229 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230}
11231
11232/*
11233 * iconv() function
11234 */
11235/*ARGSUSED*/
11236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011238 typval_T *argvars;
11239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
11241#ifdef FEAT_MBYTE
11242 char_u buf1[NUMBUFLEN];
11243 char_u buf2[NUMBUFLEN];
11244 char_u *from, *to, *str;
11245 vimconv_T vimconv;
11246#endif
11247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->v_type = VAR_STRING;
11249 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250
11251#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 str = get_tv_string(&argvars[0]);
11253 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11254 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 vimconv.vc_type = CONV_NONE;
11256 convert_setup(&vimconv, from, to);
11257
11258 /* If the encodings are equal, no conversion needed. */
11259 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
11264 convert_setup(&vimconv, NULL, NULL);
11265 vim_free(from);
11266 vim_free(to);
11267#endif
11268}
11269
11270/*
11271 * "indent()" function
11272 */
11273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 typval_T *argvars;
11276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277{
11278 linenr_T lnum;
11279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285}
11286
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011287/*
11288 * "index()" function
11289 */
11290 static void
11291f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011294{
Bram Moolenaar33570922005-01-25 22:26:29 +000011295 list_T *l;
11296 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011297 long idx = 0;
11298 int ic = FALSE;
11299
11300 rettv->vval.v_number = -1;
11301 if (argvars[0].v_type != VAR_LIST)
11302 {
11303 EMSG(_(e_listreq));
11304 return;
11305 }
11306 l = argvars[0].vval.v_list;
11307 if (l != NULL)
11308 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011309 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 int error = FALSE;
11313
Bram Moolenaar758711c2005-02-02 23:11:38 +000011314 /* Start at specified item. Use the cached index that list_find()
11315 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011317 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011318 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011319 ic = get_tv_number_chk(&argvars[3], &error);
11320 if (error)
11321 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011322 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011323
Bram Moolenaar758711c2005-02-02 23:11:38 +000011324 for ( ; item != NULL; item = item->li_next, ++idx)
11325 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011326 {
11327 rettv->vval.v_number = idx;
11328 break;
11329 }
11330 }
11331}
11332
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333static int inputsecret_flag = 0;
11334
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011335static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11336
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011338 * This function is used by f_input() and f_inputdialog() functions. The third
11339 * argument to f_input() specifies the type of completion to use at the
11340 * prompt. The third argument to f_inputdialog() specifies the value to return
11341 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 */
11343 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011344get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011345 typval_T *argvars;
11346 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011347 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 char_u *p = NULL;
11351 int c;
11352 char_u buf[NUMBUFLEN];
11353 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011355 int xp_type = EXPAND_NOTHING;
11356 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
11360#ifdef NO_CONSOLE_INPUT
11361 /* While starting up, there is no place to enter text. */
11362 if (no_console_input())
11363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 return;
11366 }
11367#endif
11368
11369 cmd_silent = FALSE; /* Want to see the prompt. */
11370 if (prompt != NULL)
11371 {
11372 /* Only the part of the message after the last NL is considered as
11373 * prompt for the command line */
11374 p = vim_strrchr(prompt, '\n');
11375 if (p == NULL)
11376 p = prompt;
11377 else
11378 {
11379 ++p;
11380 c = *p;
11381 *p = NUL;
11382 msg_start();
11383 msg_clr_eos();
11384 msg_puts_attr(prompt, echo_attr);
11385 msg_didout = FALSE;
11386 msg_starthere();
11387 *p = c;
11388 }
11389 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 if (argvars[1].v_type != VAR_UNKNOWN)
11392 {
11393 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11394 if (defstr != NULL)
11395 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011397 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011398 {
11399 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011400 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011401 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011402
Bram Moolenaar4463f292005-09-25 22:20:24 +000011403 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011404
Bram Moolenaar4463f292005-09-25 22:20:24 +000011405 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11406 if (xp_name == NULL)
11407 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011408
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011409 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011410
Bram Moolenaar4463f292005-09-25 22:20:24 +000011411 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11412 &xp_arg) == FAIL)
11413 return;
11414 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011415 }
11416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 if (defstr != NULL)
11418 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011419 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11420 xp_type, xp_arg);
11421
11422 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 /* since the user typed this, no need to wait for return */
11425 need_wait_return = FALSE;
11426 msg_didout = FALSE;
11427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 cmd_silent = cmd_silent_save;
11429}
11430
11431/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011432 * "input()" function
11433 * Also handles inputsecret() when inputsecret is set.
11434 */
11435 static void
11436f_input(argvars, rettv)
11437 typval_T *argvars;
11438 typval_T *rettv;
11439{
11440 get_user_input(argvars, rettv, FALSE);
11441}
11442
11443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 * "inputdialog()" function
11445 */
11446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
11451#if defined(FEAT_GUI_TEXTDIALOG)
11452 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11453 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11454 {
11455 char_u *message;
11456 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 message = get_tv_string_chk(&argvars[0]);
11460 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011461 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011462 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 else
11464 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 if (message != NULL && defstr != NULL
11466 && do_dialog(VIM_QUESTION, NULL, message,
11467 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 else
11470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 if (message != NULL && defstr != NULL
11472 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011473 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_string = vim_strsave(
11475 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 }
11481 else
11482#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011483 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484}
11485
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011486/*
11487 * "inputlist()" function
11488 */
11489 static void
11490f_inputlist(argvars, rettv)
11491 typval_T *argvars;
11492 typval_T *rettv;
11493{
11494 listitem_T *li;
11495 int selected;
11496 int mouse_used;
11497
11498 rettv->vval.v_number = 0;
11499#ifdef NO_CONSOLE_INPUT
11500 /* While starting up, there is no place to enter text. */
11501 if (no_console_input())
11502 return;
11503#endif
11504 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11505 {
11506 EMSG2(_(e_listarg), "inputlist()");
11507 return;
11508 }
11509
11510 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011511 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011512 lines_left = Rows; /* avoid more prompt */
11513 msg_scroll = TRUE;
11514 msg_clr_eos();
11515
11516 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11517 {
11518 msg_puts(get_tv_string(&li->li_tv));
11519 msg_putchar('\n');
11520 }
11521
11522 /* Ask for choice. */
11523 selected = prompt_for_number(&mouse_used);
11524 if (mouse_used)
11525 selected -= lines_left;
11526
11527 rettv->vval.v_number = selected;
11528}
11529
11530
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11532
11533/*
11534 * "inputrestore()" function
11535 */
11536/*ARGSUSED*/
11537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *argvars;
11540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541{
11542 if (ga_userinput.ga_len > 0)
11543 {
11544 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11546 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 }
11549 else if (p_verbose > 1)
11550 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011551 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 }
11554}
11555
11556/*
11557 * "inputsave()" function
11558 */
11559/*ARGSUSED*/
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564{
11565 /* Add an entry to the stack of typehead storage. */
11566 if (ga_grow(&ga_userinput, 1) == OK)
11567 {
11568 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11569 + ga_userinput.ga_len);
11570 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 }
11573 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575}
11576
11577/*
11578 * "inputsecret()" function
11579 */
11580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011582 typval_T *argvars;
11583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584{
11585 ++cmdline_star;
11586 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 --cmdline_star;
11589 --inputsecret_flag;
11590}
11591
11592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 * "insert()" function
11594 */
11595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599{
11600 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 listitem_T *item;
11602 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011603 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011605 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011606 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011608 else if ((l = argvars[0].vval.v_list) != NULL
11609 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610 {
11611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 before = get_tv_number_chk(&argvars[2], &error);
11613 if (error)
11614 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011615
Bram Moolenaar758711c2005-02-02 23:11:38 +000011616 if (before == l->lv_len)
11617 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618 else
11619 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011620 item = list_find(l, before);
11621 if (item == NULL)
11622 {
11623 EMSGN(_(e_listidx), before);
11624 l = NULL;
11625 }
11626 }
11627 if (l != NULL)
11628 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011629 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011630 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631 }
11632 }
11633}
11634
11635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 * "isdirectory()" function
11637 */
11638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011640 typval_T *argvars;
11641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644}
11645
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011646/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011647 * "islocked()" function
11648 */
11649 static void
11650f_islocked(argvars, rettv)
11651 typval_T *argvars;
11652 typval_T *rettv;
11653{
11654 lval_T lv;
11655 char_u *end;
11656 dictitem_T *di;
11657
11658 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011659 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11660 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011661 if (end != NULL && lv.ll_name != NULL)
11662 {
11663 if (*end != NUL)
11664 EMSG(_(e_trailing));
11665 else
11666 {
11667 if (lv.ll_tv == NULL)
11668 {
11669 if (check_changedtick(lv.ll_name))
11670 rettv->vval.v_number = 1; /* always locked */
11671 else
11672 {
11673 di = find_var(lv.ll_name, NULL);
11674 if (di != NULL)
11675 {
11676 /* Consider a variable locked when:
11677 * 1. the variable itself is locked
11678 * 2. the value of the variable is locked.
11679 * 3. the List or Dict value is locked.
11680 */
11681 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11682 || tv_islocked(&di->di_tv));
11683 }
11684 }
11685 }
11686 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011687 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011688 else if (lv.ll_newkey != NULL)
11689 EMSG2(_(e_dictkey), lv.ll_newkey);
11690 else if (lv.ll_list != NULL)
11691 /* List item. */
11692 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11693 else
11694 /* Dictionary item. */
11695 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11696 }
11697 }
11698
11699 clear_lval(&lv);
11700}
11701
Bram Moolenaar33570922005-01-25 22:26:29 +000011702static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011703
11704/*
11705 * Turn a dict into a list:
11706 * "what" == 0: list of keys
11707 * "what" == 1: list of values
11708 * "what" == 2: list of items
11709 */
11710 static void
11711dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011712 typval_T *argvars;
11713 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011714 int what;
11715{
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 list_T *l2;
11717 dictitem_T *di;
11718 hashitem_T *hi;
11719 listitem_T *li;
11720 listitem_T *li2;
11721 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011722 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011723
11724 rettv->vval.v_number = 0;
11725 if (argvars[0].v_type != VAR_DICT)
11726 {
11727 EMSG(_(e_dictreq));
11728 return;
11729 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011730 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011731 return;
11732
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011733 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011734 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011736 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011737 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011741 --todo;
11742 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011743
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011744 li = listitem_alloc();
11745 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011746 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011747 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011748
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011749 if (what == 0)
11750 {
11751 /* keys() */
11752 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011753 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011754 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11755 }
11756 else if (what == 1)
11757 {
11758 /* values() */
11759 copy_tv(&di->di_tv, &li->li_tv);
11760 }
11761 else
11762 {
11763 /* items() */
11764 l2 = list_alloc();
11765 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011766 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011767 li->li_tv.vval.v_list = l2;
11768 if (l2 == NULL)
11769 break;
11770 ++l2->lv_refcount;
11771
11772 li2 = listitem_alloc();
11773 if (li2 == NULL)
11774 break;
11775 list_append(l2, li2);
11776 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011777 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011778 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11779
11780 li2 = listitem_alloc();
11781 if (li2 == NULL)
11782 break;
11783 list_append(l2, li2);
11784 copy_tv(&di->di_tv, &li2->li_tv);
11785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011786 }
11787 }
11788}
11789
11790/*
11791 * "items(dict)" function
11792 */
11793 static void
11794f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011797{
11798 dict_list(argvars, rettv, 2);
11799}
11800
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011802 * "join()" function
11803 */
11804 static void
11805f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *argvars;
11807 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011808{
11809 garray_T ga;
11810 char_u *sep;
11811
11812 rettv->vval.v_number = 0;
11813 if (argvars[0].v_type != VAR_LIST)
11814 {
11815 EMSG(_(e_listreq));
11816 return;
11817 }
11818 if (argvars[0].vval.v_list == NULL)
11819 return;
11820 if (argvars[1].v_type == VAR_UNKNOWN)
11821 sep = (char_u *)" ";
11822 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011824
11825 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826
11827 if (sep != NULL)
11828 {
11829 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011830 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 ga_append(&ga, NUL);
11832 rettv->vval.v_string = (char_u *)ga.ga_data;
11833 }
11834 else
11835 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011836}
11837
11838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011839 * "keys()" function
11840 */
11841 static void
11842f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011843 typval_T *argvars;
11844 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011845{
11846 dict_list(argvars, rettv, 0);
11847}
11848
11849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * "last_buffer_nr()" function.
11851 */
11852/*ARGSUSED*/
11853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *argvars;
11856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857{
11858 int n = 0;
11859 buf_T *buf;
11860
11861 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11862 if (n < buf->b_fnum)
11863 n = buf->b_fnum;
11864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011866}
11867
11868/*
11869 * "len()" function
11870 */
11871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011873 typval_T *argvars;
11874 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011875{
11876 switch (argvars[0].v_type)
11877 {
11878 case VAR_STRING:
11879 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880 rettv->vval.v_number = (varnumber_T)STRLEN(
11881 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011882 break;
11883 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011885 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011886 case VAR_DICT:
11887 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11888 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011889 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011890 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011891 break;
11892 }
11893}
11894
Bram Moolenaar33570922005-01-25 22:26:29 +000011895static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011896
11897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011899 typval_T *argvars;
11900 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011901 int type;
11902{
11903#ifdef FEAT_LIBCALL
11904 char_u *string_in;
11905 char_u **string_result;
11906 int nr_result;
11907#endif
11908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011910 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011914
11915 if (check_restricted() || check_secure())
11916 return;
11917
11918#ifdef FEAT_LIBCALL
11919 /* The first two args must be strings, otherwise its meaningless */
11920 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11921 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011922 string_in = NULL;
11923 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011924 string_in = argvars[2].vval.v_string;
11925 if (type == VAR_NUMBER)
11926 string_result = NULL;
11927 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011929 if (mch_libcall(argvars[0].vval.v_string,
11930 argvars[1].vval.v_string,
11931 string_in,
11932 argvars[2].vval.v_number,
11933 string_result,
11934 &nr_result) == OK
11935 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011937 }
11938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939}
11940
11941/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011942 * "libcall()" function
11943 */
11944 static void
11945f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 typval_T *argvars;
11947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948{
11949 libcall_common(argvars, rettv, VAR_STRING);
11950}
11951
11952/*
11953 * "libcallnr()" function
11954 */
11955 static void
11956f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *argvars;
11958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011959{
11960 libcall_common(argvars, rettv, VAR_NUMBER);
11961}
11962
11963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 * "line(string)" function
11965 */
11966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 typval_T *argvars;
11969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970{
11971 linenr_T lnum = 0;
11972 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011973 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011975 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 if (fp != NULL)
11977 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979}
11980
11981/*
11982 * "line2byte(lnum)" function
11983 */
11984/*ARGSUSED*/
11985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011987 typval_T *argvars;
11988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989{
11990#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992#else
11993 linenr_T lnum;
11994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12000 if (rettv->vval.v_number >= 0)
12001 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002#endif
12003}
12004
12005/*
12006 * "lispindent(lnum)" function
12007 */
12008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 typval_T *argvars;
12011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
12013#ifdef FEAT_LISP
12014 pos_T pos;
12015 linenr_T lnum;
12016
12017 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12020 {
12021 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 curwin->w_cursor = pos;
12024 }
12025 else
12026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028}
12029
12030/*
12031 * "localtime()" function
12032 */
12033/*ARGSUSED*/
12034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035f_localtime(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040}
12041
Bram Moolenaar33570922005-01-25 22:26:29 +000012042static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043
12044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 typval_T *argvars;
12047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 int exact;
12049{
12050 char_u *keys;
12051 char_u *which;
12052 char_u buf[NUMBUFLEN];
12053 char_u *keys_buf = NULL;
12054 char_u *rhs;
12055 int mode;
12056 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012057 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058
12059 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->v_type = VAR_STRING;
12061 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 if (*keys == NUL)
12065 return;
12066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012067 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012070 if (argvars[2].v_type != VAR_UNKNOWN)
12071 abbr = get_tv_number(&argvars[2]);
12072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 else
12074 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012075 if (which == NULL)
12076 return;
12077
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 mode = get_map_mode(&which, 0);
12079
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012080 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012081 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 vim_free(keys_buf);
12083 if (rhs != NULL)
12084 {
12085 ga_init(&ga);
12086 ga.ga_itemsize = 1;
12087 ga.ga_growsize = 40;
12088
12089 while (*rhs != NUL)
12090 ga_concat(&ga, str2special(&rhs, FALSE));
12091
12092 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 }
12095}
12096
12097/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012098 * "map()" function
12099 */
12100 static void
12101f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 typval_T *argvars;
12103 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012104{
12105 filter_map(argvars, rettv, TRUE);
12106}
12107
12108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012109 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 */
12111 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012112f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 typval_T *argvars;
12114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117}
12118
12119/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012120 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 */
12122 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012123f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128}
12129
Bram Moolenaar33570922005-01-25 22:26:29 +000012130static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131
12132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 typval_T *argvars;
12135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 int type;
12137{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012138 char_u *str = NULL;
12139 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 char_u *pat;
12141 regmatch_T regmatch;
12142 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012143 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 char_u *save_cpo;
12145 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012146 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012147 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012148 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012149 list_T *l = NULL;
12150 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012151 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012152 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153
12154 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12155 save_cpo = p_cpo;
12156 p_cpo = (char_u *)"";
12157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012158 rettv->vval.v_number = -1;
12159 if (type == 3)
12160 {
12161 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012162 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012163 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012164 }
12165 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->v_type = VAR_STRING;
12168 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012171 if (argvars[0].v_type == VAR_LIST)
12172 {
12173 if ((l = argvars[0].vval.v_list) == NULL)
12174 goto theend;
12175 li = l->lv_first;
12176 }
12177 else
12178 expr = str = get_tv_string(&argvars[0]);
12179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12181 if (pat == NULL)
12182 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012183
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 int error = FALSE;
12187
12188 start = get_tv_number_chk(&argvars[2], &error);
12189 if (error)
12190 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012191 if (l != NULL)
12192 {
12193 li = list_find(l, start);
12194 if (li == NULL)
12195 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012196 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012197 }
12198 else
12199 {
12200 if (start < 0)
12201 start = 0;
12202 if (start > (long)STRLEN(str))
12203 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012204 /* When "count" argument is there ignore matches before "start",
12205 * otherwise skip part of the string. Differs when pattern is "^"
12206 * or "\<". */
12207 if (argvars[3].v_type != VAR_UNKNOWN)
12208 startcol = start;
12209 else
12210 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012211 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012212
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012213 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012214 nth = get_tv_number_chk(&argvars[3], &error);
12215 if (error)
12216 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 }
12218
12219 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12220 if (regmatch.regprog != NULL)
12221 {
12222 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012223
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012224 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012225 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012226 if (l != NULL)
12227 {
12228 if (li == NULL)
12229 {
12230 match = FALSE;
12231 break;
12232 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012233 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012234 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012235 if (str == NULL)
12236 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012237 }
12238
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012239 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012240
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012241 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012242 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012243 if (l == NULL && !match)
12244 break;
12245
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012246 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012247 if (l != NULL)
12248 {
12249 li = li->li_next;
12250 ++idx;
12251 }
12252 else
12253 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012254#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012255 startcol = (colnr_T)(regmatch.startp[0]
12256 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012257#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012258 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012259#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012260 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012261 }
12262
12263 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012265 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012267 int i;
12268
12269 /* return list with matched string and submatches */
12270 for (i = 0; i < NSUBEXP; ++i)
12271 {
12272 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012273 {
12274 if (list_append_string(rettv->vval.v_list,
12275 (char_u *)"", 0) == FAIL)
12276 break;
12277 }
12278 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012279 regmatch.startp[i],
12280 (int)(regmatch.endp[i] - regmatch.startp[i]))
12281 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012282 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012283 }
12284 }
12285 else if (type == 2)
12286 {
12287 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012288 if (l != NULL)
12289 copy_tv(&li->li_tv, rettv);
12290 else
12291 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012293 }
12294 else if (l != NULL)
12295 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 else
12297 {
12298 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 (varnumber_T)(regmatch.startp[0] - str);
12301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012304 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 }
12306 }
12307 vim_free(regmatch.regprog);
12308 }
12309
12310theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012311 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 p_cpo = save_cpo;
12313}
12314
12315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012316 * "match()" function
12317 */
12318 static void
12319f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012320 typval_T *argvars;
12321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012322{
12323 find_some_match(argvars, rettv, 1);
12324}
12325
12326/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012327 * "matcharg()" function
12328 */
12329 static void
12330f_matcharg(argvars, rettv)
12331 typval_T *argvars;
12332 typval_T *rettv;
12333{
12334 if (rettv_list_alloc(rettv) == OK)
12335 {
12336#ifdef FEAT_SEARCH_EXTRA
12337 int mi = get_tv_number(&argvars[0]);
12338
12339 if (mi >= 1 && mi <= 3)
12340 {
12341 list_append_string(rettv->vval.v_list,
12342 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12343 list_append_string(rettv->vval.v_list,
12344 curwin->w_match_pat[mi - 1], -1);
12345 }
12346#endif
12347 }
12348}
12349
12350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012351 * "matchend()" function
12352 */
12353 static void
12354f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *argvars;
12356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357{
12358 find_some_match(argvars, rettv, 0);
12359}
12360
12361/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012362 * "matchlist()" function
12363 */
12364 static void
12365f_matchlist(argvars, rettv)
12366 typval_T *argvars;
12367 typval_T *rettv;
12368{
12369 find_some_match(argvars, rettv, 3);
12370}
12371
12372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373 * "matchstr()" function
12374 */
12375 static void
12376f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 typval_T *argvars;
12378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379{
12380 find_some_match(argvars, rettv, 2);
12381}
12382
Bram Moolenaar33570922005-01-25 22:26:29 +000012383static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012384
12385 static void
12386max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012387 typval_T *argvars;
12388 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012389 int domax;
12390{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012391 long n = 0;
12392 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012393 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012394
12395 if (argvars[0].v_type == VAR_LIST)
12396 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 list_T *l;
12398 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012400 l = argvars[0].vval.v_list;
12401 if (l != NULL)
12402 {
12403 li = l->lv_first;
12404 if (li != NULL)
12405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012406 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012407 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012408 {
12409 li = li->li_next;
12410 if (li == NULL)
12411 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012413 if (domax ? i > n : i < n)
12414 n = i;
12415 }
12416 }
12417 }
12418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012419 else if (argvars[0].v_type == VAR_DICT)
12420 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012422 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012423 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012424 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012425
12426 d = argvars[0].vval.v_dict;
12427 if (d != NULL)
12428 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012429 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012432 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012434 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012436 if (first)
12437 {
12438 n = i;
12439 first = FALSE;
12440 }
12441 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012442 n = i;
12443 }
12444 }
12445 }
12446 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012447 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012448 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012449 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012450}
12451
12452/*
12453 * "max()" function
12454 */
12455 static void
12456f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012457 typval_T *argvars;
12458 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012459{
12460 max_min(argvars, rettv, TRUE);
12461}
12462
12463/*
12464 * "min()" function
12465 */
12466 static void
12467f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012468 typval_T *argvars;
12469 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012470{
12471 max_min(argvars, rettv, FALSE);
12472}
12473
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012474static int mkdir_recurse __ARGS((char_u *dir, int prot));
12475
12476/*
12477 * Create the directory in which "dir" is located, and higher levels when
12478 * needed.
12479 */
12480 static int
12481mkdir_recurse(dir, prot)
12482 char_u *dir;
12483 int prot;
12484{
12485 char_u *p;
12486 char_u *updir;
12487 int r = FAIL;
12488
12489 /* Get end of directory name in "dir".
12490 * We're done when it's "/" or "c:/". */
12491 p = gettail_sep(dir);
12492 if (p <= get_past_head(dir))
12493 return OK;
12494
12495 /* If the directory exists we're done. Otherwise: create it.*/
12496 updir = vim_strnsave(dir, (int)(p - dir));
12497 if (updir == NULL)
12498 return FAIL;
12499 if (mch_isdir(updir))
12500 r = OK;
12501 else if (mkdir_recurse(updir, prot) == OK)
12502 r = vim_mkdir_emsg(updir, prot);
12503 vim_free(updir);
12504 return r;
12505}
12506
12507#ifdef vim_mkdir
12508/*
12509 * "mkdir()" function
12510 */
12511 static void
12512f_mkdir(argvars, rettv)
12513 typval_T *argvars;
12514 typval_T *rettv;
12515{
12516 char_u *dir;
12517 char_u buf[NUMBUFLEN];
12518 int prot = 0755;
12519
12520 rettv->vval.v_number = FAIL;
12521 if (check_restricted() || check_secure())
12522 return;
12523
12524 dir = get_tv_string_buf(&argvars[0], buf);
12525 if (argvars[1].v_type != VAR_UNKNOWN)
12526 {
12527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012528 prot = get_tv_number_chk(&argvars[2], NULL);
12529 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012530 mkdir_recurse(dir, prot);
12531 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012532 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012533}
12534#endif
12535
Bram Moolenaar0d660222005-01-07 21:51:51 +000012536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 * "mode()" function
12538 */
12539/*ARGSUSED*/
12540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012541f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 typval_T *argvars;
12543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544{
12545 char_u buf[2];
12546
12547#ifdef FEAT_VISUAL
12548 if (VIsual_active)
12549 {
12550 if (VIsual_select)
12551 buf[0] = VIsual_mode + 's' - 'v';
12552 else
12553 buf[0] = VIsual_mode;
12554 }
12555 else
12556#endif
12557 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12558 buf[0] = 'r';
12559 else if (State & INSERT)
12560 {
12561 if (State & REPLACE_FLAG)
12562 buf[0] = 'R';
12563 else
12564 buf[0] = 'i';
12565 }
12566 else if (State & CMDLINE)
12567 buf[0] = 'c';
12568 else
12569 buf[0] = 'n';
12570
12571 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->vval.v_string = vim_strsave(buf);
12573 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574}
12575
12576/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577 * "nextnonblank()" function
12578 */
12579 static void
12580f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012581 typval_T *argvars;
12582 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012583{
12584 linenr_T lnum;
12585
12586 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012588 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012589 {
12590 lnum = 0;
12591 break;
12592 }
12593 if (*skipwhite(ml_get(lnum)) != NUL)
12594 break;
12595 }
12596 rettv->vval.v_number = lnum;
12597}
12598
12599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 * "nr2char()" function
12601 */
12602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012604 typval_T *argvars;
12605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606{
12607 char_u buf[NUMBUFLEN];
12608
12609#ifdef FEAT_MBYTE
12610 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012611 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 else
12613#endif
12614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 buf[1] = NUL;
12617 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012618 rettv->v_type = VAR_STRING;
12619 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012620}
12621
12622/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012623 * "pathshorten()" function
12624 */
12625 static void
12626f_pathshorten(argvars, rettv)
12627 typval_T *argvars;
12628 typval_T *rettv;
12629{
12630 char_u *p;
12631
12632 rettv->v_type = VAR_STRING;
12633 p = get_tv_string_chk(&argvars[0]);
12634 if (p == NULL)
12635 rettv->vval.v_string = NULL;
12636 else
12637 {
12638 p = vim_strsave(p);
12639 rettv->vval.v_string = p;
12640 if (p != NULL)
12641 shorten_dir(p);
12642 }
12643}
12644
12645/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646 * "prevnonblank()" function
12647 */
12648 static void
12649f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012650 typval_T *argvars;
12651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652{
12653 linenr_T lnum;
12654
12655 lnum = get_tv_lnum(argvars);
12656 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12657 lnum = 0;
12658 else
12659 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12660 --lnum;
12661 rettv->vval.v_number = lnum;
12662}
12663
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012664#ifdef HAVE_STDARG_H
12665/* This dummy va_list is here because:
12666 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12667 * - locally in the function results in a "used before set" warning
12668 * - using va_start() to initialize it gives "function with fixed args" error */
12669static va_list ap;
12670#endif
12671
Bram Moolenaar8c711452005-01-14 21:53:12 +000012672/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012673 * "printf()" function
12674 */
12675 static void
12676f_printf(argvars, rettv)
12677 typval_T *argvars;
12678 typval_T *rettv;
12679{
12680 rettv->v_type = VAR_STRING;
12681 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012682#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012683 {
12684 char_u buf[NUMBUFLEN];
12685 int len;
12686 char_u *s;
12687 int saved_did_emsg = did_emsg;
12688 char *fmt;
12689
12690 /* Get the required length, allocate the buffer and do it for real. */
12691 did_emsg = FALSE;
12692 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012693 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012694 if (!did_emsg)
12695 {
12696 s = alloc(len + 1);
12697 if (s != NULL)
12698 {
12699 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012700 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012701 }
12702 }
12703 did_emsg |= saved_did_emsg;
12704 }
12705#endif
12706}
12707
12708/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012709 * "pumvisible()" function
12710 */
12711/*ARGSUSED*/
12712 static void
12713f_pumvisible(argvars, rettv)
12714 typval_T *argvars;
12715 typval_T *rettv;
12716{
12717 rettv->vval.v_number = 0;
12718#ifdef FEAT_INS_EXPAND
12719 if (pum_visible())
12720 rettv->vval.v_number = 1;
12721#endif
12722}
12723
12724/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012725 * "range()" function
12726 */
12727 static void
12728f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012729 typval_T *argvars;
12730 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012731{
12732 long start;
12733 long end;
12734 long stride = 1;
12735 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012736 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012739 if (argvars[1].v_type == VAR_UNKNOWN)
12740 {
12741 end = start - 1;
12742 start = 0;
12743 }
12744 else
12745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012747 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012749 }
12750
12751 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 if (error)
12753 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012754 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012755 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012756 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012757 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012758 else
12759 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012760 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012761 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012762 if (list_append_number(rettv->vval.v_list,
12763 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012764 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012765 }
12766}
12767
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012768/*
12769 * "readfile()" function
12770 */
12771 static void
12772f_readfile(argvars, rettv)
12773 typval_T *argvars;
12774 typval_T *rettv;
12775{
12776 int binary = FALSE;
12777 char_u *fname;
12778 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012779 listitem_T *li;
12780#define FREAD_SIZE 200 /* optimized for text lines */
12781 char_u buf[FREAD_SIZE];
12782 int readlen; /* size of last fread() */
12783 int buflen; /* nr of valid chars in buf[] */
12784 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12785 int tolist; /* first byte in buf[] still to be put in list */
12786 int chop; /* how many CR to chop off */
12787 char_u *prev = NULL; /* previously read bytes, if any */
12788 int prevlen = 0; /* length of "prev" if not NULL */
12789 char_u *s;
12790 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012791 long maxline = MAXLNUM;
12792 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012793
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012794 if (argvars[1].v_type != VAR_UNKNOWN)
12795 {
12796 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12797 binary = TRUE;
12798 if (argvars[2].v_type != VAR_UNKNOWN)
12799 maxline = get_tv_number(&argvars[2]);
12800 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012801
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012802 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012803 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012804
12805 /* Always open the file in binary mode, library functions have a mind of
12806 * their own about CR-LF conversion. */
12807 fname = get_tv_string(&argvars[0]);
12808 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12809 {
12810 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12811 return;
12812 }
12813
12814 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012815 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012816 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012817 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012818 buflen = filtd + readlen;
12819 tolist = 0;
12820 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12821 {
12822 if (buf[filtd] == '\n' || readlen <= 0)
12823 {
12824 /* Only when in binary mode add an empty list item when the
12825 * last line ends in a '\n'. */
12826 if (!binary && readlen == 0 && filtd == 0)
12827 break;
12828
12829 /* Found end-of-line or end-of-file: add a text line to the
12830 * list. */
12831 chop = 0;
12832 if (!binary)
12833 while (filtd - chop - 1 >= tolist
12834 && buf[filtd - chop - 1] == '\r')
12835 ++chop;
12836 len = filtd - tolist - chop;
12837 if (prev == NULL)
12838 s = vim_strnsave(buf + tolist, len);
12839 else
12840 {
12841 s = alloc((unsigned)(prevlen + len + 1));
12842 if (s != NULL)
12843 {
12844 mch_memmove(s, prev, prevlen);
12845 vim_free(prev);
12846 prev = NULL;
12847 mch_memmove(s + prevlen, buf + tolist, len);
12848 s[prevlen + len] = NUL;
12849 }
12850 }
12851 tolist = filtd + 1;
12852
12853 li = listitem_alloc();
12854 if (li == NULL)
12855 {
12856 vim_free(s);
12857 break;
12858 }
12859 li->li_tv.v_type = VAR_STRING;
12860 li->li_tv.v_lock = 0;
12861 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012862 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012863
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012864 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012865 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012866 if (readlen <= 0)
12867 break;
12868 }
12869 else if (buf[filtd] == NUL)
12870 buf[filtd] = '\n';
12871 }
12872 if (readlen <= 0)
12873 break;
12874
12875 if (tolist == 0)
12876 {
12877 /* "buf" is full, need to move text to an allocated buffer */
12878 if (prev == NULL)
12879 {
12880 prev = vim_strnsave(buf, buflen);
12881 prevlen = buflen;
12882 }
12883 else
12884 {
12885 s = alloc((unsigned)(prevlen + buflen));
12886 if (s != NULL)
12887 {
12888 mch_memmove(s, prev, prevlen);
12889 mch_memmove(s + prevlen, buf, buflen);
12890 vim_free(prev);
12891 prev = s;
12892 prevlen += buflen;
12893 }
12894 }
12895 filtd = 0;
12896 }
12897 else
12898 {
12899 mch_memmove(buf, buf + tolist, buflen - tolist);
12900 filtd -= tolist;
12901 }
12902 }
12903
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012904 /*
12905 * For a negative line count use only the lines at the end of the file,
12906 * free the rest.
12907 */
12908 if (maxline < 0)
12909 while (cnt > -maxline)
12910 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012911 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012912 --cnt;
12913 }
12914
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012915 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012916 fclose(fd);
12917}
12918
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012919#if defined(FEAT_RELTIME)
12920static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12921
12922/*
12923 * Convert a List to proftime_T.
12924 * Return FAIL when there is something wrong.
12925 */
12926 static int
12927list2proftime(arg, tm)
12928 typval_T *arg;
12929 proftime_T *tm;
12930{
12931 long n1, n2;
12932 int error = FALSE;
12933
12934 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12935 || arg->vval.v_list->lv_len != 2)
12936 return FAIL;
12937 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12938 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12939# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012940 tm->HighPart = n1;
12941 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012942# else
12943 tm->tv_sec = n1;
12944 tm->tv_usec = n2;
12945# endif
12946 return error ? FAIL : OK;
12947}
12948#endif /* FEAT_RELTIME */
12949
12950/*
12951 * "reltime()" function
12952 */
12953 static void
12954f_reltime(argvars, rettv)
12955 typval_T *argvars;
12956 typval_T *rettv;
12957{
12958#ifdef FEAT_RELTIME
12959 proftime_T res;
12960 proftime_T start;
12961
12962 if (argvars[0].v_type == VAR_UNKNOWN)
12963 {
12964 /* No arguments: get current time. */
12965 profile_start(&res);
12966 }
12967 else if (argvars[1].v_type == VAR_UNKNOWN)
12968 {
12969 if (list2proftime(&argvars[0], &res) == FAIL)
12970 return;
12971 profile_end(&res);
12972 }
12973 else
12974 {
12975 /* Two arguments: compute the difference. */
12976 if (list2proftime(&argvars[0], &start) == FAIL
12977 || list2proftime(&argvars[1], &res) == FAIL)
12978 return;
12979 profile_sub(&res, &start);
12980 }
12981
12982 if (rettv_list_alloc(rettv) == OK)
12983 {
12984 long n1, n2;
12985
12986# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012987 n1 = res.HighPart;
12988 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012989# else
12990 n1 = res.tv_sec;
12991 n2 = res.tv_usec;
12992# endif
12993 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12994 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12995 }
12996#endif
12997}
12998
12999/*
13000 * "reltimestr()" function
13001 */
13002 static void
13003f_reltimestr(argvars, rettv)
13004 typval_T *argvars;
13005 typval_T *rettv;
13006{
13007#ifdef FEAT_RELTIME
13008 proftime_T tm;
13009#endif
13010
13011 rettv->v_type = VAR_STRING;
13012 rettv->vval.v_string = NULL;
13013#ifdef FEAT_RELTIME
13014 if (list2proftime(&argvars[0], &tm) == OK)
13015 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13016#endif
13017}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013018
Bram Moolenaar0d660222005-01-07 21:51:51 +000013019#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13020static void make_connection __ARGS((void));
13021static int check_connection __ARGS((void));
13022
13023 static void
13024make_connection()
13025{
13026 if (X_DISPLAY == NULL
13027# ifdef FEAT_GUI
13028 && !gui.in_use
13029# endif
13030 )
13031 {
13032 x_force_connect = TRUE;
13033 setup_term_clip();
13034 x_force_connect = FALSE;
13035 }
13036}
13037
13038 static int
13039check_connection()
13040{
13041 make_connection();
13042 if (X_DISPLAY == NULL)
13043 {
13044 EMSG(_("E240: No connection to Vim server"));
13045 return FAIL;
13046 }
13047 return OK;
13048}
13049#endif
13050
13051#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013052static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013053
13054 static void
13055remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 typval_T *argvars;
13057 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013058 int expr;
13059{
13060 char_u *server_name;
13061 char_u *keys;
13062 char_u *r = NULL;
13063 char_u buf[NUMBUFLEN];
13064# ifdef WIN32
13065 HWND w;
13066# else
13067 Window w;
13068# endif
13069
13070 if (check_restricted() || check_secure())
13071 return;
13072
13073# ifdef FEAT_X11
13074 if (check_connection() == FAIL)
13075 return;
13076# endif
13077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013078 server_name = get_tv_string_chk(&argvars[0]);
13079 if (server_name == NULL)
13080 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013081 keys = get_tv_string_buf(&argvars[1], buf);
13082# ifdef WIN32
13083 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13084# else
13085 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13086 < 0)
13087# endif
13088 {
13089 if (r != NULL)
13090 EMSG(r); /* sending worked but evaluation failed */
13091 else
13092 EMSG2(_("E241: Unable to send to %s"), server_name);
13093 return;
13094 }
13095
13096 rettv->vval.v_string = r;
13097
13098 if (argvars[2].v_type != VAR_UNKNOWN)
13099 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013100 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013101 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013103
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013104 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013105 v.di_tv.v_type = VAR_STRING;
13106 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013107 idvar = get_tv_string_chk(&argvars[2]);
13108 if (idvar != NULL)
13109 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013111 }
13112}
13113#endif
13114
13115/*
13116 * "remote_expr()" function
13117 */
13118/*ARGSUSED*/
13119 static void
13120f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013121 typval_T *argvars;
13122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013123{
13124 rettv->v_type = VAR_STRING;
13125 rettv->vval.v_string = NULL;
13126#ifdef FEAT_CLIENTSERVER
13127 remote_common(argvars, rettv, TRUE);
13128#endif
13129}
13130
13131/*
13132 * "remote_foreground()" function
13133 */
13134/*ARGSUSED*/
13135 static void
13136f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013137 typval_T *argvars;
13138 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139{
13140 rettv->vval.v_number = 0;
13141#ifdef FEAT_CLIENTSERVER
13142# ifdef WIN32
13143 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013144 {
13145 char_u *server_name = get_tv_string_chk(&argvars[0]);
13146
13147 if (server_name != NULL)
13148 serverForeground(server_name);
13149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013150# else
13151 /* Send a foreground() expression to the server. */
13152 argvars[1].v_type = VAR_STRING;
13153 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13154 argvars[2].v_type = VAR_UNKNOWN;
13155 remote_common(argvars, rettv, TRUE);
13156 vim_free(argvars[1].vval.v_string);
13157# endif
13158#endif
13159}
13160
13161/*ARGSUSED*/
13162 static void
13163f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013164 typval_T *argvars;
13165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013166{
13167#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013168 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013169 char_u *s = NULL;
13170# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013171 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013172# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013173 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013174
13175 if (check_restricted() || check_secure())
13176 {
13177 rettv->vval.v_number = -1;
13178 return;
13179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 serverid = get_tv_string_chk(&argvars[0]);
13181 if (serverid == NULL)
13182 {
13183 rettv->vval.v_number = -1;
13184 return; /* type error; errmsg already given */
13185 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013186# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013187 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188 if (n == 0)
13189 rettv->vval.v_number = -1;
13190 else
13191 {
13192 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13193 rettv->vval.v_number = (s != NULL);
13194 }
13195# else
13196 rettv->vval.v_number = 0;
13197 if (check_connection() == FAIL)
13198 return;
13199
13200 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013202# endif
13203
13204 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 char_u *retvar;
13207
Bram Moolenaar33570922005-01-25 22:26:29 +000013208 v.di_tv.v_type = VAR_STRING;
13209 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013210 retvar = get_tv_string_chk(&argvars[1]);
13211 if (retvar != NULL)
13212 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013213 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013214 }
13215#else
13216 rettv->vval.v_number = -1;
13217#endif
13218}
13219
13220/*ARGSUSED*/
13221 static void
13222f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 typval_T *argvars;
13224 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225{
13226 char_u *r = NULL;
13227
13228#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 char_u *serverid = get_tv_string_chk(&argvars[0]);
13230
13231 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232 {
13233# ifdef WIN32
13234 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013235 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013236
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013237 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 if (n != 0)
13239 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13240 if (r == NULL)
13241# else
13242 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013243 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244# endif
13245 EMSG(_("E277: Unable to read a server reply"));
13246 }
13247#endif
13248 rettv->v_type = VAR_STRING;
13249 rettv->vval.v_string = r;
13250}
13251
13252/*
13253 * "remote_send()" function
13254 */
13255/*ARGSUSED*/
13256 static void
13257f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *argvars;
13259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260{
13261 rettv->v_type = VAR_STRING;
13262 rettv->vval.v_string = NULL;
13263#ifdef FEAT_CLIENTSERVER
13264 remote_common(argvars, rettv, FALSE);
13265#endif
13266}
13267
13268/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013269 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013270 */
13271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013275{
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 list_T *l;
13277 listitem_T *item, *item2;
13278 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013279 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013280 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013281 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013282 dict_T *d;
13283 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013284
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013285 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013286 if (argvars[0].v_type == VAR_DICT)
13287 {
13288 if (argvars[2].v_type != VAR_UNKNOWN)
13289 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013290 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013291 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013293 key = get_tv_string_chk(&argvars[1]);
13294 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 di = dict_find(d, key, -1);
13297 if (di == NULL)
13298 EMSG2(_(e_dictkey), key);
13299 else
13300 {
13301 *rettv = di->di_tv;
13302 init_tv(&di->di_tv);
13303 dictitem_remove(d, di);
13304 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013306 }
13307 }
13308 else if (argvars[0].v_type != VAR_LIST)
13309 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013310 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013311 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 int error = FALSE;
13314
13315 idx = get_tv_number_chk(&argvars[1], &error);
13316 if (error)
13317 ; /* type error: do nothing, errmsg already given */
13318 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013319 EMSGN(_(e_listidx), idx);
13320 else
13321 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013322 if (argvars[2].v_type == VAR_UNKNOWN)
13323 {
13324 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013325 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013326 *rettv = item->li_tv;
13327 vim_free(item);
13328 }
13329 else
13330 {
13331 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 end = get_tv_number_chk(&argvars[2], &error);
13333 if (error)
13334 ; /* type error: do nothing */
13335 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013336 EMSGN(_(e_listidx), end);
13337 else
13338 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013339 int cnt = 0;
13340
13341 for (li = item; li != NULL; li = li->li_next)
13342 {
13343 ++cnt;
13344 if (li == item2)
13345 break;
13346 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013347 if (li == NULL) /* didn't find "item2" after "item" */
13348 EMSG(_(e_invrange));
13349 else
13350 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013351 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013352 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013353 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013354 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013355 l->lv_first = item;
13356 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013357 item->li_prev = NULL;
13358 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013359 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013360 }
13361 }
13362 }
13363 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013364 }
13365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366}
13367
13368/*
13369 * "rename({from}, {to})" function
13370 */
13371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013372f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 typval_T *argvars;
13374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375{
13376 char_u buf[NUMBUFLEN];
13377
13378 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13382 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383}
13384
13385/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013386 * "repeat()" function
13387 */
13388/*ARGSUSED*/
13389 static void
13390f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013391 typval_T *argvars;
13392 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013393{
13394 char_u *p;
13395 int n;
13396 int slen;
13397 int len;
13398 char_u *r;
13399 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013400
13401 n = get_tv_number(&argvars[1]);
13402 if (argvars[0].v_type == VAR_LIST)
13403 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013404 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013405 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013406 if (list_extend(rettv->vval.v_list,
13407 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013408 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013409 }
13410 else
13411 {
13412 p = get_tv_string(&argvars[0]);
13413 rettv->v_type = VAR_STRING;
13414 rettv->vval.v_string = NULL;
13415
13416 slen = (int)STRLEN(p);
13417 len = slen * n;
13418 if (len <= 0)
13419 return;
13420
13421 r = alloc(len + 1);
13422 if (r != NULL)
13423 {
13424 for (i = 0; i < n; i++)
13425 mch_memmove(r + i * slen, p, (size_t)slen);
13426 r[len] = NUL;
13427 }
13428
13429 rettv->vval.v_string = r;
13430 }
13431}
13432
13433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 * "resolve()" function
13435 */
13436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013437f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013438 typval_T *argvars;
13439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440{
13441 char_u *p;
13442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444#ifdef FEAT_SHORTCUT
13445 {
13446 char_u *v = NULL;
13447
13448 v = mch_resolve_shortcut(p);
13449 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 }
13454#else
13455# ifdef HAVE_READLINK
13456 {
13457 char_u buf[MAXPATHL + 1];
13458 char_u *cpy;
13459 int len;
13460 char_u *remain = NULL;
13461 char_u *q;
13462 int is_relative_to_current = FALSE;
13463 int has_trailing_pathsep = FALSE;
13464 int limit = 100;
13465
13466 p = vim_strsave(p);
13467
13468 if (p[0] == '.' && (vim_ispathsep(p[1])
13469 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13470 is_relative_to_current = TRUE;
13471
13472 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013473 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 has_trailing_pathsep = TRUE;
13475
13476 q = getnextcomp(p);
13477 if (*q != NUL)
13478 {
13479 /* Separate the first path component in "p", and keep the
13480 * remainder (beginning with the path separator). */
13481 remain = vim_strsave(q - 1);
13482 q[-1] = NUL;
13483 }
13484
13485 for (;;)
13486 {
13487 for (;;)
13488 {
13489 len = readlink((char *)p, (char *)buf, MAXPATHL);
13490 if (len <= 0)
13491 break;
13492 buf[len] = NUL;
13493
13494 if (limit-- == 0)
13495 {
13496 vim_free(p);
13497 vim_free(remain);
13498 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 goto fail;
13501 }
13502
13503 /* Ensure that the result will have a trailing path separator
13504 * if the argument has one. */
13505 if (remain == NULL && has_trailing_pathsep)
13506 add_pathsep(buf);
13507
13508 /* Separate the first path component in the link value and
13509 * concatenate the remainders. */
13510 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13511 if (*q != NUL)
13512 {
13513 if (remain == NULL)
13514 remain = vim_strsave(q - 1);
13515 else
13516 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013517 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 if (cpy != NULL)
13519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 vim_free(remain);
13521 remain = cpy;
13522 }
13523 }
13524 q[-1] = NUL;
13525 }
13526
13527 q = gettail(p);
13528 if (q > p && *q == NUL)
13529 {
13530 /* Ignore trailing path separator. */
13531 q[-1] = NUL;
13532 q = gettail(p);
13533 }
13534 if (q > p && !mch_isFullName(buf))
13535 {
13536 /* symlink is relative to directory of argument */
13537 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13538 if (cpy != NULL)
13539 {
13540 STRCPY(cpy, p);
13541 STRCPY(gettail(cpy), buf);
13542 vim_free(p);
13543 p = cpy;
13544 }
13545 }
13546 else
13547 {
13548 vim_free(p);
13549 p = vim_strsave(buf);
13550 }
13551 }
13552
13553 if (remain == NULL)
13554 break;
13555
13556 /* Append the first path component of "remain" to "p". */
13557 q = getnextcomp(remain + 1);
13558 len = q - remain - (*q != NUL);
13559 cpy = vim_strnsave(p, STRLEN(p) + len);
13560 if (cpy != NULL)
13561 {
13562 STRNCAT(cpy, remain, len);
13563 vim_free(p);
13564 p = cpy;
13565 }
13566 /* Shorten "remain". */
13567 if (*q != NUL)
13568 STRCPY(remain, q - 1);
13569 else
13570 {
13571 vim_free(remain);
13572 remain = NULL;
13573 }
13574 }
13575
13576 /* If the result is a relative path name, make it explicitly relative to
13577 * the current directory if and only if the argument had this form. */
13578 if (!vim_ispathsep(*p))
13579 {
13580 if (is_relative_to_current
13581 && *p != NUL
13582 && !(p[0] == '.'
13583 && (p[1] == NUL
13584 || vim_ispathsep(p[1])
13585 || (p[1] == '.'
13586 && (p[2] == NUL
13587 || vim_ispathsep(p[2]))))))
13588 {
13589 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013590 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 if (cpy != NULL)
13592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 vim_free(p);
13594 p = cpy;
13595 }
13596 }
13597 else if (!is_relative_to_current)
13598 {
13599 /* Strip leading "./". */
13600 q = p;
13601 while (q[0] == '.' && vim_ispathsep(q[1]))
13602 q += 2;
13603 if (q > p)
13604 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13605 }
13606 }
13607
13608 /* Ensure that the result will have no trailing path separator
13609 * if the argument had none. But keep "/" or "//". */
13610 if (!has_trailing_pathsep)
13611 {
13612 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013613 if (after_pathsep(p, q))
13614 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 }
13616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 }
13619# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621# endif
13622#endif
13623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625
13626#ifdef HAVE_READLINK
13627fail:
13628#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630}
13631
13632/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013633 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 */
13635 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013636f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *argvars;
13638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639{
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 list_T *l;
13641 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642
Bram Moolenaar0d660222005-01-07 21:51:51 +000013643 rettv->vval.v_number = 0;
13644 if (argvars[0].v_type != VAR_LIST)
13645 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013646 else if ((l = argvars[0].vval.v_list) != NULL
13647 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013648 {
13649 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013650 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013651 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013652 while (li != NULL)
13653 {
13654 ni = li->li_prev;
13655 list_append(l, li);
13656 li = ni;
13657 }
13658 rettv->vval.v_list = l;
13659 rettv->v_type = VAR_LIST;
13660 ++l->lv_refcount;
13661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662}
13663
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013664#define SP_NOMOVE 0x01 /* don't move cursor */
13665#define SP_REPEAT 0x02 /* repeat to find outer pair */
13666#define SP_RETCOUNT 0x04 /* return matchcount */
13667#define SP_SETPCMARK 0x08 /* set previous context mark */
13668#define SP_START 0x10 /* accept match at start position */
13669#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13670#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013671
Bram Moolenaar33570922005-01-25 22:26:29 +000013672static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013673
13674/*
13675 * Get flags for a search function.
13676 * Possibly sets "p_ws".
13677 * Returns BACKWARD, FORWARD or zero (for an error).
13678 */
13679 static int
13680get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682 int *flagsp;
13683{
13684 int dir = FORWARD;
13685 char_u *flags;
13686 char_u nbuf[NUMBUFLEN];
13687 int mask;
13688
13689 if (varp->v_type != VAR_UNKNOWN)
13690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 flags = get_tv_string_buf_chk(varp, nbuf);
13692 if (flags == NULL)
13693 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694 while (*flags != NUL)
13695 {
13696 switch (*flags)
13697 {
13698 case 'b': dir = BACKWARD; break;
13699 case 'w': p_ws = TRUE; break;
13700 case 'W': p_ws = FALSE; break;
13701 default: mask = 0;
13702 if (flagsp != NULL)
13703 switch (*flags)
13704 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013705 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013706 case 'e': mask = SP_END; break;
13707 case 'm': mask = SP_RETCOUNT; break;
13708 case 'n': mask = SP_NOMOVE; break;
13709 case 'p': mask = SP_SUBPAT; break;
13710 case 'r': mask = SP_REPEAT; break;
13711 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712 }
13713 if (mask == 0)
13714 {
13715 EMSG2(_(e_invarg2), flags);
13716 dir = 0;
13717 }
13718 else
13719 *flagsp |= mask;
13720 }
13721 if (dir == 0)
13722 break;
13723 ++flags;
13724 }
13725 }
13726 return dir;
13727}
13728
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013730 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013732 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013733search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013735 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013736 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013738 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 char_u *pat;
13740 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013741 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 int save_p_ws = p_ws;
13743 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013744 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013745 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013746 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013747 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013750 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013751 if (dir == 0)
13752 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013753 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013754 if (flags & SP_START)
13755 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013756 if (flags & SP_END)
13757 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013758
13759 /* Optional extra argument: line number to stop searching. */
13760 if (argvars[1].v_type != VAR_UNKNOWN
13761 && argvars[2].v_type != VAR_UNKNOWN)
13762 {
13763 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13764 if (lnum_stop < 0)
13765 goto theend;
13766 }
13767
Bram Moolenaar231334e2005-07-25 20:46:57 +000013768 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013769 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013770 * Check to make sure only those flags are set.
13771 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13772 * flags cannot be set. Check for that condition also.
13773 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013774 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013775 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013778 goto theend;
13779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013781 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013782 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13783 options, RE_SEARCH, (linenr_T)lnum_stop);
13784 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013786 if (flags & SP_SUBPAT)
13787 retval = subpatnum;
13788 else
13789 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013790 if (flags & SP_SETPCMARK)
13791 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013793 if (match_pos != NULL)
13794 {
13795 /* Store the match cursor position */
13796 match_pos->lnum = pos.lnum;
13797 match_pos->col = pos.col + 1;
13798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 /* "/$" will put the cursor after the end of the line, may need to
13800 * correct that here */
13801 check_cursor();
13802 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013803
13804 /* If 'n' flag is used: restore cursor position. */
13805 if (flags & SP_NOMOVE)
13806 curwin->w_cursor = save_cursor;
13807theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013809
13810 return retval;
13811}
13812
13813/*
13814 * "search()" function
13815 */
13816 static void
13817f_search(argvars, rettv)
13818 typval_T *argvars;
13819 typval_T *rettv;
13820{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013821 int flags = 0;
13822
13823 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824}
13825
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013827 * "searchdecl()" function
13828 */
13829 static void
13830f_searchdecl(argvars, rettv)
13831 typval_T *argvars;
13832 typval_T *rettv;
13833{
13834 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013835 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013836 int error = FALSE;
13837 char_u *name;
13838
13839 rettv->vval.v_number = 1; /* default: FAIL */
13840
13841 name = get_tv_string_chk(&argvars[0]);
13842 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013843 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013844 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013845 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13846 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13847 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013848 if (!error && name != NULL)
13849 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013850 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013851}
13852
13853/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013854 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013856 static int
13857searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013859 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860{
13861 char_u *spat, *mpat, *epat;
13862 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 int dir;
13865 int flags = 0;
13866 char_u nbuf1[NUMBUFLEN];
13867 char_u nbuf2[NUMBUFLEN];
13868 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013869 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013870 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 spat = get_tv_string_chk(&argvars[0]);
13874 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13875 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13876 if (spat == NULL || mpat == NULL || epat == NULL)
13877 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 /* Handle the optional fourth argument: flags */
13880 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013881 if (dir == 0)
13882 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013883
13884 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013885 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13886 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013887 if ((flags & (SP_END | SP_SUBPAT)) != 0
13888 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013889 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013890 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013891 goto theend;
13892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013894 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013895 if (argvars[3].v_type == VAR_UNKNOWN
13896 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 skip = (char_u *)"";
13898 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013901 if (argvars[5].v_type != VAR_UNKNOWN)
13902 {
13903 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13904 if (lnum_stop < 0)
13905 goto theend;
13906 }
13907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 if (skip == NULL)
13909 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013911 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13912 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013913
13914theend:
13915 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013916
13917 return retval;
13918}
13919
13920/*
13921 * "searchpair()" function
13922 */
13923 static void
13924f_searchpair(argvars, rettv)
13925 typval_T *argvars;
13926 typval_T *rettv;
13927{
13928 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13929}
13930
13931/*
13932 * "searchpairpos()" function
13933 */
13934 static void
13935f_searchpairpos(argvars, rettv)
13936 typval_T *argvars;
13937 typval_T *rettv;
13938{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013939 pos_T match_pos;
13940 int lnum = 0;
13941 int col = 0;
13942
13943 rettv->vval.v_number = 0;
13944
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013945 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013946 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013947
13948 if (searchpair_cmn(argvars, &match_pos) > 0)
13949 {
13950 lnum = match_pos.lnum;
13951 col = match_pos.col;
13952 }
13953
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013954 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13955 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013956}
13957
13958/*
13959 * Search for a start/middle/end thing.
13960 * Used by searchpair(), see its documentation for the details.
13961 * Returns 0 or -1 for no match,
13962 */
13963 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013964do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013965 char_u *spat; /* start pattern */
13966 char_u *mpat; /* middle pattern */
13967 char_u *epat; /* end pattern */
13968 int dir; /* BACKWARD or FORWARD */
13969 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013970 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013971 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013972 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013973{
13974 char_u *save_cpo;
13975 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13976 long retval = 0;
13977 pos_T pos;
13978 pos_T firstpos;
13979 pos_T foundpos;
13980 pos_T save_cursor;
13981 pos_T save_pos;
13982 int n;
13983 int r;
13984 int nest = 1;
13985 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013986 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013987
13988 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13989 save_cpo = p_cpo;
13990 p_cpo = (char_u *)"";
13991
13992 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13993 * start/middle/end (pat3, for the top pair). */
13994 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13995 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13996 if (pat2 == NULL || pat3 == NULL)
13997 goto theend;
13998 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13999 if (*mpat == NUL)
14000 STRCPY(pat3, pat2);
14001 else
14002 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14003 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014004 if (flags & SP_START)
14005 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014006
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 save_cursor = curwin->w_cursor;
14008 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014009 clearpos(&firstpos);
14010 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 pat = pat3;
14012 for (;;)
14013 {
14014 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014015 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14017 /* didn't find it or found the first match again: FAIL */
14018 break;
14019
14020 if (firstpos.lnum == 0)
14021 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014022 if (equalpos(pos, foundpos))
14023 {
14024 /* Found the same position again. Can happen with a pattern that
14025 * has "\zs" at the end and searching backwards. Advance one
14026 * character and try again. */
14027 if (dir == BACKWARD)
14028 decl(&pos);
14029 else
14030 incl(&pos);
14031 }
14032 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033
14034 /* If the skip pattern matches, ignore this match. */
14035 if (*skip != NUL)
14036 {
14037 save_pos = curwin->w_cursor;
14038 curwin->w_cursor = pos;
14039 r = eval_to_bool(skip, &err, NULL, FALSE);
14040 curwin->w_cursor = save_pos;
14041 if (err)
14042 {
14043 /* Evaluating {skip} caused an error, break here. */
14044 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014045 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 break;
14047 }
14048 if (r)
14049 continue;
14050 }
14051
14052 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14053 {
14054 /* Found end when searching backwards or start when searching
14055 * forward: nested pair. */
14056 ++nest;
14057 pat = pat2; /* nested, don't search for middle */
14058 }
14059 else
14060 {
14061 /* Found end when searching forward or start when searching
14062 * backward: end of (nested) pair; or found middle in outer pair. */
14063 if (--nest == 1)
14064 pat = pat3; /* outer level, search for middle */
14065 }
14066
14067 if (nest == 0)
14068 {
14069 /* Found the match: return matchcount or line number. */
14070 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014071 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014073 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014074 if (flags & SP_SETPCMARK)
14075 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 curwin->w_cursor = pos;
14077 if (!(flags & SP_REPEAT))
14078 break;
14079 nest = 1; /* search for next unmatched */
14080 }
14081 }
14082
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014083 if (match_pos != NULL)
14084 {
14085 /* Store the match cursor position */
14086 match_pos->lnum = curwin->w_cursor.lnum;
14087 match_pos->col = curwin->w_cursor.col + 1;
14088 }
14089
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014091 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 curwin->w_cursor = save_cursor;
14093
14094theend:
14095 vim_free(pat2);
14096 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014098
14099 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100}
14101
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014102/*
14103 * "searchpos()" function
14104 */
14105 static void
14106f_searchpos(argvars, rettv)
14107 typval_T *argvars;
14108 typval_T *rettv;
14109{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014110 pos_T match_pos;
14111 int lnum = 0;
14112 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014113 int n;
14114 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014115
14116 rettv->vval.v_number = 0;
14117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014119 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014120
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014121 n = search_cmn(argvars, &match_pos, &flags);
14122 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014123 {
14124 lnum = match_pos.lnum;
14125 col = match_pos.col;
14126 }
14127
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014128 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14129 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014130 if (flags & SP_SUBPAT)
14131 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014132}
14133
14134
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135/*ARGSUSED*/
14136 static void
14137f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014138 typval_T *argvars;
14139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014141#ifdef FEAT_CLIENTSERVER
14142 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 char_u *server = get_tv_string_chk(&argvars[0]);
14144 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
Bram Moolenaar0d660222005-01-07 21:51:51 +000014146 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 if (server == NULL || reply == NULL)
14148 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 if (check_restricted() || check_secure())
14150 return;
14151# ifdef FEAT_X11
14152 if (check_connection() == FAIL)
14153 return;
14154# endif
14155
14156 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158 EMSG(_("E258: Unable to send to client"));
14159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161 rettv->vval.v_number = 0;
14162#else
14163 rettv->vval.v_number = -1;
14164#endif
14165}
14166
14167/*ARGSUSED*/
14168 static void
14169f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 typval_T *argvars;
14171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172{
14173 char_u *r = NULL;
14174
14175#ifdef FEAT_CLIENTSERVER
14176# ifdef WIN32
14177 r = serverGetVimNames();
14178# else
14179 make_connection();
14180 if (X_DISPLAY != NULL)
14181 r = serverGetVimNames(X_DISPLAY);
14182# endif
14183#endif
14184 rettv->v_type = VAR_STRING;
14185 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186}
14187
14188/*
14189 * "setbufvar()" function
14190 */
14191/*ARGSUSED*/
14192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014193f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 typval_T *argvars;
14195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196{
14197 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 char_u nbuf[NUMBUFLEN];
14202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 rettv->vval.v_number = 0;
14204
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 if (check_restricted() || check_secure())
14206 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14208 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014209 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 varp = &argvars[2];
14211
14212 if (buf != NULL && varname != NULL && varp != NULL)
14213 {
14214 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216
14217 if (*varname == '&')
14218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 long numval;
14220 char_u *strval;
14221 int error = FALSE;
14222
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 numval = get_tv_number_chk(varp, &error);
14225 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 if (!error && strval != NULL)
14227 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 }
14229 else
14230 {
14231 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14232 if (bufvarname != NULL)
14233 {
14234 STRCPY(bufvarname, "b:");
14235 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014236 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 vim_free(bufvarname);
14238 }
14239 }
14240
14241 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244}
14245
14246/*
14247 * "setcmdpos()" function
14248 */
14249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014250f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014251 typval_T *argvars;
14252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 int pos = (int)get_tv_number(&argvars[0]) - 1;
14255
14256 if (pos >= 0)
14257 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258}
14259
14260/*
14261 * "setline()" function
14262 */
14263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014264f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 typval_T *argvars;
14266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267{
14268 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014269 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014270 list_T *l = NULL;
14271 listitem_T *li = NULL;
14272 long added = 0;
14273 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014275 lnum = get_tv_lnum(&argvars[0]);
14276 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014278 l = argvars[1].vval.v_list;
14279 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014281 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014283
14284 rettv->vval.v_number = 0; /* OK */
14285 for (;;)
14286 {
14287 if (l != NULL)
14288 {
14289 /* list argument, get next string */
14290 if (li == NULL)
14291 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014293 li = li->li_next;
14294 }
14295
14296 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014298 break;
14299 if (lnum <= curbuf->b_ml.ml_line_count)
14300 {
14301 /* existing line, replace it */
14302 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14303 {
14304 changed_bytes(lnum, 0);
14305 check_cursor_col();
14306 rettv->vval.v_number = 0; /* OK */
14307 }
14308 }
14309 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14310 {
14311 /* lnum is one past the last line, append the line */
14312 ++added;
14313 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14314 rettv->vval.v_number = 0; /* OK */
14315 }
14316
14317 if (l == NULL) /* only one string argument */
14318 break;
14319 ++lnum;
14320 }
14321
14322 if (added > 0)
14323 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324}
14325
14326/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014327 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014328 */
14329/*ARGSUSED*/
14330 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014331set_qf_ll_list(wp, list_arg, action_arg, rettv)
14332 win_T *wp;
14333 typval_T *list_arg;
14334 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014335 typval_T *rettv;
14336{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014337#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014338 char_u *act;
14339 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014340#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014341
Bram Moolenaar2641f772005-03-25 21:58:17 +000014342 rettv->vval.v_number = -1;
14343
14344#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014345 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014346 EMSG(_(e_listreq));
14347 else
14348 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014349 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014350
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014351 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014352 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014353 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 if (act == NULL)
14355 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014356 if (*act == 'a' || *act == 'r')
14357 action = *act;
14358 }
14359
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014360 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014361 rettv->vval.v_number = 0;
14362 }
14363#endif
14364}
14365
14366/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014367 * "setloclist()" function
14368 */
14369/*ARGSUSED*/
14370 static void
14371f_setloclist(argvars, rettv)
14372 typval_T *argvars;
14373 typval_T *rettv;
14374{
14375 win_T *win;
14376
14377 rettv->vval.v_number = -1;
14378
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014379 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014380 if (win != NULL)
14381 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14382}
14383
14384/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014385 * "setpos()" function
14386 */
14387/*ARGSUSED*/
14388 static void
14389f_setpos(argvars, rettv)
14390 typval_T *argvars;
14391 typval_T *rettv;
14392{
14393 pos_T pos;
14394 int fnum;
14395 char_u *name;
14396
14397 name = get_tv_string_chk(argvars);
14398 if (name != NULL)
14399 {
14400 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14401 {
14402 --pos.col;
14403 if (name[0] == '.') /* cursor */
14404 {
14405 if (fnum == curbuf->b_fnum)
14406 {
14407 curwin->w_cursor = pos;
14408 check_cursor();
14409 }
14410 else
14411 EMSG(_(e_invarg));
14412 }
14413 else if (name[0] == '\'') /* mark */
14414 (void)setmark_pos(name[1], &pos, fnum);
14415 else
14416 EMSG(_(e_invarg));
14417 }
14418 }
14419}
14420
14421/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014422 * "setqflist()" function
14423 */
14424/*ARGSUSED*/
14425 static void
14426f_setqflist(argvars, rettv)
14427 typval_T *argvars;
14428 typval_T *rettv;
14429{
14430 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14431}
14432
14433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 * "setreg()" function
14435 */
14436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 typval_T *argvars;
14439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440{
14441 int regname;
14442 char_u *strregname;
14443 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 int append;
14446 char_u yank_type;
14447 long block_len;
14448
14449 block_len = -1;
14450 yank_type = MAUTO;
14451 append = FALSE;
14452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456 if (strregname == NULL)
14457 return; /* type error; errmsg already given */
14458 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 if (regname == 0 || regname == '@')
14460 regname = '"';
14461 else if (regname == '=')
14462 return;
14463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014464 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014466 stropt = get_tv_string_chk(&argvars[2]);
14467 if (stropt == NULL)
14468 return; /* type error */
14469 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 switch (*stropt)
14471 {
14472 case 'a': case 'A': /* append */
14473 append = TRUE;
14474 break;
14475 case 'v': case 'c': /* character-wise selection */
14476 yank_type = MCHAR;
14477 break;
14478 case 'V': case 'l': /* line-wise selection */
14479 yank_type = MLINE;
14480 break;
14481#ifdef FEAT_VISUAL
14482 case 'b': case Ctrl_V: /* block-wise selection */
14483 yank_type = MBLOCK;
14484 if (VIM_ISDIGIT(stropt[1]))
14485 {
14486 ++stropt;
14487 block_len = getdigits(&stropt) - 1;
14488 --stropt;
14489 }
14490 break;
14491#endif
14492 }
14493 }
14494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 strval = get_tv_string_chk(&argvars[1]);
14496 if (strval != NULL)
14497 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500}
14501
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014502/*
14503 * "settabwinvar()" function
14504 */
14505 static void
14506f_settabwinvar(argvars, rettv)
14507 typval_T *argvars;
14508 typval_T *rettv;
14509{
14510 setwinvar(argvars, rettv, 1);
14511}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512
14513/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014514 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 typval_T *argvars;
14519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014521 setwinvar(argvars, rettv, 0);
14522}
14523
14524/*
14525 * "setwinvar()" and "settabwinvar()" functions
14526 */
14527 static void
14528setwinvar(argvars, rettv, off)
14529 typval_T *argvars;
14530 typval_T *rettv;
14531 int off;
14532{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 win_T *win;
14534#ifdef FEAT_WINDOWS
14535 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014536 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537#endif
14538 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014541 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 rettv->vval.v_number = 0;
14544
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 if (check_restricted() || check_secure())
14546 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014547
14548#ifdef FEAT_WINDOWS
14549 if (off == 1)
14550 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14551 else
14552 tp = curtab;
14553#endif
14554 win = find_win_by_nr(&argvars[off], tp);
14555 varname = get_tv_string_chk(&argvars[off + 1]);
14556 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557
14558 if (win != NULL && varname != NULL && varp != NULL)
14559 {
14560#ifdef FEAT_WINDOWS
14561 /* set curwin to be our win, temporarily */
14562 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014563 save_curtab = curtab;
14564 goto_tabpage_tp(tp);
14565 if (!win_valid(win))
14566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 curwin = win;
14568 curbuf = curwin->w_buffer;
14569#endif
14570
14571 if (*varname == '&')
14572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 long numval;
14574 char_u *strval;
14575 int error = FALSE;
14576
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 numval = get_tv_number_chk(varp, &error);
14579 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 if (!error && strval != NULL)
14581 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 }
14583 else
14584 {
14585 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14586 if (winvarname != NULL)
14587 {
14588 STRCPY(winvarname, "w:");
14589 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014590 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 vim_free(winvarname);
14592 }
14593 }
14594
14595#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014596 /* Restore current tabpage and window, if still valid (autocomands can
14597 * make them invalid). */
14598 if (valid_tabpage(save_curtab))
14599 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 if (win_valid(save_curwin))
14601 {
14602 curwin = save_curwin;
14603 curbuf = curwin->w_buffer;
14604 }
14605#endif
14606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607}
14608
14609/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014610 * "shellescape({string})" function
14611 */
14612 static void
14613f_shellescape(argvars, rettv)
14614 typval_T *argvars;
14615 typval_T *rettv;
14616{
14617 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14618 rettv->v_type = VAR_STRING;
14619}
14620
14621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 */
14624 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 typval_T *argvars;
14627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631 p = get_tv_string(&argvars[0]);
14632 rettv->vval.v_string = vim_strsave(p);
14633 simplify_filename(rettv->vval.v_string); /* simplify in place */
14634 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635}
14636
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637static int
14638#ifdef __BORLANDC__
14639 _RTLENTRYF
14640#endif
14641 item_compare __ARGS((const void *s1, const void *s2));
14642static int
14643#ifdef __BORLANDC__
14644 _RTLENTRYF
14645#endif
14646 item_compare2 __ARGS((const void *s1, const void *s2));
14647
14648static int item_compare_ic;
14649static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014651#define ITEM_COMPARE_FAIL 999
14652
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656 static int
14657#ifdef __BORLANDC__
14658_RTLENTRYF
14659#endif
14660item_compare(s1, s2)
14661 const void *s1;
14662 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014664 char_u *p1, *p2;
14665 char_u *tofree1, *tofree2;
14666 int res;
14667 char_u numbuf1[NUMBUFLEN];
14668 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014670 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14671 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 if (item_compare_ic)
14673 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675 res = STRCMP(p1, p2);
14676 vim_free(tofree1);
14677 vim_free(tofree2);
14678 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
14681 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682#ifdef __BORLANDC__
14683_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685item_compare2(s1, s2)
14686 const void *s1;
14687 const void *s2;
14688{
14689 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014691 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014694 /* shortcut after failure in previous call; compare all items equal */
14695 if (item_compare_func_err)
14696 return 0;
14697
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014698 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14699 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014700 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14701 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702
14703 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014704 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014705 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 clear_tv(&argv[0]);
14707 clear_tv(&argv[1]);
14708
14709 if (res == FAIL)
14710 res = ITEM_COMPARE_FAIL;
14711 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712 /* return value has wrong type */
14713 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14714 if (item_compare_func_err)
14715 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 clear_tv(&rettv);
14717 return res;
14718}
14719
14720/*
14721 * "sort({list})" function
14722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014725 typval_T *argvars;
14726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727{
Bram Moolenaar33570922005-01-25 22:26:29 +000014728 list_T *l;
14729 listitem_T *li;
14730 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 long len;
14732 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733
Bram Moolenaar0d660222005-01-07 21:51:51 +000014734 rettv->vval.v_number = 0;
14735 if (argvars[0].v_type != VAR_LIST)
14736 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 else
14738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014739 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014740 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741 return;
14742 rettv->vval.v_list = l;
14743 rettv->v_type = VAR_LIST;
14744 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745
Bram Moolenaar0d660222005-01-07 21:51:51 +000014746 len = list_len(l);
14747 if (len <= 1)
14748 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 item_compare_ic = FALSE;
14751 item_compare_func = NULL;
14752 if (argvars[1].v_type != VAR_UNKNOWN)
14753 {
14754 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756 else
14757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 int error = FALSE;
14759
14760 i = get_tv_number_chk(&argvars[1], &error);
14761 if (error)
14762 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014763 if (i == 1)
14764 item_compare_ic = TRUE;
14765 else
14766 item_compare_func = get_tv_string(&argvars[1]);
14767 }
14768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769
Bram Moolenaar0d660222005-01-07 21:51:51 +000014770 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014772 if (ptrs == NULL)
14773 return;
14774 i = 0;
14775 for (li = l->lv_first; li != NULL; li = li->li_next)
14776 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014778 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014779 /* test the compare function */
14780 if (item_compare_func != NULL
14781 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14782 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014783 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014785 {
14786 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788 item_compare_func == NULL ? item_compare : item_compare2);
14789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014790 if (!item_compare_func_err)
14791 {
14792 /* Clear the List and append the items in the sorted order. */
14793 l->lv_first = l->lv_last = NULL;
14794 l->lv_len = 0;
14795 for (i = 0; i < len; ++i)
14796 list_append(l, ptrs[i]);
14797 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798 }
14799
14800 vim_free(ptrs);
14801 }
14802}
14803
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014804/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014805 * "soundfold({word})" function
14806 */
14807 static void
14808f_soundfold(argvars, rettv)
14809 typval_T *argvars;
14810 typval_T *rettv;
14811{
14812 char_u *s;
14813
14814 rettv->v_type = VAR_STRING;
14815 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014816#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014817 rettv->vval.v_string = eval_soundfold(s);
14818#else
14819 rettv->vval.v_string = vim_strsave(s);
14820#endif
14821}
14822
14823/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014824 * "spellbadword()" function
14825 */
14826/* ARGSUSED */
14827 static void
14828f_spellbadword(argvars, rettv)
14829 typval_T *argvars;
14830 typval_T *rettv;
14831{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014832 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014833 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014834 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014835
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014836 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014837 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014838
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014839#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014840 if (argvars[0].v_type == VAR_UNKNOWN)
14841 {
14842 /* Find the start and length of the badly spelled word. */
14843 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14844 if (len != 0)
14845 word = ml_get_cursor();
14846 }
14847 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14848 {
14849 char_u *str = get_tv_string_chk(&argvars[0]);
14850 int capcol = -1;
14851
14852 if (str != NULL)
14853 {
14854 /* Check the argument for spelling. */
14855 while (*str != NUL)
14856 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014857 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014858 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014859 {
14860 word = str;
14861 break;
14862 }
14863 str += len;
14864 }
14865 }
14866 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014867#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014868
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014869 list_append_string(rettv->vval.v_list, word, len);
14870 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014871 attr == HLF_SPB ? "bad" :
14872 attr == HLF_SPR ? "rare" :
14873 attr == HLF_SPL ? "local" :
14874 attr == HLF_SPC ? "caps" :
14875 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014876}
14877
14878/*
14879 * "spellsuggest()" function
14880 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014881/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014882 static void
14883f_spellsuggest(argvars, rettv)
14884 typval_T *argvars;
14885 typval_T *rettv;
14886{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014887#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014888 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014889 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014890 int maxcount;
14891 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014892 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014893 listitem_T *li;
14894 int need_capital = FALSE;
14895#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014896
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014897 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014898 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014899
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014900#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014901 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14902 {
14903 str = get_tv_string(&argvars[0]);
14904 if (argvars[1].v_type != VAR_UNKNOWN)
14905 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014906 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014907 if (maxcount <= 0)
14908 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014909 if (argvars[2].v_type != VAR_UNKNOWN)
14910 {
14911 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14912 if (typeerr)
14913 return;
14914 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014915 }
14916 else
14917 maxcount = 25;
14918
Bram Moolenaar4770d092006-01-12 23:22:24 +000014919 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014920
14921 for (i = 0; i < ga.ga_len; ++i)
14922 {
14923 str = ((char_u **)ga.ga_data)[i];
14924
14925 li = listitem_alloc();
14926 if (li == NULL)
14927 vim_free(str);
14928 else
14929 {
14930 li->li_tv.v_type = VAR_STRING;
14931 li->li_tv.v_lock = 0;
14932 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014933 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014934 }
14935 }
14936 ga_clear(&ga);
14937 }
14938#endif
14939}
14940
Bram Moolenaar0d660222005-01-07 21:51:51 +000014941 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014942f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014943 typval_T *argvars;
14944 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014945{
14946 char_u *str;
14947 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014948 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 regmatch_T regmatch;
14950 char_u patbuf[NUMBUFLEN];
14951 char_u *save_cpo;
14952 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014953 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014954 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956
14957 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14958 save_cpo = p_cpo;
14959 p_cpo = (char_u *)"";
14960
14961 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014962 if (argvars[1].v_type != VAR_UNKNOWN)
14963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014964 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14965 if (pat == NULL)
14966 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014969 }
14970 if (pat == NULL || *pat == NUL)
14971 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014973 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014975 if (typeerr)
14976 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14979 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014982 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014983 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014984 if (*str == NUL)
14985 match = FALSE; /* empty item at the end */
14986 else
14987 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988 if (match)
14989 end = regmatch.startp[0];
14990 else
14991 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014992 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14993 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014995 if (list_append_string(rettv->vval.v_list, str,
14996 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014998 }
14999 if (!match)
15000 break;
15001 /* Advance to just after the match. */
15002 if (regmatch.endp[0] > str)
15003 col = 0;
15004 else
15005 {
15006 /* Don't get stuck at the same match. */
15007#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015008 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015009#else
15010 col = 1;
15011#endif
15012 }
15013 str = regmatch.endp[0];
15014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020}
15021
Bram Moolenaar2c932302006-03-18 21:42:09 +000015022/*
15023 * "str2nr()" function
15024 */
15025 static void
15026f_str2nr(argvars, rettv)
15027 typval_T *argvars;
15028 typval_T *rettv;
15029{
15030 int base = 10;
15031 char_u *p;
15032 long n;
15033
15034 if (argvars[1].v_type != VAR_UNKNOWN)
15035 {
15036 base = get_tv_number(&argvars[1]);
15037 if (base != 8 && base != 10 && base != 16)
15038 {
15039 EMSG(_(e_invarg));
15040 return;
15041 }
15042 }
15043
15044 p = skipwhite(get_tv_string(&argvars[0]));
15045 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15046 rettv->vval.v_number = n;
15047}
15048
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049#ifdef HAVE_STRFTIME
15050/*
15051 * "strftime({format}[, {time}])" function
15052 */
15053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015055 typval_T *argvars;
15056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057{
15058 char_u result_buf[256];
15059 struct tm *curtime;
15060 time_t seconds;
15061 char_u *p;
15062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015066 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 seconds = time(NULL);
15068 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015069 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 curtime = localtime(&seconds);
15071 /* MSVC returns NULL for an invalid value of seconds. */
15072 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015073 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 else
15075 {
15076# ifdef FEAT_MBYTE
15077 vimconv_T conv;
15078 char_u *enc;
15079
15080 conv.vc_type = CONV_NONE;
15081 enc = enc_locale();
15082 convert_setup(&conv, p_enc, enc);
15083 if (conv.vc_type != CONV_NONE)
15084 p = string_convert(&conv, p, NULL);
15085# endif
15086 if (p != NULL)
15087 (void)strftime((char *)result_buf, sizeof(result_buf),
15088 (char *)p, curtime);
15089 else
15090 result_buf[0] = NUL;
15091
15092# ifdef FEAT_MBYTE
15093 if (conv.vc_type != CONV_NONE)
15094 vim_free(p);
15095 convert_setup(&conv, enc, p_enc);
15096 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 else
15099# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101
15102# ifdef FEAT_MBYTE
15103 /* Release conversion descriptors */
15104 convert_setup(&conv, NULL, NULL);
15105 vim_free(enc);
15106# endif
15107 }
15108}
15109#endif
15110
15111/*
15112 * "stridx()" function
15113 */
15114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015115f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015116 typval_T *argvars;
15117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118{
15119 char_u buf[NUMBUFLEN];
15120 char_u *needle;
15121 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015124 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015126 needle = get_tv_string_chk(&argvars[1]);
15127 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015129 if (needle == NULL || haystack == NULL)
15130 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 if (argvars[2].v_type != VAR_UNKNOWN)
15133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 int error = FALSE;
15135
15136 start_idx = get_tv_number_chk(&argvars[2], &error);
15137 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015139 if (start_idx >= 0)
15140 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 }
15142
15143 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15144 if (pos != NULL)
15145 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146}
15147
15148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015149 * "string()" function
15150 */
15151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *argvars;
15154 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015155{
15156 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015157 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015160 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015161 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015162 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163}
15164
15165/*
15166 * "strlen()" function
15167 */
15168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015169f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015170 typval_T *argvars;
15171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 rettv->vval.v_number = (varnumber_T)(STRLEN(
15174 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175}
15176
15177/*
15178 * "strpart()" function
15179 */
15180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015181f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015182 typval_T *argvars;
15183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184{
15185 char_u *p;
15186 int n;
15187 int len;
15188 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015191 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 slen = (int)STRLEN(p);
15193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015194 n = get_tv_number_chk(&argvars[1], &error);
15195 if (error)
15196 len = 0;
15197 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015198 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 else
15200 len = slen - n; /* default len: all bytes that are available. */
15201
15202 /*
15203 * Only return the overlap between the specified part and the actual
15204 * string.
15205 */
15206 if (n < 0)
15207 {
15208 len += n;
15209 n = 0;
15210 }
15211 else if (n > slen)
15212 n = slen;
15213 if (len < 0)
15214 len = 0;
15215 else if (n + len > slen)
15216 len = slen - n;
15217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015218 rettv->v_type = VAR_STRING;
15219 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220}
15221
15222/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 * "strridx()" function
15224 */
15225 static void
15226f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 typval_T *argvars;
15228 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229{
15230 char_u buf[NUMBUFLEN];
15231 char_u *needle;
15232 char_u *haystack;
15233 char_u *rest;
15234 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015235 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 needle = get_tv_string_chk(&argvars[1]);
15238 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239
15240 rettv->vval.v_number = -1;
15241 if (needle == NULL || haystack == NULL)
15242 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015243
15244 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015245 if (argvars[2].v_type != VAR_UNKNOWN)
15246 {
15247 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015249 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015251 }
15252 else
15253 end_idx = haystack_len;
15254
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015256 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015258 lastmatch = haystack + end_idx;
15259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015261 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015262 for (rest = haystack; *rest != '\0'; ++rest)
15263 {
15264 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015265 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015266 break;
15267 lastmatch = rest;
15268 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015269 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015270
15271 if (lastmatch == NULL)
15272 rettv->vval.v_number = -1;
15273 else
15274 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15275}
15276
15277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 * "strtrans()" function
15279 */
15280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015281f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015282 typval_T *argvars;
15283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285 rettv->v_type = VAR_STRING;
15286 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287}
15288
15289/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290 * "submatch()" function
15291 */
15292 static void
15293f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015294 typval_T *argvars;
15295 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015296{
15297 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015298 rettv->vval.v_string =
15299 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015300}
15301
15302/*
15303 * "substitute()" function
15304 */
15305 static void
15306f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *argvars;
15308 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309{
15310 char_u patbuf[NUMBUFLEN];
15311 char_u subbuf[NUMBUFLEN];
15312 char_u flagsbuf[NUMBUFLEN];
15313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 char_u *str = get_tv_string_chk(&argvars[0]);
15315 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15316 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15317 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15318
Bram Moolenaar0d660222005-01-07 21:51:51 +000015319 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15321 rettv->vval.v_string = NULL;
15322 else
15323 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324}
15325
15326/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015327 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 */
15329/*ARGSUSED*/
15330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015332 typval_T *argvars;
15333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334{
15335 int id = 0;
15336#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015337 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 long col;
15339 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015340 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015342 lnum = get_tv_lnum(argvars); /* -1 on type error */
15343 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15344 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015346 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015347 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015348 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349#endif
15350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352}
15353
15354/*
15355 * "synIDattr(id, what [, mode])" function
15356 */
15357/*ARGSUSED*/
15358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 typval_T *argvars;
15361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362{
15363 char_u *p = NULL;
15364#ifdef FEAT_SYN_HL
15365 int id;
15366 char_u *what;
15367 char_u *mode;
15368 char_u modebuf[NUMBUFLEN];
15369 int modec;
15370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 id = get_tv_number(&argvars[0]);
15372 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015373 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 modec = TOLOWER_ASC(mode[0]);
15377 if (modec != 't' && modec != 'c'
15378#ifdef FEAT_GUI
15379 && modec != 'g'
15380#endif
15381 )
15382 modec = 0; /* replace invalid with current */
15383 }
15384 else
15385 {
15386#ifdef FEAT_GUI
15387 if (gui.in_use)
15388 modec = 'g';
15389 else
15390#endif
15391 if (t_colors > 1)
15392 modec = 'c';
15393 else
15394 modec = 't';
15395 }
15396
15397
15398 switch (TOLOWER_ASC(what[0]))
15399 {
15400 case 'b':
15401 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15402 p = highlight_color(id, what, modec);
15403 else /* bold */
15404 p = highlight_has_attr(id, HL_BOLD, modec);
15405 break;
15406
15407 case 'f': /* fg[#] */
15408 p = highlight_color(id, what, modec);
15409 break;
15410
15411 case 'i':
15412 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15413 p = highlight_has_attr(id, HL_INVERSE, modec);
15414 else /* italic */
15415 p = highlight_has_attr(id, HL_ITALIC, modec);
15416 break;
15417
15418 case 'n': /* name */
15419 p = get_highlight_name(NULL, id - 1);
15420 break;
15421
15422 case 'r': /* reverse */
15423 p = highlight_has_attr(id, HL_INVERSE, modec);
15424 break;
15425
15426 case 's': /* standout */
15427 p = highlight_has_attr(id, HL_STANDOUT, modec);
15428 break;
15429
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015430 case 'u':
15431 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15432 /* underline */
15433 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15434 else
15435 /* undercurl */
15436 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 break;
15438 }
15439
15440 if (p != NULL)
15441 p = vim_strsave(p);
15442#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->v_type = VAR_STRING;
15444 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445}
15446
15447/*
15448 * "synIDtrans(id)" function
15449 */
15450/*ARGSUSED*/
15451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015453 typval_T *argvars;
15454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455{
15456 int id;
15457
15458#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
15461 if (id > 0)
15462 id = syn_get_final_id(id);
15463 else
15464#endif
15465 id = 0;
15466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468}
15469
15470/*
15471 * "system()" function
15472 */
15473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 typval_T *argvars;
15476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015478 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015480 char_u *infile = NULL;
15481 char_u buf[NUMBUFLEN];
15482 int err = FALSE;
15483 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015486 {
15487 /*
15488 * Write the string to a temp file, to be used for input of the shell
15489 * command.
15490 */
15491 if ((infile = vim_tempname('i')) == NULL)
15492 {
15493 EMSG(_(e_notmp));
15494 return;
15495 }
15496
15497 fd = mch_fopen((char *)infile, WRITEBIN);
15498 if (fd == NULL)
15499 {
15500 EMSG2(_(e_notopen), infile);
15501 goto done;
15502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015503 p = get_tv_string_buf_chk(&argvars[1], buf);
15504 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015505 {
15506 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015507 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015508 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015509 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15510 err = TRUE;
15511 if (fclose(fd) != 0)
15512 err = TRUE;
15513 if (err)
15514 {
15515 EMSG(_("E677: Error writing temp file"));
15516 goto done;
15517 }
15518 }
15519
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015520 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15521 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015522
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523#ifdef USE_CR
15524 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015525 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 {
15527 char_u *s;
15528
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015529 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 {
15531 if (*s == CAR)
15532 *s = NL;
15533 }
15534 }
15535#else
15536# ifdef USE_CRNL
15537 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015538 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 {
15540 char_u *s, *d;
15541
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015542 d = res;
15543 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 {
15545 if (s[0] == CAR && s[1] == NL)
15546 ++s;
15547 *d++ = *s;
15548 }
15549 *d = NUL;
15550 }
15551# endif
15552#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015553
15554done:
15555 if (infile != NULL)
15556 {
15557 mch_remove(infile);
15558 vim_free(infile);
15559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->v_type = VAR_STRING;
15561 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562}
15563
15564/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015565 * "tabpagebuflist()" function
15566 */
15567/* ARGSUSED */
15568 static void
15569f_tabpagebuflist(argvars, rettv)
15570 typval_T *argvars;
15571 typval_T *rettv;
15572{
15573#ifndef FEAT_WINDOWS
15574 rettv->vval.v_number = 0;
15575#else
15576 tabpage_T *tp;
15577 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015578
15579 if (argvars[0].v_type == VAR_UNKNOWN)
15580 wp = firstwin;
15581 else
15582 {
15583 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15584 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015585 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015586 }
15587 if (wp == NULL)
15588 rettv->vval.v_number = 0;
15589 else
15590 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015591 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015592 rettv->vval.v_number = 0;
15593 else
15594 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015595 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015596 if (list_append_number(rettv->vval.v_list,
15597 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015598 break;
15599 }
15600 }
15601#endif
15602}
15603
15604
15605/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015606 * "tabpagenr()" function
15607 */
15608/* ARGSUSED */
15609 static void
15610f_tabpagenr(argvars, rettv)
15611 typval_T *argvars;
15612 typval_T *rettv;
15613{
15614 int nr = 1;
15615#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015616 char_u *arg;
15617
15618 if (argvars[0].v_type != VAR_UNKNOWN)
15619 {
15620 arg = get_tv_string_chk(&argvars[0]);
15621 nr = 0;
15622 if (arg != NULL)
15623 {
15624 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015625 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015626 else
15627 EMSG2(_(e_invexpr2), arg);
15628 }
15629 }
15630 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015631 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015632#endif
15633 rettv->vval.v_number = nr;
15634}
15635
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015636
15637#ifdef FEAT_WINDOWS
15638static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15639
15640/*
15641 * Common code for tabpagewinnr() and winnr().
15642 */
15643 static int
15644get_winnr(tp, argvar)
15645 tabpage_T *tp;
15646 typval_T *argvar;
15647{
15648 win_T *twin;
15649 int nr = 1;
15650 win_T *wp;
15651 char_u *arg;
15652
15653 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15654 if (argvar->v_type != VAR_UNKNOWN)
15655 {
15656 arg = get_tv_string_chk(argvar);
15657 if (arg == NULL)
15658 nr = 0; /* type error; errmsg already given */
15659 else if (STRCMP(arg, "$") == 0)
15660 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15661 else if (STRCMP(arg, "#") == 0)
15662 {
15663 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15664 if (twin == NULL)
15665 nr = 0;
15666 }
15667 else
15668 {
15669 EMSG2(_(e_invexpr2), arg);
15670 nr = 0;
15671 }
15672 }
15673
15674 if (nr > 0)
15675 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15676 wp != twin; wp = wp->w_next)
15677 ++nr;
15678 return nr;
15679}
15680#endif
15681
15682/*
15683 * "tabpagewinnr()" function
15684 */
15685/* ARGSUSED */
15686 static void
15687f_tabpagewinnr(argvars, rettv)
15688 typval_T *argvars;
15689 typval_T *rettv;
15690{
15691 int nr = 1;
15692#ifdef FEAT_WINDOWS
15693 tabpage_T *tp;
15694
15695 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15696 if (tp == NULL)
15697 nr = 0;
15698 else
15699 nr = get_winnr(tp, &argvars[1]);
15700#endif
15701 rettv->vval.v_number = nr;
15702}
15703
15704
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015705/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015706 * "tagfiles()" function
15707 */
15708/*ARGSUSED*/
15709 static void
15710f_tagfiles(argvars, rettv)
15711 typval_T *argvars;
15712 typval_T *rettv;
15713{
15714 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015715 tagname_T tn;
15716 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015717
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015718 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015719 {
15720 rettv->vval.v_number = 0;
15721 return;
15722 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015723
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015724 for (first = TRUE; ; first = FALSE)
15725 if (get_tagfname(&tn, first, fname) == FAIL
15726 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015727 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015728 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015729}
15730
15731/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015732 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015733 */
15734 static void
15735f_taglist(argvars, rettv)
15736 typval_T *argvars;
15737 typval_T *rettv;
15738{
15739 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015740
15741 tag_pattern = get_tv_string(&argvars[0]);
15742
15743 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015744 if (*tag_pattern == NUL)
15745 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015746
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015747 if (rettv_list_alloc(rettv) == OK)
15748 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015749}
15750
15751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 * "tempname()" function
15753 */
15754/*ARGSUSED*/
15755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 typval_T *argvars;
15758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759{
15760 static int x = 'A';
15761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->v_type = VAR_STRING;
15763 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764
15765 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15766 * names. Skip 'I' and 'O', they are used for shell redirection. */
15767 do
15768 {
15769 if (x == 'Z')
15770 x = '0';
15771 else if (x == '9')
15772 x = 'A';
15773 else
15774 {
15775#ifdef EBCDIC
15776 if (x == 'I')
15777 x = 'J';
15778 else if (x == 'R')
15779 x = 'S';
15780 else
15781#endif
15782 ++x;
15783 }
15784 } while (x == 'I' || x == 'O');
15785}
15786
15787/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015788 * "test(list)" function: Just checking the walls...
15789 */
15790/*ARGSUSED*/
15791 static void
15792f_test(argvars, rettv)
15793 typval_T *argvars;
15794 typval_T *rettv;
15795{
15796 /* Used for unit testing. Change the code below to your liking. */
15797#if 0
15798 listitem_T *li;
15799 list_T *l;
15800 char_u *bad, *good;
15801
15802 if (argvars[0].v_type != VAR_LIST)
15803 return;
15804 l = argvars[0].vval.v_list;
15805 if (l == NULL)
15806 return;
15807 li = l->lv_first;
15808 if (li == NULL)
15809 return;
15810 bad = get_tv_string(&li->li_tv);
15811 li = li->li_next;
15812 if (li == NULL)
15813 return;
15814 good = get_tv_string(&li->li_tv);
15815 rettv->vval.v_number = test_edit_score(bad, good);
15816#endif
15817}
15818
15819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 * "tolower(string)" function
15821 */
15822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015824 typval_T *argvars;
15825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826{
15827 char_u *p;
15828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015829 p = vim_strsave(get_tv_string(&argvars[0]));
15830 rettv->v_type = VAR_STRING;
15831 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832
15833 if (p != NULL)
15834 while (*p != NUL)
15835 {
15836#ifdef FEAT_MBYTE
15837 int l;
15838
15839 if (enc_utf8)
15840 {
15841 int c, lc;
15842
15843 c = utf_ptr2char(p);
15844 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015845 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 /* TODO: reallocate string when byte count changes. */
15847 if (utf_char2len(lc) == l)
15848 utf_char2bytes(lc, p);
15849 p += l;
15850 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015851 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 p += l; /* skip multi-byte character */
15853 else
15854#endif
15855 {
15856 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15857 ++p;
15858 }
15859 }
15860}
15861
15862/*
15863 * "toupper(string)" function
15864 */
15865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015866f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015867 typval_T *argvars;
15868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015870 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872}
15873
15874/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015875 * "tr(string, fromstr, tostr)" function
15876 */
15877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015878f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015879 typval_T *argvars;
15880 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015881{
15882 char_u *instr;
15883 char_u *fromstr;
15884 char_u *tostr;
15885 char_u *p;
15886#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015887 int inlen;
15888 int fromlen;
15889 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015890 int idx;
15891 char_u *cpstr;
15892 int cplen;
15893 int first = TRUE;
15894#endif
15895 char_u buf[NUMBUFLEN];
15896 char_u buf2[NUMBUFLEN];
15897 garray_T ga;
15898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015899 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15901 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015902
15903 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904 rettv->v_type = VAR_STRING;
15905 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 if (fromstr == NULL || tostr == NULL)
15907 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015908 ga_init2(&ga, (int)sizeof(char), 80);
15909
15910#ifdef FEAT_MBYTE
15911 if (!has_mbyte)
15912#endif
15913 /* not multi-byte: fromstr and tostr must be the same length */
15914 if (STRLEN(fromstr) != STRLEN(tostr))
15915 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015916#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015917error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015918#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015919 EMSG2(_(e_invarg2), fromstr);
15920 ga_clear(&ga);
15921 return;
15922 }
15923
15924 /* fromstr and tostr have to contain the same number of chars */
15925 while (*instr != NUL)
15926 {
15927#ifdef FEAT_MBYTE
15928 if (has_mbyte)
15929 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015930 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015931 cpstr = instr;
15932 cplen = inlen;
15933 idx = 0;
15934 for (p = fromstr; *p != NUL; p += fromlen)
15935 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015936 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015937 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15938 {
15939 for (p = tostr; *p != NUL; p += tolen)
15940 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015941 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015942 if (idx-- == 0)
15943 {
15944 cplen = tolen;
15945 cpstr = p;
15946 break;
15947 }
15948 }
15949 if (*p == NUL) /* tostr is shorter than fromstr */
15950 goto error;
15951 break;
15952 }
15953 ++idx;
15954 }
15955
15956 if (first && cpstr == instr)
15957 {
15958 /* Check that fromstr and tostr have the same number of
15959 * (multi-byte) characters. Done only once when a character
15960 * of instr doesn't appear in fromstr. */
15961 first = FALSE;
15962 for (p = tostr; *p != NUL; p += tolen)
15963 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015964 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015965 --idx;
15966 }
15967 if (idx != 0)
15968 goto error;
15969 }
15970
15971 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015972 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015973 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015974
15975 instr += inlen;
15976 }
15977 else
15978#endif
15979 {
15980 /* When not using multi-byte chars we can do it faster. */
15981 p = vim_strchr(fromstr, *instr);
15982 if (p != NULL)
15983 ga_append(&ga, tostr[p - fromstr]);
15984 else
15985 ga_append(&ga, *instr);
15986 ++instr;
15987 }
15988 }
15989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015991}
15992
15993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 * "type(expr)" function
15995 */
15996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015997f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 typval_T *argvars;
15999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016001 int n;
16002
16003 switch (argvars[0].v_type)
16004 {
16005 case VAR_NUMBER: n = 0; break;
16006 case VAR_STRING: n = 1; break;
16007 case VAR_FUNC: n = 2; break;
16008 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016009 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016010 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16011 }
16012 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013}
16014
16015/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016016 * "values(dict)" function
16017 */
16018 static void
16019f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 typval_T *argvars;
16021 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016022{
16023 dict_list(argvars, rettv, 1);
16024}
16025
16026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 * "virtcol(string)" function
16028 */
16029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016030f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016031 typval_T *argvars;
16032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033{
16034 colnr_T vcol = 0;
16035 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016036 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016038 fp = var2fpos(&argvars[0], FALSE, &fnum);
16039 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16040 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 {
16042 getvvcol(curwin, fp, NULL, NULL, &vcol);
16043 ++vcol;
16044 }
16045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047}
16048
16049/*
16050 * "visualmode()" function
16051 */
16052/*ARGSUSED*/
16053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 typval_T *argvars;
16056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057{
16058#ifdef FEAT_VISUAL
16059 char_u str[2];
16060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016061 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 str[0] = curbuf->b_visual_mode_eval;
16063 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016064 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065
16066 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016067 if ((argvars[0].v_type == VAR_NUMBER
16068 && argvars[0].vval.v_number != 0)
16069 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 curbuf->b_visual_mode_eval = NUL;
16072#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016073 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074#endif
16075}
16076
16077/*
16078 * "winbufnr(nr)" function
16079 */
16080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 typval_T *argvars;
16083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084{
16085 win_T *wp;
16086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016087 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016091 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092}
16093
16094/*
16095 * "wincol()" function
16096 */
16097/*ARGSUSED*/
16098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016100 typval_T *argvars;
16101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102{
16103 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016104 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105}
16106
16107/*
16108 * "winheight(nr)" function
16109 */
16110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016112 typval_T *argvars;
16113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114{
16115 win_T *wp;
16116
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016117 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016119 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016121 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122}
16123
16124/*
16125 * "winline()" function
16126 */
16127/*ARGSUSED*/
16128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016129f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016130 typval_T *argvars;
16131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132{
16133 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016134 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136
16137/*
16138 * "winnr()" function
16139 */
16140/* ARGSUSED */
16141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016142f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 typval_T *argvars;
16144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145{
16146 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016147
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016149 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016151 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152}
16153
16154/*
16155 * "winrestcmd()" function
16156 */
16157/* ARGSUSED */
16158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 typval_T *argvars;
16161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162{
16163#ifdef FEAT_WINDOWS
16164 win_T *wp;
16165 int winnr = 1;
16166 garray_T ga;
16167 char_u buf[50];
16168
16169 ga_init2(&ga, (int)sizeof(char), 70);
16170 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16171 {
16172 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16173 ga_concat(&ga, buf);
16174# ifdef FEAT_VERTSPLIT
16175 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16176 ga_concat(&ga, buf);
16177# endif
16178 ++winnr;
16179 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016180 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187}
16188
16189/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016190 * "winrestview()" function
16191 */
16192/* ARGSUSED */
16193 static void
16194f_winrestview(argvars, rettv)
16195 typval_T *argvars;
16196 typval_T *rettv;
16197{
16198 dict_T *dict;
16199
16200 if (argvars[0].v_type != VAR_DICT
16201 || (dict = argvars[0].vval.v_dict) == NULL)
16202 EMSG(_(e_invarg));
16203 else
16204 {
16205 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16206 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16207#ifdef FEAT_VIRTUALEDIT
16208 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16209#endif
16210 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016211 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016212
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016213 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016214#ifdef FEAT_DIFF
16215 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16216#endif
16217 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16218 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16219
16220 check_cursor();
16221 changed_cline_bef_curs();
16222 invalidate_botline();
16223 redraw_later(VALID);
16224
16225 if (curwin->w_topline == 0)
16226 curwin->w_topline = 1;
16227 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16228 curwin->w_topline = curbuf->b_ml.ml_line_count;
16229#ifdef FEAT_DIFF
16230 check_topfill(curwin, TRUE);
16231#endif
16232 }
16233}
16234
16235/*
16236 * "winsaveview()" function
16237 */
16238/* ARGSUSED */
16239 static void
16240f_winsaveview(argvars, rettv)
16241 typval_T *argvars;
16242 typval_T *rettv;
16243{
16244 dict_T *dict;
16245
16246 dict = dict_alloc();
16247 if (dict == NULL)
16248 return;
16249 rettv->v_type = VAR_DICT;
16250 rettv->vval.v_dict = dict;
16251 ++dict->dv_refcount;
16252
16253 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16254 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16255#ifdef FEAT_VIRTUALEDIT
16256 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16257#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016258 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016259 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16260
16261 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16262#ifdef FEAT_DIFF
16263 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16264#endif
16265 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16266 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16267}
16268
16269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 * "winwidth(nr)" function
16271 */
16272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 typval_T *argvars;
16275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276{
16277 win_T *wp;
16278
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016279 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 else
16283#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016284 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016286 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287#endif
16288}
16289
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016291 * "writefile()" function
16292 */
16293 static void
16294f_writefile(argvars, rettv)
16295 typval_T *argvars;
16296 typval_T *rettv;
16297{
16298 int binary = FALSE;
16299 char_u *fname;
16300 FILE *fd;
16301 listitem_T *li;
16302 char_u *s;
16303 int ret = 0;
16304 int c;
16305
16306 if (argvars[0].v_type != VAR_LIST)
16307 {
16308 EMSG2(_(e_listarg), "writefile()");
16309 return;
16310 }
16311 if (argvars[0].vval.v_list == NULL)
16312 return;
16313
16314 if (argvars[2].v_type != VAR_UNKNOWN
16315 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16316 binary = TRUE;
16317
16318 /* Always open the file in binary mode, library functions have a mind of
16319 * their own about CR-LF conversion. */
16320 fname = get_tv_string(&argvars[1]);
16321 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16322 {
16323 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16324 ret = -1;
16325 }
16326 else
16327 {
16328 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16329 li = li->li_next)
16330 {
16331 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16332 {
16333 if (*s == '\n')
16334 c = putc(NUL, fd);
16335 else
16336 c = putc(*s, fd);
16337 if (c == EOF)
16338 {
16339 ret = -1;
16340 break;
16341 }
16342 }
16343 if (!binary || li->li_next != NULL)
16344 if (putc('\n', fd) == EOF)
16345 {
16346 ret = -1;
16347 break;
16348 }
16349 if (ret < 0)
16350 {
16351 EMSG(_(e_write));
16352 break;
16353 }
16354 }
16355 fclose(fd);
16356 }
16357
16358 rettv->vval.v_number = ret;
16359}
16360
16361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016363 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364 */
16365 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016366var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016367 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016369 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016371 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016373 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374
Bram Moolenaara5525202006-03-02 22:52:09 +000016375 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016376 if (varp->v_type == VAR_LIST)
16377 {
16378 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016379 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016380 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016381
16382 l = varp->vval.v_list;
16383 if (l == NULL)
16384 return NULL;
16385
16386 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016387 pos.lnum = list_find_nr(l, 0L, &error);
16388 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016389 return NULL; /* invalid line number */
16390
16391 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016392 pos.col = list_find_nr(l, 1L, &error);
16393 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016394 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016395 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016396 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016397 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016398 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016399 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016400
Bram Moolenaara5525202006-03-02 22:52:09 +000016401#ifdef FEAT_VIRTUALEDIT
16402 /* Get the virtual offset. Defaults to zero. */
16403 pos.coladd = list_find_nr(l, 2L, &error);
16404 if (error)
16405 pos.coladd = 0;
16406#endif
16407
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016408 return &pos;
16409 }
16410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016411 name = get_tv_string_chk(varp);
16412 if (name == NULL)
16413 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 if (name[0] == '.') /* cursor */
16415 return &curwin->w_cursor;
16416 if (name[0] == '\'') /* mark */
16417 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016418 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16420 return NULL;
16421 return pp;
16422 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016423
16424#ifdef FEAT_VIRTUALEDIT
16425 pos.coladd = 0;
16426#endif
16427
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016428 if (name[0] == 'w' && lnum)
16429 {
16430 pos.col = 0;
16431 if (name[1] == '0') /* "w0": first visible line */
16432 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016433 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016434 pos.lnum = curwin->w_topline;
16435 return &pos;
16436 }
16437 else if (name[1] == '$') /* "w$": last visible line */
16438 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016439 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016440 pos.lnum = curwin->w_botline - 1;
16441 return &pos;
16442 }
16443 }
16444 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 {
16446 if (lnum)
16447 {
16448 pos.lnum = curbuf->b_ml.ml_line_count;
16449 pos.col = 0;
16450 }
16451 else
16452 {
16453 pos.lnum = curwin->w_cursor.lnum;
16454 pos.col = (colnr_T)STRLEN(ml_get_curline());
16455 }
16456 return &pos;
16457 }
16458 return NULL;
16459}
16460
16461/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016462 * Convert list in "arg" into a position and optional file number.
16463 * When "fnump" is NULL there is no file number, only 3 items.
16464 * Note that the column is passed on as-is, the caller may want to decrement
16465 * it to use 1 for the first column.
16466 * Return FAIL when conversion is not possible, doesn't check the position for
16467 * validity.
16468 */
16469 static int
16470list2fpos(arg, posp, fnump)
16471 typval_T *arg;
16472 pos_T *posp;
16473 int *fnump;
16474{
16475 list_T *l = arg->vval.v_list;
16476 long i = 0;
16477 long n;
16478
Bram Moolenaarbde35262006-07-23 20:12:24 +000016479 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16480 * when "fnump" isn't NULL and "coladd" is optional. */
16481 if (arg->v_type != VAR_LIST
16482 || l == NULL
16483 || l->lv_len < (fnump == NULL ? 2 : 3)
16484 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016485 return FAIL;
16486
16487 if (fnump != NULL)
16488 {
16489 n = list_find_nr(l, i++, NULL); /* fnum */
16490 if (n < 0)
16491 return FAIL;
16492 if (n == 0)
16493 n = curbuf->b_fnum; /* current buffer */
16494 *fnump = n;
16495 }
16496
16497 n = list_find_nr(l, i++, NULL); /* lnum */
16498 if (n < 0)
16499 return FAIL;
16500 posp->lnum = n;
16501
16502 n = list_find_nr(l, i++, NULL); /* col */
16503 if (n < 0)
16504 return FAIL;
16505 posp->col = n;
16506
16507#ifdef FEAT_VIRTUALEDIT
16508 n = list_find_nr(l, i, NULL);
16509 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016510 posp->coladd = 0;
16511 else
16512 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016513#endif
16514
16515 return OK;
16516}
16517
16518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 * Get the length of an environment variable name.
16520 * Advance "arg" to the first character after the name.
16521 * Return 0 for error.
16522 */
16523 static int
16524get_env_len(arg)
16525 char_u **arg;
16526{
16527 char_u *p;
16528 int len;
16529
16530 for (p = *arg; vim_isIDc(*p); ++p)
16531 ;
16532 if (p == *arg) /* no name found */
16533 return 0;
16534
16535 len = (int)(p - *arg);
16536 *arg = p;
16537 return len;
16538}
16539
16540/*
16541 * Get the length of the name of a function or internal variable.
16542 * "arg" is advanced to the first non-white character after the name.
16543 * Return 0 if something is wrong.
16544 */
16545 static int
16546get_id_len(arg)
16547 char_u **arg;
16548{
16549 char_u *p;
16550 int len;
16551
16552 /* Find the end of the name. */
16553 for (p = *arg; eval_isnamec(*p); ++p)
16554 ;
16555 if (p == *arg) /* no name found */
16556 return 0;
16557
16558 len = (int)(p - *arg);
16559 *arg = skipwhite(p);
16560
16561 return len;
16562}
16563
16564/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016565 * Get the length of the name of a variable or function.
16566 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016568 * Return -1 if curly braces expansion failed.
16569 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 * If the name contains 'magic' {}'s, expand them and return the
16571 * expanded name in an allocated string via 'alias' - caller must free.
16572 */
16573 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 char_u **arg;
16576 char_u **alias;
16577 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016578 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579{
16580 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 char_u *p;
16582 char_u *expr_start;
16583 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584
16585 *alias = NULL; /* default to no alias */
16586
16587 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16588 && (*arg)[2] == (int)KE_SNR)
16589 {
16590 /* hard coded <SNR>, already translated */
16591 *arg += 3;
16592 return get_id_len(arg) + 3;
16593 }
16594 len = eval_fname_script(*arg);
16595 if (len > 0)
16596 {
16597 /* literal "<SID>", "s:" or "<SNR>" */
16598 *arg += len;
16599 }
16600
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016602 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016604 p = find_name_end(*arg, &expr_start, &expr_end,
16605 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 if (expr_start != NULL)
16607 {
16608 char_u *temp_string;
16609
16610 if (!evaluate)
16611 {
16612 len += (int)(p - *arg);
16613 *arg = skipwhite(p);
16614 return len;
16615 }
16616
16617 /*
16618 * Include any <SID> etc in the expanded string:
16619 * Thus the -len here.
16620 */
16621 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16622 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016623 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 *alias = temp_string;
16625 *arg = skipwhite(p);
16626 return (int)STRLEN(temp_string);
16627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628
16629 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016630 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 EMSG2(_(e_invexpr2), *arg);
16632
16633 return len;
16634}
16635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636/*
16637 * Find the end of a variable or function name, taking care of magic braces.
16638 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16639 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016640 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641 * Return a pointer to just after the name. Equal to "arg" if there is no
16642 * valid name.
16643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016645find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 char_u *arg;
16647 char_u **expr_start;
16648 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016649 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 int mb_nest = 0;
16652 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 char_u *p;
16654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016657 *expr_start = NULL;
16658 *expr_end = NULL;
16659 }
16660
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016661 /* Quick check for valid starting character. */
16662 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16663 return arg;
16664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 for (p = arg; *p != NUL
16666 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016667 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016668 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016669 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016670 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016672 if (*p == '\'')
16673 {
16674 /* skip over 'string' to avoid counting [ and ] inside it. */
16675 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16676 ;
16677 if (*p == NUL)
16678 break;
16679 }
16680 else if (*p == '"')
16681 {
16682 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16683 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16684 if (*p == '\\' && p[1] != NUL)
16685 ++p;
16686 if (*p == NUL)
16687 break;
16688 }
16689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016690 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692 if (*p == '[')
16693 ++br_nest;
16694 else if (*p == ']')
16695 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016698 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700 if (*p == '{')
16701 {
16702 mb_nest++;
16703 if (expr_start != NULL && *expr_start == NULL)
16704 *expr_start = p;
16705 }
16706 else if (*p == '}')
16707 {
16708 mb_nest--;
16709 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16710 *expr_end = p;
16711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 }
16714
16715 return p;
16716}
16717
16718/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016719 * Expands out the 'magic' {}'s in a variable/function name.
16720 * Note that this can call itself recursively, to deal with
16721 * constructs like foo{bar}{baz}{bam}
16722 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16723 * "in_start" ^
16724 * "expr_start" ^
16725 * "expr_end" ^
16726 * "in_end" ^
16727 *
16728 * Returns a new allocated string, which the caller must free.
16729 * Returns NULL for failure.
16730 */
16731 static char_u *
16732make_expanded_name(in_start, expr_start, expr_end, in_end)
16733 char_u *in_start;
16734 char_u *expr_start;
16735 char_u *expr_end;
16736 char_u *in_end;
16737{
16738 char_u c1;
16739 char_u *retval = NULL;
16740 char_u *temp_result;
16741 char_u *nextcmd = NULL;
16742
16743 if (expr_end == NULL || in_end == NULL)
16744 return NULL;
16745 *expr_start = NUL;
16746 *expr_end = NUL;
16747 c1 = *in_end;
16748 *in_end = NUL;
16749
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016750 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016751 if (temp_result != NULL && nextcmd == NULL)
16752 {
16753 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16754 + (in_end - expr_end) + 1));
16755 if (retval != NULL)
16756 {
16757 STRCPY(retval, in_start);
16758 STRCAT(retval, temp_result);
16759 STRCAT(retval, expr_end + 1);
16760 }
16761 }
16762 vim_free(temp_result);
16763
16764 *in_end = c1; /* put char back for error messages */
16765 *expr_start = '{';
16766 *expr_end = '}';
16767
16768 if (retval != NULL)
16769 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016770 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016771 if (expr_start != NULL)
16772 {
16773 /* Further expansion! */
16774 temp_result = make_expanded_name(retval, expr_start,
16775 expr_end, temp_result);
16776 vim_free(retval);
16777 retval = temp_result;
16778 }
16779 }
16780
16781 return retval;
16782}
16783
16784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016786 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 */
16788 static int
16789eval_isnamec(c)
16790 int c;
16791{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016792 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16793}
16794
16795/*
16796 * Return TRUE if character "c" can be used as the first character in a
16797 * variable or function name (excluding '{' and '}').
16798 */
16799 static int
16800eval_isnamec1(c)
16801 int c;
16802{
16803 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804}
16805
16806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 * Set number v: variable to "val".
16808 */
16809 void
16810set_vim_var_nr(idx, val)
16811 int idx;
16812 long val;
16813{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016814 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815}
16816
16817/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016818 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 */
16820 long
16821get_vim_var_nr(idx)
16822 int idx;
16823{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016824 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825}
16826
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016827#if defined(FEAT_AUTOCMD) || defined(PROTO)
16828/*
16829 * Get string v: variable value. Uses a static buffer, can only be used once.
16830 */
16831 char_u *
16832get_vim_var_str(idx)
16833 int idx;
16834{
16835 return get_tv_string(&vimvars[idx].vv_tv);
16836}
16837#endif
16838
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839/*
16840 * Set v:count, v:count1 and v:prevcount.
16841 */
16842 void
16843set_vcount(count, count1)
16844 long count;
16845 long count1;
16846{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016847 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16848 vimvars[VV_COUNT].vv_nr = count;
16849 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850}
16851
16852/*
16853 * Set string v: variable to a copy of "val".
16854 */
16855 void
16856set_vim_var_string(idx, val, len)
16857 int idx;
16858 char_u *val;
16859 int len; /* length of "val" to use or -1 (whole string) */
16860{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016861 /* Need to do this (at least) once, since we can't initialize a union.
16862 * Will always be invoked when "v:progname" is set. */
16863 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16864
Bram Moolenaare9a41262005-01-15 22:18:47 +000016865 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016867 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016869 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016871 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872}
16873
16874/*
16875 * Set v:register if needed.
16876 */
16877 void
16878set_reg_var(c)
16879 int c;
16880{
16881 char_u regname;
16882
16883 if (c == 0 || c == ' ')
16884 regname = '"';
16885 else
16886 regname = c;
16887 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016888 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 set_vim_var_string(VV_REG, &regname, 1);
16890}
16891
16892/*
16893 * Get or set v:exception. If "oldval" == NULL, return the current value.
16894 * Otherwise, restore the value to "oldval" and return NULL.
16895 * Must always be called in pairs to save and restore v:exception! Does not
16896 * take care of memory allocations.
16897 */
16898 char_u *
16899v_exception(oldval)
16900 char_u *oldval;
16901{
16902 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016903 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904
Bram Moolenaare9a41262005-01-15 22:18:47 +000016905 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 return NULL;
16907}
16908
16909/*
16910 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16911 * Otherwise, restore the value to "oldval" and return NULL.
16912 * Must always be called in pairs to save and restore v:throwpoint! Does not
16913 * take care of memory allocations.
16914 */
16915 char_u *
16916v_throwpoint(oldval)
16917 char_u *oldval;
16918{
16919 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016920 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921
Bram Moolenaare9a41262005-01-15 22:18:47 +000016922 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 return NULL;
16924}
16925
16926#if defined(FEAT_AUTOCMD) || defined(PROTO)
16927/*
16928 * Set v:cmdarg.
16929 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16930 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16931 * Must always be called in pairs!
16932 */
16933 char_u *
16934set_cmdarg(eap, oldarg)
16935 exarg_T *eap;
16936 char_u *oldarg;
16937{
16938 char_u *oldval;
16939 char_u *newval;
16940 unsigned len;
16941
Bram Moolenaare9a41262005-01-15 22:18:47 +000016942 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016943 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016945 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016946 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016947 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 }
16949
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016950 if (eap->force_bin == FORCE_BIN)
16951 len = 6;
16952 else if (eap->force_bin == FORCE_NOBIN)
16953 len = 8;
16954 else
16955 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016956
16957 if (eap->read_edit)
16958 len += 7;
16959
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016960 if (eap->force_ff != 0)
16961 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16962# ifdef FEAT_MBYTE
16963 if (eap->force_enc != 0)
16964 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016965 if (eap->bad_char != 0)
16966 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016967# endif
16968
16969 newval = alloc(len + 1);
16970 if (newval == NULL)
16971 return NULL;
16972
16973 if (eap->force_bin == FORCE_BIN)
16974 sprintf((char *)newval, " ++bin");
16975 else if (eap->force_bin == FORCE_NOBIN)
16976 sprintf((char *)newval, " ++nobin");
16977 else
16978 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016979
16980 if (eap->read_edit)
16981 STRCAT(newval, " ++edit");
16982
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016983 if (eap->force_ff != 0)
16984 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16985 eap->cmd + eap->force_ff);
16986# ifdef FEAT_MBYTE
16987 if (eap->force_enc != 0)
16988 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16989 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016990 if (eap->bad_char != 0)
16991 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16992 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016993# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016994 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016995 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996}
16997#endif
16998
16999/*
17000 * Get the value of internal variable "name".
17001 * Return OK or FAIL.
17002 */
17003 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017004get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 char_u *name;
17006 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017008 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009{
17010 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017011 typval_T *tv = NULL;
17012 typval_T atv;
17013 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015
17016 /* truncate the name, so that we can use strcmp() */
17017 cc = name[len];
17018 name[len] = NUL;
17019
17020 /*
17021 * Check for "b:changedtick".
17022 */
17023 if (STRCMP(name, "b:changedtick") == 0)
17024 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017025 atv.v_type = VAR_NUMBER;
17026 atv.vval.v_number = curbuf->b_changedtick;
17027 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 }
17029
17030 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 * Check for user-defined variables.
17032 */
17033 else
17034 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017035 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 }
17039
Bram Moolenaare9a41262005-01-15 22:18:47 +000017040 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017042 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 ret = FAIL;
17045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017046 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017047 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048
17049 name[len] = cc;
17050
17051 return ret;
17052}
17053
17054/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017055 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17056 * Also handle function call with Funcref variable: func(expr)
17057 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17058 */
17059 static int
17060handle_subscript(arg, rettv, evaluate, verbose)
17061 char_u **arg;
17062 typval_T *rettv;
17063 int evaluate; /* do more than finding the end */
17064 int verbose; /* give error messages */
17065{
17066 int ret = OK;
17067 dict_T *selfdict = NULL;
17068 char_u *s;
17069 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017070 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017071
17072 while (ret == OK
17073 && (**arg == '['
17074 || (**arg == '.' && rettv->v_type == VAR_DICT)
17075 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17076 && !vim_iswhite(*(*arg - 1)))
17077 {
17078 if (**arg == '(')
17079 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017080 /* need to copy the funcref so that we can clear rettv */
17081 functv = *rettv;
17082 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017083
17084 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017085 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017086 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017087 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17088 &len, evaluate, selfdict);
17089
17090 /* Clear the funcref afterwards, so that deleting it while
17091 * evaluating the arguments is possible (see test55). */
17092 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017093
17094 /* Stop the expression evaluation when immediately aborting on
17095 * error, or when an interrupt occurred or an exception was thrown
17096 * but not caught. */
17097 if (aborting())
17098 {
17099 if (ret == OK)
17100 clear_tv(rettv);
17101 ret = FAIL;
17102 }
17103 dict_unref(selfdict);
17104 selfdict = NULL;
17105 }
17106 else /* **arg == '[' || **arg == '.' */
17107 {
17108 dict_unref(selfdict);
17109 if (rettv->v_type == VAR_DICT)
17110 {
17111 selfdict = rettv->vval.v_dict;
17112 if (selfdict != NULL)
17113 ++selfdict->dv_refcount;
17114 }
17115 else
17116 selfdict = NULL;
17117 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17118 {
17119 clear_tv(rettv);
17120 ret = FAIL;
17121 }
17122 }
17123 }
17124 dict_unref(selfdict);
17125 return ret;
17126}
17127
17128/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017129 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17130 * value).
17131 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017134{
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017136}
17137
17138/*
17139 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 * The string "s" must have been allocated, it is consumed.
17141 * Return NULL for out of memory, the variable otherwise.
17142 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017144alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 char_u *s;
17146{
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149 rettv = alloc_tv();
17150 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017152 rettv->v_type = VAR_STRING;
17153 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 }
17155 else
17156 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017157 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158}
17159
17160/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017163 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017164free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017165 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166{
17167 if (varp != NULL)
17168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017169 switch (varp->v_type)
17170 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017171 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017172 func_unref(varp->vval.v_string);
17173 /*FALLTHROUGH*/
17174 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017175 vim_free(varp->vval.v_string);
17176 break;
17177 case VAR_LIST:
17178 list_unref(varp->vval.v_list);
17179 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017180 case VAR_DICT:
17181 dict_unref(varp->vval.v_dict);
17182 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017183 case VAR_NUMBER:
17184 case VAR_UNKNOWN:
17185 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017186 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017187 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017188 break;
17189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 vim_free(varp);
17191 }
17192}
17193
17194/*
17195 * Free the memory for a variable value and set the value to NULL or 0.
17196 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017197 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017198clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200{
17201 if (varp != NULL)
17202 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017203 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017205 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017206 func_unref(varp->vval.v_string);
17207 /*FALLTHROUGH*/
17208 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017209 vim_free(varp->vval.v_string);
17210 varp->vval.v_string = NULL;
17211 break;
17212 case VAR_LIST:
17213 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017214 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017215 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017216 case VAR_DICT:
17217 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017218 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017219 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017220 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017221 varp->vval.v_number = 0;
17222 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017223 case VAR_UNKNOWN:
17224 break;
17225 default:
17226 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017228 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 }
17230}
17231
17232/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017233 * Set the value of a variable to NULL without freeing items.
17234 */
17235 static void
17236init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017237 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238{
17239 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241}
17242
17243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 * Get the number value of a variable.
17245 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017246 * For incompatible types, return 0.
17247 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17248 * caller of incompatible types: it sets *denote to TRUE if "denote"
17249 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 */
17251 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017252get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017253 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 int error = FALSE;
17256
17257 return get_tv_number_chk(varp, &error); /* return 0L on error */
17258}
17259
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017260 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017261get_tv_number_chk(varp, denote)
17262 typval_T *varp;
17263 int *denote;
17264{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017265 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017267 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017269 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017270 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017272 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 break;
17274 case VAR_STRING:
17275 if (varp->vval.v_string != NULL)
17276 vim_str2nr(varp->vval.v_string, NULL, NULL,
17277 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017278 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017279 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017280 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017281 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017282 case VAR_DICT:
17283 EMSG(_("E728: Using a Dictionary as a number"));
17284 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017285 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017286 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017289 if (denote == NULL) /* useful for values that must be unsigned */
17290 n = -1;
17291 else
17292 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294}
17295
17296/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017297 * Get the lnum from the first argument.
17298 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 */
17301 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017303 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304{
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 linenr_T lnum;
17307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017308 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 if (lnum == 0) /* no valid number, try using line() */
17310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 rettv.v_type = VAR_NUMBER;
17312 f_line(argvars, &rettv);
17313 lnum = rettv.vval.v_number;
17314 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
17316 return lnum;
17317}
17318
17319/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017320 * Get the lnum from the first argument.
17321 * Also accepts "$", then "buf" is used.
17322 * Returns 0 on error.
17323 */
17324 static linenr_T
17325get_tv_lnum_buf(argvars, buf)
17326 typval_T *argvars;
17327 buf_T *buf;
17328{
17329 if (argvars[0].v_type == VAR_STRING
17330 && argvars[0].vval.v_string != NULL
17331 && argvars[0].vval.v_string[0] == '$'
17332 && buf != NULL)
17333 return buf->b_ml.ml_line_count;
17334 return get_tv_number_chk(&argvars[0], NULL);
17335}
17336
17337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 * Get the string value of a variable.
17339 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017340 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17341 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 * If the String variable has never been set, return an empty string.
17343 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017344 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17345 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 */
17347 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350{
17351 static char_u mybuf[NUMBUFLEN];
17352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017353 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354}
17355
17356 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017357get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017359 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017361 char_u *res = get_tv_string_buf_chk(varp, buf);
17362
17363 return res != NULL ? res : (char_u *)"";
17364}
17365
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017366 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367get_tv_string_chk(varp)
17368 typval_T *varp;
17369{
17370 static char_u mybuf[NUMBUFLEN];
17371
17372 return get_tv_string_buf_chk(varp, mybuf);
17373}
17374
17375 static char_u *
17376get_tv_string_buf_chk(varp, buf)
17377 typval_T *varp;
17378 char_u *buf;
17379{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017380 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017382 case VAR_NUMBER:
17383 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17384 return buf;
17385 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017386 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017387 break;
17388 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017389 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017390 break;
17391 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017392 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393 break;
17394 case VAR_STRING:
17395 if (varp->vval.v_string != NULL)
17396 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017397 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017398 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017399 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403}
17404
17405/*
17406 * Find variable "name" in the list of variables.
17407 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017408 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017409 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017412 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017413find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017418 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
Bram Moolenaara7043832005-01-21 11:56:39 +000017420 ht = find_var_ht(name, &varname);
17421 if (htp != NULL)
17422 *htp = ht;
17423 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017425 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
17428/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017429 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017430 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017433find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017434 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017435 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017436 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017437{
Bram Moolenaar33570922005-01-25 22:26:29 +000017438 hashitem_T *hi;
17439
17440 if (*varname == NUL)
17441 {
17442 /* Must be something like "s:", otherwise "ht" would be NULL. */
17443 switch (varname[-2])
17444 {
17445 case 's': return &SCRIPT_SV(current_SID).sv_var;
17446 case 'g': return &globvars_var;
17447 case 'v': return &vimvars_var;
17448 case 'b': return &curbuf->b_bufvar;
17449 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017450#ifdef FEAT_WINDOWS
17451 case 't': return &curtab->tp_winvar;
17452#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017453 case 'l': return current_funccal == NULL
17454 ? NULL : &current_funccal->l_vars_var;
17455 case 'a': return current_funccal == NULL
17456 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 }
17458 return NULL;
17459 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017460
17461 hi = hash_find(ht, varname);
17462 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017463 {
17464 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017465 * worked find the variable again. Don't auto-load a script if it was
17466 * loaded already, otherwise it would be loaded every time when
17467 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017468 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017469 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017470 hi = hash_find(ht, varname);
17471 if (HASHITEM_EMPTY(hi))
17472 return NULL;
17473 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017475}
17476
17477/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017479 * Set "varname" to the start of name without ':'.
17480 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017482find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 char_u *name;
17484 char_u **varname;
17485{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017486 hashitem_T *hi;
17487
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 if (name[1] != ':')
17489 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017490 /* The name must not start with a colon or #. */
17491 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 return NULL;
17493 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017494
17495 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017496 hi = hash_find(&compat_hashtab, name);
17497 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017498 return &compat_hashtab;
17499
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 return &globvarht; /* global variable */
17502 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 }
17504 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017505 if (*name == 'g') /* global variable */
17506 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017507 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17508 */
17509 if (vim_strchr(name + 2, ':') != NULL
17510 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017511 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017513 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017516#ifdef FEAT_WINDOWS
17517 if (*name == 't') /* tab page variable */
17518 return &curtab->tp_vars.dv_hashtab;
17519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 if (*name == 'v') /* v: variable */
17521 return &vimvarht;
17522 if (*name == 'a' && current_funccal != NULL) /* function argument */
17523 return &current_funccal->l_avars.dv_hashtab;
17524 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17525 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 if (*name == 's' /* script variable */
17527 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17528 return &SCRIPT_VARS(current_SID);
17529 return NULL;
17530}
17531
17532/*
17533 * Get the string value of a (global/local) variable.
17534 * Returns NULL when it doesn't exist.
17535 */
17536 char_u *
17537get_var_value(name)
17538 char_u *name;
17539{
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541
Bram Moolenaara7043832005-01-21 11:56:39 +000017542 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 if (v == NULL)
17544 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017545 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546}
17547
17548/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 * sourcing this script and when executing functions defined in the script.
17551 */
17552 void
17553new_script_vars(id)
17554 scid_T id;
17555{
Bram Moolenaara7043832005-01-21 11:56:39 +000017556 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017557 hashtab_T *ht;
17558 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017559
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17561 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017562 /* Re-allocating ga_data means that an ht_array pointing to
17563 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017565 for (i = 1; i <= ga_scripts.ga_len; ++i)
17566 {
17567 ht = &SCRIPT_VARS(i);
17568 if (ht->ht_mask == HT_INIT_SIZE - 1)
17569 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017570 sv = &SCRIPT_SV(i);
17571 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017572 }
17573
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 while (ga_scripts.ga_len < id)
17575 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17577 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 }
17580 }
17581}
17582
17583/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17585 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 */
17587 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017588init_var_dict(dict, dict_var)
17589 dict_T *dict;
17590 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591{
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 hash_init(&dict->dv_hashtab);
17593 dict->dv_refcount = 99999;
17594 dict_var->di_tv.vval.v_dict = dict;
17595 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017596 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17598 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599}
17600
17601/*
17602 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017603 * Frees all allocated variables and the value they contain.
17604 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 */
17606 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017607vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 hashtab_T *ht;
17609{
17610 vars_clear_ext(ht, TRUE);
17611}
17612
17613/*
17614 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17615 */
17616 static void
17617vars_clear_ext(ht, free_val)
17618 hashtab_T *ht;
17619 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620{
Bram Moolenaara7043832005-01-21 11:56:39 +000017621 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 hashitem_T *hi;
17623 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624
Bram Moolenaar33570922005-01-25 22:26:29 +000017625 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017626 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017627 for (hi = ht->ht_array; todo > 0; ++hi)
17628 {
17629 if (!HASHITEM_EMPTY(hi))
17630 {
17631 --todo;
17632
Bram Moolenaar33570922005-01-25 22:26:29 +000017633 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017634 * ht_array might change then. hash_clear() takes care of it
17635 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017636 v = HI2DI(hi);
17637 if (free_val)
17638 clear_tv(&v->di_tv);
17639 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17640 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017641 }
17642 }
17643 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017644 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645}
17646
Bram Moolenaara7043832005-01-21 11:56:39 +000017647/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017648 * Delete a variable from hashtab "ht" at item "hi".
17649 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017652delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 hashtab_T *ht;
17654 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655{
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017657
17658 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 clear_tv(&di->di_tv);
17660 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661}
17662
17663/*
17664 * List the value of one internal variable.
17665 */
17666 static void
17667list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 char_u *prefix;
17670{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671 char_u *tofree;
17672 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017673 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017674
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017675 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017676 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017677 s == NULL ? (char_u *)"" : s);
17678 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679}
17680
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 static void
17682list_one_var_a(prefix, name, type, string)
17683 char_u *prefix;
17684 char_u *name;
17685 int type;
17686 char_u *string;
17687{
17688 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17689 if (name != NULL) /* "a:" vars don't have a name stored */
17690 msg_puts(name);
17691 msg_putchar(' ');
17692 msg_advance(22);
17693 if (type == VAR_NUMBER)
17694 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017695 else if (type == VAR_FUNC)
17696 msg_putchar('*');
17697 else if (type == VAR_LIST)
17698 {
17699 msg_putchar('[');
17700 if (*string == '[')
17701 ++string;
17702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017703 else if (type == VAR_DICT)
17704 {
17705 msg_putchar('{');
17706 if (*string == '{')
17707 ++string;
17708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 else
17710 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017711
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017713
17714 if (type == VAR_FUNC)
17715 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716}
17717
17718/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 * If the variable already exists, the value is updated.
17721 * Otherwise the variable is created.
17722 */
17723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017724set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728{
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017731 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017732 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017734 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017735 {
17736 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17737 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17738 ? name[2] : name[0]))
17739 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017740 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017741 return;
17742 }
17743 if (function_exists(name))
17744 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017745 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017746 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 return;
17748 }
17749 }
17750
Bram Moolenaara7043832005-01-21 11:56:39 +000017751 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017752 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017753 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017754 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017755 return;
17756 }
17757
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017758 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017761 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017762 if (var_check_ro(v->di_flags, name)
17763 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 return;
17765 if (v->di_tv.v_type != tv->v_type
17766 && !((v->di_tv.v_type == VAR_STRING
17767 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 && (tv->v_type == VAR_STRING
17769 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017770 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017771 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017772 return;
17773 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017774
17775 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017776 * Handle setting internal v: variables separately: we don't change
17777 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 */
17779 if (ht == &vimvarht)
17780 {
17781 if (v->di_tv.v_type == VAR_STRING)
17782 {
17783 vim_free(v->di_tv.vval.v_string);
17784 if (copy || tv->v_type != VAR_STRING)
17785 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17786 else
17787 {
17788 /* Take over the string to avoid an extra alloc/free. */
17789 v->di_tv.vval.v_string = tv->vval.v_string;
17790 tv->vval.v_string = NULL;
17791 }
17792 }
17793 else if (v->di_tv.v_type != VAR_NUMBER)
17794 EMSG2(_(e_intern2), "set_var()");
17795 else
17796 v->di_tv.vval.v_number = get_tv_number(tv);
17797 return;
17798 }
17799
17800 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 }
17802 else /* add a new variable */
17803 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017804 /* Can't add "v:" variable. */
17805 if (ht == &vimvarht)
17806 {
17807 EMSG2(_(e_illvar), name);
17808 return;
17809 }
17810
Bram Moolenaar92124a32005-06-17 22:03:40 +000017811 /* Make sure the variable name is valid. */
17812 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017813 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17814 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017815 {
17816 EMSG2(_(e_illvar), varname);
17817 return;
17818 }
17819
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017820 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17821 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017822 if (v == NULL)
17823 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017824 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017825 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017827 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 return;
17829 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017830 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017834 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017835 else
17836 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017837 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017838 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841}
17842
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017843/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017844 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017845 * Also give an error message.
17846 */
17847 static int
17848var_check_ro(flags, name)
17849 int flags;
17850 char_u *name;
17851{
17852 if (flags & DI_FLAGS_RO)
17853 {
17854 EMSG2(_(e_readonlyvar), name);
17855 return TRUE;
17856 }
17857 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17858 {
17859 EMSG2(_(e_readonlysbx), name);
17860 return TRUE;
17861 }
17862 return FALSE;
17863}
17864
17865/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017866 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17867 * Also give an error message.
17868 */
17869 static int
17870var_check_fixed(flags, name)
17871 int flags;
17872 char_u *name;
17873{
17874 if (flags & DI_FLAGS_FIX)
17875 {
17876 EMSG2(_("E795: Cannot delete variable %s"), name);
17877 return TRUE;
17878 }
17879 return FALSE;
17880}
17881
17882/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017883 * Return TRUE if typeval "tv" is set to be locked (immutable).
17884 * Also give an error message, using "name".
17885 */
17886 static int
17887tv_check_lock(lock, name)
17888 int lock;
17889 char_u *name;
17890{
17891 if (lock & VAR_LOCKED)
17892 {
17893 EMSG2(_("E741: Value is locked: %s"),
17894 name == NULL ? (char_u *)_("Unknown") : name);
17895 return TRUE;
17896 }
17897 if (lock & VAR_FIXED)
17898 {
17899 EMSG2(_("E742: Cannot change value of %s"),
17900 name == NULL ? (char_u *)_("Unknown") : name);
17901 return TRUE;
17902 }
17903 return FALSE;
17904}
17905
17906/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017907 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017908 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017909 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017912copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017913 typval_T *from;
17914 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017916 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017917 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017918 switch (from->v_type)
17919 {
17920 case VAR_NUMBER:
17921 to->vval.v_number = from->vval.v_number;
17922 break;
17923 case VAR_STRING:
17924 case VAR_FUNC:
17925 if (from->vval.v_string == NULL)
17926 to->vval.v_string = NULL;
17927 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017928 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017929 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017930 if (from->v_type == VAR_FUNC)
17931 func_ref(to->vval.v_string);
17932 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017933 break;
17934 case VAR_LIST:
17935 if (from->vval.v_list == NULL)
17936 to->vval.v_list = NULL;
17937 else
17938 {
17939 to->vval.v_list = from->vval.v_list;
17940 ++to->vval.v_list->lv_refcount;
17941 }
17942 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017943 case VAR_DICT:
17944 if (from->vval.v_dict == NULL)
17945 to->vval.v_dict = NULL;
17946 else
17947 {
17948 to->vval.v_dict = from->vval.v_dict;
17949 ++to->vval.v_dict->dv_refcount;
17950 }
17951 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017952 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017954 break;
17955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956}
17957
17958/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017959 * Make a copy of an item.
17960 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017961 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17962 * reference to an already copied list/dict can be used.
17963 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017964 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017965 static int
17966item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 typval_T *from;
17968 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017969 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017970 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017971{
17972 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017973 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017974
Bram Moolenaar33570922005-01-25 22:26:29 +000017975 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017976 {
17977 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017978 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017979 }
17980 ++recurse;
17981
17982 switch (from->v_type)
17983 {
17984 case VAR_NUMBER:
17985 case VAR_STRING:
17986 case VAR_FUNC:
17987 copy_tv(from, to);
17988 break;
17989 case VAR_LIST:
17990 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017991 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017992 if (from->vval.v_list == NULL)
17993 to->vval.v_list = NULL;
17994 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17995 {
17996 /* use the copy made earlier */
17997 to->vval.v_list = from->vval.v_list->lv_copylist;
17998 ++to->vval.v_list->lv_refcount;
17999 }
18000 else
18001 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18002 if (to->vval.v_list == NULL)
18003 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018004 break;
18005 case VAR_DICT:
18006 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018007 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018008 if (from->vval.v_dict == NULL)
18009 to->vval.v_dict = NULL;
18010 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18011 {
18012 /* use the copy made earlier */
18013 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18014 ++to->vval.v_dict->dv_refcount;
18015 }
18016 else
18017 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18018 if (to->vval.v_dict == NULL)
18019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018020 break;
18021 default:
18022 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018023 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018024 }
18025 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018026 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018027}
18028
18029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 * ":echo expr1 ..." print each argument separated with a space, add a
18031 * newline at the end.
18032 * ":echon expr1 ..." print each argument plain.
18033 */
18034 void
18035ex_echo(eap)
18036 exarg_T *eap;
18037{
18038 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018039 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018040 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 char_u *p;
18042 int needclr = TRUE;
18043 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018044 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045
18046 if (eap->skip)
18047 ++emsg_skip;
18048 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18049 {
18050 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 {
18053 /*
18054 * Report the invalid expression unless the expression evaluation
18055 * has been cancelled due to an aborting error, an interrupt, or an
18056 * exception.
18057 */
18058 if (!aborting())
18059 EMSG2(_(e_invexpr2), p);
18060 break;
18061 }
18062 if (!eap->skip)
18063 {
18064 if (atstart)
18065 {
18066 atstart = FALSE;
18067 /* Call msg_start() after eval1(), evaluating the expression
18068 * may cause a message to appear. */
18069 if (eap->cmdidx == CMD_echo)
18070 msg_start();
18071 }
18072 else if (eap->cmdidx == CMD_echo)
18073 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018074 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018075 if (p != NULL)
18076 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018078 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018080 if (*p != TAB && needclr)
18081 {
18082 /* remove any text still there from the command */
18083 msg_clr_eos();
18084 needclr = FALSE;
18085 }
18086 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 }
18088 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018089 {
18090#ifdef FEAT_MBYTE
18091 if (has_mbyte)
18092 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018093 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018094
18095 (void)msg_outtrans_len_attr(p, i, echo_attr);
18096 p += i - 1;
18097 }
18098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018100 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018103 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018105 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 arg = skipwhite(arg);
18107 }
18108 eap->nextcmd = check_nextcmd(arg);
18109
18110 if (eap->skip)
18111 --emsg_skip;
18112 else
18113 {
18114 /* remove text that may still be there from the command */
18115 if (needclr)
18116 msg_clr_eos();
18117 if (eap->cmdidx == CMD_echo)
18118 msg_end();
18119 }
18120}
18121
18122/*
18123 * ":echohl {name}".
18124 */
18125 void
18126ex_echohl(eap)
18127 exarg_T *eap;
18128{
18129 int id;
18130
18131 id = syn_name2id(eap->arg);
18132 if (id == 0)
18133 echo_attr = 0;
18134 else
18135 echo_attr = syn_id2attr(id);
18136}
18137
18138/*
18139 * ":execute expr1 ..." execute the result of an expression.
18140 * ":echomsg expr1 ..." Print a message
18141 * ":echoerr expr1 ..." Print an error
18142 * Each gets spaces around each argument and a newline at the end for
18143 * echo commands
18144 */
18145 void
18146ex_execute(eap)
18147 exarg_T *eap;
18148{
18149 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018150 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 int ret = OK;
18152 char_u *p;
18153 garray_T ga;
18154 int len;
18155 int save_did_emsg;
18156
18157 ga_init2(&ga, 1, 80);
18158
18159 if (eap->skip)
18160 ++emsg_skip;
18161 while (*arg != NUL && *arg != '|' && *arg != '\n')
18162 {
18163 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018164 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 {
18166 /*
18167 * Report the invalid expression unless the expression evaluation
18168 * has been cancelled due to an aborting error, an interrupt, or an
18169 * exception.
18170 */
18171 if (!aborting())
18172 EMSG2(_(e_invexpr2), p);
18173 ret = FAIL;
18174 break;
18175 }
18176
18177 if (!eap->skip)
18178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018179 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 len = (int)STRLEN(p);
18181 if (ga_grow(&ga, len + 2) == FAIL)
18182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 ret = FAIL;
18185 break;
18186 }
18187 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 ga.ga_len += len;
18191 }
18192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018193 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 arg = skipwhite(arg);
18195 }
18196
18197 if (ret != FAIL && ga.ga_data != NULL)
18198 {
18199 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018202 out_flush();
18203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 else if (eap->cmdidx == CMD_echoerr)
18205 {
18206 /* We don't want to abort following commands, restore did_emsg. */
18207 save_did_emsg = did_emsg;
18208 EMSG((char_u *)ga.ga_data);
18209 if (!force_abort)
18210 did_emsg = save_did_emsg;
18211 }
18212 else if (eap->cmdidx == CMD_execute)
18213 do_cmdline((char_u *)ga.ga_data,
18214 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18215 }
18216
18217 ga_clear(&ga);
18218
18219 if (eap->skip)
18220 --emsg_skip;
18221
18222 eap->nextcmd = check_nextcmd(arg);
18223}
18224
18225/*
18226 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18227 * "arg" points to the "&" or '+' when called, to "option" when returning.
18228 * Returns NULL when no option name found. Otherwise pointer to the char
18229 * after the option name.
18230 */
18231 static char_u *
18232find_option_end(arg, opt_flags)
18233 char_u **arg;
18234 int *opt_flags;
18235{
18236 char_u *p = *arg;
18237
18238 ++p;
18239 if (*p == 'g' && p[1] == ':')
18240 {
18241 *opt_flags = OPT_GLOBAL;
18242 p += 2;
18243 }
18244 else if (*p == 'l' && p[1] == ':')
18245 {
18246 *opt_flags = OPT_LOCAL;
18247 p += 2;
18248 }
18249 else
18250 *opt_flags = 0;
18251
18252 if (!ASCII_ISALPHA(*p))
18253 return NULL;
18254 *arg = p;
18255
18256 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18257 p += 4; /* termcap option */
18258 else
18259 while (ASCII_ISALPHA(*p))
18260 ++p;
18261 return p;
18262}
18263
18264/*
18265 * ":function"
18266 */
18267 void
18268ex_function(eap)
18269 exarg_T *eap;
18270{
18271 char_u *theline;
18272 int j;
18273 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 char_u *name = NULL;
18276 char_u *p;
18277 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018278 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 garray_T newargs;
18280 garray_T newlines;
18281 int varargs = FALSE;
18282 int mustend = FALSE;
18283 int flags = 0;
18284 ufunc_T *fp;
18285 int indent;
18286 int nesting;
18287 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018288 dictitem_T *v;
18289 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018290 static int func_nr = 0; /* number for nameless function */
18291 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018292 hashtab_T *ht;
18293 int todo;
18294 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018295 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296
18297 /*
18298 * ":function" without argument: list functions.
18299 */
18300 if (ends_excmd(*eap->arg))
18301 {
18302 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018303 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018304 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018305 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018306 {
18307 if (!HASHITEM_EMPTY(hi))
18308 {
18309 --todo;
18310 fp = HI2UF(hi);
18311 if (!isdigit(*fp->uf_name))
18312 list_func_head(fp, FALSE);
18313 }
18314 }
18315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 eap->nextcmd = check_nextcmd(eap->arg);
18317 return;
18318 }
18319
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018320 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018321 * ":function /pat": list functions matching pattern.
18322 */
18323 if (*eap->arg == '/')
18324 {
18325 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18326 if (!eap->skip)
18327 {
18328 regmatch_T regmatch;
18329
18330 c = *p;
18331 *p = NUL;
18332 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18333 *p = c;
18334 if (regmatch.regprog != NULL)
18335 {
18336 regmatch.rm_ic = p_ic;
18337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018338 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18340 {
18341 if (!HASHITEM_EMPTY(hi))
18342 {
18343 --todo;
18344 fp = HI2UF(hi);
18345 if (!isdigit(*fp->uf_name)
18346 && vim_regexec(&regmatch, fp->uf_name, 0))
18347 list_func_head(fp, FALSE);
18348 }
18349 }
18350 }
18351 }
18352 if (*p == '/')
18353 ++p;
18354 eap->nextcmd = check_nextcmd(p);
18355 return;
18356 }
18357
18358 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018359 * Get the function name. There are these situations:
18360 * func normal function name
18361 * "name" == func, "fudi.fd_dict" == NULL
18362 * dict.func new dictionary entry
18363 * "name" == NULL, "fudi.fd_dict" set,
18364 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18365 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018366 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018367 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18368 * dict.func existing dict entry that's not a Funcref
18369 * "name" == NULL, "fudi.fd_dict" set,
18370 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018373 name = trans_function_name(&p, eap->skip, 0, &fudi);
18374 paren = (vim_strchr(p, '(') != NULL);
18375 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 {
18377 /*
18378 * Return on an invalid expression in braces, unless the expression
18379 * evaluation has been cancelled due to an aborting error, an
18380 * interrupt, or an exception.
18381 */
18382 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018384 if (!eap->skip && fudi.fd_newkey != NULL)
18385 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018386 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 else
18390 eap->skip = TRUE;
18391 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018392
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 /* An error in a function call during evaluation of an expression in magic
18394 * braces should not cause the function not to be defined. */
18395 saved_did_emsg = did_emsg;
18396 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397
18398 /*
18399 * ":function func" with only function name: list function.
18400 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018401 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 {
18403 if (!ends_excmd(*skipwhite(p)))
18404 {
18405 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018406 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 }
18408 eap->nextcmd = check_nextcmd(p);
18409 if (eap->nextcmd != NULL)
18410 *p = NUL;
18411 if (!eap->skip && !got_int)
18412 {
18413 fp = find_func(name);
18414 if (fp != NULL)
18415 {
18416 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018417 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018419 if (FUNCLINE(fp, j) == NULL)
18420 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 msg_putchar('\n');
18422 msg_outnum((long)(j + 1));
18423 if (j < 9)
18424 msg_putchar(' ');
18425 if (j < 99)
18426 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018427 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 out_flush(); /* show a line at a time */
18429 ui_breakcheck();
18430 }
18431 if (!got_int)
18432 {
18433 msg_putchar('\n');
18434 msg_puts((char_u *)" endfunction");
18435 }
18436 }
18437 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018438 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018440 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 }
18442
18443 /*
18444 * ":function name(arg1, arg2)" Define function.
18445 */
18446 p = skipwhite(p);
18447 if (*p != '(')
18448 {
18449 if (!eap->skip)
18450 {
18451 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018452 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 }
18454 /* attempt to continue by skipping some text */
18455 if (vim_strchr(p, '(') != NULL)
18456 p = vim_strchr(p, '(');
18457 }
18458 p = skipwhite(p + 1);
18459
18460 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18461 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18462
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018463 if (!eap->skip)
18464 {
18465 /* Check the name of the function. */
18466 if (name != NULL)
18467 arg = name;
18468 else
18469 arg = fudi.fd_newkey;
18470 if (arg != NULL)
18471 {
18472 if (*arg == K_SPECIAL)
18473 j = 3;
18474 else
18475 j = 0;
18476 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18477 : eval_isnamec(arg[j])))
18478 ++j;
18479 if (arg[j] != NUL)
18480 emsg_funcname(_(e_invarg2), arg);
18481 }
18482 }
18483
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 /*
18485 * Isolate the arguments: "arg1, arg2, ...)"
18486 */
18487 while (*p != ')')
18488 {
18489 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18490 {
18491 varargs = TRUE;
18492 p += 3;
18493 mustend = TRUE;
18494 }
18495 else
18496 {
18497 arg = p;
18498 while (ASCII_ISALNUM(*p) || *p == '_')
18499 ++p;
18500 if (arg == p || isdigit(*arg)
18501 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18502 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18503 {
18504 if (!eap->skip)
18505 EMSG2(_("E125: Illegal argument: %s"), arg);
18506 break;
18507 }
18508 if (ga_grow(&newargs, 1) == FAIL)
18509 goto erret;
18510 c = *p;
18511 *p = NUL;
18512 arg = vim_strsave(arg);
18513 if (arg == NULL)
18514 goto erret;
18515 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18516 *p = c;
18517 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 if (*p == ',')
18519 ++p;
18520 else
18521 mustend = TRUE;
18522 }
18523 p = skipwhite(p);
18524 if (mustend && *p != ')')
18525 {
18526 if (!eap->skip)
18527 EMSG2(_(e_invarg2), eap->arg);
18528 break;
18529 }
18530 }
18531 ++p; /* skip the ')' */
18532
Bram Moolenaare9a41262005-01-15 22:18:47 +000018533 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 for (;;)
18535 {
18536 p = skipwhite(p);
18537 if (STRNCMP(p, "range", 5) == 0)
18538 {
18539 flags |= FC_RANGE;
18540 p += 5;
18541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018542 else if (STRNCMP(p, "dict", 4) == 0)
18543 {
18544 flags |= FC_DICT;
18545 p += 4;
18546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 else if (STRNCMP(p, "abort", 5) == 0)
18548 {
18549 flags |= FC_ABORT;
18550 p += 5;
18551 }
18552 else
18553 break;
18554 }
18555
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018556 /* When there is a line break use what follows for the function body.
18557 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18558 if (*p == '\n')
18559 line_arg = p + 1;
18560 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 EMSG(_(e_trailing));
18562
18563 /*
18564 * Read the body of the function, until ":endfunction" is found.
18565 */
18566 if (KeyTyped)
18567 {
18568 /* Check if the function already exists, don't let the user type the
18569 * whole function before telling him it doesn't work! For a script we
18570 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018571 if (!eap->skip && !eap->forceit)
18572 {
18573 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18574 EMSG(_(e_funcdict));
18575 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018576 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018579 if (!eap->skip && did_emsg)
18580 goto erret;
18581
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 msg_putchar('\n'); /* don't overwrite the function name */
18583 cmdline_row = msg_row;
18584 }
18585
18586 indent = 2;
18587 nesting = 0;
18588 for (;;)
18589 {
18590 msg_scroll = TRUE;
18591 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018592 sourcing_lnum_off = sourcing_lnum;
18593
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018594 if (line_arg != NULL)
18595 {
18596 /* Use eap->arg, split up in parts by line breaks. */
18597 theline = line_arg;
18598 p = vim_strchr(theline, '\n');
18599 if (p == NULL)
18600 line_arg += STRLEN(line_arg);
18601 else
18602 {
18603 *p = NUL;
18604 line_arg = p + 1;
18605 }
18606 }
18607 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 theline = getcmdline(':', 0L, indent);
18609 else
18610 theline = eap->getline(':', eap->cookie, indent);
18611 if (KeyTyped)
18612 lines_left = Rows - 1;
18613 if (theline == NULL)
18614 {
18615 EMSG(_("E126: Missing :endfunction"));
18616 goto erret;
18617 }
18618
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018619 /* Detect line continuation: sourcing_lnum increased more than one. */
18620 if (sourcing_lnum > sourcing_lnum_off + 1)
18621 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18622 else
18623 sourcing_lnum_off = 0;
18624
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 if (skip_until != NULL)
18626 {
18627 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18628 * don't check for ":endfunc". */
18629 if (STRCMP(theline, skip_until) == 0)
18630 {
18631 vim_free(skip_until);
18632 skip_until = NULL;
18633 }
18634 }
18635 else
18636 {
18637 /* skip ':' and blanks*/
18638 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18639 ;
18640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018641 /* Check for "endfunction". */
18642 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018644 if (line_arg == NULL)
18645 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 break;
18647 }
18648
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018649 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 * at "end". */
18651 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18652 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018653 else if (STRNCMP(p, "if", 2) == 0
18654 || STRNCMP(p, "wh", 2) == 0
18655 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 || STRNCMP(p, "try", 3) == 0)
18657 indent += 2;
18658
18659 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018660 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018662 if (*p == '!')
18663 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 p += eval_fname_script(p);
18665 if (ASCII_ISALPHA(*p))
18666 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018667 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 if (*skipwhite(p) == '(')
18669 {
18670 ++nesting;
18671 indent += 2;
18672 }
18673 }
18674 }
18675
18676 /* Check for ":append" or ":insert". */
18677 p = skip_range(p, NULL);
18678 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18679 || (p[0] == 'i'
18680 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18681 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18682 skip_until = vim_strsave((char_u *)".");
18683
18684 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18685 arg = skipwhite(skiptowhite(p));
18686 if (arg[0] == '<' && arg[1] =='<'
18687 && ((p[0] == 'p' && p[1] == 'y'
18688 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18689 || (p[0] == 'p' && p[1] == 'e'
18690 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18691 || (p[0] == 't' && p[1] == 'c'
18692 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18693 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18694 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018695 || (p[0] == 'm' && p[1] == 'z'
18696 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 ))
18698 {
18699 /* ":python <<" continues until a dot, like ":append" */
18700 p = skipwhite(arg + 2);
18701 if (*p == NUL)
18702 skip_until = vim_strsave((char_u *)".");
18703 else
18704 skip_until = vim_strsave(p);
18705 }
18706 }
18707
18708 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018709 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018710 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018711 if (line_arg == NULL)
18712 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018714 }
18715
18716 /* Copy the line to newly allocated memory. get_one_sourceline()
18717 * allocates 250 bytes per line, this saves 80% on average. The cost
18718 * is an extra alloc/free. */
18719 p = vim_strsave(theline);
18720 if (p != NULL)
18721 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018722 if (line_arg == NULL)
18723 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018724 theline = p;
18725 }
18726
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018727 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18728
18729 /* Add NULL lines for continuation lines, so that the line count is
18730 * equal to the index in the growarray. */
18731 while (sourcing_lnum_off-- > 0)
18732 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018733
18734 /* Check for end of eap->arg. */
18735 if (line_arg != NULL && *line_arg == NUL)
18736 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 }
18738
18739 /* Don't define the function when skipping commands or when an error was
18740 * detected. */
18741 if (eap->skip || did_emsg)
18742 goto erret;
18743
18744 /*
18745 * If there are no errors, add the function
18746 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018747 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018748 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018749 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018750 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018751 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018752 emsg_funcname("E707: Function name conflicts with variable: %s",
18753 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018754 goto erret;
18755 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018756
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018757 fp = find_func(name);
18758 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 if (!eap->forceit)
18761 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018762 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018763 goto erret;
18764 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018765 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018766 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018767 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018768 name);
18769 goto erret;
18770 }
18771 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018772 ga_clear_strings(&(fp->uf_args));
18773 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018774 vim_free(name);
18775 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 }
18778 else
18779 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 char numbuf[20];
18781
18782 fp = NULL;
18783 if (fudi.fd_newkey == NULL && !eap->forceit)
18784 {
18785 EMSG(_(e_funcdict));
18786 goto erret;
18787 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018788 if (fudi.fd_di == NULL)
18789 {
18790 /* Can't add a function to a locked dictionary */
18791 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18792 goto erret;
18793 }
18794 /* Can't change an existing function if it is locked */
18795 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18796 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018797
18798 /* Give the function a sequential number. Can only be used with a
18799 * Funcref! */
18800 vim_free(name);
18801 sprintf(numbuf, "%d", ++func_nr);
18802 name = vim_strsave((char_u *)numbuf);
18803 if (name == NULL)
18804 goto erret;
18805 }
18806
18807 if (fp == NULL)
18808 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018809 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018810 {
18811 int slen, plen;
18812 char_u *scriptname;
18813
18814 /* Check that the autoload name matches the script name. */
18815 j = FAIL;
18816 if (sourcing_name != NULL)
18817 {
18818 scriptname = autoload_name(name);
18819 if (scriptname != NULL)
18820 {
18821 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018822 plen = (int)STRLEN(p);
18823 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018824 if (slen > plen && fnamecmp(p,
18825 sourcing_name + slen - plen) == 0)
18826 j = OK;
18827 vim_free(scriptname);
18828 }
18829 }
18830 if (j == FAIL)
18831 {
18832 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18833 goto erret;
18834 }
18835 }
18836
18837 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 if (fp == NULL)
18839 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018840
18841 if (fudi.fd_dict != NULL)
18842 {
18843 if (fudi.fd_di == NULL)
18844 {
18845 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018846 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018847 if (fudi.fd_di == NULL)
18848 {
18849 vim_free(fp);
18850 goto erret;
18851 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018852 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18853 {
18854 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018855 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018856 goto erret;
18857 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018858 }
18859 else
18860 /* overwrite existing dict entry */
18861 clear_tv(&fudi.fd_di->di_tv);
18862 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018863 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018865 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018866
18867 /* behave like "dict" was used */
18868 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018869 }
18870
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018872 STRCPY(fp->uf_name, name);
18873 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018875 fp->uf_args = newargs;
18876 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018877#ifdef FEAT_PROFILE
18878 fp->uf_tml_count = NULL;
18879 fp->uf_tml_total = NULL;
18880 fp->uf_tml_self = NULL;
18881 fp->uf_profiling = FALSE;
18882 if (prof_def_func())
18883 func_do_profile(fp);
18884#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018885 fp->uf_varargs = varargs;
18886 fp->uf_flags = flags;
18887 fp->uf_calls = 0;
18888 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018889 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890
18891erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 ga_clear_strings(&newargs);
18893 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018894ret_free:
18895 vim_free(skip_until);
18896 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899}
18900
18901/*
18902 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018903 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018905 * flags:
18906 * TFN_INT: internal function name OK
18907 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 * Advances "pp" to just after the function name (if no error).
18909 */
18910 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018911trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 char_u **pp;
18913 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018914 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018917 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 char_u *start;
18919 char_u *end;
18920 int lead;
18921 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018923 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018924
18925 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018928
18929 /* Check for hard coded <SNR>: already translated function ID (from a user
18930 * command). */
18931 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18932 && (*pp)[2] == (int)KE_SNR)
18933 {
18934 *pp += 3;
18935 len = get_id_len(pp) + 3;
18936 return vim_strnsave(start, len);
18937 }
18938
18939 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18940 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018942 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018944
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018945 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18946 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018947 if (end == start)
18948 {
18949 if (!skip)
18950 EMSG(_("E129: Function name required"));
18951 goto theend;
18952 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018953 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018954 {
18955 /*
18956 * Report an invalid expression in braces, unless the expression
18957 * evaluation has been cancelled due to an aborting error, an
18958 * interrupt, or an exception.
18959 */
18960 if (!aborting())
18961 {
18962 if (end != NULL)
18963 EMSG2(_(e_invarg2), start);
18964 }
18965 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018966 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018967 goto theend;
18968 }
18969
18970 if (lv.ll_tv != NULL)
18971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018972 if (fdp != NULL)
18973 {
18974 fdp->fd_dict = lv.ll_dict;
18975 fdp->fd_newkey = lv.ll_newkey;
18976 lv.ll_newkey = NULL;
18977 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018979 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18980 {
18981 name = vim_strsave(lv.ll_tv->vval.v_string);
18982 *pp = end;
18983 }
18984 else
18985 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018986 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18987 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018988 EMSG(_(e_funcref));
18989 else
18990 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018991 name = NULL;
18992 }
18993 goto theend;
18994 }
18995
18996 if (lv.ll_name == NULL)
18997 {
18998 /* Error found, but continue after the function name. */
18999 *pp = end;
19000 goto theend;
19001 }
19002
19003 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019004 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019005 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019006 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19007 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19008 {
19009 /* When there was "s:" already or the name expanded to get a
19010 * leading "s:" then remove it. */
19011 lv.ll_name += 2;
19012 len -= 2;
19013 lead = 2;
19014 }
19015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019016 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019017 {
19018 if (lead == 2) /* skip over "s:" */
19019 lv.ll_name += 2;
19020 len = (int)(end - lv.ll_name);
19021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019022
19023 /*
19024 * Copy the function name to allocated memory.
19025 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19026 * Accept <SNR>123_name() outside a script.
19027 */
19028 if (skip)
19029 lead = 0; /* do nothing */
19030 else if (lead > 0)
19031 {
19032 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019033 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19034 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019035 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019036 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019037 if (current_SID <= 0)
19038 {
19039 EMSG(_(e_usingsid));
19040 goto theend;
19041 }
19042 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19043 lead += (int)STRLEN(sid_buf);
19044 }
19045 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019046 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019047 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019048 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019049 goto theend;
19050 }
19051 name = alloc((unsigned)(len + lead + 1));
19052 if (name != NULL)
19053 {
19054 if (lead > 0)
19055 {
19056 name[0] = K_SPECIAL;
19057 name[1] = KS_EXTRA;
19058 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019059 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019060 STRCPY(name + 3, sid_buf);
19061 }
19062 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19063 name[len + lead] = NUL;
19064 }
19065 *pp = end;
19066
19067theend:
19068 clear_lval(&lv);
19069 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070}
19071
19072/*
19073 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19074 * Return 2 if "p" starts with "s:".
19075 * Return 0 otherwise.
19076 */
19077 static int
19078eval_fname_script(p)
19079 char_u *p;
19080{
19081 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19082 || STRNICMP(p + 1, "SNR>", 4) == 0))
19083 return 5;
19084 if (p[0] == 's' && p[1] == ':')
19085 return 2;
19086 return 0;
19087}
19088
19089/*
19090 * Return TRUE if "p" starts with "<SID>" or "s:".
19091 * Only works if eval_fname_script() returned non-zero for "p"!
19092 */
19093 static int
19094eval_fname_sid(p)
19095 char_u *p;
19096{
19097 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19098}
19099
19100/*
19101 * List the head of the function: "name(arg1, arg2)".
19102 */
19103 static void
19104list_func_head(fp, indent)
19105 ufunc_T *fp;
19106 int indent;
19107{
19108 int j;
19109
19110 msg_start();
19111 if (indent)
19112 MSG_PUTS(" ");
19113 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019114 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 {
19116 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019117 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 }
19119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019120 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019122 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 {
19124 if (j)
19125 MSG_PUTS(", ");
19126 msg_puts(FUNCARG(fp, j));
19127 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019128 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 {
19130 if (j)
19131 MSG_PUTS(", ");
19132 MSG_PUTS("...");
19133 }
19134 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019135 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019136 if (p_verbose > 0)
19137 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138}
19139
19140/*
19141 * Find a function by name, return pointer to it in ufuncs.
19142 * Return NULL for unknown function.
19143 */
19144 static ufunc_T *
19145find_func(name)
19146 char_u *name;
19147{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019148 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019150 hi = hash_find(&func_hashtab, name);
19151 if (!HASHITEM_EMPTY(hi))
19152 return HI2UF(hi);
19153 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154}
19155
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019156#if defined(EXITFREE) || defined(PROTO)
19157 void
19158free_all_functions()
19159{
19160 hashitem_T *hi;
19161
19162 /* Need to start all over every time, because func_free() may change the
19163 * hash table. */
19164 while (func_hashtab.ht_used > 0)
19165 for (hi = func_hashtab.ht_array; ; ++hi)
19166 if (!HASHITEM_EMPTY(hi))
19167 {
19168 func_free(HI2UF(hi));
19169 break;
19170 }
19171}
19172#endif
19173
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019174/*
19175 * Return TRUE if a function "name" exists.
19176 */
19177 static int
19178function_exists(name)
19179 char_u *name;
19180{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019181 char_u *nm = name;
19182 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019183 int n = FALSE;
19184
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019185 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019186 nm = skipwhite(nm);
19187
19188 /* Only accept "funcname", "funcname ", "funcname (..." and
19189 * "funcname(...", not "funcname!...". */
19190 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019191 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019192 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019193 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019194 else
19195 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019196 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019197 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198 return n;
19199}
19200
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019201/*
19202 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019203 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019204 */
19205 static int
19206builtin_function(name)
19207 char_u *name;
19208{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019209 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19210 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019211}
19212
Bram Moolenaar05159a02005-02-26 23:04:13 +000019213#if defined(FEAT_PROFILE) || defined(PROTO)
19214/*
19215 * Start profiling function "fp".
19216 */
19217 static void
19218func_do_profile(fp)
19219 ufunc_T *fp;
19220{
19221 fp->uf_tm_count = 0;
19222 profile_zero(&fp->uf_tm_self);
19223 profile_zero(&fp->uf_tm_total);
19224 if (fp->uf_tml_count == NULL)
19225 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19226 (sizeof(int) * fp->uf_lines.ga_len));
19227 if (fp->uf_tml_total == NULL)
19228 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19229 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19230 if (fp->uf_tml_self == NULL)
19231 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19232 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19233 fp->uf_tml_idx = -1;
19234 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19235 || fp->uf_tml_self == NULL)
19236 return; /* out of memory */
19237
19238 fp->uf_profiling = TRUE;
19239}
19240
19241/*
19242 * Dump the profiling results for all functions in file "fd".
19243 */
19244 void
19245func_dump_profile(fd)
19246 FILE *fd;
19247{
19248 hashitem_T *hi;
19249 int todo;
19250 ufunc_T *fp;
19251 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019252 ufunc_T **sorttab;
19253 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019254
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019255 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019256 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19257
Bram Moolenaar05159a02005-02-26 23:04:13 +000019258 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19259 {
19260 if (!HASHITEM_EMPTY(hi))
19261 {
19262 --todo;
19263 fp = HI2UF(hi);
19264 if (fp->uf_profiling)
19265 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019266 if (sorttab != NULL)
19267 sorttab[st_len++] = fp;
19268
Bram Moolenaar05159a02005-02-26 23:04:13 +000019269 if (fp->uf_name[0] == K_SPECIAL)
19270 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19271 else
19272 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19273 if (fp->uf_tm_count == 1)
19274 fprintf(fd, "Called 1 time\n");
19275 else
19276 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19277 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19278 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19279 fprintf(fd, "\n");
19280 fprintf(fd, "count total (s) self (s)\n");
19281
19282 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19283 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019284 if (FUNCLINE(fp, i) == NULL)
19285 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019286 prof_func_line(fd, fp->uf_tml_count[i],
19287 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019288 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19289 }
19290 fprintf(fd, "\n");
19291 }
19292 }
19293 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019294
19295 if (sorttab != NULL && st_len > 0)
19296 {
19297 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19298 prof_total_cmp);
19299 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19300 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19301 prof_self_cmp);
19302 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19303 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019304}
Bram Moolenaar73830342005-02-28 22:48:19 +000019305
19306 static void
19307prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19308 FILE *fd;
19309 ufunc_T **sorttab;
19310 int st_len;
19311 char *title;
19312 int prefer_self; /* when equal print only self time */
19313{
19314 int i;
19315 ufunc_T *fp;
19316
19317 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19318 fprintf(fd, "count total (s) self (s) function\n");
19319 for (i = 0; i < 20 && i < st_len; ++i)
19320 {
19321 fp = sorttab[i];
19322 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19323 prefer_self);
19324 if (fp->uf_name[0] == K_SPECIAL)
19325 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19326 else
19327 fprintf(fd, " %s()\n", fp->uf_name);
19328 }
19329 fprintf(fd, "\n");
19330}
19331
19332/*
19333 * Print the count and times for one function or function line.
19334 */
19335 static void
19336prof_func_line(fd, count, total, self, prefer_self)
19337 FILE *fd;
19338 int count;
19339 proftime_T *total;
19340 proftime_T *self;
19341 int prefer_self; /* when equal print only self time */
19342{
19343 if (count > 0)
19344 {
19345 fprintf(fd, "%5d ", count);
19346 if (prefer_self && profile_equal(total, self))
19347 fprintf(fd, " ");
19348 else
19349 fprintf(fd, "%s ", profile_msg(total));
19350 if (!prefer_self && profile_equal(total, self))
19351 fprintf(fd, " ");
19352 else
19353 fprintf(fd, "%s ", profile_msg(self));
19354 }
19355 else
19356 fprintf(fd, " ");
19357}
19358
19359/*
19360 * Compare function for total time sorting.
19361 */
19362 static int
19363#ifdef __BORLANDC__
19364_RTLENTRYF
19365#endif
19366prof_total_cmp(s1, s2)
19367 const void *s1;
19368 const void *s2;
19369{
19370 ufunc_T *p1, *p2;
19371
19372 p1 = *(ufunc_T **)s1;
19373 p2 = *(ufunc_T **)s2;
19374 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19375}
19376
19377/*
19378 * Compare function for self time sorting.
19379 */
19380 static int
19381#ifdef __BORLANDC__
19382_RTLENTRYF
19383#endif
19384prof_self_cmp(s1, s2)
19385 const void *s1;
19386 const void *s2;
19387{
19388 ufunc_T *p1, *p2;
19389
19390 p1 = *(ufunc_T **)s1;
19391 p2 = *(ufunc_T **)s2;
19392 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19393}
19394
Bram Moolenaar05159a02005-02-26 23:04:13 +000019395#endif
19396
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019397/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019398 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019399 * Return TRUE if a package was loaded.
19400 */
19401 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019402script_autoload(name, reload)
19403 char_u *name;
19404 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019405{
19406 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019407 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019408 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019409 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019410
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019411 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019412 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019413 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019414 return FALSE;
19415
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019416 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019417
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019418 /* Find the name in the list of previously loaded package names. Skip
19419 * "autoload/", it's always the same. */
19420 for (i = 0; i < ga_loaded.ga_len; ++i)
19421 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19422 break;
19423 if (!reload && i < ga_loaded.ga_len)
19424 ret = FALSE; /* was loaded already */
19425 else
19426 {
19427 /* Remember the name if it wasn't loaded already. */
19428 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19429 {
19430 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19431 tofree = NULL;
19432 }
19433
19434 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019435 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019436 ret = TRUE;
19437 }
19438
19439 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019440 return ret;
19441}
19442
19443/*
19444 * Return the autoload script name for a function or variable name.
19445 * Returns NULL when out of memory.
19446 */
19447 static char_u *
19448autoload_name(name)
19449 char_u *name;
19450{
19451 char_u *p;
19452 char_u *scriptname;
19453
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019454 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019455 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19456 if (scriptname == NULL)
19457 return FALSE;
19458 STRCPY(scriptname, "autoload/");
19459 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019460 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019461 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019462 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019463 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019464 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019465}
19466
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19468
19469/*
19470 * Function given to ExpandGeneric() to obtain the list of user defined
19471 * function names.
19472 */
19473 char_u *
19474get_user_func_name(xp, idx)
19475 expand_T *xp;
19476 int idx;
19477{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019478 static long_u done;
19479 static hashitem_T *hi;
19480 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481
19482 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019484 done = 0;
19485 hi = func_hashtab.ht_array;
19486 }
19487 if (done < func_hashtab.ht_used)
19488 {
19489 if (done++ > 0)
19490 ++hi;
19491 while (HASHITEM_EMPTY(hi))
19492 ++hi;
19493 fp = HI2UF(hi);
19494
19495 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19496 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497
19498 cat_func_name(IObuff, fp);
19499 if (xp->xp_context != EXPAND_USER_FUNC)
19500 {
19501 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019502 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 STRCAT(IObuff, ")");
19504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 return IObuff;
19506 }
19507 return NULL;
19508}
19509
19510#endif /* FEAT_CMDL_COMPL */
19511
19512/*
19513 * Copy the function name of "fp" to buffer "buf".
19514 * "buf" must be able to hold the function name plus three bytes.
19515 * Takes care of script-local function names.
19516 */
19517 static void
19518cat_func_name(buf, fp)
19519 char_u *buf;
19520 ufunc_T *fp;
19521{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019522 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 {
19524 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019525 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 }
19527 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019528 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529}
19530
19531/*
19532 * ":delfunction {name}"
19533 */
19534 void
19535ex_delfunction(eap)
19536 exarg_T *eap;
19537{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019538 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 char_u *p;
19540 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019541 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542
19543 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019544 name = trans_function_name(&p, eap->skip, 0, &fudi);
19545 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019547 {
19548 if (fudi.fd_dict != NULL && !eap->skip)
19549 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 if (!ends_excmd(*skipwhite(p)))
19553 {
19554 vim_free(name);
19555 EMSG(_(e_trailing));
19556 return;
19557 }
19558 eap->nextcmd = check_nextcmd(p);
19559 if (eap->nextcmd != NULL)
19560 *p = NUL;
19561
19562 if (!eap->skip)
19563 fp = find_func(name);
19564 vim_free(name);
19565
19566 if (!eap->skip)
19567 {
19568 if (fp == NULL)
19569 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019570 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 return;
19572 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019573 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 {
19575 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19576 return;
19577 }
19578
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019579 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019581 /* Delete the dict item that refers to the function, it will
19582 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019583 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019585 else
19586 func_free(fp);
19587 }
19588}
19589
19590/*
19591 * Free a function and remove it from the list of functions.
19592 */
19593 static void
19594func_free(fp)
19595 ufunc_T *fp;
19596{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019597 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019598
19599 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019600 ga_clear_strings(&(fp->uf_args));
19601 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019602#ifdef FEAT_PROFILE
19603 vim_free(fp->uf_tml_count);
19604 vim_free(fp->uf_tml_total);
19605 vim_free(fp->uf_tml_self);
19606#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019607
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019608 /* remove the function from the function hashtable */
19609 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19610 if (HASHITEM_EMPTY(hi))
19611 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019612 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019613 hash_remove(&func_hashtab, hi);
19614
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019615 vim_free(fp);
19616}
19617
19618/*
19619 * Unreference a Function: decrement the reference count and free it when it
19620 * becomes zero. Only for numbered functions.
19621 */
19622 static void
19623func_unref(name)
19624 char_u *name;
19625{
19626 ufunc_T *fp;
19627
19628 if (name != NULL && isdigit(*name))
19629 {
19630 fp = find_func(name);
19631 if (fp == NULL)
19632 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019633 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019634 {
19635 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019636 * when "uf_calls" becomes zero. */
19637 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019638 func_free(fp);
19639 }
19640 }
19641}
19642
19643/*
19644 * Count a reference to a Function.
19645 */
19646 static void
19647func_ref(name)
19648 char_u *name;
19649{
19650 ufunc_T *fp;
19651
19652 if (name != NULL && isdigit(*name))
19653 {
19654 fp = find_func(name);
19655 if (fp == NULL)
19656 EMSG2(_(e_intern2), "func_ref()");
19657 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019658 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 }
19660}
19661
19662/*
19663 * Call a user function.
19664 */
19665 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019666call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 ufunc_T *fp; /* pointer to function */
19668 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 typval_T *argvars; /* arguments */
19670 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 linenr_T firstline; /* first line of range */
19672 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019673 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674{
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 char_u *save_sourcing_name;
19676 linenr_T save_sourcing_lnum;
19677 scid_T save_current_SID;
19678 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 int save_did_emsg;
19680 static int depth = 0;
19681 dictitem_T *v;
19682 int fixvar_idx = 0; /* index in fixvar[] */
19683 int i;
19684 int ai;
19685 char_u numbuf[NUMBUFLEN];
19686 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019687#ifdef FEAT_PROFILE
19688 proftime_T wait_start;
19689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690
19691 /* If depth of calling is getting too high, don't execute the function */
19692 if (depth >= p_mfd)
19693 {
19694 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 rettv->v_type = VAR_NUMBER;
19696 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 return;
19698 }
19699 ++depth;
19700
19701 line_breakcheck(); /* check for CTRL-C hit */
19702
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019703 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019704 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019706 fc.rettv = rettv;
19707 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 fc.linenr = 0;
19709 fc.returned = FALSE;
19710 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019712 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 fc.dbg_tick = debug_tick;
19714
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 /*
19716 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19717 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19718 * each argument variable and saves a lot of time.
19719 */
19720 /*
19721 * Init l: variables.
19722 */
19723 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019724 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019725 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019726 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19727 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019728 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019729 name = v->di_key;
19730 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19732 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19733 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019734 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 v->di_tv.vval.v_dict = selfdict;
19736 ++selfdict->dv_refcount;
19737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019738
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 /*
19740 * Init a: variables.
19741 * Set a:0 to "argcount".
19742 * Set a:000 to a list with room for the "..." arguments.
19743 */
19744 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19745 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019746 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 v = &fc.fixvar[fixvar_idx++].var;
19748 STRCPY(v->di_key, "000");
19749 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19750 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19751 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019752 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 v->di_tv.vval.v_list = &fc.l_varlist;
19754 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19755 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019756 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019757
19758 /*
19759 * Set a:firstline to "firstline" and a:lastline to "lastline".
19760 * Set a:name to named arguments.
19761 * Set a:N to the "..." arguments.
19762 */
19763 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19764 (varnumber_T)firstline);
19765 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19766 (varnumber_T)lastline);
19767 for (i = 0; i < argcount; ++i)
19768 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019769 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 if (ai < 0)
19771 /* named argument a:name */
19772 name = FUNCARG(fp, i);
19773 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019774 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019775 /* "..." argument a:1, a:2, etc. */
19776 sprintf((char *)numbuf, "%d", ai + 1);
19777 name = numbuf;
19778 }
19779 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19780 {
19781 v = &fc.fixvar[fixvar_idx++].var;
19782 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19783 }
19784 else
19785 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019786 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19787 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 if (v == NULL)
19789 break;
19790 v->di_flags = DI_FLAGS_RO;
19791 }
19792 STRCPY(v->di_key, name);
19793 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19794
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019795 /* Note: the values are copied directly to avoid alloc/free.
19796 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019797 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019798 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019799
19800 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19801 {
19802 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19803 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019804 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019805 }
19806 }
19807
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 /* Don't redraw while executing the function. */
19809 ++RedrawingDisabled;
19810 save_sourcing_name = sourcing_name;
19811 save_sourcing_lnum = sourcing_lnum;
19812 sourcing_lnum = 1;
19813 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019814 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 if (sourcing_name != NULL)
19816 {
19817 if (save_sourcing_name != NULL
19818 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19819 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19820 else
19821 STRCPY(sourcing_name, "function ");
19822 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19823
19824 if (p_verbose >= 12)
19825 {
19826 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019827 verbose_enter_scroll();
19828
Bram Moolenaar555b2802005-05-19 21:08:39 +000019829 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 if (p_verbose >= 14)
19831 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019833 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019834 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
19836 msg_puts((char_u *)"(");
19837 for (i = 0; i < argcount; ++i)
19838 {
19839 if (i > 0)
19840 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019841 if (argvars[i].v_type == VAR_NUMBER)
19842 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 else
19844 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019845 trunc_string(tv2string(&argvars[i], &tofree,
19846 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019848 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 }
19850 }
19851 msg_puts((char_u *)")");
19852 }
19853 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019854
19855 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 --no_wait_return;
19857 }
19858 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019859#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019860 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019861 {
19862 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19863 func_do_profile(fp);
19864 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019865 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019866 {
19867 ++fp->uf_tm_count;
19868 profile_start(&fp->uf_tm_start);
19869 profile_zero(&fp->uf_tm_children);
19870 }
19871 script_prof_save(&wait_start);
19872 }
19873#endif
19874
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019876 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 save_did_emsg = did_emsg;
19878 did_emsg = FALSE;
19879
19880 /* call do_cmdline() to execute the lines */
19881 do_cmdline(NULL, get_func_line, (void *)&fc,
19882 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19883
19884 --RedrawingDisabled;
19885
19886 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019887 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889 clear_tv(rettv);
19890 rettv->v_type = VAR_NUMBER;
19891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 }
19893
Bram Moolenaar05159a02005-02-26 23:04:13 +000019894#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019895 if (do_profiling == PROF_YES && (fp->uf_profiling
19896 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019897 {
19898 profile_end(&fp->uf_tm_start);
19899 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19900 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019901 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019902 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019903 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019904 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19905 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019906 }
19907 }
19908#endif
19909
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 /* when being verbose, mention the return value */
19911 if (p_verbose >= 12)
19912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019914 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019917 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019918 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019919 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19920 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019923 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019924 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019925 char_u *tofree;
19926
Bram Moolenaar555b2802005-05-19 21:08:39 +000019927 /* The value may be very long. Skip the middle part, so that we
19928 * have some idea how it starts and ends. smsg() would always
19929 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000019930 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019931 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019932 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019933 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 }
19935 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019936
19937 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 --no_wait_return;
19939 }
19940
19941 vim_free(sourcing_name);
19942 sourcing_name = save_sourcing_name;
19943 sourcing_lnum = save_sourcing_lnum;
19944 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019945#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019946 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019947 script_prof_restore(&wait_start);
19948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949
19950 if (p_verbose >= 12 && sourcing_name != NULL)
19951 {
19952 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019953 verbose_enter_scroll();
19954
Bram Moolenaar555b2802005-05-19 21:08:39 +000019955 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019957
19958 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 --no_wait_return;
19960 }
19961
19962 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019963 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964
Bram Moolenaar33570922005-01-25 22:26:29 +000019965 /* The a: variables typevals were not alloced, only free the allocated
19966 * variables. */
19967 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19968
19969 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 --depth;
19971}
19972
19973/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019974 * Add a number variable "name" to dict "dp" with value "nr".
19975 */
19976 static void
19977add_nr_var(dp, v, name, nr)
19978 dict_T *dp;
19979 dictitem_T *v;
19980 char *name;
19981 varnumber_T nr;
19982{
19983 STRCPY(v->di_key, name);
19984 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19985 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19986 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019987 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 v->di_tv.vval.v_number = nr;
19989}
19990
19991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 * ":return [expr]"
19993 */
19994 void
19995ex_return(eap)
19996 exarg_T *eap;
19997{
19998 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019999 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 int returning = FALSE;
20001
20002 if (current_funccal == NULL)
20003 {
20004 EMSG(_("E133: :return not inside a function"));
20005 return;
20006 }
20007
20008 if (eap->skip)
20009 ++emsg_skip;
20010
20011 eap->nextcmd = NULL;
20012 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020013 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 {
20015 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 }
20020 /* It's safer to return also on error. */
20021 else if (!eap->skip)
20022 {
20023 /*
20024 * Return unless the expression evaluation has been cancelled due to an
20025 * aborting error, an interrupt, or an exception.
20026 */
20027 if (!aborting())
20028 returning = do_return(eap, FALSE, TRUE, NULL);
20029 }
20030
20031 /* When skipping or the return gets pending, advance to the next command
20032 * in this line (!returning). Otherwise, ignore the rest of the line.
20033 * Following lines will be ignored by get_func_line(). */
20034 if (returning)
20035 eap->nextcmd = NULL;
20036 else if (eap->nextcmd == NULL) /* no argument */
20037 eap->nextcmd = check_nextcmd(arg);
20038
20039 if (eap->skip)
20040 --emsg_skip;
20041}
20042
20043/*
20044 * Return from a function. Possibly makes the return pending. Also called
20045 * for a pending return at the ":endtry" or after returning from an extra
20046 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020048 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 * FALSE when the return gets pending.
20050 */
20051 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 exarg_T *eap;
20054 int reanimate;
20055 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057{
20058 int idx;
20059 struct condstack *cstack = eap->cstack;
20060
20061 if (reanimate)
20062 /* Undo the return. */
20063 current_funccal->returned = FALSE;
20064
20065 /*
20066 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20067 * not in its finally clause (which then is to be executed next) is found.
20068 * In this case, make the ":return" pending for execution at the ":endtry".
20069 * Otherwise, return normally.
20070 */
20071 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20072 if (idx >= 0)
20073 {
20074 cstack->cs_pending[idx] = CSTP_RETURN;
20075
20076 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 /* A pending return again gets pending. "rettv" points to an
20078 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020080 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 else
20082 {
20083 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 {
20090 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020092 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 else
20094 EMSG(_(e_outofmem));
20095 }
20096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098
20099 if (reanimate)
20100 {
20101 /* The pending return value could be overwritten by a ":return"
20102 * without argument in a finally clause; reset the default
20103 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 current_funccal->rettv->v_type = VAR_NUMBER;
20105 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 }
20107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 }
20110 else
20111 {
20112 current_funccal->returned = TRUE;
20113
20114 /* If the return is carried out now, store the return value. For
20115 * a return immediately after reanimation, the value is already
20116 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020119 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020120 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020122 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 }
20124 }
20125
20126 return idx < 0;
20127}
20128
20129/*
20130 * Free the variable with a pending return value.
20131 */
20132 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133discard_pending_return(rettv)
20134 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135{
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137}
20138
20139/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 * is an allocated string. Used by report_pending() for verbose messages.
20142 */
20143 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020144get_return_cmd(rettv)
20145 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020147 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020149 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020151 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020152 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020153 if (s == NULL)
20154 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020155
20156 STRCPY(IObuff, ":return ");
20157 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20158 if (STRLEN(s) + 8 >= IOSIZE)
20159 STRCPY(IObuff + IOSIZE - 4, "...");
20160 vim_free(tofree);
20161 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162}
20163
20164/*
20165 * Get next function line.
20166 * Called by do_cmdline() to get the next line.
20167 * Returns allocated string, or NULL for end of function.
20168 */
20169/* ARGSUSED */
20170 char_u *
20171get_func_line(c, cookie, indent)
20172 int c; /* not used */
20173 void *cookie;
20174 int indent; /* not used */
20175{
Bram Moolenaar33570922005-01-25 22:26:29 +000020176 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020177 ufunc_T *fp = fcp->func;
20178 char_u *retval;
20179 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180
20181 /* If breakpoints have been added/deleted need to check for it. */
20182 if (fcp->dbg_tick != debug_tick)
20183 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020184 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 sourcing_lnum);
20186 fcp->dbg_tick = debug_tick;
20187 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020188#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020189 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020190 func_line_end(cookie);
20191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192
Bram Moolenaar05159a02005-02-26 23:04:13 +000020193 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020194 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20195 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 retval = NULL;
20197 else
20198 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020199 /* Skip NULL lines (continuation lines). */
20200 while (fcp->linenr < gap->ga_len
20201 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20202 ++fcp->linenr;
20203 if (fcp->linenr >= gap->ga_len)
20204 retval = NULL;
20205 else
20206 {
20207 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20208 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020209#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020210 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020211 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020212#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 }
20215
20216 /* Did we encounter a breakpoint? */
20217 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20218 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020219 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020221 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 sourcing_lnum);
20223 fcp->dbg_tick = debug_tick;
20224 }
20225
20226 return retval;
20227}
20228
Bram Moolenaar05159a02005-02-26 23:04:13 +000020229#if defined(FEAT_PROFILE) || defined(PROTO)
20230/*
20231 * Called when starting to read a function line.
20232 * "sourcing_lnum" must be correct!
20233 * When skipping lines it may not actually be executed, but we won't find out
20234 * until later and we need to store the time now.
20235 */
20236 void
20237func_line_start(cookie)
20238 void *cookie;
20239{
20240 funccall_T *fcp = (funccall_T *)cookie;
20241 ufunc_T *fp = fcp->func;
20242
20243 if (fp->uf_profiling && sourcing_lnum >= 1
20244 && sourcing_lnum <= fp->uf_lines.ga_len)
20245 {
20246 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020247 /* Skip continuation lines. */
20248 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20249 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020250 fp->uf_tml_execed = FALSE;
20251 profile_start(&fp->uf_tml_start);
20252 profile_zero(&fp->uf_tml_children);
20253 profile_get_wait(&fp->uf_tml_wait);
20254 }
20255}
20256
20257/*
20258 * Called when actually executing a function line.
20259 */
20260 void
20261func_line_exec(cookie)
20262 void *cookie;
20263{
20264 funccall_T *fcp = (funccall_T *)cookie;
20265 ufunc_T *fp = fcp->func;
20266
20267 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20268 fp->uf_tml_execed = TRUE;
20269}
20270
20271/*
20272 * Called when done with a function line.
20273 */
20274 void
20275func_line_end(cookie)
20276 void *cookie;
20277{
20278 funccall_T *fcp = (funccall_T *)cookie;
20279 ufunc_T *fp = fcp->func;
20280
20281 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20282 {
20283 if (fp->uf_tml_execed)
20284 {
20285 ++fp->uf_tml_count[fp->uf_tml_idx];
20286 profile_end(&fp->uf_tml_start);
20287 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020288 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020289 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20290 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020291 }
20292 fp->uf_tml_idx = -1;
20293 }
20294}
20295#endif
20296
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297/*
20298 * Return TRUE if the currently active function should be ended, because a
20299 * return was encountered or an error occured. Used inside a ":while".
20300 */
20301 int
20302func_has_ended(cookie)
20303 void *cookie;
20304{
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306
20307 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20308 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020309 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 || fcp->returned);
20311}
20312
20313/*
20314 * return TRUE if cookie indicates a function which "abort"s on errors.
20315 */
20316 int
20317func_has_abort(cookie)
20318 void *cookie;
20319{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020320 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321}
20322
20323#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20324typedef enum
20325{
20326 VAR_FLAVOUR_DEFAULT,
20327 VAR_FLAVOUR_SESSION,
20328 VAR_FLAVOUR_VIMINFO
20329} var_flavour_T;
20330
20331static var_flavour_T var_flavour __ARGS((char_u *varname));
20332
20333 static var_flavour_T
20334var_flavour(varname)
20335 char_u *varname;
20336{
20337 char_u *p = varname;
20338
20339 if (ASCII_ISUPPER(*p))
20340 {
20341 while (*(++p))
20342 if (ASCII_ISLOWER(*p))
20343 return VAR_FLAVOUR_SESSION;
20344 return VAR_FLAVOUR_VIMINFO;
20345 }
20346 else
20347 return VAR_FLAVOUR_DEFAULT;
20348}
20349#endif
20350
20351#if defined(FEAT_VIMINFO) || defined(PROTO)
20352/*
20353 * Restore global vars that start with a capital from the viminfo file
20354 */
20355 int
20356read_viminfo_varlist(virp, writing)
20357 vir_T *virp;
20358 int writing;
20359{
20360 char_u *tab;
20361 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020362 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
20364 if (!writing && (find_viminfo_parameter('!') != NULL))
20365 {
20366 tab = vim_strchr(virp->vir_line + 1, '\t');
20367 if (tab != NULL)
20368 {
20369 *tab++ = '\0'; /* isolate the variable name */
20370 if (*tab == 'S') /* string var */
20371 is_string = TRUE;
20372
20373 tab = vim_strchr(tab, '\t');
20374 if (tab != NULL)
20375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 if (is_string)
20377 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020378 tv.v_type = VAR_STRING;
20379 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 }
20382 else
20383 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020384 tv.v_type = VAR_NUMBER;
20385 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020387 set_var(virp->vir_line + 1, &tv, FALSE);
20388 if (is_string)
20389 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 }
20391 }
20392 }
20393
20394 return viminfo_readline(virp);
20395}
20396
20397/*
20398 * Write global vars that start with a capital to the viminfo file
20399 */
20400 void
20401write_viminfo_varlist(fp)
20402 FILE *fp;
20403{
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 hashitem_T *hi;
20405 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020406 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020407 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020408 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020409 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020410 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411
20412 if (find_viminfo_parameter('!') == NULL)
20413 return;
20414
20415 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020417 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020420 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020422 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020423 this_var = HI2DI(hi);
20424 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020427 {
20428 case VAR_STRING: s = "STR"; break;
20429 case VAR_NUMBER: s = "NUM"; break;
20430 default: continue;
20431 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020432 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020433 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020434 if (p != NULL)
20435 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020436 vim_free(tofree);
20437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 }
20439 }
20440}
20441#endif
20442
20443#if defined(FEAT_SESSION) || defined(PROTO)
20444 int
20445store_session_globals(fd)
20446 FILE *fd;
20447{
Bram Moolenaar33570922005-01-25 22:26:29 +000020448 hashitem_T *hi;
20449 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020450 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 char_u *p, *t;
20452
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020453 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020456 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020458 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020459 this_var = HI2DI(hi);
20460 if ((this_var->di_tv.v_type == VAR_NUMBER
20461 || this_var->di_tv.v_type == VAR_STRING)
20462 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020463 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020464 /* Escape special characters with a backslash. Turn a LF and
20465 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020466 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020467 (char_u *)"\\\"\n\r");
20468 if (p == NULL) /* out of memory */
20469 break;
20470 for (t = p; *t != NUL; ++t)
20471 if (*t == '\n')
20472 *t = 'n';
20473 else if (*t == '\r')
20474 *t = 'r';
20475 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020476 this_var->di_key,
20477 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20478 : ' ',
20479 p,
20480 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20481 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020482 || put_eol(fd) == FAIL)
20483 {
20484 vim_free(p);
20485 return FAIL;
20486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 vim_free(p);
20488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 }
20490 }
20491 return OK;
20492}
20493#endif
20494
Bram Moolenaar661b1822005-07-28 22:36:45 +000020495/*
20496 * Display script name where an item was last set.
20497 * Should only be invoked when 'verbose' is non-zero.
20498 */
20499 void
20500last_set_msg(scriptID)
20501 scid_T scriptID;
20502{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020503 char_u *p;
20504
Bram Moolenaar661b1822005-07-28 22:36:45 +000020505 if (scriptID != 0)
20506 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020507 p = home_replace_save(NULL, get_scriptname(scriptID));
20508 if (p != NULL)
20509 {
20510 verbose_enter();
20511 MSG_PUTS(_("\n\tLast set from "));
20512 MSG_PUTS(p);
20513 vim_free(p);
20514 verbose_leave();
20515 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020516 }
20517}
20518
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519#endif /* FEAT_EVAL */
20520
20521#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20522
20523
20524#ifdef WIN3264
20525/*
20526 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20527 */
20528static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20529static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20530static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20531
20532/*
20533 * Get the short pathname of a file.
20534 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20535 */
20536 static int
20537get_short_pathname(fnamep, bufp, fnamelen)
20538 char_u **fnamep;
20539 char_u **bufp;
20540 int *fnamelen;
20541{
20542 int l,len;
20543 char_u *newbuf;
20544
20545 len = *fnamelen;
20546
20547 l = GetShortPathName(*fnamep, *fnamep, len);
20548 if (l > len - 1)
20549 {
20550 /* If that doesn't work (not enough space), then save the string
20551 * and try again with a new buffer big enough
20552 */
20553 newbuf = vim_strnsave(*fnamep, l);
20554 if (newbuf == NULL)
20555 return 0;
20556
20557 vim_free(*bufp);
20558 *fnamep = *bufp = newbuf;
20559
20560 l = GetShortPathName(*fnamep,*fnamep,l+1);
20561
20562 /* Really should always succeed, as the buffer is big enough */
20563 }
20564
20565 *fnamelen = l;
20566 return 1;
20567}
20568
20569/*
20570 * Create a short path name. Returns the length of the buffer it needs.
20571 * Doesn't copy over the end of the buffer passed in.
20572 */
20573 static int
20574shortpath_for_invalid_fname(fname, bufp, fnamelen)
20575 char_u **fname;
20576 char_u **bufp;
20577 int *fnamelen;
20578{
20579 char_u *s, *p, *pbuf2, *pbuf3;
20580 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020581 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582
20583 /* Make a copy */
20584 len2 = *fnamelen;
20585 pbuf2 = vim_strnsave(*fname, len2);
20586 pbuf3 = NULL;
20587
20588 s = pbuf2 + len2 - 1; /* Find the end */
20589 slen = 1;
20590 plen = len2;
20591
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020592 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593 {
20594 --s;
20595 ++slen;
20596 --plen;
20597 }
20598
20599 do
20600 {
20601 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020602 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 {
20604 --s;
20605 ++slen;
20606 --plen;
20607 }
20608 if (s <= pbuf2)
20609 break;
20610
20611 /* Remeber the character that is about to be blatted */
20612 ch = *s;
20613 *s = 0; /* get_short_pathname requires a null-terminated string */
20614
20615 /* Try it in situ */
20616 p = pbuf2;
20617 if (!get_short_pathname(&p, &pbuf3, &plen))
20618 {
20619 vim_free(pbuf2);
20620 return -1;
20621 }
20622 *s = ch; /* Preserve the string */
20623 } while (plen == 0);
20624
20625 if (plen > 0)
20626 {
20627 /* Remeber the length of the new string. */
20628 *fnamelen = len = plen + slen;
20629 vim_free(*bufp);
20630 if (len > len2)
20631 {
20632 /* If there's not enough space in the currently allocated string,
20633 * then copy it to a buffer big enough.
20634 */
20635 *fname= *bufp = vim_strnsave(p, len);
20636 if (*fname == NULL)
20637 return -1;
20638 }
20639 else
20640 {
20641 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20642 *fname = *bufp = pbuf2;
20643 if (p != pbuf2)
20644 strncpy(*fname, p, plen);
20645 pbuf2 = NULL;
20646 }
20647 /* Concat the next bit */
20648 strncpy(*fname + plen, s, slen);
20649 (*fname)[len] = '\0';
20650 }
20651 vim_free(pbuf3);
20652 vim_free(pbuf2);
20653 return 0;
20654}
20655
20656/*
20657 * Get a pathname for a partial path.
20658 */
20659 static int
20660shortpath_for_partial(fnamep, bufp, fnamelen)
20661 char_u **fnamep;
20662 char_u **bufp;
20663 int *fnamelen;
20664{
20665 int sepcount, len, tflen;
20666 char_u *p;
20667 char_u *pbuf, *tfname;
20668 int hasTilde;
20669
20670 /* Count up the path seperators from the RHS.. so we know which part
20671 * of the path to return.
20672 */
20673 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020674 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 if (vim_ispathsep(*p))
20676 ++sepcount;
20677
20678 /* Need full path first (use expand_env() to remove a "~/") */
20679 hasTilde = (**fnamep == '~');
20680 if (hasTilde)
20681 pbuf = tfname = expand_env_save(*fnamep);
20682 else
20683 pbuf = tfname = FullName_save(*fnamep, FALSE);
20684
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020685 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686
20687 if (!get_short_pathname(&tfname, &pbuf, &len))
20688 return -1;
20689
20690 if (len == 0)
20691 {
20692 /* Don't have a valid filename, so shorten the rest of the
20693 * path if we can. This CAN give us invalid 8.3 filenames, but
20694 * there's not a lot of point in guessing what it might be.
20695 */
20696 len = tflen;
20697 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20698 return -1;
20699 }
20700
20701 /* Count the paths backward to find the beginning of the desired string. */
20702 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020703 {
20704#ifdef FEAT_MBYTE
20705 if (has_mbyte)
20706 p -= mb_head_off(tfname, p);
20707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 if (vim_ispathsep(*p))
20709 {
20710 if (sepcount == 0 || (hasTilde && sepcount == 1))
20711 break;
20712 else
20713 sepcount --;
20714 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 if (hasTilde)
20717 {
20718 --p;
20719 if (p >= tfname)
20720 *p = '~';
20721 else
20722 return -1;
20723 }
20724 else
20725 ++p;
20726
20727 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20728 vim_free(*bufp);
20729 *fnamelen = (int)STRLEN(p);
20730 *bufp = pbuf;
20731 *fnamep = p;
20732
20733 return 0;
20734}
20735#endif /* WIN3264 */
20736
20737/*
20738 * Adjust a filename, according to a string of modifiers.
20739 * *fnamep must be NUL terminated when called. When returning, the length is
20740 * determined by *fnamelen.
20741 * Returns valid flags.
20742 * When there is an error, *fnamep is set to NULL.
20743 */
20744 int
20745modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20746 char_u *src; /* string with modifiers */
20747 int *usedlen; /* characters after src that are used */
20748 char_u **fnamep; /* file name so far */
20749 char_u **bufp; /* buffer for allocated file name or NULL */
20750 int *fnamelen; /* length of fnamep */
20751{
20752 int valid = 0;
20753 char_u *tail;
20754 char_u *s, *p, *pbuf;
20755 char_u dirname[MAXPATHL];
20756 int c;
20757 int has_fullname = 0;
20758#ifdef WIN3264
20759 int has_shortname = 0;
20760#endif
20761
20762repeat:
20763 /* ":p" - full path/file_name */
20764 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20765 {
20766 has_fullname = 1;
20767
20768 valid |= VALID_PATH;
20769 *usedlen += 2;
20770
20771 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20772 if ((*fnamep)[0] == '~'
20773#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20774 && ((*fnamep)[1] == '/'
20775# ifdef BACKSLASH_IN_FILENAME
20776 || (*fnamep)[1] == '\\'
20777# endif
20778 || (*fnamep)[1] == NUL)
20779
20780#endif
20781 )
20782 {
20783 *fnamep = expand_env_save(*fnamep);
20784 vim_free(*bufp); /* free any allocated file name */
20785 *bufp = *fnamep;
20786 if (*fnamep == NULL)
20787 return -1;
20788 }
20789
20790 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020791 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 {
20793 if (vim_ispathsep(*p)
20794 && p[1] == '.'
20795 && (p[2] == NUL
20796 || vim_ispathsep(p[2])
20797 || (p[2] == '.'
20798 && (p[3] == NUL || vim_ispathsep(p[3])))))
20799 break;
20800 }
20801
20802 /* FullName_save() is slow, don't use it when not needed. */
20803 if (*p != NUL || !vim_isAbsName(*fnamep))
20804 {
20805 *fnamep = FullName_save(*fnamep, *p != NUL);
20806 vim_free(*bufp); /* free any allocated file name */
20807 *bufp = *fnamep;
20808 if (*fnamep == NULL)
20809 return -1;
20810 }
20811
20812 /* Append a path separator to a directory. */
20813 if (mch_isdir(*fnamep))
20814 {
20815 /* Make room for one or two extra characters. */
20816 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20817 vim_free(*bufp); /* free any allocated file name */
20818 *bufp = *fnamep;
20819 if (*fnamep == NULL)
20820 return -1;
20821 add_pathsep(*fnamep);
20822 }
20823 }
20824
20825 /* ":." - path relative to the current directory */
20826 /* ":~" - path relative to the home directory */
20827 /* ":8" - shortname path - postponed till after */
20828 while (src[*usedlen] == ':'
20829 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20830 {
20831 *usedlen += 2;
20832 if (c == '8')
20833 {
20834#ifdef WIN3264
20835 has_shortname = 1; /* Postpone this. */
20836#endif
20837 continue;
20838 }
20839 pbuf = NULL;
20840 /* Need full path first (use expand_env() to remove a "~/") */
20841 if (!has_fullname)
20842 {
20843 if (c == '.' && **fnamep == '~')
20844 p = pbuf = expand_env_save(*fnamep);
20845 else
20846 p = pbuf = FullName_save(*fnamep, FALSE);
20847 }
20848 else
20849 p = *fnamep;
20850
20851 has_fullname = 0;
20852
20853 if (p != NULL)
20854 {
20855 if (c == '.')
20856 {
20857 mch_dirname(dirname, MAXPATHL);
20858 s = shorten_fname(p, dirname);
20859 if (s != NULL)
20860 {
20861 *fnamep = s;
20862 if (pbuf != NULL)
20863 {
20864 vim_free(*bufp); /* free any allocated file name */
20865 *bufp = pbuf;
20866 pbuf = NULL;
20867 }
20868 }
20869 }
20870 else
20871 {
20872 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20873 /* Only replace it when it starts with '~' */
20874 if (*dirname == '~')
20875 {
20876 s = vim_strsave(dirname);
20877 if (s != NULL)
20878 {
20879 *fnamep = s;
20880 vim_free(*bufp);
20881 *bufp = s;
20882 }
20883 }
20884 }
20885 vim_free(pbuf);
20886 }
20887 }
20888
20889 tail = gettail(*fnamep);
20890 *fnamelen = (int)STRLEN(*fnamep);
20891
20892 /* ":h" - head, remove "/file_name", can be repeated */
20893 /* Don't remove the first "/" or "c:\" */
20894 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20895 {
20896 valid |= VALID_HEAD;
20897 *usedlen += 2;
20898 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020899 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 --tail;
20901 *fnamelen = (int)(tail - *fnamep);
20902#ifdef VMS
20903 if (*fnamelen > 0)
20904 *fnamelen += 1; /* the path separator is part of the path */
20905#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020906 while (tail > s && !after_pathsep(s, tail))
20907 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 }
20909
20910 /* ":8" - shortname */
20911 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20912 {
20913 *usedlen += 2;
20914#ifdef WIN3264
20915 has_shortname = 1;
20916#endif
20917 }
20918
20919#ifdef WIN3264
20920 /* Check shortname after we have done 'heads' and before we do 'tails'
20921 */
20922 if (has_shortname)
20923 {
20924 pbuf = NULL;
20925 /* Copy the string if it is shortened by :h */
20926 if (*fnamelen < (int)STRLEN(*fnamep))
20927 {
20928 p = vim_strnsave(*fnamep, *fnamelen);
20929 if (p == 0)
20930 return -1;
20931 vim_free(*bufp);
20932 *bufp = *fnamep = p;
20933 }
20934
20935 /* Split into two implementations - makes it easier. First is where
20936 * there isn't a full name already, second is where there is.
20937 */
20938 if (!has_fullname && !vim_isAbsName(*fnamep))
20939 {
20940 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20941 return -1;
20942 }
20943 else
20944 {
20945 int l;
20946
20947 /* Simple case, already have the full-name
20948 * Nearly always shorter, so try first time. */
20949 l = *fnamelen;
20950 if (!get_short_pathname(fnamep, bufp, &l))
20951 return -1;
20952
20953 if (l == 0)
20954 {
20955 /* Couldn't find the filename.. search the paths.
20956 */
20957 l = *fnamelen;
20958 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20959 return -1;
20960 }
20961 *fnamelen = l;
20962 }
20963 }
20964#endif /* WIN3264 */
20965
20966 /* ":t" - tail, just the basename */
20967 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20968 {
20969 *usedlen += 2;
20970 *fnamelen -= (int)(tail - *fnamep);
20971 *fnamep = tail;
20972 }
20973
20974 /* ":e" - extension, can be repeated */
20975 /* ":r" - root, without extension, can be repeated */
20976 while (src[*usedlen] == ':'
20977 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20978 {
20979 /* find a '.' in the tail:
20980 * - for second :e: before the current fname
20981 * - otherwise: The last '.'
20982 */
20983 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20984 s = *fnamep - 2;
20985 else
20986 s = *fnamep + *fnamelen - 1;
20987 for ( ; s > tail; --s)
20988 if (s[0] == '.')
20989 break;
20990 if (src[*usedlen + 1] == 'e') /* :e */
20991 {
20992 if (s > tail)
20993 {
20994 *fnamelen += (int)(*fnamep - (s + 1));
20995 *fnamep = s + 1;
20996#ifdef VMS
20997 /* cut version from the extension */
20998 s = *fnamep + *fnamelen - 1;
20999 for ( ; s > *fnamep; --s)
21000 if (s[0] == ';')
21001 break;
21002 if (s > *fnamep)
21003 *fnamelen = s - *fnamep;
21004#endif
21005 }
21006 else if (*fnamep <= tail)
21007 *fnamelen = 0;
21008 }
21009 else /* :r */
21010 {
21011 if (s > tail) /* remove one extension */
21012 *fnamelen = (int)(s - *fnamep);
21013 }
21014 *usedlen += 2;
21015 }
21016
21017 /* ":s?pat?foo?" - substitute */
21018 /* ":gs?pat?foo?" - global substitute */
21019 if (src[*usedlen] == ':'
21020 && (src[*usedlen + 1] == 's'
21021 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21022 {
21023 char_u *str;
21024 char_u *pat;
21025 char_u *sub;
21026 int sep;
21027 char_u *flags;
21028 int didit = FALSE;
21029
21030 flags = (char_u *)"";
21031 s = src + *usedlen + 2;
21032 if (src[*usedlen + 1] == 'g')
21033 {
21034 flags = (char_u *)"g";
21035 ++s;
21036 }
21037
21038 sep = *s++;
21039 if (sep)
21040 {
21041 /* find end of pattern */
21042 p = vim_strchr(s, sep);
21043 if (p != NULL)
21044 {
21045 pat = vim_strnsave(s, (int)(p - s));
21046 if (pat != NULL)
21047 {
21048 s = p + 1;
21049 /* find end of substitution */
21050 p = vim_strchr(s, sep);
21051 if (p != NULL)
21052 {
21053 sub = vim_strnsave(s, (int)(p - s));
21054 str = vim_strnsave(*fnamep, *fnamelen);
21055 if (sub != NULL && str != NULL)
21056 {
21057 *usedlen = (int)(p + 1 - src);
21058 s = do_string_sub(str, pat, sub, flags);
21059 if (s != NULL)
21060 {
21061 *fnamep = s;
21062 *fnamelen = (int)STRLEN(s);
21063 vim_free(*bufp);
21064 *bufp = s;
21065 didit = TRUE;
21066 }
21067 }
21068 vim_free(sub);
21069 vim_free(str);
21070 }
21071 vim_free(pat);
21072 }
21073 }
21074 /* after using ":s", repeat all the modifiers */
21075 if (didit)
21076 goto repeat;
21077 }
21078 }
21079
21080 return valid;
21081}
21082
21083/*
21084 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21085 * "flags" can be "g" to do a global substitute.
21086 * Returns an allocated string, NULL for error.
21087 */
21088 char_u *
21089do_string_sub(str, pat, sub, flags)
21090 char_u *str;
21091 char_u *pat;
21092 char_u *sub;
21093 char_u *flags;
21094{
21095 int sublen;
21096 regmatch_T regmatch;
21097 int i;
21098 int do_all;
21099 char_u *tail;
21100 garray_T ga;
21101 char_u *ret;
21102 char_u *save_cpo;
21103
21104 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21105 save_cpo = p_cpo;
21106 p_cpo = (char_u *)"";
21107
21108 ga_init2(&ga, 1, 200);
21109
21110 do_all = (flags[0] == 'g');
21111
21112 regmatch.rm_ic = p_ic;
21113 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21114 if (regmatch.regprog != NULL)
21115 {
21116 tail = str;
21117 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21118 {
21119 /*
21120 * Get some space for a temporary buffer to do the substitution
21121 * into. It will contain:
21122 * - The text up to where the match is.
21123 * - The substituted text.
21124 * - The text after the match.
21125 */
21126 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21127 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21128 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21129 {
21130 ga_clear(&ga);
21131 break;
21132 }
21133
21134 /* copy the text up to where the match is */
21135 i = (int)(regmatch.startp[0] - tail);
21136 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21137 /* add the substituted text */
21138 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21139 + ga.ga_len + i, TRUE, TRUE, FALSE);
21140 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 /* avoid getting stuck on a match with an empty string */
21142 if (tail == regmatch.endp[0])
21143 {
21144 if (*tail == NUL)
21145 break;
21146 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21147 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 }
21149 else
21150 {
21151 tail = regmatch.endp[0];
21152 if (*tail == NUL)
21153 break;
21154 }
21155 if (!do_all)
21156 break;
21157 }
21158
21159 if (ga.ga_data != NULL)
21160 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21161
21162 vim_free(regmatch.regprog);
21163 }
21164
21165 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21166 ga_clear(&ga);
21167 p_cpo = save_cpo;
21168
21169 return ret;
21170}
21171
21172#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */