blob: aebd682f5c38fbd472da3b670d5c34023c54c2f6 [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 Moolenaarb47a2402006-10-15 13:09:12 +00005523 static int recursive = 0; /* cach recursive loops */
5524 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005525
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005526 /* Catch lists and dicts that have an endless loop by limiting
5527 * recursiveness to 1000. */
5528 if (tv1->v_type != tv2->v_type || recursive >= 1000)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005529 return FALSE;
5530
5531 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005532 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005533 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005534 ++recursive;
5535 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5536 --recursive;
5537 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005538
5539 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005540 ++recursive;
5541 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5542 --recursive;
5543 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005544
5545 case VAR_FUNC:
5546 return (tv1->vval.v_string != NULL
5547 && tv2->vval.v_string != NULL
5548 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5549
5550 case VAR_NUMBER:
5551 return tv1->vval.v_number == tv2->vval.v_number;
5552
5553 case VAR_STRING:
5554 s1 = get_tv_string_buf(tv1, buf1);
5555 s2 = get_tv_string_buf(tv2, buf2);
5556 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005557 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005558
5559 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005560 return TRUE;
5561}
5562
5563/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 * Locate item with index "n" in list "l" and return it.
5565 * A negative index is counted from the end; -1 is the last item.
5566 * Returns NULL when "n" is out of range.
5567 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005570 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 long n;
5572{
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 long idx;
5575
5576 if (l == NULL)
5577 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005578
5579 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005581 n = l->lv_len + n;
5582
5583 /* Check for index out of range. */
5584 if (n < 0 || n >= l->lv_len)
5585 return NULL;
5586
5587 /* When there is a cached index may start search from there. */
5588 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005589 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005590 if (n < l->lv_idx / 2)
5591 {
5592 /* closest to the start of the list */
5593 item = l->lv_first;
5594 idx = 0;
5595 }
5596 else if (n > (l->lv_idx + l->lv_len) / 2)
5597 {
5598 /* closest to the end of the list */
5599 item = l->lv_last;
5600 idx = l->lv_len - 1;
5601 }
5602 else
5603 {
5604 /* closest to the cached index */
5605 item = l->lv_idx_item;
5606 idx = l->lv_idx;
5607 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 }
5609 else
5610 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005611 if (n < l->lv_len / 2)
5612 {
5613 /* closest to the start of the list */
5614 item = l->lv_first;
5615 idx = 0;
5616 }
5617 else
5618 {
5619 /* closest to the end of the list */
5620 item = l->lv_last;
5621 idx = l->lv_len - 1;
5622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005624
5625 while (n > idx)
5626 {
5627 /* search forward */
5628 item = item->li_next;
5629 ++idx;
5630 }
5631 while (n < idx)
5632 {
5633 /* search backward */
5634 item = item->li_prev;
5635 --idx;
5636 }
5637
5638 /* cache the used index */
5639 l->lv_idx = idx;
5640 l->lv_idx_item = item;
5641
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 return item;
5643}
5644
5645/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005646 * Get list item "l[idx]" as a number.
5647 */
5648 static long
5649list_find_nr(l, idx, errorp)
5650 list_T *l;
5651 long idx;
5652 int *errorp; /* set to TRUE when something wrong */
5653{
5654 listitem_T *li;
5655
5656 li = list_find(l, idx);
5657 if (li == NULL)
5658 {
5659 if (errorp != NULL)
5660 *errorp = TRUE;
5661 return -1L;
5662 }
5663 return get_tv_number_chk(&li->li_tv, errorp);
5664}
5665
5666/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005667 * Locate "item" list "l" and return its index.
5668 * Returns -1 when "item" is not in the list.
5669 */
5670 static long
5671list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005672 list_T *l;
5673 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005674{
5675 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005676 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005677
5678 if (l == NULL)
5679 return -1;
5680 idx = 0;
5681 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5682 ++idx;
5683 if (li == NULL)
5684 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005685 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005686}
5687
5688/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 * Append item "item" to the end of list "l".
5690 */
5691 static void
5692list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 list_T *l;
5694 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695{
5696 if (l->lv_last == NULL)
5697 {
5698 /* empty list */
5699 l->lv_first = item;
5700 l->lv_last = item;
5701 item->li_prev = NULL;
5702 }
5703 else
5704 {
5705 l->lv_last->li_next = item;
5706 item->li_prev = l->lv_last;
5707 l->lv_last = item;
5708 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005709 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005710 item->li_next = NULL;
5711}
5712
5713/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005714 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005715 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 */
5717 static int
5718list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005719 list_T *l;
5720 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005722 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723
Bram Moolenaar05159a02005-02-26 23:04:13 +00005724 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005726 copy_tv(tv, &li->li_tv);
5727 list_append(l, li);
5728 return OK;
5729}
5730
5731/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005732 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005733 * Return FAIL when out of memory.
5734 */
5735 int
5736list_append_dict(list, dict)
5737 list_T *list;
5738 dict_T *dict;
5739{
5740 listitem_T *li = listitem_alloc();
5741
5742 if (li == NULL)
5743 return FAIL;
5744 li->li_tv.v_type = VAR_DICT;
5745 li->li_tv.v_lock = 0;
5746 li->li_tv.vval.v_dict = dict;
5747 list_append(list, li);
5748 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 return OK;
5750}
5751
5752/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005753 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005754 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005755 * Returns FAIL when out of memory.
5756 */
5757 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005758list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005759 list_T *l;
5760 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005761 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005762{
5763 listitem_T *li = listitem_alloc();
5764
5765 if (li == NULL)
5766 return FAIL;
5767 list_append(l, li);
5768 li->li_tv.v_type = VAR_STRING;
5769 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005770 if (str == NULL)
5771 li->li_tv.vval.v_string = NULL;
5772 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005773 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005774 return FAIL;
5775 return OK;
5776}
5777
5778/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005779 * Append "n" to list "l".
5780 * Returns FAIL when out of memory.
5781 */
5782 static int
5783list_append_number(l, n)
5784 list_T *l;
5785 varnumber_T n;
5786{
5787 listitem_T *li;
5788
5789 li = listitem_alloc();
5790 if (li == NULL)
5791 return FAIL;
5792 li->li_tv.v_type = VAR_NUMBER;
5793 li->li_tv.v_lock = 0;
5794 li->li_tv.vval.v_number = n;
5795 list_append(l, li);
5796 return OK;
5797}
5798
5799/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005800 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005801 * If "item" is NULL append at the end.
5802 * Return FAIL when out of memory.
5803 */
5804 static int
5805list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 list_T *l;
5807 typval_T *tv;
5808 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005809{
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811
5812 if (ni == NULL)
5813 return FAIL;
5814 copy_tv(tv, &ni->li_tv);
5815 if (item == NULL)
5816 /* Append new item at end of list. */
5817 list_append(l, ni);
5818 else
5819 {
5820 /* Insert new item before existing item. */
5821 ni->li_prev = item->li_prev;
5822 ni->li_next = item;
5823 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005824 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005825 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005826 ++l->lv_idx;
5827 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005828 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005829 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005830 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005831 l->lv_idx_item = NULL;
5832 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005834 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005835 }
5836 return OK;
5837}
5838
5839/*
5840 * Extend "l1" with "l2".
5841 * If "bef" is NULL append at the end, otherwise insert before this item.
5842 * Returns FAIL when out of memory.
5843 */
5844 static int
5845list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 list_T *l1;
5847 list_T *l2;
5848 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849{
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005851
5852 for (item = l2->lv_first; item != NULL; item = item->li_next)
5853 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5854 return FAIL;
5855 return OK;
5856}
5857
5858/*
5859 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5860 * Return FAIL when out of memory.
5861 */
5862 static int
5863list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 list_T *l1;
5865 list_T *l2;
5866 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867{
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005869
5870 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005871 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005872 if (l == NULL)
5873 return FAIL;
5874 tv->v_type = VAR_LIST;
5875 tv->vval.v_list = l;
5876
5877 /* append all items from the second list */
5878 return list_extend(l, l2, NULL);
5879}
5880
5881/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005882 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 * Returns NULL when out of memory.
5886 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005887 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005888list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005891 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892{
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *copy;
5894 listitem_T *item;
5895 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896
5897 if (orig == NULL)
5898 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899
5900 copy = list_alloc();
5901 if (copy != NULL)
5902 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005903 if (copyID != 0)
5904 {
5905 /* Do this before adding the items, because one of the items may
5906 * refer back to this list. */
5907 orig->lv_copyID = copyID;
5908 orig->lv_copylist = copy;
5909 }
5910 for (item = orig->lv_first; item != NULL && !got_int;
5911 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 {
5913 ni = listitem_alloc();
5914 if (ni == NULL)
5915 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005916 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005917 {
5918 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5919 {
5920 vim_free(ni);
5921 break;
5922 }
5923 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005925 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 list_append(copy, ni);
5927 }
5928 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005929 if (item != NULL)
5930 {
5931 list_unref(copy);
5932 copy = NULL;
5933 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 }
5935
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 return copy;
5937}
5938
5939/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005941 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005943 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005944list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 list_T *l;
5946 listitem_T *item;
5947 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005951 /* notify watchers */
5952 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005954 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005955 list_fix_watch(l, ip);
5956 if (ip == item2)
5957 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005959
5960 if (item2->li_next == NULL)
5961 l->lv_last = item->li_prev;
5962 else
5963 item2->li_next->li_prev = item->li_prev;
5964 if (item->li_prev == NULL)
5965 l->lv_first = item2->li_next;
5966 else
5967 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005968 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969}
5970
5971/*
5972 * Return an allocated string with the string representation of a list.
5973 * May return NULL.
5974 */
5975 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005976list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005978 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979{
5980 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981
5982 if (tv->vval.v_list == NULL)
5983 return NULL;
5984 ga_init2(&ga, (int)sizeof(char), 80);
5985 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005986 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 {
5988 vim_free(ga.ga_data);
5989 return NULL;
5990 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 ga_append(&ga, ']');
5992 ga_append(&ga, NUL);
5993 return (char_u *)ga.ga_data;
5994}
5995
5996/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 * Join list "l" into a string in "*gap", using separator "sep".
5998 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006000 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006001 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006002list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006003 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005 char_u *sep;
6006 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006008{
6009 int first = TRUE;
6010 char_u *tofree;
6011 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013 char_u *s;
6014
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006015 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006016 {
6017 if (first)
6018 first = FALSE;
6019 else
6020 ga_concat(gap, sep);
6021
6022 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006023 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006024 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006025 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006026 if (s != NULL)
6027 ga_concat(gap, s);
6028 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006029 if (s == NULL)
6030 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006032 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006033}
6034
6035/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036 * Garbage collection for lists and dictionaries.
6037 *
6038 * We use reference counts to be able to free most items right away when they
6039 * are no longer used. But for composite items it's possible that it becomes
6040 * unused while the reference count is > 0: When there is a recursive
6041 * reference. Example:
6042 * :let l = [1, 2, 3]
6043 * :let d = {9: l}
6044 * :let l[1] = d
6045 *
6046 * Since this is quite unusual we handle this with garbage collection: every
6047 * once in a while find out which lists and dicts are not referenced from any
6048 * variable.
6049 *
6050 * Here is a good reference text about garbage collection (refers to Python
6051 * but it applies to all reference-counting mechanisms):
6052 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006053 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006054
6055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 * Do garbage collection for lists and dicts.
6057 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006058 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006059 int
6060garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006061{
6062 dict_T *dd;
6063 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064 int copyID = ++current_copyID;
6065 buf_T *buf;
6066 win_T *wp;
6067 int i;
6068 funccall_T *fc;
6069 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006070#ifdef FEAT_WINDOWS
6071 tabpage_T *tp;
6072#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006073
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006074 /* Only do this once. */
6075 want_garbage_collect = FALSE;
6076 may_garbage_collect = FALSE;
6077
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006078 /*
6079 * 1. Go through all accessible variables and mark all lists and dicts
6080 * with copyID.
6081 */
6082 /* script-local variables */
6083 for (i = 1; i <= ga_scripts.ga_len; ++i)
6084 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6085
6086 /* buffer-local variables */
6087 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6088 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6089
6090 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006091 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6093
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006094#ifdef FEAT_WINDOWS
6095 /* tabpage-local variables */
6096 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6097 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6098#endif
6099
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006100 /* global variables */
6101 set_ref_in_ht(&globvarht, copyID);
6102
6103 /* function-local variables */
6104 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6105 {
6106 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6107 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6108 }
6109
6110 /*
6111 * 2. Go through the list of dicts and free items without the copyID.
6112 */
6113 for (dd = first_dict; dd != NULL; )
6114 if (dd->dv_copyID != copyID)
6115 {
6116 dict_free(dd);
6117 did_free = TRUE;
6118
6119 /* restart, next dict may also have been freed */
6120 dd = first_dict;
6121 }
6122 else
6123 dd = dd->dv_used_next;
6124
6125 /*
6126 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006127 * But don't free a list that has a watcher (used in a for loop), these
6128 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006129 */
6130 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006131 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006132 {
6133 list_free(ll);
6134 did_free = TRUE;
6135
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006136 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006137 ll = first_list;
6138 }
6139 else
6140 ll = ll->lv_used_next;
6141
6142 return did_free;
6143}
6144
6145/*
6146 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6147 */
6148 static void
6149set_ref_in_ht(ht, copyID)
6150 hashtab_T *ht;
6151 int copyID;
6152{
6153 int todo;
6154 hashitem_T *hi;
6155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006156 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006157 for (hi = ht->ht_array; todo > 0; ++hi)
6158 if (!HASHITEM_EMPTY(hi))
6159 {
6160 --todo;
6161 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6162 }
6163}
6164
6165/*
6166 * Mark all lists and dicts referenced through list "l" with "copyID".
6167 */
6168 static void
6169set_ref_in_list(l, copyID)
6170 list_T *l;
6171 int copyID;
6172{
6173 listitem_T *li;
6174
6175 for (li = l->lv_first; li != NULL; li = li->li_next)
6176 set_ref_in_item(&li->li_tv, copyID);
6177}
6178
6179/*
6180 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6181 */
6182 static void
6183set_ref_in_item(tv, copyID)
6184 typval_T *tv;
6185 int copyID;
6186{
6187 dict_T *dd;
6188 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006189
6190 switch (tv->v_type)
6191 {
6192 case VAR_DICT:
6193 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006195 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 /* Didn't see this dict yet. */
6197 dd->dv_copyID = copyID;
6198 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006199 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006200 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006201
6202 case VAR_LIST:
6203 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006204 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006205 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006206 /* Didn't see this list yet. */
6207 ll->lv_copyID = copyID;
6208 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006209 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006210 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006211 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006212 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006213}
6214
6215/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006216 * Allocate an empty header for a dictionary.
6217 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006218 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006219dict_alloc()
6220{
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006225 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006226 /* Add the list to the hashtable for garbage collection. */
6227 if (first_dict != NULL)
6228 first_dict->dv_used_prev = d;
6229 d->dv_used_next = first_dict;
6230 d->dv_used_prev = NULL;
6231
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006233 d->dv_lock = 0;
6234 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006235 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006238}
6239
6240/*
6241 * Unreference a Dictionary: decrement the reference count and free it when it
6242 * becomes zero.
6243 */
6244 static void
6245dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006247{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006248 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6249 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250}
6251
6252/*
6253 * Free a Dictionary, including all items it contains.
6254 * Ignores the reference count.
6255 */
6256 static void
6257dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006262 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263
Bram Moolenaard9fba312005-06-26 22:34:35 +00006264 /* Avoid that recursive reference to the dict frees us again. */
6265 d->dv_refcount = DEL_REFCOUNT;
6266
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006267 /* Remove the dict from the list of dicts for garbage collection. */
6268 if (d->dv_used_prev == NULL)
6269 first_dict = d->dv_used_next;
6270 else
6271 d->dv_used_prev->dv_used_next = d->dv_used_next;
6272 if (d->dv_used_next != NULL)
6273 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6274
6275 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006276 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006277 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 if (!HASHITEM_EMPTY(hi))
6281 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006282 /* Remove the item before deleting it, just in case there is
6283 * something recursive causing trouble. */
6284 di = HI2DI(hi);
6285 hash_remove(&d->dv_hashtab, hi);
6286 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 --todo;
6288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006289 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006291 vim_free(d);
6292}
6293
6294/*
6295 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 * The "key" is copied to the new item.
6297 * Note that the value of the item "di_tv" still needs to be initialized!
6298 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006299 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006301dictitem_alloc(key)
6302 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006303{
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006305
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006306 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006309 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006310 di->di_flags = 0;
6311 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313}
6314
6315/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316 * Make a copy of a Dictionary item.
6317 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006319dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006321{
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006324 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6325 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006326 if (di != NULL)
6327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006329 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 copy_tv(&org->di_tv, &di->di_tv);
6331 }
6332 return di;
6333}
6334
6335/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006336 * Remove item "item" from Dictionary "dict" and free it.
6337 */
6338 static void
6339dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 dict_T *dict;
6341 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006342{
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 if (HASHITEM_EMPTY(hi))
6347 EMSG2(_(e_intern2), "dictitem_remove()");
6348 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 dictitem_free(item);
6351}
6352
6353/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 * Free a dict item. Also clears the value.
6355 */
6356 static void
6357dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006359{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006360 clear_tv(&item->di_tv);
6361 vim_free(item);
6362}
6363
6364/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6366 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006367 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368 * Returns NULL when out of memory.
6369 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006373 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006374 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006375{
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 dict_T *copy;
6377 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380
6381 if (orig == NULL)
6382 return NULL;
6383
6384 copy = dict_alloc();
6385 if (copy != NULL)
6386 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 if (copyID != 0)
6388 {
6389 orig->dv_copyID = copyID;
6390 orig->dv_copydict = copy;
6391 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006392 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006393 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006397 --todo;
6398
6399 di = dictitem_alloc(hi->hi_key);
6400 if (di == NULL)
6401 break;
6402 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006403 {
6404 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6405 copyID) == FAIL)
6406 {
6407 vim_free(di);
6408 break;
6409 }
6410 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411 else
6412 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6413 if (dict_add(copy, di) == FAIL)
6414 {
6415 dictitem_free(di);
6416 break;
6417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006419 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006420
Bram Moolenaare9a41262005-01-15 22:18:47 +00006421 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006422 if (todo > 0)
6423 {
6424 dict_unref(copy);
6425 copy = NULL;
6426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006427 }
6428
6429 return copy;
6430}
6431
6432/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006436 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006437dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 dict_T *d;
6439 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440{
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006442}
6443
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006445 * Add a number or string entry to dictionary "d".
6446 * When "str" is NULL use number "nr", otherwise use "str".
6447 * Returns FAIL when out of memory and when key already exists.
6448 */
6449 int
6450dict_add_nr_str(d, key, nr, str)
6451 dict_T *d;
6452 char *key;
6453 long nr;
6454 char_u *str;
6455{
6456 dictitem_T *item;
6457
6458 item = dictitem_alloc((char_u *)key);
6459 if (item == NULL)
6460 return FAIL;
6461 item->di_tv.v_lock = 0;
6462 if (str == NULL)
6463 {
6464 item->di_tv.v_type = VAR_NUMBER;
6465 item->di_tv.vval.v_number = nr;
6466 }
6467 else
6468 {
6469 item->di_tv.v_type = VAR_STRING;
6470 item->di_tv.vval.v_string = vim_strsave(str);
6471 }
6472 if (dict_add(d, item) == FAIL)
6473 {
6474 dictitem_free(item);
6475 return FAIL;
6476 }
6477 return OK;
6478}
6479
6480/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 * Get the number of items in a Dictionary.
6482 */
6483 static long
6484dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 if (d == NULL)
6488 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006489 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490}
6491
6492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493 * Find item "key[len]" in Dictionary "d".
6494 * If "len" is negative use strlen(key).
6495 * Returns NULL when not found.
6496 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 char_u *key;
6501 int len;
6502{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503#define AKEYLEN 200
6504 char_u buf[AKEYLEN];
6505 char_u *akey;
6506 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006509 if (len < 0)
6510 akey = key;
6511 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 tofree = akey = vim_strnsave(key, len);
6514 if (akey == NULL)
6515 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006516 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006517 else
6518 {
6519 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006520 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006521 akey = buf;
6522 }
6523
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006525 vim_free(tofree);
6526 if (HASHITEM_EMPTY(hi))
6527 return NULL;
6528 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529}
6530
6531/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006532 * Get a string item from a dictionary.
6533 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006534 * Returns NULL if the entry doesn't exist or out of memory.
6535 */
6536 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006537get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006538 dict_T *d;
6539 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006540 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006541{
6542 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006543 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006544
6545 di = dict_find(d, key, -1);
6546 if (di == NULL)
6547 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006548 s = get_tv_string(&di->di_tv);
6549 if (save && s != NULL)
6550 s = vim_strsave(s);
6551 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006552}
6553
6554/*
6555 * Get a number item from a dictionary.
6556 * Returns 0 if the entry doesn't exist or out of memory.
6557 */
6558 long
6559get_dict_number(d, key)
6560 dict_T *d;
6561 char_u *key;
6562{
6563 dictitem_T *di;
6564
6565 di = dict_find(d, key, -1);
6566 if (di == NULL)
6567 return 0;
6568 return get_tv_number(&di->di_tv);
6569}
6570
6571/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572 * Return an allocated string with the string representation of a Dictionary.
6573 * May return NULL.
6574 */
6575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006579{
6580 garray_T ga;
6581 int first = TRUE;
6582 char_u *tofree;
6583 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006585 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006587 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006588
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006590 return NULL;
6591 ga_init2(&ga, (int)sizeof(char), 80);
6592 ga_append(&ga, '{');
6593
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006594 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006597 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006599 --todo;
6600
6601 if (first)
6602 first = FALSE;
6603 else
6604 ga_concat(&ga, (char_u *)", ");
6605
6606 tofree = string_quote(hi->hi_key, FALSE);
6607 if (tofree != NULL)
6608 {
6609 ga_concat(&ga, tofree);
6610 vim_free(tofree);
6611 }
6612 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006613 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006614 if (s != NULL)
6615 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006616 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006617 if (s == NULL)
6618 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006620 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 if (todo > 0)
6622 {
6623 vim_free(ga.ga_data);
6624 return NULL;
6625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626
6627 ga_append(&ga, '}');
6628 ga_append(&ga, NUL);
6629 return (char_u *)ga.ga_data;
6630}
6631
6632/*
6633 * Allocate a variable for a Dictionary and fill it from "*arg".
6634 * Return OK or FAIL. Returns NOTDONE for {expr}.
6635 */
6636 static int
6637get_dict_tv(arg, rettv, evaluate)
6638 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 int evaluate;
6641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 dict_T *d = NULL;
6643 typval_T tvkey;
6644 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006648 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649
6650 /*
6651 * First check if it's not a curly-braces thing: {expr}.
6652 * Must do this without evaluating, otherwise a function may be called
6653 * twice. Unfortunately this means we need to call eval1() twice for the
6654 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657 if (*start != '}')
6658 {
6659 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6660 return FAIL;
6661 if (*start == '}')
6662 return NOTDONE;
6663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664
6665 if (evaluate)
6666 {
6667 d = dict_alloc();
6668 if (d == NULL)
6669 return FAIL;
6670 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006671 tvkey.v_type = VAR_UNKNOWN;
6672 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006673
6674 *arg = skipwhite(*arg + 1);
6675 while (**arg != '}' && **arg != NUL)
6676 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006677 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678 goto failret;
6679 if (**arg != ':')
6680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006681 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 goto failret;
6684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006685 key = get_tv_string_buf_chk(&tvkey, buf);
6686 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006688 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6689 if (key != NULL)
6690 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006691 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 goto failret;
6693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006694
6695 *arg = skipwhite(*arg + 1);
6696 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006698 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699 goto failret;
6700 }
6701 if (evaluate)
6702 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006703 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 if (item != NULL)
6705 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006706 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006707 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006708 clear_tv(&tv);
6709 goto failret;
6710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006711 item = dictitem_alloc(key);
6712 clear_tv(&tvkey);
6713 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006716 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006717 if (dict_add(d, item) == FAIL)
6718 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719 }
6720 }
6721
6722 if (**arg == '}')
6723 break;
6724 if (**arg != ',')
6725 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006726 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727 goto failret;
6728 }
6729 *arg = skipwhite(*arg + 1);
6730 }
6731
6732 if (**arg != '}')
6733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006734 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006735failret:
6736 if (evaluate)
6737 dict_free(d);
6738 return FAIL;
6739 }
6740
6741 *arg = skipwhite(*arg + 1);
6742 if (evaluate)
6743 {
6744 rettv->v_type = VAR_DICT;
6745 rettv->vval.v_dict = d;
6746 ++d->dv_refcount;
6747 }
6748
6749 return OK;
6750}
6751
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006753 * Return a string with the string representation of a variable.
6754 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006755 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006757 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006758 * May return NULL;
6759 */
6760 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006761echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006762 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006764 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006765 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006766{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006767 static int recurse = 0;
6768 char_u *r = NULL;
6769
Bram Moolenaar33570922005-01-25 22:26:29 +00006770 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006772 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006773 *tofree = NULL;
6774 return NULL;
6775 }
6776 ++recurse;
6777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778 switch (tv->v_type)
6779 {
6780 case VAR_FUNC:
6781 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006782 r = tv->vval.v_string;
6783 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006784
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006786 if (tv->vval.v_list == NULL)
6787 {
6788 *tofree = NULL;
6789 r = NULL;
6790 }
6791 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6792 {
6793 *tofree = NULL;
6794 r = (char_u *)"[...]";
6795 }
6796 else
6797 {
6798 tv->vval.v_list->lv_copyID = copyID;
6799 *tofree = list2string(tv, copyID);
6800 r = *tofree;
6801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006803
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006805 if (tv->vval.v_dict == NULL)
6806 {
6807 *tofree = NULL;
6808 r = NULL;
6809 }
6810 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6811 {
6812 *tofree = NULL;
6813 r = (char_u *)"{...}";
6814 }
6815 else
6816 {
6817 tv->vval.v_dict->dv_copyID = copyID;
6818 *tofree = dict2string(tv, copyID);
6819 r = *tofree;
6820 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006821 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006823 case VAR_STRING:
6824 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825 *tofree = NULL;
6826 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006830 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006831 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006833
6834 --recurse;
6835 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006836}
6837
6838/*
6839 * Return a string with the string representation of a variable.
6840 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6841 * "numbuf" is used for a number.
6842 * Puts quotes around strings, so that they can be parsed back by eval().
6843 * May return NULL;
6844 */
6845 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006846tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006847 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 char_u **tofree;
6849 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006850 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006851{
6852 switch (tv->v_type)
6853 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006854 case VAR_FUNC:
6855 *tofree = string_quote(tv->vval.v_string, TRUE);
6856 return *tofree;
6857 case VAR_STRING:
6858 *tofree = string_quote(tv->vval.v_string, FALSE);
6859 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006861 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006864 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006865 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006867 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006868}
6869
6870/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 * Return string "str" in ' quotes, doubling ' characters.
6872 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 */
6875 static char_u *
6876string_quote(str, function)
6877 char_u *str;
6878 int function;
6879{
Bram Moolenaar33570922005-01-25 22:26:29 +00006880 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006881 char_u *p, *r, *s;
6882
Bram Moolenaar33570922005-01-25 22:26:29 +00006883 len = (function ? 13 : 3);
6884 if (str != NULL)
6885 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006886 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 for (p = str; *p != NUL; mb_ptr_adv(p))
6888 if (*p == '\'')
6889 ++len;
6890 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006891 s = r = alloc(len);
6892 if (r != NULL)
6893 {
6894 if (function)
6895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006896 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006897 r += 10;
6898 }
6899 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006900 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 if (str != NULL)
6902 for (p = str; *p != NUL; )
6903 {
6904 if (*p == '\'')
6905 *r++ = '\'';
6906 MB_COPY_CHAR(p, r);
6907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006909 if (function)
6910 *r++ = ')';
6911 *r++ = NUL;
6912 }
6913 return s;
6914}
6915
6916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 * Get the value of an environment variable.
6918 * "arg" is pointing to the '$'. It is advanced to after the name.
6919 * If the environment variable was not set, silently assume it is empty.
6920 * Always return OK.
6921 */
6922 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006923get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 int evaluate;
6927{
6928 char_u *string = NULL;
6929 int len;
6930 int cc;
6931 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006932 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934 ++*arg;
6935 name = *arg;
6936 len = get_env_len(arg);
6937 if (evaluate)
6938 {
6939 if (len != 0)
6940 {
6941 cc = name[len];
6942 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006943 /* first try vim_getenv(), fast for normal environment vars */
6944 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006946 {
6947 if (!mustfree)
6948 string = vim_strsave(string);
6949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 else
6951 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006952 if (mustfree)
6953 vim_free(string);
6954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /* next try expanding things like $VIM and ${HOME} */
6956 string = expand_env_save(name - 1);
6957 if (string != NULL && *string == '$')
6958 {
6959 vim_free(string);
6960 string = NULL;
6961 }
6962 }
6963 name[len] = cc;
6964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006965 rettv->v_type = VAR_STRING;
6966 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968
6969 return OK;
6970}
6971
6972/*
6973 * Array with names and number of arguments of all internal functions
6974 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6975 */
6976static struct fst
6977{
6978 char *f_name; /* function name */
6979 char f_min_argc; /* minimal number of arguments */
6980 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006982 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983} functions[] =
6984{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006985 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {"append", 2, 2, f_append},
6987 {"argc", 0, 0, f_argc},
6988 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006989 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006991 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {"bufexists", 1, 1, f_bufexists},
6993 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6994 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6995 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6996 {"buflisted", 1, 1, f_buflisted},
6997 {"bufloaded", 1, 1, f_bufloaded},
6998 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006999 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"bufwinnr", 1, 1, f_bufwinnr},
7001 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007002 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007003 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007004 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {"char2nr", 1, 1, f_char2nr},
7006 {"cindent", 1, 1, f_cindent},
7007 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007008#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007009 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007010 {"complete_add", 1, 1, f_complete_add},
7011 {"complete_check", 0, 0, f_complete_check},
7012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007014 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007015 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007017 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007018 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {"delete", 1, 1, f_delete},
7020 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007021 {"diff_filler", 1, 1, f_diff_filler},
7022 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007023 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007025 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"eventhandler", 0, 0, f_eventhandler},
7027 {"executable", 1, 1, f_executable},
7028 {"exists", 1, 1, f_exists},
7029 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007030 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007031 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7033 {"filereadable", 1, 1, f_filereadable},
7034 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007035 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007036 {"finddir", 1, 3, f_finddir},
7037 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 {"fnamemodify", 2, 2, f_fnamemodify},
7039 {"foldclosed", 1, 1, f_foldclosed},
7040 {"foldclosedend", 1, 1, f_foldclosedend},
7041 {"foldlevel", 1, 1, f_foldlevel},
7042 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007043 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007045 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007047 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007048 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {"getbufvar", 2, 2, f_getbufvar},
7050 {"getchar", 0, 1, f_getchar},
7051 {"getcharmod", 0, 0, f_getcharmod},
7052 {"getcmdline", 0, 0, f_getcmdline},
7053 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007054 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007056 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007057 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"getfsize", 1, 1, f_getfsize},
7059 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007060 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007061 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007062 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007063 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007064 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007065 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007067 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"getwinposx", 0, 0, f_getwinposx},
7069 {"getwinposy", 0, 0, f_getwinposy},
7070 {"getwinvar", 2, 2, f_getwinvar},
7071 {"glob", 1, 1, f_glob},
7072 {"globpath", 2, 2, f_globpath},
7073 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007075 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7077 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7078 {"histadd", 2, 2, f_histadd},
7079 {"histdel", 1, 2, f_histdel},
7080 {"histget", 1, 2, f_histget},
7081 {"histnr", 1, 1, f_histnr},
7082 {"hlID", 1, 1, f_hlID},
7083 {"hlexists", 1, 1, f_hlexists},
7084 {"hostname", 0, 0, f_hostname},
7085 {"iconv", 3, 3, f_iconv},
7086 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007087 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007088 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007090 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"inputrestore", 0, 0, f_inputrestore},
7092 {"inputsave", 0, 0, f_inputsave},
7093 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007094 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007096 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007098 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007101 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {"libcall", 3, 3, f_libcall},
7103 {"libcallnr", 3, 3, f_libcallnr},
7104 {"line", 1, 1, f_line},
7105 {"line2byte", 1, 1, f_line2byte},
7106 {"lispindent", 1, 1, f_lispindent},
7107 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007108 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007109 {"maparg", 1, 3, f_maparg},
7110 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007111 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007112 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007113 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007114 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007115 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007116 {"max", 1, 1, f_max},
7117 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007118#ifdef vim_mkdir
7119 {"mkdir", 1, 3, f_mkdir},
7120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {"mode", 0, 0, f_mode},
7122 {"nextnonblank", 1, 1, f_nextnonblank},
7123 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007124 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007126 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007127 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007129 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007130 {"reltime", 0, 2, f_reltime},
7131 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {"remote_expr", 2, 3, f_remote_expr},
7133 {"remote_foreground", 1, 1, f_remote_foreground},
7134 {"remote_peek", 1, 2, f_remote_peek},
7135 {"remote_read", 1, 1, f_remote_read},
7136 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007137 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007139 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007141 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007142 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007143 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007144 {"searchpair", 3, 6, f_searchpair},
7145 {"searchpairpos", 3, 6, f_searchpairpos},
7146 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {"server2client", 2, 2, f_server2client},
7148 {"serverlist", 0, 0, f_serverlist},
7149 {"setbufvar", 3, 3, f_setbufvar},
7150 {"setcmdpos", 1, 1, f_setcmdpos},
7151 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007152 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007153 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007154 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007156 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007158 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007160 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007161 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007162 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007163 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007164 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007165 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166#ifdef HAVE_STRFTIME
7167 {"strftime", 1, 2, f_strftime},
7168#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"strlen", 1, 1, f_strlen},
7172 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007173 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {"strtrans", 1, 1, f_strtrans},
7175 {"submatch", 1, 1, f_submatch},
7176 {"substitute", 4, 4, f_substitute},
7177 {"synID", 3, 3, f_synID},
7178 {"synIDattr", 2, 3, f_synIDattr},
7179 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007180 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007182 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007184 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007185 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007187 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {"tolower", 1, 1, f_tolower},
7189 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007190 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {"virtcol", 1, 1, f_virtcol},
7194 {"visualmode", 0, 1, f_visualmode},
7195 {"winbufnr", 1, 1, f_winbufnr},
7196 {"wincol", 0, 0, f_wincol},
7197 {"winheight", 1, 1, f_winheight},
7198 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007199 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007201 {"winrestview", 1, 1, f_winrestview},
7202 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007204 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205};
7206
7207#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7208
7209/*
7210 * Function given to ExpandGeneric() to obtain the list of internal
7211 * or user defined function names.
7212 */
7213 char_u *
7214get_function_name(xp, idx)
7215 expand_T *xp;
7216 int idx;
7217{
7218 static int intidx = -1;
7219 char_u *name;
7220
7221 if (idx == 0)
7222 intidx = -1;
7223 if (intidx < 0)
7224 {
7225 name = get_user_func_name(xp, idx);
7226 if (name != NULL)
7227 return name;
7228 }
7229 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7230 {
7231 STRCPY(IObuff, functions[intidx].f_name);
7232 STRCAT(IObuff, "(");
7233 if (functions[intidx].f_max_argc == 0)
7234 STRCAT(IObuff, ")");
7235 return IObuff;
7236 }
7237
7238 return NULL;
7239}
7240
7241/*
7242 * Function given to ExpandGeneric() to obtain the list of internal or
7243 * user defined variable or function names.
7244 */
7245/*ARGSUSED*/
7246 char_u *
7247get_expr_name(xp, idx)
7248 expand_T *xp;
7249 int idx;
7250{
7251 static int intidx = -1;
7252 char_u *name;
7253
7254 if (idx == 0)
7255 intidx = -1;
7256 if (intidx < 0)
7257 {
7258 name = get_function_name(xp, idx);
7259 if (name != NULL)
7260 return name;
7261 }
7262 return get_user_var_name(xp, ++intidx);
7263}
7264
7265#endif /* FEAT_CMDL_COMPL */
7266
7267/*
7268 * Find internal function in table above.
7269 * Return index, or -1 if not found
7270 */
7271 static int
7272find_internal_func(name)
7273 char_u *name; /* name of the function */
7274{
7275 int first = 0;
7276 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7277 int cmp;
7278 int x;
7279
7280 /*
7281 * Find the function name in the table. Binary search.
7282 */
7283 while (first <= last)
7284 {
7285 x = first + ((unsigned)(last - first) >> 1);
7286 cmp = STRCMP(name, functions[x].f_name);
7287 if (cmp < 0)
7288 last = x - 1;
7289 else if (cmp > 0)
7290 first = x + 1;
7291 else
7292 return x;
7293 }
7294 return -1;
7295}
7296
7297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7299 * name it contains, otherwise return "name".
7300 */
7301 static char_u *
7302deref_func_name(name, lenp)
7303 char_u *name;
7304 int *lenp;
7305{
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007307 int cc;
7308
7309 cc = name[*lenp];
7310 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007311 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 {
7317 *lenp = 0;
7318 return (char_u *)""; /* just in case */
7319 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007320 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007322 }
7323
7324 return name;
7325}
7326
7327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 * Allocate a variable for the result of a function.
7329 * Return OK or FAIL.
7330 */
7331 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7333 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 char_u *name; /* name of the function */
7335 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 char_u **arg; /* argument, pointing to the '(' */
7338 linenr_T firstline; /* first line of range */
7339 linenr_T lastline; /* last line of range */
7340 int *doesrange; /* return: function handled range */
7341 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 char_u *argp;
7345 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007346 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 int argcount = 0; /* number of arguments found */
7348
7349 /*
7350 * Get the arguments.
7351 */
7352 argp = *arg;
7353 while (argcount < MAX_FUNC_ARGS)
7354 {
7355 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7356 if (*argp == ')' || *argp == ',' || *argp == NUL)
7357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7359 {
7360 ret = FAIL;
7361 break;
7362 }
7363 ++argcount;
7364 if (*argp != ',')
7365 break;
7366 }
7367 if (*argp == ')')
7368 ++argp;
7369 else
7370 ret = FAIL;
7371
7372 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 {
7377 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007380 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007384 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 *arg = skipwhite(argp);
7387 return ret;
7388}
7389
7390
7391/*
7392 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007393 * Return OK when the function can't be called, FAIL otherwise.
7394 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 */
7396 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007397call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 char_u *name; /* name of the function */
7400 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007403 typval_T *argvars; /* vars for arguments, must have "argcount"
7404 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 linenr_T firstline; /* first line of range */
7406 linenr_T lastline; /* last line of range */
7407 int *doesrange; /* return: function handled range */
7408 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
7411 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412#define ERROR_UNKNOWN 0
7413#define ERROR_TOOMANY 1
7414#define ERROR_TOOFEW 2
7415#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416#define ERROR_DICT 4
7417#define ERROR_NONE 5
7418#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int error = ERROR_NONE;
7420 int i;
7421 int llen;
7422 ufunc_T *fp;
7423 int cc;
7424#define FLEN_FIXED 40
7425 char_u fname_buf[FLEN_FIXED + 1];
7426 char_u *fname;
7427
7428 /*
7429 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7430 * Change <SNR>123_name() to K_SNR 123_name().
7431 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7432 */
7433 cc = name[len];
7434 name[len] = NUL;
7435 llen = eval_fname_script(name);
7436 if (llen > 0)
7437 {
7438 fname_buf[0] = K_SPECIAL;
7439 fname_buf[1] = KS_EXTRA;
7440 fname_buf[2] = (int)KE_SNR;
7441 i = 3;
7442 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7443 {
7444 if (current_SID <= 0)
7445 error = ERROR_SCRIPT;
7446 else
7447 {
7448 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7449 i = (int)STRLEN(fname_buf);
7450 }
7451 }
7452 if (i + STRLEN(name + llen) < FLEN_FIXED)
7453 {
7454 STRCPY(fname_buf + i, name + llen);
7455 fname = fname_buf;
7456 }
7457 else
7458 {
7459 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7460 if (fname == NULL)
7461 error = ERROR_OTHER;
7462 else
7463 {
7464 mch_memmove(fname, fname_buf, (size_t)i);
7465 STRCPY(fname + i, name + llen);
7466 }
7467 }
7468 }
7469 else
7470 fname = name;
7471
7472 *doesrange = FALSE;
7473
7474
7475 /* execute the function if no errors detected and executing */
7476 if (evaluate && error == ERROR_NONE)
7477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007478 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 error = ERROR_UNKNOWN;
7480
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007481 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
7483 /*
7484 * User defined function.
7485 */
7486 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007489 /* Trigger FuncUndefined event, may load the function. */
7490 if (fp == NULL
7491 && apply_autocmds(EVENT_FUNCUNDEFINED,
7492 fname, fname, TRUE, NULL)
7493 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007495 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 fp = find_func(fname);
7497 }
7498#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007499 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007500 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007501 {
7502 /* loaded a package, search for the function again */
7503 fp = find_func(fname);
7504 }
7505
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 if (fp != NULL)
7507 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007508 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007510 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007512 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007514 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 else
7517 {
7518 /*
7519 * Call the user function.
7520 * Save and restore search patterns, script variables and
7521 * redo buffer.
7522 */
7523 save_search_patterns();
7524 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007525 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007528 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7529 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7530 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007531 /* Function was unreferenced while being used, free it
7532 * now. */
7533 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 restoreRedobuff();
7535 restore_search_patterns();
7536 error = ERROR_NONE;
7537 }
7538 }
7539 }
7540 else
7541 {
7542 /*
7543 * Find the function name in the table, call its implementation.
7544 */
7545 i = find_internal_func(fname);
7546 if (i >= 0)
7547 {
7548 if (argcount < functions[i].f_min_argc)
7549 error = ERROR_TOOFEW;
7550 else if (argcount > functions[i].f_max_argc)
7551 error = ERROR_TOOMANY;
7552 else
7553 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007554 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 error = ERROR_NONE;
7557 }
7558 }
7559 }
7560 /*
7561 * The function call (or "FuncUndefined" autocommand sequence) might
7562 * have been aborted by an error, an interrupt, or an explicitly thrown
7563 * exception that has not been caught so far. This situation can be
7564 * tested for by calling aborting(). For an error in an internal
7565 * function or for the "E132" error in call_user_func(), however, the
7566 * throw point at which the "force_abort" flag (temporarily reset by
7567 * emsg()) is normally updated has not been reached yet. We need to
7568 * update that flag first to make aborting() reliable.
7569 */
7570 update_force_abort();
7571 }
7572 if (error == ERROR_NONE)
7573 ret = OK;
7574
7575 /*
7576 * Report an error unless the argument evaluation or function call has been
7577 * cancelled due to an aborting error, an interrupt, or an exception.
7578 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 if (!aborting())
7580 {
7581 switch (error)
7582 {
7583 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007584 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 break;
7586 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007587 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588 break;
7589 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007590 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591 name);
7592 break;
7593 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007594 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 name);
7596 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007598 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599 name);
7600 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 }
7602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
7604 name[len] = cc;
7605 if (fname != name && fname != fname_buf)
7606 vim_free(fname);
7607
7608 return ret;
7609}
7610
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611/*
7612 * Give an error message with a function name. Handle <SNR> things.
7613 */
7614 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007615emsg_funcname(ermsg, name)
7616 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007617 char_u *name;
7618{
7619 char_u *p;
7620
7621 if (*name == K_SPECIAL)
7622 p = concat_str((char_u *)"<SNR>", name + 3);
7623 else
7624 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007625 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 if (p != name)
7627 vim_free(p);
7628}
7629
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630/*********************************************
7631 * Implementation of the built-in functions
7632 */
7633
7634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007635 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 */
7637 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007638f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 typval_T *argvars;
7640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007647 if ((l = argvars[0].vval.v_list) != NULL
7648 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7649 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 }
7652 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007653 EMSG(_(e_listreq));
7654}
7655
7656/*
7657 * "append(lnum, string/list)" function
7658 */
7659 static void
7660f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *argvars;
7662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007663{
7664 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007665 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 list_T *l = NULL;
7667 listitem_T *li = NULL;
7668 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007669 long added = 0;
7670
Bram Moolenaar0d660222005-01-07 21:51:51 +00007671 lnum = get_tv_lnum(argvars);
7672 if (lnum >= 0
7673 && lnum <= curbuf->b_ml.ml_line_count
7674 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007677 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007678 l = argvars[1].vval.v_list;
7679 if (l == NULL)
7680 return;
7681 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007683 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007684 for (;;)
7685 {
7686 if (l == NULL)
7687 tv = &argvars[1]; /* append a string */
7688 else if (li == NULL)
7689 break; /* end of list */
7690 else
7691 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007692 line = get_tv_string_chk(tv);
7693 if (line == NULL) /* type error */
7694 {
7695 rettv->vval.v_number = 1; /* Failed */
7696 break;
7697 }
7698 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007699 ++added;
7700 if (l == NULL)
7701 break;
7702 li = li->li_next;
7703 }
7704
7705 appended_lines_mark(lnum, added);
7706 if (curwin->w_cursor.lnum > lnum)
7707 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007709 else
7710 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
7714 * "argc()" function
7715 */
7716/* ARGSUSED */
7717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 typval_T *argvars;
7720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
7725/*
7726 * "argidx()" function
7727 */
7728/* ARGSUSED */
7729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007730f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 typval_T *argvars;
7732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
7736
7737/*
7738 * "argv(nr)" function
7739 */
7740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 typval_T *argvars;
7743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 int idx;
7746
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007747 if (argvars[0].v_type != VAR_UNKNOWN)
7748 {
7749 idx = get_tv_number_chk(&argvars[0], NULL);
7750 if (idx >= 0 && idx < ARGCOUNT)
7751 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7752 else
7753 rettv->vval.v_string = NULL;
7754 rettv->v_type = VAR_STRING;
7755 }
7756 else if (rettv_list_alloc(rettv) == OK)
7757 for (idx = 0; idx < ARGCOUNT; ++idx)
7758 list_append_string(rettv->vval.v_list,
7759 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761
7762/*
7763 * "browse(save, title, initdir, default)" function
7764 */
7765/* ARGSUSED */
7766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T *argvars;
7769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771#ifdef FEAT_BROWSE
7772 int save;
7773 char_u *title;
7774 char_u *initdir;
7775 char_u *defname;
7776 char_u buf[NUMBUFLEN];
7777 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007778 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007780 save = get_tv_number_chk(&argvars[0], &error);
7781 title = get_tv_string_chk(&argvars[1]);
7782 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7783 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007785 if (error || title == NULL || initdir == NULL || defname == NULL)
7786 rettv->vval.v_string = NULL;
7787 else
7788 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007789 do_browse(save ? BROWSE_SAVE : 0,
7790 title, defname, NULL, initdir, NULL, curbuf);
7791#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007795}
7796
7797/*
7798 * "browsedir(title, initdir)" function
7799 */
7800/* ARGSUSED */
7801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007805{
7806#ifdef FEAT_BROWSE
7807 char_u *title;
7808 char_u *initdir;
7809 char_u buf[NUMBUFLEN];
7810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007811 title = get_tv_string_chk(&argvars[0]);
7812 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007814 if (title == NULL || initdir == NULL)
7815 rettv->vval.v_string = NULL;
7816 else
7817 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007818 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823}
7824
Bram Moolenaar33570922005-01-25 22:26:29 +00007825static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007826
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827/*
7828 * Find a buffer by number or exact name.
7829 */
7830 static buf_T *
7831find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836 if (avar->v_type == VAR_NUMBER)
7837 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007838 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007841 if (buf == NULL)
7842 {
7843 /* No full path name match, try a match with a URL or a "nofile"
7844 * buffer, these don't use the full path. */
7845 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7846 if (buf->b_fname != NULL
7847 && (path_with_url(buf->b_fname)
7848#ifdef FEAT_QUICKFIX
7849 || bt_nofile(buf)
7850#endif
7851 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007853 break;
7854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 }
7856 return buf;
7857}
7858
7859/*
7860 * "bufexists(expr)" function
7861 */
7862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007863f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 typval_T *argvars;
7865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868}
7869
7870/*
7871 * "buflisted(expr)" function
7872 */
7873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 typval_T *argvars;
7876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 buf_T *buf;
7879
7880 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882}
7883
7884/*
7885 * "bufloaded(expr)" function
7886 */
7887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007888f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 typval_T *argvars;
7890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 buf_T *buf;
7893
7894 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896}
7897
Bram Moolenaar33570922005-01-25 22:26:29 +00007898static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900/*
7901 * Get buffer by number or pattern.
7902 */
7903 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007904get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007907 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 int save_magic;
7909 char_u *save_cpo;
7910 buf_T *buf;
7911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007912 if (tv->v_type == VAR_NUMBER)
7913 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007914 if (tv->v_type != VAR_STRING)
7915 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 if (name == NULL || *name == NUL)
7917 return curbuf;
7918 if (name[0] == '$' && name[1] == NUL)
7919 return lastbuf;
7920
7921 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7922 save_magic = p_magic;
7923 p_magic = TRUE;
7924 save_cpo = p_cpo;
7925 p_cpo = (char_u *)"";
7926
7927 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7928 TRUE, FALSE));
7929
7930 p_magic = save_magic;
7931 p_cpo = save_cpo;
7932
7933 /* If not found, try expanding the name, like done for bufexists(). */
7934 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937 return buf;
7938}
7939
7940/*
7941 * "bufname(expr)" function
7942 */
7943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 typval_T *argvars;
7946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 buf_T *buf;
7949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007950 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 buf = get_buf_tv(&argvars[0]);
7953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 --emsg_off;
7959}
7960
7961/*
7962 * "bufnr(expr)" function
7963 */
7964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 typval_T *argvars;
7967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007970 int error = FALSE;
7971 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007973 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007976 --emsg_off;
7977
7978 /* If the buffer isn't found and the second argument is not zero create a
7979 * new buffer. */
7980 if (buf == NULL
7981 && argvars[1].v_type != VAR_UNKNOWN
7982 && get_tv_number_chk(&argvars[1], &error) != 0
7983 && !error
7984 && (name = get_tv_string_chk(&argvars[0])) != NULL
7985 && !error)
7986 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7987
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007991 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992}
7993
7994/*
7995 * "bufwinnr(nr)" function
7996 */
7997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007998f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007999 typval_T *argvars;
8000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002#ifdef FEAT_WINDOWS
8003 win_T *wp;
8004 int winnr = 0;
8005#endif
8006 buf_T *buf;
8007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008008 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#ifdef FEAT_WINDOWS
8012 for (wp = firstwin; wp; wp = wp->w_next)
8013 {
8014 ++winnr;
8015 if (wp->w_buffer == buf)
8016 break;
8017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021#endif
8022 --emsg_off;
8023}
8024
8025/*
8026 * "byte2line(byte)" function
8027 */
8028/*ARGSUSED*/
8029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 typval_T *argvars;
8032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
8034#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036#else
8037 long boff = 0;
8038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008039 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 (linenr_T)0, &boff);
8045#endif
8046}
8047
8048/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008049 * "byteidx()" function
8050 */
8051/*ARGSUSED*/
8052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008053f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 typval_T *argvars;
8055 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008056{
8057#ifdef FEAT_MBYTE
8058 char_u *t;
8059#endif
8060 char_u *str;
8061 long idx;
8062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008063 str = get_tv_string_chk(&argvars[0]);
8064 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008067 return;
8068
8069#ifdef FEAT_MBYTE
8070 t = str;
8071 for ( ; idx > 0; idx--)
8072 {
8073 if (*t == NUL) /* EOL reached */
8074 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008075 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008076 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008077 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008078#else
8079 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008080 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008081#endif
8082}
8083
8084/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008085 * "call(func, arglist)" function
8086 */
8087 static void
8088f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 typval_T *argvars;
8090 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008091{
8092 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008093 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008094 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008095 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008096 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008098
8099 rettv->vval.v_number = 0;
8100 if (argvars[1].v_type != VAR_LIST)
8101 {
8102 EMSG(_(e_listreq));
8103 return;
8104 }
8105 if (argvars[1].vval.v_list == NULL)
8106 return;
8107
8108 if (argvars[0].v_type == VAR_FUNC)
8109 func = argvars[0].vval.v_string;
8110 else
8111 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112 if (*func == NUL)
8113 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115 if (argvars[2].v_type != VAR_UNKNOWN)
8116 {
8117 if (argvars[2].v_type != VAR_DICT)
8118 {
8119 EMSG(_(e_dictreq));
8120 return;
8121 }
8122 selfdict = argvars[2].vval.v_dict;
8123 }
8124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8126 item = item->li_next)
8127 {
8128 if (argc == MAX_FUNC_ARGS)
8129 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008130 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008131 break;
8132 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008133 /* Make a copy of each argument. This is needed to be able to set
8134 * v_lock to VAR_FIXED in the copy without changing the original list.
8135 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008136 copy_tv(&item->li_tv, &argv[argc++]);
8137 }
8138
8139 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008140 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8142 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008143
8144 /* Free the arguments. */
8145 while (argc > 0)
8146 clear_tv(&argv[--argc]);
8147}
8148
8149/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008150 * "changenr()" function
8151 */
8152/*ARGSUSED*/
8153 static void
8154f_changenr(argvars, rettv)
8155 typval_T *argvars;
8156 typval_T *rettv;
8157{
8158 rettv->vval.v_number = curbuf->b_u_seq_cur;
8159}
8160
8161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 * "char2nr(string)" function
8163 */
8164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008165f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 typval_T *argvars;
8167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169#ifdef FEAT_MBYTE
8170 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008171 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 else
8173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175}
8176
8177/*
8178 * "cindent(lnum)" function
8179 */
8180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008181f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 typval_T *argvars;
8183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184{
8185#ifdef FEAT_CINDENT
8186 pos_T pos;
8187 linenr_T lnum;
8188
8189 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008190 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8192 {
8193 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 curwin->w_cursor = pos;
8196 }
8197 else
8198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200}
8201
8202/*
8203 * "col(string)" function
8204 */
8205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008206f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 typval_T *argvars;
8208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209{
8210 colnr_T col = 0;
8211 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008212 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008214 fp = var2fpos(&argvars[0], FALSE, &fnum);
8215 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 if (fp->col == MAXCOL)
8218 {
8219 /* '> can be MAXCOL, get the length of the line then */
8220 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008221 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 else
8223 col = MAXCOL;
8224 }
8225 else
8226 {
8227 col = fp->col + 1;
8228#ifdef FEAT_VIRTUALEDIT
8229 /* col(".") when the cursor is on the NUL at the end of the line
8230 * because of "coladd" can be seen as an extra column. */
8231 if (virtual_active() && fp == &curwin->w_cursor)
8232 {
8233 char_u *p = ml_get_cursor();
8234
8235 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8236 curwin->w_virtcol - curwin->w_cursor.coladd))
8237 {
8238# ifdef FEAT_MBYTE
8239 int l;
8240
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008241 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 col += l;
8243# else
8244 if (*p != NUL && p[1] == NUL)
8245 ++col;
8246# endif
8247 }
8248 }
8249#endif
8250 }
8251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008252 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
Bram Moolenaar572cb562005-08-05 21:35:02 +00008255#if defined(FEAT_INS_EXPAND)
8256/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008257 * "complete()" function
8258 */
8259/*ARGSUSED*/
8260 static void
8261f_complete(argvars, rettv)
8262 typval_T *argvars;
8263 typval_T *rettv;
8264{
8265 int startcol;
8266
8267 if ((State & INSERT) == 0)
8268 {
8269 EMSG(_("E785: complete() can only be used in Insert mode"));
8270 return;
8271 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008272
8273 /* Check for undo allowed here, because if something was already inserted
8274 * the line was already saved for undo and this check isn't done. */
8275 if (!undo_allowed())
8276 return;
8277
Bram Moolenaarade00832006-03-10 21:46:58 +00008278 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8279 {
8280 EMSG(_(e_invarg));
8281 return;
8282 }
8283
8284 startcol = get_tv_number_chk(&argvars[0], NULL);
8285 if (startcol <= 0)
8286 return;
8287
8288 set_completion(startcol - 1, argvars[1].vval.v_list);
8289}
8290
8291/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008292 * "complete_add()" function
8293 */
8294/*ARGSUSED*/
8295 static void
8296f_complete_add(argvars, rettv)
8297 typval_T *argvars;
8298 typval_T *rettv;
8299{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008300 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008301}
8302
8303/*
8304 * "complete_check()" function
8305 */
8306/*ARGSUSED*/
8307 static void
8308f_complete_check(argvars, rettv)
8309 typval_T *argvars;
8310 typval_T *rettv;
8311{
8312 int saved = RedrawingDisabled;
8313
8314 RedrawingDisabled = 0;
8315 ins_compl_check_keys(0);
8316 rettv->vval.v_number = compl_interrupted;
8317 RedrawingDisabled = saved;
8318}
8319#endif
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321/*
8322 * "confirm(message, buttons[, default [, type]])" function
8323 */
8324/*ARGSUSED*/
8325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 typval_T *argvars;
8328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8331 char_u *message;
8332 char_u *buttons = NULL;
8333 char_u buf[NUMBUFLEN];
8334 char_u buf2[NUMBUFLEN];
8335 int def = 1;
8336 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008337 char_u *typestr;
8338 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008340 message = get_tv_string_chk(&argvars[0]);
8341 if (message == NULL)
8342 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008343 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008345 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8346 if (buttons == NULL)
8347 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008351 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008353 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8354 if (typestr == NULL)
8355 error = TRUE;
8356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008358 switch (TOUPPER_ASC(*typestr))
8359 {
8360 case 'E': type = VIM_ERROR; break;
8361 case 'Q': type = VIM_QUESTION; break;
8362 case 'I': type = VIM_INFO; break;
8363 case 'W': type = VIM_WARNING; break;
8364 case 'G': type = VIM_GENERIC; break;
8365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 }
8367 }
8368 }
8369 }
8370
8371 if (buttons == NULL || *buttons == NUL)
8372 buttons = (char_u *)_("&Ok");
8373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008374 if (error)
8375 rettv->vval.v_number = 0;
8376 else
8377 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 def, NULL);
8379#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#endif
8382}
8383
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008384/*
8385 * "copy()" function
8386 */
8387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008389 typval_T *argvars;
8390 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008391{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008392 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008396 * "count()" function
8397 */
8398 static void
8399f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 typval_T *argvars;
8401 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008402{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008403 long n = 0;
8404 int ic = FALSE;
8405
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008407 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008408 listitem_T *li;
8409 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008410 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 if ((l = argvars[0].vval.v_list) != NULL)
8413 {
8414 li = l->lv_first;
8415 if (argvars[2].v_type != VAR_UNKNOWN)
8416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 int error = FALSE;
8418
8419 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 if (argvars[3].v_type != VAR_UNKNOWN)
8421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422 idx = get_tv_number_chk(&argvars[3], &error);
8423 if (!error)
8424 {
8425 li = list_find(l, idx);
8426 if (li == NULL)
8427 EMSGN(_(e_listidx), idx);
8428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430 if (error)
8431 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008432 }
8433
8434 for ( ; li != NULL; li = li->li_next)
8435 if (tv_equal(&li->li_tv, &argvars[1], ic))
8436 ++n;
8437 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008439 else if (argvars[0].v_type == VAR_DICT)
8440 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 int todo;
8442 dict_T *d;
8443 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008444
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008445 if ((d = argvars[0].vval.v_dict) != NULL)
8446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008447 int error = FALSE;
8448
Bram Moolenaare9a41262005-01-15 22:18:47 +00008449 if (argvars[2].v_type != VAR_UNKNOWN)
8450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 if (argvars[3].v_type != VAR_UNKNOWN)
8453 EMSG(_(e_invarg));
8454 }
8455
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008456 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008457 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008458 {
8459 if (!HASHITEM_EMPTY(hi))
8460 {
8461 --todo;
8462 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8463 ++n;
8464 }
8465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008466 }
8467 }
8468 else
8469 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008470 rettv->vval.v_number = n;
8471}
8472
8473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8475 *
8476 * Checks the existence of a cscope connection.
8477 */
8478/*ARGSUSED*/
8479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 typval_T *argvars;
8482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
8484#ifdef FEAT_CSCOPE
8485 int num = 0;
8486 char_u *dbpath = NULL;
8487 char_u *prepend = NULL;
8488 char_u buf[NUMBUFLEN];
8489
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008490 if (argvars[0].v_type != VAR_UNKNOWN
8491 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 num = (int)get_tv_number(&argvars[0]);
8494 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502#endif
8503}
8504
8505/*
8506 * "cursor(lnum, col)" function
8507 *
8508 * Moves the cursor to the specified line and column
8509 */
8510/*ARGSUSED*/
8511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 typval_T *argvars;
8514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008517#ifdef FEAT_VIRTUALEDIT
8518 long coladd = 0;
8519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaara5525202006-03-02 22:52:09 +00008521 if (argvars[1].v_type == VAR_UNKNOWN)
8522 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008523 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008524
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008525 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008526 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008527 line = pos.lnum;
8528 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008529#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008530 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008531#endif
8532 }
8533 else
8534 {
8535 line = get_tv_lnum(argvars);
8536 col = get_tv_number_chk(&argvars[1], NULL);
8537#ifdef FEAT_VIRTUALEDIT
8538 if (argvars[2].v_type != VAR_UNKNOWN)
8539 coladd = get_tv_number_chk(&argvars[2], NULL);
8540#endif
8541 }
8542 if (line < 0 || col < 0
8543#ifdef FEAT_VIRTUALEDIT
8544 || coladd < 0
8545#endif
8546 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008547 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (line > 0)
8549 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (col > 0)
8551 curwin->w_cursor.col = col - 1;
8552#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008553 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554#endif
8555
8556 /* Make sure the cursor is in a valid position. */
8557 check_cursor();
8558#ifdef FEAT_MBYTE
8559 /* Correct cursor for multi-byte character. */
8560 if (has_mbyte)
8561 mb_adjust_cursor();
8562#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008563
8564 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565}
8566
8567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 */
8570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008575 int noref = 0;
8576
8577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008578 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008579 if (noref < 0 || noref > 1)
8580 EMSG(_(e_invarg));
8581 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008582 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584
8585/*
8586 * "delete()" function
8587 */
8588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 typval_T *argvars;
8591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597}
8598
8599/*
8600 * "did_filetype()" function
8601 */
8602/*ARGSUSED*/
8603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 typval_T *argvars;
8606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
8608#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612#endif
8613}
8614
8615/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008616 * "diff_filler()" function
8617 */
8618/*ARGSUSED*/
8619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 typval_T *argvars;
8622 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008623{
8624#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008626#endif
8627}
8628
8629/*
8630 * "diff_hlID()" function
8631 */
8632/*ARGSUSED*/
8633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 typval_T *argvars;
8636 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008637{
8638#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008640 static linenr_T prev_lnum = 0;
8641 static int changedtick = 0;
8642 static int fnum = 0;
8643 static int change_start = 0;
8644 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008645 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008646 int filler_lines;
8647 int col;
8648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 if (lnum < 0) /* ignore type error in {lnum} arg */
8650 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008651 if (lnum != prev_lnum
8652 || changedtick != curbuf->b_changedtick
8653 || fnum != curbuf->b_fnum)
8654 {
8655 /* New line, buffer, change: need to get the values. */
8656 filler_lines = diff_check(curwin, lnum);
8657 if (filler_lines < 0)
8658 {
8659 if (filler_lines == -1)
8660 {
8661 change_start = MAXCOL;
8662 change_end = -1;
8663 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8664 hlID = HLF_ADD; /* added line */
8665 else
8666 hlID = HLF_CHD; /* changed line */
8667 }
8668 else
8669 hlID = HLF_ADD; /* added line */
8670 }
8671 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008672 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008673 prev_lnum = lnum;
8674 changedtick = curbuf->b_changedtick;
8675 fnum = curbuf->b_fnum;
8676 }
8677
8678 if (hlID == HLF_CHD || hlID == HLF_TXD)
8679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008680 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008681 if (col >= change_start && col <= change_end)
8682 hlID = HLF_TXD; /* changed text */
8683 else
8684 hlID = HLF_CHD; /* changed line */
8685 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008686 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008687#endif
8688}
8689
8690/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008691 * "empty({expr})" function
8692 */
8693 static void
8694f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008695 typval_T *argvars;
8696 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008697{
8698 int n;
8699
8700 switch (argvars[0].v_type)
8701 {
8702 case VAR_STRING:
8703 case VAR_FUNC:
8704 n = argvars[0].vval.v_string == NULL
8705 || *argvars[0].vval.v_string == NUL;
8706 break;
8707 case VAR_NUMBER:
8708 n = argvars[0].vval.v_number == 0;
8709 break;
8710 case VAR_LIST:
8711 n = argvars[0].vval.v_list == NULL
8712 || argvars[0].vval.v_list->lv_first == NULL;
8713 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008714 case VAR_DICT:
8715 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008716 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008717 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008718 default:
8719 EMSG2(_(e_intern2), "f_empty()");
8720 n = 0;
8721 }
8722
8723 rettv->vval.v_number = n;
8724}
8725
8726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * "escape({string}, {chars})" function
8728 */
8729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 typval_T *argvars;
8732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 char_u buf[NUMBUFLEN];
8735
Bram Moolenaar758711c2005-02-02 23:11:38 +00008736 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8737 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739}
8740
8741/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008742 * "eval()" function
8743 */
8744/*ARGSUSED*/
8745 static void
8746f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008747 typval_T *argvars;
8748 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008749{
8750 char_u *s;
8751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752 s = get_tv_string_chk(&argvars[0]);
8753 if (s != NULL)
8754 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008756 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8757 {
8758 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008759 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008760 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008761 else if (*s != NUL)
8762 EMSG(_(e_trailing));
8763}
8764
8765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 * "eventhandler()" function
8767 */
8768/*ARGSUSED*/
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
8777/*
8778 * "executable()" function
8779 */
8780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 typval_T *argvars;
8783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786}
8787
8788/*
8789 * "exists()" function
8790 */
8791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008793 typval_T *argvars;
8794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
8796 char_u *p;
8797 char_u *name;
8798 int n = FALSE;
8799 int len = 0;
8800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 if (*p == '$') /* environment variable */
8803 {
8804 /* first try "normal" environment variables (fast) */
8805 if (mch_getenv(p + 1) != NULL)
8806 n = TRUE;
8807 else
8808 {
8809 /* try expanding things like $VIM and ${HOME} */
8810 p = expand_env_save(p);
8811 if (p != NULL && *p != '$')
8812 n = TRUE;
8813 vim_free(p);
8814 }
8815 }
8816 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008819 if (*skipwhite(p) != NUL)
8820 n = FALSE; /* trailing garbage */
8821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 else if (*p == '*') /* internal or user defined function */
8823 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008824 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826 else if (*p == ':')
8827 {
8828 n = cmd_exists(p + 1);
8829 }
8830 else if (*p == '#')
8831 {
8832#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008833 if (p[1] == '#')
8834 n = autocmd_supported(p + 2);
8835 else
8836 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837#endif
8838 }
8839 else /* internal variable */
8840 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008841 char_u *tofree;
8842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008844 /* get_name_len() takes care of expanding curly braces */
8845 name = p;
8846 len = get_name_len(&p, &tofree, TRUE, FALSE);
8847 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008849 if (tofree != NULL)
8850 name = tofree;
8851 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8852 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008854 /* handle d.key, l[idx], f(expr) */
8855 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8856 if (n)
8857 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
8859 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008860 if (*p != NUL)
8861 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008863 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 }
8865
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867}
8868
8869/*
8870 * "expand()" function
8871 */
8872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 typval_T *argvars;
8875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 char_u *s;
8878 int len;
8879 char_u *errormsg;
8880 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8881 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->v_type = VAR_STRING;
8885 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 if (*s == '%' || *s == '#' || *s == '<')
8887 {
8888 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 --emsg_off;
8891 }
8892 else
8893 {
8894 /* When the optional second argument is non-zero, don't remove matches
8895 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 if (argvars[1].v_type != VAR_UNKNOWN
8897 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 if (!error)
8900 {
8901 ExpandInit(&xpc);
8902 xpc.xp_context = EXPAND_FILES;
8903 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 }
8905 else
8906 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
8908}
8909
8910/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008912 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008913 */
8914 static void
8915f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 typval_T *argvars;
8917 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008918{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008919 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008920 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008921 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 list_T *l1, *l2;
8923 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008924 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008926
Bram Moolenaare9a41262005-01-15 22:18:47 +00008927 l1 = argvars[0].vval.v_list;
8928 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008929 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8930 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 {
8932 if (argvars[2].v_type != VAR_UNKNOWN)
8933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 before = get_tv_number_chk(&argvars[2], &error);
8935 if (error)
8936 return; /* type error; errmsg already given */
8937
Bram Moolenaar758711c2005-02-02 23:11:38 +00008938 if (before == l1->lv_len)
8939 item = NULL;
8940 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008942 item = list_find(l1, before);
8943 if (item == NULL)
8944 {
8945 EMSGN(_(e_listidx), before);
8946 return;
8947 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 }
8949 }
8950 else
8951 item = NULL;
8952 list_extend(l1, l2, item);
8953
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 copy_tv(&argvars[0], rettv);
8955 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 dict_T *d1, *d2;
8960 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 char_u *action;
8962 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008964 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008965
8966 d1 = argvars[0].vval.v_dict;
8967 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008968 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8969 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008970 {
8971 /* Check the third argument. */
8972 if (argvars[2].v_type != VAR_UNKNOWN)
8973 {
8974 static char *(av[]) = {"keep", "force", "error"};
8975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 action = get_tv_string_chk(&argvars[2]);
8977 if (action == NULL)
8978 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 for (i = 0; i < 3; ++i)
8980 if (STRCMP(action, av[i]) == 0)
8981 break;
8982 if (i == 3)
8983 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008984 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008985 return;
8986 }
8987 }
8988 else
8989 action = (char_u *)"force";
8990
8991 /* Go over all entries in the second dict and add them to the
8992 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008993 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008996 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008998 --todo;
8999 di1 = dict_find(d1, hi2->hi_key, -1);
9000 if (di1 == NULL)
9001 {
9002 di1 = dictitem_copy(HI2DI(hi2));
9003 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9004 dictitem_free(di1);
9005 }
9006 else if (*action == 'e')
9007 {
9008 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9009 break;
9010 }
9011 else if (*action == 'f')
9012 {
9013 clear_tv(&di1->di_tv);
9014 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009016 }
9017 }
9018
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 copy_tv(&argvars[0], rettv);
9020 }
9021 }
9022 else
9023 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009024}
9025
9026/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009027 * "feedkeys()" function
9028 */
9029/*ARGSUSED*/
9030 static void
9031f_feedkeys(argvars, rettv)
9032 typval_T *argvars;
9033 typval_T *rettv;
9034{
9035 int remap = TRUE;
9036 char_u *keys, *flags;
9037 char_u nbuf[NUMBUFLEN];
9038 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009039 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009040
9041 rettv->vval.v_number = 0;
9042 keys = get_tv_string(&argvars[0]);
9043 if (*keys != NUL)
9044 {
9045 if (argvars[1].v_type != VAR_UNKNOWN)
9046 {
9047 flags = get_tv_string_buf(&argvars[1], nbuf);
9048 for ( ; *flags != NUL; ++flags)
9049 {
9050 switch (*flags)
9051 {
9052 case 'n': remap = FALSE; break;
9053 case 'm': remap = TRUE; break;
9054 case 't': typed = TRUE; break;
9055 }
9056 }
9057 }
9058
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009059 /* Need to escape K_SPECIAL and CSI before putting the string in the
9060 * typeahead buffer. */
9061 keys_esc = vim_strsave_escape_csi(keys);
9062 if (keys_esc != NULL)
9063 {
9064 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009065 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009066 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009067 if (vgetc_busy)
9068 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009069 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009070 }
9071}
9072
9073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 * "filereadable()" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 FILE *fd;
9082 char_u *p;
9083 int n;
9084
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9087 {
9088 n = TRUE;
9089 fclose(fd);
9090 }
9091 else
9092 n = FALSE;
9093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095}
9096
9097/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009098 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 * rights to write into.
9100 */
9101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009103 typval_T *argvars;
9104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009106 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108
Bram Moolenaar33570922005-01-25 22:26:29 +00009109static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009110
9111 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009112findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 typval_T *argvars;
9114 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009115 int dir;
9116{
9117#ifdef FEAT_SEARCHPATH
9118 char_u *fname;
9119 char_u *fresult = NULL;
9120 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9121 char_u *p;
9122 char_u pathbuf[NUMBUFLEN];
9123 int count = 1;
9124 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009125 int error = FALSE;
9126#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009127
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009128 rettv->vval.v_string = NULL;
9129 rettv->v_type = VAR_STRING;
9130
9131#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009133
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009134 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9137 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009138 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139 else
9140 {
9141 if (*p != NUL)
9142 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009145 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009147 }
9148
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009149 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9150 error = TRUE;
9151
9152 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 do
9155 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009156 if (rettv->v_type == VAR_STRING)
9157 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009158 fresult = find_file_in_path_option(first ? fname : NULL,
9159 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009160 0, first, path, dir, NULL,
9161 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009163
9164 if (fresult != NULL && rettv->v_type == VAR_LIST)
9165 list_append_string(rettv->vval.v_list, fresult, -1);
9166
9167 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009168 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009169
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009170 if (rettv->v_type == VAR_STRING)
9171 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009172#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009173}
9174
Bram Moolenaar33570922005-01-25 22:26:29 +00009175static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9176static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009177
9178/*
9179 * Implementation of map() and filter().
9180 */
9181 static void
9182filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009183 typval_T *argvars;
9184 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009185 int map;
9186{
9187 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009188 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009189 listitem_T *li, *nli;
9190 list_T *l = NULL;
9191 dictitem_T *di;
9192 hashtab_T *ht;
9193 hashitem_T *hi;
9194 dict_T *d = NULL;
9195 typval_T save_val;
9196 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009197 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009198 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009199 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009200 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009201
9202 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009203 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009204 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009205 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009206 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009207 return;
9208 }
9209 else if (argvars[0].v_type == VAR_DICT)
9210 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009211 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009212 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009213 return;
9214 }
9215 else
9216 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009217 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009218 return;
9219 }
9220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 expr = get_tv_string_buf_chk(&argvars[1], buf);
9222 /* On type errors, the preceding call has already displayed an error
9223 * message. Avoid a misleading error message for an empty string that
9224 * was not passed as argument. */
9225 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 prepare_vimvar(VV_VAL, &save_val);
9228 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009229
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009230 /* We reset "did_emsg" to be able to detect whether an error
9231 * occurred during evaluation of the expression. */
9232 save_did_emsg = did_emsg;
9233 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 prepare_vimvar(VV_KEY, &save_key);
9238 vimvars[VV_KEY].vv_type = VAR_STRING;
9239
9240 ht = &d->dv_hashtab;
9241 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009242 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 if (!HASHITEM_EMPTY(hi))
9246 {
9247 --todo;
9248 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009249 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 break;
9251 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009252 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009253 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009254 break;
9255 if (!map && rem)
9256 dictitem_remove(d, di);
9257 clear_tv(&vimvars[VV_KEY].vv_tv);
9258 }
9259 }
9260 hash_unlock(ht);
9261
9262 restore_vimvar(VV_KEY, &save_key);
9263 }
9264 else
9265 {
9266 for (li = l->lv_first; li != NULL; li = nli)
9267 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009268 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009269 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009271 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009272 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009273 break;
9274 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009276 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009277 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009280
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009281 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009282 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009283
9284 copy_tv(&argvars[0], rettv);
9285}
9286
9287 static int
9288filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 char_u *expr;
9291 int map;
9292 int *remp;
9293{
Bram Moolenaar33570922005-01-25 22:26:29 +00009294 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009295 char_u *s;
9296
Bram Moolenaar33570922005-01-25 22:26:29 +00009297 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009298 s = expr;
9299 if (eval1(&s, &rettv, TRUE) == FAIL)
9300 return FAIL;
9301 if (*s != NUL) /* check for trailing chars after expr */
9302 {
9303 EMSG2(_(e_invexpr2), s);
9304 return FAIL;
9305 }
9306 if (map)
9307 {
9308 /* map(): replace the list item value */
9309 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009310 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 *tv = rettv;
9312 }
9313 else
9314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009315 int error = FALSE;
9316
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009319 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320 /* On type error, nothing has been removed; return FAIL to stop the
9321 * loop. The error message was given by get_tv_number_chk(). */
9322 if (error)
9323 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009324 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009326 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009327}
9328
9329/*
9330 * "filter()" function
9331 */
9332 static void
9333f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 typval_T *argvars;
9335 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009336{
9337 filter_map(argvars, rettv, FALSE);
9338}
9339
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009341 * "finddir({fname}[, {path}[, {count}]])" function
9342 */
9343 static void
9344f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009345 typval_T *argvars;
9346 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009347{
9348 findfilendir(argvars, rettv, TRUE);
9349}
9350
9351/*
9352 * "findfile({fname}[, {path}[, {count}]])" function
9353 */
9354 static void
9355f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009358{
9359 findfilendir(argvars, rettv, FALSE);
9360}
9361
9362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 * "fnamemodify({fname}, {mods})" function
9364 */
9365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009367 typval_T *argvars;
9368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 char_u *fname;
9371 char_u *mods;
9372 int usedlen = 0;
9373 int len;
9374 char_u *fbuf = NULL;
9375 char_u buf[NUMBUFLEN];
9376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 fname = get_tv_string_chk(&argvars[0]);
9378 mods = get_tv_string_buf_chk(&argvars[1], buf);
9379 if (fname == NULL || mods == NULL)
9380 fname = NULL;
9381 else
9382 {
9383 len = (int)STRLEN(fname);
9384 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 vim_free(fbuf);
9393}
9394
Bram Moolenaar33570922005-01-25 22:26:29 +00009395static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396
9397/*
9398 * "foldclosed()" function
9399 */
9400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 typval_T *argvars;
9403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 int end;
9405{
9406#ifdef FEAT_FOLDING
9407 linenr_T lnum;
9408 linenr_T first, last;
9409
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9412 {
9413 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9414 {
9415 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 return;
9420 }
9421 }
9422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425
9426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 * "foldclosed()" function
9428 */
9429 static void
9430f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009431 typval_T *argvars;
9432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433{
9434 foldclosed_both(argvars, rettv, FALSE);
9435}
9436
9437/*
9438 * "foldclosedend()" function
9439 */
9440 static void
9441f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 typval_T *argvars;
9443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009444{
9445 foldclosed_both(argvars, rettv, TRUE);
9446}
9447
9448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 * "foldlevel()" function
9450 */
9451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009453 typval_T *argvars;
9454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_FOLDING
9457 linenr_T lnum;
9458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 else
9463#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466
9467/*
9468 * "foldtext()" function
9469 */
9470/*ARGSUSED*/
9471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009473 typval_T *argvars;
9474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475{
9476#ifdef FEAT_FOLDING
9477 linenr_T lnum;
9478 char_u *s;
9479 char_u *r;
9480 int len;
9481 char *txt;
9482#endif
9483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->v_type = VAR_STRING;
9485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009487 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9488 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9489 <= curbuf->b_ml.ml_line_count
9490 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 {
9492 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009493 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9494 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 {
9496 if (!linewhite(lnum))
9497 break;
9498 ++lnum;
9499 }
9500
9501 /* Find interesting text in this line. */
9502 s = skipwhite(ml_get(lnum));
9503 /* skip C comment-start */
9504 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009507 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009508 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009509 {
9510 s = skipwhite(ml_get(lnum + 1));
9511 if (*s == '*')
9512 s = skipwhite(s + 1);
9513 }
9514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 txt = _("+-%s%3ld lines: ");
9516 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009517 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 + 20 /* for %3ld */
9519 + STRLEN(s))); /* concatenated */
9520 if (r != NULL)
9521 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009522 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9523 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9524 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 len = (int)STRLEN(r);
9526 STRCAT(r, s);
9527 /* remove 'foldmarker' and 'commentstring' */
9528 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 }
9531 }
9532#endif
9533}
9534
9535/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009536 * "foldtextresult(lnum)" function
9537 */
9538/*ARGSUSED*/
9539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009541 typval_T *argvars;
9542 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009543{
9544#ifdef FEAT_FOLDING
9545 linenr_T lnum;
9546 char_u *text;
9547 char_u buf[51];
9548 foldinfo_T foldinfo;
9549 int fold_count;
9550#endif
9551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->v_type = VAR_STRING;
9553 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009554#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 /* treat illegal types and illegal string values for {lnum} the same */
9557 if (lnum < 0)
9558 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009559 fold_count = foldedCount(curwin, lnum, &foldinfo);
9560 if (fold_count > 0)
9561 {
9562 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9563 &foldinfo, buf);
9564 if (text == buf)
9565 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009567 }
9568#endif
9569}
9570
9571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 * "foreground()" function
9573 */
9574/*ARGSUSED*/
9575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009577 typval_T *argvars;
9578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#ifdef FEAT_GUI
9582 if (gui.in_use)
9583 gui_mch_set_foreground();
9584#else
9585# ifdef WIN32
9586 win32_set_foreground();
9587# endif
9588#endif
9589}
9590
9591/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009592 * "function()" function
9593 */
9594/*ARGSUSED*/
9595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009599{
9600 char_u *s;
9601
Bram Moolenaara7043832005-01-21 11:56:39 +00009602 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009604 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009605 EMSG2(_(e_invarg2), s);
9606 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009607 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009608 else
9609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 rettv->vval.v_string = vim_strsave(s);
9611 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612 }
9613}
9614
9615/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009616 * "garbagecollect()" function
9617 */
9618/*ARGSUSED*/
9619 static void
9620f_garbagecollect(argvars, rettv)
9621 typval_T *argvars;
9622 typval_T *rettv;
9623{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009624 /* This is postponed until we are back at the toplevel, because we may be
9625 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9626 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009627}
9628
9629/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009630 * "get()" function
9631 */
9632 static void
9633f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 typval_T *argvars;
9635 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009636{
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 listitem_T *li;
9638 list_T *l;
9639 dictitem_T *di;
9640 dict_T *d;
9641 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009644 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 int error = FALSE;
9648
9649 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9650 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009652 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009653 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 else if (argvars[0].v_type == VAR_DICT)
9655 {
9656 if ((d = argvars[0].vval.v_dict) != NULL)
9657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009658 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 if (di != NULL)
9660 tv = &di->di_tv;
9661 }
9662 }
9663 else
9664 EMSG2(_(e_listdictarg), "get()");
9665
9666 if (tv == NULL)
9667 {
9668 if (argvars[2].v_type == VAR_UNKNOWN)
9669 rettv->vval.v_number = 0;
9670 else
9671 copy_tv(&argvars[2], rettv);
9672 }
9673 else
9674 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009675}
9676
Bram Moolenaar342337a2005-07-21 21:11:17 +00009677static 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 +00009678
9679/*
9680 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009681 * Return a range (from start to end) of lines in rettv from the specified
9682 * buffer.
9683 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009684 */
9685 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009686get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009687 buf_T *buf;
9688 linenr_T start;
9689 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009690 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009691 typval_T *rettv;
9692{
9693 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009694
Bram Moolenaar342337a2005-07-21 21:11:17 +00009695 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009696 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009697 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009698 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009699 }
9700 else
9701 rettv->vval.v_number = 0;
9702
9703 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9704 return;
9705
9706 if (!retlist)
9707 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009708 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9709 p = ml_get_buf(buf, start, FALSE);
9710 else
9711 p = (char_u *)"";
9712
9713 rettv->v_type = VAR_STRING;
9714 rettv->vval.v_string = vim_strsave(p);
9715 }
9716 else
9717 {
9718 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 return;
9720
9721 if (start < 1)
9722 start = 1;
9723 if (end > buf->b_ml.ml_line_count)
9724 end = buf->b_ml.ml_line_count;
9725 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009726 if (list_append_string(rettv->vval.v_list,
9727 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009728 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009729 }
9730}
9731
9732/*
9733 * "getbufline()" function
9734 */
9735 static void
9736f_getbufline(argvars, rettv)
9737 typval_T *argvars;
9738 typval_T *rettv;
9739{
9740 linenr_T lnum;
9741 linenr_T end;
9742 buf_T *buf;
9743
9744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9745 ++emsg_off;
9746 buf = get_buf_tv(&argvars[0]);
9747 --emsg_off;
9748
Bram Moolenaar661b1822005-07-28 22:36:45 +00009749 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009750 if (argvars[2].v_type == VAR_UNKNOWN)
9751 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009752 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009753 end = get_tv_lnum_buf(&argvars[2], buf);
9754
Bram Moolenaar342337a2005-07-21 21:11:17 +00009755 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009756}
9757
Bram Moolenaar0d660222005-01-07 21:51:51 +00009758/*
9759 * "getbufvar()" function
9760 */
9761 static void
9762f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009763 typval_T *argvars;
9764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009765{
9766 buf_T *buf;
9767 buf_T *save_curbuf;
9768 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9772 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009773 ++emsg_off;
9774 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009775
9776 rettv->v_type = VAR_STRING;
9777 rettv->vval.v_string = NULL;
9778
9779 if (buf != NULL && varname != NULL)
9780 {
9781 if (*varname == '&') /* buffer-local-option */
9782 {
9783 /* set curbuf to be our buf, temporarily */
9784 save_curbuf = curbuf;
9785 curbuf = buf;
9786
9787 get_option_tv(&varname, rettv, TRUE);
9788
9789 /* restore previous notion of curbuf */
9790 curbuf = save_curbuf;
9791 }
9792 else
9793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 if (*varname == NUL)
9795 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9796 * scope prefix before the NUL byte is required by
9797 * find_var_in_ht(). */
9798 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009799 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009800 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009801 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009802 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009803 }
9804 }
9805
9806 --emsg_off;
9807}
9808
9809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 * "getchar()" function
9811 */
9812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009814 typval_T *argvars;
9815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009820 /* Position the cursor. Needed after a message that ends in a space. */
9821 windgoto(msg_row, msg_col);
9822
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 ++no_mapping;
9824 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 /* getchar(): blocking wait. */
9827 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009828 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 /* getchar(1): only check if char avail */
9830 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 else if (error || vpeekc() == NUL)
9832 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 n = 0;
9834 else
9835 /* getchar(0) and char avail: return char */
9836 n = safe_vgetc();
9837 --no_mapping;
9838 --allow_keys;
9839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 if (IS_SPECIAL(n) || mod_mask != 0)
9842 {
9843 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9844 int i = 0;
9845
9846 /* Turn a special key into three bytes, plus modifier. */
9847 if (mod_mask != 0)
9848 {
9849 temp[i++] = K_SPECIAL;
9850 temp[i++] = KS_MODIFIER;
9851 temp[i++] = mod_mask;
9852 }
9853 if (IS_SPECIAL(n))
9854 {
9855 temp[i++] = K_SPECIAL;
9856 temp[i++] = K_SECOND(n);
9857 temp[i++] = K_THIRD(n);
9858 }
9859#ifdef FEAT_MBYTE
9860 else if (has_mbyte)
9861 i += (*mb_char2bytes)(n, temp + i);
9862#endif
9863 else
9864 temp[i++] = n;
9865 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->v_type = VAR_STRING;
9867 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 }
9869}
9870
9871/*
9872 * "getcharmod()" function
9873 */
9874/*ARGSUSED*/
9875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 typval_T *argvars;
9878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881}
9882
9883/*
9884 * "getcmdline()" function
9885 */
9886/*ARGSUSED*/
9887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 typval_T *argvars;
9890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 rettv->v_type = VAR_STRING;
9893 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
9896/*
9897 * "getcmdpos()" function
9898 */
9899/*ARGSUSED*/
9900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906}
9907
9908/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009909 * "getcmdtype()" function
9910 */
9911/*ARGSUSED*/
9912 static void
9913f_getcmdtype(argvars, rettv)
9914 typval_T *argvars;
9915 typval_T *rettv;
9916{
9917 rettv->v_type = VAR_STRING;
9918 rettv->vval.v_string = alloc(2);
9919 if (rettv->vval.v_string != NULL)
9920 {
9921 rettv->vval.v_string[0] = get_cmdline_type();
9922 rettv->vval.v_string[1] = NUL;
9923 }
9924}
9925
9926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 * "getcwd()" function
9928 */
9929/*ARGSUSED*/
9930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009932 typval_T *argvars;
9933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
9935 char_u cwd[MAXPATHL];
9936
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 else
9941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009944 if (rettv->vval.v_string != NULL)
9945 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946#endif
9947 }
9948}
9949
9950/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009951 * "getfontname()" function
9952 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009953/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009956 typval_T *argvars;
9957 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959 rettv->v_type = VAR_STRING;
9960 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009961#ifdef FEAT_GUI
9962 if (gui.in_use)
9963 {
9964 GuiFont font;
9965 char_u *name = NULL;
9966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009967 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009968 {
9969 /* Get the "Normal" font. Either the name saved by
9970 * hl_set_font_name() or from the font ID. */
9971 font = gui.norm_font;
9972 name = hl_get_font_name();
9973 }
9974 else
9975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009977 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9978 return;
9979 font = gui_mch_get_font(name, FALSE);
9980 if (font == NOFONT)
9981 return; /* Invalid font name, return empty string. */
9982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009984 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009985 gui_mch_free_font(font);
9986 }
9987#endif
9988}
9989
9990/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009991 * "getfperm({fname})" function
9992 */
9993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 typval_T *argvars;
9996 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009997{
9998 char_u *fname;
9999 struct stat st;
10000 char_u *perm = NULL;
10001 char_u flags[] = "rwx";
10002 int i;
10003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010007 if (mch_stat((char *)fname, &st) >= 0)
10008 {
10009 perm = vim_strsave((char_u *)"---------");
10010 if (perm != NULL)
10011 {
10012 for (i = 0; i < 9; i++)
10013 {
10014 if (st.st_mode & (1 << (8 - i)))
10015 perm[i] = flags[i % 3];
10016 }
10017 }
10018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010020}
10021
10022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 * "getfsize({fname})" function
10024 */
10025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010027 typval_T *argvars;
10028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 char_u *fname;
10031 struct stat st;
10032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036
10037 if (mch_stat((char *)fname, &st) >= 0)
10038 {
10039 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "getftime({fname})" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
10056 char_u *fname;
10057 struct stat st;
10058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
10061 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065}
10066
10067/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010068 * "getftype({fname})" function
10069 */
10070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 typval_T *argvars;
10073 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010074{
10075 char_u *fname;
10076 struct stat st;
10077 char_u *type = NULL;
10078 char *t;
10079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010083 if (mch_lstat((char *)fname, &st) >= 0)
10084 {
10085#ifdef S_ISREG
10086 if (S_ISREG(st.st_mode))
10087 t = "file";
10088 else if (S_ISDIR(st.st_mode))
10089 t = "dir";
10090# ifdef S_ISLNK
10091 else if (S_ISLNK(st.st_mode))
10092 t = "link";
10093# endif
10094# ifdef S_ISBLK
10095 else if (S_ISBLK(st.st_mode))
10096 t = "bdev";
10097# endif
10098# ifdef S_ISCHR
10099 else if (S_ISCHR(st.st_mode))
10100 t = "cdev";
10101# endif
10102# ifdef S_ISFIFO
10103 else if (S_ISFIFO(st.st_mode))
10104 t = "fifo";
10105# endif
10106# ifdef S_ISSOCK
10107 else if (S_ISSOCK(st.st_mode))
10108 t = "fifo";
10109# endif
10110 else
10111 t = "other";
10112#else
10113# ifdef S_IFMT
10114 switch (st.st_mode & S_IFMT)
10115 {
10116 case S_IFREG: t = "file"; break;
10117 case S_IFDIR: t = "dir"; break;
10118# ifdef S_IFLNK
10119 case S_IFLNK: t = "link"; break;
10120# endif
10121# ifdef S_IFBLK
10122 case S_IFBLK: t = "bdev"; break;
10123# endif
10124# ifdef S_IFCHR
10125 case S_IFCHR: t = "cdev"; break;
10126# endif
10127# ifdef S_IFIFO
10128 case S_IFIFO: t = "fifo"; break;
10129# endif
10130# ifdef S_IFSOCK
10131 case S_IFSOCK: t = "socket"; break;
10132# endif
10133 default: t = "other";
10134 }
10135# else
10136 if (mch_isdir(fname))
10137 t = "dir";
10138 else
10139 t = "file";
10140# endif
10141#endif
10142 type = vim_strsave((char_u *)t);
10143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010145}
10146
10147/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010148 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010149 */
10150 static void
10151f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010152 typval_T *argvars;
10153 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010154{
10155 linenr_T lnum;
10156 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010157 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010158
10159 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010160 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010161 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010162 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010163 retlist = FALSE;
10164 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010165 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010166 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010167 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010168 retlist = TRUE;
10169 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010170
Bram Moolenaar342337a2005-07-21 21:11:17 +000010171 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010172}
10173
10174/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010175 * "getpos(string)" function
10176 */
10177 static void
10178f_getpos(argvars, rettv)
10179 typval_T *argvars;
10180 typval_T *rettv;
10181{
10182 pos_T *fp;
10183 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010184 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010185
10186 if (rettv_list_alloc(rettv) == OK)
10187 {
10188 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010189 fp = var2fpos(&argvars[0], TRUE, &fnum);
10190 if (fnum != -1)
10191 list_append_number(l, (varnumber_T)fnum);
10192 else
10193 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010194 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10195 : (varnumber_T)0);
10196 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10197 : (varnumber_T)0);
10198 list_append_number(l,
10199#ifdef FEAT_VIRTUALEDIT
10200 (fp != NULL) ? (varnumber_T)fp->coladd :
10201#endif
10202 (varnumber_T)0);
10203 }
10204 else
10205 rettv->vval.v_number = FALSE;
10206}
10207
10208/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010209 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010210 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010211/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010212 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010213f_getqflist(argvars, rettv)
10214 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010215 typval_T *rettv;
10216{
10217#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010218 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010219#endif
10220
10221 rettv->vval.v_number = FALSE;
10222#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010223 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010224 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010225 wp = NULL;
10226 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10227 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010228 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010229 if (wp == NULL)
10230 return;
10231 }
10232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010233 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010234 }
10235#endif
10236}
10237
10238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 * "getreg()" function
10240 */
10241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246 char_u *strregname;
10247 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010248 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010251 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 strregname = get_tv_string_chk(&argvars[0]);
10254 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010259 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 regname = (strregname == NULL ? '"' : *strregname);
10261 if (regname == 0)
10262 regname = '"';
10263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 rettv->vval.v_string = error ? NULL :
10266 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267}
10268
10269/*
10270 * "getregtype()" function
10271 */
10272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010273f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010274 typval_T *argvars;
10275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276{
10277 char_u *strregname;
10278 int regname;
10279 char_u buf[NUMBUFLEN + 2];
10280 long reglen = 0;
10281
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010282 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 {
10284 strregname = get_tv_string_chk(&argvars[0]);
10285 if (strregname == NULL) /* type error; errmsg already given */
10286 {
10287 rettv->v_type = VAR_STRING;
10288 rettv->vval.v_string = NULL;
10289 return;
10290 }
10291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 else
10293 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295
10296 regname = (strregname == NULL ? '"' : *strregname);
10297 if (regname == 0)
10298 regname = '"';
10299
10300 buf[0] = NUL;
10301 buf[1] = NUL;
10302 switch (get_reg_type(regname, &reglen))
10303 {
10304 case MLINE: buf[0] = 'V'; break;
10305 case MCHAR: buf[0] = 'v'; break;
10306#ifdef FEAT_VISUAL
10307 case MBLOCK:
10308 buf[0] = Ctrl_V;
10309 sprintf((char *)buf + 1, "%ld", reglen + 1);
10310 break;
10311#endif
10312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 rettv->v_type = VAR_STRING;
10314 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315}
10316
10317/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010318 * "gettabwinvar()" function
10319 */
10320 static void
10321f_gettabwinvar(argvars, rettv)
10322 typval_T *argvars;
10323 typval_T *rettv;
10324{
10325 getwinvar(argvars, rettv, 1);
10326}
10327
10328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 * "getwinposx()" function
10330 */
10331/*ARGSUSED*/
10332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010334 typval_T *argvars;
10335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338#ifdef FEAT_GUI
10339 if (gui.in_use)
10340 {
10341 int x, y;
10342
10343 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 }
10346#endif
10347}
10348
10349/*
10350 * "getwinposy()" function
10351 */
10352/*ARGSUSED*/
10353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010354f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010355 typval_T *argvars;
10356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359#ifdef FEAT_GUI
10360 if (gui.in_use)
10361 {
10362 int x, y;
10363
10364 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 }
10367#endif
10368}
10369
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010370/*
10371 * Find window specifed by "vp" in tabpage "tp".
10372 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010373 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010374find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010375 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010376 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010377{
10378#ifdef FEAT_WINDOWS
10379 win_T *wp;
10380#endif
10381 int nr;
10382
10383 nr = get_tv_number_chk(vp, NULL);
10384
10385#ifdef FEAT_WINDOWS
10386 if (nr < 0)
10387 return NULL;
10388 if (nr == 0)
10389 return curwin;
10390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010391 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10392 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010393 if (--nr <= 0)
10394 break;
10395 return wp;
10396#else
10397 if (nr == 0 || nr == 1)
10398 return curwin;
10399 return NULL;
10400#endif
10401}
10402
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403/*
10404 * "getwinvar()" function
10405 */
10406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010407f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 typval_T *argvars;
10409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010411 getwinvar(argvars, rettv, 0);
10412}
10413
10414/*
10415 * getwinvar() and gettabwinvar()
10416 */
10417 static void
10418getwinvar(argvars, rettv, off)
10419 typval_T *argvars;
10420 typval_T *rettv;
10421 int off; /* 1 for gettabwinvar() */
10422{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 win_T *win, *oldcurwin;
10424 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010426 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010428#ifdef FEAT_WINDOWS
10429 if (off == 1)
10430 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10431 else
10432 tp = curtab;
10433#endif
10434 win = find_win_by_nr(&argvars[off], tp);
10435 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->v_type = VAR_STRING;
10439 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
10441 if (win != NULL && varname != NULL)
10442 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010443 /* Set curwin to be our win, temporarily. Also set curbuf, so
10444 * that we can get buffer-local options. */
10445 oldcurwin = curwin;
10446 curwin = win;
10447 curbuf = win->w_buffer;
10448
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 else
10452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 if (*varname == NUL)
10454 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10455 * scope prefix before the NUL byte is required by
10456 * find_var_in_ht(). */
10457 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010459 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010463
10464 /* restore previous notion of curwin */
10465 curwin = oldcurwin;
10466 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 }
10468
10469 --emsg_off;
10470}
10471
10472/*
10473 * "glob()" function
10474 */
10475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010477 typval_T *argvars;
10478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479{
10480 expand_T xpc;
10481
10482 ExpandInit(&xpc);
10483 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 rettv->v_type = VAR_STRING;
10485 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487}
10488
10489/*
10490 * "globpath()" function
10491 */
10492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 typval_T *argvars;
10495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010498 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010501 if (file == NULL)
10502 rettv->vval.v_string = NULL;
10503 else
10504 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505}
10506
10507/*
10508 * "has()" function
10509 */
10510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 typval_T *argvars;
10513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 int i;
10516 char_u *name;
10517 int n = FALSE;
10518 static char *(has_list[]) =
10519 {
10520#ifdef AMIGA
10521 "amiga",
10522# ifdef FEAT_ARP
10523 "arp",
10524# endif
10525#endif
10526#ifdef __BEOS__
10527 "beos",
10528#endif
10529#ifdef MSDOS
10530# ifdef DJGPP
10531 "dos32",
10532# else
10533 "dos16",
10534# endif
10535#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010536#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 "mac",
10538#endif
10539#if defined(MACOS_X_UNIX)
10540 "macunix",
10541#endif
10542#ifdef OS2
10543 "os2",
10544#endif
10545#ifdef __QNX__
10546 "qnx",
10547#endif
10548#ifdef RISCOS
10549 "riscos",
10550#endif
10551#ifdef UNIX
10552 "unix",
10553#endif
10554#ifdef VMS
10555 "vms",
10556#endif
10557#ifdef WIN16
10558 "win16",
10559#endif
10560#ifdef WIN32
10561 "win32",
10562#endif
10563#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10564 "win32unix",
10565#endif
10566#ifdef WIN64
10567 "win64",
10568#endif
10569#ifdef EBCDIC
10570 "ebcdic",
10571#endif
10572#ifndef CASE_INSENSITIVE_FILENAME
10573 "fname_case",
10574#endif
10575#ifdef FEAT_ARABIC
10576 "arabic",
10577#endif
10578#ifdef FEAT_AUTOCMD
10579 "autocmd",
10580#endif
10581#ifdef FEAT_BEVAL
10582 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010583# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10584 "balloon_multiline",
10585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586#endif
10587#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10588 "builtin_terms",
10589# ifdef ALL_BUILTIN_TCAPS
10590 "all_builtin_terms",
10591# endif
10592#endif
10593#ifdef FEAT_BYTEOFF
10594 "byte_offset",
10595#endif
10596#ifdef FEAT_CINDENT
10597 "cindent",
10598#endif
10599#ifdef FEAT_CLIENTSERVER
10600 "clientserver",
10601#endif
10602#ifdef FEAT_CLIPBOARD
10603 "clipboard",
10604#endif
10605#ifdef FEAT_CMDL_COMPL
10606 "cmdline_compl",
10607#endif
10608#ifdef FEAT_CMDHIST
10609 "cmdline_hist",
10610#endif
10611#ifdef FEAT_COMMENTS
10612 "comments",
10613#endif
10614#ifdef FEAT_CRYPT
10615 "cryptv",
10616#endif
10617#ifdef FEAT_CSCOPE
10618 "cscope",
10619#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010620#ifdef CURSOR_SHAPE
10621 "cursorshape",
10622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623#ifdef DEBUG
10624 "debug",
10625#endif
10626#ifdef FEAT_CON_DIALOG
10627 "dialog_con",
10628#endif
10629#ifdef FEAT_GUI_DIALOG
10630 "dialog_gui",
10631#endif
10632#ifdef FEAT_DIFF
10633 "diff",
10634#endif
10635#ifdef FEAT_DIGRAPHS
10636 "digraphs",
10637#endif
10638#ifdef FEAT_DND
10639 "dnd",
10640#endif
10641#ifdef FEAT_EMACS_TAGS
10642 "emacs_tags",
10643#endif
10644 "eval", /* always present, of course! */
10645#ifdef FEAT_EX_EXTRA
10646 "ex_extra",
10647#endif
10648#ifdef FEAT_SEARCH_EXTRA
10649 "extra_search",
10650#endif
10651#ifdef FEAT_FKMAP
10652 "farsi",
10653#endif
10654#ifdef FEAT_SEARCHPATH
10655 "file_in_path",
10656#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010657#if defined(UNIX) && !defined(USE_SYSTEM)
10658 "filterpipe",
10659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660#ifdef FEAT_FIND_ID
10661 "find_in_path",
10662#endif
10663#ifdef FEAT_FOLDING
10664 "folding",
10665#endif
10666#ifdef FEAT_FOOTER
10667 "footer",
10668#endif
10669#if !defined(USE_SYSTEM) && defined(UNIX)
10670 "fork",
10671#endif
10672#ifdef FEAT_GETTEXT
10673 "gettext",
10674#endif
10675#ifdef FEAT_GUI
10676 "gui",
10677#endif
10678#ifdef FEAT_GUI_ATHENA
10679# ifdef FEAT_GUI_NEXTAW
10680 "gui_neXtaw",
10681# else
10682 "gui_athena",
10683# endif
10684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685#ifdef FEAT_GUI_GTK
10686 "gui_gtk",
10687# ifdef HAVE_GTK2
10688 "gui_gtk2",
10689# endif
10690#endif
10691#ifdef FEAT_GUI_MAC
10692 "gui_mac",
10693#endif
10694#ifdef FEAT_GUI_MOTIF
10695 "gui_motif",
10696#endif
10697#ifdef FEAT_GUI_PHOTON
10698 "gui_photon",
10699#endif
10700#ifdef FEAT_GUI_W16
10701 "gui_win16",
10702#endif
10703#ifdef FEAT_GUI_W32
10704 "gui_win32",
10705#endif
10706#ifdef FEAT_HANGULIN
10707 "hangul_input",
10708#endif
10709#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10710 "iconv",
10711#endif
10712#ifdef FEAT_INS_EXPAND
10713 "insert_expand",
10714#endif
10715#ifdef FEAT_JUMPLIST
10716 "jumplist",
10717#endif
10718#ifdef FEAT_KEYMAP
10719 "keymap",
10720#endif
10721#ifdef FEAT_LANGMAP
10722 "langmap",
10723#endif
10724#ifdef FEAT_LIBCALL
10725 "libcall",
10726#endif
10727#ifdef FEAT_LINEBREAK
10728 "linebreak",
10729#endif
10730#ifdef FEAT_LISP
10731 "lispindent",
10732#endif
10733#ifdef FEAT_LISTCMDS
10734 "listcmds",
10735#endif
10736#ifdef FEAT_LOCALMAP
10737 "localmap",
10738#endif
10739#ifdef FEAT_MENU
10740 "menu",
10741#endif
10742#ifdef FEAT_SESSION
10743 "mksession",
10744#endif
10745#ifdef FEAT_MODIFY_FNAME
10746 "modify_fname",
10747#endif
10748#ifdef FEAT_MOUSE
10749 "mouse",
10750#endif
10751#ifdef FEAT_MOUSESHAPE
10752 "mouseshape",
10753#endif
10754#if defined(UNIX) || defined(VMS)
10755# ifdef FEAT_MOUSE_DEC
10756 "mouse_dec",
10757# endif
10758# ifdef FEAT_MOUSE_GPM
10759 "mouse_gpm",
10760# endif
10761# ifdef FEAT_MOUSE_JSB
10762 "mouse_jsbterm",
10763# endif
10764# ifdef FEAT_MOUSE_NET
10765 "mouse_netterm",
10766# endif
10767# ifdef FEAT_MOUSE_PTERM
10768 "mouse_pterm",
10769# endif
10770# ifdef FEAT_MOUSE_XTERM
10771 "mouse_xterm",
10772# endif
10773#endif
10774#ifdef FEAT_MBYTE
10775 "multi_byte",
10776#endif
10777#ifdef FEAT_MBYTE_IME
10778 "multi_byte_ime",
10779#endif
10780#ifdef FEAT_MULTI_LANG
10781 "multi_lang",
10782#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010783#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010784#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010785 "mzscheme",
10786#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788#ifdef FEAT_OLE
10789 "ole",
10790#endif
10791#ifdef FEAT_OSFILETYPE
10792 "osfiletype",
10793#endif
10794#ifdef FEAT_PATH_EXTRA
10795 "path_extra",
10796#endif
10797#ifdef FEAT_PERL
10798#ifndef DYNAMIC_PERL
10799 "perl",
10800#endif
10801#endif
10802#ifdef FEAT_PYTHON
10803#ifndef DYNAMIC_PYTHON
10804 "python",
10805#endif
10806#endif
10807#ifdef FEAT_POSTSCRIPT
10808 "postscript",
10809#endif
10810#ifdef FEAT_PRINTER
10811 "printer",
10812#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010813#ifdef FEAT_PROFILE
10814 "profile",
10815#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010816#ifdef FEAT_RELTIME
10817 "reltime",
10818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819#ifdef FEAT_QUICKFIX
10820 "quickfix",
10821#endif
10822#ifdef FEAT_RIGHTLEFT
10823 "rightleft",
10824#endif
10825#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10826 "ruby",
10827#endif
10828#ifdef FEAT_SCROLLBIND
10829 "scrollbind",
10830#endif
10831#ifdef FEAT_CMDL_INFO
10832 "showcmd",
10833 "cmdline_info",
10834#endif
10835#ifdef FEAT_SIGNS
10836 "signs",
10837#endif
10838#ifdef FEAT_SMARTINDENT
10839 "smartindent",
10840#endif
10841#ifdef FEAT_SNIFF
10842 "sniff",
10843#endif
10844#ifdef FEAT_STL_OPT
10845 "statusline",
10846#endif
10847#ifdef FEAT_SUN_WORKSHOP
10848 "sun_workshop",
10849#endif
10850#ifdef FEAT_NETBEANS_INTG
10851 "netbeans_intg",
10852#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010853#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010854 "spell",
10855#endif
10856#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 "syntax",
10858#endif
10859#if defined(USE_SYSTEM) || !defined(UNIX)
10860 "system",
10861#endif
10862#ifdef FEAT_TAG_BINS
10863 "tag_binary",
10864#endif
10865#ifdef FEAT_TAG_OLDSTATIC
10866 "tag_old_static",
10867#endif
10868#ifdef FEAT_TAG_ANYWHITE
10869 "tag_any_white",
10870#endif
10871#ifdef FEAT_TCL
10872# ifndef DYNAMIC_TCL
10873 "tcl",
10874# endif
10875#endif
10876#ifdef TERMINFO
10877 "terminfo",
10878#endif
10879#ifdef FEAT_TERMRESPONSE
10880 "termresponse",
10881#endif
10882#ifdef FEAT_TEXTOBJ
10883 "textobjects",
10884#endif
10885#ifdef HAVE_TGETENT
10886 "tgetent",
10887#endif
10888#ifdef FEAT_TITLE
10889 "title",
10890#endif
10891#ifdef FEAT_TOOLBAR
10892 "toolbar",
10893#endif
10894#ifdef FEAT_USR_CMDS
10895 "user-commands", /* was accidentally included in 5.4 */
10896 "user_commands",
10897#endif
10898#ifdef FEAT_VIMINFO
10899 "viminfo",
10900#endif
10901#ifdef FEAT_VERTSPLIT
10902 "vertsplit",
10903#endif
10904#ifdef FEAT_VIRTUALEDIT
10905 "virtualedit",
10906#endif
10907#ifdef FEAT_VISUAL
10908 "visual",
10909#endif
10910#ifdef FEAT_VISUALEXTRA
10911 "visualextra",
10912#endif
10913#ifdef FEAT_VREPLACE
10914 "vreplace",
10915#endif
10916#ifdef FEAT_WILDIGN
10917 "wildignore",
10918#endif
10919#ifdef FEAT_WILDMENU
10920 "wildmenu",
10921#endif
10922#ifdef FEAT_WINDOWS
10923 "windows",
10924#endif
10925#ifdef FEAT_WAK
10926 "winaltkeys",
10927#endif
10928#ifdef FEAT_WRITEBACKUP
10929 "writebackup",
10930#endif
10931#ifdef FEAT_XIM
10932 "xim",
10933#endif
10934#ifdef FEAT_XFONTSET
10935 "xfontset",
10936#endif
10937#ifdef USE_XSMP
10938 "xsmp",
10939#endif
10940#ifdef USE_XSMP_INTERACT
10941 "xsmp_interact",
10942#endif
10943#ifdef FEAT_XCLIPBOARD
10944 "xterm_clipboard",
10945#endif
10946#ifdef FEAT_XTERM_SAVE
10947 "xterm_save",
10948#endif
10949#if defined(UNIX) && defined(FEAT_X11)
10950 "X11",
10951#endif
10952 NULL
10953 };
10954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 for (i = 0; has_list[i] != NULL; ++i)
10957 if (STRICMP(name, has_list[i]) == 0)
10958 {
10959 n = TRUE;
10960 break;
10961 }
10962
10963 if (n == FALSE)
10964 {
10965 if (STRNICMP(name, "patch", 5) == 0)
10966 n = has_patch(atoi((char *)name + 5));
10967 else if (STRICMP(name, "vim_starting") == 0)
10968 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010969#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10970 else if (STRICMP(name, "balloon_multiline") == 0)
10971 n = multiline_balloon_available();
10972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973#ifdef DYNAMIC_TCL
10974 else if (STRICMP(name, "tcl") == 0)
10975 n = tcl_enabled(FALSE);
10976#endif
10977#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10978 else if (STRICMP(name, "iconv") == 0)
10979 n = iconv_enabled(FALSE);
10980#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010981#ifdef DYNAMIC_MZSCHEME
10982 else if (STRICMP(name, "mzscheme") == 0)
10983 n = mzscheme_enabled(FALSE);
10984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985#ifdef DYNAMIC_RUBY
10986 else if (STRICMP(name, "ruby") == 0)
10987 n = ruby_enabled(FALSE);
10988#endif
10989#ifdef DYNAMIC_PYTHON
10990 else if (STRICMP(name, "python") == 0)
10991 n = python_enabled(FALSE);
10992#endif
10993#ifdef DYNAMIC_PERL
10994 else if (STRICMP(name, "perl") == 0)
10995 n = perl_enabled(FALSE);
10996#endif
10997#ifdef FEAT_GUI
10998 else if (STRICMP(name, "gui_running") == 0)
10999 n = (gui.in_use || gui.starting);
11000# ifdef FEAT_GUI_W32
11001 else if (STRICMP(name, "gui_win32s") == 0)
11002 n = gui_is_win32s();
11003# endif
11004# ifdef FEAT_BROWSE
11005 else if (STRICMP(name, "browse") == 0)
11006 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11007# endif
11008#endif
11009#ifdef FEAT_SYN_HL
11010 else if (STRICMP(name, "syntax_items") == 0)
11011 n = syntax_present(curbuf);
11012#endif
11013#if defined(WIN3264)
11014 else if (STRICMP(name, "win95") == 0)
11015 n = mch_windows95();
11016#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011017#ifdef FEAT_NETBEANS_INTG
11018 else if (STRICMP(name, "netbeans_enabled") == 0)
11019 n = usingNetbeans;
11020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
11026/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011027 * "has_key()" function
11028 */
11029 static void
11030f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 typval_T *argvars;
11032 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011033{
11034 rettv->vval.v_number = 0;
11035 if (argvars[0].v_type != VAR_DICT)
11036 {
11037 EMSG(_(e_dictreq));
11038 return;
11039 }
11040 if (argvars[0].vval.v_dict == NULL)
11041 return;
11042
11043 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011044 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045}
11046
11047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 * "hasmapto()" function
11049 */
11050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 typval_T *argvars;
11053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054{
11055 char_u *name;
11056 char_u *mode;
11057 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011058 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011061 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 mode = (char_u *)"nvo";
11063 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011066 if (argvars[2].v_type != VAR_UNKNOWN)
11067 abbr = get_tv_number(&argvars[2]);
11068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069
Bram Moolenaar2c932302006-03-18 21:42:09 +000011070 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074}
11075
11076/*
11077 * "histadd()" function
11078 */
11079/*ARGSUSED*/
11080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 typval_T *argvars;
11083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084{
11085#ifdef FEAT_CMDHIST
11086 int histype;
11087 char_u *str;
11088 char_u buf[NUMBUFLEN];
11089#endif
11090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 if (check_restricted() || check_secure())
11093 return;
11094#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11096 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 if (histype >= 0)
11098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 if (*str != NUL)
11101 {
11102 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 return;
11105 }
11106 }
11107#endif
11108}
11109
11110/*
11111 * "histdel()" function
11112 */
11113/*ARGSUSED*/
11114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
11119#ifdef FEAT_CMDHIST
11120 int n;
11121 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11125 if (str == NULL)
11126 n = 0;
11127 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011130 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 else
11135 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 get_tv_string_buf(&argvars[1], buf));
11138 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141#endif
11142}
11143
11144/*
11145 * "histget()" function
11146 */
11147/*ARGSUSED*/
11148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011150 typval_T *argvars;
11151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152{
11153#ifdef FEAT_CMDHIST
11154 int type;
11155 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11159 if (str == NULL)
11160 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 {
11163 type = get_histtype(str);
11164 if (argvars[1].v_type == VAR_UNKNOWN)
11165 idx = get_history_idx(type);
11166 else
11167 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11168 /* -1 on type error */
11169 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
11178 * "histnr()" function
11179 */
11180/*ARGSUSED*/
11181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185{
11186 int i;
11187
11188#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 char_u *history = get_tv_string_chk(&argvars[0]);
11190
11191 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 if (i >= HIST_CMD && i < HIST_COUNT)
11193 i = get_history_idx(i);
11194 else
11195#endif
11196 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011197 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198}
11199
11200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 * "highlightID(name)" function
11202 */
11203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011205 typval_T *argvars;
11206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209}
11210
11211/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011212 * "highlight_exists()" function
11213 */
11214 static void
11215f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 typval_T *argvars;
11217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011218{
11219 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11220}
11221
11222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 * "hostname()" function
11224 */
11225/*ARGSUSED*/
11226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011228 typval_T *argvars;
11229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230{
11231 char_u hostname[256];
11232
11233 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234 rettv->v_type = VAR_STRING;
11235 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236}
11237
11238/*
11239 * iconv() function
11240 */
11241/*ARGSUSED*/
11242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 typval_T *argvars;
11245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
11247#ifdef FEAT_MBYTE
11248 char_u buf1[NUMBUFLEN];
11249 char_u buf2[NUMBUFLEN];
11250 char_u *from, *to, *str;
11251 vimconv_T vimconv;
11252#endif
11253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->v_type = VAR_STRING;
11255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256
11257#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 str = get_tv_string(&argvars[0]);
11259 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11260 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 vimconv.vc_type = CONV_NONE;
11262 convert_setup(&vimconv, from, to);
11263
11264 /* If the encodings are equal, no conversion needed. */
11265 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
11270 convert_setup(&vimconv, NULL, NULL);
11271 vim_free(from);
11272 vim_free(to);
11273#endif
11274}
11275
11276/*
11277 * "indent()" function
11278 */
11279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
11284 linenr_T lnum;
11285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291}
11292
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011293/*
11294 * "index()" function
11295 */
11296 static void
11297f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *argvars;
11299 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011300{
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 list_T *l;
11302 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011303 long idx = 0;
11304 int ic = FALSE;
11305
11306 rettv->vval.v_number = -1;
11307 if (argvars[0].v_type != VAR_LIST)
11308 {
11309 EMSG(_(e_listreq));
11310 return;
11311 }
11312 l = argvars[0].vval.v_list;
11313 if (l != NULL)
11314 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011315 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011316 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 int error = FALSE;
11319
Bram Moolenaar758711c2005-02-02 23:11:38 +000011320 /* Start at specified item. Use the cached index that list_find()
11321 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011322 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011323 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011324 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 ic = get_tv_number_chk(&argvars[3], &error);
11326 if (error)
11327 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011329
Bram Moolenaar758711c2005-02-02 23:11:38 +000011330 for ( ; item != NULL; item = item->li_next, ++idx)
11331 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011332 {
11333 rettv->vval.v_number = idx;
11334 break;
11335 }
11336 }
11337}
11338
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339static int inputsecret_flag = 0;
11340
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011341static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11342
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011344 * This function is used by f_input() and f_inputdialog() functions. The third
11345 * argument to f_input() specifies the type of completion to use at the
11346 * prompt. The third argument to f_inputdialog() specifies the value to return
11347 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 */
11349 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011350get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *argvars;
11352 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011353 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 char_u *p = NULL;
11357 int c;
11358 char_u buf[NUMBUFLEN];
11359 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011361 int xp_type = EXPAND_NOTHING;
11362 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
11366#ifdef NO_CONSOLE_INPUT
11367 /* While starting up, there is no place to enter text. */
11368 if (no_console_input())
11369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 return;
11372 }
11373#endif
11374
11375 cmd_silent = FALSE; /* Want to see the prompt. */
11376 if (prompt != NULL)
11377 {
11378 /* Only the part of the message after the last NL is considered as
11379 * prompt for the command line */
11380 p = vim_strrchr(prompt, '\n');
11381 if (p == NULL)
11382 p = prompt;
11383 else
11384 {
11385 ++p;
11386 c = *p;
11387 *p = NUL;
11388 msg_start();
11389 msg_clr_eos();
11390 msg_puts_attr(prompt, echo_attr);
11391 msg_didout = FALSE;
11392 msg_starthere();
11393 *p = c;
11394 }
11395 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 if (argvars[1].v_type != VAR_UNKNOWN)
11398 {
11399 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11400 if (defstr != NULL)
11401 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011403 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011404 {
11405 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011406 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011407 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011408
Bram Moolenaar4463f292005-09-25 22:20:24 +000011409 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011410
Bram Moolenaar4463f292005-09-25 22:20:24 +000011411 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11412 if (xp_name == NULL)
11413 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011414
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011415 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011416
Bram Moolenaar4463f292005-09-25 22:20:24 +000011417 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11418 &xp_arg) == FAIL)
11419 return;
11420 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011421 }
11422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011423 if (defstr != NULL)
11424 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011425 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11426 xp_type, xp_arg);
11427
11428 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 /* since the user typed this, no need to wait for return */
11431 need_wait_return = FALSE;
11432 msg_didout = FALSE;
11433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 cmd_silent = cmd_silent_save;
11435}
11436
11437/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011438 * "input()" function
11439 * Also handles inputsecret() when inputsecret is set.
11440 */
11441 static void
11442f_input(argvars, rettv)
11443 typval_T *argvars;
11444 typval_T *rettv;
11445{
11446 get_user_input(argvars, rettv, FALSE);
11447}
11448
11449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 * "inputdialog()" function
11451 */
11452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456{
11457#if defined(FEAT_GUI_TEXTDIALOG)
11458 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11459 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11460 {
11461 char_u *message;
11462 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 message = get_tv_string_chk(&argvars[0]);
11466 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011467 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011468 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 else
11470 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 if (message != NULL && defstr != NULL
11472 && do_dialog(VIM_QUESTION, NULL, message,
11473 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else
11476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 if (message != NULL && defstr != NULL
11478 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011479 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->vval.v_string = vim_strsave(
11481 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 }
11487 else
11488#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011489 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490}
11491
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011492/*
11493 * "inputlist()" function
11494 */
11495 static void
11496f_inputlist(argvars, rettv)
11497 typval_T *argvars;
11498 typval_T *rettv;
11499{
11500 listitem_T *li;
11501 int selected;
11502 int mouse_used;
11503
11504 rettv->vval.v_number = 0;
11505#ifdef NO_CONSOLE_INPUT
11506 /* While starting up, there is no place to enter text. */
11507 if (no_console_input())
11508 return;
11509#endif
11510 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11511 {
11512 EMSG2(_(e_listarg), "inputlist()");
11513 return;
11514 }
11515
11516 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011517 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011518 lines_left = Rows; /* avoid more prompt */
11519 msg_scroll = TRUE;
11520 msg_clr_eos();
11521
11522 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11523 {
11524 msg_puts(get_tv_string(&li->li_tv));
11525 msg_putchar('\n');
11526 }
11527
11528 /* Ask for choice. */
11529 selected = prompt_for_number(&mouse_used);
11530 if (mouse_used)
11531 selected -= lines_left;
11532
11533 rettv->vval.v_number = selected;
11534}
11535
11536
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11538
11539/*
11540 * "inputrestore()" function
11541 */
11542/*ARGSUSED*/
11543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 typval_T *argvars;
11546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547{
11548 if (ga_userinput.ga_len > 0)
11549 {
11550 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11552 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 }
11555 else if (p_verbose > 1)
11556 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011557 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 }
11560}
11561
11562/*
11563 * "inputsave()" function
11564 */
11565/*ARGSUSED*/
11566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *argvars;
11569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570{
11571 /* Add an entry to the stack of typehead storage. */
11572 if (ga_grow(&ga_userinput, 1) == OK)
11573 {
11574 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11575 + ga_userinput.ga_len);
11576 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 }
11579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581}
11582
11583/*
11584 * "inputsecret()" function
11585 */
11586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011588 typval_T *argvars;
11589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590{
11591 ++cmdline_star;
11592 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 --cmdline_star;
11595 --inputsecret_flag;
11596}
11597
11598/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599 * "insert()" function
11600 */
11601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 typval_T *argvars;
11604 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011605{
11606 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 listitem_T *item;
11608 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011611 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011612 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011614 else if ((l = argvars[0].vval.v_list) != NULL
11615 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011616 {
11617 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011618 before = get_tv_number_chk(&argvars[2], &error);
11619 if (error)
11620 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011621
Bram Moolenaar758711c2005-02-02 23:11:38 +000011622 if (before == l->lv_len)
11623 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011624 else
11625 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011626 item = list_find(l, before);
11627 if (item == NULL)
11628 {
11629 EMSGN(_(e_listidx), before);
11630 l = NULL;
11631 }
11632 }
11633 if (l != NULL)
11634 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011635 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011636 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011637 }
11638 }
11639}
11640
11641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 * "isdirectory()" function
11643 */
11644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011646 typval_T *argvars;
11647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650}
11651
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011652/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011653 * "islocked()" function
11654 */
11655 static void
11656f_islocked(argvars, rettv)
11657 typval_T *argvars;
11658 typval_T *rettv;
11659{
11660 lval_T lv;
11661 char_u *end;
11662 dictitem_T *di;
11663
11664 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011665 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11666 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011667 if (end != NULL && lv.ll_name != NULL)
11668 {
11669 if (*end != NUL)
11670 EMSG(_(e_trailing));
11671 else
11672 {
11673 if (lv.ll_tv == NULL)
11674 {
11675 if (check_changedtick(lv.ll_name))
11676 rettv->vval.v_number = 1; /* always locked */
11677 else
11678 {
11679 di = find_var(lv.ll_name, NULL);
11680 if (di != NULL)
11681 {
11682 /* Consider a variable locked when:
11683 * 1. the variable itself is locked
11684 * 2. the value of the variable is locked.
11685 * 3. the List or Dict value is locked.
11686 */
11687 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11688 || tv_islocked(&di->di_tv));
11689 }
11690 }
11691 }
11692 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011693 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011694 else if (lv.ll_newkey != NULL)
11695 EMSG2(_(e_dictkey), lv.ll_newkey);
11696 else if (lv.ll_list != NULL)
11697 /* List item. */
11698 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11699 else
11700 /* Dictionary item. */
11701 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11702 }
11703 }
11704
11705 clear_lval(&lv);
11706}
11707
Bram Moolenaar33570922005-01-25 22:26:29 +000011708static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011709
11710/*
11711 * Turn a dict into a list:
11712 * "what" == 0: list of keys
11713 * "what" == 1: list of values
11714 * "what" == 2: list of items
11715 */
11716 static void
11717dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011720 int what;
11721{
Bram Moolenaar33570922005-01-25 22:26:29 +000011722 list_T *l2;
11723 dictitem_T *di;
11724 hashitem_T *hi;
11725 listitem_T *li;
11726 listitem_T *li2;
11727 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011728 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011729
11730 rettv->vval.v_number = 0;
11731 if (argvars[0].v_type != VAR_DICT)
11732 {
11733 EMSG(_(e_dictreq));
11734 return;
11735 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011736 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011737 return;
11738
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011739 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011740 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011741
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011742 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011743 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011747 --todo;
11748 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011749
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011750 li = listitem_alloc();
11751 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011752 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011753 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011754
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011755 if (what == 0)
11756 {
11757 /* keys() */
11758 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011759 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011760 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11761 }
11762 else if (what == 1)
11763 {
11764 /* values() */
11765 copy_tv(&di->di_tv, &li->li_tv);
11766 }
11767 else
11768 {
11769 /* items() */
11770 l2 = list_alloc();
11771 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011772 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011773 li->li_tv.vval.v_list = l2;
11774 if (l2 == NULL)
11775 break;
11776 ++l2->lv_refcount;
11777
11778 li2 = listitem_alloc();
11779 if (li2 == NULL)
11780 break;
11781 list_append(l2, li2);
11782 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011783 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011784 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11785
11786 li2 = listitem_alloc();
11787 if (li2 == NULL)
11788 break;
11789 list_append(l2, li2);
11790 copy_tv(&di->di_tv, &li2->li_tv);
11791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011792 }
11793 }
11794}
11795
11796/*
11797 * "items(dict)" function
11798 */
11799 static void
11800f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 typval_T *argvars;
11802 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011803{
11804 dict_list(argvars, rettv, 2);
11805}
11806
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011808 * "join()" function
11809 */
11810 static void
11811f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011812 typval_T *argvars;
11813 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011814{
11815 garray_T ga;
11816 char_u *sep;
11817
11818 rettv->vval.v_number = 0;
11819 if (argvars[0].v_type != VAR_LIST)
11820 {
11821 EMSG(_(e_listreq));
11822 return;
11823 }
11824 if (argvars[0].vval.v_list == NULL)
11825 return;
11826 if (argvars[1].v_type == VAR_UNKNOWN)
11827 sep = (char_u *)" ";
11828 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011830
11831 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832
11833 if (sep != NULL)
11834 {
11835 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011836 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011837 ga_append(&ga, NUL);
11838 rettv->vval.v_string = (char_u *)ga.ga_data;
11839 }
11840 else
11841 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011842}
11843
11844/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011845 * "keys()" function
11846 */
11847 static void
11848f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011849 typval_T *argvars;
11850 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011851{
11852 dict_list(argvars, rettv, 0);
11853}
11854
11855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 * "last_buffer_nr()" function.
11857 */
11858/*ARGSUSED*/
11859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 typval_T *argvars;
11862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
11864 int n = 0;
11865 buf_T *buf;
11866
11867 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11868 if (n < buf->b_fnum)
11869 n = buf->b_fnum;
11870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011872}
11873
11874/*
11875 * "len()" function
11876 */
11877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011879 typval_T *argvars;
11880 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011881{
11882 switch (argvars[0].v_type)
11883 {
11884 case VAR_STRING:
11885 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886 rettv->vval.v_number = (varnumber_T)STRLEN(
11887 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011888 break;
11889 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011891 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011892 case VAR_DICT:
11893 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11894 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011895 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011896 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011897 break;
11898 }
11899}
11900
Bram Moolenaar33570922005-01-25 22:26:29 +000011901static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011902
11903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011907 int type;
11908{
11909#ifdef FEAT_LIBCALL
11910 char_u *string_in;
11911 char_u **string_result;
11912 int nr_result;
11913#endif
11914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011916 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011920
11921 if (check_restricted() || check_secure())
11922 return;
11923
11924#ifdef FEAT_LIBCALL
11925 /* The first two args must be strings, otherwise its meaningless */
11926 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11927 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011928 string_in = NULL;
11929 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011930 string_in = argvars[2].vval.v_string;
11931 if (type == VAR_NUMBER)
11932 string_result = NULL;
11933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935 if (mch_libcall(argvars[0].vval.v_string,
11936 argvars[1].vval.v_string,
11937 string_in,
11938 argvars[2].vval.v_number,
11939 string_result,
11940 &nr_result) == OK
11941 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011943 }
11944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945}
11946
11947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948 * "libcall()" function
11949 */
11950 static void
11951f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *argvars;
11953 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011954{
11955 libcall_common(argvars, rettv, VAR_STRING);
11956}
11957
11958/*
11959 * "libcallnr()" function
11960 */
11961 static void
11962f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011963 typval_T *argvars;
11964 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011965{
11966 libcall_common(argvars, rettv, VAR_NUMBER);
11967}
11968
11969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 * "line(string)" function
11971 */
11972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011974 typval_T *argvars;
11975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976{
11977 linenr_T lnum = 0;
11978 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011979 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011981 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 if (fp != NULL)
11983 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985}
11986
11987/*
11988 * "line2byte(lnum)" function
11989 */
11990/*ARGSUSED*/
11991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011993 typval_T *argvars;
11994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995{
11996#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998#else
11999 linenr_T lnum;
12000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12006 if (rettv->vval.v_number >= 0)
12007 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008#endif
12009}
12010
12011/*
12012 * "lispindent(lnum)" function
12013 */
12014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012016 typval_T *argvars;
12017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018{
12019#ifdef FEAT_LISP
12020 pos_T pos;
12021 linenr_T lnum;
12022
12023 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12026 {
12027 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 curwin->w_cursor = pos;
12030 }
12031 else
12032#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034}
12035
12036/*
12037 * "localtime()" function
12038 */
12039/*ARGSUSED*/
12040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046}
12047
Bram Moolenaar33570922005-01-25 22:26:29 +000012048static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 int exact;
12055{
12056 char_u *keys;
12057 char_u *which;
12058 char_u buf[NUMBUFLEN];
12059 char_u *keys_buf = NULL;
12060 char_u *rhs;
12061 int mode;
12062 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012063 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064
12065 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->v_type = VAR_STRING;
12067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 if (*keys == NUL)
12071 return;
12072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012073 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012075 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012076 if (argvars[2].v_type != VAR_UNKNOWN)
12077 abbr = get_tv_number(&argvars[2]);
12078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 else
12080 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 if (which == NULL)
12082 return;
12083
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 mode = get_map_mode(&which, 0);
12085
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012086 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012087 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 vim_free(keys_buf);
12089 if (rhs != NULL)
12090 {
12091 ga_init(&ga);
12092 ga.ga_itemsize = 1;
12093 ga.ga_growsize = 40;
12094
12095 while (*rhs != NUL)
12096 ga_concat(&ga, str2special(&rhs, FALSE));
12097
12098 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 }
12101}
12102
12103/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012104 * "map()" function
12105 */
12106 static void
12107f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 typval_T *argvars;
12109 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012110{
12111 filter_map(argvars, rettv, TRUE);
12112}
12113
12114/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012115 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 */
12117 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012118f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012119 typval_T *argvars;
12120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123}
12124
12125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 */
12128 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012130 typval_T *argvars;
12131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134}
12135
Bram Moolenaar33570922005-01-25 22:26:29 +000012136static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137
12138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 typval_T *argvars;
12141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 int type;
12143{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012144 char_u *str = NULL;
12145 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 char_u *pat;
12147 regmatch_T regmatch;
12148 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012149 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 char_u *save_cpo;
12151 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012152 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012153 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012154 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 list_T *l = NULL;
12156 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012157 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012158 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
12160 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12161 save_cpo = p_cpo;
12162 p_cpo = (char_u *)"";
12163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012164 rettv->vval.v_number = -1;
12165 if (type == 3)
12166 {
12167 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012168 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012169 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012170 }
12171 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 rettv->v_type = VAR_STRING;
12174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012177 if (argvars[0].v_type == VAR_LIST)
12178 {
12179 if ((l = argvars[0].vval.v_list) == NULL)
12180 goto theend;
12181 li = l->lv_first;
12182 }
12183 else
12184 expr = str = get_tv_string(&argvars[0]);
12185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12187 if (pat == NULL)
12188 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012189
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012190 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012192 int error = FALSE;
12193
12194 start = get_tv_number_chk(&argvars[2], &error);
12195 if (error)
12196 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012197 if (l != NULL)
12198 {
12199 li = list_find(l, start);
12200 if (li == NULL)
12201 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012202 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012203 }
12204 else
12205 {
12206 if (start < 0)
12207 start = 0;
12208 if (start > (long)STRLEN(str))
12209 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012210 /* When "count" argument is there ignore matches before "start",
12211 * otherwise skip part of the string. Differs when pattern is "^"
12212 * or "\<". */
12213 if (argvars[3].v_type != VAR_UNKNOWN)
12214 startcol = start;
12215 else
12216 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012217 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012219 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012220 nth = get_tv_number_chk(&argvars[3], &error);
12221 if (error)
12222 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 }
12224
12225 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12226 if (regmatch.regprog != NULL)
12227 {
12228 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012229
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012230 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012231 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012232 if (l != NULL)
12233 {
12234 if (li == NULL)
12235 {
12236 match = FALSE;
12237 break;
12238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012239 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012240 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012241 if (str == NULL)
12242 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012243 }
12244
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012245 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012246
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012247 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012248 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012249 if (l == NULL && !match)
12250 break;
12251
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012252 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012253 if (l != NULL)
12254 {
12255 li = li->li_next;
12256 ++idx;
12257 }
12258 else
12259 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012260#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012261 startcol = (colnr_T)(regmatch.startp[0]
12262 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012263#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012264 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012265#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012266 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012267 }
12268
12269 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012271 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012273 int i;
12274
12275 /* return list with matched string and submatches */
12276 for (i = 0; i < NSUBEXP; ++i)
12277 {
12278 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012279 {
12280 if (list_append_string(rettv->vval.v_list,
12281 (char_u *)"", 0) == FAIL)
12282 break;
12283 }
12284 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012285 regmatch.startp[i],
12286 (int)(regmatch.endp[i] - regmatch.startp[i]))
12287 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012288 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012289 }
12290 }
12291 else if (type == 2)
12292 {
12293 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012294 if (l != NULL)
12295 copy_tv(&li->li_tv, rettv);
12296 else
12297 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012299 }
12300 else if (l != NULL)
12301 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 else
12303 {
12304 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012305 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 (varnumber_T)(regmatch.startp[0] - str);
12307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012310 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 }
12312 }
12313 vim_free(regmatch.regprog);
12314 }
12315
12316theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012317 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 p_cpo = save_cpo;
12319}
12320
12321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012322 * "match()" function
12323 */
12324 static void
12325f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012326 typval_T *argvars;
12327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328{
12329 find_some_match(argvars, rettv, 1);
12330}
12331
12332/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012333 * "matcharg()" function
12334 */
12335 static void
12336f_matcharg(argvars, rettv)
12337 typval_T *argvars;
12338 typval_T *rettv;
12339{
12340 if (rettv_list_alloc(rettv) == OK)
12341 {
12342#ifdef FEAT_SEARCH_EXTRA
12343 int mi = get_tv_number(&argvars[0]);
12344
12345 if (mi >= 1 && mi <= 3)
12346 {
12347 list_append_string(rettv->vval.v_list,
12348 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12349 list_append_string(rettv->vval.v_list,
12350 curwin->w_match_pat[mi - 1], -1);
12351 }
12352#endif
12353 }
12354}
12355
12356/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357 * "matchend()" function
12358 */
12359 static void
12360f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012361 typval_T *argvars;
12362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363{
12364 find_some_match(argvars, rettv, 0);
12365}
12366
12367/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012368 * "matchlist()" function
12369 */
12370 static void
12371f_matchlist(argvars, rettv)
12372 typval_T *argvars;
12373 typval_T *rettv;
12374{
12375 find_some_match(argvars, rettv, 3);
12376}
12377
12378/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379 * "matchstr()" function
12380 */
12381 static void
12382f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012383 typval_T *argvars;
12384 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385{
12386 find_some_match(argvars, rettv, 2);
12387}
12388
Bram Moolenaar33570922005-01-25 22:26:29 +000012389static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012390
12391 static void
12392max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *argvars;
12394 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012395 int domax;
12396{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012397 long n = 0;
12398 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012399 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012400
12401 if (argvars[0].v_type == VAR_LIST)
12402 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012403 list_T *l;
12404 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012405
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012406 l = argvars[0].vval.v_list;
12407 if (l != NULL)
12408 {
12409 li = l->lv_first;
12410 if (li != NULL)
12411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012413 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012414 {
12415 li = li->li_next;
12416 if (li == NULL)
12417 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012418 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012419 if (domax ? i > n : i < n)
12420 n = i;
12421 }
12422 }
12423 }
12424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012425 else if (argvars[0].v_type == VAR_DICT)
12426 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012427 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012428 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012430 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431
12432 d = argvars[0].vval.v_dict;
12433 if (d != NULL)
12434 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012435 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012438 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012439 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012440 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012441 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012442 if (first)
12443 {
12444 n = i;
12445 first = FALSE;
12446 }
12447 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012448 n = i;
12449 }
12450 }
12451 }
12452 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012453 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012454 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012455 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012456}
12457
12458/*
12459 * "max()" function
12460 */
12461 static void
12462f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 typval_T *argvars;
12464 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012465{
12466 max_min(argvars, rettv, TRUE);
12467}
12468
12469/*
12470 * "min()" function
12471 */
12472 static void
12473f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 typval_T *argvars;
12475 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012476{
12477 max_min(argvars, rettv, FALSE);
12478}
12479
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012480static int mkdir_recurse __ARGS((char_u *dir, int prot));
12481
12482/*
12483 * Create the directory in which "dir" is located, and higher levels when
12484 * needed.
12485 */
12486 static int
12487mkdir_recurse(dir, prot)
12488 char_u *dir;
12489 int prot;
12490{
12491 char_u *p;
12492 char_u *updir;
12493 int r = FAIL;
12494
12495 /* Get end of directory name in "dir".
12496 * We're done when it's "/" or "c:/". */
12497 p = gettail_sep(dir);
12498 if (p <= get_past_head(dir))
12499 return OK;
12500
12501 /* If the directory exists we're done. Otherwise: create it.*/
12502 updir = vim_strnsave(dir, (int)(p - dir));
12503 if (updir == NULL)
12504 return FAIL;
12505 if (mch_isdir(updir))
12506 r = OK;
12507 else if (mkdir_recurse(updir, prot) == OK)
12508 r = vim_mkdir_emsg(updir, prot);
12509 vim_free(updir);
12510 return r;
12511}
12512
12513#ifdef vim_mkdir
12514/*
12515 * "mkdir()" function
12516 */
12517 static void
12518f_mkdir(argvars, rettv)
12519 typval_T *argvars;
12520 typval_T *rettv;
12521{
12522 char_u *dir;
12523 char_u buf[NUMBUFLEN];
12524 int prot = 0755;
12525
12526 rettv->vval.v_number = FAIL;
12527 if (check_restricted() || check_secure())
12528 return;
12529
12530 dir = get_tv_string_buf(&argvars[0], buf);
12531 if (argvars[1].v_type != VAR_UNKNOWN)
12532 {
12533 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012534 prot = get_tv_number_chk(&argvars[2], NULL);
12535 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012536 mkdir_recurse(dir, prot);
12537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012539}
12540#endif
12541
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 * "mode()" function
12544 */
12545/*ARGSUSED*/
12546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012548 typval_T *argvars;
12549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550{
12551 char_u buf[2];
12552
12553#ifdef FEAT_VISUAL
12554 if (VIsual_active)
12555 {
12556 if (VIsual_select)
12557 buf[0] = VIsual_mode + 's' - 'v';
12558 else
12559 buf[0] = VIsual_mode;
12560 }
12561 else
12562#endif
12563 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12564 buf[0] = 'r';
12565 else if (State & INSERT)
12566 {
12567 if (State & REPLACE_FLAG)
12568 buf[0] = 'R';
12569 else
12570 buf[0] = 'i';
12571 }
12572 else if (State & CMDLINE)
12573 buf[0] = 'c';
12574 else
12575 buf[0] = 'n';
12576
12577 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012578 rettv->vval.v_string = vim_strsave(buf);
12579 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580}
12581
12582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012583 * "nextnonblank()" function
12584 */
12585 static void
12586f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012587 typval_T *argvars;
12588 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012589{
12590 linenr_T lnum;
12591
12592 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012594 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595 {
12596 lnum = 0;
12597 break;
12598 }
12599 if (*skipwhite(ml_get(lnum)) != NUL)
12600 break;
12601 }
12602 rettv->vval.v_number = lnum;
12603}
12604
12605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 * "nr2char()" function
12607 */
12608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012610 typval_T *argvars;
12611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612{
12613 char_u buf[NUMBUFLEN];
12614
12615#ifdef FEAT_MBYTE
12616 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 else
12619#endif
12620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 buf[1] = NUL;
12623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624 rettv->v_type = VAR_STRING;
12625 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012626}
12627
12628/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012629 * "pathshorten()" function
12630 */
12631 static void
12632f_pathshorten(argvars, rettv)
12633 typval_T *argvars;
12634 typval_T *rettv;
12635{
12636 char_u *p;
12637
12638 rettv->v_type = VAR_STRING;
12639 p = get_tv_string_chk(&argvars[0]);
12640 if (p == NULL)
12641 rettv->vval.v_string = NULL;
12642 else
12643 {
12644 p = vim_strsave(p);
12645 rettv->vval.v_string = p;
12646 if (p != NULL)
12647 shorten_dir(p);
12648 }
12649}
12650
12651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652 * "prevnonblank()" function
12653 */
12654 static void
12655f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012656 typval_T *argvars;
12657 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658{
12659 linenr_T lnum;
12660
12661 lnum = get_tv_lnum(argvars);
12662 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12663 lnum = 0;
12664 else
12665 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12666 --lnum;
12667 rettv->vval.v_number = lnum;
12668}
12669
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012670#ifdef HAVE_STDARG_H
12671/* This dummy va_list is here because:
12672 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12673 * - locally in the function results in a "used before set" warning
12674 * - using va_start() to initialize it gives "function with fixed args" error */
12675static va_list ap;
12676#endif
12677
Bram Moolenaar8c711452005-01-14 21:53:12 +000012678/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012679 * "printf()" function
12680 */
12681 static void
12682f_printf(argvars, rettv)
12683 typval_T *argvars;
12684 typval_T *rettv;
12685{
12686 rettv->v_type = VAR_STRING;
12687 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012688#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012689 {
12690 char_u buf[NUMBUFLEN];
12691 int len;
12692 char_u *s;
12693 int saved_did_emsg = did_emsg;
12694 char *fmt;
12695
12696 /* Get the required length, allocate the buffer and do it for real. */
12697 did_emsg = FALSE;
12698 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012699 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012700 if (!did_emsg)
12701 {
12702 s = alloc(len + 1);
12703 if (s != NULL)
12704 {
12705 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012706 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012707 }
12708 }
12709 did_emsg |= saved_did_emsg;
12710 }
12711#endif
12712}
12713
12714/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012715 * "pumvisible()" function
12716 */
12717/*ARGSUSED*/
12718 static void
12719f_pumvisible(argvars, rettv)
12720 typval_T *argvars;
12721 typval_T *rettv;
12722{
12723 rettv->vval.v_number = 0;
12724#ifdef FEAT_INS_EXPAND
12725 if (pum_visible())
12726 rettv->vval.v_number = 1;
12727#endif
12728}
12729
12730/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012731 * "range()" function
12732 */
12733 static void
12734f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 typval_T *argvars;
12736 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012737{
12738 long start;
12739 long end;
12740 long stride = 1;
12741 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012745 if (argvars[1].v_type == VAR_UNKNOWN)
12746 {
12747 end = start - 1;
12748 start = 0;
12749 }
12750 else
12751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012755 }
12756
12757 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012758 if (error)
12759 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012760 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012761 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012762 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012763 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012764 else
12765 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012766 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012767 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012768 if (list_append_number(rettv->vval.v_list,
12769 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012770 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012771 }
12772}
12773
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012774/*
12775 * "readfile()" function
12776 */
12777 static void
12778f_readfile(argvars, rettv)
12779 typval_T *argvars;
12780 typval_T *rettv;
12781{
12782 int binary = FALSE;
12783 char_u *fname;
12784 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012785 listitem_T *li;
12786#define FREAD_SIZE 200 /* optimized for text lines */
12787 char_u buf[FREAD_SIZE];
12788 int readlen; /* size of last fread() */
12789 int buflen; /* nr of valid chars in buf[] */
12790 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12791 int tolist; /* first byte in buf[] still to be put in list */
12792 int chop; /* how many CR to chop off */
12793 char_u *prev = NULL; /* previously read bytes, if any */
12794 int prevlen = 0; /* length of "prev" if not NULL */
12795 char_u *s;
12796 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012797 long maxline = MAXLNUM;
12798 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012799
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012800 if (argvars[1].v_type != VAR_UNKNOWN)
12801 {
12802 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12803 binary = TRUE;
12804 if (argvars[2].v_type != VAR_UNKNOWN)
12805 maxline = get_tv_number(&argvars[2]);
12806 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012807
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012808 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012809 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012810
12811 /* Always open the file in binary mode, library functions have a mind of
12812 * their own about CR-LF conversion. */
12813 fname = get_tv_string(&argvars[0]);
12814 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12815 {
12816 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12817 return;
12818 }
12819
12820 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012821 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012823 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012824 buflen = filtd + readlen;
12825 tolist = 0;
12826 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12827 {
12828 if (buf[filtd] == '\n' || readlen <= 0)
12829 {
12830 /* Only when in binary mode add an empty list item when the
12831 * last line ends in a '\n'. */
12832 if (!binary && readlen == 0 && filtd == 0)
12833 break;
12834
12835 /* Found end-of-line or end-of-file: add a text line to the
12836 * list. */
12837 chop = 0;
12838 if (!binary)
12839 while (filtd - chop - 1 >= tolist
12840 && buf[filtd - chop - 1] == '\r')
12841 ++chop;
12842 len = filtd - tolist - chop;
12843 if (prev == NULL)
12844 s = vim_strnsave(buf + tolist, len);
12845 else
12846 {
12847 s = alloc((unsigned)(prevlen + len + 1));
12848 if (s != NULL)
12849 {
12850 mch_memmove(s, prev, prevlen);
12851 vim_free(prev);
12852 prev = NULL;
12853 mch_memmove(s + prevlen, buf + tolist, len);
12854 s[prevlen + len] = NUL;
12855 }
12856 }
12857 tolist = filtd + 1;
12858
12859 li = listitem_alloc();
12860 if (li == NULL)
12861 {
12862 vim_free(s);
12863 break;
12864 }
12865 li->li_tv.v_type = VAR_STRING;
12866 li->li_tv.v_lock = 0;
12867 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012868 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012869
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012870 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012871 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012872 if (readlen <= 0)
12873 break;
12874 }
12875 else if (buf[filtd] == NUL)
12876 buf[filtd] = '\n';
12877 }
12878 if (readlen <= 0)
12879 break;
12880
12881 if (tolist == 0)
12882 {
12883 /* "buf" is full, need to move text to an allocated buffer */
12884 if (prev == NULL)
12885 {
12886 prev = vim_strnsave(buf, buflen);
12887 prevlen = buflen;
12888 }
12889 else
12890 {
12891 s = alloc((unsigned)(prevlen + buflen));
12892 if (s != NULL)
12893 {
12894 mch_memmove(s, prev, prevlen);
12895 mch_memmove(s + prevlen, buf, buflen);
12896 vim_free(prev);
12897 prev = s;
12898 prevlen += buflen;
12899 }
12900 }
12901 filtd = 0;
12902 }
12903 else
12904 {
12905 mch_memmove(buf, buf + tolist, buflen - tolist);
12906 filtd -= tolist;
12907 }
12908 }
12909
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012910 /*
12911 * For a negative line count use only the lines at the end of the file,
12912 * free the rest.
12913 */
12914 if (maxline < 0)
12915 while (cnt > -maxline)
12916 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012917 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012918 --cnt;
12919 }
12920
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012921 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012922 fclose(fd);
12923}
12924
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012925#if defined(FEAT_RELTIME)
12926static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12927
12928/*
12929 * Convert a List to proftime_T.
12930 * Return FAIL when there is something wrong.
12931 */
12932 static int
12933list2proftime(arg, tm)
12934 typval_T *arg;
12935 proftime_T *tm;
12936{
12937 long n1, n2;
12938 int error = FALSE;
12939
12940 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12941 || arg->vval.v_list->lv_len != 2)
12942 return FAIL;
12943 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12944 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12945# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012946 tm->HighPart = n1;
12947 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012948# else
12949 tm->tv_sec = n1;
12950 tm->tv_usec = n2;
12951# endif
12952 return error ? FAIL : OK;
12953}
12954#endif /* FEAT_RELTIME */
12955
12956/*
12957 * "reltime()" function
12958 */
12959 static void
12960f_reltime(argvars, rettv)
12961 typval_T *argvars;
12962 typval_T *rettv;
12963{
12964#ifdef FEAT_RELTIME
12965 proftime_T res;
12966 proftime_T start;
12967
12968 if (argvars[0].v_type == VAR_UNKNOWN)
12969 {
12970 /* No arguments: get current time. */
12971 profile_start(&res);
12972 }
12973 else if (argvars[1].v_type == VAR_UNKNOWN)
12974 {
12975 if (list2proftime(&argvars[0], &res) == FAIL)
12976 return;
12977 profile_end(&res);
12978 }
12979 else
12980 {
12981 /* Two arguments: compute the difference. */
12982 if (list2proftime(&argvars[0], &start) == FAIL
12983 || list2proftime(&argvars[1], &res) == FAIL)
12984 return;
12985 profile_sub(&res, &start);
12986 }
12987
12988 if (rettv_list_alloc(rettv) == OK)
12989 {
12990 long n1, n2;
12991
12992# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012993 n1 = res.HighPart;
12994 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012995# else
12996 n1 = res.tv_sec;
12997 n2 = res.tv_usec;
12998# endif
12999 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13000 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13001 }
13002#endif
13003}
13004
13005/*
13006 * "reltimestr()" function
13007 */
13008 static void
13009f_reltimestr(argvars, rettv)
13010 typval_T *argvars;
13011 typval_T *rettv;
13012{
13013#ifdef FEAT_RELTIME
13014 proftime_T tm;
13015#endif
13016
13017 rettv->v_type = VAR_STRING;
13018 rettv->vval.v_string = NULL;
13019#ifdef FEAT_RELTIME
13020 if (list2proftime(&argvars[0], &tm) == OK)
13021 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13022#endif
13023}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013024
Bram Moolenaar0d660222005-01-07 21:51:51 +000013025#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13026static void make_connection __ARGS((void));
13027static int check_connection __ARGS((void));
13028
13029 static void
13030make_connection()
13031{
13032 if (X_DISPLAY == NULL
13033# ifdef FEAT_GUI
13034 && !gui.in_use
13035# endif
13036 )
13037 {
13038 x_force_connect = TRUE;
13039 setup_term_clip();
13040 x_force_connect = FALSE;
13041 }
13042}
13043
13044 static int
13045check_connection()
13046{
13047 make_connection();
13048 if (X_DISPLAY == NULL)
13049 {
13050 EMSG(_("E240: No connection to Vim server"));
13051 return FAIL;
13052 }
13053 return OK;
13054}
13055#endif
13056
13057#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013058static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013059
13060 static void
13061remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *argvars;
13063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013064 int expr;
13065{
13066 char_u *server_name;
13067 char_u *keys;
13068 char_u *r = NULL;
13069 char_u buf[NUMBUFLEN];
13070# ifdef WIN32
13071 HWND w;
13072# else
13073 Window w;
13074# endif
13075
13076 if (check_restricted() || check_secure())
13077 return;
13078
13079# ifdef FEAT_X11
13080 if (check_connection() == FAIL)
13081 return;
13082# endif
13083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 server_name = get_tv_string_chk(&argvars[0]);
13085 if (server_name == NULL)
13086 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013087 keys = get_tv_string_buf(&argvars[1], buf);
13088# ifdef WIN32
13089 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13090# else
13091 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13092 < 0)
13093# endif
13094 {
13095 if (r != NULL)
13096 EMSG(r); /* sending worked but evaluation failed */
13097 else
13098 EMSG2(_("E241: Unable to send to %s"), server_name);
13099 return;
13100 }
13101
13102 rettv->vval.v_string = r;
13103
13104 if (argvars[2].v_type != VAR_UNKNOWN)
13105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013106 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013107 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013109
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013110 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013111 v.di_tv.v_type = VAR_STRING;
13112 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013113 idvar = get_tv_string_chk(&argvars[2]);
13114 if (idvar != NULL)
13115 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013117 }
13118}
13119#endif
13120
13121/*
13122 * "remote_expr()" function
13123 */
13124/*ARGSUSED*/
13125 static void
13126f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *argvars;
13128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013129{
13130 rettv->v_type = VAR_STRING;
13131 rettv->vval.v_string = NULL;
13132#ifdef FEAT_CLIENTSERVER
13133 remote_common(argvars, rettv, TRUE);
13134#endif
13135}
13136
13137/*
13138 * "remote_foreground()" function
13139 */
13140/*ARGSUSED*/
13141 static void
13142f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013143 typval_T *argvars;
13144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013145{
13146 rettv->vval.v_number = 0;
13147#ifdef FEAT_CLIENTSERVER
13148# ifdef WIN32
13149 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 {
13151 char_u *server_name = get_tv_string_chk(&argvars[0]);
13152
13153 if (server_name != NULL)
13154 serverForeground(server_name);
13155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013156# else
13157 /* Send a foreground() expression to the server. */
13158 argvars[1].v_type = VAR_STRING;
13159 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13160 argvars[2].v_type = VAR_UNKNOWN;
13161 remote_common(argvars, rettv, TRUE);
13162 vim_free(argvars[1].vval.v_string);
13163# endif
13164#endif
13165}
13166
13167/*ARGSUSED*/
13168 static void
13169f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013170 typval_T *argvars;
13171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013172{
13173#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013174 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013175 char_u *s = NULL;
13176# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013177 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013180
13181 if (check_restricted() || check_secure())
13182 {
13183 rettv->vval.v_number = -1;
13184 return;
13185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013186 serverid = get_tv_string_chk(&argvars[0]);
13187 if (serverid == NULL)
13188 {
13189 rettv->vval.v_number = -1;
13190 return; /* type error; errmsg already given */
13191 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013193 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013194 if (n == 0)
13195 rettv->vval.v_number = -1;
13196 else
13197 {
13198 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13199 rettv->vval.v_number = (s != NULL);
13200 }
13201# else
13202 rettv->vval.v_number = 0;
13203 if (check_connection() == FAIL)
13204 return;
13205
13206 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013208# endif
13209
13210 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 char_u *retvar;
13213
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 v.di_tv.v_type = VAR_STRING;
13215 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 retvar = get_tv_string_chk(&argvars[1]);
13217 if (retvar != NULL)
13218 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013219 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013220 }
13221#else
13222 rettv->vval.v_number = -1;
13223#endif
13224}
13225
13226/*ARGSUSED*/
13227 static void
13228f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013231{
13232 char_u *r = NULL;
13233
13234#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 char_u *serverid = get_tv_string_chk(&argvars[0]);
13236
13237 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 {
13239# ifdef WIN32
13240 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013241 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013242
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013243 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244 if (n != 0)
13245 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13246 if (r == NULL)
13247# else
13248 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013250# endif
13251 EMSG(_("E277: Unable to read a server reply"));
13252 }
13253#endif
13254 rettv->v_type = VAR_STRING;
13255 rettv->vval.v_string = r;
13256}
13257
13258/*
13259 * "remote_send()" function
13260 */
13261/*ARGSUSED*/
13262 static void
13263f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *argvars;
13265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013266{
13267 rettv->v_type = VAR_STRING;
13268 rettv->vval.v_string = NULL;
13269#ifdef FEAT_CLIENTSERVER
13270 remote_common(argvars, rettv, FALSE);
13271#endif
13272}
13273
13274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013275 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013276 */
13277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013279 typval_T *argvars;
13280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013281{
Bram Moolenaar33570922005-01-25 22:26:29 +000013282 list_T *l;
13283 listitem_T *item, *item2;
13284 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013285 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013286 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013287 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 dict_T *d;
13289 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013290
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013291 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013292 if (argvars[0].v_type == VAR_DICT)
13293 {
13294 if (argvars[2].v_type != VAR_UNKNOWN)
13295 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013296 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013297 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013299 key = get_tv_string_chk(&argvars[1]);
13300 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 di = dict_find(d, key, -1);
13303 if (di == NULL)
13304 EMSG2(_(e_dictkey), key);
13305 else
13306 {
13307 *rettv = di->di_tv;
13308 init_tv(&di->di_tv);
13309 dictitem_remove(d, di);
13310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013312 }
13313 }
13314 else if (argvars[0].v_type != VAR_LIST)
13315 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013316 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013317 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 int error = FALSE;
13320
13321 idx = get_tv_number_chk(&argvars[1], &error);
13322 if (error)
13323 ; /* type error: do nothing, errmsg already given */
13324 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013325 EMSGN(_(e_listidx), idx);
13326 else
13327 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013328 if (argvars[2].v_type == VAR_UNKNOWN)
13329 {
13330 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013331 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013332 *rettv = item->li_tv;
13333 vim_free(item);
13334 }
13335 else
13336 {
13337 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013338 end = get_tv_number_chk(&argvars[2], &error);
13339 if (error)
13340 ; /* type error: do nothing */
13341 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013342 EMSGN(_(e_listidx), end);
13343 else
13344 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013345 int cnt = 0;
13346
13347 for (li = item; li != NULL; li = li->li_next)
13348 {
13349 ++cnt;
13350 if (li == item2)
13351 break;
13352 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013353 if (li == NULL) /* didn't find "item2" after "item" */
13354 EMSG(_(e_invrange));
13355 else
13356 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013357 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013358 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013359 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013360 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013361 l->lv_first = item;
13362 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013363 item->li_prev = NULL;
13364 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013365 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013366 }
13367 }
13368 }
13369 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013370 }
13371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
13375 * "rename({from}, {to})" function
13376 */
13377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381{
13382 char_u buf[NUMBUFLEN];
13383
13384 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13388 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389}
13390
13391/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013392 * "repeat()" function
13393 */
13394/*ARGSUSED*/
13395 static void
13396f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 typval_T *argvars;
13398 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013399{
13400 char_u *p;
13401 int n;
13402 int slen;
13403 int len;
13404 char_u *r;
13405 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013406
13407 n = get_tv_number(&argvars[1]);
13408 if (argvars[0].v_type == VAR_LIST)
13409 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013410 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013411 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013412 if (list_extend(rettv->vval.v_list,
13413 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013414 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013415 }
13416 else
13417 {
13418 p = get_tv_string(&argvars[0]);
13419 rettv->v_type = VAR_STRING;
13420 rettv->vval.v_string = NULL;
13421
13422 slen = (int)STRLEN(p);
13423 len = slen * n;
13424 if (len <= 0)
13425 return;
13426
13427 r = alloc(len + 1);
13428 if (r != NULL)
13429 {
13430 for (i = 0; i < n; i++)
13431 mch_memmove(r + i * slen, p, (size_t)slen);
13432 r[len] = NUL;
13433 }
13434
13435 rettv->vval.v_string = r;
13436 }
13437}
13438
13439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 * "resolve()" function
13441 */
13442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013444 typval_T *argvars;
13445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446{
13447 char_u *p;
13448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450#ifdef FEAT_SHORTCUT
13451 {
13452 char_u *v = NULL;
13453
13454 v = mch_resolve_shortcut(p);
13455 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 }
13460#else
13461# ifdef HAVE_READLINK
13462 {
13463 char_u buf[MAXPATHL + 1];
13464 char_u *cpy;
13465 int len;
13466 char_u *remain = NULL;
13467 char_u *q;
13468 int is_relative_to_current = FALSE;
13469 int has_trailing_pathsep = FALSE;
13470 int limit = 100;
13471
13472 p = vim_strsave(p);
13473
13474 if (p[0] == '.' && (vim_ispathsep(p[1])
13475 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13476 is_relative_to_current = TRUE;
13477
13478 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013479 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 has_trailing_pathsep = TRUE;
13481
13482 q = getnextcomp(p);
13483 if (*q != NUL)
13484 {
13485 /* Separate the first path component in "p", and keep the
13486 * remainder (beginning with the path separator). */
13487 remain = vim_strsave(q - 1);
13488 q[-1] = NUL;
13489 }
13490
13491 for (;;)
13492 {
13493 for (;;)
13494 {
13495 len = readlink((char *)p, (char *)buf, MAXPATHL);
13496 if (len <= 0)
13497 break;
13498 buf[len] = NUL;
13499
13500 if (limit-- == 0)
13501 {
13502 vim_free(p);
13503 vim_free(remain);
13504 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 goto fail;
13507 }
13508
13509 /* Ensure that the result will have a trailing path separator
13510 * if the argument has one. */
13511 if (remain == NULL && has_trailing_pathsep)
13512 add_pathsep(buf);
13513
13514 /* Separate the first path component in the link value and
13515 * concatenate the remainders. */
13516 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13517 if (*q != NUL)
13518 {
13519 if (remain == NULL)
13520 remain = vim_strsave(q - 1);
13521 else
13522 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013523 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 if (cpy != NULL)
13525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 vim_free(remain);
13527 remain = cpy;
13528 }
13529 }
13530 q[-1] = NUL;
13531 }
13532
13533 q = gettail(p);
13534 if (q > p && *q == NUL)
13535 {
13536 /* Ignore trailing path separator. */
13537 q[-1] = NUL;
13538 q = gettail(p);
13539 }
13540 if (q > p && !mch_isFullName(buf))
13541 {
13542 /* symlink is relative to directory of argument */
13543 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13544 if (cpy != NULL)
13545 {
13546 STRCPY(cpy, p);
13547 STRCPY(gettail(cpy), buf);
13548 vim_free(p);
13549 p = cpy;
13550 }
13551 }
13552 else
13553 {
13554 vim_free(p);
13555 p = vim_strsave(buf);
13556 }
13557 }
13558
13559 if (remain == NULL)
13560 break;
13561
13562 /* Append the first path component of "remain" to "p". */
13563 q = getnextcomp(remain + 1);
13564 len = q - remain - (*q != NUL);
13565 cpy = vim_strnsave(p, STRLEN(p) + len);
13566 if (cpy != NULL)
13567 {
13568 STRNCAT(cpy, remain, len);
13569 vim_free(p);
13570 p = cpy;
13571 }
13572 /* Shorten "remain". */
13573 if (*q != NUL)
13574 STRCPY(remain, q - 1);
13575 else
13576 {
13577 vim_free(remain);
13578 remain = NULL;
13579 }
13580 }
13581
13582 /* If the result is a relative path name, make it explicitly relative to
13583 * the current directory if and only if the argument had this form. */
13584 if (!vim_ispathsep(*p))
13585 {
13586 if (is_relative_to_current
13587 && *p != NUL
13588 && !(p[0] == '.'
13589 && (p[1] == NUL
13590 || vim_ispathsep(p[1])
13591 || (p[1] == '.'
13592 && (p[2] == NUL
13593 || vim_ispathsep(p[2]))))))
13594 {
13595 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013596 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 if (cpy != NULL)
13598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 vim_free(p);
13600 p = cpy;
13601 }
13602 }
13603 else if (!is_relative_to_current)
13604 {
13605 /* Strip leading "./". */
13606 q = p;
13607 while (q[0] == '.' && vim_ispathsep(q[1]))
13608 q += 2;
13609 if (q > p)
13610 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13611 }
13612 }
13613
13614 /* Ensure that the result will have no trailing path separator
13615 * if the argument had none. But keep "/" or "//". */
13616 if (!has_trailing_pathsep)
13617 {
13618 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013619 if (after_pathsep(p, q))
13620 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 }
13622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 }
13625# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627# endif
13628#endif
13629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631
13632#ifdef HAVE_READLINK
13633fail:
13634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636}
13637
13638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013639 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 */
13641 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013642f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *argvars;
13644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 list_T *l;
13647 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
Bram Moolenaar0d660222005-01-07 21:51:51 +000013649 rettv->vval.v_number = 0;
13650 if (argvars[0].v_type != VAR_LIST)
13651 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013652 else if ((l = argvars[0].vval.v_list) != NULL
13653 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013654 {
13655 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013656 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013657 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013658 while (li != NULL)
13659 {
13660 ni = li->li_prev;
13661 list_append(l, li);
13662 li = ni;
13663 }
13664 rettv->vval.v_list = l;
13665 rettv->v_type = VAR_LIST;
13666 ++l->lv_refcount;
13667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668}
13669
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013670#define SP_NOMOVE 0x01 /* don't move cursor */
13671#define SP_REPEAT 0x02 /* repeat to find outer pair */
13672#define SP_RETCOUNT 0x04 /* return matchcount */
13673#define SP_SETPCMARK 0x08 /* set previous context mark */
13674#define SP_START 0x10 /* accept match at start position */
13675#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13676#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013677
Bram Moolenaar33570922005-01-25 22:26:29 +000013678static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013679
13680/*
13681 * Get flags for a search function.
13682 * Possibly sets "p_ws".
13683 * Returns BACKWARD, FORWARD or zero (for an error).
13684 */
13685 static int
13686get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013687 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688 int *flagsp;
13689{
13690 int dir = FORWARD;
13691 char_u *flags;
13692 char_u nbuf[NUMBUFLEN];
13693 int mask;
13694
13695 if (varp->v_type != VAR_UNKNOWN)
13696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 flags = get_tv_string_buf_chk(varp, nbuf);
13698 if (flags == NULL)
13699 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013700 while (*flags != NUL)
13701 {
13702 switch (*flags)
13703 {
13704 case 'b': dir = BACKWARD; break;
13705 case 'w': p_ws = TRUE; break;
13706 case 'W': p_ws = FALSE; break;
13707 default: mask = 0;
13708 if (flagsp != NULL)
13709 switch (*flags)
13710 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013711 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013712 case 'e': mask = SP_END; break;
13713 case 'm': mask = SP_RETCOUNT; break;
13714 case 'n': mask = SP_NOMOVE; break;
13715 case 'p': mask = SP_SUBPAT; break;
13716 case 'r': mask = SP_REPEAT; break;
13717 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013718 }
13719 if (mask == 0)
13720 {
13721 EMSG2(_(e_invarg2), flags);
13722 dir = 0;
13723 }
13724 else
13725 *flagsp |= mask;
13726 }
13727 if (dir == 0)
13728 break;
13729 ++flags;
13730 }
13731 }
13732 return dir;
13733}
13734
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013736 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013738 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013739search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013741 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013742 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013744 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 char_u *pat;
13746 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013747 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 int save_p_ws = p_ws;
13749 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013750 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013751 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013752 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013753 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013756 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013757 if (dir == 0)
13758 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013759 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013760 if (flags & SP_START)
13761 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013762 if (flags & SP_END)
13763 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013764
13765 /* Optional extra argument: line number to stop searching. */
13766 if (argvars[1].v_type != VAR_UNKNOWN
13767 && argvars[2].v_type != VAR_UNKNOWN)
13768 {
13769 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13770 if (lnum_stop < 0)
13771 goto theend;
13772 }
13773
Bram Moolenaar231334e2005-07-25 20:46:57 +000013774 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013775 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013776 * Check to make sure only those flags are set.
13777 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13778 * flags cannot be set. Check for that condition also.
13779 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013780 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013781 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013784 goto theend;
13785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013787 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013788 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13789 options, RE_SEARCH, (linenr_T)lnum_stop);
13790 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013792 if (flags & SP_SUBPAT)
13793 retval = subpatnum;
13794 else
13795 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013796 if (flags & SP_SETPCMARK)
13797 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013799 if (match_pos != NULL)
13800 {
13801 /* Store the match cursor position */
13802 match_pos->lnum = pos.lnum;
13803 match_pos->col = pos.col + 1;
13804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 /* "/$" will put the cursor after the end of the line, may need to
13806 * correct that here */
13807 check_cursor();
13808 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013809
13810 /* If 'n' flag is used: restore cursor position. */
13811 if (flags & SP_NOMOVE)
13812 curwin->w_cursor = save_cursor;
13813theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013815
13816 return retval;
13817}
13818
13819/*
13820 * "search()" function
13821 */
13822 static void
13823f_search(argvars, rettv)
13824 typval_T *argvars;
13825 typval_T *rettv;
13826{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013827 int flags = 0;
13828
13829 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830}
13831
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013833 * "searchdecl()" function
13834 */
13835 static void
13836f_searchdecl(argvars, rettv)
13837 typval_T *argvars;
13838 typval_T *rettv;
13839{
13840 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013841 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013842 int error = FALSE;
13843 char_u *name;
13844
13845 rettv->vval.v_number = 1; /* default: FAIL */
13846
13847 name = get_tv_string_chk(&argvars[0]);
13848 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013849 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013850 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013851 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13852 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13853 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013854 if (!error && name != NULL)
13855 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013856 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013857}
13858
13859/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013860 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013862 static int
13863searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013864 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013865 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866{
13867 char_u *spat, *mpat, *epat;
13868 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 int dir;
13871 int flags = 0;
13872 char_u nbuf1[NUMBUFLEN];
13873 char_u nbuf2[NUMBUFLEN];
13874 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013875 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013876 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013879 spat = get_tv_string_chk(&argvars[0]);
13880 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13881 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13882 if (spat == NULL || mpat == NULL || epat == NULL)
13883 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 /* Handle the optional fourth argument: flags */
13886 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013887 if (dir == 0)
13888 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013889
13890 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013891 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13892 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013893 if ((flags & (SP_END | SP_SUBPAT)) != 0
13894 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013895 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013896 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013897 goto theend;
13898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013900 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013901 if (argvars[3].v_type == VAR_UNKNOWN
13902 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 skip = (char_u *)"";
13904 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013907 if (argvars[5].v_type != VAR_UNKNOWN)
13908 {
13909 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13910 if (lnum_stop < 0)
13911 goto theend;
13912 }
13913 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 if (skip == NULL)
13915 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013917 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13918 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013919
13920theend:
13921 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013922
13923 return retval;
13924}
13925
13926/*
13927 * "searchpair()" function
13928 */
13929 static void
13930f_searchpair(argvars, rettv)
13931 typval_T *argvars;
13932 typval_T *rettv;
13933{
13934 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13935}
13936
13937/*
13938 * "searchpairpos()" function
13939 */
13940 static void
13941f_searchpairpos(argvars, rettv)
13942 typval_T *argvars;
13943 typval_T *rettv;
13944{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013945 pos_T match_pos;
13946 int lnum = 0;
13947 int col = 0;
13948
13949 rettv->vval.v_number = 0;
13950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013951 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013952 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013953
13954 if (searchpair_cmn(argvars, &match_pos) > 0)
13955 {
13956 lnum = match_pos.lnum;
13957 col = match_pos.col;
13958 }
13959
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013960 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13961 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013962}
13963
13964/*
13965 * Search for a start/middle/end thing.
13966 * Used by searchpair(), see its documentation for the details.
13967 * Returns 0 or -1 for no match,
13968 */
13969 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013970do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013971 char_u *spat; /* start pattern */
13972 char_u *mpat; /* middle pattern */
13973 char_u *epat; /* end pattern */
13974 int dir; /* BACKWARD or FORWARD */
13975 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013976 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013977 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013978 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013979{
13980 char_u *save_cpo;
13981 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13982 long retval = 0;
13983 pos_T pos;
13984 pos_T firstpos;
13985 pos_T foundpos;
13986 pos_T save_cursor;
13987 pos_T save_pos;
13988 int n;
13989 int r;
13990 int nest = 1;
13991 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013992 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013993
13994 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13995 save_cpo = p_cpo;
13996 p_cpo = (char_u *)"";
13997
13998 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13999 * start/middle/end (pat3, for the top pair). */
14000 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14001 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14002 if (pat2 == NULL || pat3 == NULL)
14003 goto theend;
14004 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14005 if (*mpat == NUL)
14006 STRCPY(pat3, pat2);
14007 else
14008 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14009 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014010 if (flags & SP_START)
14011 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014012
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 save_cursor = curwin->w_cursor;
14014 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014015 clearpos(&firstpos);
14016 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 pat = pat3;
14018 for (;;)
14019 {
14020 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014021 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14023 /* didn't find it or found the first match again: FAIL */
14024 break;
14025
14026 if (firstpos.lnum == 0)
14027 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014028 if (equalpos(pos, foundpos))
14029 {
14030 /* Found the same position again. Can happen with a pattern that
14031 * has "\zs" at the end and searching backwards. Advance one
14032 * character and try again. */
14033 if (dir == BACKWARD)
14034 decl(&pos);
14035 else
14036 incl(&pos);
14037 }
14038 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039
14040 /* If the skip pattern matches, ignore this match. */
14041 if (*skip != NUL)
14042 {
14043 save_pos = curwin->w_cursor;
14044 curwin->w_cursor = pos;
14045 r = eval_to_bool(skip, &err, NULL, FALSE);
14046 curwin->w_cursor = save_pos;
14047 if (err)
14048 {
14049 /* Evaluating {skip} caused an error, break here. */
14050 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014051 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 break;
14053 }
14054 if (r)
14055 continue;
14056 }
14057
14058 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14059 {
14060 /* Found end when searching backwards or start when searching
14061 * forward: nested pair. */
14062 ++nest;
14063 pat = pat2; /* nested, don't search for middle */
14064 }
14065 else
14066 {
14067 /* Found end when searching forward or start when searching
14068 * backward: end of (nested) pair; or found middle in outer pair. */
14069 if (--nest == 1)
14070 pat = pat3; /* outer level, search for middle */
14071 }
14072
14073 if (nest == 0)
14074 {
14075 /* Found the match: return matchcount or line number. */
14076 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014077 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014079 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014080 if (flags & SP_SETPCMARK)
14081 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 curwin->w_cursor = pos;
14083 if (!(flags & SP_REPEAT))
14084 break;
14085 nest = 1; /* search for next unmatched */
14086 }
14087 }
14088
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014089 if (match_pos != NULL)
14090 {
14091 /* Store the match cursor position */
14092 match_pos->lnum = curwin->w_cursor.lnum;
14093 match_pos->col = curwin->w_cursor.col + 1;
14094 }
14095
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014097 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 curwin->w_cursor = save_cursor;
14099
14100theend:
14101 vim_free(pat2);
14102 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014104
14105 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106}
14107
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014108/*
14109 * "searchpos()" function
14110 */
14111 static void
14112f_searchpos(argvars, rettv)
14113 typval_T *argvars;
14114 typval_T *rettv;
14115{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014116 pos_T match_pos;
14117 int lnum = 0;
14118 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014119 int n;
14120 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121
14122 rettv->vval.v_number = 0;
14123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014125 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014126
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014127 n = search_cmn(argvars, &match_pos, &flags);
14128 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014129 {
14130 lnum = match_pos.lnum;
14131 col = match_pos.col;
14132 }
14133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014134 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14135 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014136 if (flags & SP_SUBPAT)
14137 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014138}
14139
14140
Bram Moolenaar0d660222005-01-07 21:51:51 +000014141/*ARGSUSED*/
14142 static void
14143f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 typval_T *argvars;
14145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147#ifdef FEAT_CLIENTSERVER
14148 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 char_u *server = get_tv_string_chk(&argvars[0]);
14150 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151
Bram Moolenaar0d660222005-01-07 21:51:51 +000014152 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 if (server == NULL || reply == NULL)
14154 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155 if (check_restricted() || check_secure())
14156 return;
14157# ifdef FEAT_X11
14158 if (check_connection() == FAIL)
14159 return;
14160# endif
14161
14162 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014164 EMSG(_("E258: Unable to send to client"));
14165 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014167 rettv->vval.v_number = 0;
14168#else
14169 rettv->vval.v_number = -1;
14170#endif
14171}
14172
14173/*ARGSUSED*/
14174 static void
14175f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014178{
14179 char_u *r = NULL;
14180
14181#ifdef FEAT_CLIENTSERVER
14182# ifdef WIN32
14183 r = serverGetVimNames();
14184# else
14185 make_connection();
14186 if (X_DISPLAY != NULL)
14187 r = serverGetVimNames(X_DISPLAY);
14188# endif
14189#endif
14190 rettv->v_type = VAR_STRING;
14191 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192}
14193
14194/*
14195 * "setbufvar()" function
14196 */
14197/*ARGSUSED*/
14198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014199f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *argvars;
14201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202{
14203 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 char_u nbuf[NUMBUFLEN];
14208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 rettv->vval.v_number = 0;
14210
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 if (check_restricted() || check_secure())
14212 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14214 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014215 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 varp = &argvars[2];
14217
14218 if (buf != NULL && varname != NULL && varp != NULL)
14219 {
14220 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222
14223 if (*varname == '&')
14224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 long numval;
14226 char_u *strval;
14227 int error = FALSE;
14228
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 numval = get_tv_number_chk(varp, &error);
14231 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 if (!error && strval != NULL)
14233 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 }
14235 else
14236 {
14237 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14238 if (bufvarname != NULL)
14239 {
14240 STRCPY(bufvarname, "b:");
14241 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014242 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 vim_free(bufvarname);
14244 }
14245 }
14246
14247 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250}
14251
14252/*
14253 * "setcmdpos()" function
14254 */
14255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014257 typval_T *argvars;
14258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 int pos = (int)get_tv_number(&argvars[0]) - 1;
14261
14262 if (pos >= 0)
14263 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264}
14265
14266/*
14267 * "setline()" function
14268 */
14269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014271 typval_T *argvars;
14272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014275 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014276 list_T *l = NULL;
14277 listitem_T *li = NULL;
14278 long added = 0;
14279 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014281 lnum = get_tv_lnum(&argvars[0]);
14282 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014284 l = argvars[1].vval.v_list;
14285 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014287 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014288 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014289
14290 rettv->vval.v_number = 0; /* OK */
14291 for (;;)
14292 {
14293 if (l != NULL)
14294 {
14295 /* list argument, get next string */
14296 if (li == NULL)
14297 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014299 li = li->li_next;
14300 }
14301
14302 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014304 break;
14305 if (lnum <= curbuf->b_ml.ml_line_count)
14306 {
14307 /* existing line, replace it */
14308 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14309 {
14310 changed_bytes(lnum, 0);
14311 check_cursor_col();
14312 rettv->vval.v_number = 0; /* OK */
14313 }
14314 }
14315 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14316 {
14317 /* lnum is one past the last line, append the line */
14318 ++added;
14319 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14320 rettv->vval.v_number = 0; /* OK */
14321 }
14322
14323 if (l == NULL) /* only one string argument */
14324 break;
14325 ++lnum;
14326 }
14327
14328 if (added > 0)
14329 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330}
14331
14332/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014333 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014334 */
14335/*ARGSUSED*/
14336 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014337set_qf_ll_list(wp, list_arg, action_arg, rettv)
14338 win_T *wp;
14339 typval_T *list_arg;
14340 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014341 typval_T *rettv;
14342{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014343#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014344 char_u *act;
14345 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014346#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014347
Bram Moolenaar2641f772005-03-25 21:58:17 +000014348 rettv->vval.v_number = -1;
14349
14350#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014351 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014352 EMSG(_(e_listreq));
14353 else
14354 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014355 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014356
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014357 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014358 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014359 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 if (act == NULL)
14361 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014362 if (*act == 'a' || *act == 'r')
14363 action = *act;
14364 }
14365
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014366 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014367 rettv->vval.v_number = 0;
14368 }
14369#endif
14370}
14371
14372/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014373 * "setloclist()" function
14374 */
14375/*ARGSUSED*/
14376 static void
14377f_setloclist(argvars, rettv)
14378 typval_T *argvars;
14379 typval_T *rettv;
14380{
14381 win_T *win;
14382
14383 rettv->vval.v_number = -1;
14384
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014385 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014386 if (win != NULL)
14387 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14388}
14389
14390/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014391 * "setpos()" function
14392 */
14393/*ARGSUSED*/
14394 static void
14395f_setpos(argvars, rettv)
14396 typval_T *argvars;
14397 typval_T *rettv;
14398{
14399 pos_T pos;
14400 int fnum;
14401 char_u *name;
14402
14403 name = get_tv_string_chk(argvars);
14404 if (name != NULL)
14405 {
14406 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14407 {
14408 --pos.col;
14409 if (name[0] == '.') /* cursor */
14410 {
14411 if (fnum == curbuf->b_fnum)
14412 {
14413 curwin->w_cursor = pos;
14414 check_cursor();
14415 }
14416 else
14417 EMSG(_(e_invarg));
14418 }
14419 else if (name[0] == '\'') /* mark */
14420 (void)setmark_pos(name[1], &pos, fnum);
14421 else
14422 EMSG(_(e_invarg));
14423 }
14424 }
14425}
14426
14427/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014428 * "setqflist()" function
14429 */
14430/*ARGSUSED*/
14431 static void
14432f_setqflist(argvars, rettv)
14433 typval_T *argvars;
14434 typval_T *rettv;
14435{
14436 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14437}
14438
14439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 * "setreg()" function
14441 */
14442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014443f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 typval_T *argvars;
14445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446{
14447 int regname;
14448 char_u *strregname;
14449 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 int append;
14452 char_u yank_type;
14453 long block_len;
14454
14455 block_len = -1;
14456 yank_type = MAUTO;
14457 append = FALSE;
14458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014462 if (strregname == NULL)
14463 return; /* type error; errmsg already given */
14464 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 if (regname == 0 || regname == '@')
14466 regname = '"';
14467 else if (regname == '=')
14468 return;
14469
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014470 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014472 stropt = get_tv_string_chk(&argvars[2]);
14473 if (stropt == NULL)
14474 return; /* type error */
14475 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 switch (*stropt)
14477 {
14478 case 'a': case 'A': /* append */
14479 append = TRUE;
14480 break;
14481 case 'v': case 'c': /* character-wise selection */
14482 yank_type = MCHAR;
14483 break;
14484 case 'V': case 'l': /* line-wise selection */
14485 yank_type = MLINE;
14486 break;
14487#ifdef FEAT_VISUAL
14488 case 'b': case Ctrl_V: /* block-wise selection */
14489 yank_type = MBLOCK;
14490 if (VIM_ISDIGIT(stropt[1]))
14491 {
14492 ++stropt;
14493 block_len = getdigits(&stropt) - 1;
14494 --stropt;
14495 }
14496 break;
14497#endif
14498 }
14499 }
14500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 strval = get_tv_string_chk(&argvars[1]);
14502 if (strval != NULL)
14503 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506}
14507
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014508/*
14509 * "settabwinvar()" function
14510 */
14511 static void
14512f_settabwinvar(argvars, rettv)
14513 typval_T *argvars;
14514 typval_T *rettv;
14515{
14516 setwinvar(argvars, rettv, 1);
14517}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518
14519/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014520 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014527 setwinvar(argvars, rettv, 0);
14528}
14529
14530/*
14531 * "setwinvar()" and "settabwinvar()" functions
14532 */
14533 static void
14534setwinvar(argvars, rettv, off)
14535 typval_T *argvars;
14536 typval_T *rettv;
14537 int off;
14538{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 win_T *win;
14540#ifdef FEAT_WINDOWS
14541 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014542 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543#endif
14544 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014547 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 rettv->vval.v_number = 0;
14550
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 if (check_restricted() || check_secure())
14552 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014553
14554#ifdef FEAT_WINDOWS
14555 if (off == 1)
14556 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14557 else
14558 tp = curtab;
14559#endif
14560 win = find_win_by_nr(&argvars[off], tp);
14561 varname = get_tv_string_chk(&argvars[off + 1]);
14562 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563
14564 if (win != NULL && varname != NULL && varp != NULL)
14565 {
14566#ifdef FEAT_WINDOWS
14567 /* set curwin to be our win, temporarily */
14568 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014569 save_curtab = curtab;
14570 goto_tabpage_tp(tp);
14571 if (!win_valid(win))
14572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 curwin = win;
14574 curbuf = curwin->w_buffer;
14575#endif
14576
14577 if (*varname == '&')
14578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014579 long numval;
14580 char_u *strval;
14581 int error = FALSE;
14582
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 numval = get_tv_number_chk(varp, &error);
14585 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 if (!error && strval != NULL)
14587 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 }
14589 else
14590 {
14591 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14592 if (winvarname != NULL)
14593 {
14594 STRCPY(winvarname, "w:");
14595 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014596 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 vim_free(winvarname);
14598 }
14599 }
14600
14601#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014602 /* Restore current tabpage and window, if still valid (autocomands can
14603 * make them invalid). */
14604 if (valid_tabpage(save_curtab))
14605 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 if (win_valid(save_curwin))
14607 {
14608 curwin = save_curwin;
14609 curbuf = curwin->w_buffer;
14610 }
14611#endif
14612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613}
14614
14615/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014616 * "shellescape({string})" function
14617 */
14618 static void
14619f_shellescape(argvars, rettv)
14620 typval_T *argvars;
14621 typval_T *rettv;
14622{
14623 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14624 rettv->v_type = VAR_STRING;
14625}
14626
14627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 */
14630 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014632 typval_T *argvars;
14633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637 p = get_tv_string(&argvars[0]);
14638 rettv->vval.v_string = vim_strsave(p);
14639 simplify_filename(rettv->vval.v_string); /* simplify in place */
14640 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641}
14642
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643static int
14644#ifdef __BORLANDC__
14645 _RTLENTRYF
14646#endif
14647 item_compare __ARGS((const void *s1, const void *s2));
14648static int
14649#ifdef __BORLANDC__
14650 _RTLENTRYF
14651#endif
14652 item_compare2 __ARGS((const void *s1, const void *s2));
14653
14654static int item_compare_ic;
14655static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657#define ITEM_COMPARE_FAIL 999
14658
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014660 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662 static int
14663#ifdef __BORLANDC__
14664_RTLENTRYF
14665#endif
14666item_compare(s1, s2)
14667 const void *s1;
14668 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670 char_u *p1, *p2;
14671 char_u *tofree1, *tofree2;
14672 int res;
14673 char_u numbuf1[NUMBUFLEN];
14674 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014676 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14677 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 if (item_compare_ic)
14679 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 res = STRCMP(p1, p2);
14682 vim_free(tofree1);
14683 vim_free(tofree2);
14684 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685}
14686
14687 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688#ifdef __BORLANDC__
14689_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691item_compare2(s1, s2)
14692 const void *s1;
14693 const void *s2;
14694{
14695 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014696 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014697 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014700 /* shortcut after failure in previous call; compare all items equal */
14701 if (item_compare_func_err)
14702 return 0;
14703
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014704 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14705 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014706 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14707 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708
14709 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014710 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014711 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014712 clear_tv(&argv[0]);
14713 clear_tv(&argv[1]);
14714
14715 if (res == FAIL)
14716 res = ITEM_COMPARE_FAIL;
14717 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 /* return value has wrong type */
14719 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14720 if (item_compare_func_err)
14721 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722 clear_tv(&rettv);
14723 return res;
14724}
14725
14726/*
14727 * "sort({list})" function
14728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014731 typval_T *argvars;
14732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733{
Bram Moolenaar33570922005-01-25 22:26:29 +000014734 list_T *l;
14735 listitem_T *li;
14736 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737 long len;
14738 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 rettv->vval.v_number = 0;
14741 if (argvars[0].v_type != VAR_LIST)
14742 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 else
14744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014745 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014746 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747 return;
14748 rettv->vval.v_list = l;
14749 rettv->v_type = VAR_LIST;
14750 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751
Bram Moolenaar0d660222005-01-07 21:51:51 +000014752 len = list_len(l);
14753 if (len <= 1)
14754 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756 item_compare_ic = FALSE;
14757 item_compare_func = NULL;
14758 if (argvars[1].v_type != VAR_UNKNOWN)
14759 {
14760 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014761 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 else
14763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 int error = FALSE;
14765
14766 i = get_tv_number_chk(&argvars[1], &error);
14767 if (error)
14768 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014769 if (i == 1)
14770 item_compare_ic = TRUE;
14771 else
14772 item_compare_func = get_tv_string(&argvars[1]);
14773 }
14774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 if (ptrs == NULL)
14779 return;
14780 i = 0;
14781 for (li = l->lv_first; li != NULL; li = li->li_next)
14782 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014785 /* test the compare function */
14786 if (item_compare_func != NULL
14787 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14788 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014789 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014791 {
14792 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014793 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014794 item_compare_func == NULL ? item_compare : item_compare2);
14795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 if (!item_compare_func_err)
14797 {
14798 /* Clear the List and append the items in the sorted order. */
14799 l->lv_first = l->lv_last = NULL;
14800 l->lv_len = 0;
14801 for (i = 0; i < len; ++i)
14802 list_append(l, ptrs[i]);
14803 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014804 }
14805
14806 vim_free(ptrs);
14807 }
14808}
14809
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014810/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014811 * "soundfold({word})" function
14812 */
14813 static void
14814f_soundfold(argvars, rettv)
14815 typval_T *argvars;
14816 typval_T *rettv;
14817{
14818 char_u *s;
14819
14820 rettv->v_type = VAR_STRING;
14821 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014822#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014823 rettv->vval.v_string = eval_soundfold(s);
14824#else
14825 rettv->vval.v_string = vim_strsave(s);
14826#endif
14827}
14828
14829/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014830 * "spellbadword()" function
14831 */
14832/* ARGSUSED */
14833 static void
14834f_spellbadword(argvars, rettv)
14835 typval_T *argvars;
14836 typval_T *rettv;
14837{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014838 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014839 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014840 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014842 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014843 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014844
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014845#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014846 if (argvars[0].v_type == VAR_UNKNOWN)
14847 {
14848 /* Find the start and length of the badly spelled word. */
14849 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14850 if (len != 0)
14851 word = ml_get_cursor();
14852 }
14853 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14854 {
14855 char_u *str = get_tv_string_chk(&argvars[0]);
14856 int capcol = -1;
14857
14858 if (str != NULL)
14859 {
14860 /* Check the argument for spelling. */
14861 while (*str != NUL)
14862 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014863 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014864 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014865 {
14866 word = str;
14867 break;
14868 }
14869 str += len;
14870 }
14871 }
14872 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014873#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014874
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014875 list_append_string(rettv->vval.v_list, word, len);
14876 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014877 attr == HLF_SPB ? "bad" :
14878 attr == HLF_SPR ? "rare" :
14879 attr == HLF_SPL ? "local" :
14880 attr == HLF_SPC ? "caps" :
14881 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014882}
14883
14884/*
14885 * "spellsuggest()" function
14886 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014887/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014888 static void
14889f_spellsuggest(argvars, rettv)
14890 typval_T *argvars;
14891 typval_T *rettv;
14892{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014893#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014894 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014895 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014896 int maxcount;
14897 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014898 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014899 listitem_T *li;
14900 int need_capital = FALSE;
14901#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014902
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014903 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014904 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014905
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014906#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014907 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14908 {
14909 str = get_tv_string(&argvars[0]);
14910 if (argvars[1].v_type != VAR_UNKNOWN)
14911 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014912 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014913 if (maxcount <= 0)
14914 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014915 if (argvars[2].v_type != VAR_UNKNOWN)
14916 {
14917 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14918 if (typeerr)
14919 return;
14920 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014921 }
14922 else
14923 maxcount = 25;
14924
Bram Moolenaar4770d092006-01-12 23:22:24 +000014925 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014926
14927 for (i = 0; i < ga.ga_len; ++i)
14928 {
14929 str = ((char_u **)ga.ga_data)[i];
14930
14931 li = listitem_alloc();
14932 if (li == NULL)
14933 vim_free(str);
14934 else
14935 {
14936 li->li_tv.v_type = VAR_STRING;
14937 li->li_tv.v_lock = 0;
14938 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014939 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014940 }
14941 }
14942 ga_clear(&ga);
14943 }
14944#endif
14945}
14946
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014948f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 typval_T *argvars;
14950 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951{
14952 char_u *str;
14953 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014954 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 regmatch_T regmatch;
14956 char_u patbuf[NUMBUFLEN];
14957 char_u *save_cpo;
14958 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014960 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014961 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962
14963 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14964 save_cpo = p_cpo;
14965 p_cpo = (char_u *)"";
14966
14967 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014968 if (argvars[1].v_type != VAR_UNKNOWN)
14969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14971 if (pat == NULL)
14972 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014973 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014975 }
14976 if (pat == NULL || *pat == NUL)
14977 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014979 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014981 if (typeerr)
14982 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14985 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014988 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014990 if (*str == NUL)
14991 match = FALSE; /* empty item at the end */
14992 else
14993 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994 if (match)
14995 end = regmatch.startp[0];
14996 else
14997 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014998 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14999 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015001 if (list_append_string(rettv->vval.v_list, str,
15002 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015004 }
15005 if (!match)
15006 break;
15007 /* Advance to just after the match. */
15008 if (regmatch.endp[0] > str)
15009 col = 0;
15010 else
15011 {
15012 /* Don't get stuck at the same match. */
15013#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015014 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015#else
15016 col = 1;
15017#endif
15018 }
15019 str = regmatch.endp[0];
15020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026}
15027
Bram Moolenaar2c932302006-03-18 21:42:09 +000015028/*
15029 * "str2nr()" function
15030 */
15031 static void
15032f_str2nr(argvars, rettv)
15033 typval_T *argvars;
15034 typval_T *rettv;
15035{
15036 int base = 10;
15037 char_u *p;
15038 long n;
15039
15040 if (argvars[1].v_type != VAR_UNKNOWN)
15041 {
15042 base = get_tv_number(&argvars[1]);
15043 if (base != 8 && base != 10 && base != 16)
15044 {
15045 EMSG(_(e_invarg));
15046 return;
15047 }
15048 }
15049
15050 p = skipwhite(get_tv_string(&argvars[0]));
15051 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15052 rettv->vval.v_number = n;
15053}
15054
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055#ifdef HAVE_STRFTIME
15056/*
15057 * "strftime({format}[, {time}])" function
15058 */
15059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015061 typval_T *argvars;
15062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063{
15064 char_u result_buf[256];
15065 struct tm *curtime;
15066 time_t seconds;
15067 char_u *p;
15068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015069 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015071 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015072 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 seconds = time(NULL);
15074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 curtime = localtime(&seconds);
15077 /* MSVC returns NULL for an invalid value of seconds. */
15078 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 else
15081 {
15082# ifdef FEAT_MBYTE
15083 vimconv_T conv;
15084 char_u *enc;
15085
15086 conv.vc_type = CONV_NONE;
15087 enc = enc_locale();
15088 convert_setup(&conv, p_enc, enc);
15089 if (conv.vc_type != CONV_NONE)
15090 p = string_convert(&conv, p, NULL);
15091# endif
15092 if (p != NULL)
15093 (void)strftime((char *)result_buf, sizeof(result_buf),
15094 (char *)p, curtime);
15095 else
15096 result_buf[0] = NUL;
15097
15098# ifdef FEAT_MBYTE
15099 if (conv.vc_type != CONV_NONE)
15100 vim_free(p);
15101 convert_setup(&conv, enc, p_enc);
15102 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015103 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 else
15105# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107
15108# ifdef FEAT_MBYTE
15109 /* Release conversion descriptors */
15110 convert_setup(&conv, NULL, NULL);
15111 vim_free(enc);
15112# endif
15113 }
15114}
15115#endif
15116
15117/*
15118 * "stridx()" function
15119 */
15120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 typval_T *argvars;
15123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124{
15125 char_u buf[NUMBUFLEN];
15126 char_u *needle;
15127 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015132 needle = get_tv_string_chk(&argvars[1]);
15133 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015134 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 if (needle == NULL || haystack == NULL)
15136 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 if (argvars[2].v_type != VAR_UNKNOWN)
15139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 int error = FALSE;
15141
15142 start_idx = get_tv_number_chk(&argvars[2], &error);
15143 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015144 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015145 if (start_idx >= 0)
15146 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 }
15148
15149 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15150 if (pos != NULL)
15151 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152}
15153
15154/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015155 * "string()" function
15156 */
15157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015158f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015159 typval_T *argvars;
15160 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015161{
15162 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015163 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015166 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015167 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015168 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169}
15170
15171/*
15172 * "strlen()" function
15173 */
15174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 typval_T *argvars;
15177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015179 rettv->vval.v_number = (varnumber_T)(STRLEN(
15180 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181}
15182
15183/*
15184 * "strpart()" function
15185 */
15186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015188 typval_T *argvars;
15189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190{
15191 char_u *p;
15192 int n;
15193 int len;
15194 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015195 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 slen = (int)STRLEN(p);
15199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015200 n = get_tv_number_chk(&argvars[1], &error);
15201 if (error)
15202 len = 0;
15203 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 else
15206 len = slen - n; /* default len: all bytes that are available. */
15207
15208 /*
15209 * Only return the overlap between the specified part and the actual
15210 * string.
15211 */
15212 if (n < 0)
15213 {
15214 len += n;
15215 n = 0;
15216 }
15217 else if (n > slen)
15218 n = slen;
15219 if (len < 0)
15220 len = 0;
15221 else if (n + len > slen)
15222 len = slen - n;
15223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015224 rettv->v_type = VAR_STRING;
15225 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226}
15227
15228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229 * "strridx()" function
15230 */
15231 static void
15232f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 typval_T *argvars;
15234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015235{
15236 char_u buf[NUMBUFLEN];
15237 char_u *needle;
15238 char_u *haystack;
15239 char_u *rest;
15240 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015241 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 needle = get_tv_string_chk(&argvars[1]);
15244 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015245
15246 rettv->vval.v_number = -1;
15247 if (needle == NULL || haystack == NULL)
15248 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015249
15250 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015251 if (argvars[2].v_type != VAR_UNKNOWN)
15252 {
15253 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015255 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015256 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015257 }
15258 else
15259 end_idx = haystack_len;
15260
Bram Moolenaar0d660222005-01-07 21:51:51 +000015261 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015262 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015264 lastmatch = haystack + end_idx;
15265 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015266 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015267 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268 for (rest = haystack; *rest != '\0'; ++rest)
15269 {
15270 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015271 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272 break;
15273 lastmatch = rest;
15274 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015275 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276
15277 if (lastmatch == NULL)
15278 rettv->vval.v_number = -1;
15279 else
15280 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15281}
15282
15283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 * "strtrans()" function
15285 */
15286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015288 typval_T *argvars;
15289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 rettv->v_type = VAR_STRING;
15292 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293}
15294
15295/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015296 * "submatch()" function
15297 */
15298 static void
15299f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015300 typval_T *argvars;
15301 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302{
15303 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 rettv->vval.v_string =
15305 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015306}
15307
15308/*
15309 * "substitute()" function
15310 */
15311 static void
15312f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 typval_T *argvars;
15314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315{
15316 char_u patbuf[NUMBUFLEN];
15317 char_u subbuf[NUMBUFLEN];
15318 char_u flagsbuf[NUMBUFLEN];
15319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 char_u *str = get_tv_string_chk(&argvars[0]);
15321 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15322 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15323 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15324
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15327 rettv->vval.v_string = NULL;
15328 else
15329 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330}
15331
15332/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015333 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 */
15335/*ARGSUSED*/
15336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015338 typval_T *argvars;
15339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340{
15341 int id = 0;
15342#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015343 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 long col;
15345 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015346 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 lnum = get_tv_lnum(argvars); /* -1 on type error */
15349 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15350 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015352 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015353 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015354 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355#endif
15356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358}
15359
15360/*
15361 * "synIDattr(id, what [, mode])" function
15362 */
15363/*ARGSUSED*/
15364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015366 typval_T *argvars;
15367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368{
15369 char_u *p = NULL;
15370#ifdef FEAT_SYN_HL
15371 int id;
15372 char_u *what;
15373 char_u *mode;
15374 char_u modebuf[NUMBUFLEN];
15375 int modec;
15376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 id = get_tv_number(&argvars[0]);
15378 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015379 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 modec = TOLOWER_ASC(mode[0]);
15383 if (modec != 't' && modec != 'c'
15384#ifdef FEAT_GUI
15385 && modec != 'g'
15386#endif
15387 )
15388 modec = 0; /* replace invalid with current */
15389 }
15390 else
15391 {
15392#ifdef FEAT_GUI
15393 if (gui.in_use)
15394 modec = 'g';
15395 else
15396#endif
15397 if (t_colors > 1)
15398 modec = 'c';
15399 else
15400 modec = 't';
15401 }
15402
15403
15404 switch (TOLOWER_ASC(what[0]))
15405 {
15406 case 'b':
15407 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15408 p = highlight_color(id, what, modec);
15409 else /* bold */
15410 p = highlight_has_attr(id, HL_BOLD, modec);
15411 break;
15412
15413 case 'f': /* fg[#] */
15414 p = highlight_color(id, what, modec);
15415 break;
15416
15417 case 'i':
15418 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15419 p = highlight_has_attr(id, HL_INVERSE, modec);
15420 else /* italic */
15421 p = highlight_has_attr(id, HL_ITALIC, modec);
15422 break;
15423
15424 case 'n': /* name */
15425 p = get_highlight_name(NULL, id - 1);
15426 break;
15427
15428 case 'r': /* reverse */
15429 p = highlight_has_attr(id, HL_INVERSE, modec);
15430 break;
15431
15432 case 's': /* standout */
15433 p = highlight_has_attr(id, HL_STANDOUT, modec);
15434 break;
15435
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015436 case 'u':
15437 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15438 /* underline */
15439 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15440 else
15441 /* undercurl */
15442 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 break;
15444 }
15445
15446 if (p != NULL)
15447 p = vim_strsave(p);
15448#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 rettv->v_type = VAR_STRING;
15450 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451}
15452
15453/*
15454 * "synIDtrans(id)" function
15455 */
15456/*ARGSUSED*/
15457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *argvars;
15460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461{
15462 int id;
15463
15464#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
15467 if (id > 0)
15468 id = syn_get_final_id(id);
15469 else
15470#endif
15471 id = 0;
15472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015473 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474}
15475
15476/*
15477 * "system()" function
15478 */
15479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015480f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015481 typval_T *argvars;
15482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015484 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015486 char_u *infile = NULL;
15487 char_u buf[NUMBUFLEN];
15488 int err = FALSE;
15489 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015491 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015492 {
15493 /*
15494 * Write the string to a temp file, to be used for input of the shell
15495 * command.
15496 */
15497 if ((infile = vim_tempname('i')) == NULL)
15498 {
15499 EMSG(_(e_notmp));
15500 return;
15501 }
15502
15503 fd = mch_fopen((char *)infile, WRITEBIN);
15504 if (fd == NULL)
15505 {
15506 EMSG2(_(e_notopen), infile);
15507 goto done;
15508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015509 p = get_tv_string_buf_chk(&argvars[1], buf);
15510 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015511 {
15512 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015513 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015514 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015515 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15516 err = TRUE;
15517 if (fclose(fd) != 0)
15518 err = TRUE;
15519 if (err)
15520 {
15521 EMSG(_("E677: Error writing temp file"));
15522 goto done;
15523 }
15524 }
15525
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015526 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15527 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015528
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529#ifdef USE_CR
15530 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015531 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 {
15533 char_u *s;
15534
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015535 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 {
15537 if (*s == CAR)
15538 *s = NL;
15539 }
15540 }
15541#else
15542# ifdef USE_CRNL
15543 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015544 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 {
15546 char_u *s, *d;
15547
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015548 d = res;
15549 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 {
15551 if (s[0] == CAR && s[1] == NL)
15552 ++s;
15553 *d++ = *s;
15554 }
15555 *d = NUL;
15556 }
15557# endif
15558#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015559
15560done:
15561 if (infile != NULL)
15562 {
15563 mch_remove(infile);
15564 vim_free(infile);
15565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 rettv->v_type = VAR_STRING;
15567 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568}
15569
15570/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015571 * "tabpagebuflist()" function
15572 */
15573/* ARGSUSED */
15574 static void
15575f_tabpagebuflist(argvars, rettv)
15576 typval_T *argvars;
15577 typval_T *rettv;
15578{
15579#ifndef FEAT_WINDOWS
15580 rettv->vval.v_number = 0;
15581#else
15582 tabpage_T *tp;
15583 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015584
15585 if (argvars[0].v_type == VAR_UNKNOWN)
15586 wp = firstwin;
15587 else
15588 {
15589 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15590 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015591 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015592 }
15593 if (wp == NULL)
15594 rettv->vval.v_number = 0;
15595 else
15596 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015597 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015598 rettv->vval.v_number = 0;
15599 else
15600 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015601 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015602 if (list_append_number(rettv->vval.v_list,
15603 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015604 break;
15605 }
15606 }
15607#endif
15608}
15609
15610
15611/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015612 * "tabpagenr()" function
15613 */
15614/* ARGSUSED */
15615 static void
15616f_tabpagenr(argvars, rettv)
15617 typval_T *argvars;
15618 typval_T *rettv;
15619{
15620 int nr = 1;
15621#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015622 char_u *arg;
15623
15624 if (argvars[0].v_type != VAR_UNKNOWN)
15625 {
15626 arg = get_tv_string_chk(&argvars[0]);
15627 nr = 0;
15628 if (arg != NULL)
15629 {
15630 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015631 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015632 else
15633 EMSG2(_(e_invexpr2), arg);
15634 }
15635 }
15636 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015637 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015638#endif
15639 rettv->vval.v_number = nr;
15640}
15641
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015642
15643#ifdef FEAT_WINDOWS
15644static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15645
15646/*
15647 * Common code for tabpagewinnr() and winnr().
15648 */
15649 static int
15650get_winnr(tp, argvar)
15651 tabpage_T *tp;
15652 typval_T *argvar;
15653{
15654 win_T *twin;
15655 int nr = 1;
15656 win_T *wp;
15657 char_u *arg;
15658
15659 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15660 if (argvar->v_type != VAR_UNKNOWN)
15661 {
15662 arg = get_tv_string_chk(argvar);
15663 if (arg == NULL)
15664 nr = 0; /* type error; errmsg already given */
15665 else if (STRCMP(arg, "$") == 0)
15666 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15667 else if (STRCMP(arg, "#") == 0)
15668 {
15669 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15670 if (twin == NULL)
15671 nr = 0;
15672 }
15673 else
15674 {
15675 EMSG2(_(e_invexpr2), arg);
15676 nr = 0;
15677 }
15678 }
15679
15680 if (nr > 0)
15681 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15682 wp != twin; wp = wp->w_next)
15683 ++nr;
15684 return nr;
15685}
15686#endif
15687
15688/*
15689 * "tabpagewinnr()" function
15690 */
15691/* ARGSUSED */
15692 static void
15693f_tabpagewinnr(argvars, rettv)
15694 typval_T *argvars;
15695 typval_T *rettv;
15696{
15697 int nr = 1;
15698#ifdef FEAT_WINDOWS
15699 tabpage_T *tp;
15700
15701 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15702 if (tp == NULL)
15703 nr = 0;
15704 else
15705 nr = get_winnr(tp, &argvars[1]);
15706#endif
15707 rettv->vval.v_number = nr;
15708}
15709
15710
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015711/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015712 * "tagfiles()" function
15713 */
15714/*ARGSUSED*/
15715 static void
15716f_tagfiles(argvars, rettv)
15717 typval_T *argvars;
15718 typval_T *rettv;
15719{
15720 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015721 tagname_T tn;
15722 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015723
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015724 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015725 {
15726 rettv->vval.v_number = 0;
15727 return;
15728 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015729
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015730 for (first = TRUE; ; first = FALSE)
15731 if (get_tagfname(&tn, first, fname) == FAIL
15732 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015733 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015734 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015735}
15736
15737/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015738 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015739 */
15740 static void
15741f_taglist(argvars, rettv)
15742 typval_T *argvars;
15743 typval_T *rettv;
15744{
15745 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015746
15747 tag_pattern = get_tv_string(&argvars[0]);
15748
15749 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015750 if (*tag_pattern == NUL)
15751 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015752
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015753 if (rettv_list_alloc(rettv) == OK)
15754 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015755}
15756
15757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 * "tempname()" function
15759 */
15760/*ARGSUSED*/
15761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015763 typval_T *argvars;
15764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765{
15766 static int x = 'A';
15767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 rettv->v_type = VAR_STRING;
15769 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770
15771 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15772 * names. Skip 'I' and 'O', they are used for shell redirection. */
15773 do
15774 {
15775 if (x == 'Z')
15776 x = '0';
15777 else if (x == '9')
15778 x = 'A';
15779 else
15780 {
15781#ifdef EBCDIC
15782 if (x == 'I')
15783 x = 'J';
15784 else if (x == 'R')
15785 x = 'S';
15786 else
15787#endif
15788 ++x;
15789 }
15790 } while (x == 'I' || x == 'O');
15791}
15792
15793/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015794 * "test(list)" function: Just checking the walls...
15795 */
15796/*ARGSUSED*/
15797 static void
15798f_test(argvars, rettv)
15799 typval_T *argvars;
15800 typval_T *rettv;
15801{
15802 /* Used for unit testing. Change the code below to your liking. */
15803#if 0
15804 listitem_T *li;
15805 list_T *l;
15806 char_u *bad, *good;
15807
15808 if (argvars[0].v_type != VAR_LIST)
15809 return;
15810 l = argvars[0].vval.v_list;
15811 if (l == NULL)
15812 return;
15813 li = l->lv_first;
15814 if (li == NULL)
15815 return;
15816 bad = get_tv_string(&li->li_tv);
15817 li = li->li_next;
15818 if (li == NULL)
15819 return;
15820 good = get_tv_string(&li->li_tv);
15821 rettv->vval.v_number = test_edit_score(bad, good);
15822#endif
15823}
15824
15825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 * "tolower(string)" function
15827 */
15828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015829f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T *argvars;
15831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832{
15833 char_u *p;
15834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015835 p = vim_strsave(get_tv_string(&argvars[0]));
15836 rettv->v_type = VAR_STRING;
15837 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838
15839 if (p != NULL)
15840 while (*p != NUL)
15841 {
15842#ifdef FEAT_MBYTE
15843 int l;
15844
15845 if (enc_utf8)
15846 {
15847 int c, lc;
15848
15849 c = utf_ptr2char(p);
15850 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015851 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 /* TODO: reallocate string when byte count changes. */
15853 if (utf_char2len(lc) == l)
15854 utf_char2bytes(lc, p);
15855 p += l;
15856 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015857 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 p += l; /* skip multi-byte character */
15859 else
15860#endif
15861 {
15862 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15863 ++p;
15864 }
15865 }
15866}
15867
15868/*
15869 * "toupper(string)" function
15870 */
15871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015872f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 typval_T *argvars;
15874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015876 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015877 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878}
15879
15880/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015881 * "tr(string, fromstr, tostr)" function
15882 */
15883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015884f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015885 typval_T *argvars;
15886 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015887{
15888 char_u *instr;
15889 char_u *fromstr;
15890 char_u *tostr;
15891 char_u *p;
15892#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015893 int inlen;
15894 int fromlen;
15895 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015896 int idx;
15897 char_u *cpstr;
15898 int cplen;
15899 int first = TRUE;
15900#endif
15901 char_u buf[NUMBUFLEN];
15902 char_u buf2[NUMBUFLEN];
15903 garray_T ga;
15904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15907 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015908
15909 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015910 rettv->v_type = VAR_STRING;
15911 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 if (fromstr == NULL || tostr == NULL)
15913 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015914 ga_init2(&ga, (int)sizeof(char), 80);
15915
15916#ifdef FEAT_MBYTE
15917 if (!has_mbyte)
15918#endif
15919 /* not multi-byte: fromstr and tostr must be the same length */
15920 if (STRLEN(fromstr) != STRLEN(tostr))
15921 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015922#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015923error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015924#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015925 EMSG2(_(e_invarg2), fromstr);
15926 ga_clear(&ga);
15927 return;
15928 }
15929
15930 /* fromstr and tostr have to contain the same number of chars */
15931 while (*instr != NUL)
15932 {
15933#ifdef FEAT_MBYTE
15934 if (has_mbyte)
15935 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015936 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015937 cpstr = instr;
15938 cplen = inlen;
15939 idx = 0;
15940 for (p = fromstr; *p != NUL; p += fromlen)
15941 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015942 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015943 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15944 {
15945 for (p = tostr; *p != NUL; p += tolen)
15946 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015947 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015948 if (idx-- == 0)
15949 {
15950 cplen = tolen;
15951 cpstr = p;
15952 break;
15953 }
15954 }
15955 if (*p == NUL) /* tostr is shorter than fromstr */
15956 goto error;
15957 break;
15958 }
15959 ++idx;
15960 }
15961
15962 if (first && cpstr == instr)
15963 {
15964 /* Check that fromstr and tostr have the same number of
15965 * (multi-byte) characters. Done only once when a character
15966 * of instr doesn't appear in fromstr. */
15967 first = FALSE;
15968 for (p = tostr; *p != NUL; p += tolen)
15969 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015970 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015971 --idx;
15972 }
15973 if (idx != 0)
15974 goto error;
15975 }
15976
15977 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015978 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015979 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015980
15981 instr += inlen;
15982 }
15983 else
15984#endif
15985 {
15986 /* When not using multi-byte chars we can do it faster. */
15987 p = vim_strchr(fromstr, *instr);
15988 if (p != NULL)
15989 ga_append(&ga, tostr[p - fromstr]);
15990 else
15991 ga_append(&ga, *instr);
15992 ++instr;
15993 }
15994 }
15995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015997}
15998
15999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 * "type(expr)" function
16001 */
16002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 typval_T *argvars;
16005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016007 int n;
16008
16009 switch (argvars[0].v_type)
16010 {
16011 case VAR_NUMBER: n = 0; break;
16012 case VAR_STRING: n = 1; break;
16013 case VAR_FUNC: n = 2; break;
16014 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016015 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016016 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16017 }
16018 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019}
16020
16021/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016022 * "values(dict)" function
16023 */
16024 static void
16025f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 typval_T *argvars;
16027 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016028{
16029 dict_list(argvars, rettv, 1);
16030}
16031
16032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 * "virtcol(string)" function
16034 */
16035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016036f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016037 typval_T *argvars;
16038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039{
16040 colnr_T vcol = 0;
16041 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016042 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016044 fp = var2fpos(&argvars[0], FALSE, &fnum);
16045 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16046 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 {
16048 getvvcol(curwin, fp, NULL, NULL, &vcol);
16049 ++vcol;
16050 }
16051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016052 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053}
16054
16055/*
16056 * "visualmode()" function
16057 */
16058/*ARGSUSED*/
16059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T *argvars;
16062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063{
16064#ifdef FEAT_VISUAL
16065 char_u str[2];
16066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016067 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 str[0] = curbuf->b_visual_mode_eval;
16069 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071
16072 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016073 if ((argvars[0].v_type == VAR_NUMBER
16074 && argvars[0].vval.v_number != 0)
16075 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 curbuf->b_visual_mode_eval = NUL;
16078#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080#endif
16081}
16082
16083/*
16084 * "winbufnr(nr)" function
16085 */
16086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016087f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016088 typval_T *argvars;
16089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090{
16091 win_T *wp;
16092
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016093 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098}
16099
16100/*
16101 * "wincol()" function
16102 */
16103/*ARGSUSED*/
16104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016105f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016106 typval_T *argvars;
16107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108{
16109 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016110 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111}
16112
16113/*
16114 * "winheight(nr)" function
16115 */
16116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016117f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016118 typval_T *argvars;
16119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120{
16121 win_T *wp;
16122
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016123 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016125 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016127 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128}
16129
16130/*
16131 * "winline()" function
16132 */
16133/*ARGSUSED*/
16134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016135f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016136 typval_T *argvars;
16137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138{
16139 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016140 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141}
16142
16143/*
16144 * "winnr()" function
16145 */
16146/* ARGSUSED */
16147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 typval_T *argvars;
16150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151{
16152 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016153
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016155 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016157 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158}
16159
16160/*
16161 * "winrestcmd()" function
16162 */
16163/* ARGSUSED */
16164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016165f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 typval_T *argvars;
16167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168{
16169#ifdef FEAT_WINDOWS
16170 win_T *wp;
16171 int winnr = 1;
16172 garray_T ga;
16173 char_u buf[50];
16174
16175 ga_init2(&ga, (int)sizeof(char), 70);
16176 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16177 {
16178 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16179 ga_concat(&ga, buf);
16180# ifdef FEAT_VERTSPLIT
16181 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16182 ga_concat(&ga, buf);
16183# endif
16184 ++winnr;
16185 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016186 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016192 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193}
16194
16195/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016196 * "winrestview()" function
16197 */
16198/* ARGSUSED */
16199 static void
16200f_winrestview(argvars, rettv)
16201 typval_T *argvars;
16202 typval_T *rettv;
16203{
16204 dict_T *dict;
16205
16206 if (argvars[0].v_type != VAR_DICT
16207 || (dict = argvars[0].vval.v_dict) == NULL)
16208 EMSG(_(e_invarg));
16209 else
16210 {
16211 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16212 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16213#ifdef FEAT_VIRTUALEDIT
16214 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16215#endif
16216 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016217 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016218
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016219 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016220#ifdef FEAT_DIFF
16221 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16222#endif
16223 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16224 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16225
16226 check_cursor();
16227 changed_cline_bef_curs();
16228 invalidate_botline();
16229 redraw_later(VALID);
16230
16231 if (curwin->w_topline == 0)
16232 curwin->w_topline = 1;
16233 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16234 curwin->w_topline = curbuf->b_ml.ml_line_count;
16235#ifdef FEAT_DIFF
16236 check_topfill(curwin, TRUE);
16237#endif
16238 }
16239}
16240
16241/*
16242 * "winsaveview()" function
16243 */
16244/* ARGSUSED */
16245 static void
16246f_winsaveview(argvars, rettv)
16247 typval_T *argvars;
16248 typval_T *rettv;
16249{
16250 dict_T *dict;
16251
16252 dict = dict_alloc();
16253 if (dict == NULL)
16254 return;
16255 rettv->v_type = VAR_DICT;
16256 rettv->vval.v_dict = dict;
16257 ++dict->dv_refcount;
16258
16259 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16260 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16261#ifdef FEAT_VIRTUALEDIT
16262 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16263#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016264 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016265 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16266
16267 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16268#ifdef FEAT_DIFF
16269 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16270#endif
16271 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16272 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16273}
16274
16275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 * "winwidth(nr)" function
16277 */
16278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016279f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 typval_T *argvars;
16281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282{
16283 win_T *wp;
16284
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016285 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016287 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 else
16289#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016290 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016292 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293#endif
16294}
16295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016297 * "writefile()" function
16298 */
16299 static void
16300f_writefile(argvars, rettv)
16301 typval_T *argvars;
16302 typval_T *rettv;
16303{
16304 int binary = FALSE;
16305 char_u *fname;
16306 FILE *fd;
16307 listitem_T *li;
16308 char_u *s;
16309 int ret = 0;
16310 int c;
16311
16312 if (argvars[0].v_type != VAR_LIST)
16313 {
16314 EMSG2(_(e_listarg), "writefile()");
16315 return;
16316 }
16317 if (argvars[0].vval.v_list == NULL)
16318 return;
16319
16320 if (argvars[2].v_type != VAR_UNKNOWN
16321 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16322 binary = TRUE;
16323
16324 /* Always open the file in binary mode, library functions have a mind of
16325 * their own about CR-LF conversion. */
16326 fname = get_tv_string(&argvars[1]);
16327 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16328 {
16329 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16330 ret = -1;
16331 }
16332 else
16333 {
16334 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16335 li = li->li_next)
16336 {
16337 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16338 {
16339 if (*s == '\n')
16340 c = putc(NUL, fd);
16341 else
16342 c = putc(*s, fd);
16343 if (c == EOF)
16344 {
16345 ret = -1;
16346 break;
16347 }
16348 }
16349 if (!binary || li->li_next != NULL)
16350 if (putc('\n', fd) == EOF)
16351 {
16352 ret = -1;
16353 break;
16354 }
16355 if (ret < 0)
16356 {
16357 EMSG(_(e_write));
16358 break;
16359 }
16360 }
16361 fclose(fd);
16362 }
16363
16364 rettv->vval.v_number = ret;
16365}
16366
16367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016369 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 */
16371 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016372var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016373 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016375 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016377 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016379 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380
Bram Moolenaara5525202006-03-02 22:52:09 +000016381 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016382 if (varp->v_type == VAR_LIST)
16383 {
16384 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016385 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016386 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016387
16388 l = varp->vval.v_list;
16389 if (l == NULL)
16390 return NULL;
16391
16392 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016393 pos.lnum = list_find_nr(l, 0L, &error);
16394 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016395 return NULL; /* invalid line number */
16396
16397 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016398 pos.col = list_find_nr(l, 1L, &error);
16399 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016400 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016401 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016402 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016403 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016404 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016405 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016406
Bram Moolenaara5525202006-03-02 22:52:09 +000016407#ifdef FEAT_VIRTUALEDIT
16408 /* Get the virtual offset. Defaults to zero. */
16409 pos.coladd = list_find_nr(l, 2L, &error);
16410 if (error)
16411 pos.coladd = 0;
16412#endif
16413
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016414 return &pos;
16415 }
16416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 name = get_tv_string_chk(varp);
16418 if (name == NULL)
16419 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 if (name[0] == '.') /* cursor */
16421 return &curwin->w_cursor;
16422 if (name[0] == '\'') /* mark */
16423 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016424 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16426 return NULL;
16427 return pp;
16428 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016429
16430#ifdef FEAT_VIRTUALEDIT
16431 pos.coladd = 0;
16432#endif
16433
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016434 if (name[0] == 'w' && lnum)
16435 {
16436 pos.col = 0;
16437 if (name[1] == '0') /* "w0": first visible line */
16438 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016439 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016440 pos.lnum = curwin->w_topline;
16441 return &pos;
16442 }
16443 else if (name[1] == '$') /* "w$": last visible line */
16444 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016445 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016446 pos.lnum = curwin->w_botline - 1;
16447 return &pos;
16448 }
16449 }
16450 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 {
16452 if (lnum)
16453 {
16454 pos.lnum = curbuf->b_ml.ml_line_count;
16455 pos.col = 0;
16456 }
16457 else
16458 {
16459 pos.lnum = curwin->w_cursor.lnum;
16460 pos.col = (colnr_T)STRLEN(ml_get_curline());
16461 }
16462 return &pos;
16463 }
16464 return NULL;
16465}
16466
16467/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016468 * Convert list in "arg" into a position and optional file number.
16469 * When "fnump" is NULL there is no file number, only 3 items.
16470 * Note that the column is passed on as-is, the caller may want to decrement
16471 * it to use 1 for the first column.
16472 * Return FAIL when conversion is not possible, doesn't check the position for
16473 * validity.
16474 */
16475 static int
16476list2fpos(arg, posp, fnump)
16477 typval_T *arg;
16478 pos_T *posp;
16479 int *fnump;
16480{
16481 list_T *l = arg->vval.v_list;
16482 long i = 0;
16483 long n;
16484
Bram Moolenaarbde35262006-07-23 20:12:24 +000016485 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16486 * when "fnump" isn't NULL and "coladd" is optional. */
16487 if (arg->v_type != VAR_LIST
16488 || l == NULL
16489 || l->lv_len < (fnump == NULL ? 2 : 3)
16490 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016491 return FAIL;
16492
16493 if (fnump != NULL)
16494 {
16495 n = list_find_nr(l, i++, NULL); /* fnum */
16496 if (n < 0)
16497 return FAIL;
16498 if (n == 0)
16499 n = curbuf->b_fnum; /* current buffer */
16500 *fnump = n;
16501 }
16502
16503 n = list_find_nr(l, i++, NULL); /* lnum */
16504 if (n < 0)
16505 return FAIL;
16506 posp->lnum = n;
16507
16508 n = list_find_nr(l, i++, NULL); /* col */
16509 if (n < 0)
16510 return FAIL;
16511 posp->col = n;
16512
16513#ifdef FEAT_VIRTUALEDIT
16514 n = list_find_nr(l, i, NULL);
16515 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016516 posp->coladd = 0;
16517 else
16518 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016519#endif
16520
16521 return OK;
16522}
16523
16524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 * Get the length of an environment variable name.
16526 * Advance "arg" to the first character after the name.
16527 * Return 0 for error.
16528 */
16529 static int
16530get_env_len(arg)
16531 char_u **arg;
16532{
16533 char_u *p;
16534 int len;
16535
16536 for (p = *arg; vim_isIDc(*p); ++p)
16537 ;
16538 if (p == *arg) /* no name found */
16539 return 0;
16540
16541 len = (int)(p - *arg);
16542 *arg = p;
16543 return len;
16544}
16545
16546/*
16547 * Get the length of the name of a function or internal variable.
16548 * "arg" is advanced to the first non-white character after the name.
16549 * Return 0 if something is wrong.
16550 */
16551 static int
16552get_id_len(arg)
16553 char_u **arg;
16554{
16555 char_u *p;
16556 int len;
16557
16558 /* Find the end of the name. */
16559 for (p = *arg; eval_isnamec(*p); ++p)
16560 ;
16561 if (p == *arg) /* no name found */
16562 return 0;
16563
16564 len = (int)(p - *arg);
16565 *arg = skipwhite(p);
16566
16567 return len;
16568}
16569
16570/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016571 * Get the length of the name of a variable or function.
16572 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574 * Return -1 if curly braces expansion failed.
16575 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 * If the name contains 'magic' {}'s, expand them and return the
16577 * expanded name in an allocated string via 'alias' - caller must free.
16578 */
16579 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016580get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 char_u **arg;
16582 char_u **alias;
16583 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016584 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585{
16586 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 char_u *p;
16588 char_u *expr_start;
16589 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590
16591 *alias = NULL; /* default to no alias */
16592
16593 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16594 && (*arg)[2] == (int)KE_SNR)
16595 {
16596 /* hard coded <SNR>, already translated */
16597 *arg += 3;
16598 return get_id_len(arg) + 3;
16599 }
16600 len = eval_fname_script(*arg);
16601 if (len > 0)
16602 {
16603 /* literal "<SID>", "s:" or "<SNR>" */
16604 *arg += len;
16605 }
16606
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016608 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016610 p = find_name_end(*arg, &expr_start, &expr_end,
16611 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 if (expr_start != NULL)
16613 {
16614 char_u *temp_string;
16615
16616 if (!evaluate)
16617 {
16618 len += (int)(p - *arg);
16619 *arg = skipwhite(p);
16620 return len;
16621 }
16622
16623 /*
16624 * Include any <SID> etc in the expanded string:
16625 * Thus the -len here.
16626 */
16627 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16628 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 *alias = temp_string;
16631 *arg = skipwhite(p);
16632 return (int)STRLEN(temp_string);
16633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634
16635 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016636 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 EMSG2(_(e_invexpr2), *arg);
16638
16639 return len;
16640}
16641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642/*
16643 * Find the end of a variable or function name, taking care of magic braces.
16644 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16645 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016646 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647 * Return a pointer to just after the name. Equal to "arg" if there is no
16648 * valid name.
16649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016651find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 char_u *arg;
16653 char_u **expr_start;
16654 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016655 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016657 int mb_nest = 0;
16658 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 char_u *p;
16660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663 *expr_start = NULL;
16664 *expr_end = NULL;
16665 }
16666
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016667 /* Quick check for valid starting character. */
16668 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16669 return arg;
16670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671 for (p = arg; *p != NUL
16672 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016673 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016674 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016676 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016677 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016678 if (*p == '\'')
16679 {
16680 /* skip over 'string' to avoid counting [ and ] inside it. */
16681 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16682 ;
16683 if (*p == NUL)
16684 break;
16685 }
16686 else if (*p == '"')
16687 {
16688 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16689 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16690 if (*p == '\\' && p[1] != NUL)
16691 ++p;
16692 if (*p == NUL)
16693 break;
16694 }
16695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016696 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016698 if (*p == '[')
16699 ++br_nest;
16700 else if (*p == ']')
16701 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016706 if (*p == '{')
16707 {
16708 mb_nest++;
16709 if (expr_start != NULL && *expr_start == NULL)
16710 *expr_start = p;
16711 }
16712 else if (*p == '}')
16713 {
16714 mb_nest--;
16715 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16716 *expr_end = p;
16717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 }
16720
16721 return p;
16722}
16723
16724/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016725 * Expands out the 'magic' {}'s in a variable/function name.
16726 * Note that this can call itself recursively, to deal with
16727 * constructs like foo{bar}{baz}{bam}
16728 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16729 * "in_start" ^
16730 * "expr_start" ^
16731 * "expr_end" ^
16732 * "in_end" ^
16733 *
16734 * Returns a new allocated string, which the caller must free.
16735 * Returns NULL for failure.
16736 */
16737 static char_u *
16738make_expanded_name(in_start, expr_start, expr_end, in_end)
16739 char_u *in_start;
16740 char_u *expr_start;
16741 char_u *expr_end;
16742 char_u *in_end;
16743{
16744 char_u c1;
16745 char_u *retval = NULL;
16746 char_u *temp_result;
16747 char_u *nextcmd = NULL;
16748
16749 if (expr_end == NULL || in_end == NULL)
16750 return NULL;
16751 *expr_start = NUL;
16752 *expr_end = NUL;
16753 c1 = *in_end;
16754 *in_end = NUL;
16755
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016756 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757 if (temp_result != NULL && nextcmd == NULL)
16758 {
16759 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16760 + (in_end - expr_end) + 1));
16761 if (retval != NULL)
16762 {
16763 STRCPY(retval, in_start);
16764 STRCAT(retval, temp_result);
16765 STRCAT(retval, expr_end + 1);
16766 }
16767 }
16768 vim_free(temp_result);
16769
16770 *in_end = c1; /* put char back for error messages */
16771 *expr_start = '{';
16772 *expr_end = '}';
16773
16774 if (retval != NULL)
16775 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016776 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016777 if (expr_start != NULL)
16778 {
16779 /* Further expansion! */
16780 temp_result = make_expanded_name(retval, expr_start,
16781 expr_end, temp_result);
16782 vim_free(retval);
16783 retval = temp_result;
16784 }
16785 }
16786
16787 return retval;
16788}
16789
16790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016792 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 */
16794 static int
16795eval_isnamec(c)
16796 int c;
16797{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016798 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16799}
16800
16801/*
16802 * Return TRUE if character "c" can be used as the first character in a
16803 * variable or function name (excluding '{' and '}').
16804 */
16805 static int
16806eval_isnamec1(c)
16807 int c;
16808{
16809 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810}
16811
16812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 * Set number v: variable to "val".
16814 */
16815 void
16816set_vim_var_nr(idx, val)
16817 int idx;
16818 long val;
16819{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016820 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821}
16822
16823/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016824 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 */
16826 long
16827get_vim_var_nr(idx)
16828 int idx;
16829{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016830 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831}
16832
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016833#if defined(FEAT_AUTOCMD) || defined(PROTO)
16834/*
16835 * Get string v: variable value. Uses a static buffer, can only be used once.
16836 */
16837 char_u *
16838get_vim_var_str(idx)
16839 int idx;
16840{
16841 return get_tv_string(&vimvars[idx].vv_tv);
16842}
16843#endif
16844
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845/*
16846 * Set v:count, v:count1 and v:prevcount.
16847 */
16848 void
16849set_vcount(count, count1)
16850 long count;
16851 long count1;
16852{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016853 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16854 vimvars[VV_COUNT].vv_nr = count;
16855 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856}
16857
16858/*
16859 * Set string v: variable to a copy of "val".
16860 */
16861 void
16862set_vim_var_string(idx, val, len)
16863 int idx;
16864 char_u *val;
16865 int len; /* length of "val" to use or -1 (whole string) */
16866{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016867 /* Need to do this (at least) once, since we can't initialize a union.
16868 * Will always be invoked when "v:progname" is set. */
16869 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16870
Bram Moolenaare9a41262005-01-15 22:18:47 +000016871 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016873 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016875 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016877 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878}
16879
16880/*
16881 * Set v:register if needed.
16882 */
16883 void
16884set_reg_var(c)
16885 int c;
16886{
16887 char_u regname;
16888
16889 if (c == 0 || c == ' ')
16890 regname = '"';
16891 else
16892 regname = c;
16893 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016894 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 set_vim_var_string(VV_REG, &regname, 1);
16896}
16897
16898/*
16899 * Get or set v:exception. If "oldval" == NULL, return the current value.
16900 * Otherwise, restore the value to "oldval" and return NULL.
16901 * Must always be called in pairs to save and restore v:exception! Does not
16902 * take care of memory allocations.
16903 */
16904 char_u *
16905v_exception(oldval)
16906 char_u *oldval;
16907{
16908 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016909 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910
Bram Moolenaare9a41262005-01-15 22:18:47 +000016911 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912 return NULL;
16913}
16914
16915/*
16916 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16917 * Otherwise, restore the value to "oldval" and return NULL.
16918 * Must always be called in pairs to save and restore v:throwpoint! Does not
16919 * take care of memory allocations.
16920 */
16921 char_u *
16922v_throwpoint(oldval)
16923 char_u *oldval;
16924{
16925 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016926 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927
Bram Moolenaare9a41262005-01-15 22:18:47 +000016928 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 return NULL;
16930}
16931
16932#if defined(FEAT_AUTOCMD) || defined(PROTO)
16933/*
16934 * Set v:cmdarg.
16935 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16936 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16937 * Must always be called in pairs!
16938 */
16939 char_u *
16940set_cmdarg(eap, oldarg)
16941 exarg_T *eap;
16942 char_u *oldarg;
16943{
16944 char_u *oldval;
16945 char_u *newval;
16946 unsigned len;
16947
Bram Moolenaare9a41262005-01-15 22:18:47 +000016948 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016949 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016951 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016952 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 }
16955
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016956 if (eap->force_bin == FORCE_BIN)
16957 len = 6;
16958 else if (eap->force_bin == FORCE_NOBIN)
16959 len = 8;
16960 else
16961 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016962
16963 if (eap->read_edit)
16964 len += 7;
16965
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016966 if (eap->force_ff != 0)
16967 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16968# ifdef FEAT_MBYTE
16969 if (eap->force_enc != 0)
16970 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016971 if (eap->bad_char != 0)
16972 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016973# endif
16974
16975 newval = alloc(len + 1);
16976 if (newval == NULL)
16977 return NULL;
16978
16979 if (eap->force_bin == FORCE_BIN)
16980 sprintf((char *)newval, " ++bin");
16981 else if (eap->force_bin == FORCE_NOBIN)
16982 sprintf((char *)newval, " ++nobin");
16983 else
16984 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016985
16986 if (eap->read_edit)
16987 STRCAT(newval, " ++edit");
16988
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016989 if (eap->force_ff != 0)
16990 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16991 eap->cmd + eap->force_ff);
16992# ifdef FEAT_MBYTE
16993 if (eap->force_enc != 0)
16994 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16995 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016996 if (eap->bad_char != 0)
16997 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16998 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016999# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017000 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017001 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002}
17003#endif
17004
17005/*
17006 * Get the value of internal variable "name".
17007 * Return OK or FAIL.
17008 */
17009 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017010get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 char_u *name;
17012 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017014 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015{
17016 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 typval_T *tv = NULL;
17018 typval_T atv;
17019 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021
17022 /* truncate the name, so that we can use strcmp() */
17023 cc = name[len];
17024 name[len] = NUL;
17025
17026 /*
17027 * Check for "b:changedtick".
17028 */
17029 if (STRCMP(name, "b:changedtick") == 0)
17030 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017031 atv.v_type = VAR_NUMBER;
17032 atv.vval.v_number = curbuf->b_changedtick;
17033 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 }
17035
17036 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 * Check for user-defined variables.
17038 */
17039 else
17040 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017041 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 }
17045
Bram Moolenaare9a41262005-01-15 22:18:47 +000017046 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017048 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017049 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 ret = FAIL;
17051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017052 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017053 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054
17055 name[len] = cc;
17056
17057 return ret;
17058}
17059
17060/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017061 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17062 * Also handle function call with Funcref variable: func(expr)
17063 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17064 */
17065 static int
17066handle_subscript(arg, rettv, evaluate, verbose)
17067 char_u **arg;
17068 typval_T *rettv;
17069 int evaluate; /* do more than finding the end */
17070 int verbose; /* give error messages */
17071{
17072 int ret = OK;
17073 dict_T *selfdict = NULL;
17074 char_u *s;
17075 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017076 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017077
17078 while (ret == OK
17079 && (**arg == '['
17080 || (**arg == '.' && rettv->v_type == VAR_DICT)
17081 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17082 && !vim_iswhite(*(*arg - 1)))
17083 {
17084 if (**arg == '(')
17085 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017086 /* need to copy the funcref so that we can clear rettv */
17087 functv = *rettv;
17088 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017089
17090 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017091 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017092 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017093 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17094 &len, evaluate, selfdict);
17095
17096 /* Clear the funcref afterwards, so that deleting it while
17097 * evaluating the arguments is possible (see test55). */
17098 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017099
17100 /* Stop the expression evaluation when immediately aborting on
17101 * error, or when an interrupt occurred or an exception was thrown
17102 * but not caught. */
17103 if (aborting())
17104 {
17105 if (ret == OK)
17106 clear_tv(rettv);
17107 ret = FAIL;
17108 }
17109 dict_unref(selfdict);
17110 selfdict = NULL;
17111 }
17112 else /* **arg == '[' || **arg == '.' */
17113 {
17114 dict_unref(selfdict);
17115 if (rettv->v_type == VAR_DICT)
17116 {
17117 selfdict = rettv->vval.v_dict;
17118 if (selfdict != NULL)
17119 ++selfdict->dv_refcount;
17120 }
17121 else
17122 selfdict = NULL;
17123 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17124 {
17125 clear_tv(rettv);
17126 ret = FAIL;
17127 }
17128 }
17129 }
17130 dict_unref(selfdict);
17131 return ret;
17132}
17133
17134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017135 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17136 * value).
17137 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017138 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017139alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017140{
Bram Moolenaar33570922005-01-25 22:26:29 +000017141 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017142}
17143
17144/*
17145 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 * The string "s" must have been allocated, it is consumed.
17147 * Return NULL for out of memory, the variable otherwise.
17148 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 char_u *s;
17152{
Bram Moolenaar33570922005-01-25 22:26:29 +000017153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017155 rettv = alloc_tv();
17156 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017158 rettv->v_type = VAR_STRING;
17159 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 }
17161 else
17162 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017163 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164}
17165
17166/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017167 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017169 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172{
17173 if (varp != NULL)
17174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017175 switch (varp->v_type)
17176 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017177 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017178 func_unref(varp->vval.v_string);
17179 /*FALLTHROUGH*/
17180 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017181 vim_free(varp->vval.v_string);
17182 break;
17183 case VAR_LIST:
17184 list_unref(varp->vval.v_list);
17185 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017186 case VAR_DICT:
17187 dict_unref(varp->vval.v_dict);
17188 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017189 case VAR_NUMBER:
17190 case VAR_UNKNOWN:
17191 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017192 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017193 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194 break;
17195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 vim_free(varp);
17197 }
17198}
17199
17200/*
17201 * Free the memory for a variable value and set the value to NULL or 0.
17202 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017203 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017205 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206{
17207 if (varp != NULL)
17208 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017209 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017211 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017212 func_unref(varp->vval.v_string);
17213 /*FALLTHROUGH*/
17214 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017215 vim_free(varp->vval.v_string);
17216 varp->vval.v_string = NULL;
17217 break;
17218 case VAR_LIST:
17219 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017220 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017221 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017222 case VAR_DICT:
17223 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017224 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017225 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017227 varp->vval.v_number = 0;
17228 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229 case VAR_UNKNOWN:
17230 break;
17231 default:
17232 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017234 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 }
17236}
17237
17238/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239 * Set the value of a variable to NULL without freeing items.
17240 */
17241 static void
17242init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017244{
17245 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247}
17248
17249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 * Get the number value of a variable.
17251 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 * For incompatible types, return 0.
17253 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17254 * caller of incompatible types: it sets *denote to TRUE if "denote"
17255 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 */
17257 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017258get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017261 int error = FALSE;
17262
17263 return get_tv_number_chk(varp, &error); /* return 0L on error */
17264}
17265
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017266 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017267get_tv_number_chk(varp, denote)
17268 typval_T *varp;
17269 int *denote;
17270{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017275 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017276 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017277 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017278 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017279 break;
17280 case VAR_STRING:
17281 if (varp->vval.v_string != NULL)
17282 vim_str2nr(varp->vval.v_string, NULL, NULL,
17283 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017285 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017286 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017287 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017288 case VAR_DICT:
17289 EMSG(_("E728: Using a Dictionary as a number"));
17290 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017292 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017295 if (denote == NULL) /* useful for values that must be unsigned */
17296 n = -1;
17297 else
17298 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017299 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300}
17301
17302/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017303 * Get the lnum from the first argument.
17304 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017305 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 */
17307 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017308get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017309 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310{
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 linenr_T lnum;
17313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017314 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 if (lnum == 0) /* no valid number, try using line() */
17316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017317 rettv.v_type = VAR_NUMBER;
17318 f_line(argvars, &rettv);
17319 lnum = rettv.vval.v_number;
17320 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 }
17322 return lnum;
17323}
17324
17325/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017326 * Get the lnum from the first argument.
17327 * Also accepts "$", then "buf" is used.
17328 * Returns 0 on error.
17329 */
17330 static linenr_T
17331get_tv_lnum_buf(argvars, buf)
17332 typval_T *argvars;
17333 buf_T *buf;
17334{
17335 if (argvars[0].v_type == VAR_STRING
17336 && argvars[0].vval.v_string != NULL
17337 && argvars[0].vval.v_string[0] == '$'
17338 && buf != NULL)
17339 return buf->b_ml.ml_line_count;
17340 return get_tv_number_chk(&argvars[0], NULL);
17341}
17342
17343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 * Get the string value of a variable.
17345 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017346 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17347 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 * If the String variable has never been set, return an empty string.
17349 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017350 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17351 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 */
17353 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017354get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356{
17357 static char_u mybuf[NUMBUFLEN];
17358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360}
17361
17362 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017365 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 char_u *res = get_tv_string_buf_chk(varp, buf);
17368
17369 return res != NULL ? res : (char_u *)"";
17370}
17371
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017372 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373get_tv_string_chk(varp)
17374 typval_T *varp;
17375{
17376 static char_u mybuf[NUMBUFLEN];
17377
17378 return get_tv_string_buf_chk(varp, mybuf);
17379}
17380
17381 static char_u *
17382get_tv_string_buf_chk(varp, buf)
17383 typval_T *varp;
17384 char_u *buf;
17385{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017386 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017388 case VAR_NUMBER:
17389 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17390 return buf;
17391 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017392 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393 break;
17394 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017395 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017396 break;
17397 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017398 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017399 break;
17400 case VAR_STRING:
17401 if (varp->vval.v_string != NULL)
17402 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017403 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017405 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017408 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409}
17410
17411/*
17412 * Find variable "name" in the list of variables.
17413 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017414 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017415 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017416 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017418 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017419find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017421 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017424 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425
Bram Moolenaara7043832005-01-21 11:56:39 +000017426 ht = find_var_ht(name, &varname);
17427 if (htp != NULL)
17428 *htp = ht;
17429 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017431 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432}
17433
17434/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017436 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017438 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017439find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017440 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017441 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017442 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017443{
Bram Moolenaar33570922005-01-25 22:26:29 +000017444 hashitem_T *hi;
17445
17446 if (*varname == NUL)
17447 {
17448 /* Must be something like "s:", otherwise "ht" would be NULL. */
17449 switch (varname[-2])
17450 {
17451 case 's': return &SCRIPT_SV(current_SID).sv_var;
17452 case 'g': return &globvars_var;
17453 case 'v': return &vimvars_var;
17454 case 'b': return &curbuf->b_bufvar;
17455 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017456#ifdef FEAT_WINDOWS
17457 case 't': return &curtab->tp_winvar;
17458#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017459 case 'l': return current_funccal == NULL
17460 ? NULL : &current_funccal->l_vars_var;
17461 case 'a': return current_funccal == NULL
17462 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 }
17464 return NULL;
17465 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017466
17467 hi = hash_find(ht, varname);
17468 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017469 {
17470 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017471 * worked find the variable again. Don't auto-load a script if it was
17472 * loaded already, otherwise it would be loaded every time when
17473 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017474 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017475 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017476 hi = hash_find(ht, varname);
17477 if (HASHITEM_EMPTY(hi))
17478 return NULL;
17479 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017481}
17482
17483/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017484 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017485 * Set "varname" to the start of name without ':'.
17486 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017488find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 char_u *name;
17490 char_u **varname;
17491{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017492 hashitem_T *hi;
17493
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 if (name[1] != ':')
17495 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017496 /* The name must not start with a colon or #. */
17497 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 return NULL;
17499 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017500
17501 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017502 hi = hash_find(&compat_hashtab, name);
17503 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017504 return &compat_hashtab;
17505
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 return &globvarht; /* global variable */
17508 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 }
17510 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017511 if (*name == 'g') /* global variable */
17512 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017513 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17514 */
17515 if (vim_strchr(name + 2, ':') != NULL
17516 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017517 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017519 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017521 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017522#ifdef FEAT_WINDOWS
17523 if (*name == 't') /* tab page variable */
17524 return &curtab->tp_vars.dv_hashtab;
17525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 if (*name == 'v') /* v: variable */
17527 return &vimvarht;
17528 if (*name == 'a' && current_funccal != NULL) /* function argument */
17529 return &current_funccal->l_avars.dv_hashtab;
17530 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17531 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 if (*name == 's' /* script variable */
17533 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17534 return &SCRIPT_VARS(current_SID);
17535 return NULL;
17536}
17537
17538/*
17539 * Get the string value of a (global/local) variable.
17540 * Returns NULL when it doesn't exist.
17541 */
17542 char_u *
17543get_var_value(name)
17544 char_u *name;
17545{
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
Bram Moolenaara7043832005-01-21 11:56:39 +000017548 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 if (v == NULL)
17550 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552}
17553
17554/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 * sourcing this script and when executing functions defined in the script.
17557 */
17558 void
17559new_script_vars(id)
17560 scid_T id;
17561{
Bram Moolenaara7043832005-01-21 11:56:39 +000017562 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017563 hashtab_T *ht;
17564 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017565
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17567 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017568 /* Re-allocating ga_data means that an ht_array pointing to
17569 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017570 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017571 for (i = 1; i <= ga_scripts.ga_len; ++i)
17572 {
17573 ht = &SCRIPT_VARS(i);
17574 if (ht->ht_mask == HT_INIT_SIZE - 1)
17575 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 sv = &SCRIPT_SV(i);
17577 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017578 }
17579
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 while (ga_scripts.ga_len < id)
17581 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017582 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17583 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 }
17586 }
17587}
17588
17589/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017590 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17591 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 */
17593 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017594init_var_dict(dict, dict_var)
17595 dict_T *dict;
17596 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597{
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 hash_init(&dict->dv_hashtab);
17599 dict->dv_refcount = 99999;
17600 dict_var->di_tv.vval.v_dict = dict;
17601 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017602 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017603 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17604 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605}
17606
17607/*
17608 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017609 * Frees all allocated variables and the value they contain.
17610 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 */
17612 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017613vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 hashtab_T *ht;
17615{
17616 vars_clear_ext(ht, TRUE);
17617}
17618
17619/*
17620 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17621 */
17622 static void
17623vars_clear_ext(ht, free_val)
17624 hashtab_T *ht;
17625 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
Bram Moolenaara7043832005-01-21 11:56:39 +000017627 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 hashitem_T *hi;
17629 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
Bram Moolenaar33570922005-01-25 22:26:29 +000017631 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017632 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017633 for (hi = ht->ht_array; todo > 0; ++hi)
17634 {
17635 if (!HASHITEM_EMPTY(hi))
17636 {
17637 --todo;
17638
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017640 * ht_array might change then. hash_clear() takes care of it
17641 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 v = HI2DI(hi);
17643 if (free_val)
17644 clear_tv(&v->di_tv);
17645 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17646 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017647 }
17648 }
17649 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017650 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651}
17652
Bram Moolenaara7043832005-01-21 11:56:39 +000017653/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 * Delete a variable from hashtab "ht" at item "hi".
17655 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017658delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 hashtab_T *ht;
17660 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661{
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017663
17664 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 clear_tv(&di->di_tv);
17666 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667}
17668
17669/*
17670 * List the value of one internal variable.
17671 */
17672 static void
17673list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 char_u *prefix;
17676{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017677 char_u *tofree;
17678 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017679 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017680
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017681 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017682 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017683 s == NULL ? (char_u *)"" : s);
17684 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685}
17686
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 static void
17688list_one_var_a(prefix, name, type, string)
17689 char_u *prefix;
17690 char_u *name;
17691 int type;
17692 char_u *string;
17693{
17694 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17695 if (name != NULL) /* "a:" vars don't have a name stored */
17696 msg_puts(name);
17697 msg_putchar(' ');
17698 msg_advance(22);
17699 if (type == VAR_NUMBER)
17700 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017701 else if (type == VAR_FUNC)
17702 msg_putchar('*');
17703 else if (type == VAR_LIST)
17704 {
17705 msg_putchar('[');
17706 if (*string == '[')
17707 ++string;
17708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017709 else if (type == VAR_DICT)
17710 {
17711 msg_putchar('{');
17712 if (*string == '{')
17713 ++string;
17714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 else
17716 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017717
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017719
17720 if (type == VAR_FUNC)
17721 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722}
17723
17724/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017725 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 * If the variable already exists, the value is updated.
17727 * Otherwise the variable is created.
17728 */
17729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017730set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017732 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017733 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734{
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017738 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017740 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017741 {
17742 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17743 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17744 ? name[2] : name[0]))
17745 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017746 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 return;
17748 }
17749 if (function_exists(name))
17750 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017751 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017752 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017753 return;
17754 }
17755 }
17756
Bram Moolenaara7043832005-01-21 11:56:39 +000017757 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017759 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017760 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017761 return;
17762 }
17763
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017764 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017768 if (var_check_ro(v->di_flags, name)
17769 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 return;
17771 if (v->di_tv.v_type != tv->v_type
17772 && !((v->di_tv.v_type == VAR_STRING
17773 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017774 && (tv->v_type == VAR_STRING
17775 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017776 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017777 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017778 return;
17779 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017780
17781 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017782 * Handle setting internal v: variables separately: we don't change
17783 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 */
17785 if (ht == &vimvarht)
17786 {
17787 if (v->di_tv.v_type == VAR_STRING)
17788 {
17789 vim_free(v->di_tv.vval.v_string);
17790 if (copy || tv->v_type != VAR_STRING)
17791 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17792 else
17793 {
17794 /* Take over the string to avoid an extra alloc/free. */
17795 v->di_tv.vval.v_string = tv->vval.v_string;
17796 tv->vval.v_string = NULL;
17797 }
17798 }
17799 else if (v->di_tv.v_type != VAR_NUMBER)
17800 EMSG2(_(e_intern2), "set_var()");
17801 else
17802 v->di_tv.vval.v_number = get_tv_number(tv);
17803 return;
17804 }
17805
17806 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 }
17808 else /* add a new variable */
17809 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017810 /* Can't add "v:" variable. */
17811 if (ht == &vimvarht)
17812 {
17813 EMSG2(_(e_illvar), name);
17814 return;
17815 }
17816
Bram Moolenaar92124a32005-06-17 22:03:40 +000017817 /* Make sure the variable name is valid. */
17818 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017819 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17820 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017821 {
17822 EMSG2(_(e_illvar), varname);
17823 return;
17824 }
17825
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017826 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17827 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017828 if (v == NULL)
17829 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017830 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017831 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017833 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 return;
17835 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017836 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017840 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017841 else
17842 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017844 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017845 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847}
17848
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017849/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017850 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 * Also give an error message.
17852 */
17853 static int
17854var_check_ro(flags, name)
17855 int flags;
17856 char_u *name;
17857{
17858 if (flags & DI_FLAGS_RO)
17859 {
17860 EMSG2(_(e_readonlyvar), name);
17861 return TRUE;
17862 }
17863 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17864 {
17865 EMSG2(_(e_readonlysbx), name);
17866 return TRUE;
17867 }
17868 return FALSE;
17869}
17870
17871/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017872 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17873 * Also give an error message.
17874 */
17875 static int
17876var_check_fixed(flags, name)
17877 int flags;
17878 char_u *name;
17879{
17880 if (flags & DI_FLAGS_FIX)
17881 {
17882 EMSG2(_("E795: Cannot delete variable %s"), name);
17883 return TRUE;
17884 }
17885 return FALSE;
17886}
17887
17888/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017889 * Return TRUE if typeval "tv" is set to be locked (immutable).
17890 * Also give an error message, using "name".
17891 */
17892 static int
17893tv_check_lock(lock, name)
17894 int lock;
17895 char_u *name;
17896{
17897 if (lock & VAR_LOCKED)
17898 {
17899 EMSG2(_("E741: Value is locked: %s"),
17900 name == NULL ? (char_u *)_("Unknown") : name);
17901 return TRUE;
17902 }
17903 if (lock & VAR_FIXED)
17904 {
17905 EMSG2(_("E742: Cannot change value of %s"),
17906 name == NULL ? (char_u *)_("Unknown") : name);
17907 return TRUE;
17908 }
17909 return FALSE;
17910}
17911
17912/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017913 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017914 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017915 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 typval_T *from;
17920 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017922 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017923 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017924 switch (from->v_type)
17925 {
17926 case VAR_NUMBER:
17927 to->vval.v_number = from->vval.v_number;
17928 break;
17929 case VAR_STRING:
17930 case VAR_FUNC:
17931 if (from->vval.v_string == NULL)
17932 to->vval.v_string = NULL;
17933 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017934 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017935 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017936 if (from->v_type == VAR_FUNC)
17937 func_ref(to->vval.v_string);
17938 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017939 break;
17940 case VAR_LIST:
17941 if (from->vval.v_list == NULL)
17942 to->vval.v_list = NULL;
17943 else
17944 {
17945 to->vval.v_list = from->vval.v_list;
17946 ++to->vval.v_list->lv_refcount;
17947 }
17948 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017949 case VAR_DICT:
17950 if (from->vval.v_dict == NULL)
17951 to->vval.v_dict = NULL;
17952 else
17953 {
17954 to->vval.v_dict = from->vval.v_dict;
17955 ++to->vval.v_dict->dv_refcount;
17956 }
17957 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017958 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017959 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017960 break;
17961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962}
17963
17964/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017965 * Make a copy of an item.
17966 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017967 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17968 * reference to an already copied list/dict can be used.
17969 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017970 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017971 static int
17972item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017973 typval_T *from;
17974 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017975 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017976 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017977{
17978 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017979 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017980
Bram Moolenaar33570922005-01-25 22:26:29 +000017981 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017982 {
17983 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017984 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017985 }
17986 ++recurse;
17987
17988 switch (from->v_type)
17989 {
17990 case VAR_NUMBER:
17991 case VAR_STRING:
17992 case VAR_FUNC:
17993 copy_tv(from, to);
17994 break;
17995 case VAR_LIST:
17996 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017997 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017998 if (from->vval.v_list == NULL)
17999 to->vval.v_list = NULL;
18000 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18001 {
18002 /* use the copy made earlier */
18003 to->vval.v_list = from->vval.v_list->lv_copylist;
18004 ++to->vval.v_list->lv_refcount;
18005 }
18006 else
18007 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18008 if (to->vval.v_list == NULL)
18009 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018010 break;
18011 case VAR_DICT:
18012 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018013 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018014 if (from->vval.v_dict == NULL)
18015 to->vval.v_dict = NULL;
18016 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18017 {
18018 /* use the copy made earlier */
18019 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18020 ++to->vval.v_dict->dv_refcount;
18021 }
18022 else
18023 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18024 if (to->vval.v_dict == NULL)
18025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018026 break;
18027 default:
18028 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018029 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018030 }
18031 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018032 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018033}
18034
18035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 * ":echo expr1 ..." print each argument separated with a space, add a
18037 * newline at the end.
18038 * ":echon expr1 ..." print each argument plain.
18039 */
18040 void
18041ex_echo(eap)
18042 exarg_T *eap;
18043{
18044 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018046 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 char_u *p;
18048 int needclr = TRUE;
18049 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018050 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051
18052 if (eap->skip)
18053 ++emsg_skip;
18054 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18055 {
18056 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018057 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 {
18059 /*
18060 * Report the invalid expression unless the expression evaluation
18061 * has been cancelled due to an aborting error, an interrupt, or an
18062 * exception.
18063 */
18064 if (!aborting())
18065 EMSG2(_(e_invexpr2), p);
18066 break;
18067 }
18068 if (!eap->skip)
18069 {
18070 if (atstart)
18071 {
18072 atstart = FALSE;
18073 /* Call msg_start() after eval1(), evaluating the expression
18074 * may cause a message to appear. */
18075 if (eap->cmdidx == CMD_echo)
18076 msg_start();
18077 }
18078 else if (eap->cmdidx == CMD_echo)
18079 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018080 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018081 if (p != NULL)
18082 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018084 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018086 if (*p != TAB && needclr)
18087 {
18088 /* remove any text still there from the command */
18089 msg_clr_eos();
18090 needclr = FALSE;
18091 }
18092 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 }
18094 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018095 {
18096#ifdef FEAT_MBYTE
18097 if (has_mbyte)
18098 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018099 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018100
18101 (void)msg_outtrans_len_attr(p, i, echo_attr);
18102 p += i - 1;
18103 }
18104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018106 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018109 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 arg = skipwhite(arg);
18113 }
18114 eap->nextcmd = check_nextcmd(arg);
18115
18116 if (eap->skip)
18117 --emsg_skip;
18118 else
18119 {
18120 /* remove text that may still be there from the command */
18121 if (needclr)
18122 msg_clr_eos();
18123 if (eap->cmdidx == CMD_echo)
18124 msg_end();
18125 }
18126}
18127
18128/*
18129 * ":echohl {name}".
18130 */
18131 void
18132ex_echohl(eap)
18133 exarg_T *eap;
18134{
18135 int id;
18136
18137 id = syn_name2id(eap->arg);
18138 if (id == 0)
18139 echo_attr = 0;
18140 else
18141 echo_attr = syn_id2attr(id);
18142}
18143
18144/*
18145 * ":execute expr1 ..." execute the result of an expression.
18146 * ":echomsg expr1 ..." Print a message
18147 * ":echoerr expr1 ..." Print an error
18148 * Each gets spaces around each argument and a newline at the end for
18149 * echo commands
18150 */
18151 void
18152ex_execute(eap)
18153 exarg_T *eap;
18154{
18155 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018156 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 int ret = OK;
18158 char_u *p;
18159 garray_T ga;
18160 int len;
18161 int save_did_emsg;
18162
18163 ga_init2(&ga, 1, 80);
18164
18165 if (eap->skip)
18166 ++emsg_skip;
18167 while (*arg != NUL && *arg != '|' && *arg != '\n')
18168 {
18169 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 {
18172 /*
18173 * Report the invalid expression unless the expression evaluation
18174 * has been cancelled due to an aborting error, an interrupt, or an
18175 * exception.
18176 */
18177 if (!aborting())
18178 EMSG2(_(e_invexpr2), p);
18179 ret = FAIL;
18180 break;
18181 }
18182
18183 if (!eap->skip)
18184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018185 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 len = (int)STRLEN(p);
18187 if (ga_grow(&ga, len + 2) == FAIL)
18188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018189 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 ret = FAIL;
18191 break;
18192 }
18193 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 ga.ga_len += len;
18197 }
18198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018199 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 arg = skipwhite(arg);
18201 }
18202
18203 if (ret != FAIL && ga.ga_data != NULL)
18204 {
18205 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018208 out_flush();
18209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 else if (eap->cmdidx == CMD_echoerr)
18211 {
18212 /* We don't want to abort following commands, restore did_emsg. */
18213 save_did_emsg = did_emsg;
18214 EMSG((char_u *)ga.ga_data);
18215 if (!force_abort)
18216 did_emsg = save_did_emsg;
18217 }
18218 else if (eap->cmdidx == CMD_execute)
18219 do_cmdline((char_u *)ga.ga_data,
18220 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18221 }
18222
18223 ga_clear(&ga);
18224
18225 if (eap->skip)
18226 --emsg_skip;
18227
18228 eap->nextcmd = check_nextcmd(arg);
18229}
18230
18231/*
18232 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18233 * "arg" points to the "&" or '+' when called, to "option" when returning.
18234 * Returns NULL when no option name found. Otherwise pointer to the char
18235 * after the option name.
18236 */
18237 static char_u *
18238find_option_end(arg, opt_flags)
18239 char_u **arg;
18240 int *opt_flags;
18241{
18242 char_u *p = *arg;
18243
18244 ++p;
18245 if (*p == 'g' && p[1] == ':')
18246 {
18247 *opt_flags = OPT_GLOBAL;
18248 p += 2;
18249 }
18250 else if (*p == 'l' && p[1] == ':')
18251 {
18252 *opt_flags = OPT_LOCAL;
18253 p += 2;
18254 }
18255 else
18256 *opt_flags = 0;
18257
18258 if (!ASCII_ISALPHA(*p))
18259 return NULL;
18260 *arg = p;
18261
18262 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18263 p += 4; /* termcap option */
18264 else
18265 while (ASCII_ISALPHA(*p))
18266 ++p;
18267 return p;
18268}
18269
18270/*
18271 * ":function"
18272 */
18273 void
18274ex_function(eap)
18275 exarg_T *eap;
18276{
18277 char_u *theline;
18278 int j;
18279 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 char_u *name = NULL;
18282 char_u *p;
18283 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018284 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 garray_T newargs;
18286 garray_T newlines;
18287 int varargs = FALSE;
18288 int mustend = FALSE;
18289 int flags = 0;
18290 ufunc_T *fp;
18291 int indent;
18292 int nesting;
18293 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018294 dictitem_T *v;
18295 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018296 static int func_nr = 0; /* number for nameless function */
18297 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018298 hashtab_T *ht;
18299 int todo;
18300 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018301 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302
18303 /*
18304 * ":function" without argument: list functions.
18305 */
18306 if (ends_excmd(*eap->arg))
18307 {
18308 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018309 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018310 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018311 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018312 {
18313 if (!HASHITEM_EMPTY(hi))
18314 {
18315 --todo;
18316 fp = HI2UF(hi);
18317 if (!isdigit(*fp->uf_name))
18318 list_func_head(fp, FALSE);
18319 }
18320 }
18321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 eap->nextcmd = check_nextcmd(eap->arg);
18323 return;
18324 }
18325
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018326 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018327 * ":function /pat": list functions matching pattern.
18328 */
18329 if (*eap->arg == '/')
18330 {
18331 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18332 if (!eap->skip)
18333 {
18334 regmatch_T regmatch;
18335
18336 c = *p;
18337 *p = NUL;
18338 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18339 *p = c;
18340 if (regmatch.regprog != NULL)
18341 {
18342 regmatch.rm_ic = p_ic;
18343
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018344 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018345 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18346 {
18347 if (!HASHITEM_EMPTY(hi))
18348 {
18349 --todo;
18350 fp = HI2UF(hi);
18351 if (!isdigit(*fp->uf_name)
18352 && vim_regexec(&regmatch, fp->uf_name, 0))
18353 list_func_head(fp, FALSE);
18354 }
18355 }
18356 }
18357 }
18358 if (*p == '/')
18359 ++p;
18360 eap->nextcmd = check_nextcmd(p);
18361 return;
18362 }
18363
18364 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018365 * Get the function name. There are these situations:
18366 * func normal function name
18367 * "name" == func, "fudi.fd_dict" == NULL
18368 * dict.func new dictionary entry
18369 * "name" == NULL, "fudi.fd_dict" set,
18370 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18371 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018372 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018373 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18374 * dict.func existing dict entry that's not a Funcref
18375 * "name" == NULL, "fudi.fd_dict" set,
18376 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018379 name = trans_function_name(&p, eap->skip, 0, &fudi);
18380 paren = (vim_strchr(p, '(') != NULL);
18381 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 {
18383 /*
18384 * Return on an invalid expression in braces, unless the expression
18385 * evaluation has been cancelled due to an aborting error, an
18386 * interrupt, or an exception.
18387 */
18388 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018390 if (!eap->skip && fudi.fd_newkey != NULL)
18391 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018392 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 else
18396 eap->skip = TRUE;
18397 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018398
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 /* An error in a function call during evaluation of an expression in magic
18400 * braces should not cause the function not to be defined. */
18401 saved_did_emsg = did_emsg;
18402 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403
18404 /*
18405 * ":function func" with only function name: list function.
18406 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018407 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 {
18409 if (!ends_excmd(*skipwhite(p)))
18410 {
18411 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018412 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 }
18414 eap->nextcmd = check_nextcmd(p);
18415 if (eap->nextcmd != NULL)
18416 *p = NUL;
18417 if (!eap->skip && !got_int)
18418 {
18419 fp = find_func(name);
18420 if (fp != NULL)
18421 {
18422 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018423 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018425 if (FUNCLINE(fp, j) == NULL)
18426 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 msg_putchar('\n');
18428 msg_outnum((long)(j + 1));
18429 if (j < 9)
18430 msg_putchar(' ');
18431 if (j < 99)
18432 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018433 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 out_flush(); /* show a line at a time */
18435 ui_breakcheck();
18436 }
18437 if (!got_int)
18438 {
18439 msg_putchar('\n');
18440 msg_puts((char_u *)" endfunction");
18441 }
18442 }
18443 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018444 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018446 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 }
18448
18449 /*
18450 * ":function name(arg1, arg2)" Define function.
18451 */
18452 p = skipwhite(p);
18453 if (*p != '(')
18454 {
18455 if (!eap->skip)
18456 {
18457 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018458 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 }
18460 /* attempt to continue by skipping some text */
18461 if (vim_strchr(p, '(') != NULL)
18462 p = vim_strchr(p, '(');
18463 }
18464 p = skipwhite(p + 1);
18465
18466 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18467 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18468
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018469 if (!eap->skip)
18470 {
18471 /* Check the name of the function. */
18472 if (name != NULL)
18473 arg = name;
18474 else
18475 arg = fudi.fd_newkey;
18476 if (arg != NULL)
18477 {
18478 if (*arg == K_SPECIAL)
18479 j = 3;
18480 else
18481 j = 0;
18482 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18483 : eval_isnamec(arg[j])))
18484 ++j;
18485 if (arg[j] != NUL)
18486 emsg_funcname(_(e_invarg2), arg);
18487 }
18488 }
18489
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 /*
18491 * Isolate the arguments: "arg1, arg2, ...)"
18492 */
18493 while (*p != ')')
18494 {
18495 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18496 {
18497 varargs = TRUE;
18498 p += 3;
18499 mustend = TRUE;
18500 }
18501 else
18502 {
18503 arg = p;
18504 while (ASCII_ISALNUM(*p) || *p == '_')
18505 ++p;
18506 if (arg == p || isdigit(*arg)
18507 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18508 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18509 {
18510 if (!eap->skip)
18511 EMSG2(_("E125: Illegal argument: %s"), arg);
18512 break;
18513 }
18514 if (ga_grow(&newargs, 1) == FAIL)
18515 goto erret;
18516 c = *p;
18517 *p = NUL;
18518 arg = vim_strsave(arg);
18519 if (arg == NULL)
18520 goto erret;
18521 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18522 *p = c;
18523 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 if (*p == ',')
18525 ++p;
18526 else
18527 mustend = TRUE;
18528 }
18529 p = skipwhite(p);
18530 if (mustend && *p != ')')
18531 {
18532 if (!eap->skip)
18533 EMSG2(_(e_invarg2), eap->arg);
18534 break;
18535 }
18536 }
18537 ++p; /* skip the ')' */
18538
Bram Moolenaare9a41262005-01-15 22:18:47 +000018539 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 for (;;)
18541 {
18542 p = skipwhite(p);
18543 if (STRNCMP(p, "range", 5) == 0)
18544 {
18545 flags |= FC_RANGE;
18546 p += 5;
18547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018548 else if (STRNCMP(p, "dict", 4) == 0)
18549 {
18550 flags |= FC_DICT;
18551 p += 4;
18552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 else if (STRNCMP(p, "abort", 5) == 0)
18554 {
18555 flags |= FC_ABORT;
18556 p += 5;
18557 }
18558 else
18559 break;
18560 }
18561
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018562 /* When there is a line break use what follows for the function body.
18563 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18564 if (*p == '\n')
18565 line_arg = p + 1;
18566 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 EMSG(_(e_trailing));
18568
18569 /*
18570 * Read the body of the function, until ":endfunction" is found.
18571 */
18572 if (KeyTyped)
18573 {
18574 /* Check if the function already exists, don't let the user type the
18575 * whole function before telling him it doesn't work! For a script we
18576 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018577 if (!eap->skip && !eap->forceit)
18578 {
18579 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18580 EMSG(_(e_funcdict));
18581 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018582 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018585 if (!eap->skip && did_emsg)
18586 goto erret;
18587
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 msg_putchar('\n'); /* don't overwrite the function name */
18589 cmdline_row = msg_row;
18590 }
18591
18592 indent = 2;
18593 nesting = 0;
18594 for (;;)
18595 {
18596 msg_scroll = TRUE;
18597 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018598 sourcing_lnum_off = sourcing_lnum;
18599
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018600 if (line_arg != NULL)
18601 {
18602 /* Use eap->arg, split up in parts by line breaks. */
18603 theline = line_arg;
18604 p = vim_strchr(theline, '\n');
18605 if (p == NULL)
18606 line_arg += STRLEN(line_arg);
18607 else
18608 {
18609 *p = NUL;
18610 line_arg = p + 1;
18611 }
18612 }
18613 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 theline = getcmdline(':', 0L, indent);
18615 else
18616 theline = eap->getline(':', eap->cookie, indent);
18617 if (KeyTyped)
18618 lines_left = Rows - 1;
18619 if (theline == NULL)
18620 {
18621 EMSG(_("E126: Missing :endfunction"));
18622 goto erret;
18623 }
18624
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018625 /* Detect line continuation: sourcing_lnum increased more than one. */
18626 if (sourcing_lnum > sourcing_lnum_off + 1)
18627 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18628 else
18629 sourcing_lnum_off = 0;
18630
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 if (skip_until != NULL)
18632 {
18633 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18634 * don't check for ":endfunc". */
18635 if (STRCMP(theline, skip_until) == 0)
18636 {
18637 vim_free(skip_until);
18638 skip_until = NULL;
18639 }
18640 }
18641 else
18642 {
18643 /* skip ':' and blanks*/
18644 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18645 ;
18646
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018647 /* Check for "endfunction". */
18648 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018650 if (line_arg == NULL)
18651 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 break;
18653 }
18654
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018655 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 * at "end". */
18657 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18658 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018659 else if (STRNCMP(p, "if", 2) == 0
18660 || STRNCMP(p, "wh", 2) == 0
18661 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 || STRNCMP(p, "try", 3) == 0)
18663 indent += 2;
18664
18665 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018666 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018668 if (*p == '!')
18669 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 p += eval_fname_script(p);
18671 if (ASCII_ISALPHA(*p))
18672 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018673 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 if (*skipwhite(p) == '(')
18675 {
18676 ++nesting;
18677 indent += 2;
18678 }
18679 }
18680 }
18681
18682 /* Check for ":append" or ":insert". */
18683 p = skip_range(p, NULL);
18684 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18685 || (p[0] == 'i'
18686 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18687 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18688 skip_until = vim_strsave((char_u *)".");
18689
18690 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18691 arg = skipwhite(skiptowhite(p));
18692 if (arg[0] == '<' && arg[1] =='<'
18693 && ((p[0] == 'p' && p[1] == 'y'
18694 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18695 || (p[0] == 'p' && p[1] == 'e'
18696 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18697 || (p[0] == 't' && p[1] == 'c'
18698 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18699 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18700 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018701 || (p[0] == 'm' && p[1] == 'z'
18702 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 ))
18704 {
18705 /* ":python <<" continues until a dot, like ":append" */
18706 p = skipwhite(arg + 2);
18707 if (*p == NUL)
18708 skip_until = vim_strsave((char_u *)".");
18709 else
18710 skip_until = vim_strsave(p);
18711 }
18712 }
18713
18714 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018715 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018716 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018717 if (line_arg == NULL)
18718 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018720 }
18721
18722 /* Copy the line to newly allocated memory. get_one_sourceline()
18723 * allocates 250 bytes per line, this saves 80% on average. The cost
18724 * is an extra alloc/free. */
18725 p = vim_strsave(theline);
18726 if (p != NULL)
18727 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018728 if (line_arg == NULL)
18729 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018730 theline = p;
18731 }
18732
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018733 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18734
18735 /* Add NULL lines for continuation lines, so that the line count is
18736 * equal to the index in the growarray. */
18737 while (sourcing_lnum_off-- > 0)
18738 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018739
18740 /* Check for end of eap->arg. */
18741 if (line_arg != NULL && *line_arg == NUL)
18742 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 }
18744
18745 /* Don't define the function when skipping commands or when an error was
18746 * detected. */
18747 if (eap->skip || did_emsg)
18748 goto erret;
18749
18750 /*
18751 * If there are no errors, add the function
18752 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018753 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018754 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018755 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018756 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018757 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018758 emsg_funcname("E707: Function name conflicts with variable: %s",
18759 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 goto erret;
18761 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018762
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018763 fp = find_func(name);
18764 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018766 if (!eap->forceit)
18767 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018768 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018769 goto erret;
18770 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018771 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018772 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018773 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018774 name);
18775 goto erret;
18776 }
18777 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018778 ga_clear_strings(&(fp->uf_args));
18779 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 vim_free(name);
18781 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 }
18784 else
18785 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018786 char numbuf[20];
18787
18788 fp = NULL;
18789 if (fudi.fd_newkey == NULL && !eap->forceit)
18790 {
18791 EMSG(_(e_funcdict));
18792 goto erret;
18793 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018794 if (fudi.fd_di == NULL)
18795 {
18796 /* Can't add a function to a locked dictionary */
18797 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18798 goto erret;
18799 }
18800 /* Can't change an existing function if it is locked */
18801 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18802 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018803
18804 /* Give the function a sequential number. Can only be used with a
18805 * Funcref! */
18806 vim_free(name);
18807 sprintf(numbuf, "%d", ++func_nr);
18808 name = vim_strsave((char_u *)numbuf);
18809 if (name == NULL)
18810 goto erret;
18811 }
18812
18813 if (fp == NULL)
18814 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018815 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018816 {
18817 int slen, plen;
18818 char_u *scriptname;
18819
18820 /* Check that the autoload name matches the script name. */
18821 j = FAIL;
18822 if (sourcing_name != NULL)
18823 {
18824 scriptname = autoload_name(name);
18825 if (scriptname != NULL)
18826 {
18827 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018828 plen = (int)STRLEN(p);
18829 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018830 if (slen > plen && fnamecmp(p,
18831 sourcing_name + slen - plen) == 0)
18832 j = OK;
18833 vim_free(scriptname);
18834 }
18835 }
18836 if (j == FAIL)
18837 {
18838 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18839 goto erret;
18840 }
18841 }
18842
18843 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844 if (fp == NULL)
18845 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018846
18847 if (fudi.fd_dict != NULL)
18848 {
18849 if (fudi.fd_di == NULL)
18850 {
18851 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018852 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018853 if (fudi.fd_di == NULL)
18854 {
18855 vim_free(fp);
18856 goto erret;
18857 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018858 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18859 {
18860 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018861 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018862 goto erret;
18863 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864 }
18865 else
18866 /* overwrite existing dict entry */
18867 clear_tv(&fudi.fd_di->di_tv);
18868 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018869 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018870 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018871 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018872
18873 /* behave like "dict" was used */
18874 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018875 }
18876
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018878 STRCPY(fp->uf_name, name);
18879 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018881 fp->uf_args = newargs;
18882 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018883#ifdef FEAT_PROFILE
18884 fp->uf_tml_count = NULL;
18885 fp->uf_tml_total = NULL;
18886 fp->uf_tml_self = NULL;
18887 fp->uf_profiling = FALSE;
18888 if (prof_def_func())
18889 func_do_profile(fp);
18890#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018891 fp->uf_varargs = varargs;
18892 fp->uf_flags = flags;
18893 fp->uf_calls = 0;
18894 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018895 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896
18897erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 ga_clear_strings(&newargs);
18899 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018900ret_free:
18901 vim_free(skip_until);
18902 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905}
18906
18907/*
18908 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018909 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018911 * flags:
18912 * TFN_INT: internal function name OK
18913 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 * Advances "pp" to just after the function name (if no error).
18915 */
18916 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018917trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 char_u **pp;
18919 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018920 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018921 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018923 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 char_u *start;
18925 char_u *end;
18926 int lead;
18927 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018929 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018930
18931 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018932 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018934
18935 /* Check for hard coded <SNR>: already translated function ID (from a user
18936 * command). */
18937 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18938 && (*pp)[2] == (int)KE_SNR)
18939 {
18940 *pp += 3;
18941 len = get_id_len(pp) + 3;
18942 return vim_strnsave(start, len);
18943 }
18944
18945 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18946 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018948 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018950
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018951 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18952 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018953 if (end == start)
18954 {
18955 if (!skip)
18956 EMSG(_("E129: Function name required"));
18957 goto theend;
18958 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018959 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018960 {
18961 /*
18962 * Report an invalid expression in braces, unless the expression
18963 * evaluation has been cancelled due to an aborting error, an
18964 * interrupt, or an exception.
18965 */
18966 if (!aborting())
18967 {
18968 if (end != NULL)
18969 EMSG2(_(e_invarg2), start);
18970 }
18971 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018972 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018973 goto theend;
18974 }
18975
18976 if (lv.ll_tv != NULL)
18977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018978 if (fdp != NULL)
18979 {
18980 fdp->fd_dict = lv.ll_dict;
18981 fdp->fd_newkey = lv.ll_newkey;
18982 lv.ll_newkey = NULL;
18983 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018984 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018985 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18986 {
18987 name = vim_strsave(lv.ll_tv->vval.v_string);
18988 *pp = end;
18989 }
18990 else
18991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018992 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18993 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018994 EMSG(_(e_funcref));
18995 else
18996 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018997 name = NULL;
18998 }
18999 goto theend;
19000 }
19001
19002 if (lv.ll_name == NULL)
19003 {
19004 /* Error found, but continue after the function name. */
19005 *pp = end;
19006 goto theend;
19007 }
19008
19009 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019010 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019011 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019012 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19013 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19014 {
19015 /* When there was "s:" already or the name expanded to get a
19016 * leading "s:" then remove it. */
19017 lv.ll_name += 2;
19018 len -= 2;
19019 lead = 2;
19020 }
19021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019022 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019023 {
19024 if (lead == 2) /* skip over "s:" */
19025 lv.ll_name += 2;
19026 len = (int)(end - lv.ll_name);
19027 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019028
19029 /*
19030 * Copy the function name to allocated memory.
19031 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19032 * Accept <SNR>123_name() outside a script.
19033 */
19034 if (skip)
19035 lead = 0; /* do nothing */
19036 else if (lead > 0)
19037 {
19038 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019039 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19040 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019041 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019042 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019043 if (current_SID <= 0)
19044 {
19045 EMSG(_(e_usingsid));
19046 goto theend;
19047 }
19048 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19049 lead += (int)STRLEN(sid_buf);
19050 }
19051 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019052 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019053 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019054 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019055 goto theend;
19056 }
19057 name = alloc((unsigned)(len + lead + 1));
19058 if (name != NULL)
19059 {
19060 if (lead > 0)
19061 {
19062 name[0] = K_SPECIAL;
19063 name[1] = KS_EXTRA;
19064 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019065 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019066 STRCPY(name + 3, sid_buf);
19067 }
19068 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19069 name[len + lead] = NUL;
19070 }
19071 *pp = end;
19072
19073theend:
19074 clear_lval(&lv);
19075 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076}
19077
19078/*
19079 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19080 * Return 2 if "p" starts with "s:".
19081 * Return 0 otherwise.
19082 */
19083 static int
19084eval_fname_script(p)
19085 char_u *p;
19086{
19087 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19088 || STRNICMP(p + 1, "SNR>", 4) == 0))
19089 return 5;
19090 if (p[0] == 's' && p[1] == ':')
19091 return 2;
19092 return 0;
19093}
19094
19095/*
19096 * Return TRUE if "p" starts with "<SID>" or "s:".
19097 * Only works if eval_fname_script() returned non-zero for "p"!
19098 */
19099 static int
19100eval_fname_sid(p)
19101 char_u *p;
19102{
19103 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19104}
19105
19106/*
19107 * List the head of the function: "name(arg1, arg2)".
19108 */
19109 static void
19110list_func_head(fp, indent)
19111 ufunc_T *fp;
19112 int indent;
19113{
19114 int j;
19115
19116 msg_start();
19117 if (indent)
19118 MSG_PUTS(" ");
19119 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019120 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 {
19122 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019123 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 }
19125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019126 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019128 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 {
19130 if (j)
19131 MSG_PUTS(", ");
19132 msg_puts(FUNCARG(fp, j));
19133 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019134 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 {
19136 if (j)
19137 MSG_PUTS(", ");
19138 MSG_PUTS("...");
19139 }
19140 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019141 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019142 if (p_verbose > 0)
19143 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144}
19145
19146/*
19147 * Find a function by name, return pointer to it in ufuncs.
19148 * Return NULL for unknown function.
19149 */
19150 static ufunc_T *
19151find_func(name)
19152 char_u *name;
19153{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019154 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019156 hi = hash_find(&func_hashtab, name);
19157 if (!HASHITEM_EMPTY(hi))
19158 return HI2UF(hi);
19159 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160}
19161
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019162#if defined(EXITFREE) || defined(PROTO)
19163 void
19164free_all_functions()
19165{
19166 hashitem_T *hi;
19167
19168 /* Need to start all over every time, because func_free() may change the
19169 * hash table. */
19170 while (func_hashtab.ht_used > 0)
19171 for (hi = func_hashtab.ht_array; ; ++hi)
19172 if (!HASHITEM_EMPTY(hi))
19173 {
19174 func_free(HI2UF(hi));
19175 break;
19176 }
19177}
19178#endif
19179
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019180/*
19181 * Return TRUE if a function "name" exists.
19182 */
19183 static int
19184function_exists(name)
19185 char_u *name;
19186{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019187 char_u *nm = name;
19188 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189 int n = FALSE;
19190
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019191 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019192 nm = skipwhite(nm);
19193
19194 /* Only accept "funcname", "funcname ", "funcname (..." and
19195 * "funcname(...", not "funcname!...". */
19196 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019197 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019198 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019200 else
19201 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019203 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019204 return n;
19205}
19206
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019207/*
19208 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019209 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019210 */
19211 static int
19212builtin_function(name)
19213 char_u *name;
19214{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019215 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19216 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019217}
19218
Bram Moolenaar05159a02005-02-26 23:04:13 +000019219#if defined(FEAT_PROFILE) || defined(PROTO)
19220/*
19221 * Start profiling function "fp".
19222 */
19223 static void
19224func_do_profile(fp)
19225 ufunc_T *fp;
19226{
19227 fp->uf_tm_count = 0;
19228 profile_zero(&fp->uf_tm_self);
19229 profile_zero(&fp->uf_tm_total);
19230 if (fp->uf_tml_count == NULL)
19231 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19232 (sizeof(int) * fp->uf_lines.ga_len));
19233 if (fp->uf_tml_total == NULL)
19234 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19235 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19236 if (fp->uf_tml_self == NULL)
19237 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19238 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19239 fp->uf_tml_idx = -1;
19240 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19241 || fp->uf_tml_self == NULL)
19242 return; /* out of memory */
19243
19244 fp->uf_profiling = TRUE;
19245}
19246
19247/*
19248 * Dump the profiling results for all functions in file "fd".
19249 */
19250 void
19251func_dump_profile(fd)
19252 FILE *fd;
19253{
19254 hashitem_T *hi;
19255 int todo;
19256 ufunc_T *fp;
19257 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019258 ufunc_T **sorttab;
19259 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019260
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019261 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019262 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19263
Bram Moolenaar05159a02005-02-26 23:04:13 +000019264 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19265 {
19266 if (!HASHITEM_EMPTY(hi))
19267 {
19268 --todo;
19269 fp = HI2UF(hi);
19270 if (fp->uf_profiling)
19271 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019272 if (sorttab != NULL)
19273 sorttab[st_len++] = fp;
19274
Bram Moolenaar05159a02005-02-26 23:04:13 +000019275 if (fp->uf_name[0] == K_SPECIAL)
19276 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19277 else
19278 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19279 if (fp->uf_tm_count == 1)
19280 fprintf(fd, "Called 1 time\n");
19281 else
19282 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19283 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19284 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19285 fprintf(fd, "\n");
19286 fprintf(fd, "count total (s) self (s)\n");
19287
19288 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19289 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019290 if (FUNCLINE(fp, i) == NULL)
19291 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019292 prof_func_line(fd, fp->uf_tml_count[i],
19293 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019294 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19295 }
19296 fprintf(fd, "\n");
19297 }
19298 }
19299 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019300
19301 if (sorttab != NULL && st_len > 0)
19302 {
19303 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19304 prof_total_cmp);
19305 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19306 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19307 prof_self_cmp);
19308 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19309 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019310}
Bram Moolenaar73830342005-02-28 22:48:19 +000019311
19312 static void
19313prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19314 FILE *fd;
19315 ufunc_T **sorttab;
19316 int st_len;
19317 char *title;
19318 int prefer_self; /* when equal print only self time */
19319{
19320 int i;
19321 ufunc_T *fp;
19322
19323 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19324 fprintf(fd, "count total (s) self (s) function\n");
19325 for (i = 0; i < 20 && i < st_len; ++i)
19326 {
19327 fp = sorttab[i];
19328 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19329 prefer_self);
19330 if (fp->uf_name[0] == K_SPECIAL)
19331 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19332 else
19333 fprintf(fd, " %s()\n", fp->uf_name);
19334 }
19335 fprintf(fd, "\n");
19336}
19337
19338/*
19339 * Print the count and times for one function or function line.
19340 */
19341 static void
19342prof_func_line(fd, count, total, self, prefer_self)
19343 FILE *fd;
19344 int count;
19345 proftime_T *total;
19346 proftime_T *self;
19347 int prefer_self; /* when equal print only self time */
19348{
19349 if (count > 0)
19350 {
19351 fprintf(fd, "%5d ", count);
19352 if (prefer_self && profile_equal(total, self))
19353 fprintf(fd, " ");
19354 else
19355 fprintf(fd, "%s ", profile_msg(total));
19356 if (!prefer_self && profile_equal(total, self))
19357 fprintf(fd, " ");
19358 else
19359 fprintf(fd, "%s ", profile_msg(self));
19360 }
19361 else
19362 fprintf(fd, " ");
19363}
19364
19365/*
19366 * Compare function for total time sorting.
19367 */
19368 static int
19369#ifdef __BORLANDC__
19370_RTLENTRYF
19371#endif
19372prof_total_cmp(s1, s2)
19373 const void *s1;
19374 const void *s2;
19375{
19376 ufunc_T *p1, *p2;
19377
19378 p1 = *(ufunc_T **)s1;
19379 p2 = *(ufunc_T **)s2;
19380 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19381}
19382
19383/*
19384 * Compare function for self time sorting.
19385 */
19386 static int
19387#ifdef __BORLANDC__
19388_RTLENTRYF
19389#endif
19390prof_self_cmp(s1, s2)
19391 const void *s1;
19392 const void *s2;
19393{
19394 ufunc_T *p1, *p2;
19395
19396 p1 = *(ufunc_T **)s1;
19397 p2 = *(ufunc_T **)s2;
19398 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19399}
19400
Bram Moolenaar05159a02005-02-26 23:04:13 +000019401#endif
19402
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019403/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019404 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019405 * Return TRUE if a package was loaded.
19406 */
19407 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019408script_autoload(name, reload)
19409 char_u *name;
19410 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019411{
19412 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019413 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019414 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019415 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019416
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019417 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019418 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019419 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019420 return FALSE;
19421
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019422 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019423
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019424 /* Find the name in the list of previously loaded package names. Skip
19425 * "autoload/", it's always the same. */
19426 for (i = 0; i < ga_loaded.ga_len; ++i)
19427 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19428 break;
19429 if (!reload && i < ga_loaded.ga_len)
19430 ret = FALSE; /* was loaded already */
19431 else
19432 {
19433 /* Remember the name if it wasn't loaded already. */
19434 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19435 {
19436 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19437 tofree = NULL;
19438 }
19439
19440 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019441 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019442 ret = TRUE;
19443 }
19444
19445 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019446 return ret;
19447}
19448
19449/*
19450 * Return the autoload script name for a function or variable name.
19451 * Returns NULL when out of memory.
19452 */
19453 static char_u *
19454autoload_name(name)
19455 char_u *name;
19456{
19457 char_u *p;
19458 char_u *scriptname;
19459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019460 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019461 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19462 if (scriptname == NULL)
19463 return FALSE;
19464 STRCPY(scriptname, "autoload/");
19465 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019466 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019467 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019468 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019469 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019470 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019471}
19472
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19474
19475/*
19476 * Function given to ExpandGeneric() to obtain the list of user defined
19477 * function names.
19478 */
19479 char_u *
19480get_user_func_name(xp, idx)
19481 expand_T *xp;
19482 int idx;
19483{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019484 static long_u done;
19485 static hashitem_T *hi;
19486 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487
19488 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019490 done = 0;
19491 hi = func_hashtab.ht_array;
19492 }
19493 if (done < func_hashtab.ht_used)
19494 {
19495 if (done++ > 0)
19496 ++hi;
19497 while (HASHITEM_EMPTY(hi))
19498 ++hi;
19499 fp = HI2UF(hi);
19500
19501 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19502 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503
19504 cat_func_name(IObuff, fp);
19505 if (xp->xp_context != EXPAND_USER_FUNC)
19506 {
19507 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 STRCAT(IObuff, ")");
19510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 return IObuff;
19512 }
19513 return NULL;
19514}
19515
19516#endif /* FEAT_CMDL_COMPL */
19517
19518/*
19519 * Copy the function name of "fp" to buffer "buf".
19520 * "buf" must be able to hold the function name plus three bytes.
19521 * Takes care of script-local function names.
19522 */
19523 static void
19524cat_func_name(buf, fp)
19525 char_u *buf;
19526 ufunc_T *fp;
19527{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019528 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 {
19530 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019531 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 }
19533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019534 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535}
19536
19537/*
19538 * ":delfunction {name}"
19539 */
19540 void
19541ex_delfunction(eap)
19542 exarg_T *eap;
19543{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019544 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 char_u *p;
19546 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019547 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548
19549 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019550 name = trans_function_name(&p, eap->skip, 0, &fudi);
19551 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019553 {
19554 if (fudi.fd_dict != NULL && !eap->skip)
19555 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 if (!ends_excmd(*skipwhite(p)))
19559 {
19560 vim_free(name);
19561 EMSG(_(e_trailing));
19562 return;
19563 }
19564 eap->nextcmd = check_nextcmd(p);
19565 if (eap->nextcmd != NULL)
19566 *p = NUL;
19567
19568 if (!eap->skip)
19569 fp = find_func(name);
19570 vim_free(name);
19571
19572 if (!eap->skip)
19573 {
19574 if (fp == NULL)
19575 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019576 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 return;
19578 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019579 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 {
19581 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19582 return;
19583 }
19584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019585 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019587 /* Delete the dict item that refers to the function, it will
19588 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019589 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019591 else
19592 func_free(fp);
19593 }
19594}
19595
19596/*
19597 * Free a function and remove it from the list of functions.
19598 */
19599 static void
19600func_free(fp)
19601 ufunc_T *fp;
19602{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019603 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019604
19605 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019606 ga_clear_strings(&(fp->uf_args));
19607 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019608#ifdef FEAT_PROFILE
19609 vim_free(fp->uf_tml_count);
19610 vim_free(fp->uf_tml_total);
19611 vim_free(fp->uf_tml_self);
19612#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019613
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019614 /* remove the function from the function hashtable */
19615 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19616 if (HASHITEM_EMPTY(hi))
19617 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019618 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019619 hash_remove(&func_hashtab, hi);
19620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019621 vim_free(fp);
19622}
19623
19624/*
19625 * Unreference a Function: decrement the reference count and free it when it
19626 * becomes zero. Only for numbered functions.
19627 */
19628 static void
19629func_unref(name)
19630 char_u *name;
19631{
19632 ufunc_T *fp;
19633
19634 if (name != NULL && isdigit(*name))
19635 {
19636 fp = find_func(name);
19637 if (fp == NULL)
19638 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019639 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019640 {
19641 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019642 * when "uf_calls" becomes zero. */
19643 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019644 func_free(fp);
19645 }
19646 }
19647}
19648
19649/*
19650 * Count a reference to a Function.
19651 */
19652 static void
19653func_ref(name)
19654 char_u *name;
19655{
19656 ufunc_T *fp;
19657
19658 if (name != NULL && isdigit(*name))
19659 {
19660 fp = find_func(name);
19661 if (fp == NULL)
19662 EMSG2(_(e_intern2), "func_ref()");
19663 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019664 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 }
19666}
19667
19668/*
19669 * Call a user function.
19670 */
19671 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019672call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 ufunc_T *fp; /* pointer to function */
19674 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 typval_T *argvars; /* arguments */
19676 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 linenr_T firstline; /* first line of range */
19678 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680{
Bram Moolenaar33570922005-01-25 22:26:29 +000019681 char_u *save_sourcing_name;
19682 linenr_T save_sourcing_lnum;
19683 scid_T save_current_SID;
19684 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 int save_did_emsg;
19686 static int depth = 0;
19687 dictitem_T *v;
19688 int fixvar_idx = 0; /* index in fixvar[] */
19689 int i;
19690 int ai;
19691 char_u numbuf[NUMBUFLEN];
19692 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019693#ifdef FEAT_PROFILE
19694 proftime_T wait_start;
19695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696
19697 /* If depth of calling is getting too high, don't execute the function */
19698 if (depth >= p_mfd)
19699 {
19700 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019701 rettv->v_type = VAR_NUMBER;
19702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 return;
19704 }
19705 ++depth;
19706
19707 line_breakcheck(); /* check for CTRL-C hit */
19708
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019709 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019710 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712 fc.rettv = rettv;
19713 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 fc.linenr = 0;
19715 fc.returned = FALSE;
19716 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019718 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 fc.dbg_tick = debug_tick;
19720
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 /*
19722 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19723 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19724 * each argument variable and saves a lot of time.
19725 */
19726 /*
19727 * Init l: variables.
19728 */
19729 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019730 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019731 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019732 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19733 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019735 name = v->di_key;
19736 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019737 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19738 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19739 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019740 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019741 v->di_tv.vval.v_dict = selfdict;
19742 ++selfdict->dv_refcount;
19743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019744
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 /*
19746 * Init a: variables.
19747 * Set a:0 to "argcount".
19748 * Set a:000 to a list with room for the "..." arguments.
19749 */
19750 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19751 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019752 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 v = &fc.fixvar[fixvar_idx++].var;
19754 STRCPY(v->di_key, "000");
19755 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19756 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19757 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019758 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019759 v->di_tv.vval.v_list = &fc.l_varlist;
19760 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19761 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019762 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019763
19764 /*
19765 * Set a:firstline to "firstline" and a:lastline to "lastline".
19766 * Set a:name to named arguments.
19767 * Set a:N to the "..." arguments.
19768 */
19769 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19770 (varnumber_T)firstline);
19771 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19772 (varnumber_T)lastline);
19773 for (i = 0; i < argcount; ++i)
19774 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019775 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019776 if (ai < 0)
19777 /* named argument a:name */
19778 name = FUNCARG(fp, i);
19779 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019780 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019781 /* "..." argument a:1, a:2, etc. */
19782 sprintf((char *)numbuf, "%d", ai + 1);
19783 name = numbuf;
19784 }
19785 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19786 {
19787 v = &fc.fixvar[fixvar_idx++].var;
19788 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19789 }
19790 else
19791 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019792 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19793 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 if (v == NULL)
19795 break;
19796 v->di_flags = DI_FLAGS_RO;
19797 }
19798 STRCPY(v->di_key, name);
19799 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19800
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019801 /* Note: the values are copied directly to avoid alloc/free.
19802 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019803 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019804 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019805
19806 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19807 {
19808 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19809 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019810 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019811 }
19812 }
19813
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 /* Don't redraw while executing the function. */
19815 ++RedrawingDisabled;
19816 save_sourcing_name = sourcing_name;
19817 save_sourcing_lnum = sourcing_lnum;
19818 sourcing_lnum = 1;
19819 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019820 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 if (sourcing_name != NULL)
19822 {
19823 if (save_sourcing_name != NULL
19824 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19825 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19826 else
19827 STRCPY(sourcing_name, "function ");
19828 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19829
19830 if (p_verbose >= 12)
19831 {
19832 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019833 verbose_enter_scroll();
19834
Bram Moolenaar555b2802005-05-19 21:08:39 +000019835 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 if (p_verbose >= 14)
19837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019839 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019840 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841
19842 msg_puts((char_u *)"(");
19843 for (i = 0; i < argcount; ++i)
19844 {
19845 if (i > 0)
19846 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019847 if (argvars[i].v_type == VAR_NUMBER)
19848 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 else
19850 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019851 trunc_string(tv2string(&argvars[i], &tofree,
19852 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019854 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 }
19856 }
19857 msg_puts((char_u *)")");
19858 }
19859 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019860
19861 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 --no_wait_return;
19863 }
19864 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019865#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019866 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019867 {
19868 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19869 func_do_profile(fp);
19870 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019871 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019872 {
19873 ++fp->uf_tm_count;
19874 profile_start(&fp->uf_tm_start);
19875 profile_zero(&fp->uf_tm_children);
19876 }
19877 script_prof_save(&wait_start);
19878 }
19879#endif
19880
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019882 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 save_did_emsg = did_emsg;
19884 did_emsg = FALSE;
19885
19886 /* call do_cmdline() to execute the lines */
19887 do_cmdline(NULL, get_func_line, (void *)&fc,
19888 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19889
19890 --RedrawingDisabled;
19891
19892 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019893 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895 clear_tv(rettv);
19896 rettv->v_type = VAR_NUMBER;
19897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 }
19899
Bram Moolenaar05159a02005-02-26 23:04:13 +000019900#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019901 if (do_profiling == PROF_YES && (fp->uf_profiling
19902 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019903 {
19904 profile_end(&fp->uf_tm_start);
19905 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19906 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019907 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019908 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019909 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019910 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19911 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019912 }
19913 }
19914#endif
19915
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 /* when being verbose, mention the return value */
19917 if (p_verbose >= 12)
19918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019920 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019923 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019925 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19926 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019929 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019930 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019931 char_u *tofree;
19932
Bram Moolenaar555b2802005-05-19 21:08:39 +000019933 /* The value may be very long. Skip the middle part, so that we
19934 * have some idea how it starts and ends. smsg() would always
19935 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000019936 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019937 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019938 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019939 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 }
19941 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019942
19943 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 --no_wait_return;
19945 }
19946
19947 vim_free(sourcing_name);
19948 sourcing_name = save_sourcing_name;
19949 sourcing_lnum = save_sourcing_lnum;
19950 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019951#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019952 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019953 script_prof_restore(&wait_start);
19954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955
19956 if (p_verbose >= 12 && sourcing_name != NULL)
19957 {
19958 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019959 verbose_enter_scroll();
19960
Bram Moolenaar555b2802005-05-19 21:08:39 +000019961 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019963
19964 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 --no_wait_return;
19966 }
19967
19968 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019969 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 /* The a: variables typevals were not alloced, only free the allocated
19972 * variables. */
19973 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19974
19975 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 --depth;
19977}
19978
19979/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019980 * Add a number variable "name" to dict "dp" with value "nr".
19981 */
19982 static void
19983add_nr_var(dp, v, name, nr)
19984 dict_T *dp;
19985 dictitem_T *v;
19986 char *name;
19987 varnumber_T nr;
19988{
19989 STRCPY(v->di_key, name);
19990 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19991 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19992 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019993 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 v->di_tv.vval.v_number = nr;
19995}
19996
19997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 * ":return [expr]"
19999 */
20000 void
20001ex_return(eap)
20002 exarg_T *eap;
20003{
20004 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020005 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 int returning = FALSE;
20007
20008 if (current_funccal == NULL)
20009 {
20010 EMSG(_("E133: :return not inside a function"));
20011 return;
20012 }
20013
20014 if (eap->skip)
20015 ++emsg_skip;
20016
20017 eap->nextcmd = NULL;
20018 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 {
20021 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 }
20026 /* It's safer to return also on error. */
20027 else if (!eap->skip)
20028 {
20029 /*
20030 * Return unless the expression evaluation has been cancelled due to an
20031 * aborting error, an interrupt, or an exception.
20032 */
20033 if (!aborting())
20034 returning = do_return(eap, FALSE, TRUE, NULL);
20035 }
20036
20037 /* When skipping or the return gets pending, advance to the next command
20038 * in this line (!returning). Otherwise, ignore the rest of the line.
20039 * Following lines will be ignored by get_func_line(). */
20040 if (returning)
20041 eap->nextcmd = NULL;
20042 else if (eap->nextcmd == NULL) /* no argument */
20043 eap->nextcmd = check_nextcmd(arg);
20044
20045 if (eap->skip)
20046 --emsg_skip;
20047}
20048
20049/*
20050 * Return from a function. Possibly makes the return pending. Also called
20051 * for a pending return at the ":endtry" or after returning from an extra
20052 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 * FALSE when the return gets pending.
20056 */
20057 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 exarg_T *eap;
20060 int reanimate;
20061 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063{
20064 int idx;
20065 struct condstack *cstack = eap->cstack;
20066
20067 if (reanimate)
20068 /* Undo the return. */
20069 current_funccal->returned = FALSE;
20070
20071 /*
20072 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20073 * not in its finally clause (which then is to be executed next) is found.
20074 * In this case, make the ":return" pending for execution at the ":endtry".
20075 * Otherwise, return normally.
20076 */
20077 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20078 if (idx >= 0)
20079 {
20080 cstack->cs_pending[idx] = CSTP_RETURN;
20081
20082 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 /* A pending return again gets pending. "rettv" points to an
20084 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 else
20088 {
20089 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 {
20096 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 else
20100 EMSG(_(e_outofmem));
20101 }
20102 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020103 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104
20105 if (reanimate)
20106 {
20107 /* The pending return value could be overwritten by a ":return"
20108 * without argument in a finally clause; reset the default
20109 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020110 current_funccal->rettv->v_type = VAR_NUMBER;
20111 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 }
20113 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 }
20116 else
20117 {
20118 current_funccal->returned = TRUE;
20119
20120 /* If the return is carried out now, store the return value. For
20121 * a return immediately after reanimation, the value is already
20122 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020123 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020125 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020126 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020128 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 }
20130 }
20131
20132 return idx < 0;
20133}
20134
20135/*
20136 * Free the variable with a pending return value.
20137 */
20138 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139discard_pending_return(rettv)
20140 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141{
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143}
20144
20145/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 * is an allocated string. Used by report_pending() for verbose messages.
20148 */
20149 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020150get_return_cmd(rettv)
20151 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020153 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020154 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020155 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020157 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020158 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020159 if (s == NULL)
20160 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020161
20162 STRCPY(IObuff, ":return ");
20163 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20164 if (STRLEN(s) + 8 >= IOSIZE)
20165 STRCPY(IObuff + IOSIZE - 4, "...");
20166 vim_free(tofree);
20167 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168}
20169
20170/*
20171 * Get next function line.
20172 * Called by do_cmdline() to get the next line.
20173 * Returns allocated string, or NULL for end of function.
20174 */
20175/* ARGSUSED */
20176 char_u *
20177get_func_line(c, cookie, indent)
20178 int c; /* not used */
20179 void *cookie;
20180 int indent; /* not used */
20181{
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020183 ufunc_T *fp = fcp->func;
20184 char_u *retval;
20185 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186
20187 /* If breakpoints have been added/deleted need to check for it. */
20188 if (fcp->dbg_tick != debug_tick)
20189 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020190 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 sourcing_lnum);
20192 fcp->dbg_tick = debug_tick;
20193 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020194#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020195 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020196 func_line_end(cookie);
20197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198
Bram Moolenaar05159a02005-02-26 23:04:13 +000020199 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020200 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20201 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 retval = NULL;
20203 else
20204 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020205 /* Skip NULL lines (continuation lines). */
20206 while (fcp->linenr < gap->ga_len
20207 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20208 ++fcp->linenr;
20209 if (fcp->linenr >= gap->ga_len)
20210 retval = NULL;
20211 else
20212 {
20213 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20214 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020215#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020216 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020217 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020218#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 }
20221
20222 /* Did we encounter a breakpoint? */
20223 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20224 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020225 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020227 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 sourcing_lnum);
20229 fcp->dbg_tick = debug_tick;
20230 }
20231
20232 return retval;
20233}
20234
Bram Moolenaar05159a02005-02-26 23:04:13 +000020235#if defined(FEAT_PROFILE) || defined(PROTO)
20236/*
20237 * Called when starting to read a function line.
20238 * "sourcing_lnum" must be correct!
20239 * When skipping lines it may not actually be executed, but we won't find out
20240 * until later and we need to store the time now.
20241 */
20242 void
20243func_line_start(cookie)
20244 void *cookie;
20245{
20246 funccall_T *fcp = (funccall_T *)cookie;
20247 ufunc_T *fp = fcp->func;
20248
20249 if (fp->uf_profiling && sourcing_lnum >= 1
20250 && sourcing_lnum <= fp->uf_lines.ga_len)
20251 {
20252 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020253 /* Skip continuation lines. */
20254 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20255 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020256 fp->uf_tml_execed = FALSE;
20257 profile_start(&fp->uf_tml_start);
20258 profile_zero(&fp->uf_tml_children);
20259 profile_get_wait(&fp->uf_tml_wait);
20260 }
20261}
20262
20263/*
20264 * Called when actually executing a function line.
20265 */
20266 void
20267func_line_exec(cookie)
20268 void *cookie;
20269{
20270 funccall_T *fcp = (funccall_T *)cookie;
20271 ufunc_T *fp = fcp->func;
20272
20273 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20274 fp->uf_tml_execed = TRUE;
20275}
20276
20277/*
20278 * Called when done with a function line.
20279 */
20280 void
20281func_line_end(cookie)
20282 void *cookie;
20283{
20284 funccall_T *fcp = (funccall_T *)cookie;
20285 ufunc_T *fp = fcp->func;
20286
20287 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20288 {
20289 if (fp->uf_tml_execed)
20290 {
20291 ++fp->uf_tml_count[fp->uf_tml_idx];
20292 profile_end(&fp->uf_tml_start);
20293 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020294 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020295 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20296 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020297 }
20298 fp->uf_tml_idx = -1;
20299 }
20300}
20301#endif
20302
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303/*
20304 * Return TRUE if the currently active function should be ended, because a
20305 * return was encountered or an error occured. Used inside a ":while".
20306 */
20307 int
20308func_has_ended(cookie)
20309 void *cookie;
20310{
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312
20313 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20314 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020315 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 || fcp->returned);
20317}
20318
20319/*
20320 * return TRUE if cookie indicates a function which "abort"s on errors.
20321 */
20322 int
20323func_has_abort(cookie)
20324 void *cookie;
20325{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020326 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327}
20328
20329#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20330typedef enum
20331{
20332 VAR_FLAVOUR_DEFAULT,
20333 VAR_FLAVOUR_SESSION,
20334 VAR_FLAVOUR_VIMINFO
20335} var_flavour_T;
20336
20337static var_flavour_T var_flavour __ARGS((char_u *varname));
20338
20339 static var_flavour_T
20340var_flavour(varname)
20341 char_u *varname;
20342{
20343 char_u *p = varname;
20344
20345 if (ASCII_ISUPPER(*p))
20346 {
20347 while (*(++p))
20348 if (ASCII_ISLOWER(*p))
20349 return VAR_FLAVOUR_SESSION;
20350 return VAR_FLAVOUR_VIMINFO;
20351 }
20352 else
20353 return VAR_FLAVOUR_DEFAULT;
20354}
20355#endif
20356
20357#if defined(FEAT_VIMINFO) || defined(PROTO)
20358/*
20359 * Restore global vars that start with a capital from the viminfo file
20360 */
20361 int
20362read_viminfo_varlist(virp, writing)
20363 vir_T *virp;
20364 int writing;
20365{
20366 char_u *tab;
20367 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
20370 if (!writing && (find_viminfo_parameter('!') != NULL))
20371 {
20372 tab = vim_strchr(virp->vir_line + 1, '\t');
20373 if (tab != NULL)
20374 {
20375 *tab++ = '\0'; /* isolate the variable name */
20376 if (*tab == 'S') /* string var */
20377 is_string = TRUE;
20378
20379 tab = vim_strchr(tab, '\t');
20380 if (tab != NULL)
20381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 if (is_string)
20383 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020384 tv.v_type = VAR_STRING;
20385 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 }
20388 else
20389 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020390 tv.v_type = VAR_NUMBER;
20391 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020393 set_var(virp->vir_line + 1, &tv, FALSE);
20394 if (is_string)
20395 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 }
20397 }
20398 }
20399
20400 return viminfo_readline(virp);
20401}
20402
20403/*
20404 * Write global vars that start with a capital to the viminfo file
20405 */
20406 void
20407write_viminfo_varlist(fp)
20408 FILE *fp;
20409{
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 hashitem_T *hi;
20411 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020412 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020413 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020414 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020415 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020416 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417
20418 if (find_viminfo_parameter('!') == NULL)
20419 return;
20420
20421 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020422
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020423 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020426 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020428 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020429 this_var = HI2DI(hi);
20430 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020432 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020433 {
20434 case VAR_STRING: s = "STR"; break;
20435 case VAR_NUMBER: s = "NUM"; break;
20436 default: continue;
20437 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020438 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020439 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020440 if (p != NULL)
20441 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020442 vim_free(tofree);
20443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 }
20445 }
20446}
20447#endif
20448
20449#if defined(FEAT_SESSION) || defined(PROTO)
20450 int
20451store_session_globals(fd)
20452 FILE *fd;
20453{
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 hashitem_T *hi;
20455 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020456 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 char_u *p, *t;
20458
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020459 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020460 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020462 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020464 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020465 this_var = HI2DI(hi);
20466 if ((this_var->di_tv.v_type == VAR_NUMBER
20467 || this_var->di_tv.v_type == VAR_STRING)
20468 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020469 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020470 /* Escape special characters with a backslash. Turn a LF and
20471 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020473 (char_u *)"\\\"\n\r");
20474 if (p == NULL) /* out of memory */
20475 break;
20476 for (t = p; *t != NUL; ++t)
20477 if (*t == '\n')
20478 *t = 'n';
20479 else if (*t == '\r')
20480 *t = 'r';
20481 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 this_var->di_key,
20483 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20484 : ' ',
20485 p,
20486 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20487 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020488 || put_eol(fd) == FAIL)
20489 {
20490 vim_free(p);
20491 return FAIL;
20492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 vim_free(p);
20494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 }
20496 }
20497 return OK;
20498}
20499#endif
20500
Bram Moolenaar661b1822005-07-28 22:36:45 +000020501/*
20502 * Display script name where an item was last set.
20503 * Should only be invoked when 'verbose' is non-zero.
20504 */
20505 void
20506last_set_msg(scriptID)
20507 scid_T scriptID;
20508{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020509 char_u *p;
20510
Bram Moolenaar661b1822005-07-28 22:36:45 +000020511 if (scriptID != 0)
20512 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020513 p = home_replace_save(NULL, get_scriptname(scriptID));
20514 if (p != NULL)
20515 {
20516 verbose_enter();
20517 MSG_PUTS(_("\n\tLast set from "));
20518 MSG_PUTS(p);
20519 vim_free(p);
20520 verbose_leave();
20521 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020522 }
20523}
20524
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525#endif /* FEAT_EVAL */
20526
20527#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20528
20529
20530#ifdef WIN3264
20531/*
20532 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20533 */
20534static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20535static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20536static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20537
20538/*
20539 * Get the short pathname of a file.
20540 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20541 */
20542 static int
20543get_short_pathname(fnamep, bufp, fnamelen)
20544 char_u **fnamep;
20545 char_u **bufp;
20546 int *fnamelen;
20547{
20548 int l,len;
20549 char_u *newbuf;
20550
20551 len = *fnamelen;
20552
20553 l = GetShortPathName(*fnamep, *fnamep, len);
20554 if (l > len - 1)
20555 {
20556 /* If that doesn't work (not enough space), then save the string
20557 * and try again with a new buffer big enough
20558 */
20559 newbuf = vim_strnsave(*fnamep, l);
20560 if (newbuf == NULL)
20561 return 0;
20562
20563 vim_free(*bufp);
20564 *fnamep = *bufp = newbuf;
20565
20566 l = GetShortPathName(*fnamep,*fnamep,l+1);
20567
20568 /* Really should always succeed, as the buffer is big enough */
20569 }
20570
20571 *fnamelen = l;
20572 return 1;
20573}
20574
20575/*
20576 * Create a short path name. Returns the length of the buffer it needs.
20577 * Doesn't copy over the end of the buffer passed in.
20578 */
20579 static int
20580shortpath_for_invalid_fname(fname, bufp, fnamelen)
20581 char_u **fname;
20582 char_u **bufp;
20583 int *fnamelen;
20584{
20585 char_u *s, *p, *pbuf2, *pbuf3;
20586 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020587 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588
20589 /* Make a copy */
20590 len2 = *fnamelen;
20591 pbuf2 = vim_strnsave(*fname, len2);
20592 pbuf3 = NULL;
20593
20594 s = pbuf2 + len2 - 1; /* Find the end */
20595 slen = 1;
20596 plen = len2;
20597
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020598 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 {
20600 --s;
20601 ++slen;
20602 --plen;
20603 }
20604
20605 do
20606 {
20607 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020608 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 {
20610 --s;
20611 ++slen;
20612 --plen;
20613 }
20614 if (s <= pbuf2)
20615 break;
20616
20617 /* Remeber the character that is about to be blatted */
20618 ch = *s;
20619 *s = 0; /* get_short_pathname requires a null-terminated string */
20620
20621 /* Try it in situ */
20622 p = pbuf2;
20623 if (!get_short_pathname(&p, &pbuf3, &plen))
20624 {
20625 vim_free(pbuf2);
20626 return -1;
20627 }
20628 *s = ch; /* Preserve the string */
20629 } while (plen == 0);
20630
20631 if (plen > 0)
20632 {
20633 /* Remeber the length of the new string. */
20634 *fnamelen = len = plen + slen;
20635 vim_free(*bufp);
20636 if (len > len2)
20637 {
20638 /* If there's not enough space in the currently allocated string,
20639 * then copy it to a buffer big enough.
20640 */
20641 *fname= *bufp = vim_strnsave(p, len);
20642 if (*fname == NULL)
20643 return -1;
20644 }
20645 else
20646 {
20647 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20648 *fname = *bufp = pbuf2;
20649 if (p != pbuf2)
20650 strncpy(*fname, p, plen);
20651 pbuf2 = NULL;
20652 }
20653 /* Concat the next bit */
20654 strncpy(*fname + plen, s, slen);
20655 (*fname)[len] = '\0';
20656 }
20657 vim_free(pbuf3);
20658 vim_free(pbuf2);
20659 return 0;
20660}
20661
20662/*
20663 * Get a pathname for a partial path.
20664 */
20665 static int
20666shortpath_for_partial(fnamep, bufp, fnamelen)
20667 char_u **fnamep;
20668 char_u **bufp;
20669 int *fnamelen;
20670{
20671 int sepcount, len, tflen;
20672 char_u *p;
20673 char_u *pbuf, *tfname;
20674 int hasTilde;
20675
20676 /* Count up the path seperators from the RHS.. so we know which part
20677 * of the path to return.
20678 */
20679 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020680 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 if (vim_ispathsep(*p))
20682 ++sepcount;
20683
20684 /* Need full path first (use expand_env() to remove a "~/") */
20685 hasTilde = (**fnamep == '~');
20686 if (hasTilde)
20687 pbuf = tfname = expand_env_save(*fnamep);
20688 else
20689 pbuf = tfname = FullName_save(*fnamep, FALSE);
20690
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020691 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692
20693 if (!get_short_pathname(&tfname, &pbuf, &len))
20694 return -1;
20695
20696 if (len == 0)
20697 {
20698 /* Don't have a valid filename, so shorten the rest of the
20699 * path if we can. This CAN give us invalid 8.3 filenames, but
20700 * there's not a lot of point in guessing what it might be.
20701 */
20702 len = tflen;
20703 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20704 return -1;
20705 }
20706
20707 /* Count the paths backward to find the beginning of the desired string. */
20708 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020709 {
20710#ifdef FEAT_MBYTE
20711 if (has_mbyte)
20712 p -= mb_head_off(tfname, p);
20713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 if (vim_ispathsep(*p))
20715 {
20716 if (sepcount == 0 || (hasTilde && sepcount == 1))
20717 break;
20718 else
20719 sepcount --;
20720 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 if (hasTilde)
20723 {
20724 --p;
20725 if (p >= tfname)
20726 *p = '~';
20727 else
20728 return -1;
20729 }
20730 else
20731 ++p;
20732
20733 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20734 vim_free(*bufp);
20735 *fnamelen = (int)STRLEN(p);
20736 *bufp = pbuf;
20737 *fnamep = p;
20738
20739 return 0;
20740}
20741#endif /* WIN3264 */
20742
20743/*
20744 * Adjust a filename, according to a string of modifiers.
20745 * *fnamep must be NUL terminated when called. When returning, the length is
20746 * determined by *fnamelen.
20747 * Returns valid flags.
20748 * When there is an error, *fnamep is set to NULL.
20749 */
20750 int
20751modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20752 char_u *src; /* string with modifiers */
20753 int *usedlen; /* characters after src that are used */
20754 char_u **fnamep; /* file name so far */
20755 char_u **bufp; /* buffer for allocated file name or NULL */
20756 int *fnamelen; /* length of fnamep */
20757{
20758 int valid = 0;
20759 char_u *tail;
20760 char_u *s, *p, *pbuf;
20761 char_u dirname[MAXPATHL];
20762 int c;
20763 int has_fullname = 0;
20764#ifdef WIN3264
20765 int has_shortname = 0;
20766#endif
20767
20768repeat:
20769 /* ":p" - full path/file_name */
20770 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20771 {
20772 has_fullname = 1;
20773
20774 valid |= VALID_PATH;
20775 *usedlen += 2;
20776
20777 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20778 if ((*fnamep)[0] == '~'
20779#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20780 && ((*fnamep)[1] == '/'
20781# ifdef BACKSLASH_IN_FILENAME
20782 || (*fnamep)[1] == '\\'
20783# endif
20784 || (*fnamep)[1] == NUL)
20785
20786#endif
20787 )
20788 {
20789 *fnamep = expand_env_save(*fnamep);
20790 vim_free(*bufp); /* free any allocated file name */
20791 *bufp = *fnamep;
20792 if (*fnamep == NULL)
20793 return -1;
20794 }
20795
20796 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020797 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 {
20799 if (vim_ispathsep(*p)
20800 && p[1] == '.'
20801 && (p[2] == NUL
20802 || vim_ispathsep(p[2])
20803 || (p[2] == '.'
20804 && (p[3] == NUL || vim_ispathsep(p[3])))))
20805 break;
20806 }
20807
20808 /* FullName_save() is slow, don't use it when not needed. */
20809 if (*p != NUL || !vim_isAbsName(*fnamep))
20810 {
20811 *fnamep = FullName_save(*fnamep, *p != NUL);
20812 vim_free(*bufp); /* free any allocated file name */
20813 *bufp = *fnamep;
20814 if (*fnamep == NULL)
20815 return -1;
20816 }
20817
20818 /* Append a path separator to a directory. */
20819 if (mch_isdir(*fnamep))
20820 {
20821 /* Make room for one or two extra characters. */
20822 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20823 vim_free(*bufp); /* free any allocated file name */
20824 *bufp = *fnamep;
20825 if (*fnamep == NULL)
20826 return -1;
20827 add_pathsep(*fnamep);
20828 }
20829 }
20830
20831 /* ":." - path relative to the current directory */
20832 /* ":~" - path relative to the home directory */
20833 /* ":8" - shortname path - postponed till after */
20834 while (src[*usedlen] == ':'
20835 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20836 {
20837 *usedlen += 2;
20838 if (c == '8')
20839 {
20840#ifdef WIN3264
20841 has_shortname = 1; /* Postpone this. */
20842#endif
20843 continue;
20844 }
20845 pbuf = NULL;
20846 /* Need full path first (use expand_env() to remove a "~/") */
20847 if (!has_fullname)
20848 {
20849 if (c == '.' && **fnamep == '~')
20850 p = pbuf = expand_env_save(*fnamep);
20851 else
20852 p = pbuf = FullName_save(*fnamep, FALSE);
20853 }
20854 else
20855 p = *fnamep;
20856
20857 has_fullname = 0;
20858
20859 if (p != NULL)
20860 {
20861 if (c == '.')
20862 {
20863 mch_dirname(dirname, MAXPATHL);
20864 s = shorten_fname(p, dirname);
20865 if (s != NULL)
20866 {
20867 *fnamep = s;
20868 if (pbuf != NULL)
20869 {
20870 vim_free(*bufp); /* free any allocated file name */
20871 *bufp = pbuf;
20872 pbuf = NULL;
20873 }
20874 }
20875 }
20876 else
20877 {
20878 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20879 /* Only replace it when it starts with '~' */
20880 if (*dirname == '~')
20881 {
20882 s = vim_strsave(dirname);
20883 if (s != NULL)
20884 {
20885 *fnamep = s;
20886 vim_free(*bufp);
20887 *bufp = s;
20888 }
20889 }
20890 }
20891 vim_free(pbuf);
20892 }
20893 }
20894
20895 tail = gettail(*fnamep);
20896 *fnamelen = (int)STRLEN(*fnamep);
20897
20898 /* ":h" - head, remove "/file_name", can be repeated */
20899 /* Don't remove the first "/" or "c:\" */
20900 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20901 {
20902 valid |= VALID_HEAD;
20903 *usedlen += 2;
20904 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020905 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 --tail;
20907 *fnamelen = (int)(tail - *fnamep);
20908#ifdef VMS
20909 if (*fnamelen > 0)
20910 *fnamelen += 1; /* the path separator is part of the path */
20911#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020912 while (tail > s && !after_pathsep(s, tail))
20913 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 }
20915
20916 /* ":8" - shortname */
20917 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20918 {
20919 *usedlen += 2;
20920#ifdef WIN3264
20921 has_shortname = 1;
20922#endif
20923 }
20924
20925#ifdef WIN3264
20926 /* Check shortname after we have done 'heads' and before we do 'tails'
20927 */
20928 if (has_shortname)
20929 {
20930 pbuf = NULL;
20931 /* Copy the string if it is shortened by :h */
20932 if (*fnamelen < (int)STRLEN(*fnamep))
20933 {
20934 p = vim_strnsave(*fnamep, *fnamelen);
20935 if (p == 0)
20936 return -1;
20937 vim_free(*bufp);
20938 *bufp = *fnamep = p;
20939 }
20940
20941 /* Split into two implementations - makes it easier. First is where
20942 * there isn't a full name already, second is where there is.
20943 */
20944 if (!has_fullname && !vim_isAbsName(*fnamep))
20945 {
20946 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20947 return -1;
20948 }
20949 else
20950 {
20951 int l;
20952
20953 /* Simple case, already have the full-name
20954 * Nearly always shorter, so try first time. */
20955 l = *fnamelen;
20956 if (!get_short_pathname(fnamep, bufp, &l))
20957 return -1;
20958
20959 if (l == 0)
20960 {
20961 /* Couldn't find the filename.. search the paths.
20962 */
20963 l = *fnamelen;
20964 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20965 return -1;
20966 }
20967 *fnamelen = l;
20968 }
20969 }
20970#endif /* WIN3264 */
20971
20972 /* ":t" - tail, just the basename */
20973 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20974 {
20975 *usedlen += 2;
20976 *fnamelen -= (int)(tail - *fnamep);
20977 *fnamep = tail;
20978 }
20979
20980 /* ":e" - extension, can be repeated */
20981 /* ":r" - root, without extension, can be repeated */
20982 while (src[*usedlen] == ':'
20983 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20984 {
20985 /* find a '.' in the tail:
20986 * - for second :e: before the current fname
20987 * - otherwise: The last '.'
20988 */
20989 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20990 s = *fnamep - 2;
20991 else
20992 s = *fnamep + *fnamelen - 1;
20993 for ( ; s > tail; --s)
20994 if (s[0] == '.')
20995 break;
20996 if (src[*usedlen + 1] == 'e') /* :e */
20997 {
20998 if (s > tail)
20999 {
21000 *fnamelen += (int)(*fnamep - (s + 1));
21001 *fnamep = s + 1;
21002#ifdef VMS
21003 /* cut version from the extension */
21004 s = *fnamep + *fnamelen - 1;
21005 for ( ; s > *fnamep; --s)
21006 if (s[0] == ';')
21007 break;
21008 if (s > *fnamep)
21009 *fnamelen = s - *fnamep;
21010#endif
21011 }
21012 else if (*fnamep <= tail)
21013 *fnamelen = 0;
21014 }
21015 else /* :r */
21016 {
21017 if (s > tail) /* remove one extension */
21018 *fnamelen = (int)(s - *fnamep);
21019 }
21020 *usedlen += 2;
21021 }
21022
21023 /* ":s?pat?foo?" - substitute */
21024 /* ":gs?pat?foo?" - global substitute */
21025 if (src[*usedlen] == ':'
21026 && (src[*usedlen + 1] == 's'
21027 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21028 {
21029 char_u *str;
21030 char_u *pat;
21031 char_u *sub;
21032 int sep;
21033 char_u *flags;
21034 int didit = FALSE;
21035
21036 flags = (char_u *)"";
21037 s = src + *usedlen + 2;
21038 if (src[*usedlen + 1] == 'g')
21039 {
21040 flags = (char_u *)"g";
21041 ++s;
21042 }
21043
21044 sep = *s++;
21045 if (sep)
21046 {
21047 /* find end of pattern */
21048 p = vim_strchr(s, sep);
21049 if (p != NULL)
21050 {
21051 pat = vim_strnsave(s, (int)(p - s));
21052 if (pat != NULL)
21053 {
21054 s = p + 1;
21055 /* find end of substitution */
21056 p = vim_strchr(s, sep);
21057 if (p != NULL)
21058 {
21059 sub = vim_strnsave(s, (int)(p - s));
21060 str = vim_strnsave(*fnamep, *fnamelen);
21061 if (sub != NULL && str != NULL)
21062 {
21063 *usedlen = (int)(p + 1 - src);
21064 s = do_string_sub(str, pat, sub, flags);
21065 if (s != NULL)
21066 {
21067 *fnamep = s;
21068 *fnamelen = (int)STRLEN(s);
21069 vim_free(*bufp);
21070 *bufp = s;
21071 didit = TRUE;
21072 }
21073 }
21074 vim_free(sub);
21075 vim_free(str);
21076 }
21077 vim_free(pat);
21078 }
21079 }
21080 /* after using ":s", repeat all the modifiers */
21081 if (didit)
21082 goto repeat;
21083 }
21084 }
21085
21086 return valid;
21087}
21088
21089/*
21090 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21091 * "flags" can be "g" to do a global substitute.
21092 * Returns an allocated string, NULL for error.
21093 */
21094 char_u *
21095do_string_sub(str, pat, sub, flags)
21096 char_u *str;
21097 char_u *pat;
21098 char_u *sub;
21099 char_u *flags;
21100{
21101 int sublen;
21102 regmatch_T regmatch;
21103 int i;
21104 int do_all;
21105 char_u *tail;
21106 garray_T ga;
21107 char_u *ret;
21108 char_u *save_cpo;
21109
21110 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21111 save_cpo = p_cpo;
21112 p_cpo = (char_u *)"";
21113
21114 ga_init2(&ga, 1, 200);
21115
21116 do_all = (flags[0] == 'g');
21117
21118 regmatch.rm_ic = p_ic;
21119 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21120 if (regmatch.regprog != NULL)
21121 {
21122 tail = str;
21123 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21124 {
21125 /*
21126 * Get some space for a temporary buffer to do the substitution
21127 * into. It will contain:
21128 * - The text up to where the match is.
21129 * - The substituted text.
21130 * - The text after the match.
21131 */
21132 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21133 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21134 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21135 {
21136 ga_clear(&ga);
21137 break;
21138 }
21139
21140 /* copy the text up to where the match is */
21141 i = (int)(regmatch.startp[0] - tail);
21142 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21143 /* add the substituted text */
21144 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21145 + ga.ga_len + i, TRUE, TRUE, FALSE);
21146 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 /* avoid getting stuck on a match with an empty string */
21148 if (tail == regmatch.endp[0])
21149 {
21150 if (*tail == NUL)
21151 break;
21152 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21153 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 }
21155 else
21156 {
21157 tail = regmatch.endp[0];
21158 if (*tail == NUL)
21159 break;
21160 }
21161 if (!do_all)
21162 break;
21163 }
21164
21165 if (ga.ga_data != NULL)
21166 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21167
21168 vim_free(regmatch.regprog);
21169 }
21170
21171 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21172 ga_clear(&ga);
21173 p_cpo = save_cpo;
21174
21175 return ret;
21176}
21177
21178#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */