blob: 29bec6f462b146a5d0a82136ff15b9a8742d1599 [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 Moolenaar81bf7082005-02-12 14:31:42 +0000457static void emsg_funcname __ARGS((char *msg, 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));
625static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000627static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000628static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000631static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632#ifdef HAVE_STRFTIME
633static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
635static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000647static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000648static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000649static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000650static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000651static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000653static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000667static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000670static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000672static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
673static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static int get_env_len __ARGS((char_u **arg));
675static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000676static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000677static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
678#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
679#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
680 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000681static 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 +0000682static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000683static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000684static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
685static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static typval_T *alloc_tv __ARGS((void));
687static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void init_tv __ARGS((typval_T *varp));
689static long get_tv_number __ARGS((typval_T *varp));
690static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000691static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static char_u *get_tv_string __ARGS((typval_T *varp));
693static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000694static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000696static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
698static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
699static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
700static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
701static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
702static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
703static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000704static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000706static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
708static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
709static int eval_fname_script __ARGS((char_u *p));
710static int eval_fname_sid __ARGS((char_u *p));
711static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static ufunc_T *find_func __ARGS((char_u *name));
713static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000714static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715#ifdef FEAT_PROFILE
716static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000717static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
718static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
719static int
720# ifdef __BORLANDC__
721 _RTLENTRYF
722# endif
723 prof_total_cmp __ARGS((const void *s1, const void *s2));
724static int
725# ifdef __BORLANDC__
726 _RTLENTRYF
727# endif
728 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000729#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000731static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000732static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void func_free __ARGS((ufunc_T *fp));
734static void func_unref __ARGS((char_u *name));
735static void func_ref __ARGS((char_u *name));
736static 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));
737static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000738static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
739static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000740static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000741static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000742static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000744/* Character used as separated in autoload function/variable names. */
745#define AUTOLOAD_CHAR '#'
746
Bram Moolenaar33570922005-01-25 22:26:29 +0000747/*
748 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000749 */
750 void
751eval_init()
752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 int i;
754 struct vimvar *p;
755
756 init_var_dict(&globvardict, &globvars_var);
757 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000758 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000759 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
761 for (i = 0; i < VV_LEN; ++i)
762 {
763 p = &vimvars[i];
764 STRCPY(p->vv_di.di_key, p->vv_name);
765 if (p->vv_flags & VV_RO)
766 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
767 else if (p->vv_flags & VV_RO_SBX)
768 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
769 else
770 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771
772 /* add to v: scope dict, unless the value is not always available */
773 if (p->vv_type != VAR_UNKNOWN)
774 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000776 /* add to compat scope dict */
777 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000779}
780
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000781#if defined(EXITFREE) || defined(PROTO)
782 void
783eval_clear()
784{
785 int i;
786 struct vimvar *p;
787
788 for (i = 0; i < VV_LEN; ++i)
789 {
790 p = &vimvars[i];
791 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000792 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000793 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 p->vv_di.di_tv.vval.v_string = NULL;
795 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796 }
797 hash_clear(&vimvarht);
798 hash_clear(&compat_hashtab);
799
800 /* script-local variables */
801 for (i = 1; i <= ga_scripts.ga_len; ++i)
802 vars_clear(&SCRIPT_VARS(i));
803 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000805
806 /* global variables */
807 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000808
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000809 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000810 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000811 hash_clear(&func_hashtab);
812
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000813 /* autoloaded script names */
814 ga_clear_strings(&ga_loaded);
815
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000816 /* unreferenced lists and dicts */
817 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000818}
819#endif
820
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Return the name of the executed function.
823 */
824 char_u *
825func_name(cookie)
826 void *cookie;
827{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/*
832 * Return the address holding the next breakpoint line for a funccall cookie.
833 */
834 linenr_T *
835func_breakpoint(cookie)
836 void *cookie;
837{
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * Return the address holding the debug tick for a funccall cookie.
843 */
844 int *
845func_dbg_tick(cookie)
846 void *cookie;
847{
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
851/*
852 * Return the nesting level for a funccall cookie.
853 */
854 int
855func_level(cookie)
856 void *cookie;
857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000862funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864/*
865 * Return TRUE when a function was ended by a ":return" command.
866 */
867 int
868current_func_returned()
869{
870 return current_funccal->returned;
871}
872
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Set an internal variable to a string value. Creates the variable if it does
876 * not already exist.
877 */
878 void
879set_internal_string_var(name, value)
880 char_u *name;
881 char_u *value;
882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000883 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886 val = vim_strsave(value);
887 if (val != NULL)
888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000890 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 }
896}
897
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898static lval_T *redir_lval = NULL;
899static char_u *redir_endp = NULL;
900static char_u *redir_varname = NULL;
901
902/*
903 * Start recording command output to a variable
904 * Returns OK if successfully completed the setup. FAIL otherwise.
905 */
906 int
907var_redir_start(name, append)
908 char_u *name;
909 int append; /* append to an existing variable */
910{
911 int save_emsg;
912 int err;
913 typval_T tv;
914
915 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000916 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 {
918 EMSG(_(e_invarg));
919 return FAIL;
920 }
921
922 redir_varname = vim_strsave(name);
923 if (redir_varname == NULL)
924 return FAIL;
925
926 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
927 if (redir_lval == NULL)
928 {
929 var_redir_stop();
930 return FAIL;
931 }
932
933 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000934 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
935 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000936 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
937 {
938 if (redir_endp != NULL && *redir_endp != NUL)
939 /* Trailing characters are present after the variable name */
940 EMSG(_(e_trailing));
941 else
942 EMSG(_(e_invarg));
943 var_redir_stop();
944 return FAIL;
945 }
946
947 /* check if we can write to the variable: set it to or append an empty
948 * string */
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 tv.v_type = VAR_STRING;
952 tv.vval.v_string = (char_u *)"";
953 if (append)
954 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
955 else
956 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
957 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000958 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000959 if (err)
960 {
961 var_redir_stop();
962 return FAIL;
963 }
964 if (redir_lval->ll_newkey != NULL)
965 {
966 /* Dictionary item was created, don't do it again. */
967 vim_free(redir_lval->ll_newkey);
968 redir_lval->ll_newkey = NULL;
969 }
970
971 return OK;
972}
973
974/*
975 * Append "value[len]" to the variable set by var_redir_start().
976 */
977 void
978var_redir_str(value, len)
979 char_u *value;
980 int len;
981{
982 char_u *val;
983 typval_T tv;
984 int save_emsg;
985 int err;
986
987 if (redir_lval == NULL)
988 return;
989
990 if (len == -1)
991 /* Append the entire string */
992 val = vim_strsave(value);
993 else
994 /* Append only the specified number of characters */
995 val = vim_strnsave(value, len);
996 if (val == NULL)
997 return;
998
999 tv.v_type = VAR_STRING;
1000 tv.vval.v_string = val;
1001
1002 save_emsg = did_emsg;
1003 did_emsg = FALSE;
1004 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1005 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001006 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 if (err)
1008 var_redir_stop();
1009
1010 vim_free(tv.vval.v_string);
1011}
1012
1013/*
1014 * Stop redirecting command output to a variable.
1015 */
1016 void
1017var_redir_stop()
1018{
1019 if (redir_lval != NULL)
1020 {
1021 clear_lval(redir_lval);
1022 vim_free(redir_lval);
1023 redir_lval = NULL;
1024 }
1025 vim_free(redir_varname);
1026 redir_varname = NULL;
1027}
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029# if defined(FEAT_MBYTE) || defined(PROTO)
1030 int
1031eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1032 char_u *enc_from;
1033 char_u *enc_to;
1034 char_u *fname_from;
1035 char_u *fname_to;
1036{
1037 int err = FALSE;
1038
1039 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1040 set_vim_var_string(VV_CC_TO, enc_to, -1);
1041 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1042 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1043 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1044 err = TRUE;
1045 set_vim_var_string(VV_CC_FROM, NULL, -1);
1046 set_vim_var_string(VV_CC_TO, NULL, -1);
1047 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1048 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1049
1050 if (err)
1051 return FAIL;
1052 return OK;
1053}
1054# endif
1055
1056# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1057 int
1058eval_printexpr(fname, args)
1059 char_u *fname;
1060 char_u *args;
1061{
1062 int err = FALSE;
1063
1064 set_vim_var_string(VV_FNAME_IN, fname, -1);
1065 set_vim_var_string(VV_CMDARG, args, -1);
1066 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1067 err = TRUE;
1068 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1069 set_vim_var_string(VV_CMDARG, NULL, -1);
1070
1071 if (err)
1072 {
1073 mch_remove(fname);
1074 return FAIL;
1075 }
1076 return OK;
1077}
1078# endif
1079
1080# if defined(FEAT_DIFF) || defined(PROTO)
1081 void
1082eval_diff(origfile, newfile, outfile)
1083 char_u *origfile;
1084 char_u *newfile;
1085 char_u *outfile;
1086{
1087 int err = FALSE;
1088
1089 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1090 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1092 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1093 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1094 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1096}
1097
1098 void
1099eval_patch(origfile, difffile, outfile)
1100 char_u *origfile;
1101 char_u *difffile;
1102 char_u *outfile;
1103{
1104 int err;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114# endif
1115
1116/*
1117 * Top level evaluation function, returning a boolean.
1118 * Sets "error" to TRUE if there was an error.
1119 * Return TRUE or FALSE.
1120 */
1121 int
1122eval_to_bool(arg, error, nextcmd, skip)
1123 char_u *arg;
1124 int *error;
1125 char_u **nextcmd;
1126 int skip; /* only parse, don't execute */
1127{
Bram Moolenaar33570922005-01-25 22:26:29 +00001128 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int retval = FALSE;
1130
1131 if (skip)
1132 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 else
1136 {
1137 *error = FALSE;
1138 if (!skip)
1139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001140 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144 if (skip)
1145 --emsg_skip;
1146
1147 return retval;
1148}
1149
1150/*
1151 * Top level evaluation function, returning a string. If "skip" is TRUE,
1152 * only parsing to "nextcmd" is done, without reporting errors. Return
1153 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1154 */
1155 char_u *
1156eval_to_string_skip(arg, nextcmd, skip)
1157 char_u *arg;
1158 char_u **nextcmd;
1159 int skip; /* only parse, don't execute */
1160{
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *retval;
1163
1164 if (skip)
1165 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = NULL;
1168 else
1169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 retval = vim_strsave(get_tv_string(&tv));
1171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 if (skip)
1174 --emsg_skip;
1175
1176 return retval;
1177}
1178
1179/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001180 * Skip over an expression at "*pp".
1181 * Return FAIL for an error, OK otherwise.
1182 */
1183 int
1184skip_expr(pp)
1185 char_u **pp;
1186{
Bram Moolenaar33570922005-01-25 22:26:29 +00001187 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001188
1189 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001191}
1192
1193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 * Top level evaluation function, returning a string.
1195 * Return pointer to allocated memory, or NULL for failure.
1196 */
1197 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001198eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 char_u *arg;
1200 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001201 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
Bram Moolenaar33570922005-01-25 22:26:29 +00001203 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001205 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = NULL;
1209 else
1210 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001211 if (dolist && tv.v_type == VAR_LIST)
1212 {
1213 ga_init2(&ga, (int)sizeof(char), 80);
1214 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1215 ga_append(&ga, NUL);
1216 retval = (char_u *)ga.ga_data;
1217 }
1218 else
1219 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 return retval;
1224}
1225
1226/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 * Call eval_to_string() without using current local variables and using
1228 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001231eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 char_u *arg;
1233 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001234 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 char_u *retval;
1237 void *save_funccalp;
1238
1239 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001240 if (use_sandbox)
1241 ++sandbox;
1242 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001243 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 if (use_sandbox)
1245 --sandbox;
1246 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 restore_funccal(save_funccalp);
1248 return retval;
1249}
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
1252 * Top level evaluation function, returning a number.
1253 * Evaluates "expr" silently.
1254 * Returns -1 for an error.
1255 */
1256 int
1257eval_to_number(expr)
1258 char_u *expr;
1259{
Bram Moolenaar33570922005-01-25 22:26:29 +00001260 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001262 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 ++emsg_off;
1265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 retval = -1;
1268 else
1269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001270 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 --emsg_off;
1274
1275 return retval;
1276}
1277
Bram Moolenaara40058a2005-07-11 22:42:07 +00001278/*
1279 * Prepare v: variable "idx" to be used.
1280 * Save the current typeval in "save_tv".
1281 * When not used yet add the variable to the v: hashtable.
1282 */
1283 static void
1284prepare_vimvar(idx, save_tv)
1285 int idx;
1286 typval_T *save_tv;
1287{
1288 *save_tv = vimvars[idx].vv_tv;
1289 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1290 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1291}
1292
1293/*
1294 * Restore v: variable "idx" to typeval "save_tv".
1295 * When no longer defined, remove the variable from the v: hashtable.
1296 */
1297 static void
1298restore_vimvar(idx, save_tv)
1299 int idx;
1300 typval_T *save_tv;
1301{
1302 hashitem_T *hi;
1303
1304 clear_tv(&vimvars[idx].vv_tv);
1305 vimvars[idx].vv_tv = *save_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 {
1308 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1309 if (HASHITEM_EMPTY(hi))
1310 EMSG2(_(e_intern2), "restore_vimvar()");
1311 else
1312 hash_remove(&vimvarht, hi);
1313 }
1314}
1315
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001316#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001317/*
1318 * Evaluate an expression to a list with suggestions.
1319 * For the "expr:" part of 'spellsuggest'.
1320 */
1321 list_T *
1322eval_spell_expr(badword, expr)
1323 char_u *badword;
1324 char_u *expr;
1325{
1326 typval_T save_val;
1327 typval_T rettv;
1328 list_T *list = NULL;
1329 char_u *p = skipwhite(expr);
1330
1331 /* Set "v:val" to the bad word. */
1332 prepare_vimvar(VV_VAL, &save_val);
1333 vimvars[VV_VAL].vv_type = VAR_STRING;
1334 vimvars[VV_VAL].vv_str = badword;
1335 if (p_verbose == 0)
1336 ++emsg_off;
1337
1338 if (eval1(&p, &rettv, TRUE) == OK)
1339 {
1340 if (rettv.v_type != VAR_LIST)
1341 clear_tv(&rettv);
1342 else
1343 list = rettv.vval.v_list;
1344 }
1345
1346 if (p_verbose == 0)
1347 --emsg_off;
1348 vimvars[VV_VAL].vv_str = NULL;
1349 restore_vimvar(VV_VAL, &save_val);
1350
1351 return list;
1352}
1353
1354/*
1355 * "list" is supposed to contain two items: a word and a number. Return the
1356 * word in "pp" and the number as the return value.
1357 * Return -1 if anything isn't right.
1358 * Used to get the good word and score from the eval_spell_expr() result.
1359 */
1360 int
1361get_spellword(list, pp)
1362 list_T *list;
1363 char_u **pp;
1364{
1365 listitem_T *li;
1366
1367 li = list->lv_first;
1368 if (li == NULL)
1369 return -1;
1370 *pp = get_tv_string(&li->li_tv);
1371
1372 li = li->li_next;
1373 if (li == NULL)
1374 return -1;
1375 return get_tv_number(&li->li_tv);
1376}
1377#endif
1378
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001379/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001380 * Top level evaluation function.
1381 * Returns an allocated typval_T with the result.
1382 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001383 */
1384 typval_T *
1385eval_expr(arg, nextcmd)
1386 char_u *arg;
1387 char_u **nextcmd;
1388{
1389 typval_T *tv;
1390
1391 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001392 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001393 {
1394 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396 }
1397
1398 return tv;
1399}
1400
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1403/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001404 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 static int
1409call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *func;
1411 int argc;
1412 char_u **argv;
1413 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 long n;
1418 int len;
1419 int i;
1420 int doesrange;
1421 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001424 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 for (i = 0; i < argc; i++)
1429 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001430 /* Pass a NULL or empty argument as an empty string */
1431 if (argv[i] == NULL || *argv[i] == NUL)
1432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001433 argvars[i].v_type = VAR_STRING;
1434 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001435 continue;
1436 }
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 /* Recognize a number argument, the others must be strings. */
1439 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1440 if (len != 0 && len == (int)STRLEN(argv[i]))
1441 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001442 argvars[i].v_type = VAR_NUMBER;
1443 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 else
1446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001447 argvars[i].v_type = VAR_STRING;
1448 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450 }
1451
1452 if (safe)
1453 {
1454 save_funccalp = save_funccal();
1455 ++sandbox;
1456 }
1457
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1459 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (safe)
1463 {
1464 --sandbox;
1465 restore_funccal(save_funccalp);
1466 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001467 vim_free(argvars);
1468
1469 if (ret == FAIL)
1470 clear_tv(rettv);
1471
1472 return ret;
1473}
1474
1475/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001476 * Call vimL function "func" and return the result as a string.
1477 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 * Uses argv[argc] for the function arguments.
1479 */
1480 void *
1481call_func_retstr(func, argc, argv, safe)
1482 char_u *func;
1483 int argc;
1484 char_u **argv;
1485 int safe; /* use the sandbox */
1486{
1487 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001488 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489
1490 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1491 return NULL;
1492
1493 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return retval;
1496}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001498#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500 * Call vimL function "func" and return the result as a number.
1501 * Returns -1 when calling the function fails.
1502 * Uses argv[argc] for the function arguments.
1503 */
1504 long
1505call_func_retnr(func, argc, argv, safe)
1506 char_u *func;
1507 int argc;
1508 char_u **argv;
1509 int safe; /* use the sandbox */
1510{
1511 typval_T rettv;
1512 long retval;
1513
1514 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1515 return -1;
1516
1517 retval = get_tv_number_chk(&rettv, NULL);
1518 clear_tv(&rettv);
1519 return retval;
1520}
1521#endif
1522
1523/*
1524 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001525 * Uses argv[argc] for the function arguments.
1526 */
1527 void *
1528call_func_retlist(func, argc, argv, safe)
1529 char_u *func;
1530 int argc;
1531 char_u **argv;
1532 int safe; /* use the sandbox */
1533{
1534 typval_T rettv;
1535
1536 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1537 return NULL;
1538
1539 if (rettv.v_type != VAR_LIST)
1540 {
1541 clear_tv(&rettv);
1542 return NULL;
1543 }
1544
1545 return rettv.vval.v_list;
1546}
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#endif
1549
1550/*
1551 * Save the current function call pointer, and set it to NULL.
1552 * Used when executing autocommands and for ":source".
1553 */
1554 void *
1555save_funccal()
1556{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001557 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 current_funccal = NULL;
1560 return (void *)fc;
1561}
1562
1563 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001564restore_funccal(vfc)
1565 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001567 funccall_T *fc = (funccall_T *)vfc;
1568
1569 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570}
1571
Bram Moolenaar05159a02005-02-26 23:04:13 +00001572#if defined(FEAT_PROFILE) || defined(PROTO)
1573/*
1574 * Prepare profiling for entering a child or something else that is not
1575 * counted for the script/function itself.
1576 * Should always be called in pair with prof_child_exit().
1577 */
1578 void
1579prof_child_enter(tm)
1580 proftime_T *tm; /* place to store waittime */
1581{
1582 funccall_T *fc = current_funccal;
1583
1584 if (fc != NULL && fc->func->uf_profiling)
1585 profile_start(&fc->prof_child);
1586 script_prof_save(tm);
1587}
1588
1589/*
1590 * Take care of time spent in a child.
1591 * Should always be called after prof_child_enter().
1592 */
1593 void
1594prof_child_exit(tm)
1595 proftime_T *tm; /* where waittime was stored */
1596{
1597 funccall_T *fc = current_funccal;
1598
1599 if (fc != NULL && fc->func->uf_profiling)
1600 {
1601 profile_end(&fc->prof_child);
1602 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1603 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1604 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1605 }
1606 script_prof_restore(tm);
1607}
1608#endif
1609
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#ifdef FEAT_FOLDING
1612/*
1613 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1614 * it in "*cp". Doesn't give error messages.
1615 */
1616 int
1617eval_foldexpr(arg, cp)
1618 char_u *arg;
1619 int *cp;
1620{
Bram Moolenaar33570922005-01-25 22:26:29 +00001621 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 int retval;
1623 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001624 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1625 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001628 if (use_sandbox)
1629 ++sandbox;
1630 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 retval = 0;
1634 else
1635 {
1636 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 if (tv.v_type == VAR_NUMBER)
1638 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001639 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 retval = 0;
1641 else
1642 {
1643 /* If the result is a string, check if there is a non-digit before
1644 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (!VIM_ISDIGIT(*s) && *s != '-')
1647 *cp = *s++;
1648 retval = atol((char *)s);
1649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
1652 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001653 if (use_sandbox)
1654 --sandbox;
1655 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
1657 return retval;
1658}
1659#endif
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 * ":let" list all variable values
1663 * ":let var1 var2" list variable values
1664 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 * ":let var += expr" assignment command.
1666 * ":let var -= expr" assignment command.
1667 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
1670 void
1671ex_let(eap)
1672 exarg_T *eap;
1673{
1674 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 int var_count = 0;
1679 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001681 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaardb552d602006-03-23 22:59:57 +00001683 argend = skip_var_list(arg, &var_count, &semicolon);
1684 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001686 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1687 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001688 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001691 /*
1692 * ":let" without "=": list variables
1693 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 if (*arg == '[')
1695 EMSG(_(e_invarg));
1696 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 /* ":let var1 var2" */
1698 arg = list_arg_vars(eap, arg);
1699 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 list_glob_vars();
1703 list_buf_vars();
1704 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001705#ifdef FEAT_WINDOWS
1706 list_tab_vars();
1707#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001708 list_script_vars();
1709 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001710 list_vim_vars();
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 eap->nextcmd = check_nextcmd(arg);
1713 }
1714 else
1715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 op[0] = '=';
1717 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001718 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1721 op[0] = expr[-1]; /* +=, -= or .= */
1722 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (eap->skip)
1726 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (eap->skip)
1729 {
1730 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --emsg_skip;
1733 }
1734 else if (i != FAIL)
1735 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 }
1741}
1742
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743/*
1744 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1745 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 * When "nextchars" is not NULL it points to a string with characters that
1747 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1748 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 * Returns OK or FAIL;
1750 */
1751 static int
1752ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1753 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001754 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 int copy; /* copy values from "tv", don't move */
1756 int semicolon; /* from skip_var_list() */
1757 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759{
1760 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 listitem_T *item;
1764 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765
1766 if (*arg != '[')
1767 {
1768 /*
1769 * ":let var = expr" or ":for var in list"
1770 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 return FAIL;
1773 return OK;
1774 }
1775
1776 /*
1777 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1778 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001779 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 {
1781 EMSG(_(e_listreq));
1782 return FAIL;
1783 }
1784
1785 i = list_len(l);
1786 if (semicolon == 0 && var_count < i)
1787 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001788 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 return FAIL;
1790 }
1791 if (var_count - semicolon > i)
1792 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001793 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 return FAIL;
1795 }
1796
1797 item = l->lv_first;
1798 while (*arg != ']')
1799 {
1800 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 item = item->li_next;
1803 if (arg == NULL)
1804 return FAIL;
1805
1806 arg = skipwhite(arg);
1807 if (*arg == ';')
1808 {
1809 /* Put the rest of the list (may be empty) in the var after ';'.
1810 * Create a new list for this. */
1811 l = list_alloc();
1812 if (l == NULL)
1813 return FAIL;
1814 while (item != NULL)
1815 {
1816 list_append_tv(l, &item->li_tv);
1817 item = item->li_next;
1818 }
1819
1820 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001821 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 ltv.vval.v_list = l;
1823 l->lv_refcount = 1;
1824
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1826 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 clear_tv(&ltv);
1828 if (arg == NULL)
1829 return FAIL;
1830 break;
1831 }
1832 else if (*arg != ',' && *arg != ']')
1833 {
1834 EMSG2(_(e_intern2), "ex_let_vars()");
1835 return FAIL;
1836 }
1837 }
1838
1839 return OK;
1840}
1841
1842/*
1843 * Skip over assignable variable "var" or list of variables "[var, var]".
1844 * Used for ":let varvar = expr" and ":for varvar in expr".
1845 * For "[var, var]" increment "*var_count" for each variable.
1846 * for "[var, var; var]" set "semicolon".
1847 * Return NULL for an error.
1848 */
1849 static char_u *
1850skip_var_list(arg, var_count, semicolon)
1851 char_u *arg;
1852 int *var_count;
1853 int *semicolon;
1854{
1855 char_u *p, *s;
1856
1857 if (*arg == '[')
1858 {
1859 /* "[var, var]": find the matching ']'. */
1860 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001861 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 {
1863 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1864 s = skip_var_one(p);
1865 if (s == p)
1866 {
1867 EMSG2(_(e_invarg2), p);
1868 return NULL;
1869 }
1870 ++*var_count;
1871
1872 p = skipwhite(s);
1873 if (*p == ']')
1874 break;
1875 else if (*p == ';')
1876 {
1877 if (*semicolon == 1)
1878 {
1879 EMSG(_("Double ; in list of variables"));
1880 return NULL;
1881 }
1882 *semicolon = 1;
1883 }
1884 else if (*p != ',')
1885 {
1886 EMSG2(_(e_invarg2), p);
1887 return NULL;
1888 }
1889 }
1890 return p + 1;
1891 }
1892 else
1893 return skip_var_one(arg);
1894}
1895
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001896/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001897 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1898 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001899 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 static char_u *
1901skip_var_one(arg)
1902 char_u *arg;
1903{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001904 if (*arg == '@' && arg[1] != NUL)
1905 return arg + 2;
1906 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1907 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908}
1909
Bram Moolenaara7043832005-01-21 11:56:39 +00001910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 * List variables for hashtab "ht" with prefix "prefix".
1912 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001913 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001915list_hashtable_vars(ht, prefix, empty)
1916 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919{
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 hashitem_T *hi;
1921 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 int todo;
1923
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001924 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1926 {
1927 if (!HASHITEM_EMPTY(hi))
1928 {
1929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 di = HI2DI(hi);
1931 if (empty || di->di_tv.v_type != VAR_STRING
1932 || di->di_tv.vval.v_string != NULL)
1933 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 }
1935 }
1936}
1937
1938/*
1939 * List global variables.
1940 */
1941 static void
1942list_glob_vars()
1943{
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001945}
1946
1947/*
1948 * List buffer variables.
1949 */
1950 static void
1951list_buf_vars()
1952{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001953 char_u numbuf[NUMBUFLEN];
1954
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956
1957 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1958 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001959}
1960
1961/*
1962 * List window variables.
1963 */
1964 static void
1965list_win_vars()
1966{
Bram Moolenaar33570922005-01-25 22:26:29 +00001967 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968}
1969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001970#ifdef FEAT_WINDOWS
1971/*
1972 * List tab page variables.
1973 */
1974 static void
1975list_tab_vars()
1976{
1977 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1978}
1979#endif
1980
Bram Moolenaara7043832005-01-21 11:56:39 +00001981/*
1982 * List Vim variables.
1983 */
1984 static void
1985list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986{
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988}
1989
1990/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001991 * List script-local variables, if there is a script.
1992 */
1993 static void
1994list_script_vars()
1995{
1996 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1997 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1998}
1999
2000/*
2001 * List function variables, if there is a function.
2002 */
2003 static void
2004list_func_vars()
2005{
2006 if (current_funccal != NULL)
2007 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2008 (char_u *)"l:", FALSE);
2009}
2010
2011/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 * List variables in "arg".
2013 */
2014 static char_u *
2015list_arg_vars(eap, arg)
2016 exarg_T *eap;
2017 char_u *arg;
2018{
2019 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 char_u *name_start;
2023 char_u *arg_subsc;
2024 char_u *tofree;
2025 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026
2027 while (!ends_excmd(*arg) && !got_int)
2028 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002029 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002031 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2033 {
2034 emsg_severe = TRUE;
2035 EMSG(_(e_trailing));
2036 break;
2037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 /* get_name_len() takes care of expanding curly braces */
2042 name_start = name = arg;
2043 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2044 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046 /* This is mainly to keep test 49 working: when expanding
2047 * curly braces fails overrule the exception error message. */
2048 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050 emsg_severe = TRUE;
2051 EMSG2(_(e_invarg2), arg);
2052 break;
2053 }
2054 error = TRUE;
2055 }
2056 else
2057 {
2058 if (tofree != NULL)
2059 name = tofree;
2060 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 else
2063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 /* handle d.key, l[idx], f(expr) */
2065 arg_subsc = arg;
2066 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002070 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 case 'g': list_glob_vars(); break;
2075 case 'b': list_buf_vars(); break;
2076 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002077#ifdef FEAT_WINDOWS
2078 case 't': list_tab_vars(); break;
2079#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002081 case 's': list_script_vars(); break;
2082 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 default:
2084 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
2087 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088 {
2089 char_u numbuf[NUMBUFLEN];
2090 char_u *tf;
2091 int c;
2092 char_u *s;
2093
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002094 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095 c = *arg;
2096 *arg = NUL;
2097 list_one_var_a((char_u *)"",
2098 arg == arg_subsc ? name : name_start,
2099 tv.v_type, s == NULL ? (char_u *)"" : s);
2100 *arg = c;
2101 vim_free(tf);
2102 }
2103 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 }
2106 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107
2108 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110
2111 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 }
2113
2114 return arg;
2115}
2116
2117/*
2118 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2119 * Returns a pointer to the char just after the var name.
2120 * Returns NULL if there is an error.
2121 */
2122 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002123ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129{
2130 int c1;
2131 char_u *name;
2132 char_u *p;
2133 char_u *arg_end = NULL;
2134 int len;
2135 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137
2138 /*
2139 * ":let $VAR = expr": Set environment variable.
2140 */
2141 if (*arg == '$')
2142 {
2143 /* Find the end of the name. */
2144 ++arg;
2145 name = arg;
2146 len = get_env_len(&arg);
2147 if (len == 0)
2148 EMSG2(_(e_invarg2), name - 1);
2149 else
2150 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 if (op != NULL && (*op == '+' || *op == '-'))
2152 EMSG2(_(e_letwrong), op);
2153 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 EMSG(_(e_letunexp));
2156 else
2157 {
2158 c1 = name[len];
2159 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002160 p = get_tv_string_chk(tv);
2161 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 {
2163 int mustfree = FALSE;
2164 char_u *s = vim_getenv(name, &mustfree);
2165
2166 if (s != NULL)
2167 {
2168 p = tofree = concat_str(s, p);
2169 if (mustfree)
2170 vim_free(s);
2171 }
2172 }
2173 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 if (STRICMP(name, "HOME") == 0)
2177 init_homedir();
2178 else if (didset_vim && STRICMP(name, "VIM") == 0)
2179 didset_vim = FALSE;
2180 else if (didset_vimruntime
2181 && STRICMP(name, "VIMRUNTIME") == 0)
2182 didset_vimruntime = FALSE;
2183 arg_end = arg;
2184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
2189 }
2190
2191 /*
2192 * ":let &option = expr": Set option value.
2193 * ":let &l:option = expr": Set local option value.
2194 * ":let &g:option = expr": Set global option value.
2195 */
2196 else if (*arg == '&')
2197 {
2198 /* Find the end of the name. */
2199 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002200 if (p == NULL || (endchars != NULL
2201 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 EMSG(_(e_letunexp));
2203 else
2204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 long n;
2206 int opt_type;
2207 long numval;
2208 char_u *stringval = NULL;
2209 char_u *s;
2210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 c1 = *p;
2212 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213
2214 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002215 s = get_tv_string_chk(tv); /* != NULL if number or string */
2216 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 {
2218 opt_type = get_option_value(arg, &numval,
2219 &stringval, opt_flags);
2220 if ((opt_type == 1 && *op == '.')
2221 || (opt_type == 0 && *op != '.'))
2222 EMSG2(_(e_letwrong), op);
2223 else
2224 {
2225 if (opt_type == 1) /* number */
2226 {
2227 if (*op == '+')
2228 n = numval + n;
2229 else
2230 n = numval - n;
2231 }
2232 else if (opt_type == 0 && stringval != NULL) /* string */
2233 {
2234 s = concat_str(stringval, s);
2235 vim_free(stringval);
2236 stringval = s;
2237 }
2238 }
2239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 if (s != NULL)
2241 {
2242 set_option_value(arg, n, s, opt_flags);
2243 arg_end = p;
2244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 }
2248 }
2249
2250 /*
2251 * ":let @r = expr": Set register contents.
2252 */
2253 else if (*arg == '@')
2254 {
2255 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 if (op != NULL && (*op == '+' || *op == '-'))
2257 EMSG2(_(e_letwrong), op);
2258 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002259 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 EMSG(_(e_letunexp));
2261 else
2262 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002263 char_u *tofree = NULL;
2264 char_u *s;
2265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002266 p = get_tv_string_chk(tv);
2267 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002269 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 if (s != NULL)
2271 {
2272 p = tofree = concat_str(s, p);
2273 vim_free(s);
2274 }
2275 }
2276 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 arg_end = arg + 1;
2280 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
2284
2285 /*
2286 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002289 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002291 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2297 EMSG(_(e_letunexp));
2298 else
2299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 arg_end = p;
2302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
2306
2307 else
2308 EMSG2(_(e_invarg2), arg);
2309
2310 return arg_end;
2311}
2312
2313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002314 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2315 */
2316 static int
2317check_changedtick(arg)
2318 char_u *arg;
2319{
2320 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2321 {
2322 EMSG2(_(e_readonlyvar), arg);
2323 return TRUE;
2324 }
2325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 * Get an lval: variable, Dict item or List item that can be assigned a value
2330 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2331 * "name.key", "name.key[expr]" etc.
2332 * Indexing only works if "name" is an existing List or Dictionary.
2333 * "name" points to the start of the name.
2334 * If "rettv" is not NULL it points to the value to be assigned.
2335 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2336 * wrong; must end in space or cmd separator.
2337 *
2338 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 */
2342 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002343get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002345 typval_T *rettv;
2346 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 int unlet;
2348 int skip;
2349 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002350 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 char_u *expr_start, *expr_end;
2354 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002355 dictitem_T *v;
2356 typval_T var1;
2357 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002359 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002362 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002365 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366
2367 if (skip)
2368 {
2369 /* When skipping just find the end of the name. */
2370 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002371 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 }
2373
2374 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002375 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (expr_start != NULL)
2377 {
2378 /* Don't expand the name when we already know there is an error. */
2379 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2380 && *p != '[' && *p != '.')
2381 {
2382 EMSG(_(e_trailing));
2383 return NULL;
2384 }
2385
2386 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2387 if (lp->ll_exp_name == NULL)
2388 {
2389 /* Report an invalid expression in braces, unless the
2390 * expression evaluation has been cancelled due to an
2391 * aborting error, an interrupt, or an exception. */
2392 if (!aborting() && !quiet)
2393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002394 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 EMSG2(_(e_invarg2), name);
2396 return NULL;
2397 }
2398 }
2399 lp->ll_name = lp->ll_exp_name;
2400 }
2401 else
2402 lp->ll_name = name;
2403
2404 /* Without [idx] or .key we are done. */
2405 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2406 return p;
2407
2408 cc = *p;
2409 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002410 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (v == NULL && !quiet)
2412 EMSG2(_(e_undefvar), lp->ll_name);
2413 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 if (v == NULL)
2415 return NULL;
2416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 /*
2418 * Loop until no more [idx] or .key is following.
2419 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002420 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2424 && !(lp->ll_tv->v_type == VAR_DICT
2425 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (!quiet)
2428 EMSG(_("E689: Can only index a List or Dictionary"));
2429 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (!quiet)
2434 EMSG(_("E708: [:] must come last"));
2435 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002437
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 len = -1;
2439 if (*p == '.')
2440 {
2441 key = p + 1;
2442 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2443 ;
2444 if (len == 0)
2445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSG(_(e_emptykey));
2448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
2450 p = key + len;
2451 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002452 else
2453 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002455 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 if (*p == ':')
2457 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002458 else
2459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 empty1 = FALSE;
2461 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 if (get_tv_string_chk(&var1) == NULL)
2464 {
2465 /* not a number or string */
2466 clear_tv(&var1);
2467 return NULL;
2468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470
2471 /* Optionally get the second index [ :expr]. */
2472 if (*p == ':')
2473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002478 if (!empty1)
2479 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (rettv != NULL && (rettv->v_type != VAR_LIST
2483 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (!empty1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491 p = skipwhite(p + 1);
2492 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 else
2495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2498 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (!empty1)
2500 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 if (get_tv_string_chk(&var2) == NULL)
2504 {
2505 /* not a number or string */
2506 if (!empty1)
2507 clear_tv(&var1);
2508 clear_tv(&var2);
2509 return NULL;
2510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (!quiet)
2520 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 if (!empty1)
2522 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
2527
2528 /* Skip to past ']'. */
2529 ++p;
2530 }
2531
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 {
2534 if (len == -1)
2535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 if (*key == NUL)
2539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (!quiet)
2541 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 }
2545 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 lp->ll_list = NULL;
2547 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002548 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002551 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (len == -1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 }
2560 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 if (len == -1)
2565 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 p = NULL;
2568 break;
2569 }
2570 if (len == -1)
2571 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 }
2574 else
2575 {
2576 /*
2577 * Get the number and item for the only or first index of the List.
2578 */
2579 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 else
2582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002583 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 clear_tv(&var1);
2585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 lp->ll_list = lp->ll_tv->vval.v_list;
2588 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2589 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002591 if (lp->ll_n1 < 0)
2592 {
2593 lp->ll_n1 = 0;
2594 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2595 }
2596 }
2597 if (lp->ll_li == NULL)
2598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 }
2603
2604 /*
2605 * May need to find the item or absolute index for the second
2606 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * When no index given: "lp->ll_empty2" is TRUE.
2608 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 }
2621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2623 if (lp->ll_n1 < 0)
2624 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2625 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 }
2628
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 }
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 return p;
2634}
2635
2636/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002637 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 */
2639 static void
2640clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642{
2643 vim_free(lp->ll_exp_name);
2644 vim_free(lp->ll_newkey);
2645}
2646
2647/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002648 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 */
2652 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002653set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659{
2660 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002661 listitem_T *ri;
2662 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663
2664 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 cc = *endp;
2669 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 if (op != NULL && *op != '=')
2671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002674 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002675 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002676 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 {
2678 if (tv_op(&tv, rettv, op) == OK)
2679 set_var(lp->ll_name, &tv, FALSE);
2680 clear_tv(&tv);
2681 }
2682 }
2683 else
2684 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688 else if (tv_check_lock(lp->ll_newkey == NULL
2689 ? lp->ll_tv->v_lock
2690 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2691 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 else if (lp->ll_range)
2693 {
2694 /*
2695 * Assign the List values to the list items.
2696 */
2697 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 if (op != NULL && *op != '=')
2700 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2701 else
2702 {
2703 clear_tv(&lp->ll_li->li_tv);
2704 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 ri = ri->li_next;
2707 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2708 break;
2709 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002712 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 ri = NULL;
2715 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_li = lp->ll_li->li_next;
2719 ++lp->ll_n1;
2720 }
2721 if (ri != NULL)
2722 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 else if (lp->ll_empty2
2724 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 : lp->ll_n1 != lp->ll_n2)
2726 EMSG(_("E711: List value has not enough items"));
2727 }
2728 else
2729 {
2730 /*
2731 * Assign to a List or Dictionary item.
2732 */
2733 if (lp->ll_newkey != NULL)
2734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 if (op != NULL && *op != '=')
2736 {
2737 EMSG2(_(e_letwrong), op);
2738 return;
2739 }
2740
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002742 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (di == NULL)
2744 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2746 {
2747 vim_free(di);
2748 return;
2749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 else if (op != NULL && *op != '=')
2753 {
2754 tv_op(lp->ll_tv, rettv, op);
2755 return;
2756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 /*
2761 * Assign the value to the variable or list item.
2762 */
2763 if (copy)
2764 copy_tv(rettv, lp->ll_tv);
2765 else
2766 {
2767 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002768 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002770 }
2771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772}
2773
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002775 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2776 * Returns OK or FAIL.
2777 */
2778 static int
2779tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 typval_T *tv1;
2781 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 char_u *op;
2783{
2784 long n;
2785 char_u numbuf[NUMBUFLEN];
2786 char_u *s;
2787
2788 /* Can't do anything with a Funcref or a Dict on the right. */
2789 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2790 {
2791 switch (tv1->v_type)
2792 {
2793 case VAR_DICT:
2794 case VAR_FUNC:
2795 break;
2796
2797 case VAR_LIST:
2798 if (*op != '+' || tv2->v_type != VAR_LIST)
2799 break;
2800 /* List += List */
2801 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2802 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2803 return OK;
2804
2805 case VAR_NUMBER:
2806 case VAR_STRING:
2807 if (tv2->v_type == VAR_LIST)
2808 break;
2809 if (*op == '+' || *op == '-')
2810 {
2811 /* nr += nr or nr -= nr*/
2812 n = get_tv_number(tv1);
2813 if (*op == '+')
2814 n += get_tv_number(tv2);
2815 else
2816 n -= get_tv_number(tv2);
2817 clear_tv(tv1);
2818 tv1->v_type = VAR_NUMBER;
2819 tv1->vval.v_number = n;
2820 }
2821 else
2822 {
2823 /* str .= str */
2824 s = get_tv_string(tv1);
2825 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2826 clear_tv(tv1);
2827 tv1->v_type = VAR_STRING;
2828 tv1->vval.v_string = s;
2829 }
2830 return OK;
2831 }
2832 }
2833
2834 EMSG2(_(e_letwrong), op);
2835 return FAIL;
2836}
2837
2838/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 * Add a watcher to a list.
2840 */
2841 static void
2842list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 list_T *l;
2844 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002845{
2846 lw->lw_next = l->lv_watch;
2847 l->lv_watch = lw;
2848}
2849
2850/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002851 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 * No warning when it isn't found...
2853 */
2854 static void
2855list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 list_T *l;
2857 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860
2861 lwp = &l->lv_watch;
2862 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2863 {
2864 if (lw == lwrem)
2865 {
2866 *lwp = lw->lw_next;
2867 break;
2868 }
2869 lwp = &lw->lw_next;
2870 }
2871}
2872
2873/*
2874 * Just before removing an item from a list: advance watchers to the next
2875 * item.
2876 */
2877 static void
2878list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 list_T *l;
2880 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881{
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
2884 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2885 if (lw->lw_item == item)
2886 lw->lw_item = item->li_next;
2887}
2888
2889/*
2890 * Evaluate the expression used in a ":for var in expr" command.
2891 * "arg" points to "var".
2892 * Set "*errp" to TRUE for an error, FALSE otherwise;
2893 * Return a pointer that holds the info. Null when there is an error.
2894 */
2895 void *
2896eval_for_line(arg, errp, nextcmdp, skip)
2897 char_u *arg;
2898 int *errp;
2899 char_u **nextcmdp;
2900 int skip;
2901{
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 typval_T tv;
2905 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906
2907 *errp = TRUE; /* default: there is an error */
2908
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 if (fi == NULL)
2911 return NULL;
2912
2913 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2914 if (expr == NULL)
2915 return fi;
2916
2917 expr = skipwhite(expr);
2918 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2919 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002920 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921 return fi;
2922 }
2923
2924 if (skip)
2925 ++emsg_skip;
2926 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2927 {
2928 *errp = FALSE;
2929 if (!skip)
2930 {
2931 l = tv.vval.v_list;
2932 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002933 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 clear_tv(&tv);
2936 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937 else
2938 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002939 /* No need to increment the refcount, it's already set for the
2940 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 fi->fi_list = l;
2942 list_add_watch(l, &fi->fi_lw);
2943 fi->fi_lw.lw_item = l->lv_first;
2944 }
2945 }
2946 }
2947 if (skip)
2948 --emsg_skip;
2949
2950 return fi;
2951}
2952
2953/*
2954 * Use the first item in a ":for" list. Advance to the next.
2955 * Assign the values to the variable (list). "arg" points to the first one.
2956 * Return TRUE when a valid item was found, FALSE when at end of list or
2957 * something wrong.
2958 */
2959 int
2960next_for_item(fi_void, arg)
2961 void *fi_void;
2962 char_u *arg;
2963{
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002965 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967
2968 item = fi->fi_lw.lw_item;
2969 if (item == NULL)
2970 result = FALSE;
2971 else
2972 {
2973 fi->fi_lw.lw_item = item->li_next;
2974 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2975 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2976 }
2977 return result;
2978}
2979
2980/*
2981 * Free the structure used to store info used by ":for".
2982 */
2983 void
2984free_for_info(fi_void)
2985 void *fi_void;
2986{
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002989 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002992 list_unref(fi->fi_list);
2993 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 vim_free(fi);
2995}
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2998
2999 void
3000set_context_for_expression(xp, arg, cmdidx)
3001 expand_T *xp;
3002 char_u *arg;
3003 cmdidx_T cmdidx;
3004{
3005 int got_eq = FALSE;
3006 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 if (cmdidx == CMD_let)
3010 {
3011 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003012 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 {
3014 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003015 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003016 {
3017 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 if (vim_iswhite(*p))
3020 break;
3021 }
3022 return;
3023 }
3024 }
3025 else
3026 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3027 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 while ((xp->xp_pattern = vim_strpbrk(arg,
3029 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3030 {
3031 c = *xp->xp_pattern;
3032 if (c == '&')
3033 {
3034 c = xp->xp_pattern[1];
3035 if (c == '&')
3036 {
3037 ++xp->xp_pattern;
3038 xp->xp_context = cmdidx != CMD_let || got_eq
3039 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3040 }
3041 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3045 xp->xp_pattern += 2;
3046
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049 else if (c == '$')
3050 {
3051 /* environment variable */
3052 xp->xp_context = EXPAND_ENV_VARS;
3053 }
3054 else if (c == '=')
3055 {
3056 got_eq = TRUE;
3057 xp->xp_context = EXPAND_EXPRESSION;
3058 }
3059 else if (c == '<'
3060 && xp->xp_context == EXPAND_FUNCTIONS
3061 && vim_strchr(xp->xp_pattern, '(') == NULL)
3062 {
3063 /* Function name can start with "<SNR>" */
3064 break;
3065 }
3066 else if (cmdidx != CMD_let || got_eq)
3067 {
3068 if (c == '"') /* string */
3069 {
3070 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3071 if (c == '\\' && xp->xp_pattern[1] != NUL)
3072 ++xp->xp_pattern;
3073 xp->xp_context = EXPAND_NOTHING;
3074 }
3075 else if (c == '\'') /* literal string */
3076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003077 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3079 /* skip */ ;
3080 xp->xp_context = EXPAND_NOTHING;
3081 }
3082 else if (c == '|')
3083 {
3084 if (xp->xp_pattern[1] == '|')
3085 {
3086 ++xp->xp_pattern;
3087 xp->xp_context = EXPAND_EXPRESSION;
3088 }
3089 else
3090 xp->xp_context = EXPAND_COMMANDS;
3091 }
3092 else
3093 xp->xp_context = EXPAND_EXPRESSION;
3094 }
3095 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 /* Doesn't look like something valid, expand as an expression
3097 * anyway. */
3098 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 arg = xp->xp_pattern;
3100 if (*arg != NUL)
3101 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3102 /* skip */ ;
3103 }
3104 xp->xp_pattern = arg;
3105}
3106
3107#endif /* FEAT_CMDL_COMPL */
3108
3109/*
3110 * ":1,25call func(arg1, arg2)" function call.
3111 */
3112 void
3113ex_call(eap)
3114 exarg_T *eap;
3115{
3116 char_u *arg = eap->arg;
3117 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 linenr_T lnum;
3123 int doesrange;
3124 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003127 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3128 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 if (tofree == NULL)
3130 return;
3131
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003132 /* Increase refcount on dictionary, it could get deleted when evaluating
3133 * the arguments. */
3134 if (fudi.fd_dict != NULL)
3135 ++fudi.fd_dict->dv_refcount;
3136
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003138 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
Bram Moolenaar532c7802005-01-27 14:44:31 +00003141 /* Skip white space to allow ":call func ()". Not good, but required for
3142 * backward compatibility. */
3143 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003144 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 if (*startarg != '(')
3147 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003148 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 goto end;
3150 }
3151
3152 /*
3153 * When skipping, evaluate the function once, to find the end of the
3154 * arguments.
3155 * When the function takes a range, this is discovered after the first
3156 * call, and the loop is broken.
3157 */
3158 if (eap->skip)
3159 {
3160 ++emsg_skip;
3161 lnum = eap->line2; /* do it once, also with an invalid range */
3162 }
3163 else
3164 lnum = eap->line1;
3165 for ( ; lnum <= eap->line2; ++lnum)
3166 {
3167 if (!eap->skip && eap->addr_count > 0)
3168 {
3169 curwin->w_cursor.lnum = lnum;
3170 curwin->w_cursor.col = 0;
3171 }
3172 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003173 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 eap->line1, eap->line2, &doesrange,
3175 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
3177 failed = TRUE;
3178 break;
3179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (doesrange || eap->skip)
3182 break;
3183 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003184 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003186 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (aborting())
3188 break;
3189 }
3190 if (eap->skip)
3191 --emsg_skip;
3192
3193 if (!failed)
3194 {
3195 /* Check for trailing illegal characters and a following command. */
3196 if (!ends_excmd(*arg))
3197 {
3198 emsg_severe = TRUE;
3199 EMSG(_(e_trailing));
3200 }
3201 else
3202 eap->nextcmd = check_nextcmd(arg);
3203 }
3204
3205end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003206 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003207 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208}
3209
3210/*
3211 * ":unlet[!] var1 ... " command.
3212 */
3213 void
3214ex_unlet(eap)
3215 exarg_T *eap;
3216{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003217 ex_unletlock(eap, eap->arg, 0);
3218}
3219
3220/*
3221 * ":lockvar" and ":unlockvar" commands
3222 */
3223 void
3224ex_lockvar(eap)
3225 exarg_T *eap;
3226{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003228 int deep = 2;
3229
3230 if (eap->forceit)
3231 deep = -1;
3232 else if (vim_isdigit(*arg))
3233 {
3234 deep = getdigits(&arg);
3235 arg = skipwhite(arg);
3236 }
3237
3238 ex_unletlock(eap, arg, deep);
3239}
3240
3241/*
3242 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3243 */
3244 static void
3245ex_unletlock(eap, argstart, deep)
3246 exarg_T *eap;
3247 char_u *argstart;
3248 int deep;
3249{
3250 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 do
3256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003258 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3259 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 if (lv.ll_name == NULL)
3261 error = TRUE; /* error but continue parsing */
3262 if (name_end == NULL || (!vim_iswhite(*name_end)
3263 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 if (name_end != NULL)
3266 {
3267 emsg_severe = TRUE;
3268 EMSG(_(e_trailing));
3269 }
3270 if (!(eap->skip || error))
3271 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 break;
3273 }
3274
3275 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276 {
3277 if (eap->cmdidx == CMD_unlet)
3278 {
3279 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3280 error = TRUE;
3281 }
3282 else
3283 {
3284 if (do_lock_var(&lv, name_end, deep,
3285 eap->cmdidx == CMD_lockvar) == FAIL)
3286 error = TRUE;
3287 }
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003290 if (!eap->skip)
3291 clear_lval(&lv);
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 arg = skipwhite(name_end);
3294 } while (!ends_excmd(*arg));
3295
3296 eap->nextcmd = check_nextcmd(arg);
3297}
3298
Bram Moolenaar8c711452005-01-14 21:53:12 +00003299 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003300do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003301 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003302 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303 int forceit;
3304{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003305 int ret = OK;
3306 int cc;
3307
3308 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 cc = *name_end;
3311 *name_end = NUL;
3312
3313 /* Normal name or expanded name. */
3314 if (check_changedtick(lp->ll_name))
3315 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003316 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003317 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003319 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003320 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3321 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003322 else if (lp->ll_range)
3323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003325
3326 /* Delete a range of List items. */
3327 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3328 {
3329 li = lp->ll_li->li_next;
3330 listitem_remove(lp->ll_list, lp->ll_li);
3331 lp->ll_li = li;
3332 ++lp->ll_n1;
3333 }
3334 }
3335 else
3336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003337 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003338 /* unlet a List item. */
3339 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003340 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003342 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 }
3344
3345 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003346}
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348/*
3349 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003350 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 */
3352 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003353do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 hashtab_T *ht;
3358 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003359 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 ht = find_var_ht(name, &varname);
3362 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003364 hi = hash_find(ht, varname);
3365 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003366 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 if (var_check_ro(HI2DI(hi)->di_flags, name))
3368 return FAIL;
3369 delete_var(ht, hi);
3370 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373 if (forceit)
3374 return OK;
3375 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return FAIL;
3377}
3378
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003379/*
3380 * Lock or unlock variable indicated by "lp".
3381 * "deep" is the levels to go (-1 for unlimited);
3382 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3383 */
3384 static int
3385do_lock_var(lp, name_end, deep, lock)
3386 lval_T *lp;
3387 char_u *name_end;
3388 int deep;
3389 int lock;
3390{
3391 int ret = OK;
3392 int cc;
3393 dictitem_T *di;
3394
3395 if (deep == 0) /* nothing to do */
3396 return OK;
3397
3398 if (lp->ll_tv == NULL)
3399 {
3400 cc = *name_end;
3401 *name_end = NUL;
3402
3403 /* Normal name or expanded name. */
3404 if (check_changedtick(lp->ll_name))
3405 ret = FAIL;
3406 else
3407 {
3408 di = find_var(lp->ll_name, NULL);
3409 if (di == NULL)
3410 ret = FAIL;
3411 else
3412 {
3413 if (lock)
3414 di->di_flags |= DI_FLAGS_LOCK;
3415 else
3416 di->di_flags &= ~DI_FLAGS_LOCK;
3417 item_lock(&di->di_tv, deep, lock);
3418 }
3419 }
3420 *name_end = cc;
3421 }
3422 else if (lp->ll_range)
3423 {
3424 listitem_T *li = lp->ll_li;
3425
3426 /* (un)lock a range of List items. */
3427 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3428 {
3429 item_lock(&li->li_tv, deep, lock);
3430 li = li->li_next;
3431 ++lp->ll_n1;
3432 }
3433 }
3434 else if (lp->ll_list != NULL)
3435 /* (un)lock a List item. */
3436 item_lock(&lp->ll_li->li_tv, deep, lock);
3437 else
3438 /* un(lock) a Dictionary item. */
3439 item_lock(&lp->ll_di->di_tv, deep, lock);
3440
3441 return ret;
3442}
3443
3444/*
3445 * Lock or unlock an item. "deep" is nr of levels to go.
3446 */
3447 static void
3448item_lock(tv, deep, lock)
3449 typval_T *tv;
3450 int deep;
3451 int lock;
3452{
3453 static int recurse = 0;
3454 list_T *l;
3455 listitem_T *li;
3456 dict_T *d;
3457 hashitem_T *hi;
3458 int todo;
3459
3460 if (recurse >= DICT_MAXNEST)
3461 {
3462 EMSG(_("E743: variable nested too deep for (un)lock"));
3463 return;
3464 }
3465 if (deep == 0)
3466 return;
3467 ++recurse;
3468
3469 /* lock/unlock the item itself */
3470 if (lock)
3471 tv->v_lock |= VAR_LOCKED;
3472 else
3473 tv->v_lock &= ~VAR_LOCKED;
3474
3475 switch (tv->v_type)
3476 {
3477 case VAR_LIST:
3478 if ((l = tv->vval.v_list) != NULL)
3479 {
3480 if (lock)
3481 l->lv_lock |= VAR_LOCKED;
3482 else
3483 l->lv_lock &= ~VAR_LOCKED;
3484 if (deep < 0 || deep > 1)
3485 /* recursive: lock/unlock the items the List contains */
3486 for (li = l->lv_first; li != NULL; li = li->li_next)
3487 item_lock(&li->li_tv, deep - 1, lock);
3488 }
3489 break;
3490 case VAR_DICT:
3491 if ((d = tv->vval.v_dict) != NULL)
3492 {
3493 if (lock)
3494 d->dv_lock |= VAR_LOCKED;
3495 else
3496 d->dv_lock &= ~VAR_LOCKED;
3497 if (deep < 0 || deep > 1)
3498 {
3499 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3502 {
3503 if (!HASHITEM_EMPTY(hi))
3504 {
3505 --todo;
3506 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3507 }
3508 }
3509 }
3510 }
3511 }
3512 --recurse;
3513}
3514
Bram Moolenaara40058a2005-07-11 22:42:07 +00003515/*
3516 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3517 * it refers to a List or Dictionary that is locked.
3518 */
3519 static int
3520tv_islocked(tv)
3521 typval_T *tv;
3522{
3523 return (tv->v_lock & VAR_LOCKED)
3524 || (tv->v_type == VAR_LIST
3525 && tv->vval.v_list != NULL
3526 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3527 || (tv->v_type == VAR_DICT
3528 && tv->vval.v_dict != NULL
3529 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3530}
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3533/*
3534 * Delete all "menutrans_" variables.
3535 */
3536 void
3537del_menutrans_vars()
3538{
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003543 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003544 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 {
3546 if (!HASHITEM_EMPTY(hi))
3547 {
3548 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3550 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003551 }
3552 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
3555#endif
3556
3557#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3558
3559/*
3560 * Local string buffer for the next two functions to store a variable name
3561 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3562 * get_user_var_name().
3563 */
3564
3565static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3566
3567static char_u *varnamebuf = NULL;
3568static int varnamebuflen = 0;
3569
3570/*
3571 * Function to concatenate a prefix and a variable name.
3572 */
3573 static char_u *
3574cat_prefix_varname(prefix, name)
3575 int prefix;
3576 char_u *name;
3577{
3578 int len;
3579
3580 len = (int)STRLEN(name) + 3;
3581 if (len > varnamebuflen)
3582 {
3583 vim_free(varnamebuf);
3584 len += 10; /* some additional space */
3585 varnamebuf = alloc(len);
3586 if (varnamebuf == NULL)
3587 {
3588 varnamebuflen = 0;
3589 return NULL;
3590 }
3591 varnamebuflen = len;
3592 }
3593 *varnamebuf = prefix;
3594 varnamebuf[1] = ':';
3595 STRCPY(varnamebuf + 2, name);
3596 return varnamebuf;
3597}
3598
3599/*
3600 * Function given to ExpandGeneric() to obtain the list of user defined
3601 * (global/buffer/window/built-in) variable names.
3602 */
3603/*ARGSUSED*/
3604 char_u *
3605get_user_var_name(xp, idx)
3606 expand_T *xp;
3607 int idx;
3608{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003609 static long_u gdone;
3610 static long_u bdone;
3611 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003612#ifdef FEAT_WINDOWS
3613 static long_u tdone;
3614#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003615 static int vidx;
3616 static hashitem_T *hi;
3617 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003620 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003621 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003622#ifdef FEAT_WINDOWS
3623 tdone = 0;
3624#endif
3625 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003626
3627 /* Global variables */
3628 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003630 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003632 else
3633 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003634 while (HASHITEM_EMPTY(hi))
3635 ++hi;
3636 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3637 return cat_prefix_varname('g', hi->hi_key);
3638 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003640
3641 /* b: variables */
3642 ht = &curbuf->b_vars.dv_hashtab;
3643 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003645 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003647 else
3648 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 while (HASHITEM_EMPTY(hi))
3650 ++hi;
3651 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return (char_u *)"b:changedtick";
3657 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003658
3659 /* w: variables */
3660 ht = &curwin->w_vars.dv_hashtab;
3661 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003663 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003665 else
3666 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 while (HASHITEM_EMPTY(hi))
3668 ++hi;
3669 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003671
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003672#ifdef FEAT_WINDOWS
3673 /* t: variables */
3674 ht = &curtab->tp_vars.dv_hashtab;
3675 if (tdone < ht->ht_used)
3676 {
3677 if (tdone++ == 0)
3678 hi = ht->ht_array;
3679 else
3680 ++hi;
3681 while (HASHITEM_EMPTY(hi))
3682 ++hi;
3683 return cat_prefix_varname('t', hi->hi_key);
3684 }
3685#endif
3686
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 /* v: variables */
3688 if (vidx < VV_LEN)
3689 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 vim_free(varnamebuf);
3692 varnamebuf = NULL;
3693 varnamebuflen = 0;
3694 return NULL;
3695}
3696
3697#endif /* FEAT_CMDL_COMPL */
3698
3699/*
3700 * types for expressions.
3701 */
3702typedef enum
3703{
3704 TYPE_UNKNOWN = 0
3705 , TYPE_EQUAL /* == */
3706 , TYPE_NEQUAL /* != */
3707 , TYPE_GREATER /* > */
3708 , TYPE_GEQUAL /* >= */
3709 , TYPE_SMALLER /* < */
3710 , TYPE_SEQUAL /* <= */
3711 , TYPE_MATCH /* =~ */
3712 , TYPE_NOMATCH /* !~ */
3713} exptype_T;
3714
3715/*
3716 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3719 */
3720
3721/*
3722 * Handle zero level expression.
3723 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003725 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * Return OK or FAIL.
3727 */
3728 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 char_u **nextcmd;
3733 int evaluate;
3734{
3735 int ret;
3736 char_u *p;
3737
3738 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (ret == FAIL || !ends_excmd(*p))
3741 {
3742 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 /*
3745 * Report the invalid expression unless the expression evaluation has
3746 * been cancelled due to an aborting error, an interrupt, or an
3747 * exception.
3748 */
3749 if (!aborting())
3750 EMSG2(_(e_invexpr2), arg);
3751 ret = FAIL;
3752 }
3753 if (nextcmd != NULL)
3754 *nextcmd = check_nextcmd(p);
3755
3756 return ret;
3757}
3758
3759/*
3760 * Handle top level expression:
3761 * expr1 ? expr0 : expr0
3762 *
3763 * "arg" must point to the first non-white of the expression.
3764 * "arg" is advanced to the next non-white after the recognized expression.
3765 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003766 * Note: "rettv.v_lock" is not set.
3767 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 * Return OK or FAIL.
3769 */
3770 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int evaluate;
3775{
3776 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003777 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 /*
3780 * Get the first variable.
3781 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return FAIL;
3784
3785 if ((*arg)[0] == '?')
3786 {
3787 result = FALSE;
3788 if (evaluate)
3789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003790 int error = FALSE;
3791
3792 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 if (error)
3796 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798
3799 /*
3800 * Get the second variable.
3801 */
3802 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805
3806 /*
3807 * Check for the ":".
3808 */
3809 if ((*arg)[0] != ':')
3810 {
3811 EMSG(_("E109: Missing ':' after '?'"));
3812 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return FAIL;
3815 }
3816
3817 /*
3818 * Get the third variable.
3819 */
3820 *arg = skipwhite(*arg + 1);
3821 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3822 {
3823 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826 }
3827 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830
3831 return OK;
3832}
3833
3834/*
3835 * Handle first level expression:
3836 * expr2 || expr2 || expr2 logical OR
3837 *
3838 * "arg" must point to the first non-white of the expression.
3839 * "arg" is advanced to the next non-white after the recognized expression.
3840 *
3841 * Return OK or FAIL.
3842 */
3843 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003844eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 int evaluate;
3848{
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 long result;
3851 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003852 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
3854 /*
3855 * Get the first variable.
3856 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return FAIL;
3859
3860 /*
3861 * Repeat until there is no following "||".
3862 */
3863 first = TRUE;
3864 result = FALSE;
3865 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3866 {
3867 if (evaluate && first)
3868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 if (error)
3873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 first = FALSE;
3875 }
3876
3877 /*
3878 * Get the second variable.
3879 */
3880 *arg = skipwhite(*arg + 2);
3881 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3882 return FAIL;
3883
3884 /*
3885 * Compute the result.
3886 */
3887 if (evaluate && !result)
3888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003889 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003892 if (error)
3893 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 if (evaluate)
3896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897 rettv->v_type = VAR_NUMBER;
3898 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 }
3901
3902 return OK;
3903}
3904
3905/*
3906 * Handle second level expression:
3907 * expr3 && expr3 && expr3 logical AND
3908 *
3909 * "arg" must point to the first non-white of the expression.
3910 * "arg" is advanced to the next non-white after the recognized expression.
3911 *
3912 * Return OK or FAIL.
3913 */
3914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 int evaluate;
3919{
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 long result;
3922 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 /*
3926 * Get the first variable.
3927 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003928 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return FAIL;
3930
3931 /*
3932 * Repeat until there is no following "&&".
3933 */
3934 first = TRUE;
3935 result = TRUE;
3936 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3937 {
3938 if (evaluate && first)
3939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (error)
3944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 first = FALSE;
3946 }
3947
3948 /*
3949 * Get the second variable.
3950 */
3951 *arg = skipwhite(*arg + 2);
3952 if (eval4(arg, &var2, evaluate && result) == FAIL)
3953 return FAIL;
3954
3955 /*
3956 * Compute the result.
3957 */
3958 if (evaluate && result)
3959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003960 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003963 if (error)
3964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 if (evaluate)
3967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 rettv->v_type = VAR_NUMBER;
3969 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
3972
3973 return OK;
3974}
3975
3976/*
3977 * Handle third level expression:
3978 * var1 == var2
3979 * var1 =~ var2
3980 * var1 != var2
3981 * var1 !~ var2
3982 * var1 > var2
3983 * var1 >= var2
3984 * var1 < var2
3985 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003986 * var1 is var2
3987 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 *
3989 * "arg" must point to the first non-white of the expression.
3990 * "arg" is advanced to the next non-white after the recognized expression.
3991 *
3992 * Return OK or FAIL.
3993 */
3994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int evaluate;
3999{
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u *p;
4002 int i;
4003 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 int len = 2;
4006 long n1, n2;
4007 char_u *s1, *s2;
4008 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4009 regmatch_T regmatch;
4010 int ic;
4011 char_u *save_cpo;
4012
4013 /*
4014 * Get the first variable.
4015 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return FAIL;
4018
4019 p = *arg;
4020 switch (p[0])
4021 {
4022 case '=': if (p[1] == '=')
4023 type = TYPE_EQUAL;
4024 else if (p[1] == '~')
4025 type = TYPE_MATCH;
4026 break;
4027 case '!': if (p[1] == '=')
4028 type = TYPE_NEQUAL;
4029 else if (p[1] == '~')
4030 type = TYPE_NOMATCH;
4031 break;
4032 case '>': if (p[1] != '=')
4033 {
4034 type = TYPE_GREATER;
4035 len = 1;
4036 }
4037 else
4038 type = TYPE_GEQUAL;
4039 break;
4040 case '<': if (p[1] != '=')
4041 {
4042 type = TYPE_SMALLER;
4043 len = 1;
4044 }
4045 else
4046 type = TYPE_SEQUAL;
4047 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004048 case 'i': if (p[1] == 's')
4049 {
4050 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4051 len = 5;
4052 if (!vim_isIDc(p[len]))
4053 {
4054 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4055 type_is = TRUE;
4056 }
4057 }
4058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 /*
4062 * If there is a comparitive operator, use it.
4063 */
4064 if (type != TYPE_UNKNOWN)
4065 {
4066 /* extra question mark appended: ignore case */
4067 if (p[len] == '?')
4068 {
4069 ic = TRUE;
4070 ++len;
4071 }
4072 /* extra '#' appended: match case */
4073 else if (p[len] == '#')
4074 {
4075 ic = FALSE;
4076 ++len;
4077 }
4078 /* nothing appened: use 'ignorecase' */
4079 else
4080 ic = p_ic;
4081
4082 /*
4083 * Get the second variable.
4084 */
4085 *arg = skipwhite(p + len);
4086 if (eval5(arg, &var2, evaluate) == FAIL)
4087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return FAIL;
4090 }
4091
4092 if (evaluate)
4093 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004094 if (type_is && rettv->v_type != var2.v_type)
4095 {
4096 /* For "is" a different type always means FALSE, for "notis"
4097 * it means TRUE. */
4098 n1 = (type == TYPE_NEQUAL);
4099 }
4100 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4101 {
4102 if (type_is)
4103 {
4104 n1 = (rettv->v_type == var2.v_type
4105 && rettv->vval.v_list == var2.vval.v_list);
4106 if (type == TYPE_NEQUAL)
4107 n1 = !n1;
4108 }
4109 else if (rettv->v_type != var2.v_type
4110 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4111 {
4112 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004113 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004114 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004115 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004116 clear_tv(rettv);
4117 clear_tv(&var2);
4118 return FAIL;
4119 }
4120 else
4121 {
4122 /* Compare two Lists for being equal or unequal. */
4123 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4124 if (type == TYPE_NEQUAL)
4125 n1 = !n1;
4126 }
4127 }
4128
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004129 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4130 {
4131 if (type_is)
4132 {
4133 n1 = (rettv->v_type == var2.v_type
4134 && rettv->vval.v_dict == var2.vval.v_dict);
4135 if (type == TYPE_NEQUAL)
4136 n1 = !n1;
4137 }
4138 else if (rettv->v_type != var2.v_type
4139 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4140 {
4141 if (rettv->v_type != var2.v_type)
4142 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4143 else
4144 EMSG(_("E736: Invalid operation for Dictionary"));
4145 clear_tv(rettv);
4146 clear_tv(&var2);
4147 return FAIL;
4148 }
4149 else
4150 {
4151 /* Compare two Dictionaries for being equal or unequal. */
4152 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4153 if (type == TYPE_NEQUAL)
4154 n1 = !n1;
4155 }
4156 }
4157
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004158 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4159 {
4160 if (rettv->v_type != var2.v_type
4161 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4162 {
4163 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004164 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004165 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 clear_tv(rettv);
4168 clear_tv(&var2);
4169 return FAIL;
4170 }
4171 else
4172 {
4173 /* Compare two Funcrefs for being equal or unequal. */
4174 if (rettv->vval.v_string == NULL
4175 || var2.vval.v_string == NULL)
4176 n1 = FALSE;
4177 else
4178 n1 = STRCMP(rettv->vval.v_string,
4179 var2.vval.v_string) == 0;
4180 if (type == TYPE_NEQUAL)
4181 n1 = !n1;
4182 }
4183 }
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /*
4186 * If one of the two variables is a number, compare as a number.
4187 * When using "=~" or "!~", always compare as string.
4188 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004189 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 n1 = get_tv_number(rettv);
4193 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 switch (type)
4195 {
4196 case TYPE_EQUAL: n1 = (n1 == n2); break;
4197 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4198 case TYPE_GREATER: n1 = (n1 > n2); break;
4199 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4200 case TYPE_SMALLER: n1 = (n1 < n2); break;
4201 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4202 case TYPE_UNKNOWN:
4203 case TYPE_MATCH:
4204 case TYPE_NOMATCH: break; /* avoid gcc warning */
4205 }
4206 }
4207 else
4208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 s1 = get_tv_string_buf(rettv, buf1);
4210 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4212 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4213 else
4214 i = 0;
4215 n1 = FALSE;
4216 switch (type)
4217 {
4218 case TYPE_EQUAL: n1 = (i == 0); break;
4219 case TYPE_NEQUAL: n1 = (i != 0); break;
4220 case TYPE_GREATER: n1 = (i > 0); break;
4221 case TYPE_GEQUAL: n1 = (i >= 0); break;
4222 case TYPE_SMALLER: n1 = (i < 0); break;
4223 case TYPE_SEQUAL: n1 = (i <= 0); break;
4224
4225 case TYPE_MATCH:
4226 case TYPE_NOMATCH:
4227 /* avoid 'l' flag in 'cpoptions' */
4228 save_cpo = p_cpo;
4229 p_cpo = (char_u *)"";
4230 regmatch.regprog = vim_regcomp(s2,
4231 RE_MAGIC + RE_STRING);
4232 regmatch.rm_ic = ic;
4233 if (regmatch.regprog != NULL)
4234 {
4235 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4236 vim_free(regmatch.regprog);
4237 if (type == TYPE_NOMATCH)
4238 n1 = !n1;
4239 }
4240 p_cpo = save_cpo;
4241 break;
4242
4243 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4244 }
4245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
4247 clear_tv(&var2);
4248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Handle fourth level expression:
4258 * + number addition
4259 * - number subtraction
4260 * . string concatenation
4261 *
4262 * "arg" must point to the first non-white of the expression.
4263 * "arg" is advanced to the next non-white after the recognized expression.
4264 *
4265 * Return OK or FAIL.
4266 */
4267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int evaluate;
4272{
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T var2;
4274 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 int op;
4276 long n1, n2;
4277 char_u *s1, *s2;
4278 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4279 char_u *p;
4280
4281 /*
4282 * Get the first variable.
4283 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286
4287 /*
4288 * Repeat computing, until no '+', '-' or '.' is following.
4289 */
4290 for (;;)
4291 {
4292 op = **arg;
4293 if (op != '+' && op != '-' && op != '.')
4294 break;
4295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (op != '+' || rettv->v_type != VAR_LIST)
4297 {
4298 /* For "list + ...", an illegal use of the first operand as
4299 * a number cannot be determined before evaluating the 2nd
4300 * operand: if this is also a list, all is ok.
4301 * For "something . ...", "something - ..." or "non-list + ...",
4302 * we know that the first operand needs to be a string or number
4303 * without evaluating the 2nd operand. So check before to avoid
4304 * side effects after an error. */
4305 if (evaluate && get_tv_string_chk(rettv) == NULL)
4306 {
4307 clear_tv(rettv);
4308 return FAIL;
4309 }
4310 }
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(*arg + 1);
4316 if (eval6(arg, &var2, evaluate) == FAIL)
4317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320 }
4321
4322 if (evaluate)
4323 {
4324 /*
4325 * Compute the result.
4326 */
4327 if (op == '.')
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4330 s2 = get_tv_string_buf_chk(&var2, buf2);
4331 if (s2 == NULL) /* type error ? */
4332 {
4333 clear_tv(rettv);
4334 clear_tv(&var2);
4335 return FAIL;
4336 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004337 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(rettv);
4339 rettv->v_type = VAR_STRING;
4340 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004342 else if (op == '+' && rettv->v_type == VAR_LIST
4343 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344 {
4345 /* concatenate Lists */
4346 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4347 &var3) == FAIL)
4348 {
4349 clear_tv(rettv);
4350 clear_tv(&var2);
4351 return FAIL;
4352 }
4353 clear_tv(rettv);
4354 *rettv = var3;
4355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 else
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 int error = FALSE;
4359
4360 n1 = get_tv_number_chk(rettv, &error);
4361 if (error)
4362 {
4363 /* This can only happen for "list + non-list".
4364 * For "non-list + ..." or "something - ...", we returned
4365 * before evaluating the 2nd operand. */
4366 clear_tv(rettv);
4367 return FAIL;
4368 }
4369 n2 = get_tv_number_chk(&var2, &error);
4370 if (error)
4371 {
4372 clear_tv(rettv);
4373 clear_tv(&var2);
4374 return FAIL;
4375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (op == '+')
4378 n1 = n1 + n2;
4379 else
4380 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 rettv->v_type = VAR_NUMBER;
4382 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 }
4387 return OK;
4388}
4389
4390/*
4391 * Handle fifth level expression:
4392 * * number multiplication
4393 * / number division
4394 * % number modulo
4395 *
4396 * "arg" must point to the first non-white of the expression.
4397 * "arg" is advanced to the next non-white after the recognized expression.
4398 *
4399 * Return OK or FAIL.
4400 */
4401 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 int evaluate;
4406{
Bram Moolenaar33570922005-01-25 22:26:29 +00004407 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 int op;
4409 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 /*
4413 * Get the first variable.
4414 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 return FAIL;
4417
4418 /*
4419 * Repeat computing, until no '*', '/' or '%' is following.
4420 */
4421 for (;;)
4422 {
4423 op = **arg;
4424 if (op != '*' && op != '/' && op != '%')
4425 break;
4426
4427 if (evaluate)
4428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431 if (error)
4432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 else
4435 n1 = 0;
4436
4437 /*
4438 * Get the second variable.
4439 */
4440 *arg = skipwhite(*arg + 1);
4441 if (eval7(arg, &var2, evaluate) == FAIL)
4442 return FAIL;
4443
4444 if (evaluate)
4445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004447 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 if (error)
4449 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /*
4452 * Compute the result.
4453 */
4454 if (op == '*')
4455 n1 = n1 * n2;
4456 else if (op == '/')
4457 {
4458 if (n2 == 0) /* give an error message? */
4459 n1 = 0x7fffffffL;
4460 else
4461 n1 = n1 / n2;
4462 }
4463 else
4464 {
4465 if (n2 == 0) /* give an error message? */
4466 n1 = 0;
4467 else
4468 n1 = n1 % n2;
4469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_NUMBER;
4471 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 }
4474
4475 return OK;
4476}
4477
4478/*
4479 * Handle sixth level expression:
4480 * number number constant
4481 * "string" string contstant
4482 * 'string' literal string contstant
4483 * &option-name option value
4484 * @r register contents
4485 * identifier variable value
4486 * function() function call
4487 * $VAR environment variable
4488 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004489 * [expr, expr] List
4490 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 *
4492 * Also handle:
4493 * ! in front logical NOT
4494 * - in front unary minus
4495 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 * trailing [] subscript in String or List
4497 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 *
4499 * "arg" must point to the first non-white of the expression.
4500 * "arg" is advanced to the next non-white after the recognized expression.
4501 *
4502 * Return OK or FAIL.
4503 */
4504 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 int evaluate;
4509{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 long n;
4511 int len;
4512 char_u *s;
4513 int val;
4514 char_u *start_leader, *end_leader;
4515 int ret = OK;
4516 char_u *alias;
4517
4518 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 /*
4525 * Skip '!' and '-' characters. They are handled later.
4526 */
4527 start_leader = *arg;
4528 while (**arg == '!' || **arg == '-' || **arg == '+')
4529 *arg = skipwhite(*arg + 1);
4530 end_leader = *arg;
4531
4532 switch (**arg)
4533 {
4534 /*
4535 * Number constant.
4536 */
4537 case '0':
4538 case '1':
4539 case '2':
4540 case '3':
4541 case '4':
4542 case '5':
4543 case '6':
4544 case '7':
4545 case '8':
4546 case '9':
4547 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4548 *arg += len;
4549 if (evaluate)
4550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004551 rettv->v_type = VAR_NUMBER;
4552 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 break;
4555
4556 /*
4557 * String constant: "string".
4558 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 break;
4561
4562 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 break;
4567
4568 /*
4569 * List: [expr, expr]
4570 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 break;
4573
4574 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 * Dictionary: {key: val, key: val}
4576 */
4577 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4578 break;
4579
4580 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004581 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004583 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585
4586 /*
4587 * Environment variable: $VAR.
4588 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004589 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 break;
4591
4592 /*
4593 * Register contents: @r.
4594 */
4595 case '@': ++*arg;
4596 if (evaluate)
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004599 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 if (**arg != NUL)
4602 ++*arg;
4603 break;
4604
4605 /*
4606 * nested expression: (expression).
4607 */
4608 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (**arg == ')')
4611 ++*arg;
4612 else if (ret == OK)
4613 {
4614 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 ret = FAIL;
4617 }
4618 break;
4619
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 break;
4622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623
4624 if (ret == NOTDONE)
4625 {
4626 /*
4627 * Must be a variable or function name.
4628 * Can also be a curly-braces kind of name: {expr}.
4629 */
4630 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004631 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004632 if (alias != NULL)
4633 s = alias;
4634
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004635 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 ret = FAIL;
4637 else
4638 {
4639 if (**arg == '(') /* recursive! */
4640 {
4641 /* If "s" is the name of a variable of type VAR_FUNC
4642 * use its contents. */
4643 s = deref_func_name(s, &len);
4644
4645 /* Invoke the function. */
4646 ret = get_func_tv(s, len, rettv, arg,
4647 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004648 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 /* Stop the expression evaluation when immediately
4650 * aborting on error, or when an interrupt occurred or
4651 * an exception was thrown but not caught. */
4652 if (aborting())
4653 {
4654 if (ret == OK)
4655 clear_tv(rettv);
4656 ret = FAIL;
4657 }
4658 }
4659 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004660 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004661 else
4662 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 }
4664
4665 if (alias != NULL)
4666 vim_free(alias);
4667 }
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 *arg = skipwhite(*arg);
4670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004671 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4672 * expr(expr). */
4673 if (ret == OK)
4674 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /*
4677 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4678 */
4679 if (ret == OK && evaluate && end_leader > start_leader)
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 int error = FALSE;
4682
4683 val = get_tv_number_chk(rettv, &error);
4684 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 clear_tv(rettv);
4687 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 else
4690 {
4691 while (end_leader > start_leader)
4692 {
4693 --end_leader;
4694 if (*end_leader == '!')
4695 val = !val;
4696 else if (*end_leader == '-')
4697 val = -val;
4698 }
4699 clear_tv(rettv);
4700 rettv->v_type = VAR_NUMBER;
4701 rettv->vval.v_number = val;
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704
4705 return ret;
4706}
4707
4708/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004709 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4710 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4712 */
4713 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004714eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004716 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004718 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719{
4720 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004721 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004723 long len = -1;
4724 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004730 if (verbose)
4731 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 return FAIL;
4733 }
4734
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 /*
4738 * dict.name
4739 */
4740 key = *arg + 1;
4741 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4742 ;
4743 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 }
4747 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 /*
4750 * something[idx]
4751 *
4752 * Get the (first) variable from inside the [].
4753 */
4754 *arg = skipwhite(*arg + 1);
4755 if (**arg == ':')
4756 empty1 = TRUE;
4757 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4758 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4760 {
4761 /* not a number or string */
4762 clear_tv(&var1);
4763 return FAIL;
4764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004765
4766 /*
4767 * Get the second variable from inside the [:].
4768 */
4769 if (**arg == ':')
4770 {
4771 range = TRUE;
4772 *arg = skipwhite(*arg + 1);
4773 if (**arg == ']')
4774 empty2 = TRUE;
4775 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 if (!empty1)
4778 clear_tv(&var1);
4779 return FAIL;
4780 }
4781 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4782 {
4783 /* not a number or string */
4784 if (!empty1)
4785 clear_tv(&var1);
4786 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787 return FAIL;
4788 }
4789 }
4790
4791 /* Check for the ']'. */
4792 if (**arg != ']')
4793 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004794 if (verbose)
4795 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 clear_tv(&var1);
4797 if (range)
4798 clear_tv(&var2);
4799 return FAIL;
4800 }
4801 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 }
4803
4804 if (evaluate)
4805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806 n1 = 0;
4807 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 n1 = get_tv_number(&var1);
4810 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 }
4812 if (range)
4813 {
4814 if (empty2)
4815 n2 = -1;
4816 else
4817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 n2 = get_tv_number(&var2);
4819 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 }
4821 }
4822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 {
4825 case VAR_NUMBER:
4826 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 len = (long)STRLEN(s);
4829 if (range)
4830 {
4831 /* The resulting variable is a substring. If the indexes
4832 * are out of range the result is empty. */
4833 if (n1 < 0)
4834 {
4835 n1 = len + n1;
4836 if (n1 < 0)
4837 n1 = 0;
4838 }
4839 if (n2 < 0)
4840 n2 = len + n2;
4841 else if (n2 >= len)
4842 n2 = len;
4843 if (n1 >= len || n2 < 0 || n1 > n2)
4844 s = NULL;
4845 else
4846 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4847 }
4848 else
4849 {
4850 /* The resulting variable is a string of a single
4851 * character. If the index is too big or negative the
4852 * result is empty. */
4853 if (n1 >= len || n1 < 0)
4854 s = NULL;
4855 else
4856 s = vim_strnsave(s + n1, 1);
4857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 clear_tv(rettv);
4859 rettv->v_type = VAR_STRING;
4860 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 break;
4862
4863 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 if (n1 < 0)
4866 n1 = len + n1;
4867 if (!empty1 && (n1 < 0 || n1 >= len))
4868 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004869 /* For a range we allow invalid values and return an empty
4870 * list. A list index out of range is an error. */
4871 if (!range)
4872 {
4873 if (verbose)
4874 EMSGN(_(e_listidx), n1);
4875 return FAIL;
4876 }
4877 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 }
4879 if (range)
4880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004881 list_T *l;
4882 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883
4884 if (n2 < 0)
4885 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004886 else if (n2 >= len)
4887 n2 = len - 1;
4888 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004889 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890 l = list_alloc();
4891 if (l == NULL)
4892 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 n1 <= n2; ++n1)
4895 {
4896 if (list_append_tv(l, &item->li_tv) == FAIL)
4897 {
4898 list_free(l);
4899 return FAIL;
4900 }
4901 item = item->li_next;
4902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 clear_tv(rettv);
4904 rettv->v_type = VAR_LIST;
4905 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004906 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907 }
4908 else
4909 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004910 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
4912 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 }
4914 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915
4916 case VAR_DICT:
4917 if (range)
4918 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004919 if (verbose)
4920 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 if (len == -1)
4922 clear_tv(&var1);
4923 return FAIL;
4924 }
4925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004926 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927
4928 if (len == -1)
4929 {
4930 key = get_tv_string(&var1);
4931 if (*key == NUL)
4932 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004933 if (verbose)
4934 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 clear_tv(&var1);
4936 return FAIL;
4937 }
4938 }
4939
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004940 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004942 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004943 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 if (len == -1)
4945 clear_tv(&var1);
4946 if (item == NULL)
4947 return FAIL;
4948
4949 copy_tv(&item->di_tv, &var1);
4950 clear_tv(rettv);
4951 *rettv = var1;
4952 }
4953 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 }
4955 }
4956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 return OK;
4958}
4959
4960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 * Get an option value.
4962 * "arg" points to the '&' or '+' before the option name.
4963 * "arg" is advanced to character after the option name.
4964 * Return OK or FAIL.
4965 */
4966 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004969 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 int evaluate;
4971{
4972 char_u *option_end;
4973 long numval;
4974 char_u *stringval;
4975 int opt_type;
4976 int c;
4977 int working = (**arg == '+'); /* has("+option") */
4978 int ret = OK;
4979 int opt_flags;
4980
4981 /*
4982 * Isolate the option name and find its value.
4983 */
4984 option_end = find_option_end(arg, &opt_flags);
4985 if (option_end == NULL)
4986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 EMSG2(_("E112: Option name missing: %s"), *arg);
4989 return FAIL;
4990 }
4991
4992 if (!evaluate)
4993 {
4994 *arg = option_end;
4995 return OK;
4996 }
4997
4998 c = *option_end;
4999 *option_end = NUL;
5000 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003 if (opt_type == -3) /* invalid name */
5004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 EMSG2(_("E113: Unknown option: %s"), *arg);
5007 ret = FAIL;
5008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 if (opt_type == -2) /* hidden string option */
5012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 rettv->v_type = VAR_STRING;
5014 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 else if (opt_type == -1) /* hidden number option */
5017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 else if (opt_type == 1) /* number option */
5022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 rettv->v_type = VAR_NUMBER;
5024 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 else /* string option */
5027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 rettv->v_type = VAR_STRING;
5029 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 }
5032 else if (working && (opt_type == -2 || opt_type == -1))
5033 ret = FAIL;
5034
5035 *option_end = c; /* put back for error messages */
5036 *arg = option_end;
5037
5038 return ret;
5039}
5040
5041/*
5042 * Allocate a variable for a string constant.
5043 * Return OK or FAIL.
5044 */
5045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 int evaluate;
5050{
5051 char_u *p;
5052 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 int extra = 0;
5054
5055 /*
5056 * Find the end of the string, skipping backslashed characters.
5057 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 if (*p == '\\' && p[1] != NUL)
5061 {
5062 ++p;
5063 /* A "\<x>" form occupies at least 4 characters, and produces up
5064 * to 6 characters: reserve space for 2 extra */
5065 if (*p == '<')
5066 extra += 2;
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
5070 if (*p != '"')
5071 {
5072 EMSG2(_("E114: Missing quote: %s"), *arg);
5073 return FAIL;
5074 }
5075
5076 /* If only parsing, set *arg and return here */
5077 if (!evaluate)
5078 {
5079 *arg = p + 1;
5080 return OK;
5081 }
5082
5083 /*
5084 * Copy the string into allocated memory, handling backslashed
5085 * characters.
5086 */
5087 name = alloc((unsigned)(p - *arg + extra));
5088 if (name == NULL)
5089 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 rettv->v_type = VAR_STRING;
5091 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 if (*p == '\\')
5096 {
5097 switch (*++p)
5098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 case 'b': *name++ = BS; ++p; break;
5100 case 'e': *name++ = ESC; ++p; break;
5101 case 'f': *name++ = FF; ++p; break;
5102 case 'n': *name++ = NL; ++p; break;
5103 case 'r': *name++ = CAR; ++p; break;
5104 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
5106 case 'X': /* hex: "\x1", "\x12" */
5107 case 'x':
5108 case 'u': /* Unicode: "\u0023" */
5109 case 'U':
5110 if (vim_isxdigit(p[1]))
5111 {
5112 int n, nr;
5113 int c = toupper(*p);
5114
5115 if (c == 'X')
5116 n = 2;
5117 else
5118 n = 4;
5119 nr = 0;
5120 while (--n >= 0 && vim_isxdigit(p[1]))
5121 {
5122 ++p;
5123 nr = (nr << 4) + hex2nr(*p);
5124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef FEAT_MBYTE
5127 /* For "\u" store the number according to
5128 * 'encoding'. */
5129 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else
5132#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136
5137 /* octal: "\1", "\12", "\123" */
5138 case '0':
5139 case '1':
5140 case '2':
5141 case '3':
5142 case '4':
5143 case '5':
5144 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 case '7': *name = *p++ - '0';
5146 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 *name = (*name << 3) + *p++ - '0';
5149 if (*p >= '0' && *p <= '7')
5150 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (extra != 0)
5158 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161 }
5162 /* FALLTHROUGH */
5163
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 break;
5166 }
5167 }
5168 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 *arg = p + 1;
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return OK;
5176}
5177
5178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Return OK or FAIL.
5181 */
5182 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 int evaluate;
5187{
5188 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005189 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005190 int reduce = 0;
5191
5192 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005194 */
5195 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5196 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005200 break;
5201 ++reduce;
5202 ++p;
5203 }
5204 }
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005207 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005209 return FAIL;
5210 }
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005213 if (!evaluate)
5214 {
5215 *arg = p + 1;
5216 return OK;
5217 }
5218
5219 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005221 */
5222 str = alloc((unsigned)((p - *arg) - reduce));
5223 if (str == NULL)
5224 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 rettv->v_type = VAR_STRING;
5226 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005227
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005229 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005233 break;
5234 ++p;
5235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005239 *arg = p + 1;
5240
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 return OK;
5242}
5243
5244/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 * Allocate a variable for a List and fill it from "*arg".
5246 * Return OK or FAIL.
5247 */
5248 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005251 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 int evaluate;
5253{
Bram Moolenaar33570922005-01-25 22:26:29 +00005254 list_T *l = NULL;
5255 typval_T tv;
5256 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257
5258 if (evaluate)
5259 {
5260 l = list_alloc();
5261 if (l == NULL)
5262 return FAIL;
5263 }
5264
5265 *arg = skipwhite(*arg + 1);
5266 while (**arg != ']' && **arg != NUL)
5267 {
5268 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5269 goto failret;
5270 if (evaluate)
5271 {
5272 item = listitem_alloc();
5273 if (item != NULL)
5274 {
5275 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005276 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 list_append(l, item);
5278 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005279 else
5280 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 }
5282
5283 if (**arg == ']')
5284 break;
5285 if (**arg != ',')
5286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 goto failret;
5289 }
5290 *arg = skipwhite(*arg + 1);
5291 }
5292
5293 if (**arg != ']')
5294 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296failret:
5297 if (evaluate)
5298 list_free(l);
5299 return FAIL;
5300 }
5301
5302 *arg = skipwhite(*arg + 1);
5303 if (evaluate)
5304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 rettv->v_type = VAR_LIST;
5306 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 ++l->lv_refcount;
5308 }
5309
5310 return OK;
5311}
5312
5313/*
5314 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005315 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005317 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318list_alloc()
5319{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320 list_T *l;
5321
5322 l = (list_T *)alloc_clear(sizeof(list_T));
5323 if (l != NULL)
5324 {
5325 /* Prepend the list to the list of lists for garbage collection. */
5326 if (first_list != NULL)
5327 first_list->lv_used_prev = l;
5328 l->lv_used_prev = NULL;
5329 l->lv_used_next = first_list;
5330 first_list = l;
5331 }
5332 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333}
5334
5335/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005336 * Allocate an empty list for a return value.
5337 * Returns OK or FAIL.
5338 */
5339 static int
5340rettv_list_alloc(rettv)
5341 typval_T *rettv;
5342{
5343 list_T *l = list_alloc();
5344
5345 if (l == NULL)
5346 return FAIL;
5347
5348 rettv->vval.v_list = l;
5349 rettv->v_type = VAR_LIST;
5350 ++l->lv_refcount;
5351 return OK;
5352}
5353
5354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 * Unreference a list: decrement the reference count and free it when it
5356 * becomes zero.
5357 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005358 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5363 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364}
5365
5366/*
5367 * Free a list, including all items it points to.
5368 * Ignores the reference count.
5369 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005370 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005372 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373{
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaard9fba312005-06-26 22:34:35 +00005376 /* Avoid that recursive reference to the list frees us again. */
5377 l->lv_refcount = DEL_REFCOUNT;
5378
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379 /* Remove the list from the list of lists for garbage collection. */
5380 if (l->lv_used_prev == NULL)
5381 first_list = l->lv_used_next;
5382 else
5383 l->lv_used_prev->lv_used_next = l->lv_used_next;
5384 if (l->lv_used_next != NULL)
5385 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5386
Bram Moolenaard9fba312005-06-26 22:34:35 +00005387 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005389 /* Remove the item before deleting it. */
5390 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 listitem_free(item);
5392 }
5393 vim_free(l);
5394}
5395
5396/*
5397 * Allocate a list item.
5398 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400listitem_alloc()
5401{
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403}
5404
5405/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005406 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 */
5408 static void
5409listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 vim_free(item);
5414}
5415
5416/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005417 * Remove a list item from a List and free it. Also clears the value.
5418 */
5419 static void
5420listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 list_T *l;
5422 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005423{
5424 list_remove(l, item, item);
5425 listitem_free(item);
5426}
5427
5428/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 * Get the number of items in a list.
5430 */
5431 static long
5432list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005433 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 if (l == NULL)
5436 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438}
5439
5440/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005441 * Return TRUE when two lists have exactly the same values.
5442 */
5443 static int
5444list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 list_T *l1;
5446 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 int ic; /* ignore case for strings */
5448{
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005450
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005451 if (list_len(l1) != list_len(l2))
5452 return FALSE;
5453
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005454 for (item1 = l1->lv_first, item2 = l2->lv_first;
5455 item1 != NULL && item2 != NULL;
5456 item1 = item1->li_next, item2 = item2->li_next)
5457 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5458 return FALSE;
5459 return item1 == NULL && item2 == NULL;
5460}
5461
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005462#if defined(FEAT_PYTHON) || defined(PROTO)
5463/*
5464 * Return the dictitem that an entry in a hashtable points to.
5465 */
5466 dictitem_T *
5467dict_lookup(hi)
5468 hashitem_T *hi;
5469{
5470 return HI2DI(hi);
5471}
5472#endif
5473
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005474/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005475 * Return TRUE when two dictionaries have exactly the same key/values.
5476 */
5477 static int
5478dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005479 dict_T *d1;
5480 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005481 int ic; /* ignore case for strings */
5482{
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 hashitem_T *hi;
5484 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005486
5487 if (dict_len(d1) != dict_len(d2))
5488 return FALSE;
5489
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005490 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005493 if (!HASHITEM_EMPTY(hi))
5494 {
5495 item2 = dict_find(d2, hi->hi_key, -1);
5496 if (item2 == NULL)
5497 return FALSE;
5498 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5499 return FALSE;
5500 --todo;
5501 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005502 }
5503 return TRUE;
5504}
5505
5506/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005507 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005508 * Compares the items just like "==" would compare them, but strings and
5509 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 */
5511 static int
5512tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005513 typval_T *tv1;
5514 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005515 int ic; /* ignore case */
5516{
5517 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005518 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005519
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005520 if (tv1->v_type != tv2->v_type)
5521 return FALSE;
5522
5523 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005524 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005525 case VAR_LIST:
5526 /* recursive! */
5527 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5528
5529 case VAR_DICT:
5530 /* recursive! */
5531 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5532
5533 case VAR_FUNC:
5534 return (tv1->vval.v_string != NULL
5535 && tv2->vval.v_string != NULL
5536 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5537
5538 case VAR_NUMBER:
5539 return tv1->vval.v_number == tv2->vval.v_number;
5540
5541 case VAR_STRING:
5542 s1 = get_tv_string_buf(tv1, buf1);
5543 s2 = get_tv_string_buf(tv2, buf2);
5544 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005545 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005546
5547 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 return TRUE;
5549}
5550
5551/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 * Locate item with index "n" in list "l" and return it.
5553 * A negative index is counted from the end; -1 is the last item.
5554 * Returns NULL when "n" is out of range.
5555 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005558 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 long n;
5560{
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 long idx;
5563
5564 if (l == NULL)
5565 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005566
5567 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005569 n = l->lv_len + n;
5570
5571 /* Check for index out of range. */
5572 if (n < 0 || n >= l->lv_len)
5573 return NULL;
5574
5575 /* When there is a cached index may start search from there. */
5576 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005578 if (n < l->lv_idx / 2)
5579 {
5580 /* closest to the start of the list */
5581 item = l->lv_first;
5582 idx = 0;
5583 }
5584 else if (n > (l->lv_idx + l->lv_len) / 2)
5585 {
5586 /* closest to the end of the list */
5587 item = l->lv_last;
5588 idx = l->lv_len - 1;
5589 }
5590 else
5591 {
5592 /* closest to the cached index */
5593 item = l->lv_idx_item;
5594 idx = l->lv_idx;
5595 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 }
5597 else
5598 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005599 if (n < l->lv_len / 2)
5600 {
5601 /* closest to the start of the list */
5602 item = l->lv_first;
5603 idx = 0;
5604 }
5605 else
5606 {
5607 /* closest to the end of the list */
5608 item = l->lv_last;
5609 idx = l->lv_len - 1;
5610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005612
5613 while (n > idx)
5614 {
5615 /* search forward */
5616 item = item->li_next;
5617 ++idx;
5618 }
5619 while (n < idx)
5620 {
5621 /* search backward */
5622 item = item->li_prev;
5623 --idx;
5624 }
5625
5626 /* cache the used index */
5627 l->lv_idx = idx;
5628 l->lv_idx_item = item;
5629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 return item;
5631}
5632
5633/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005634 * Get list item "l[idx]" as a number.
5635 */
5636 static long
5637list_find_nr(l, idx, errorp)
5638 list_T *l;
5639 long idx;
5640 int *errorp; /* set to TRUE when something wrong */
5641{
5642 listitem_T *li;
5643
5644 li = list_find(l, idx);
5645 if (li == NULL)
5646 {
5647 if (errorp != NULL)
5648 *errorp = TRUE;
5649 return -1L;
5650 }
5651 return get_tv_number_chk(&li->li_tv, errorp);
5652}
5653
5654/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005655 * Locate "item" list "l" and return its index.
5656 * Returns -1 when "item" is not in the list.
5657 */
5658 static long
5659list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005660 list_T *l;
5661 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005662{
5663 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005664 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005665
5666 if (l == NULL)
5667 return -1;
5668 idx = 0;
5669 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5670 ++idx;
5671 if (li == NULL)
5672 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005673 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005674}
5675
5676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 * Append item "item" to the end of list "l".
5678 */
5679 static void
5680list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005681 list_T *l;
5682 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683{
5684 if (l->lv_last == NULL)
5685 {
5686 /* empty list */
5687 l->lv_first = item;
5688 l->lv_last = item;
5689 item->li_prev = NULL;
5690 }
5691 else
5692 {
5693 l->lv_last->li_next = item;
5694 item->li_prev = l->lv_last;
5695 l->lv_last = item;
5696 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005697 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 item->li_next = NULL;
5699}
5700
5701/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005703 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 */
5705 static int
5706list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 list_T *l;
5708 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005710 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005711
Bram Moolenaar05159a02005-02-26 23:04:13 +00005712 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005714 copy_tv(tv, &li->li_tv);
5715 list_append(l, li);
5716 return OK;
5717}
5718
5719/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005720 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005721 * Return FAIL when out of memory.
5722 */
5723 int
5724list_append_dict(list, dict)
5725 list_T *list;
5726 dict_T *dict;
5727{
5728 listitem_T *li = listitem_alloc();
5729
5730 if (li == NULL)
5731 return FAIL;
5732 li->li_tv.v_type = VAR_DICT;
5733 li->li_tv.v_lock = 0;
5734 li->li_tv.vval.v_dict = dict;
5735 list_append(list, li);
5736 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 return OK;
5738}
5739
5740/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005741 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005742 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005743 * Returns FAIL when out of memory.
5744 */
5745 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005746list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005747 list_T *l;
5748 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005749 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005750{
5751 listitem_T *li = listitem_alloc();
5752
5753 if (li == NULL)
5754 return FAIL;
5755 list_append(l, li);
5756 li->li_tv.v_type = VAR_STRING;
5757 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005758 if (str == NULL)
5759 li->li_tv.vval.v_string = NULL;
5760 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005761 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005762 return FAIL;
5763 return OK;
5764}
5765
5766/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005767 * Append "n" to list "l".
5768 * Returns FAIL when out of memory.
5769 */
5770 static int
5771list_append_number(l, n)
5772 list_T *l;
5773 varnumber_T n;
5774{
5775 listitem_T *li;
5776
5777 li = listitem_alloc();
5778 if (li == NULL)
5779 return FAIL;
5780 li->li_tv.v_type = VAR_NUMBER;
5781 li->li_tv.v_lock = 0;
5782 li->li_tv.vval.v_number = n;
5783 list_append(l, li);
5784 return OK;
5785}
5786
5787/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005789 * If "item" is NULL append at the end.
5790 * Return FAIL when out of memory.
5791 */
5792 static int
5793list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005794 list_T *l;
5795 typval_T *tv;
5796 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005797{
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005799
5800 if (ni == NULL)
5801 return FAIL;
5802 copy_tv(tv, &ni->li_tv);
5803 if (item == NULL)
5804 /* Append new item at end of list. */
5805 list_append(l, ni);
5806 else
5807 {
5808 /* Insert new item before existing item. */
5809 ni->li_prev = item->li_prev;
5810 ni->li_next = item;
5811 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005812 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005813 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005814 ++l->lv_idx;
5815 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005817 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005818 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005819 l->lv_idx_item = NULL;
5820 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005822 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005823 }
5824 return OK;
5825}
5826
5827/*
5828 * Extend "l1" with "l2".
5829 * If "bef" is NULL append at the end, otherwise insert before this item.
5830 * Returns FAIL when out of memory.
5831 */
5832 static int
5833list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 list_T *l1;
5835 list_T *l2;
5836 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005837{
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005839
5840 for (item = l2->lv_first; item != NULL; item = item->li_next)
5841 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5842 return FAIL;
5843 return OK;
5844}
5845
5846/*
5847 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5848 * Return FAIL when out of memory.
5849 */
5850 static int
5851list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005852 list_T *l1;
5853 list_T *l2;
5854 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005855{
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005857
5858 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005859 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005860 if (l == NULL)
5861 return FAIL;
5862 tv->v_type = VAR_LIST;
5863 tv->vval.v_list = l;
5864
5865 /* append all items from the second list */
5866 return list_extend(l, l2, NULL);
5867}
5868
5869/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005870 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005872 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 * Returns NULL when out of memory.
5874 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005876list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880{
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *copy;
5882 listitem_T *item;
5883 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884
5885 if (orig == NULL)
5886 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887
5888 copy = list_alloc();
5889 if (copy != NULL)
5890 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005891 if (copyID != 0)
5892 {
5893 /* Do this before adding the items, because one of the items may
5894 * refer back to this list. */
5895 orig->lv_copyID = copyID;
5896 orig->lv_copylist = copy;
5897 }
5898 for (item = orig->lv_first; item != NULL && !got_int;
5899 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 {
5901 ni = listitem_alloc();
5902 if (ni == NULL)
5903 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005904 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005905 {
5906 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5907 {
5908 vim_free(ni);
5909 break;
5910 }
5911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005913 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 list_append(copy, ni);
5915 }
5916 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005917 if (item != NULL)
5918 {
5919 list_unref(copy);
5920 copy = NULL;
5921 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 }
5923
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 return copy;
5925}
5926
5927/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005929 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005932list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 list_T *l;
5934 listitem_T *item;
5935 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005936{
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005939 /* notify watchers */
5940 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005942 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005943 list_fix_watch(l, ip);
5944 if (ip == item2)
5945 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005947
5948 if (item2->li_next == NULL)
5949 l->lv_last = item->li_prev;
5950 else
5951 item2->li_next->li_prev = item->li_prev;
5952 if (item->li_prev == NULL)
5953 l->lv_first = item2->li_next;
5954 else
5955 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005956 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957}
5958
5959/*
5960 * Return an allocated string with the string representation of a list.
5961 * May return NULL.
5962 */
5963 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005964list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005966 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967{
5968 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969
5970 if (tv->vval.v_list == NULL)
5971 return NULL;
5972 ga_init2(&ga, (int)sizeof(char), 80);
5973 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005974 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 {
5976 vim_free(ga.ga_data);
5977 return NULL;
5978 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 ga_append(&ga, ']');
5980 ga_append(&ga, NUL);
5981 return (char_u *)ga.ga_data;
5982}
5983
5984/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 * Join list "l" into a string in "*gap", using separator "sep".
5986 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005988 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005989 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005990list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 char_u *sep;
5994 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996{
5997 int first = TRUE;
5998 char_u *tofree;
5999 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 char_u *s;
6002
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006003 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006004 {
6005 if (first)
6006 first = FALSE;
6007 else
6008 ga_concat(gap, sep);
6009
6010 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006011 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006012 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006014 if (s != NULL)
6015 ga_concat(gap, s);
6016 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006017 if (s == NULL)
6018 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006019 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021}
6022
6023/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 * Garbage collection for lists and dictionaries.
6025 *
6026 * We use reference counts to be able to free most items right away when they
6027 * are no longer used. But for composite items it's possible that it becomes
6028 * unused while the reference count is > 0: When there is a recursive
6029 * reference. Example:
6030 * :let l = [1, 2, 3]
6031 * :let d = {9: l}
6032 * :let l[1] = d
6033 *
6034 * Since this is quite unusual we handle this with garbage collection: every
6035 * once in a while find out which lists and dicts are not referenced from any
6036 * variable.
6037 *
6038 * Here is a good reference text about garbage collection (refers to Python
6039 * but it applies to all reference-counting mechanisms):
6040 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042
6043/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006044 * Do garbage collection for lists and dicts.
6045 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 int
6048garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049{
6050 dict_T *dd;
6051 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006052 int copyID = ++current_copyID;
6053 buf_T *buf;
6054 win_T *wp;
6055 int i;
6056 funccall_T *fc;
6057 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006058#ifdef FEAT_WINDOWS
6059 tabpage_T *tp;
6060#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006061
6062 /*
6063 * 1. Go through all accessible variables and mark all lists and dicts
6064 * with copyID.
6065 */
6066 /* script-local variables */
6067 for (i = 1; i <= ga_scripts.ga_len; ++i)
6068 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6069
6070 /* buffer-local variables */
6071 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6072 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6073
6074 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006075 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6077
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006078#ifdef FEAT_WINDOWS
6079 /* tabpage-local variables */
6080 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6081 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6082#endif
6083
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006084 /* global variables */
6085 set_ref_in_ht(&globvarht, copyID);
6086
6087 /* function-local variables */
6088 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6089 {
6090 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6091 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6092 }
6093
6094 /*
6095 * 2. Go through the list of dicts and free items without the copyID.
6096 */
6097 for (dd = first_dict; dd != NULL; )
6098 if (dd->dv_copyID != copyID)
6099 {
6100 dict_free(dd);
6101 did_free = TRUE;
6102
6103 /* restart, next dict may also have been freed */
6104 dd = first_dict;
6105 }
6106 else
6107 dd = dd->dv_used_next;
6108
6109 /*
6110 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006111 * But don't free a list that has a watcher (used in a for loop), these
6112 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 */
6114 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006115 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 {
6117 list_free(ll);
6118 did_free = TRUE;
6119
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006120 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 ll = first_list;
6122 }
6123 else
6124 ll = ll->lv_used_next;
6125
6126 return did_free;
6127}
6128
6129/*
6130 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6131 */
6132 static void
6133set_ref_in_ht(ht, copyID)
6134 hashtab_T *ht;
6135 int copyID;
6136{
6137 int todo;
6138 hashitem_T *hi;
6139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006140 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006141 for (hi = ht->ht_array; todo > 0; ++hi)
6142 if (!HASHITEM_EMPTY(hi))
6143 {
6144 --todo;
6145 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6146 }
6147}
6148
6149/*
6150 * Mark all lists and dicts referenced through list "l" with "copyID".
6151 */
6152 static void
6153set_ref_in_list(l, copyID)
6154 list_T *l;
6155 int copyID;
6156{
6157 listitem_T *li;
6158
6159 for (li = l->lv_first; li != NULL; li = li->li_next)
6160 set_ref_in_item(&li->li_tv, copyID);
6161}
6162
6163/*
6164 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6165 */
6166 static void
6167set_ref_in_item(tv, copyID)
6168 typval_T *tv;
6169 int copyID;
6170{
6171 dict_T *dd;
6172 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006173
6174 switch (tv->v_type)
6175 {
6176 case VAR_DICT:
6177 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006178 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006179 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006180 /* Didn't see this dict yet. */
6181 dd->dv_copyID = copyID;
6182 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006183 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006184 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006185
6186 case VAR_LIST:
6187 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006189 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190 /* Didn't see this list yet. */
6191 ll->lv_copyID = copyID;
6192 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006193 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006195 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006197}
6198
6199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006200 * Allocate an empty header for a dictionary.
6201 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006202 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006203dict_alloc()
6204{
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206
Bram Moolenaar33570922005-01-25 22:26:29 +00006207 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006208 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006209 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006210 /* Add the list to the hashtable for garbage collection. */
6211 if (first_dict != NULL)
6212 first_dict->dv_used_prev = d;
6213 d->dv_used_next = first_dict;
6214 d->dv_used_prev = NULL;
6215
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006217 d->dv_lock = 0;
6218 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006219 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006222}
6223
6224/*
6225 * Unreference a Dictionary: decrement the reference count and free it when it
6226 * becomes zero.
6227 */
6228 static void
6229dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006231{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006232 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6233 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234}
6235
6236/*
6237 * Free a Dictionary, including all items it contains.
6238 * Ignores the reference count.
6239 */
6240 static void
6241dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006242 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006243{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006244 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006246 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006247
Bram Moolenaard9fba312005-06-26 22:34:35 +00006248 /* Avoid that recursive reference to the dict frees us again. */
6249 d->dv_refcount = DEL_REFCOUNT;
6250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006251 /* Remove the dict from the list of dicts for garbage collection. */
6252 if (d->dv_used_prev == NULL)
6253 first_dict = d->dv_used_next;
6254 else
6255 d->dv_used_prev->dv_used_next = d->dv_used_next;
6256 if (d->dv_used_next != NULL)
6257 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6258
6259 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006260 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006261 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 if (!HASHITEM_EMPTY(hi))
6265 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006266 /* Remove the item before deleting it, just in case there is
6267 * something recursive causing trouble. */
6268 di = HI2DI(hi);
6269 hash_remove(&d->dv_hashtab, hi);
6270 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 --todo;
6272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006273 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006275 vim_free(d);
6276}
6277
6278/*
6279 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 * The "key" is copied to the new item.
6281 * Note that the value of the item "di_tv" still needs to be initialized!
6282 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285dictitem_alloc(key)
6286 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287{
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006290 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006291 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006293 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006294 di->di_flags = 0;
6295 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006297}
6298
6299/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 * Make a copy of a Dictionary item.
6301 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305{
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006308 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6309 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310 if (di != NULL)
6311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006313 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314 copy_tv(&org->di_tv, &di->di_tv);
6315 }
6316 return di;
6317}
6318
6319/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 * Remove item "item" from Dictionary "dict" and free it.
6321 */
6322 static void
6323dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 dict_T *dict;
6325 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326{
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 if (HASHITEM_EMPTY(hi))
6331 EMSG2(_(e_intern2), "dictitem_remove()");
6332 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 dictitem_free(item);
6335}
6336
6337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 * Free a dict item. Also clears the value.
6339 */
6340 static void
6341dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 clear_tv(&item->di_tv);
6345 vim_free(item);
6346}
6347
6348/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006349 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6350 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 * Returns NULL when out of memory.
6353 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359{
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 dict_T *copy;
6361 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006362 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364
6365 if (orig == NULL)
6366 return NULL;
6367
6368 copy = dict_alloc();
6369 if (copy != NULL)
6370 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371 if (copyID != 0)
6372 {
6373 orig->dv_copyID = copyID;
6374 orig->dv_copydict = copy;
6375 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006376 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006381 --todo;
6382
6383 di = dictitem_alloc(hi->hi_key);
6384 if (di == NULL)
6385 break;
6386 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 {
6388 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6389 copyID) == FAIL)
6390 {
6391 vim_free(di);
6392 break;
6393 }
6394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 else
6396 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6397 if (dict_add(copy, di) == FAIL)
6398 {
6399 dictitem_free(di);
6400 break;
6401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404
Bram Moolenaare9a41262005-01-15 22:18:47 +00006405 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 if (todo > 0)
6407 {
6408 dict_unref(copy);
6409 copy = NULL;
6410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006411 }
6412
6413 return copy;
6414}
6415
6416/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006420 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dict_T *d;
6423 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424{
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426}
6427
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006429 * Add a number or string entry to dictionary "d".
6430 * When "str" is NULL use number "nr", otherwise use "str".
6431 * Returns FAIL when out of memory and when key already exists.
6432 */
6433 int
6434dict_add_nr_str(d, key, nr, str)
6435 dict_T *d;
6436 char *key;
6437 long nr;
6438 char_u *str;
6439{
6440 dictitem_T *item;
6441
6442 item = dictitem_alloc((char_u *)key);
6443 if (item == NULL)
6444 return FAIL;
6445 item->di_tv.v_lock = 0;
6446 if (str == NULL)
6447 {
6448 item->di_tv.v_type = VAR_NUMBER;
6449 item->di_tv.vval.v_number = nr;
6450 }
6451 else
6452 {
6453 item->di_tv.v_type = VAR_STRING;
6454 item->di_tv.vval.v_string = vim_strsave(str);
6455 }
6456 if (dict_add(d, item) == FAIL)
6457 {
6458 dictitem_free(item);
6459 return FAIL;
6460 }
6461 return OK;
6462}
6463
6464/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 * Get the number of items in a Dictionary.
6466 */
6467 static long
6468dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006471 if (d == NULL)
6472 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006473 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474}
6475
6476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477 * Find item "key[len]" in Dictionary "d".
6478 * If "len" is negative use strlen(key).
6479 * Returns NULL when not found.
6480 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006482dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006483 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 char_u *key;
6485 int len;
6486{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487#define AKEYLEN 200
6488 char_u buf[AKEYLEN];
6489 char_u *akey;
6490 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006492
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006493 if (len < 0)
6494 akey = key;
6495 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 tofree = akey = vim_strnsave(key, len);
6498 if (akey == NULL)
6499 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006500 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 else
6502 {
6503 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006504 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006505 akey = buf;
6506 }
6507
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006509 vim_free(tofree);
6510 if (HASHITEM_EMPTY(hi))
6511 return NULL;
6512 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513}
6514
6515/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006516 * Get a string item from a dictionary.
6517 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006518 * Returns NULL if the entry doesn't exist or out of memory.
6519 */
6520 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006521get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006522 dict_T *d;
6523 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006524 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006525{
6526 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006527 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006528
6529 di = dict_find(d, key, -1);
6530 if (di == NULL)
6531 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006532 s = get_tv_string(&di->di_tv);
6533 if (save && s != NULL)
6534 s = vim_strsave(s);
6535 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006536}
6537
6538/*
6539 * Get a number item from a dictionary.
6540 * Returns 0 if the entry doesn't exist or out of memory.
6541 */
6542 long
6543get_dict_number(d, key)
6544 dict_T *d;
6545 char_u *key;
6546{
6547 dictitem_T *di;
6548
6549 di = dict_find(d, key, -1);
6550 if (di == NULL)
6551 return 0;
6552 return get_tv_number(&di->di_tv);
6553}
6554
6555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 * Return an allocated string with the string representation of a Dictionary.
6557 * May return NULL.
6558 */
6559 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006560dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006562 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563{
6564 garray_T ga;
6565 int first = TRUE;
6566 char_u *tofree;
6567 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006573 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574 return NULL;
6575 ga_init2(&ga, (int)sizeof(char), 80);
6576 ga_append(&ga, '{');
6577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006578 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 --todo;
6584
6585 if (first)
6586 first = FALSE;
6587 else
6588 ga_concat(&ga, (char_u *)", ");
6589
6590 tofree = string_quote(hi->hi_key, FALSE);
6591 if (tofree != NULL)
6592 {
6593 ga_concat(&ga, tofree);
6594 vim_free(tofree);
6595 }
6596 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006598 if (s != NULL)
6599 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006600 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 if (s == NULL)
6602 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006604 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 if (todo > 0)
6606 {
6607 vim_free(ga.ga_data);
6608 return NULL;
6609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610
6611 ga_append(&ga, '}');
6612 ga_append(&ga, NUL);
6613 return (char_u *)ga.ga_data;
6614}
6615
6616/*
6617 * Allocate a variable for a Dictionary and fill it from "*arg".
6618 * Return OK or FAIL. Returns NOTDONE for {expr}.
6619 */
6620 static int
6621get_dict_tv(arg, rettv, evaluate)
6622 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 int evaluate;
6625{
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 dict_T *d = NULL;
6627 typval_T tvkey;
6628 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006630 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006631 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006632 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633
6634 /*
6635 * First check if it's not a curly-braces thing: {expr}.
6636 * Must do this without evaluating, otherwise a function may be called
6637 * twice. Unfortunately this means we need to call eval1() twice for the
6638 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006641 if (*start != '}')
6642 {
6643 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6644 return FAIL;
6645 if (*start == '}')
6646 return NOTDONE;
6647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648
6649 if (evaluate)
6650 {
6651 d = dict_alloc();
6652 if (d == NULL)
6653 return FAIL;
6654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006655 tvkey.v_type = VAR_UNKNOWN;
6656 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657
6658 *arg = skipwhite(*arg + 1);
6659 while (**arg != '}' && **arg != NUL)
6660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 goto failret;
6663 if (**arg != ':')
6664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006665 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006666 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006667 goto failret;
6668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006669 key = get_tv_string_buf_chk(&tvkey, buf);
6670 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006672 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6673 if (key != NULL)
6674 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006675 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 goto failret;
6677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678
6679 *arg = skipwhite(*arg + 1);
6680 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6681 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 goto failret;
6684 }
6685 if (evaluate)
6686 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006687 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688 if (item != NULL)
6689 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006690 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006691 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 clear_tv(&tv);
6693 goto failret;
6694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 item = dictitem_alloc(key);
6696 clear_tv(&tvkey);
6697 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006700 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006701 if (dict_add(d, item) == FAIL)
6702 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703 }
6704 }
6705
6706 if (**arg == '}')
6707 break;
6708 if (**arg != ',')
6709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006710 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711 goto failret;
6712 }
6713 *arg = skipwhite(*arg + 1);
6714 }
6715
6716 if (**arg != '}')
6717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006718 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719failret:
6720 if (evaluate)
6721 dict_free(d);
6722 return FAIL;
6723 }
6724
6725 *arg = skipwhite(*arg + 1);
6726 if (evaluate)
6727 {
6728 rettv->v_type = VAR_DICT;
6729 rettv->vval.v_dict = d;
6730 ++d->dv_refcount;
6731 }
6732
6733 return OK;
6734}
6735
Bram Moolenaar8c711452005-01-14 21:53:12 +00006736/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006737 * Return a string with the string representation of a variable.
6738 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006739 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006741 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006742 * May return NULL;
6743 */
6744 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006745echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006747 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006749 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 static int recurse = 0;
6752 char_u *r = NULL;
6753
Bram Moolenaar33570922005-01-25 22:26:29 +00006754 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006756 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006757 *tofree = NULL;
6758 return NULL;
6759 }
6760 ++recurse;
6761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762 switch (tv->v_type)
6763 {
6764 case VAR_FUNC:
6765 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 r = tv->vval.v_string;
6767 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006769 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006770 if (tv->vval.v_list == NULL)
6771 {
6772 *tofree = NULL;
6773 r = NULL;
6774 }
6775 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6776 {
6777 *tofree = NULL;
6778 r = (char_u *)"[...]";
6779 }
6780 else
6781 {
6782 tv->vval.v_list->lv_copyID = copyID;
6783 *tofree = list2string(tv, copyID);
6784 r = *tofree;
6785 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006786 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006787
Bram Moolenaar8c711452005-01-14 21:53:12 +00006788 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006789 if (tv->vval.v_dict == NULL)
6790 {
6791 *tofree = NULL;
6792 r = NULL;
6793 }
6794 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6795 {
6796 *tofree = NULL;
6797 r = (char_u *)"{...}";
6798 }
6799 else
6800 {
6801 tv->vval.v_dict->dv_copyID = copyID;
6802 *tofree = dict2string(tv, copyID);
6803 r = *tofree;
6804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 case VAR_STRING:
6808 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 *tofree = NULL;
6810 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006811 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006817
6818 --recurse;
6819 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820}
6821
6822/*
6823 * Return a string with the string representation of a variable.
6824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6825 * "numbuf" is used for a number.
6826 * Puts quotes around strings, so that they can be parsed back by eval().
6827 * May return NULL;
6828 */
6829 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006830tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006831 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 char_u **tofree;
6833 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006834 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835{
6836 switch (tv->v_type)
6837 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838 case VAR_FUNC:
6839 *tofree = string_quote(tv->vval.v_string, TRUE);
6840 return *tofree;
6841 case VAR_STRING:
6842 *tofree = string_quote(tv->vval.v_string, FALSE);
6843 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006846 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006851 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006852}
6853
6854/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 * Return string "str" in ' quotes, doubling ' characters.
6856 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006857 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858 */
6859 static char_u *
6860string_quote(str, function)
6861 char_u *str;
6862 int function;
6863{
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006865 char_u *p, *r, *s;
6866
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 len = (function ? 13 : 3);
6868 if (str != NULL)
6869 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006870 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 for (p = str; *p != NUL; mb_ptr_adv(p))
6872 if (*p == '\'')
6873 ++len;
6874 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875 s = r = alloc(len);
6876 if (r != NULL)
6877 {
6878 if (function)
6879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006881 r += 10;
6882 }
6883 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006885 if (str != NULL)
6886 for (p = str; *p != NUL; )
6887 {
6888 if (*p == '\'')
6889 *r++ = '\'';
6890 MB_COPY_CHAR(p, r);
6891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 if (function)
6894 *r++ = ')';
6895 *r++ = NUL;
6896 }
6897 return s;
6898}
6899
6900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 * Get the value of an environment variable.
6902 * "arg" is pointing to the '$'. It is advanced to after the name.
6903 * If the environment variable was not set, silently assume it is empty.
6904 * Always return OK.
6905 */
6906 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006907get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 int evaluate;
6911{
6912 char_u *string = NULL;
6913 int len;
6914 int cc;
6915 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006916 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917
6918 ++*arg;
6919 name = *arg;
6920 len = get_env_len(arg);
6921 if (evaluate)
6922 {
6923 if (len != 0)
6924 {
6925 cc = name[len];
6926 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006927 /* first try vim_getenv(), fast for normal environment vars */
6928 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006930 {
6931 if (!mustfree)
6932 string = vim_strsave(string);
6933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 else
6935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006936 if (mustfree)
6937 vim_free(string);
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 /* next try expanding things like $VIM and ${HOME} */
6940 string = expand_env_save(name - 1);
6941 if (string != NULL && *string == '$')
6942 {
6943 vim_free(string);
6944 string = NULL;
6945 }
6946 }
6947 name[len] = cc;
6948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006949 rettv->v_type = VAR_STRING;
6950 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952
6953 return OK;
6954}
6955
6956/*
6957 * Array with names and number of arguments of all internal functions
6958 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6959 */
6960static struct fst
6961{
6962 char *f_name; /* function name */
6963 char f_min_argc; /* minimal number of arguments */
6964 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006966 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967} functions[] =
6968{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006969 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {"append", 2, 2, f_append},
6971 {"argc", 0, 0, f_argc},
6972 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006973 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006975 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {"bufexists", 1, 1, f_bufexists},
6977 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6978 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6979 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6980 {"buflisted", 1, 1, f_buflisted},
6981 {"bufloaded", 1, 1, f_bufloaded},
6982 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006983 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {"bufwinnr", 1, 1, f_bufwinnr},
6985 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006986 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006988 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {"char2nr", 1, 1, f_char2nr},
6990 {"cindent", 1, 1, f_cindent},
6991 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006992#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006993 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006994 {"complete_add", 1, 1, f_complete_add},
6995 {"complete_check", 0, 0, f_complete_check},
6996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006998 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007001 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007002 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"delete", 1, 1, f_delete},
7004 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007005 {"diff_filler", 1, 1, f_diff_filler},
7006 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007007 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007009 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"eventhandler", 0, 0, f_eventhandler},
7011 {"executable", 1, 1, f_executable},
7012 {"exists", 1, 1, f_exists},
7013 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007014 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007015 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7017 {"filereadable", 1, 1, f_filereadable},
7018 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007019 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007020 {"finddir", 1, 3, f_finddir},
7021 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {"fnamemodify", 2, 2, f_fnamemodify},
7023 {"foldclosed", 1, 1, f_foldclosed},
7024 {"foldclosedend", 1, 1, f_foldclosedend},
7025 {"foldlevel", 1, 1, f_foldlevel},
7026 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007027 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007029 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007031 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007032 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {"getbufvar", 2, 2, f_getbufvar},
7034 {"getchar", 0, 1, f_getchar},
7035 {"getcharmod", 0, 0, f_getcharmod},
7036 {"getcmdline", 0, 0, f_getcmdline},
7037 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007038 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007040 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007041 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"getfsize", 1, 1, f_getfsize},
7043 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007044 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007045 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007046 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007047 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007048 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007049 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007051 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"getwinposx", 0, 0, f_getwinposx},
7053 {"getwinposy", 0, 0, f_getwinposy},
7054 {"getwinvar", 2, 2, f_getwinvar},
7055 {"glob", 1, 1, f_glob},
7056 {"globpath", 2, 2, f_globpath},
7057 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007058 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007059 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7061 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7062 {"histadd", 2, 2, f_histadd},
7063 {"histdel", 1, 2, f_histdel},
7064 {"histget", 1, 2, f_histget},
7065 {"histnr", 1, 1, f_histnr},
7066 {"hlID", 1, 1, f_hlID},
7067 {"hlexists", 1, 1, f_hlexists},
7068 {"hostname", 0, 0, f_hostname},
7069 {"iconv", 3, 3, f_iconv},
7070 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007071 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007074 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"inputrestore", 0, 0, f_inputrestore},
7076 {"inputsave", 0, 0, f_inputsave},
7077 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007078 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007080 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007082 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"libcall", 3, 3, f_libcall},
7087 {"libcallnr", 3, 3, f_libcallnr},
7088 {"line", 1, 1, f_line},
7089 {"line2byte", 1, 1, f_line2byte},
7090 {"lispindent", 1, 1, f_lispindent},
7091 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007092 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007093 {"maparg", 1, 3, f_maparg},
7094 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007095 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007097 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007098 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007099 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007100 {"max", 1, 1, f_max},
7101 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007102#ifdef vim_mkdir
7103 {"mkdir", 1, 3, f_mkdir},
7104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"mode", 0, 0, f_mode},
7106 {"nextnonblank", 1, 1, f_nextnonblank},
7107 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007108 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007110 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007111 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007113 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007114 {"reltime", 0, 2, f_reltime},
7115 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"remote_expr", 2, 3, f_remote_expr},
7117 {"remote_foreground", 1, 1, f_remote_foreground},
7118 {"remote_peek", 1, 2, f_remote_peek},
7119 {"remote_read", 1, 1, f_remote_read},
7120 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007121 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007123 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007125 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007126 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007127 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007128 {"searchpair", 3, 6, f_searchpair},
7129 {"searchpairpos", 3, 6, f_searchpairpos},
7130 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"server2client", 2, 2, f_server2client},
7132 {"serverlist", 0, 0, f_serverlist},
7133 {"setbufvar", 3, 3, f_setbufvar},
7134 {"setcmdpos", 1, 1, f_setcmdpos},
7135 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007136 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007137 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007138 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007140 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {"setwinvar", 3, 3, f_setwinvar},
7142 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007143 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007144 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007145 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007146 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007147 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007148 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#ifdef HAVE_STRFTIME
7150 {"strftime", 1, 2, f_strftime},
7151#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007153 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {"strlen", 1, 1, f_strlen},
7155 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007156 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {"strtrans", 1, 1, f_strtrans},
7158 {"submatch", 1, 1, f_submatch},
7159 {"substitute", 4, 4, f_substitute},
7160 {"synID", 3, 3, f_synID},
7161 {"synIDattr", 2, 3, f_synIDattr},
7162 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007163 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007165 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007167 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007168 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007170 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"tolower", 1, 1, f_tolower},
7172 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007173 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"virtcol", 1, 1, f_virtcol},
7177 {"visualmode", 0, 1, f_visualmode},
7178 {"winbufnr", 1, 1, f_winbufnr},
7179 {"wincol", 0, 0, f_wincol},
7180 {"winheight", 1, 1, f_winheight},
7181 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007182 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007184 {"winrestview", 1, 1, f_winrestview},
7185 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007187 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188};
7189
7190#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7191
7192/*
7193 * Function given to ExpandGeneric() to obtain the list of internal
7194 * or user defined function names.
7195 */
7196 char_u *
7197get_function_name(xp, idx)
7198 expand_T *xp;
7199 int idx;
7200{
7201 static int intidx = -1;
7202 char_u *name;
7203
7204 if (idx == 0)
7205 intidx = -1;
7206 if (intidx < 0)
7207 {
7208 name = get_user_func_name(xp, idx);
7209 if (name != NULL)
7210 return name;
7211 }
7212 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7213 {
7214 STRCPY(IObuff, functions[intidx].f_name);
7215 STRCAT(IObuff, "(");
7216 if (functions[intidx].f_max_argc == 0)
7217 STRCAT(IObuff, ")");
7218 return IObuff;
7219 }
7220
7221 return NULL;
7222}
7223
7224/*
7225 * Function given to ExpandGeneric() to obtain the list of internal or
7226 * user defined variable or function names.
7227 */
7228/*ARGSUSED*/
7229 char_u *
7230get_expr_name(xp, idx)
7231 expand_T *xp;
7232 int idx;
7233{
7234 static int intidx = -1;
7235 char_u *name;
7236
7237 if (idx == 0)
7238 intidx = -1;
7239 if (intidx < 0)
7240 {
7241 name = get_function_name(xp, idx);
7242 if (name != NULL)
7243 return name;
7244 }
7245 return get_user_var_name(xp, ++intidx);
7246}
7247
7248#endif /* FEAT_CMDL_COMPL */
7249
7250/*
7251 * Find internal function in table above.
7252 * Return index, or -1 if not found
7253 */
7254 static int
7255find_internal_func(name)
7256 char_u *name; /* name of the function */
7257{
7258 int first = 0;
7259 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7260 int cmp;
7261 int x;
7262
7263 /*
7264 * Find the function name in the table. Binary search.
7265 */
7266 while (first <= last)
7267 {
7268 x = first + ((unsigned)(last - first) >> 1);
7269 cmp = STRCMP(name, functions[x].f_name);
7270 if (cmp < 0)
7271 last = x - 1;
7272 else if (cmp > 0)
7273 first = x + 1;
7274 else
7275 return x;
7276 }
7277 return -1;
7278}
7279
7280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007281 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7282 * name it contains, otherwise return "name".
7283 */
7284 static char_u *
7285deref_func_name(name, lenp)
7286 char_u *name;
7287 int *lenp;
7288{
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 int cc;
7291
7292 cc = name[*lenp];
7293 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007294 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007295 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 {
7300 *lenp = 0;
7301 return (char_u *)""; /* just in case */
7302 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 }
7306
7307 return name;
7308}
7309
7310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 * Allocate a variable for the result of a function.
7312 * Return OK or FAIL.
7313 */
7314 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7316 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 char_u *name; /* name of the function */
7318 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 char_u **arg; /* argument, pointing to the '(' */
7321 linenr_T firstline; /* first line of range */
7322 linenr_T lastline; /* last line of range */
7323 int *doesrange; /* return: function handled range */
7324 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327 char_u *argp;
7328 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007329 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 int argcount = 0; /* number of arguments found */
7331
7332 /*
7333 * Get the arguments.
7334 */
7335 argp = *arg;
7336 while (argcount < MAX_FUNC_ARGS)
7337 {
7338 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7339 if (*argp == ')' || *argp == ',' || *argp == NUL)
7340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7342 {
7343 ret = FAIL;
7344 break;
7345 }
7346 ++argcount;
7347 if (*argp != ',')
7348 break;
7349 }
7350 if (*argp == ')')
7351 ++argp;
7352 else
7353 ret = FAIL;
7354
7355 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007356 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 {
7360 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007363 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
7366 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
7369 *arg = skipwhite(argp);
7370 return ret;
7371}
7372
7373
7374/*
7375 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007376 * Return OK when the function can't be called, FAIL otherwise.
7377 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
7379 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007380call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 char_u *name; /* name of the function */
7383 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007386 typval_T *argvars; /* vars for arguments, must have "argcount"
7387 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 linenr_T firstline; /* first line of range */
7389 linenr_T lastline; /* last line of range */
7390 int *doesrange; /* return: function handled range */
7391 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#define ERROR_UNKNOWN 0
7396#define ERROR_TOOMANY 1
7397#define ERROR_TOOFEW 2
7398#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399#define ERROR_DICT 4
7400#define ERROR_NONE 5
7401#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int error = ERROR_NONE;
7403 int i;
7404 int llen;
7405 ufunc_T *fp;
7406 int cc;
7407#define FLEN_FIXED 40
7408 char_u fname_buf[FLEN_FIXED + 1];
7409 char_u *fname;
7410
7411 /*
7412 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7413 * Change <SNR>123_name() to K_SNR 123_name().
7414 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7415 */
7416 cc = name[len];
7417 name[len] = NUL;
7418 llen = eval_fname_script(name);
7419 if (llen > 0)
7420 {
7421 fname_buf[0] = K_SPECIAL;
7422 fname_buf[1] = KS_EXTRA;
7423 fname_buf[2] = (int)KE_SNR;
7424 i = 3;
7425 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7426 {
7427 if (current_SID <= 0)
7428 error = ERROR_SCRIPT;
7429 else
7430 {
7431 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7432 i = (int)STRLEN(fname_buf);
7433 }
7434 }
7435 if (i + STRLEN(name + llen) < FLEN_FIXED)
7436 {
7437 STRCPY(fname_buf + i, name + llen);
7438 fname = fname_buf;
7439 }
7440 else
7441 {
7442 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7443 if (fname == NULL)
7444 error = ERROR_OTHER;
7445 else
7446 {
7447 mch_memmove(fname, fname_buf, (size_t)i);
7448 STRCPY(fname + i, name + llen);
7449 }
7450 }
7451 }
7452 else
7453 fname = name;
7454
7455 *doesrange = FALSE;
7456
7457
7458 /* execute the function if no errors detected and executing */
7459 if (evaluate && error == ERROR_NONE)
7460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 error = ERROR_UNKNOWN;
7463
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007464 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
7466 /*
7467 * User defined function.
7468 */
7469 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007472 /* Trigger FuncUndefined event, may load the function. */
7473 if (fp == NULL
7474 && apply_autocmds(EVENT_FUNCUNDEFINED,
7475 fname, fname, TRUE, NULL)
7476 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007478 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 fp = find_func(fname);
7480 }
7481#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007482 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007483 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007484 {
7485 /* loaded a package, search for the function again */
7486 fp = find_func(fname);
7487 }
7488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 if (fp != NULL)
7490 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007491 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007493 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007495 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007497 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007498 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 else
7500 {
7501 /*
7502 * Call the user function.
7503 * Save and restore search patterns, script variables and
7504 * redo buffer.
7505 */
7506 save_search_patterns();
7507 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007508 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007511 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7512 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7513 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007514 /* Function was unreferenced while being used, free it
7515 * now. */
7516 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 restoreRedobuff();
7518 restore_search_patterns();
7519 error = ERROR_NONE;
7520 }
7521 }
7522 }
7523 else
7524 {
7525 /*
7526 * Find the function name in the table, call its implementation.
7527 */
7528 i = find_internal_func(fname);
7529 if (i >= 0)
7530 {
7531 if (argcount < functions[i].f_min_argc)
7532 error = ERROR_TOOFEW;
7533 else if (argcount > functions[i].f_max_argc)
7534 error = ERROR_TOOMANY;
7535 else
7536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007537 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 error = ERROR_NONE;
7540 }
7541 }
7542 }
7543 /*
7544 * The function call (or "FuncUndefined" autocommand sequence) might
7545 * have been aborted by an error, an interrupt, or an explicitly thrown
7546 * exception that has not been caught so far. This situation can be
7547 * tested for by calling aborting(). For an error in an internal
7548 * function or for the "E132" error in call_user_func(), however, the
7549 * throw point at which the "force_abort" flag (temporarily reset by
7550 * emsg()) is normally updated has not been reached yet. We need to
7551 * update that flag first to make aborting() reliable.
7552 */
7553 update_force_abort();
7554 }
7555 if (error == ERROR_NONE)
7556 ret = OK;
7557
7558 /*
7559 * Report an error unless the argument evaluation or function call has been
7560 * cancelled due to an aborting error, an interrupt, or an exception.
7561 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 if (!aborting())
7563 {
7564 switch (error)
7565 {
7566 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007567 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 break;
7569 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007570 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 break;
7572 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007573 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 name);
7575 break;
7576 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 name);
7579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007581 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 name);
7583 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 }
7585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
7587 name[len] = cc;
7588 if (fname != name && fname != fname_buf)
7589 vim_free(fname);
7590
7591 return ret;
7592}
7593
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007594/*
7595 * Give an error message with a function name. Handle <SNR> things.
7596 */
7597 static void
7598emsg_funcname(msg, name)
7599 char *msg;
7600 char_u *name;
7601{
7602 char_u *p;
7603
7604 if (*name == K_SPECIAL)
7605 p = concat_str((char_u *)"<SNR>", name + 3);
7606 else
7607 p = name;
7608 EMSG2(_(msg), p);
7609 if (p != name)
7610 vim_free(p);
7611}
7612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613/*********************************************
7614 * Implementation of the built-in functions
7615 */
7616
7617/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007618 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 */
7620 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007621f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 typval_T *argvars;
7623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007630 if ((l = argvars[0].vval.v_list) != NULL
7631 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7632 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007633 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 }
7635 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007636 EMSG(_(e_listreq));
7637}
7638
7639/*
7640 * "append(lnum, string/list)" function
7641 */
7642 static void
7643f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 typval_T *argvars;
7645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007646{
7647 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007648 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 list_T *l = NULL;
7650 listitem_T *li = NULL;
7651 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007652 long added = 0;
7653
Bram Moolenaar0d660222005-01-07 21:51:51 +00007654 lnum = get_tv_lnum(argvars);
7655 if (lnum >= 0
7656 && lnum <= curbuf->b_ml.ml_line_count
7657 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007659 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 l = argvars[1].vval.v_list;
7662 if (l == NULL)
7663 return;
7664 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007666 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007667 for (;;)
7668 {
7669 if (l == NULL)
7670 tv = &argvars[1]; /* append a string */
7671 else if (li == NULL)
7672 break; /* end of list */
7673 else
7674 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007675 line = get_tv_string_chk(tv);
7676 if (line == NULL) /* type error */
7677 {
7678 rettv->vval.v_number = 1; /* Failed */
7679 break;
7680 }
7681 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007682 ++added;
7683 if (l == NULL)
7684 break;
7685 li = li->li_next;
7686 }
7687
7688 appended_lines_mark(lnum, added);
7689 if (curwin->w_cursor.lnum > lnum)
7690 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007692 else
7693 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694}
7695
7696/*
7697 * "argc()" function
7698 */
7699/* ARGSUSED */
7700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 typval_T *argvars;
7703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707
7708/*
7709 * "argidx()" function
7710 */
7711/* ARGSUSED */
7712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 typval_T *argvars;
7715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
7720/*
7721 * "argv(nr)" function
7722 */
7723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *argvars;
7726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 int idx;
7729
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007730 if (argvars[0].v_type != VAR_UNKNOWN)
7731 {
7732 idx = get_tv_number_chk(&argvars[0], NULL);
7733 if (idx >= 0 && idx < ARGCOUNT)
7734 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7735 else
7736 rettv->vval.v_string = NULL;
7737 rettv->v_type = VAR_STRING;
7738 }
7739 else if (rettv_list_alloc(rettv) == OK)
7740 for (idx = 0; idx < ARGCOUNT; ++idx)
7741 list_append_string(rettv->vval.v_list,
7742 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
7746 * "browse(save, title, initdir, default)" function
7747 */
7748/* ARGSUSED */
7749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *argvars;
7752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754#ifdef FEAT_BROWSE
7755 int save;
7756 char_u *title;
7757 char_u *initdir;
7758 char_u *defname;
7759 char_u buf[NUMBUFLEN];
7760 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007761 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 save = get_tv_number_chk(&argvars[0], &error);
7764 title = get_tv_string_chk(&argvars[1]);
7765 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7766 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007768 if (error || title == NULL || initdir == NULL || defname == NULL)
7769 rettv->vval.v_string = NULL;
7770 else
7771 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007772 do_browse(save ? BROWSE_SAVE : 0,
7773 title, defname, NULL, initdir, NULL, curbuf);
7774#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007776#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007778}
7779
7780/*
7781 * "browsedir(title, initdir)" function
7782 */
7783/* ARGSUSED */
7784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T *argvars;
7787 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007788{
7789#ifdef FEAT_BROWSE
7790 char_u *title;
7791 char_u *initdir;
7792 char_u buf[NUMBUFLEN];
7793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007794 title = get_tv_string_chk(&argvars[0]);
7795 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007797 if (title == NULL || initdir == NULL)
7798 rettv->vval.v_string = NULL;
7799 else
7800 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007801 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806}
7807
Bram Moolenaar33570922005-01-25 22:26:29 +00007808static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007809
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810/*
7811 * Find a buffer by number or exact name.
7812 */
7813 static buf_T *
7814find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819 if (avar->v_type == VAR_NUMBER)
7820 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007821 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007824 if (buf == NULL)
7825 {
7826 /* No full path name match, try a match with a URL or a "nofile"
7827 * buffer, these don't use the full path. */
7828 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7829 if (buf->b_fname != NULL
7830 && (path_with_url(buf->b_fname)
7831#ifdef FEAT_QUICKFIX
7832 || bt_nofile(buf)
7833#endif
7834 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007836 break;
7837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839 return buf;
7840}
7841
7842/*
7843 * "bufexists(expr)" function
7844 */
7845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 typval_T *argvars;
7848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851}
7852
7853/*
7854 * "buflisted(expr)" function
7855 */
7856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 typval_T *argvars;
7859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 buf_T *buf;
7862
7863 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865}
7866
7867/*
7868 * "bufloaded(expr)" function
7869 */
7870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T *argvars;
7873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 buf_T *buf;
7876
7877 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879}
7880
Bram Moolenaar33570922005-01-25 22:26:29 +00007881static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007882
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883/*
7884 * Get buffer by number or pattern.
7885 */
7886 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 int save_magic;
7892 char_u *save_cpo;
7893 buf_T *buf;
7894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 if (tv->v_type == VAR_NUMBER)
7896 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007897 if (tv->v_type != VAR_STRING)
7898 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (name == NULL || *name == NUL)
7900 return curbuf;
7901 if (name[0] == '$' && name[1] == NUL)
7902 return lastbuf;
7903
7904 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7905 save_magic = p_magic;
7906 p_magic = TRUE;
7907 save_cpo = p_cpo;
7908 p_cpo = (char_u *)"";
7909
7910 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7911 TRUE, FALSE));
7912
7913 p_magic = save_magic;
7914 p_cpo = save_cpo;
7915
7916 /* If not found, try expanding the name, like done for bufexists(). */
7917 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
7920 return buf;
7921}
7922
7923/*
7924 * "bufname(expr)" function
7925 */
7926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 typval_T *argvars;
7929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 buf_T *buf;
7932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 buf = get_buf_tv(&argvars[0]);
7936 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 --emsg_off;
7942}
7943
7944/*
7945 * "bufnr(expr)" function
7946 */
7947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007953 int error = FALSE;
7954 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007959 --emsg_off;
7960
7961 /* If the buffer isn't found and the second argument is not zero create a
7962 * new buffer. */
7963 if (buf == NULL
7964 && argvars[1].v_type != VAR_UNKNOWN
7965 && get_tv_number_chk(&argvars[1], &error) != 0
7966 && !error
7967 && (name = get_tv_string_chk(&argvars[0])) != NULL
7968 && !error)
7969 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975}
7976
7977/*
7978 * "bufwinnr(nr)" function
7979 */
7980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *argvars;
7983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985#ifdef FEAT_WINDOWS
7986 win_T *wp;
7987 int winnr = 0;
7988#endif
7989 buf_T *buf;
7990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007991 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994#ifdef FEAT_WINDOWS
7995 for (wp = firstwin; wp; wp = wp->w_next)
7996 {
7997 ++winnr;
7998 if (wp->w_buffer == buf)
7999 break;
8000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004#endif
8005 --emsg_off;
8006}
8007
8008/*
8009 * "byte2line(byte)" function
8010 */
8011/*ARGSUSED*/
8012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 typval_T *argvars;
8015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019#else
8020 long boff = 0;
8021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008026 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 (linenr_T)0, &boff);
8028#endif
8029}
8030
8031/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008032 * "byteidx()" function
8033 */
8034/*ARGSUSED*/
8035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 typval_T *argvars;
8038 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008039{
8040#ifdef FEAT_MBYTE
8041 char_u *t;
8042#endif
8043 char_u *str;
8044 long idx;
8045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 str = get_tv_string_chk(&argvars[0]);
8047 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008050 return;
8051
8052#ifdef FEAT_MBYTE
8053 t = str;
8054 for ( ; idx > 0; idx--)
8055 {
8056 if (*t == NUL) /* EOL reached */
8057 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008058 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008059 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008060 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008061#else
8062 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008064#endif
8065}
8066
8067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068 * "call(func, arglist)" function
8069 */
8070 static void
8071f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 typval_T *argvars;
8073 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074{
8075 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008076 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008079 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008081
8082 rettv->vval.v_number = 0;
8083 if (argvars[1].v_type != VAR_LIST)
8084 {
8085 EMSG(_(e_listreq));
8086 return;
8087 }
8088 if (argvars[1].vval.v_list == NULL)
8089 return;
8090
8091 if (argvars[0].v_type == VAR_FUNC)
8092 func = argvars[0].vval.v_string;
8093 else
8094 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 if (*func == NUL)
8096 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008097
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 if (argvars[2].v_type != VAR_UNKNOWN)
8099 {
8100 if (argvars[2].v_type != VAR_DICT)
8101 {
8102 EMSG(_(e_dictreq));
8103 return;
8104 }
8105 selfdict = argvars[2].vval.v_dict;
8106 }
8107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008108 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8109 item = item->li_next)
8110 {
8111 if (argc == MAX_FUNC_ARGS)
8112 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008113 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114 break;
8115 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008116 /* Make a copy of each argument. This is needed to be able to set
8117 * v_lock to VAR_FIXED in the copy without changing the original list.
8118 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008119 copy_tv(&item->li_tv, &argv[argc++]);
8120 }
8121
8122 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008123 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008124 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8125 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126
8127 /* Free the arguments. */
8128 while (argc > 0)
8129 clear_tv(&argv[--argc]);
8130}
8131
8132/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008133 * "changenr()" function
8134 */
8135/*ARGSUSED*/
8136 static void
8137f_changenr(argvars, rettv)
8138 typval_T *argvars;
8139 typval_T *rettv;
8140{
8141 rettv->vval.v_number = curbuf->b_u_seq_cur;
8142}
8143
8144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * "char2nr(string)" function
8146 */
8147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008148f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 typval_T *argvars;
8150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152#ifdef FEAT_MBYTE
8153 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 else
8156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158}
8159
8160/*
8161 * "cindent(lnum)" function
8162 */
8163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008165 typval_T *argvars;
8166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168#ifdef FEAT_CINDENT
8169 pos_T pos;
8170 linenr_T lnum;
8171
8172 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8175 {
8176 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008177 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 curwin->w_cursor = pos;
8179 }
8180 else
8181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183}
8184
8185/*
8186 * "col(string)" function
8187 */
8188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008190 typval_T *argvars;
8191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 colnr_T col = 0;
8194 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008195 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008197 fp = var2fpos(&argvars[0], FALSE, &fnum);
8198 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 if (fp->col == MAXCOL)
8201 {
8202 /* '> can be MAXCOL, get the length of the line then */
8203 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008204 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 else
8206 col = MAXCOL;
8207 }
8208 else
8209 {
8210 col = fp->col + 1;
8211#ifdef FEAT_VIRTUALEDIT
8212 /* col(".") when the cursor is on the NUL at the end of the line
8213 * because of "coladd" can be seen as an extra column. */
8214 if (virtual_active() && fp == &curwin->w_cursor)
8215 {
8216 char_u *p = ml_get_cursor();
8217
8218 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8219 curwin->w_virtcol - curwin->w_cursor.coladd))
8220 {
8221# ifdef FEAT_MBYTE
8222 int l;
8223
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008224 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 col += l;
8226# else
8227 if (*p != NUL && p[1] == NUL)
8228 ++col;
8229# endif
8230 }
8231 }
8232#endif
8233 }
8234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
Bram Moolenaar572cb562005-08-05 21:35:02 +00008238#if defined(FEAT_INS_EXPAND)
8239/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008240 * "complete()" function
8241 */
8242/*ARGSUSED*/
8243 static void
8244f_complete(argvars, rettv)
8245 typval_T *argvars;
8246 typval_T *rettv;
8247{
8248 int startcol;
8249
8250 if ((State & INSERT) == 0)
8251 {
8252 EMSG(_("E785: complete() can only be used in Insert mode"));
8253 return;
8254 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008255
8256 /* Check for undo allowed here, because if something was already inserted
8257 * the line was already saved for undo and this check isn't done. */
8258 if (!undo_allowed())
8259 return;
8260
Bram Moolenaarade00832006-03-10 21:46:58 +00008261 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8262 {
8263 EMSG(_(e_invarg));
8264 return;
8265 }
8266
8267 startcol = get_tv_number_chk(&argvars[0], NULL);
8268 if (startcol <= 0)
8269 return;
8270
8271 set_completion(startcol - 1, argvars[1].vval.v_list);
8272}
8273
8274/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008275 * "complete_add()" function
8276 */
8277/*ARGSUSED*/
8278 static void
8279f_complete_add(argvars, rettv)
8280 typval_T *argvars;
8281 typval_T *rettv;
8282{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008283 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008284}
8285
8286/*
8287 * "complete_check()" function
8288 */
8289/*ARGSUSED*/
8290 static void
8291f_complete_check(argvars, rettv)
8292 typval_T *argvars;
8293 typval_T *rettv;
8294{
8295 int saved = RedrawingDisabled;
8296
8297 RedrawingDisabled = 0;
8298 ins_compl_check_keys(0);
8299 rettv->vval.v_number = compl_interrupted;
8300 RedrawingDisabled = saved;
8301}
8302#endif
8303
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304/*
8305 * "confirm(message, buttons[, default [, type]])" function
8306 */
8307/*ARGSUSED*/
8308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 typval_T *argvars;
8311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8314 char_u *message;
8315 char_u *buttons = NULL;
8316 char_u buf[NUMBUFLEN];
8317 char_u buf2[NUMBUFLEN];
8318 int def = 1;
8319 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 char_u *typestr;
8321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008323 message = get_tv_string_chk(&argvars[0]);
8324 if (message == NULL)
8325 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008326 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008328 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8329 if (buttons == NULL)
8330 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008333 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008334 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008336 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8337 if (typestr == NULL)
8338 error = TRUE;
8339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008341 switch (TOUPPER_ASC(*typestr))
8342 {
8343 case 'E': type = VIM_ERROR; break;
8344 case 'Q': type = VIM_QUESTION; break;
8345 case 'I': type = VIM_INFO; break;
8346 case 'W': type = VIM_WARNING; break;
8347 case 'G': type = VIM_GENERIC; break;
8348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350 }
8351 }
8352 }
8353
8354 if (buttons == NULL || *buttons == NUL)
8355 buttons = (char_u *)_("&Ok");
8356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008357 if (error)
8358 rettv->vval.v_number = 0;
8359 else
8360 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 def, NULL);
8362#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365}
8366
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008367/*
8368 * "copy()" function
8369 */
8370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *argvars;
8373 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008375 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
8378/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008379 * "count()" function
8380 */
8381 static void
8382f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 typval_T *argvars;
8384 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008385{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008386 long n = 0;
8387 int ic = FALSE;
8388
Bram Moolenaare9a41262005-01-15 22:18:47 +00008389 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008390 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 listitem_T *li;
8392 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008393 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008394
Bram Moolenaare9a41262005-01-15 22:18:47 +00008395 if ((l = argvars[0].vval.v_list) != NULL)
8396 {
8397 li = l->lv_first;
8398 if (argvars[2].v_type != VAR_UNKNOWN)
8399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008400 int error = FALSE;
8401
8402 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008403 if (argvars[3].v_type != VAR_UNKNOWN)
8404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008405 idx = get_tv_number_chk(&argvars[3], &error);
8406 if (!error)
8407 {
8408 li = list_find(l, idx);
8409 if (li == NULL)
8410 EMSGN(_(e_listidx), idx);
8411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008413 if (error)
8414 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415 }
8416
8417 for ( ; li != NULL; li = li->li_next)
8418 if (tv_equal(&li->li_tv, &argvars[1], ic))
8419 ++n;
8420 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422 else if (argvars[0].v_type == VAR_DICT)
8423 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 int todo;
8425 dict_T *d;
8426 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008428 if ((d = argvars[0].vval.v_dict) != NULL)
8429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430 int error = FALSE;
8431
Bram Moolenaare9a41262005-01-15 22:18:47 +00008432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 if (argvars[3].v_type != VAR_UNKNOWN)
8436 EMSG(_(e_invarg));
8437 }
8438
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008439 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008441 {
8442 if (!HASHITEM_EMPTY(hi))
8443 {
8444 --todo;
8445 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8446 ++n;
8447 }
8448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008449 }
8450 }
8451 else
8452 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008453 rettv->vval.v_number = n;
8454}
8455
8456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8458 *
8459 * Checks the existence of a cscope connection.
8460 */
8461/*ARGSUSED*/
8462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 typval_T *argvars;
8465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
8467#ifdef FEAT_CSCOPE
8468 int num = 0;
8469 char_u *dbpath = NULL;
8470 char_u *prepend = NULL;
8471 char_u buf[NUMBUFLEN];
8472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008473 if (argvars[0].v_type != VAR_UNKNOWN
8474 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 num = (int)get_tv_number(&argvars[0]);
8477 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008478 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008479 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
8481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485#endif
8486}
8487
8488/*
8489 * "cursor(lnum, col)" function
8490 *
8491 * Moves the cursor to the specified line and column
8492 */
8493/*ARGSUSED*/
8494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 typval_T *argvars;
8497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008500#ifdef FEAT_VIRTUALEDIT
8501 long coladd = 0;
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
Bram Moolenaara5525202006-03-02 22:52:09 +00008504 if (argvars[1].v_type == VAR_UNKNOWN)
8505 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008506 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008507
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008508 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008509 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008510 line = pos.lnum;
8511 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008512#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008514#endif
8515 }
8516 else
8517 {
8518 line = get_tv_lnum(argvars);
8519 col = get_tv_number_chk(&argvars[1], NULL);
8520#ifdef FEAT_VIRTUALEDIT
8521 if (argvars[2].v_type != VAR_UNKNOWN)
8522 coladd = get_tv_number_chk(&argvars[2], NULL);
8523#endif
8524 }
8525 if (line < 0 || col < 0
8526#ifdef FEAT_VIRTUALEDIT
8527 || coladd < 0
8528#endif
8529 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008530 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (line > 0)
8532 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (col > 0)
8534 curwin->w_cursor.col = col - 1;
8535#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008536 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537#endif
8538
8539 /* Make sure the cursor is in a valid position. */
8540 check_cursor();
8541#ifdef FEAT_MBYTE
8542 /* Correct cursor for multi-byte character. */
8543 if (has_mbyte)
8544 mb_adjust_cursor();
8545#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008546
8547 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549
8550/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 */
8553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008558 int noref = 0;
8559
8560 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 if (noref < 0 || noref > 1)
8563 EMSG(_(e_invarg));
8564 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008565 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566}
8567
8568/*
8569 * "delete()" function
8570 */
8571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 typval_T *argvars;
8574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580}
8581
8582/*
8583 * "did_filetype()" function
8584 */
8585/*ARGSUSED*/
8586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 typval_T *argvars;
8589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596}
8597
8598/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008599 * "diff_filler()" function
8600 */
8601/*ARGSUSED*/
8602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *argvars;
8605 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008606{
8607#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008609#endif
8610}
8611
8612/*
8613 * "diff_hlID()" function
8614 */
8615/*ARGSUSED*/
8616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *argvars;
8619 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008620{
8621#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008623 static linenr_T prev_lnum = 0;
8624 static int changedtick = 0;
8625 static int fnum = 0;
8626 static int change_start = 0;
8627 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008628 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008629 int filler_lines;
8630 int col;
8631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 if (lnum < 0) /* ignore type error in {lnum} arg */
8633 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008634 if (lnum != prev_lnum
8635 || changedtick != curbuf->b_changedtick
8636 || fnum != curbuf->b_fnum)
8637 {
8638 /* New line, buffer, change: need to get the values. */
8639 filler_lines = diff_check(curwin, lnum);
8640 if (filler_lines < 0)
8641 {
8642 if (filler_lines == -1)
8643 {
8644 change_start = MAXCOL;
8645 change_end = -1;
8646 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8647 hlID = HLF_ADD; /* added line */
8648 else
8649 hlID = HLF_CHD; /* changed line */
8650 }
8651 else
8652 hlID = HLF_ADD; /* added line */
8653 }
8654 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008655 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008656 prev_lnum = lnum;
8657 changedtick = curbuf->b_changedtick;
8658 fnum = curbuf->b_fnum;
8659 }
8660
8661 if (hlID == HLF_CHD || hlID == HLF_TXD)
8662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008664 if (col >= change_start && col <= change_end)
8665 hlID = HLF_TXD; /* changed text */
8666 else
8667 hlID = HLF_CHD; /* changed line */
8668 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008669 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008670#endif
8671}
8672
8673/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008674 * "empty({expr})" function
8675 */
8676 static void
8677f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008680{
8681 int n;
8682
8683 switch (argvars[0].v_type)
8684 {
8685 case VAR_STRING:
8686 case VAR_FUNC:
8687 n = argvars[0].vval.v_string == NULL
8688 || *argvars[0].vval.v_string == NUL;
8689 break;
8690 case VAR_NUMBER:
8691 n = argvars[0].vval.v_number == 0;
8692 break;
8693 case VAR_LIST:
8694 n = argvars[0].vval.v_list == NULL
8695 || argvars[0].vval.v_list->lv_first == NULL;
8696 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 case VAR_DICT:
8698 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008701 default:
8702 EMSG2(_(e_intern2), "f_empty()");
8703 n = 0;
8704 }
8705
8706 rettv->vval.v_number = n;
8707}
8708
8709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 * "escape({string}, {chars})" function
8711 */
8712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *argvars;
8715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
8717 char_u buf[NUMBUFLEN];
8718
Bram Moolenaar758711c2005-02-02 23:11:38 +00008719 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8720 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722}
8723
8724/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008725 * "eval()" function
8726 */
8727/*ARGSUSED*/
8728 static void
8729f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *argvars;
8731 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008732{
8733 char_u *s;
8734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 s = get_tv_string_chk(&argvars[0]);
8736 if (s != NULL)
8737 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8740 {
8741 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008742 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008744 else if (*s != NUL)
8745 EMSG(_(e_trailing));
8746}
8747
8748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 * "eventhandler()" function
8750 */
8751/*ARGSUSED*/
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *argvars;
8755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758}
8759
8760/*
8761 * "executable()" function
8762 */
8763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 typval_T *argvars;
8766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
8771/*
8772 * "exists()" function
8773 */
8774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 typval_T *argvars;
8777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 char_u *p;
8780 char_u *name;
8781 int n = FALSE;
8782 int len = 0;
8783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 if (*p == '$') /* environment variable */
8786 {
8787 /* first try "normal" environment variables (fast) */
8788 if (mch_getenv(p + 1) != NULL)
8789 n = TRUE;
8790 else
8791 {
8792 /* try expanding things like $VIM and ${HOME} */
8793 p = expand_env_save(p);
8794 if (p != NULL && *p != '$')
8795 n = TRUE;
8796 vim_free(p);
8797 }
8798 }
8799 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008802 if (*skipwhite(p) != NUL)
8803 n = FALSE; /* trailing garbage */
8804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 else if (*p == '*') /* internal or user defined function */
8806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008807 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
8809 else if (*p == ':')
8810 {
8811 n = cmd_exists(p + 1);
8812 }
8813 else if (*p == '#')
8814 {
8815#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008816 if (p[1] == '#')
8817 n = autocmd_supported(p + 2);
8818 else
8819 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820#endif
8821 }
8822 else /* internal variable */
8823 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008824 char_u *tofree;
8825 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008827 /* get_name_len() takes care of expanding curly braces */
8828 name = p;
8829 len = get_name_len(&p, &tofree, TRUE, FALSE);
8830 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008832 if (tofree != NULL)
8833 name = tofree;
8834 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8835 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008837 /* handle d.key, l[idx], f(expr) */
8838 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8839 if (n)
8840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008843 if (*p != NUL)
8844 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008846 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 }
8848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
8853 * "expand()" function
8854 */
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 char_u *s;
8861 int len;
8862 char_u *errormsg;
8863 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8864 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008865 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 rettv->v_type = VAR_STRING;
8868 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (*s == '%' || *s == '#' || *s == '<')
8870 {
8871 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 --emsg_off;
8874 }
8875 else
8876 {
8877 /* When the optional second argument is non-zero, don't remove matches
8878 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 if (argvars[1].v_type != VAR_UNKNOWN
8880 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 if (!error)
8883 {
8884 ExpandInit(&xpc);
8885 xpc.xp_context = EXPAND_FILES;
8886 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 }
8888 else
8889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891}
8892
8893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008894 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008895 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008896 */
8897 static void
8898f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008899 typval_T *argvars;
8900 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008901{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008902 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008903 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008904 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 list_T *l1, *l2;
8906 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008907 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008909
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 l1 = argvars[0].vval.v_list;
8911 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008912 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8913 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008914 {
8915 if (argvars[2].v_type != VAR_UNKNOWN)
8916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 before = get_tv_number_chk(&argvars[2], &error);
8918 if (error)
8919 return; /* type error; errmsg already given */
8920
Bram Moolenaar758711c2005-02-02 23:11:38 +00008921 if (before == l1->lv_len)
8922 item = NULL;
8923 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008924 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008925 item = list_find(l1, before);
8926 if (item == NULL)
8927 {
8928 EMSGN(_(e_listidx), before);
8929 return;
8930 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 }
8932 }
8933 else
8934 item = NULL;
8935 list_extend(l1, l2, item);
8936
Bram Moolenaare9a41262005-01-15 22:18:47 +00008937 copy_tv(&argvars[0], rettv);
8938 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 dict_T *d1, *d2;
8943 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 char_u *action;
8945 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008947 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948
8949 d1 = argvars[0].vval.v_dict;
8950 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008951 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8952 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008953 {
8954 /* Check the third argument. */
8955 if (argvars[2].v_type != VAR_UNKNOWN)
8956 {
8957 static char *(av[]) = {"keep", "force", "error"};
8958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959 action = get_tv_string_chk(&argvars[2]);
8960 if (action == NULL)
8961 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 for (i = 0; i < 3; ++i)
8963 if (STRCMP(action, av[i]) == 0)
8964 break;
8965 if (i == 3)
8966 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008967 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 return;
8969 }
8970 }
8971 else
8972 action = (char_u *)"force";
8973
8974 /* Go over all entries in the second dict and add them to the
8975 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008976 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008979 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008980 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008981 --todo;
8982 di1 = dict_find(d1, hi2->hi_key, -1);
8983 if (di1 == NULL)
8984 {
8985 di1 = dictitem_copy(HI2DI(hi2));
8986 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8987 dictitem_free(di1);
8988 }
8989 else if (*action == 'e')
8990 {
8991 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8992 break;
8993 }
8994 else if (*action == 'f')
8995 {
8996 clear_tv(&di1->di_tv);
8997 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8998 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 }
9000 }
9001
Bram Moolenaare9a41262005-01-15 22:18:47 +00009002 copy_tv(&argvars[0], rettv);
9003 }
9004 }
9005 else
9006 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007}
9008
9009/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009010 * "feedkeys()" function
9011 */
9012/*ARGSUSED*/
9013 static void
9014f_feedkeys(argvars, rettv)
9015 typval_T *argvars;
9016 typval_T *rettv;
9017{
9018 int remap = TRUE;
9019 char_u *keys, *flags;
9020 char_u nbuf[NUMBUFLEN];
9021 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009022 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009023
9024 rettv->vval.v_number = 0;
9025 keys = get_tv_string(&argvars[0]);
9026 if (*keys != NUL)
9027 {
9028 if (argvars[1].v_type != VAR_UNKNOWN)
9029 {
9030 flags = get_tv_string_buf(&argvars[1], nbuf);
9031 for ( ; *flags != NUL; ++flags)
9032 {
9033 switch (*flags)
9034 {
9035 case 'n': remap = FALSE; break;
9036 case 'm': remap = TRUE; break;
9037 case 't': typed = TRUE; break;
9038 }
9039 }
9040 }
9041
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009042 /* Need to escape K_SPECIAL and CSI before putting the string in the
9043 * typeahead buffer. */
9044 keys_esc = vim_strsave_escape_csi(keys);
9045 if (keys_esc != NULL)
9046 {
9047 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009048 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009049 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009050 if (vgetc_busy)
9051 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009052 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009053 }
9054}
9055
9056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 * "filereadable()" function
9058 */
9059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 typval_T *argvars;
9062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 FILE *fd;
9065 char_u *p;
9066 int n;
9067
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9070 {
9071 n = TRUE;
9072 fclose(fd);
9073 }
9074 else
9075 n = FALSE;
9076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078}
9079
9080/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009081 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * rights to write into.
9083 */
9084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 typval_T *argvars;
9087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009089 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090}
9091
Bram Moolenaar33570922005-01-25 22:26:29 +00009092static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009093
9094 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009095findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009098 int dir;
9099{
9100#ifdef FEAT_SEARCHPATH
9101 char_u *fname;
9102 char_u *fresult = NULL;
9103 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9104 char_u *p;
9105 char_u pathbuf[NUMBUFLEN];
9106 int count = 1;
9107 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009108 int error = FALSE;
9109#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009110
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009111 rettv->vval.v_string = NULL;
9112 rettv->v_type = VAR_STRING;
9113
9114#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009116
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9120 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009121 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009122 else
9123 {
9124 if (*p != NUL)
9125 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009128 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009130 }
9131
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009132 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9133 error = TRUE;
9134
9135 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 do
9138 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009139 if (rettv->v_type == VAR_STRING)
9140 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 fresult = find_file_in_path_option(first ? fname : NULL,
9142 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009143 0, first, path, dir, NULL,
9144 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009146
9147 if (fresult != NULL && rettv->v_type == VAR_LIST)
9148 list_append_string(rettv->vval.v_list, fresult, -1);
9149
9150 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009152
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009153 if (rettv->v_type == VAR_STRING)
9154 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009155#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009156}
9157
Bram Moolenaar33570922005-01-25 22:26:29 +00009158static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9159static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009160
9161/*
9162 * Implementation of map() and filter().
9163 */
9164 static void
9165filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *argvars;
9167 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009168 int map;
9169{
9170 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 listitem_T *li, *nli;
9173 list_T *l = NULL;
9174 dictitem_T *di;
9175 hashtab_T *ht;
9176 hashitem_T *hi;
9177 dict_T *d = NULL;
9178 typval_T save_val;
9179 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009180 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009181 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009182 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009183 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009184
9185 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009186 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009187 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009188 if ((l = argvars[0].vval.v_list) == NULL
9189 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009190 return;
9191 }
9192 else if (argvars[0].v_type == VAR_DICT)
9193 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009194 if ((d = argvars[0].vval.v_dict) == NULL
9195 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009196 return;
9197 }
9198 else
9199 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009200 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 return;
9202 }
9203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 expr = get_tv_string_buf_chk(&argvars[1], buf);
9205 /* On type errors, the preceding call has already displayed an error
9206 * message. Avoid a misleading error message for an empty string that
9207 * was not passed as argument. */
9208 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009210 prepare_vimvar(VV_VAL, &save_val);
9211 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009212
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009213 /* We reset "did_emsg" to be able to detect whether an error
9214 * occurred during evaluation of the expression. */
9215 save_did_emsg = did_emsg;
9216 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 prepare_vimvar(VV_KEY, &save_key);
9221 vimvars[VV_KEY].vv_type = VAR_STRING;
9222
9223 ht = &d->dv_hashtab;
9224 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009225 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009226 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 if (!HASHITEM_EMPTY(hi))
9229 {
9230 --todo;
9231 di = HI2DI(hi);
9232 if (tv_check_lock(di->di_tv.v_lock, msg))
9233 break;
9234 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009235 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009236 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 break;
9238 if (!map && rem)
9239 dictitem_remove(d, di);
9240 clear_tv(&vimvars[VV_KEY].vv_tv);
9241 }
9242 }
9243 hash_unlock(ht);
9244
9245 restore_vimvar(VV_KEY, &save_key);
9246 }
9247 else
9248 {
9249 for (li = l->lv_first; li != NULL; li = nli)
9250 {
9251 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009252 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009254 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009255 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009256 break;
9257 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009259 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009260 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009263
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009264 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266
9267 copy_tv(&argvars[0], rettv);
9268}
9269
9270 static int
9271filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 char_u *expr;
9274 int map;
9275 int *remp;
9276{
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009278 char_u *s;
9279
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009281 s = expr;
9282 if (eval1(&s, &rettv, TRUE) == FAIL)
9283 return FAIL;
9284 if (*s != NUL) /* check for trailing chars after expr */
9285 {
9286 EMSG2(_(e_invexpr2), s);
9287 return FAIL;
9288 }
9289 if (map)
9290 {
9291 /* map(): replace the list item value */
9292 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009293 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 *tv = rettv;
9295 }
9296 else
9297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009298 int error = FALSE;
9299
Bram Moolenaare9a41262005-01-15 22:18:47 +00009300 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009302 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 /* On type error, nothing has been removed; return FAIL to stop the
9304 * loop. The error message was given by get_tv_number_chk(). */
9305 if (error)
9306 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009310}
9311
9312/*
9313 * "filter()" function
9314 */
9315 static void
9316f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009319{
9320 filter_map(argvars, rettv, FALSE);
9321}
9322
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009323/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009324 * "finddir({fname}[, {path}[, {count}]])" function
9325 */
9326 static void
9327f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009330{
9331 findfilendir(argvars, rettv, TRUE);
9332}
9333
9334/*
9335 * "findfile({fname}[, {path}[, {count}]])" function
9336 */
9337 static void
9338f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 typval_T *argvars;
9340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009341{
9342 findfilendir(argvars, rettv, FALSE);
9343}
9344
9345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 * "fnamemodify({fname}, {mods})" function
9347 */
9348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009350 typval_T *argvars;
9351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 char_u *fname;
9354 char_u *mods;
9355 int usedlen = 0;
9356 int len;
9357 char_u *fbuf = NULL;
9358 char_u buf[NUMBUFLEN];
9359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 fname = get_tv_string_chk(&argvars[0]);
9361 mods = get_tv_string_buf_chk(&argvars[1], buf);
9362 if (fname == NULL || mods == NULL)
9363 fname = NULL;
9364 else
9365 {
9366 len = (int)STRLEN(fname);
9367 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 vim_free(fbuf);
9376}
9377
Bram Moolenaar33570922005-01-25 22:26:29 +00009378static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379
9380/*
9381 * "foldclosed()" function
9382 */
9383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009384foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009385 typval_T *argvars;
9386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 int end;
9388{
9389#ifdef FEAT_FOLDING
9390 linenr_T lnum;
9391 linenr_T first, last;
9392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9395 {
9396 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9397 {
9398 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 return;
9403 }
9404 }
9405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410 * "foldclosed()" function
9411 */
9412 static void
9413f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 typval_T *argvars;
9415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009416{
9417 foldclosed_both(argvars, rettv, FALSE);
9418}
9419
9420/*
9421 * "foldclosedend()" function
9422 */
9423 static void
9424f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 typval_T *argvars;
9426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427{
9428 foldclosed_both(argvars, rettv, TRUE);
9429}
9430
9431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 * "foldlevel()" function
9433 */
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439#ifdef FEAT_FOLDING
9440 linenr_T lnum;
9441
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 else
9446#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448}
9449
9450/*
9451 * "foldtext()" function
9452 */
9453/*ARGSUSED*/
9454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 typval_T *argvars;
9457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459#ifdef FEAT_FOLDING
9460 linenr_T lnum;
9461 char_u *s;
9462 char_u *r;
9463 int len;
9464 char *txt;
9465#endif
9466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467 rettv->v_type = VAR_STRING;
9468 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009470 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9471 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9472 <= curbuf->b_ml.ml_line_count
9473 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 {
9475 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009476 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9477 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 {
9479 if (!linewhite(lnum))
9480 break;
9481 ++lnum;
9482 }
9483
9484 /* Find interesting text in this line. */
9485 s = skipwhite(ml_get(lnum));
9486 /* skip C comment-start */
9487 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009490 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009491 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009492 {
9493 s = skipwhite(ml_get(lnum + 1));
9494 if (*s == '*')
9495 s = skipwhite(s + 1);
9496 }
9497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 txt = _("+-%s%3ld lines: ");
9499 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009500 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 + 20 /* for %3ld */
9502 + STRLEN(s))); /* concatenated */
9503 if (r != NULL)
9504 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009505 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9506 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9507 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 len = (int)STRLEN(r);
9509 STRCAT(r, s);
9510 /* remove 'foldmarker' and 'commentstring' */
9511 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
9514 }
9515#endif
9516}
9517
9518/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009519 * "foldtextresult(lnum)" function
9520 */
9521/*ARGSUSED*/
9522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009524 typval_T *argvars;
9525 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009526{
9527#ifdef FEAT_FOLDING
9528 linenr_T lnum;
9529 char_u *text;
9530 char_u buf[51];
9531 foldinfo_T foldinfo;
9532 int fold_count;
9533#endif
9534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->v_type = VAR_STRING;
9536 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009537#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 /* treat illegal types and illegal string values for {lnum} the same */
9540 if (lnum < 0)
9541 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009542 fold_count = foldedCount(curwin, lnum, &foldinfo);
9543 if (fold_count > 0)
9544 {
9545 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9546 &foldinfo, buf);
9547 if (text == buf)
9548 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009550 }
9551#endif
9552}
9553
9554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 * "foreground()" function
9556 */
9557/*ARGSUSED*/
9558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#ifdef FEAT_GUI
9565 if (gui.in_use)
9566 gui_mch_set_foreground();
9567#else
9568# ifdef WIN32
9569 win32_set_foreground();
9570# endif
9571#endif
9572}
9573
9574/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009575 * "function()" function
9576 */
9577/*ARGSUSED*/
9578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 typval_T *argvars;
9581 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582{
9583 char_u *s;
9584
Bram Moolenaara7043832005-01-21 11:56:39 +00009585 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009587 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009588 EMSG2(_(e_invarg2), s);
9589 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009590 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 else
9592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_string = vim_strsave(s);
9594 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009595 }
9596}
9597
9598/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009599 * "garbagecollect()" function
9600 */
9601/*ARGSUSED*/
9602 static void
9603f_garbagecollect(argvars, rettv)
9604 typval_T *argvars;
9605 typval_T *rettv;
9606{
9607 garbage_collect();
9608}
9609
9610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009611 * "get()" function
9612 */
9613 static void
9614f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009617{
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 listitem_T *li;
9619 list_T *l;
9620 dictitem_T *di;
9621 dict_T *d;
9622 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009623
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009625 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009628 int error = FALSE;
9629
9630 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9631 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 else if (argvars[0].v_type == VAR_DICT)
9636 {
9637 if ((d = argvars[0].vval.v_dict) != NULL)
9638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009639 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 if (di != NULL)
9641 tv = &di->di_tv;
9642 }
9643 }
9644 else
9645 EMSG2(_(e_listdictarg), "get()");
9646
9647 if (tv == NULL)
9648 {
9649 if (argvars[2].v_type == VAR_UNKNOWN)
9650 rettv->vval.v_number = 0;
9651 else
9652 copy_tv(&argvars[2], rettv);
9653 }
9654 else
9655 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009656}
9657
Bram Moolenaar342337a2005-07-21 21:11:17 +00009658static 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 +00009659
9660/*
9661 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009662 * Return a range (from start to end) of lines in rettv from the specified
9663 * buffer.
9664 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009665 */
9666 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009667get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009668 buf_T *buf;
9669 linenr_T start;
9670 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009671 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009672 typval_T *rettv;
9673{
9674 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009675
Bram Moolenaar342337a2005-07-21 21:11:17 +00009676 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009677 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009678 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009679 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009680 }
9681 else
9682 rettv->vval.v_number = 0;
9683
9684 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9685 return;
9686
9687 if (!retlist)
9688 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009689 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9690 p = ml_get_buf(buf, start, FALSE);
9691 else
9692 p = (char_u *)"";
9693
9694 rettv->v_type = VAR_STRING;
9695 rettv->vval.v_string = vim_strsave(p);
9696 }
9697 else
9698 {
9699 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009700 return;
9701
9702 if (start < 1)
9703 start = 1;
9704 if (end > buf->b_ml.ml_line_count)
9705 end = buf->b_ml.ml_line_count;
9706 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009707 if (list_append_string(rettv->vval.v_list,
9708 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009709 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009710 }
9711}
9712
9713/*
9714 * "getbufline()" function
9715 */
9716 static void
9717f_getbufline(argvars, rettv)
9718 typval_T *argvars;
9719 typval_T *rettv;
9720{
9721 linenr_T lnum;
9722 linenr_T end;
9723 buf_T *buf;
9724
9725 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9726 ++emsg_off;
9727 buf = get_buf_tv(&argvars[0]);
9728 --emsg_off;
9729
Bram Moolenaar661b1822005-07-28 22:36:45 +00009730 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009731 if (argvars[2].v_type == VAR_UNKNOWN)
9732 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009733 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009734 end = get_tv_lnum_buf(&argvars[2], buf);
9735
Bram Moolenaar342337a2005-07-21 21:11:17 +00009736 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009737}
9738
Bram Moolenaar0d660222005-01-07 21:51:51 +00009739/*
9740 * "getbufvar()" function
9741 */
9742 static void
9743f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009744 typval_T *argvars;
9745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009746{
9747 buf_T *buf;
9748 buf_T *save_curbuf;
9749 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9753 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754 ++emsg_off;
9755 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009756
9757 rettv->v_type = VAR_STRING;
9758 rettv->vval.v_string = NULL;
9759
9760 if (buf != NULL && varname != NULL)
9761 {
9762 if (*varname == '&') /* buffer-local-option */
9763 {
9764 /* set curbuf to be our buf, temporarily */
9765 save_curbuf = curbuf;
9766 curbuf = buf;
9767
9768 get_option_tv(&varname, rettv, TRUE);
9769
9770 /* restore previous notion of curbuf */
9771 curbuf = save_curbuf;
9772 }
9773 else
9774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 if (*varname == NUL)
9776 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9777 * scope prefix before the NUL byte is required by
9778 * find_var_in_ht(). */
9779 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009780 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009781 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009782 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009784 }
9785 }
9786
9787 --emsg_off;
9788}
9789
9790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * "getchar()" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009801 /* Position the cursor. Needed after a message that ends in a space. */
9802 windgoto(msg_row, msg_col);
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 ++no_mapping;
9805 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009806 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 /* getchar(): blocking wait. */
9808 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 /* getchar(1): only check if char avail */
9811 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 else if (error || vpeekc() == NUL)
9813 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 n = 0;
9815 else
9816 /* getchar(0) and char avail: return char */
9817 n = safe_vgetc();
9818 --no_mapping;
9819 --allow_keys;
9820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 if (IS_SPECIAL(n) || mod_mask != 0)
9823 {
9824 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9825 int i = 0;
9826
9827 /* Turn a special key into three bytes, plus modifier. */
9828 if (mod_mask != 0)
9829 {
9830 temp[i++] = K_SPECIAL;
9831 temp[i++] = KS_MODIFIER;
9832 temp[i++] = mod_mask;
9833 }
9834 if (IS_SPECIAL(n))
9835 {
9836 temp[i++] = K_SPECIAL;
9837 temp[i++] = K_SECOND(n);
9838 temp[i++] = K_THIRD(n);
9839 }
9840#ifdef FEAT_MBYTE
9841 else if (has_mbyte)
9842 i += (*mb_char2bytes)(n, temp + i);
9843#endif
9844 else
9845 temp[i++] = n;
9846 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 rettv->v_type = VAR_STRING;
9848 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 }
9850}
9851
9852/*
9853 * "getcharmod()" function
9854 */
9855/*ARGSUSED*/
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864/*
9865 * "getcmdline()" function
9866 */
9867/*ARGSUSED*/
9868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 typval_T *argvars;
9871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->v_type = VAR_STRING;
9874 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875}
9876
9877/*
9878 * "getcmdpos()" function
9879 */
9880/*ARGSUSED*/
9881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009883 typval_T *argvars;
9884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
9889/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009890 * "getcmdtype()" function
9891 */
9892/*ARGSUSED*/
9893 static void
9894f_getcmdtype(argvars, rettv)
9895 typval_T *argvars;
9896 typval_T *rettv;
9897{
9898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = alloc(2);
9900 if (rettv->vval.v_string != NULL)
9901 {
9902 rettv->vval.v_string[0] = get_cmdline_type();
9903 rettv->vval.v_string[1] = NUL;
9904 }
9905}
9906
9907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 * "getcwd()" function
9909 */
9910/*ARGSUSED*/
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 char_u cwd[MAXPATHL];
9917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 else
9922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009925 if (rettv->vval.v_string != NULL)
9926 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#endif
9928 }
9929}
9930
9931/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009932 * "getfontname()" function
9933 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009934/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 typval_T *argvars;
9938 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->v_type = VAR_STRING;
9941 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009942#ifdef FEAT_GUI
9943 if (gui.in_use)
9944 {
9945 GuiFont font;
9946 char_u *name = NULL;
9947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009948 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009949 {
9950 /* Get the "Normal" font. Either the name saved by
9951 * hl_set_font_name() or from the font ID. */
9952 font = gui.norm_font;
9953 name = hl_get_font_name();
9954 }
9955 else
9956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009958 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9959 return;
9960 font = gui_mch_get_font(name, FALSE);
9961 if (font == NOFONT)
9962 return; /* Invalid font name, return empty string. */
9963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009965 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009966 gui_mch_free_font(font);
9967 }
9968#endif
9969}
9970
9971/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009972 * "getfperm({fname})" function
9973 */
9974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009978{
9979 char_u *fname;
9980 struct stat st;
9981 char_u *perm = NULL;
9982 char_u flags[] = "rwx";
9983 int i;
9984
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009986
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009988 if (mch_stat((char *)fname, &st) >= 0)
9989 {
9990 perm = vim_strsave((char_u *)"---------");
9991 if (perm != NULL)
9992 {
9993 for (i = 0; i < 9; i++)
9994 {
9995 if (st.st_mode & (1 << (8 - i)))
9996 perm[i] = flags[i % 3];
9997 }
9998 }
9999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010001}
10002
10003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 * "getfsize({fname})" function
10005 */
10006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010008 typval_T *argvars;
10009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010{
10011 char_u *fname;
10012 struct stat st;
10013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017
10018 if (mch_stat((char *)fname, &st) >= 0)
10019 {
10020 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027}
10028
10029/*
10030 * "getftime({fname})" function
10031 */
10032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010034 typval_T *argvars;
10035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 char_u *fname;
10038 struct stat st;
10039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010049 * "getftype({fname})" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010055{
10056 char_u *fname;
10057 struct stat st;
10058 char_u *type = NULL;
10059 char *t;
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010064 if (mch_lstat((char *)fname, &st) >= 0)
10065 {
10066#ifdef S_ISREG
10067 if (S_ISREG(st.st_mode))
10068 t = "file";
10069 else if (S_ISDIR(st.st_mode))
10070 t = "dir";
10071# ifdef S_ISLNK
10072 else if (S_ISLNK(st.st_mode))
10073 t = "link";
10074# endif
10075# ifdef S_ISBLK
10076 else if (S_ISBLK(st.st_mode))
10077 t = "bdev";
10078# endif
10079# ifdef S_ISCHR
10080 else if (S_ISCHR(st.st_mode))
10081 t = "cdev";
10082# endif
10083# ifdef S_ISFIFO
10084 else if (S_ISFIFO(st.st_mode))
10085 t = "fifo";
10086# endif
10087# ifdef S_ISSOCK
10088 else if (S_ISSOCK(st.st_mode))
10089 t = "fifo";
10090# endif
10091 else
10092 t = "other";
10093#else
10094# ifdef S_IFMT
10095 switch (st.st_mode & S_IFMT)
10096 {
10097 case S_IFREG: t = "file"; break;
10098 case S_IFDIR: t = "dir"; break;
10099# ifdef S_IFLNK
10100 case S_IFLNK: t = "link"; break;
10101# endif
10102# ifdef S_IFBLK
10103 case S_IFBLK: t = "bdev"; break;
10104# endif
10105# ifdef S_IFCHR
10106 case S_IFCHR: t = "cdev"; break;
10107# endif
10108# ifdef S_IFIFO
10109 case S_IFIFO: t = "fifo"; break;
10110# endif
10111# ifdef S_IFSOCK
10112 case S_IFSOCK: t = "socket"; break;
10113# endif
10114 default: t = "other";
10115 }
10116# else
10117 if (mch_isdir(fname))
10118 t = "dir";
10119 else
10120 t = "file";
10121# endif
10122#endif
10123 type = vim_strsave((char_u *)t);
10124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010126}
10127
10128/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010129 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010130 */
10131 static void
10132f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010133 typval_T *argvars;
10134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010135{
10136 linenr_T lnum;
10137 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010138 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010139
10140 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010141 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010142 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010143 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010144 retlist = FALSE;
10145 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010146 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010147 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010148 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010149 retlist = TRUE;
10150 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010151
Bram Moolenaar342337a2005-07-21 21:11:17 +000010152 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010153}
10154
10155/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010156 * "getpos(string)" function
10157 */
10158 static void
10159f_getpos(argvars, rettv)
10160 typval_T *argvars;
10161 typval_T *rettv;
10162{
10163 pos_T *fp;
10164 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010165 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010166
10167 if (rettv_list_alloc(rettv) == OK)
10168 {
10169 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010170 fp = var2fpos(&argvars[0], TRUE, &fnum);
10171 if (fnum != -1)
10172 list_append_number(l, (varnumber_T)fnum);
10173 else
10174 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010175 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10176 : (varnumber_T)0);
10177 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10178 : (varnumber_T)0);
10179 list_append_number(l,
10180#ifdef FEAT_VIRTUALEDIT
10181 (fp != NULL) ? (varnumber_T)fp->coladd :
10182#endif
10183 (varnumber_T)0);
10184 }
10185 else
10186 rettv->vval.v_number = FALSE;
10187}
10188
10189/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010190 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010191 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010192/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010193 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010194f_getqflist(argvars, rettv)
10195 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010196 typval_T *rettv;
10197{
10198#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010199 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010200#endif
10201
10202 rettv->vval.v_number = FALSE;
10203#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010204 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010205 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010206 wp = NULL;
10207 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10208 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010209 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010210 if (wp == NULL)
10211 return;
10212 }
10213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010214 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010215 }
10216#endif
10217}
10218
10219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 * "getreg()" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 char_u *strregname;
10228 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010229 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010232 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 strregname = get_tv_string_chk(&argvars[0]);
10235 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010236 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 regname = (strregname == NULL ? '"' : *strregname);
10242 if (regname == 0)
10243 regname = '"';
10244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010246 rettv->vval.v_string = error ? NULL :
10247 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248}
10249
10250/*
10251 * "getregtype()" function
10252 */
10253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 typval_T *argvars;
10256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257{
10258 char_u *strregname;
10259 int regname;
10260 char_u buf[NUMBUFLEN + 2];
10261 long reglen = 0;
10262
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010263 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 {
10265 strregname = get_tv_string_chk(&argvars[0]);
10266 if (strregname == NULL) /* type error; errmsg already given */
10267 {
10268 rettv->v_type = VAR_STRING;
10269 rettv->vval.v_string = NULL;
10270 return;
10271 }
10272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 else
10274 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276
10277 regname = (strregname == NULL ? '"' : *strregname);
10278 if (regname == 0)
10279 regname = '"';
10280
10281 buf[0] = NUL;
10282 buf[1] = NUL;
10283 switch (get_reg_type(regname, &reglen))
10284 {
10285 case MLINE: buf[0] = 'V'; break;
10286 case MCHAR: buf[0] = 'v'; break;
10287#ifdef FEAT_VISUAL
10288 case MBLOCK:
10289 buf[0] = Ctrl_V;
10290 sprintf((char *)buf + 1, "%ld", reglen + 1);
10291 break;
10292#endif
10293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->v_type = VAR_STRING;
10295 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296}
10297
10298/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010299 * "gettabwinvar()" function
10300 */
10301 static void
10302f_gettabwinvar(argvars, rettv)
10303 typval_T *argvars;
10304 typval_T *rettv;
10305{
10306 getwinvar(argvars, rettv, 1);
10307}
10308
10309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 * "getwinposx()" function
10311 */
10312/*ARGSUSED*/
10313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319#ifdef FEAT_GUI
10320 if (gui.in_use)
10321 {
10322 int x, y;
10323
10324 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 }
10327#endif
10328}
10329
10330/*
10331 * "getwinposy()" function
10332 */
10333/*ARGSUSED*/
10334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_GUI
10341 if (gui.in_use)
10342 {
10343 int x, y;
10344
10345 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 }
10348#endif
10349}
10350
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010351/*
10352 * Find window specifed by "vp" in tabpage "tp".
10353 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010354 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010355find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010356 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010357 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010358{
10359#ifdef FEAT_WINDOWS
10360 win_T *wp;
10361#endif
10362 int nr;
10363
10364 nr = get_tv_number_chk(vp, NULL);
10365
10366#ifdef FEAT_WINDOWS
10367 if (nr < 0)
10368 return NULL;
10369 if (nr == 0)
10370 return curwin;
10371
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010372 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10373 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010374 if (--nr <= 0)
10375 break;
10376 return wp;
10377#else
10378 if (nr == 0 || nr == 1)
10379 return curwin;
10380 return NULL;
10381#endif
10382}
10383
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384/*
10385 * "getwinvar()" function
10386 */
10387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010389 typval_T *argvars;
10390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010392 getwinvar(argvars, rettv, 0);
10393}
10394
10395/*
10396 * getwinvar() and gettabwinvar()
10397 */
10398 static void
10399getwinvar(argvars, rettv, off)
10400 typval_T *argvars;
10401 typval_T *rettv;
10402 int off; /* 1 for gettabwinvar() */
10403{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 win_T *win, *oldcurwin;
10405 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010407 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010409#ifdef FEAT_WINDOWS
10410 if (off == 1)
10411 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10412 else
10413 tp = curtab;
10414#endif
10415 win = find_win_by_nr(&argvars[off], tp);
10416 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419 rettv->v_type = VAR_STRING;
10420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421
10422 if (win != NULL && varname != NULL)
10423 {
10424 if (*varname == '&') /* window-local-option */
10425 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010426 /* Set curwin to be our win, temporarily. Also set curbuf, so
10427 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 oldcurwin = curwin;
10429 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010430 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
10434 /* restore previous notion of curwin */
10435 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010436 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 }
10438 else
10439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 if (*varname == NUL)
10441 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10442 * scope prefix before the NUL byte is required by
10443 * find_var_in_ht(). */
10444 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010446 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 }
10450 }
10451
10452 --emsg_off;
10453}
10454
10455/*
10456 * "glob()" function
10457 */
10458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 expand_T xpc;
10464
10465 ExpandInit(&xpc);
10466 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->v_type = VAR_STRING;
10468 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470}
10471
10472/*
10473 * "globpath()" function
10474 */
10475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476f_globpath(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 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 if (file == NULL)
10485 rettv->vval.v_string = NULL;
10486 else
10487 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488}
10489
10490/*
10491 * "has()" function
10492 */
10493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 typval_T *argvars;
10496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 int i;
10499 char_u *name;
10500 int n = FALSE;
10501 static char *(has_list[]) =
10502 {
10503#ifdef AMIGA
10504 "amiga",
10505# ifdef FEAT_ARP
10506 "arp",
10507# endif
10508#endif
10509#ifdef __BEOS__
10510 "beos",
10511#endif
10512#ifdef MSDOS
10513# ifdef DJGPP
10514 "dos32",
10515# else
10516 "dos16",
10517# endif
10518#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010519#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 "mac",
10521#endif
10522#if defined(MACOS_X_UNIX)
10523 "macunix",
10524#endif
10525#ifdef OS2
10526 "os2",
10527#endif
10528#ifdef __QNX__
10529 "qnx",
10530#endif
10531#ifdef RISCOS
10532 "riscos",
10533#endif
10534#ifdef UNIX
10535 "unix",
10536#endif
10537#ifdef VMS
10538 "vms",
10539#endif
10540#ifdef WIN16
10541 "win16",
10542#endif
10543#ifdef WIN32
10544 "win32",
10545#endif
10546#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10547 "win32unix",
10548#endif
10549#ifdef WIN64
10550 "win64",
10551#endif
10552#ifdef EBCDIC
10553 "ebcdic",
10554#endif
10555#ifndef CASE_INSENSITIVE_FILENAME
10556 "fname_case",
10557#endif
10558#ifdef FEAT_ARABIC
10559 "arabic",
10560#endif
10561#ifdef FEAT_AUTOCMD
10562 "autocmd",
10563#endif
10564#ifdef FEAT_BEVAL
10565 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010566# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10567 "balloon_multiline",
10568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#endif
10570#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10571 "builtin_terms",
10572# ifdef ALL_BUILTIN_TCAPS
10573 "all_builtin_terms",
10574# endif
10575#endif
10576#ifdef FEAT_BYTEOFF
10577 "byte_offset",
10578#endif
10579#ifdef FEAT_CINDENT
10580 "cindent",
10581#endif
10582#ifdef FEAT_CLIENTSERVER
10583 "clientserver",
10584#endif
10585#ifdef FEAT_CLIPBOARD
10586 "clipboard",
10587#endif
10588#ifdef FEAT_CMDL_COMPL
10589 "cmdline_compl",
10590#endif
10591#ifdef FEAT_CMDHIST
10592 "cmdline_hist",
10593#endif
10594#ifdef FEAT_COMMENTS
10595 "comments",
10596#endif
10597#ifdef FEAT_CRYPT
10598 "cryptv",
10599#endif
10600#ifdef FEAT_CSCOPE
10601 "cscope",
10602#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010603#ifdef CURSOR_SHAPE
10604 "cursorshape",
10605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606#ifdef DEBUG
10607 "debug",
10608#endif
10609#ifdef FEAT_CON_DIALOG
10610 "dialog_con",
10611#endif
10612#ifdef FEAT_GUI_DIALOG
10613 "dialog_gui",
10614#endif
10615#ifdef FEAT_DIFF
10616 "diff",
10617#endif
10618#ifdef FEAT_DIGRAPHS
10619 "digraphs",
10620#endif
10621#ifdef FEAT_DND
10622 "dnd",
10623#endif
10624#ifdef FEAT_EMACS_TAGS
10625 "emacs_tags",
10626#endif
10627 "eval", /* always present, of course! */
10628#ifdef FEAT_EX_EXTRA
10629 "ex_extra",
10630#endif
10631#ifdef FEAT_SEARCH_EXTRA
10632 "extra_search",
10633#endif
10634#ifdef FEAT_FKMAP
10635 "farsi",
10636#endif
10637#ifdef FEAT_SEARCHPATH
10638 "file_in_path",
10639#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010640#if defined(UNIX) && !defined(USE_SYSTEM)
10641 "filterpipe",
10642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643#ifdef FEAT_FIND_ID
10644 "find_in_path",
10645#endif
10646#ifdef FEAT_FOLDING
10647 "folding",
10648#endif
10649#ifdef FEAT_FOOTER
10650 "footer",
10651#endif
10652#if !defined(USE_SYSTEM) && defined(UNIX)
10653 "fork",
10654#endif
10655#ifdef FEAT_GETTEXT
10656 "gettext",
10657#endif
10658#ifdef FEAT_GUI
10659 "gui",
10660#endif
10661#ifdef FEAT_GUI_ATHENA
10662# ifdef FEAT_GUI_NEXTAW
10663 "gui_neXtaw",
10664# else
10665 "gui_athena",
10666# endif
10667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668#ifdef FEAT_GUI_GTK
10669 "gui_gtk",
10670# ifdef HAVE_GTK2
10671 "gui_gtk2",
10672# endif
10673#endif
10674#ifdef FEAT_GUI_MAC
10675 "gui_mac",
10676#endif
10677#ifdef FEAT_GUI_MOTIF
10678 "gui_motif",
10679#endif
10680#ifdef FEAT_GUI_PHOTON
10681 "gui_photon",
10682#endif
10683#ifdef FEAT_GUI_W16
10684 "gui_win16",
10685#endif
10686#ifdef FEAT_GUI_W32
10687 "gui_win32",
10688#endif
10689#ifdef FEAT_HANGULIN
10690 "hangul_input",
10691#endif
10692#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10693 "iconv",
10694#endif
10695#ifdef FEAT_INS_EXPAND
10696 "insert_expand",
10697#endif
10698#ifdef FEAT_JUMPLIST
10699 "jumplist",
10700#endif
10701#ifdef FEAT_KEYMAP
10702 "keymap",
10703#endif
10704#ifdef FEAT_LANGMAP
10705 "langmap",
10706#endif
10707#ifdef FEAT_LIBCALL
10708 "libcall",
10709#endif
10710#ifdef FEAT_LINEBREAK
10711 "linebreak",
10712#endif
10713#ifdef FEAT_LISP
10714 "lispindent",
10715#endif
10716#ifdef FEAT_LISTCMDS
10717 "listcmds",
10718#endif
10719#ifdef FEAT_LOCALMAP
10720 "localmap",
10721#endif
10722#ifdef FEAT_MENU
10723 "menu",
10724#endif
10725#ifdef FEAT_SESSION
10726 "mksession",
10727#endif
10728#ifdef FEAT_MODIFY_FNAME
10729 "modify_fname",
10730#endif
10731#ifdef FEAT_MOUSE
10732 "mouse",
10733#endif
10734#ifdef FEAT_MOUSESHAPE
10735 "mouseshape",
10736#endif
10737#if defined(UNIX) || defined(VMS)
10738# ifdef FEAT_MOUSE_DEC
10739 "mouse_dec",
10740# endif
10741# ifdef FEAT_MOUSE_GPM
10742 "mouse_gpm",
10743# endif
10744# ifdef FEAT_MOUSE_JSB
10745 "mouse_jsbterm",
10746# endif
10747# ifdef FEAT_MOUSE_NET
10748 "mouse_netterm",
10749# endif
10750# ifdef FEAT_MOUSE_PTERM
10751 "mouse_pterm",
10752# endif
10753# ifdef FEAT_MOUSE_XTERM
10754 "mouse_xterm",
10755# endif
10756#endif
10757#ifdef FEAT_MBYTE
10758 "multi_byte",
10759#endif
10760#ifdef FEAT_MBYTE_IME
10761 "multi_byte_ime",
10762#endif
10763#ifdef FEAT_MULTI_LANG
10764 "multi_lang",
10765#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010766#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010767#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010768 "mzscheme",
10769#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771#ifdef FEAT_OLE
10772 "ole",
10773#endif
10774#ifdef FEAT_OSFILETYPE
10775 "osfiletype",
10776#endif
10777#ifdef FEAT_PATH_EXTRA
10778 "path_extra",
10779#endif
10780#ifdef FEAT_PERL
10781#ifndef DYNAMIC_PERL
10782 "perl",
10783#endif
10784#endif
10785#ifdef FEAT_PYTHON
10786#ifndef DYNAMIC_PYTHON
10787 "python",
10788#endif
10789#endif
10790#ifdef FEAT_POSTSCRIPT
10791 "postscript",
10792#endif
10793#ifdef FEAT_PRINTER
10794 "printer",
10795#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010796#ifdef FEAT_PROFILE
10797 "profile",
10798#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010799#ifdef FEAT_RELTIME
10800 "reltime",
10801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802#ifdef FEAT_QUICKFIX
10803 "quickfix",
10804#endif
10805#ifdef FEAT_RIGHTLEFT
10806 "rightleft",
10807#endif
10808#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10809 "ruby",
10810#endif
10811#ifdef FEAT_SCROLLBIND
10812 "scrollbind",
10813#endif
10814#ifdef FEAT_CMDL_INFO
10815 "showcmd",
10816 "cmdline_info",
10817#endif
10818#ifdef FEAT_SIGNS
10819 "signs",
10820#endif
10821#ifdef FEAT_SMARTINDENT
10822 "smartindent",
10823#endif
10824#ifdef FEAT_SNIFF
10825 "sniff",
10826#endif
10827#ifdef FEAT_STL_OPT
10828 "statusline",
10829#endif
10830#ifdef FEAT_SUN_WORKSHOP
10831 "sun_workshop",
10832#endif
10833#ifdef FEAT_NETBEANS_INTG
10834 "netbeans_intg",
10835#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010836#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010837 "spell",
10838#endif
10839#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 "syntax",
10841#endif
10842#if defined(USE_SYSTEM) || !defined(UNIX)
10843 "system",
10844#endif
10845#ifdef FEAT_TAG_BINS
10846 "tag_binary",
10847#endif
10848#ifdef FEAT_TAG_OLDSTATIC
10849 "tag_old_static",
10850#endif
10851#ifdef FEAT_TAG_ANYWHITE
10852 "tag_any_white",
10853#endif
10854#ifdef FEAT_TCL
10855# ifndef DYNAMIC_TCL
10856 "tcl",
10857# endif
10858#endif
10859#ifdef TERMINFO
10860 "terminfo",
10861#endif
10862#ifdef FEAT_TERMRESPONSE
10863 "termresponse",
10864#endif
10865#ifdef FEAT_TEXTOBJ
10866 "textobjects",
10867#endif
10868#ifdef HAVE_TGETENT
10869 "tgetent",
10870#endif
10871#ifdef FEAT_TITLE
10872 "title",
10873#endif
10874#ifdef FEAT_TOOLBAR
10875 "toolbar",
10876#endif
10877#ifdef FEAT_USR_CMDS
10878 "user-commands", /* was accidentally included in 5.4 */
10879 "user_commands",
10880#endif
10881#ifdef FEAT_VIMINFO
10882 "viminfo",
10883#endif
10884#ifdef FEAT_VERTSPLIT
10885 "vertsplit",
10886#endif
10887#ifdef FEAT_VIRTUALEDIT
10888 "virtualedit",
10889#endif
10890#ifdef FEAT_VISUAL
10891 "visual",
10892#endif
10893#ifdef FEAT_VISUALEXTRA
10894 "visualextra",
10895#endif
10896#ifdef FEAT_VREPLACE
10897 "vreplace",
10898#endif
10899#ifdef FEAT_WILDIGN
10900 "wildignore",
10901#endif
10902#ifdef FEAT_WILDMENU
10903 "wildmenu",
10904#endif
10905#ifdef FEAT_WINDOWS
10906 "windows",
10907#endif
10908#ifdef FEAT_WAK
10909 "winaltkeys",
10910#endif
10911#ifdef FEAT_WRITEBACKUP
10912 "writebackup",
10913#endif
10914#ifdef FEAT_XIM
10915 "xim",
10916#endif
10917#ifdef FEAT_XFONTSET
10918 "xfontset",
10919#endif
10920#ifdef USE_XSMP
10921 "xsmp",
10922#endif
10923#ifdef USE_XSMP_INTERACT
10924 "xsmp_interact",
10925#endif
10926#ifdef FEAT_XCLIPBOARD
10927 "xterm_clipboard",
10928#endif
10929#ifdef FEAT_XTERM_SAVE
10930 "xterm_save",
10931#endif
10932#if defined(UNIX) && defined(FEAT_X11)
10933 "X11",
10934#endif
10935 NULL
10936 };
10937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 for (i = 0; has_list[i] != NULL; ++i)
10940 if (STRICMP(name, has_list[i]) == 0)
10941 {
10942 n = TRUE;
10943 break;
10944 }
10945
10946 if (n == FALSE)
10947 {
10948 if (STRNICMP(name, "patch", 5) == 0)
10949 n = has_patch(atoi((char *)name + 5));
10950 else if (STRICMP(name, "vim_starting") == 0)
10951 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010952#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10953 else if (STRICMP(name, "balloon_multiline") == 0)
10954 n = multiline_balloon_available();
10955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956#ifdef DYNAMIC_TCL
10957 else if (STRICMP(name, "tcl") == 0)
10958 n = tcl_enabled(FALSE);
10959#endif
10960#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10961 else if (STRICMP(name, "iconv") == 0)
10962 n = iconv_enabled(FALSE);
10963#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010964#ifdef DYNAMIC_MZSCHEME
10965 else if (STRICMP(name, "mzscheme") == 0)
10966 n = mzscheme_enabled(FALSE);
10967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968#ifdef DYNAMIC_RUBY
10969 else if (STRICMP(name, "ruby") == 0)
10970 n = ruby_enabled(FALSE);
10971#endif
10972#ifdef DYNAMIC_PYTHON
10973 else if (STRICMP(name, "python") == 0)
10974 n = python_enabled(FALSE);
10975#endif
10976#ifdef DYNAMIC_PERL
10977 else if (STRICMP(name, "perl") == 0)
10978 n = perl_enabled(FALSE);
10979#endif
10980#ifdef FEAT_GUI
10981 else if (STRICMP(name, "gui_running") == 0)
10982 n = (gui.in_use || gui.starting);
10983# ifdef FEAT_GUI_W32
10984 else if (STRICMP(name, "gui_win32s") == 0)
10985 n = gui_is_win32s();
10986# endif
10987# ifdef FEAT_BROWSE
10988 else if (STRICMP(name, "browse") == 0)
10989 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10990# endif
10991#endif
10992#ifdef FEAT_SYN_HL
10993 else if (STRICMP(name, "syntax_items") == 0)
10994 n = syntax_present(curbuf);
10995#endif
10996#if defined(WIN3264)
10997 else if (STRICMP(name, "win95") == 0)
10998 n = mch_windows95();
10999#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011000#ifdef FEAT_NETBEANS_INTG
11001 else if (STRICMP(name, "netbeans_enabled") == 0)
11002 n = usingNetbeans;
11003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 }
11005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 * "has_key()" function
11011 */
11012 static void
11013f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016{
11017 rettv->vval.v_number = 0;
11018 if (argvars[0].v_type != VAR_DICT)
11019 {
11020 EMSG(_(e_dictreq));
11021 return;
11022 }
11023 if (argvars[0].vval.v_dict == NULL)
11024 return;
11025
11026 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011027 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011028}
11029
11030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 * "hasmapto()" function
11032 */
11033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038 char_u *name;
11039 char_u *mode;
11040 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011041 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 mode = (char_u *)"nvo";
11046 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011049 if (argvars[2].v_type != VAR_UNKNOWN)
11050 abbr = get_tv_number(&argvars[2]);
11051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052
Bram Moolenaar2c932302006-03-18 21:42:09 +000011053 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057}
11058
11059/*
11060 * "histadd()" function
11061 */
11062/*ARGSUSED*/
11063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011065 typval_T *argvars;
11066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067{
11068#ifdef FEAT_CMDHIST
11069 int histype;
11070 char_u *str;
11071 char_u buf[NUMBUFLEN];
11072#endif
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if (check_restricted() || check_secure())
11076 return;
11077#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11079 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 if (histype >= 0)
11081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 if (*str != NUL)
11084 {
11085 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 return;
11088 }
11089 }
11090#endif
11091}
11092
11093/*
11094 * "histdel()" function
11095 */
11096/*ARGSUSED*/
11097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101{
11102#ifdef FEAT_CMDHIST
11103 int n;
11104 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11108 if (str == NULL)
11109 n = 0;
11110 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011113 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 else
11118 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 get_tv_string_buf(&argvars[1], buf));
11121 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124#endif
11125}
11126
11127/*
11128 * "histget()" function
11129 */
11130/*ARGSUSED*/
11131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 typval_T *argvars;
11134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
11136#ifdef FEAT_CMDHIST
11137 int type;
11138 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11142 if (str == NULL)
11143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 {
11146 type = get_histtype(str);
11147 if (argvars[1].v_type == VAR_UNKNOWN)
11148 idx = get_history_idx(type);
11149 else
11150 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11151 /* -1 on type error */
11152 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159
11160/*
11161 * "histnr()" function
11162 */
11163/*ARGSUSED*/
11164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *argvars;
11167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
11169 int i;
11170
11171#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 char_u *history = get_tv_string_chk(&argvars[0]);
11173
11174 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 if (i >= HIST_CMD && i < HIST_COUNT)
11176 i = get_history_idx(i);
11177 else
11178#endif
11179 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181}
11182
11183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 * "highlightID(name)" function
11185 */
11186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192}
11193
11194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011195 * "highlight_exists()" function
11196 */
11197 static void
11198f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011199 typval_T *argvars;
11200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201{
11202 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11203}
11204
11205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 * "hostname()" function
11207 */
11208/*ARGSUSED*/
11209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
11214 char_u hostname[256];
11215
11216 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->v_type = VAR_STRING;
11218 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219}
11220
11221/*
11222 * iconv() function
11223 */
11224/*ARGSUSED*/
11225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 typval_T *argvars;
11228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229{
11230#ifdef FEAT_MBYTE
11231 char_u buf1[NUMBUFLEN];
11232 char_u buf2[NUMBUFLEN];
11233 char_u *from, *to, *str;
11234 vimconv_T vimconv;
11235#endif
11236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239
11240#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 str = get_tv_string(&argvars[0]);
11242 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11243 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 vimconv.vc_type = CONV_NONE;
11245 convert_setup(&vimconv, from, to);
11246
11247 /* If the encodings are equal, no conversion needed. */
11248 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252
11253 convert_setup(&vimconv, NULL, NULL);
11254 vim_free(from);
11255 vim_free(to);
11256#endif
11257}
11258
11259/*
11260 * "indent()" function
11261 */
11262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011264 typval_T *argvars;
11265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266{
11267 linenr_T lnum;
11268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274}
11275
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011276/*
11277 * "index()" function
11278 */
11279 static void
11280f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011283{
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 list_T *l;
11285 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011286 long idx = 0;
11287 int ic = FALSE;
11288
11289 rettv->vval.v_number = -1;
11290 if (argvars[0].v_type != VAR_LIST)
11291 {
11292 EMSG(_(e_listreq));
11293 return;
11294 }
11295 l = argvars[0].vval.v_list;
11296 if (l != NULL)
11297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011298 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 int error = FALSE;
11302
Bram Moolenaar758711c2005-02-02 23:11:38 +000011303 /* Start at specified item. Use the cached index that list_find()
11304 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011306 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011307 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 ic = get_tv_number_chk(&argvars[3], &error);
11309 if (error)
11310 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011312
Bram Moolenaar758711c2005-02-02 23:11:38 +000011313 for ( ; item != NULL; item = item->li_next, ++idx)
11314 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011315 {
11316 rettv->vval.v_number = idx;
11317 break;
11318 }
11319 }
11320}
11321
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322static int inputsecret_flag = 0;
11323
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011324static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11325
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011327 * This function is used by f_input() and f_inputdialog() functions. The third
11328 * argument to f_input() specifies the type of completion to use at the
11329 * prompt. The third argument to f_inputdialog() specifies the value to return
11330 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 */
11332 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011333get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 typval_T *argvars;
11335 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011336 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 char_u *p = NULL;
11340 int c;
11341 char_u buf[NUMBUFLEN];
11342 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011344 int xp_type = EXPAND_NOTHING;
11345 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348
11349#ifdef NO_CONSOLE_INPUT
11350 /* While starting up, there is no place to enter text. */
11351 if (no_console_input())
11352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 return;
11355 }
11356#endif
11357
11358 cmd_silent = FALSE; /* Want to see the prompt. */
11359 if (prompt != NULL)
11360 {
11361 /* Only the part of the message after the last NL is considered as
11362 * prompt for the command line */
11363 p = vim_strrchr(prompt, '\n');
11364 if (p == NULL)
11365 p = prompt;
11366 else
11367 {
11368 ++p;
11369 c = *p;
11370 *p = NUL;
11371 msg_start();
11372 msg_clr_eos();
11373 msg_puts_attr(prompt, echo_attr);
11374 msg_didout = FALSE;
11375 msg_starthere();
11376 *p = c;
11377 }
11378 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 if (argvars[1].v_type != VAR_UNKNOWN)
11381 {
11382 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11383 if (defstr != NULL)
11384 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011386 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011387 {
11388 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011389 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011390 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011391
Bram Moolenaar4463f292005-09-25 22:20:24 +000011392 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011393
Bram Moolenaar4463f292005-09-25 22:20:24 +000011394 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11395 if (xp_name == NULL)
11396 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011397
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011398 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011399
Bram Moolenaar4463f292005-09-25 22:20:24 +000011400 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11401 &xp_arg) == FAIL)
11402 return;
11403 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011404 }
11405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 if (defstr != NULL)
11407 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011408 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11409 xp_type, xp_arg);
11410
11411 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 /* since the user typed this, no need to wait for return */
11414 need_wait_return = FALSE;
11415 msg_didout = FALSE;
11416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 cmd_silent = cmd_silent_save;
11418}
11419
11420/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011421 * "input()" function
11422 * Also handles inputsecret() when inputsecret is set.
11423 */
11424 static void
11425f_input(argvars, rettv)
11426 typval_T *argvars;
11427 typval_T *rettv;
11428{
11429 get_user_input(argvars, rettv, FALSE);
11430}
11431
11432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 * "inputdialog()" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440#if defined(FEAT_GUI_TEXTDIALOG)
11441 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11442 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11443 {
11444 char_u *message;
11445 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 message = get_tv_string_chk(&argvars[0]);
11449 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011450 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011451 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 else
11453 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 if (message != NULL && defstr != NULL
11455 && do_dialog(VIM_QUESTION, NULL, message,
11456 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 else
11459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 if (message != NULL && defstr != NULL
11461 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011462 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463 rettv->vval.v_string = vim_strsave(
11464 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 }
11470 else
11471#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011472 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473}
11474
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011475/*
11476 * "inputlist()" function
11477 */
11478 static void
11479f_inputlist(argvars, rettv)
11480 typval_T *argvars;
11481 typval_T *rettv;
11482{
11483 listitem_T *li;
11484 int selected;
11485 int mouse_used;
11486
11487 rettv->vval.v_number = 0;
11488#ifdef NO_CONSOLE_INPUT
11489 /* While starting up, there is no place to enter text. */
11490 if (no_console_input())
11491 return;
11492#endif
11493 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11494 {
11495 EMSG2(_(e_listarg), "inputlist()");
11496 return;
11497 }
11498
11499 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011500 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011501 lines_left = Rows; /* avoid more prompt */
11502 msg_scroll = TRUE;
11503 msg_clr_eos();
11504
11505 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11506 {
11507 msg_puts(get_tv_string(&li->li_tv));
11508 msg_putchar('\n');
11509 }
11510
11511 /* Ask for choice. */
11512 selected = prompt_for_number(&mouse_used);
11513 if (mouse_used)
11514 selected -= lines_left;
11515
11516 rettv->vval.v_number = selected;
11517}
11518
11519
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11521
11522/*
11523 * "inputrestore()" function
11524 */
11525/*ARGSUSED*/
11526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 if (ga_userinput.ga_len > 0)
11532 {
11533 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11535 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 }
11538 else if (p_verbose > 1)
11539 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011540 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543}
11544
11545/*
11546 * "inputsave()" function
11547 */
11548/*ARGSUSED*/
11549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011551 typval_T *argvars;
11552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553{
11554 /* Add an entry to the stack of typehead storage. */
11555 if (ga_grow(&ga_userinput, 1) == OK)
11556 {
11557 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11558 + ga_userinput.ga_len);
11559 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 }
11562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564}
11565
11566/*
11567 * "inputsecret()" function
11568 */
11569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574 ++cmdline_star;
11575 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 --cmdline_star;
11578 --inputsecret_flag;
11579}
11580
11581/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011582 * "insert()" function
11583 */
11584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 typval_T *argvars;
11587 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011588{
11589 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011590 listitem_T *item;
11591 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011594 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011595 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011597 else if ((l = argvars[0].vval.v_list) != NULL
11598 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599 {
11600 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011601 before = get_tv_number_chk(&argvars[2], &error);
11602 if (error)
11603 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604
Bram Moolenaar758711c2005-02-02 23:11:38 +000011605 if (before == l->lv_len)
11606 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011607 else
11608 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011609 item = list_find(l, before);
11610 if (item == NULL)
11611 {
11612 EMSGN(_(e_listidx), before);
11613 l = NULL;
11614 }
11615 }
11616 if (l != NULL)
11617 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011618 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011619 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011620 }
11621 }
11622}
11623
11624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 * "isdirectory()" function
11626 */
11627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011629 typval_T *argvars;
11630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011635/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011636 * "islocked()" function
11637 */
11638 static void
11639f_islocked(argvars, rettv)
11640 typval_T *argvars;
11641 typval_T *rettv;
11642{
11643 lval_T lv;
11644 char_u *end;
11645 dictitem_T *di;
11646
11647 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011648 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11649 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011650 if (end != NULL && lv.ll_name != NULL)
11651 {
11652 if (*end != NUL)
11653 EMSG(_(e_trailing));
11654 else
11655 {
11656 if (lv.ll_tv == NULL)
11657 {
11658 if (check_changedtick(lv.ll_name))
11659 rettv->vval.v_number = 1; /* always locked */
11660 else
11661 {
11662 di = find_var(lv.ll_name, NULL);
11663 if (di != NULL)
11664 {
11665 /* Consider a variable locked when:
11666 * 1. the variable itself is locked
11667 * 2. the value of the variable is locked.
11668 * 3. the List or Dict value is locked.
11669 */
11670 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11671 || tv_islocked(&di->di_tv));
11672 }
11673 }
11674 }
11675 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011676 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011677 else if (lv.ll_newkey != NULL)
11678 EMSG2(_(e_dictkey), lv.ll_newkey);
11679 else if (lv.ll_list != NULL)
11680 /* List item. */
11681 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11682 else
11683 /* Dictionary item. */
11684 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11685 }
11686 }
11687
11688 clear_lval(&lv);
11689}
11690
Bram Moolenaar33570922005-01-25 22:26:29 +000011691static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011692
11693/*
11694 * Turn a dict into a list:
11695 * "what" == 0: list of keys
11696 * "what" == 1: list of values
11697 * "what" == 2: list of items
11698 */
11699 static void
11700dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011701 typval_T *argvars;
11702 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011703 int what;
11704{
Bram Moolenaar33570922005-01-25 22:26:29 +000011705 list_T *l2;
11706 dictitem_T *di;
11707 hashitem_T *hi;
11708 listitem_T *li;
11709 listitem_T *li2;
11710 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011711 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011712
11713 rettv->vval.v_number = 0;
11714 if (argvars[0].v_type != VAR_DICT)
11715 {
11716 EMSG(_(e_dictreq));
11717 return;
11718 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011719 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011720 return;
11721
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011722 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011723 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011724
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011725 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011726 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011727 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011728 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011730 --todo;
11731 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011732
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011733 li = listitem_alloc();
11734 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011735 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011736 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011737
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011738 if (what == 0)
11739 {
11740 /* keys() */
11741 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011742 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011743 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11744 }
11745 else if (what == 1)
11746 {
11747 /* values() */
11748 copy_tv(&di->di_tv, &li->li_tv);
11749 }
11750 else
11751 {
11752 /* items() */
11753 l2 = list_alloc();
11754 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011755 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011756 li->li_tv.vval.v_list = l2;
11757 if (l2 == NULL)
11758 break;
11759 ++l2->lv_refcount;
11760
11761 li2 = listitem_alloc();
11762 if (li2 == NULL)
11763 break;
11764 list_append(l2, li2);
11765 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011766 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011767 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11768
11769 li2 = listitem_alloc();
11770 if (li2 == NULL)
11771 break;
11772 list_append(l2, li2);
11773 copy_tv(&di->di_tv, &li2->li_tv);
11774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011775 }
11776 }
11777}
11778
11779/*
11780 * "items(dict)" function
11781 */
11782 static void
11783f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *argvars;
11785 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011786{
11787 dict_list(argvars, rettv, 2);
11788}
11789
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011791 * "join()" function
11792 */
11793 static void
11794f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011797{
11798 garray_T ga;
11799 char_u *sep;
11800
11801 rettv->vval.v_number = 0;
11802 if (argvars[0].v_type != VAR_LIST)
11803 {
11804 EMSG(_(e_listreq));
11805 return;
11806 }
11807 if (argvars[0].vval.v_list == NULL)
11808 return;
11809 if (argvars[1].v_type == VAR_UNKNOWN)
11810 sep = (char_u *)" ";
11811 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011813
11814 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815
11816 if (sep != NULL)
11817 {
11818 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011819 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 ga_append(&ga, NUL);
11821 rettv->vval.v_string = (char_u *)ga.ga_data;
11822 }
11823 else
11824 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825}
11826
11827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011828 * "keys()" function
11829 */
11830 static void
11831f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011832 typval_T *argvars;
11833 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011834{
11835 dict_list(argvars, rettv, 0);
11836}
11837
11838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 * "last_buffer_nr()" function.
11840 */
11841/*ARGSUSED*/
11842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 typval_T *argvars;
11845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846{
11847 int n = 0;
11848 buf_T *buf;
11849
11850 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11851 if (n < buf->b_fnum)
11852 n = buf->b_fnum;
11853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855}
11856
11857/*
11858 * "len()" function
11859 */
11860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011864{
11865 switch (argvars[0].v_type)
11866 {
11867 case VAR_STRING:
11868 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number = (varnumber_T)STRLEN(
11870 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011871 break;
11872 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011875 case VAR_DICT:
11876 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11877 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011878 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011879 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011880 break;
11881 }
11882}
11883
Bram Moolenaar33570922005-01-25 22:26:29 +000011884static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011885
11886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011888 typval_T *argvars;
11889 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011890 int type;
11891{
11892#ifdef FEAT_LIBCALL
11893 char_u *string_in;
11894 char_u **string_result;
11895 int nr_result;
11896#endif
11897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011899 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011903
11904 if (check_restricted() || check_secure())
11905 return;
11906
11907#ifdef FEAT_LIBCALL
11908 /* The first two args must be strings, otherwise its meaningless */
11909 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11910 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011911 string_in = NULL;
11912 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011913 string_in = argvars[2].vval.v_string;
11914 if (type == VAR_NUMBER)
11915 string_result = NULL;
11916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011918 if (mch_libcall(argvars[0].vval.v_string,
11919 argvars[1].vval.v_string,
11920 string_in,
11921 argvars[2].vval.v_number,
11922 string_result,
11923 &nr_result) == OK
11924 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011926 }
11927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011931 * "libcall()" function
11932 */
11933 static void
11934f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *argvars;
11936 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011937{
11938 libcall_common(argvars, rettv, VAR_STRING);
11939}
11940
11941/*
11942 * "libcallnr()" function
11943 */
11944 static void
11945f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 typval_T *argvars;
11947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948{
11949 libcall_common(argvars, rettv, VAR_NUMBER);
11950}
11951
11952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 * "line(string)" function
11954 */
11955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *argvars;
11958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959{
11960 linenr_T lnum = 0;
11961 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011962 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011964 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 if (fp != NULL)
11966 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968}
11969
11970/*
11971 * "line2byte(lnum)" function
11972 */
11973/*ARGSUSED*/
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
11979#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981#else
11982 linenr_T lnum;
11983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11989 if (rettv->vval.v_number >= 0)
11990 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991#endif
11992}
11993
11994/*
11995 * "lispindent(lnum)" function
11996 */
11997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *argvars;
12000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
12002#ifdef FEAT_LISP
12003 pos_T pos;
12004 linenr_T lnum;
12005
12006 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12009 {
12010 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 curwin->w_cursor = pos;
12013 }
12014 else
12015#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017}
12018
12019/*
12020 * "localtime()" function
12021 */
12022/*ARGSUSED*/
12023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012025 typval_T *argvars;
12026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029}
12030
Bram Moolenaar33570922005-01-25 22:26:29 +000012031static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
12033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012035 typval_T *argvars;
12036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 int exact;
12038{
12039 char_u *keys;
12040 char_u *which;
12041 char_u buf[NUMBUFLEN];
12042 char_u *keys_buf = NULL;
12043 char_u *rhs;
12044 int mode;
12045 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012046 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
12048 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 rettv->v_type = VAR_STRING;
12050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 if (*keys == NUL)
12054 return;
12055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012056 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012059 if (argvars[2].v_type != VAR_UNKNOWN)
12060 abbr = get_tv_number(&argvars[2]);
12061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 else
12063 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012064 if (which == NULL)
12065 return;
12066
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 mode = get_map_mode(&which, 0);
12068
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012069 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012070 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 vim_free(keys_buf);
12072 if (rhs != NULL)
12073 {
12074 ga_init(&ga);
12075 ga.ga_itemsize = 1;
12076 ga.ga_growsize = 40;
12077
12078 while (*rhs != NUL)
12079 ga_concat(&ga, str2special(&rhs, FALSE));
12080
12081 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 }
12084}
12085
12086/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012087 * "map()" function
12088 */
12089 static void
12090f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012091 typval_T *argvars;
12092 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012093{
12094 filter_map(argvars, rettv, TRUE);
12095}
12096
12097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012098 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 */
12100 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012101f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 typval_T *argvars;
12103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106}
12107
12108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012109 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 */
12111 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012112f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 typval_T *argvars;
12114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117}
12118
Bram Moolenaar33570922005-01-25 22:26:29 +000012119static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120
12121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 typval_T *argvars;
12124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 int type;
12126{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012127 char_u *str = NULL;
12128 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 char_u *pat;
12130 regmatch_T regmatch;
12131 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012132 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 char_u *save_cpo;
12134 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012135 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012136 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012137 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 list_T *l = NULL;
12139 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012140 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012141 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142
12143 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12144 save_cpo = p_cpo;
12145 p_cpo = (char_u *)"";
12146
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012147 rettv->vval.v_number = -1;
12148 if (type == 3)
12149 {
12150 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012151 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012152 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012153 }
12154 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->v_type = VAR_STRING;
12157 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012160 if (argvars[0].v_type == VAR_LIST)
12161 {
12162 if ((l = argvars[0].vval.v_list) == NULL)
12163 goto theend;
12164 li = l->lv_first;
12165 }
12166 else
12167 expr = str = get_tv_string(&argvars[0]);
12168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12170 if (pat == NULL)
12171 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012172
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 int error = FALSE;
12176
12177 start = get_tv_number_chk(&argvars[2], &error);
12178 if (error)
12179 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012180 if (l != NULL)
12181 {
12182 li = list_find(l, start);
12183 if (li == NULL)
12184 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012185 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012186 }
12187 else
12188 {
12189 if (start < 0)
12190 start = 0;
12191 if (start > (long)STRLEN(str))
12192 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012193 /* When "count" argument is there ignore matches before "start",
12194 * otherwise skip part of the string. Differs when pattern is "^"
12195 * or "\<". */
12196 if (argvars[3].v_type != VAR_UNKNOWN)
12197 startcol = start;
12198 else
12199 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012200 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012201
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012202 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012203 nth = get_tv_number_chk(&argvars[3], &error);
12204 if (error)
12205 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 }
12207
12208 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12209 if (regmatch.regprog != NULL)
12210 {
12211 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012212
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012213 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012214 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012215 if (l != NULL)
12216 {
12217 if (li == NULL)
12218 {
12219 match = FALSE;
12220 break;
12221 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012222 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012223 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012224 if (str == NULL)
12225 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012226 }
12227
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012228 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012229
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012230 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012231 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012232 if (l == NULL && !match)
12233 break;
12234
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012235 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012236 if (l != NULL)
12237 {
12238 li = li->li_next;
12239 ++idx;
12240 }
12241 else
12242 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012243#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012244 startcol = (colnr_T)(regmatch.startp[0]
12245 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012246#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012247 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012248#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012249 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012250 }
12251
12252 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012254 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012256 int i;
12257
12258 /* return list with matched string and submatches */
12259 for (i = 0; i < NSUBEXP; ++i)
12260 {
12261 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012262 {
12263 if (list_append_string(rettv->vval.v_list,
12264 (char_u *)"", 0) == FAIL)
12265 break;
12266 }
12267 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012268 regmatch.startp[i],
12269 (int)(regmatch.endp[i] - regmatch.startp[i]))
12270 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012271 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012272 }
12273 }
12274 else if (type == 2)
12275 {
12276 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012277 if (l != NULL)
12278 copy_tv(&li->li_tv, rettv);
12279 else
12280 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012282 }
12283 else if (l != NULL)
12284 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 else
12286 {
12287 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 (varnumber_T)(regmatch.startp[0] - str);
12290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012293 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 }
12295 }
12296 vim_free(regmatch.regprog);
12297 }
12298
12299theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012300 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 p_cpo = save_cpo;
12302}
12303
12304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305 * "match()" function
12306 */
12307 static void
12308f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *argvars;
12310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012311{
12312 find_some_match(argvars, rettv, 1);
12313}
12314
12315/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012316 * "matcharg()" function
12317 */
12318 static void
12319f_matcharg(argvars, rettv)
12320 typval_T *argvars;
12321 typval_T *rettv;
12322{
12323 if (rettv_list_alloc(rettv) == OK)
12324 {
12325#ifdef FEAT_SEARCH_EXTRA
12326 int mi = get_tv_number(&argvars[0]);
12327
12328 if (mi >= 1 && mi <= 3)
12329 {
12330 list_append_string(rettv->vval.v_list,
12331 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12332 list_append_string(rettv->vval.v_list,
12333 curwin->w_match_pat[mi - 1], -1);
12334 }
12335#endif
12336 }
12337}
12338
12339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012340 * "matchend()" function
12341 */
12342 static void
12343f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012344 typval_T *argvars;
12345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012346{
12347 find_some_match(argvars, rettv, 0);
12348}
12349
12350/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012351 * "matchlist()" function
12352 */
12353 static void
12354f_matchlist(argvars, rettv)
12355 typval_T *argvars;
12356 typval_T *rettv;
12357{
12358 find_some_match(argvars, rettv, 3);
12359}
12360
12361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362 * "matchstr()" function
12363 */
12364 static void
12365f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *argvars;
12367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368{
12369 find_some_match(argvars, rettv, 2);
12370}
12371
Bram Moolenaar33570922005-01-25 22:26:29 +000012372static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012373
12374 static void
12375max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *argvars;
12377 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012378 int domax;
12379{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012380 long n = 0;
12381 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012383
12384 if (argvars[0].v_type == VAR_LIST)
12385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012386 list_T *l;
12387 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012388
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012389 l = argvars[0].vval.v_list;
12390 if (l != NULL)
12391 {
12392 li = l->lv_first;
12393 if (li != NULL)
12394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012395 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012396 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012397 {
12398 li = li->li_next;
12399 if (li == NULL)
12400 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012402 if (domax ? i > n : i < n)
12403 n = i;
12404 }
12405 }
12406 }
12407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 else if (argvars[0].v_type == VAR_DICT)
12409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012411 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012412 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012413 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414
12415 d = argvars[0].vval.v_dict;
12416 if (d != NULL)
12417 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012418 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012419 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012421 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012423 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012425 if (first)
12426 {
12427 n = i;
12428 first = FALSE;
12429 }
12430 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 n = i;
12432 }
12433 }
12434 }
12435 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012436 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012437 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012439}
12440
12441/*
12442 * "max()" function
12443 */
12444 static void
12445f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012446 typval_T *argvars;
12447 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012448{
12449 max_min(argvars, rettv, TRUE);
12450}
12451
12452/*
12453 * "min()" function
12454 */
12455 static void
12456f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012457 typval_T *argvars;
12458 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012459{
12460 max_min(argvars, rettv, FALSE);
12461}
12462
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012463static int mkdir_recurse __ARGS((char_u *dir, int prot));
12464
12465/*
12466 * Create the directory in which "dir" is located, and higher levels when
12467 * needed.
12468 */
12469 static int
12470mkdir_recurse(dir, prot)
12471 char_u *dir;
12472 int prot;
12473{
12474 char_u *p;
12475 char_u *updir;
12476 int r = FAIL;
12477
12478 /* Get end of directory name in "dir".
12479 * We're done when it's "/" or "c:/". */
12480 p = gettail_sep(dir);
12481 if (p <= get_past_head(dir))
12482 return OK;
12483
12484 /* If the directory exists we're done. Otherwise: create it.*/
12485 updir = vim_strnsave(dir, (int)(p - dir));
12486 if (updir == NULL)
12487 return FAIL;
12488 if (mch_isdir(updir))
12489 r = OK;
12490 else if (mkdir_recurse(updir, prot) == OK)
12491 r = vim_mkdir_emsg(updir, prot);
12492 vim_free(updir);
12493 return r;
12494}
12495
12496#ifdef vim_mkdir
12497/*
12498 * "mkdir()" function
12499 */
12500 static void
12501f_mkdir(argvars, rettv)
12502 typval_T *argvars;
12503 typval_T *rettv;
12504{
12505 char_u *dir;
12506 char_u buf[NUMBUFLEN];
12507 int prot = 0755;
12508
12509 rettv->vval.v_number = FAIL;
12510 if (check_restricted() || check_secure())
12511 return;
12512
12513 dir = get_tv_string_buf(&argvars[0], buf);
12514 if (argvars[1].v_type != VAR_UNKNOWN)
12515 {
12516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 prot = get_tv_number_chk(&argvars[2], NULL);
12518 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012519 mkdir_recurse(dir, prot);
12520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012521 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012522}
12523#endif
12524
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 * "mode()" function
12527 */
12528/*ARGSUSED*/
12529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 typval_T *argvars;
12532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533{
12534 char_u buf[2];
12535
12536#ifdef FEAT_VISUAL
12537 if (VIsual_active)
12538 {
12539 if (VIsual_select)
12540 buf[0] = VIsual_mode + 's' - 'v';
12541 else
12542 buf[0] = VIsual_mode;
12543 }
12544 else
12545#endif
12546 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12547 buf[0] = 'r';
12548 else if (State & INSERT)
12549 {
12550 if (State & REPLACE_FLAG)
12551 buf[0] = 'R';
12552 else
12553 buf[0] = 'i';
12554 }
12555 else if (State & CMDLINE)
12556 buf[0] = 'c';
12557 else
12558 buf[0] = 'n';
12559
12560 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_string = vim_strsave(buf);
12562 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563}
12564
12565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566 * "nextnonblank()" function
12567 */
12568 static void
12569f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012570 typval_T *argvars;
12571 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572{
12573 linenr_T lnum;
12574
12575 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578 {
12579 lnum = 0;
12580 break;
12581 }
12582 if (*skipwhite(ml_get(lnum)) != NUL)
12583 break;
12584 }
12585 rettv->vval.v_number = lnum;
12586}
12587
12588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589 * "nr2char()" function
12590 */
12591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595{
12596 char_u buf[NUMBUFLEN];
12597
12598#ifdef FEAT_MBYTE
12599 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012600 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601 else
12602#endif
12603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 buf[1] = NUL;
12606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 rettv->v_type = VAR_STRING;
12608 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012609}
12610
12611/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012612 * "pathshorten()" function
12613 */
12614 static void
12615f_pathshorten(argvars, rettv)
12616 typval_T *argvars;
12617 typval_T *rettv;
12618{
12619 char_u *p;
12620
12621 rettv->v_type = VAR_STRING;
12622 p = get_tv_string_chk(&argvars[0]);
12623 if (p == NULL)
12624 rettv->vval.v_string = NULL;
12625 else
12626 {
12627 p = vim_strsave(p);
12628 rettv->vval.v_string = p;
12629 if (p != NULL)
12630 shorten_dir(p);
12631 }
12632}
12633
12634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635 * "prevnonblank()" function
12636 */
12637 static void
12638f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641{
12642 linenr_T lnum;
12643
12644 lnum = get_tv_lnum(argvars);
12645 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12646 lnum = 0;
12647 else
12648 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12649 --lnum;
12650 rettv->vval.v_number = lnum;
12651}
12652
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012653#ifdef HAVE_STDARG_H
12654/* This dummy va_list is here because:
12655 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12656 * - locally in the function results in a "used before set" warning
12657 * - using va_start() to initialize it gives "function with fixed args" error */
12658static va_list ap;
12659#endif
12660
Bram Moolenaar8c711452005-01-14 21:53:12 +000012661/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012662 * "printf()" function
12663 */
12664 static void
12665f_printf(argvars, rettv)
12666 typval_T *argvars;
12667 typval_T *rettv;
12668{
12669 rettv->v_type = VAR_STRING;
12670 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012671#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012672 {
12673 char_u buf[NUMBUFLEN];
12674 int len;
12675 char_u *s;
12676 int saved_did_emsg = did_emsg;
12677 char *fmt;
12678
12679 /* Get the required length, allocate the buffer and do it for real. */
12680 did_emsg = FALSE;
12681 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012682 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012683 if (!did_emsg)
12684 {
12685 s = alloc(len + 1);
12686 if (s != NULL)
12687 {
12688 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012689 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012690 }
12691 }
12692 did_emsg |= saved_did_emsg;
12693 }
12694#endif
12695}
12696
12697/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012698 * "pumvisible()" function
12699 */
12700/*ARGSUSED*/
12701 static void
12702f_pumvisible(argvars, rettv)
12703 typval_T *argvars;
12704 typval_T *rettv;
12705{
12706 rettv->vval.v_number = 0;
12707#ifdef FEAT_INS_EXPAND
12708 if (pum_visible())
12709 rettv->vval.v_number = 1;
12710#endif
12711}
12712
12713/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012714 * "range()" function
12715 */
12716 static void
12717f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012718 typval_T *argvars;
12719 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012720{
12721 long start;
12722 long end;
12723 long stride = 1;
12724 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012728 if (argvars[1].v_type == VAR_UNKNOWN)
12729 {
12730 end = start - 1;
12731 start = 0;
12732 }
12733 else
12734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012736 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012738 }
12739
12740 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 if (error)
12742 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012743 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012744 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012745 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012746 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012747 else
12748 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012749 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012750 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012751 if (list_append_number(rettv->vval.v_list,
12752 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012754 }
12755}
12756
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012757/*
12758 * "readfile()" function
12759 */
12760 static void
12761f_readfile(argvars, rettv)
12762 typval_T *argvars;
12763 typval_T *rettv;
12764{
12765 int binary = FALSE;
12766 char_u *fname;
12767 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012768 listitem_T *li;
12769#define FREAD_SIZE 200 /* optimized for text lines */
12770 char_u buf[FREAD_SIZE];
12771 int readlen; /* size of last fread() */
12772 int buflen; /* nr of valid chars in buf[] */
12773 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12774 int tolist; /* first byte in buf[] still to be put in list */
12775 int chop; /* how many CR to chop off */
12776 char_u *prev = NULL; /* previously read bytes, if any */
12777 int prevlen = 0; /* length of "prev" if not NULL */
12778 char_u *s;
12779 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012780 long maxline = MAXLNUM;
12781 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012782
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012783 if (argvars[1].v_type != VAR_UNKNOWN)
12784 {
12785 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12786 binary = TRUE;
12787 if (argvars[2].v_type != VAR_UNKNOWN)
12788 maxline = get_tv_number(&argvars[2]);
12789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012792 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012793
12794 /* Always open the file in binary mode, library functions have a mind of
12795 * their own about CR-LF conversion. */
12796 fname = get_tv_string(&argvars[0]);
12797 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12798 {
12799 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12800 return;
12801 }
12802
12803 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012804 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012805 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012806 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012807 buflen = filtd + readlen;
12808 tolist = 0;
12809 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12810 {
12811 if (buf[filtd] == '\n' || readlen <= 0)
12812 {
12813 /* Only when in binary mode add an empty list item when the
12814 * last line ends in a '\n'. */
12815 if (!binary && readlen == 0 && filtd == 0)
12816 break;
12817
12818 /* Found end-of-line or end-of-file: add a text line to the
12819 * list. */
12820 chop = 0;
12821 if (!binary)
12822 while (filtd - chop - 1 >= tolist
12823 && buf[filtd - chop - 1] == '\r')
12824 ++chop;
12825 len = filtd - tolist - chop;
12826 if (prev == NULL)
12827 s = vim_strnsave(buf + tolist, len);
12828 else
12829 {
12830 s = alloc((unsigned)(prevlen + len + 1));
12831 if (s != NULL)
12832 {
12833 mch_memmove(s, prev, prevlen);
12834 vim_free(prev);
12835 prev = NULL;
12836 mch_memmove(s + prevlen, buf + tolist, len);
12837 s[prevlen + len] = NUL;
12838 }
12839 }
12840 tolist = filtd + 1;
12841
12842 li = listitem_alloc();
12843 if (li == NULL)
12844 {
12845 vim_free(s);
12846 break;
12847 }
12848 li->li_tv.v_type = VAR_STRING;
12849 li->li_tv.v_lock = 0;
12850 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012851 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012852
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012853 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012854 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012855 if (readlen <= 0)
12856 break;
12857 }
12858 else if (buf[filtd] == NUL)
12859 buf[filtd] = '\n';
12860 }
12861 if (readlen <= 0)
12862 break;
12863
12864 if (tolist == 0)
12865 {
12866 /* "buf" is full, need to move text to an allocated buffer */
12867 if (prev == NULL)
12868 {
12869 prev = vim_strnsave(buf, buflen);
12870 prevlen = buflen;
12871 }
12872 else
12873 {
12874 s = alloc((unsigned)(prevlen + buflen));
12875 if (s != NULL)
12876 {
12877 mch_memmove(s, prev, prevlen);
12878 mch_memmove(s + prevlen, buf, buflen);
12879 vim_free(prev);
12880 prev = s;
12881 prevlen += buflen;
12882 }
12883 }
12884 filtd = 0;
12885 }
12886 else
12887 {
12888 mch_memmove(buf, buf + tolist, buflen - tolist);
12889 filtd -= tolist;
12890 }
12891 }
12892
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012893 /*
12894 * For a negative line count use only the lines at the end of the file,
12895 * free the rest.
12896 */
12897 if (maxline < 0)
12898 while (cnt > -maxline)
12899 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012900 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012901 --cnt;
12902 }
12903
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012904 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012905 fclose(fd);
12906}
12907
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012908#if defined(FEAT_RELTIME)
12909static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12910
12911/*
12912 * Convert a List to proftime_T.
12913 * Return FAIL when there is something wrong.
12914 */
12915 static int
12916list2proftime(arg, tm)
12917 typval_T *arg;
12918 proftime_T *tm;
12919{
12920 long n1, n2;
12921 int error = FALSE;
12922
12923 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12924 || arg->vval.v_list->lv_len != 2)
12925 return FAIL;
12926 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12927 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12928# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012929 tm->HighPart = n1;
12930 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012931# else
12932 tm->tv_sec = n1;
12933 tm->tv_usec = n2;
12934# endif
12935 return error ? FAIL : OK;
12936}
12937#endif /* FEAT_RELTIME */
12938
12939/*
12940 * "reltime()" function
12941 */
12942 static void
12943f_reltime(argvars, rettv)
12944 typval_T *argvars;
12945 typval_T *rettv;
12946{
12947#ifdef FEAT_RELTIME
12948 proftime_T res;
12949 proftime_T start;
12950
12951 if (argvars[0].v_type == VAR_UNKNOWN)
12952 {
12953 /* No arguments: get current time. */
12954 profile_start(&res);
12955 }
12956 else if (argvars[1].v_type == VAR_UNKNOWN)
12957 {
12958 if (list2proftime(&argvars[0], &res) == FAIL)
12959 return;
12960 profile_end(&res);
12961 }
12962 else
12963 {
12964 /* Two arguments: compute the difference. */
12965 if (list2proftime(&argvars[0], &start) == FAIL
12966 || list2proftime(&argvars[1], &res) == FAIL)
12967 return;
12968 profile_sub(&res, &start);
12969 }
12970
12971 if (rettv_list_alloc(rettv) == OK)
12972 {
12973 long n1, n2;
12974
12975# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012976 n1 = res.HighPart;
12977 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012978# else
12979 n1 = res.tv_sec;
12980 n2 = res.tv_usec;
12981# endif
12982 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12983 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12984 }
12985#endif
12986}
12987
12988/*
12989 * "reltimestr()" function
12990 */
12991 static void
12992f_reltimestr(argvars, rettv)
12993 typval_T *argvars;
12994 typval_T *rettv;
12995{
12996#ifdef FEAT_RELTIME
12997 proftime_T tm;
12998#endif
12999
13000 rettv->v_type = VAR_STRING;
13001 rettv->vval.v_string = NULL;
13002#ifdef FEAT_RELTIME
13003 if (list2proftime(&argvars[0], &tm) == OK)
13004 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13005#endif
13006}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013007
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13009static void make_connection __ARGS((void));
13010static int check_connection __ARGS((void));
13011
13012 static void
13013make_connection()
13014{
13015 if (X_DISPLAY == NULL
13016# ifdef FEAT_GUI
13017 && !gui.in_use
13018# endif
13019 )
13020 {
13021 x_force_connect = TRUE;
13022 setup_term_clip();
13023 x_force_connect = FALSE;
13024 }
13025}
13026
13027 static int
13028check_connection()
13029{
13030 make_connection();
13031 if (X_DISPLAY == NULL)
13032 {
13033 EMSG(_("E240: No connection to Vim server"));
13034 return FAIL;
13035 }
13036 return OK;
13037}
13038#endif
13039
13040#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013041static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042
13043 static void
13044remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 int expr;
13048{
13049 char_u *server_name;
13050 char_u *keys;
13051 char_u *r = NULL;
13052 char_u buf[NUMBUFLEN];
13053# ifdef WIN32
13054 HWND w;
13055# else
13056 Window w;
13057# endif
13058
13059 if (check_restricted() || check_secure())
13060 return;
13061
13062# ifdef FEAT_X11
13063 if (check_connection() == FAIL)
13064 return;
13065# endif
13066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 server_name = get_tv_string_chk(&argvars[0]);
13068 if (server_name == NULL)
13069 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013070 keys = get_tv_string_buf(&argvars[1], buf);
13071# ifdef WIN32
13072 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13073# else
13074 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13075 < 0)
13076# endif
13077 {
13078 if (r != NULL)
13079 EMSG(r); /* sending worked but evaluation failed */
13080 else
13081 EMSG2(_("E241: Unable to send to %s"), server_name);
13082 return;
13083 }
13084
13085 rettv->vval.v_string = r;
13086
13087 if (argvars[2].v_type != VAR_UNKNOWN)
13088 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013089 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013090 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013092
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013093 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013094 v.di_tv.v_type = VAR_STRING;
13095 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 idvar = get_tv_string_chk(&argvars[2]);
13097 if (idvar != NULL)
13098 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013099 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100 }
13101}
13102#endif
13103
13104/*
13105 * "remote_expr()" function
13106 */
13107/*ARGSUSED*/
13108 static void
13109f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *argvars;
13111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112{
13113 rettv->v_type = VAR_STRING;
13114 rettv->vval.v_string = NULL;
13115#ifdef FEAT_CLIENTSERVER
13116 remote_common(argvars, rettv, TRUE);
13117#endif
13118}
13119
13120/*
13121 * "remote_foreground()" function
13122 */
13123/*ARGSUSED*/
13124 static void
13125f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013126 typval_T *argvars;
13127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128{
13129 rettv->vval.v_number = 0;
13130#ifdef FEAT_CLIENTSERVER
13131# ifdef WIN32
13132 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013133 {
13134 char_u *server_name = get_tv_string_chk(&argvars[0]);
13135
13136 if (server_name != NULL)
13137 serverForeground(server_name);
13138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139# else
13140 /* Send a foreground() expression to the server. */
13141 argvars[1].v_type = VAR_STRING;
13142 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13143 argvars[2].v_type = VAR_UNKNOWN;
13144 remote_common(argvars, rettv, TRUE);
13145 vim_free(argvars[1].vval.v_string);
13146# endif
13147#endif
13148}
13149
13150/*ARGSUSED*/
13151 static void
13152f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 typval_T *argvars;
13154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013155{
13156#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158 char_u *s = NULL;
13159# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013160 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013161# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013162 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013163
13164 if (check_restricted() || check_secure())
13165 {
13166 rettv->vval.v_number = -1;
13167 return;
13168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013169 serverid = get_tv_string_chk(&argvars[0]);
13170 if (serverid == NULL)
13171 {
13172 rettv->vval.v_number = -1;
13173 return; /* type error; errmsg already given */
13174 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013175# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013176 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013177 if (n == 0)
13178 rettv->vval.v_number = -1;
13179 else
13180 {
13181 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13182 rettv->vval.v_number = (s != NULL);
13183 }
13184# else
13185 rettv->vval.v_number = 0;
13186 if (check_connection() == FAIL)
13187 return;
13188
13189 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013190 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191# endif
13192
13193 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013195 char_u *retvar;
13196
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 v.di_tv.v_type = VAR_STRING;
13198 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 retvar = get_tv_string_chk(&argvars[1]);
13200 if (retvar != NULL)
13201 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013202 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013203 }
13204#else
13205 rettv->vval.v_number = -1;
13206#endif
13207}
13208
13209/*ARGSUSED*/
13210 static void
13211f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013212 typval_T *argvars;
13213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013214{
13215 char_u *r = NULL;
13216
13217#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 char_u *serverid = get_tv_string_chk(&argvars[0]);
13219
13220 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013221 {
13222# ifdef WIN32
13223 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013224 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013226 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013227 if (n != 0)
13228 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13229 if (r == NULL)
13230# else
13231 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013233# endif
13234 EMSG(_("E277: Unable to read a server reply"));
13235 }
13236#endif
13237 rettv->v_type = VAR_STRING;
13238 rettv->vval.v_string = r;
13239}
13240
13241/*
13242 * "remote_send()" function
13243 */
13244/*ARGSUSED*/
13245 static void
13246f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 typval_T *argvars;
13248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249{
13250 rettv->v_type = VAR_STRING;
13251 rettv->vval.v_string = NULL;
13252#ifdef FEAT_CLIENTSERVER
13253 remote_common(argvars, rettv, FALSE);
13254#endif
13255}
13256
13257/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013258 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 */
13260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013262 typval_T *argvars;
13263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013264{
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 list_T *l;
13266 listitem_T *item, *item2;
13267 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013268 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013269 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013270 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 dict_T *d;
13272 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013273
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013274 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013275 if (argvars[0].v_type == VAR_DICT)
13276 {
13277 if (argvars[2].v_type != VAR_UNKNOWN)
13278 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013279 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013280 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 key = get_tv_string_chk(&argvars[1]);
13283 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 di = dict_find(d, key, -1);
13286 if (di == NULL)
13287 EMSG2(_(e_dictkey), key);
13288 else
13289 {
13290 *rettv = di->di_tv;
13291 init_tv(&di->di_tv);
13292 dictitem_remove(d, di);
13293 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013295 }
13296 }
13297 else if (argvars[0].v_type != VAR_LIST)
13298 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013299 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013300 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 int error = FALSE;
13303
13304 idx = get_tv_number_chk(&argvars[1], &error);
13305 if (error)
13306 ; /* type error: do nothing, errmsg already given */
13307 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 EMSGN(_(e_listidx), idx);
13309 else
13310 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013311 if (argvars[2].v_type == VAR_UNKNOWN)
13312 {
13313 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013314 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013315 *rettv = item->li_tv;
13316 vim_free(item);
13317 }
13318 else
13319 {
13320 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 end = get_tv_number_chk(&argvars[2], &error);
13322 if (error)
13323 ; /* type error: do nothing */
13324 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013325 EMSGN(_(e_listidx), end);
13326 else
13327 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013328 int cnt = 0;
13329
13330 for (li = item; li != NULL; li = li->li_next)
13331 {
13332 ++cnt;
13333 if (li == item2)
13334 break;
13335 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013336 if (li == NULL) /* didn't find "item2" after "item" */
13337 EMSG(_(e_invrange));
13338 else
13339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013340 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013341 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013342 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013343 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013344 l->lv_first = item;
13345 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013346 item->li_prev = NULL;
13347 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013348 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013349 }
13350 }
13351 }
13352 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013353 }
13354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
13357/*
13358 * "rename({from}, {to})" function
13359 */
13360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
13365 char_u buf[NUMBUFLEN];
13366
13367 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13371 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013375 * "repeat()" function
13376 */
13377/*ARGSUSED*/
13378 static void
13379f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013380 typval_T *argvars;
13381 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013382{
13383 char_u *p;
13384 int n;
13385 int slen;
13386 int len;
13387 char_u *r;
13388 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013389
13390 n = get_tv_number(&argvars[1]);
13391 if (argvars[0].v_type == VAR_LIST)
13392 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013393 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013394 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013395 if (list_extend(rettv->vval.v_list,
13396 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013397 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013398 }
13399 else
13400 {
13401 p = get_tv_string(&argvars[0]);
13402 rettv->v_type = VAR_STRING;
13403 rettv->vval.v_string = NULL;
13404
13405 slen = (int)STRLEN(p);
13406 len = slen * n;
13407 if (len <= 0)
13408 return;
13409
13410 r = alloc(len + 1);
13411 if (r != NULL)
13412 {
13413 for (i = 0; i < n; i++)
13414 mch_memmove(r + i * slen, p, (size_t)slen);
13415 r[len] = NUL;
13416 }
13417
13418 rettv->vval.v_string = r;
13419 }
13420}
13421
13422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 * "resolve()" function
13424 */
13425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013427 typval_T *argvars;
13428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429{
13430 char_u *p;
13431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433#ifdef FEAT_SHORTCUT
13434 {
13435 char_u *v = NULL;
13436
13437 v = mch_resolve_shortcut(p);
13438 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 }
13443#else
13444# ifdef HAVE_READLINK
13445 {
13446 char_u buf[MAXPATHL + 1];
13447 char_u *cpy;
13448 int len;
13449 char_u *remain = NULL;
13450 char_u *q;
13451 int is_relative_to_current = FALSE;
13452 int has_trailing_pathsep = FALSE;
13453 int limit = 100;
13454
13455 p = vim_strsave(p);
13456
13457 if (p[0] == '.' && (vim_ispathsep(p[1])
13458 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13459 is_relative_to_current = TRUE;
13460
13461 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013462 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 has_trailing_pathsep = TRUE;
13464
13465 q = getnextcomp(p);
13466 if (*q != NUL)
13467 {
13468 /* Separate the first path component in "p", and keep the
13469 * remainder (beginning with the path separator). */
13470 remain = vim_strsave(q - 1);
13471 q[-1] = NUL;
13472 }
13473
13474 for (;;)
13475 {
13476 for (;;)
13477 {
13478 len = readlink((char *)p, (char *)buf, MAXPATHL);
13479 if (len <= 0)
13480 break;
13481 buf[len] = NUL;
13482
13483 if (limit-- == 0)
13484 {
13485 vim_free(p);
13486 vim_free(remain);
13487 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 goto fail;
13490 }
13491
13492 /* Ensure that the result will have a trailing path separator
13493 * if the argument has one. */
13494 if (remain == NULL && has_trailing_pathsep)
13495 add_pathsep(buf);
13496
13497 /* Separate the first path component in the link value and
13498 * concatenate the remainders. */
13499 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13500 if (*q != NUL)
13501 {
13502 if (remain == NULL)
13503 remain = vim_strsave(q - 1);
13504 else
13505 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013506 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 if (cpy != NULL)
13508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 vim_free(remain);
13510 remain = cpy;
13511 }
13512 }
13513 q[-1] = NUL;
13514 }
13515
13516 q = gettail(p);
13517 if (q > p && *q == NUL)
13518 {
13519 /* Ignore trailing path separator. */
13520 q[-1] = NUL;
13521 q = gettail(p);
13522 }
13523 if (q > p && !mch_isFullName(buf))
13524 {
13525 /* symlink is relative to directory of argument */
13526 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13527 if (cpy != NULL)
13528 {
13529 STRCPY(cpy, p);
13530 STRCPY(gettail(cpy), buf);
13531 vim_free(p);
13532 p = cpy;
13533 }
13534 }
13535 else
13536 {
13537 vim_free(p);
13538 p = vim_strsave(buf);
13539 }
13540 }
13541
13542 if (remain == NULL)
13543 break;
13544
13545 /* Append the first path component of "remain" to "p". */
13546 q = getnextcomp(remain + 1);
13547 len = q - remain - (*q != NUL);
13548 cpy = vim_strnsave(p, STRLEN(p) + len);
13549 if (cpy != NULL)
13550 {
13551 STRNCAT(cpy, remain, len);
13552 vim_free(p);
13553 p = cpy;
13554 }
13555 /* Shorten "remain". */
13556 if (*q != NUL)
13557 STRCPY(remain, q - 1);
13558 else
13559 {
13560 vim_free(remain);
13561 remain = NULL;
13562 }
13563 }
13564
13565 /* If the result is a relative path name, make it explicitly relative to
13566 * the current directory if and only if the argument had this form. */
13567 if (!vim_ispathsep(*p))
13568 {
13569 if (is_relative_to_current
13570 && *p != NUL
13571 && !(p[0] == '.'
13572 && (p[1] == NUL
13573 || vim_ispathsep(p[1])
13574 || (p[1] == '.'
13575 && (p[2] == NUL
13576 || vim_ispathsep(p[2]))))))
13577 {
13578 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013579 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 if (cpy != NULL)
13581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 vim_free(p);
13583 p = cpy;
13584 }
13585 }
13586 else if (!is_relative_to_current)
13587 {
13588 /* Strip leading "./". */
13589 q = p;
13590 while (q[0] == '.' && vim_ispathsep(q[1]))
13591 q += 2;
13592 if (q > p)
13593 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13594 }
13595 }
13596
13597 /* Ensure that the result will have no trailing path separator
13598 * if the argument had none. But keep "/" or "//". */
13599 if (!has_trailing_pathsep)
13600 {
13601 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013602 if (after_pathsep(p, q))
13603 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 }
13605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 }
13608# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610# endif
13611#endif
13612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
13615#ifdef HAVE_READLINK
13616fail:
13617#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619}
13620
13621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013622 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 */
13624 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *argvars;
13627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 list_T *l;
13630 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631
Bram Moolenaar0d660222005-01-07 21:51:51 +000013632 rettv->vval.v_number = 0;
13633 if (argvars[0].v_type != VAR_LIST)
13634 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013635 else if ((l = argvars[0].vval.v_list) != NULL
13636 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013637 {
13638 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013639 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013640 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 while (li != NULL)
13642 {
13643 ni = li->li_prev;
13644 list_append(l, li);
13645 li = ni;
13646 }
13647 rettv->vval.v_list = l;
13648 rettv->v_type = VAR_LIST;
13649 ++l->lv_refcount;
13650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013653#define SP_NOMOVE 0x01 /* don't move cursor */
13654#define SP_REPEAT 0x02 /* repeat to find outer pair */
13655#define SP_RETCOUNT 0x04 /* return matchcount */
13656#define SP_SETPCMARK 0x08 /* set previous context mark */
13657#define SP_START 0x10 /* accept match at start position */
13658#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13659#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013660
Bram Moolenaar33570922005-01-25 22:26:29 +000013661static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013662
13663/*
13664 * Get flags for a search function.
13665 * Possibly sets "p_ws".
13666 * Returns BACKWARD, FORWARD or zero (for an error).
13667 */
13668 static int
13669get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013670 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671 int *flagsp;
13672{
13673 int dir = FORWARD;
13674 char_u *flags;
13675 char_u nbuf[NUMBUFLEN];
13676 int mask;
13677
13678 if (varp->v_type != VAR_UNKNOWN)
13679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 flags = get_tv_string_buf_chk(varp, nbuf);
13681 if (flags == NULL)
13682 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013683 while (*flags != NUL)
13684 {
13685 switch (*flags)
13686 {
13687 case 'b': dir = BACKWARD; break;
13688 case 'w': p_ws = TRUE; break;
13689 case 'W': p_ws = FALSE; break;
13690 default: mask = 0;
13691 if (flagsp != NULL)
13692 switch (*flags)
13693 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013694 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013695 case 'e': mask = SP_END; break;
13696 case 'm': mask = SP_RETCOUNT; break;
13697 case 'n': mask = SP_NOMOVE; break;
13698 case 'p': mask = SP_SUBPAT; break;
13699 case 'r': mask = SP_REPEAT; break;
13700 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 }
13702 if (mask == 0)
13703 {
13704 EMSG2(_(e_invarg2), flags);
13705 dir = 0;
13706 }
13707 else
13708 *flagsp |= mask;
13709 }
13710 if (dir == 0)
13711 break;
13712 ++flags;
13713 }
13714 }
13715 return dir;
13716}
13717
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013719 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013721 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013722search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013724 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013725 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013727 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 char_u *pat;
13729 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013730 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 int save_p_ws = p_ws;
13732 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013733 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013734 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013735 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013736 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013739 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013740 if (dir == 0)
13741 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013742 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013743 if (flags & SP_START)
13744 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013745 if (flags & SP_END)
13746 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013747
13748 /* Optional extra argument: line number to stop searching. */
13749 if (argvars[1].v_type != VAR_UNKNOWN
13750 && argvars[2].v_type != VAR_UNKNOWN)
13751 {
13752 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13753 if (lnum_stop < 0)
13754 goto theend;
13755 }
13756
Bram Moolenaar231334e2005-07-25 20:46:57 +000013757 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013758 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013759 * Check to make sure only those flags are set.
13760 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13761 * flags cannot be set. Check for that condition also.
13762 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013763 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013764 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013767 goto theend;
13768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013770 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013771 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13772 options, RE_SEARCH, (linenr_T)lnum_stop);
13773 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013775 if (flags & SP_SUBPAT)
13776 retval = subpatnum;
13777 else
13778 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013779 if (flags & SP_SETPCMARK)
13780 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013782 if (match_pos != NULL)
13783 {
13784 /* Store the match cursor position */
13785 match_pos->lnum = pos.lnum;
13786 match_pos->col = pos.col + 1;
13787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 /* "/$" will put the cursor after the end of the line, may need to
13789 * correct that here */
13790 check_cursor();
13791 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013792
13793 /* If 'n' flag is used: restore cursor position. */
13794 if (flags & SP_NOMOVE)
13795 curwin->w_cursor = save_cursor;
13796theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013798
13799 return retval;
13800}
13801
13802/*
13803 * "search()" function
13804 */
13805 static void
13806f_search(argvars, rettv)
13807 typval_T *argvars;
13808 typval_T *rettv;
13809{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013810 int flags = 0;
13811
13812 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813}
13814
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013816 * "searchdecl()" function
13817 */
13818 static void
13819f_searchdecl(argvars, rettv)
13820 typval_T *argvars;
13821 typval_T *rettv;
13822{
13823 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013824 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013825 int error = FALSE;
13826 char_u *name;
13827
13828 rettv->vval.v_number = 1; /* default: FAIL */
13829
13830 name = get_tv_string_chk(&argvars[0]);
13831 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013832 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013833 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013834 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13835 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13836 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013837 if (!error && name != NULL)
13838 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013839 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013840}
13841
13842/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013843 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013845 static int
13846searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013847 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013848 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849{
13850 char_u *spat, *mpat, *epat;
13851 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 int dir;
13854 int flags = 0;
13855 char_u nbuf1[NUMBUFLEN];
13856 char_u nbuf2[NUMBUFLEN];
13857 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013858 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013859 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 spat = get_tv_string_chk(&argvars[0]);
13863 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13864 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13865 if (spat == NULL || mpat == NULL || epat == NULL)
13866 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 /* Handle the optional fourth argument: flags */
13869 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013870 if (dir == 0)
13871 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013872
13873 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013874 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13875 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013876 if ((flags & (SP_END | SP_SUBPAT)) != 0
13877 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013878 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013879 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013880 goto theend;
13881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013883 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013884 if (argvars[3].v_type == VAR_UNKNOWN
13885 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 skip = (char_u *)"";
13887 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013890 if (argvars[5].v_type != VAR_UNKNOWN)
13891 {
13892 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13893 if (lnum_stop < 0)
13894 goto theend;
13895 }
13896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 if (skip == NULL)
13898 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013900 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13901 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013902
13903theend:
13904 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013905
13906 return retval;
13907}
13908
13909/*
13910 * "searchpair()" function
13911 */
13912 static void
13913f_searchpair(argvars, rettv)
13914 typval_T *argvars;
13915 typval_T *rettv;
13916{
13917 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13918}
13919
13920/*
13921 * "searchpairpos()" function
13922 */
13923 static void
13924f_searchpairpos(argvars, rettv)
13925 typval_T *argvars;
13926 typval_T *rettv;
13927{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013928 pos_T match_pos;
13929 int lnum = 0;
13930 int col = 0;
13931
13932 rettv->vval.v_number = 0;
13933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013935 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013936
13937 if (searchpair_cmn(argvars, &match_pos) > 0)
13938 {
13939 lnum = match_pos.lnum;
13940 col = match_pos.col;
13941 }
13942
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013943 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13944 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013945}
13946
13947/*
13948 * Search for a start/middle/end thing.
13949 * Used by searchpair(), see its documentation for the details.
13950 * Returns 0 or -1 for no match,
13951 */
13952 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013953do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013954 char_u *spat; /* start pattern */
13955 char_u *mpat; /* middle pattern */
13956 char_u *epat; /* end pattern */
13957 int dir; /* BACKWARD or FORWARD */
13958 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013959 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013960 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013961 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013962{
13963 char_u *save_cpo;
13964 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13965 long retval = 0;
13966 pos_T pos;
13967 pos_T firstpos;
13968 pos_T foundpos;
13969 pos_T save_cursor;
13970 pos_T save_pos;
13971 int n;
13972 int r;
13973 int nest = 1;
13974 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013975 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013976
13977 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13978 save_cpo = p_cpo;
13979 p_cpo = (char_u *)"";
13980
13981 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13982 * start/middle/end (pat3, for the top pair). */
13983 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13984 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13985 if (pat2 == NULL || pat3 == NULL)
13986 goto theend;
13987 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13988 if (*mpat == NUL)
13989 STRCPY(pat3, pat2);
13990 else
13991 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13992 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013993 if (flags & SP_START)
13994 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013995
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 save_cursor = curwin->w_cursor;
13997 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013998 clearpos(&firstpos);
13999 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 pat = pat3;
14001 for (;;)
14002 {
14003 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014004 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14006 /* didn't find it or found the first match again: FAIL */
14007 break;
14008
14009 if (firstpos.lnum == 0)
14010 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014011 if (equalpos(pos, foundpos))
14012 {
14013 /* Found the same position again. Can happen with a pattern that
14014 * has "\zs" at the end and searching backwards. Advance one
14015 * character and try again. */
14016 if (dir == BACKWARD)
14017 decl(&pos);
14018 else
14019 incl(&pos);
14020 }
14021 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022
14023 /* If the skip pattern matches, ignore this match. */
14024 if (*skip != NUL)
14025 {
14026 save_pos = curwin->w_cursor;
14027 curwin->w_cursor = pos;
14028 r = eval_to_bool(skip, &err, NULL, FALSE);
14029 curwin->w_cursor = save_pos;
14030 if (err)
14031 {
14032 /* Evaluating {skip} caused an error, break here. */
14033 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014034 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 break;
14036 }
14037 if (r)
14038 continue;
14039 }
14040
14041 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14042 {
14043 /* Found end when searching backwards or start when searching
14044 * forward: nested pair. */
14045 ++nest;
14046 pat = pat2; /* nested, don't search for middle */
14047 }
14048 else
14049 {
14050 /* Found end when searching forward or start when searching
14051 * backward: end of (nested) pair; or found middle in outer pair. */
14052 if (--nest == 1)
14053 pat = pat3; /* outer level, search for middle */
14054 }
14055
14056 if (nest == 0)
14057 {
14058 /* Found the match: return matchcount or line number. */
14059 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014060 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014062 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014063 if (flags & SP_SETPCMARK)
14064 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 curwin->w_cursor = pos;
14066 if (!(flags & SP_REPEAT))
14067 break;
14068 nest = 1; /* search for next unmatched */
14069 }
14070 }
14071
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014072 if (match_pos != NULL)
14073 {
14074 /* Store the match cursor position */
14075 match_pos->lnum = curwin->w_cursor.lnum;
14076 match_pos->col = curwin->w_cursor.col + 1;
14077 }
14078
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014080 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 curwin->w_cursor = save_cursor;
14082
14083theend:
14084 vim_free(pat2);
14085 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014087
14088 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089}
14090
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014091/*
14092 * "searchpos()" function
14093 */
14094 static void
14095f_searchpos(argvars, rettv)
14096 typval_T *argvars;
14097 typval_T *rettv;
14098{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014099 pos_T match_pos;
14100 int lnum = 0;
14101 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014102 int n;
14103 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014104
14105 rettv->vval.v_number = 0;
14106
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014107 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014108 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014109
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014110 n = search_cmn(argvars, &match_pos, &flags);
14111 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014112 {
14113 lnum = match_pos.lnum;
14114 col = match_pos.col;
14115 }
14116
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014117 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14118 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014119 if (flags & SP_SUBPAT)
14120 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121}
14122
14123
Bram Moolenaar0d660222005-01-07 21:51:51 +000014124/*ARGSUSED*/
14125 static void
14126f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 typval_T *argvars;
14128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014130#ifdef FEAT_CLIENTSERVER
14131 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 char_u *server = get_tv_string_chk(&argvars[0]);
14133 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 if (server == NULL || reply == NULL)
14137 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138 if (check_restricted() || check_secure())
14139 return;
14140# ifdef FEAT_X11
14141 if (check_connection() == FAIL)
14142 return;
14143# endif
14144
14145 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 EMSG(_("E258: Unable to send to client"));
14148 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014150 rettv->vval.v_number = 0;
14151#else
14152 rettv->vval.v_number = -1;
14153#endif
14154}
14155
14156/*ARGSUSED*/
14157 static void
14158f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161{
14162 char_u *r = NULL;
14163
14164#ifdef FEAT_CLIENTSERVER
14165# ifdef WIN32
14166 r = serverGetVimNames();
14167# else
14168 make_connection();
14169 if (X_DISPLAY != NULL)
14170 r = serverGetVimNames(X_DISPLAY);
14171# endif
14172#endif
14173 rettv->v_type = VAR_STRING;
14174 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175}
14176
14177/*
14178 * "setbufvar()" function
14179 */
14180/*ARGSUSED*/
14181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 typval_T *argvars;
14184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185{
14186 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 char_u nbuf[NUMBUFLEN];
14191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 rettv->vval.v_number = 0;
14193
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 if (check_restricted() || check_secure())
14195 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14197 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 varp = &argvars[2];
14200
14201 if (buf != NULL && varname != NULL && varp != NULL)
14202 {
14203 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205
14206 if (*varname == '&')
14207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 long numval;
14209 char_u *strval;
14210 int error = FALSE;
14211
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 numval = get_tv_number_chk(varp, &error);
14214 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 if (!error && strval != NULL)
14216 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 }
14218 else
14219 {
14220 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14221 if (bufvarname != NULL)
14222 {
14223 STRCPY(bufvarname, "b:");
14224 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014225 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 vim_free(bufvarname);
14227 }
14228 }
14229
14230 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233}
14234
14235/*
14236 * "setcmdpos()" function
14237 */
14238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *argvars;
14241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 int pos = (int)get_tv_number(&argvars[0]) - 1;
14244
14245 if (pos >= 0)
14246 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247}
14248
14249/*
14250 * "setline()" function
14251 */
14252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014254 typval_T *argvars;
14255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256{
14257 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014258 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014259 list_T *l = NULL;
14260 listitem_T *li = NULL;
14261 long added = 0;
14262 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014264 lnum = get_tv_lnum(&argvars[0]);
14265 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014267 l = argvars[1].vval.v_list;
14268 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014270 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014272
14273 rettv->vval.v_number = 0; /* OK */
14274 for (;;)
14275 {
14276 if (l != NULL)
14277 {
14278 /* list argument, get next string */
14279 if (li == NULL)
14280 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014281 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014282 li = li->li_next;
14283 }
14284
14285 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014287 break;
14288 if (lnum <= curbuf->b_ml.ml_line_count)
14289 {
14290 /* existing line, replace it */
14291 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14292 {
14293 changed_bytes(lnum, 0);
14294 check_cursor_col();
14295 rettv->vval.v_number = 0; /* OK */
14296 }
14297 }
14298 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14299 {
14300 /* lnum is one past the last line, append the line */
14301 ++added;
14302 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14303 rettv->vval.v_number = 0; /* OK */
14304 }
14305
14306 if (l == NULL) /* only one string argument */
14307 break;
14308 ++lnum;
14309 }
14310
14311 if (added > 0)
14312 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313}
14314
14315/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014316 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014317 */
14318/*ARGSUSED*/
14319 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014320set_qf_ll_list(wp, list_arg, action_arg, rettv)
14321 win_T *wp;
14322 typval_T *list_arg;
14323 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014324 typval_T *rettv;
14325{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014326#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014327 char_u *act;
14328 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014329#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014330
Bram Moolenaar2641f772005-03-25 21:58:17 +000014331 rettv->vval.v_number = -1;
14332
14333#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014334 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014335 EMSG(_(e_listreq));
14336 else
14337 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014338 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014339
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014340 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014341 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014342 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 if (act == NULL)
14344 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014345 if (*act == 'a' || *act == 'r')
14346 action = *act;
14347 }
14348
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014349 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014350 rettv->vval.v_number = 0;
14351 }
14352#endif
14353}
14354
14355/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014356 * "setloclist()" function
14357 */
14358/*ARGSUSED*/
14359 static void
14360f_setloclist(argvars, rettv)
14361 typval_T *argvars;
14362 typval_T *rettv;
14363{
14364 win_T *win;
14365
14366 rettv->vval.v_number = -1;
14367
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014368 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014369 if (win != NULL)
14370 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14371}
14372
14373/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014374 * "setpos()" function
14375 */
14376/*ARGSUSED*/
14377 static void
14378f_setpos(argvars, rettv)
14379 typval_T *argvars;
14380 typval_T *rettv;
14381{
14382 pos_T pos;
14383 int fnum;
14384 char_u *name;
14385
14386 name = get_tv_string_chk(argvars);
14387 if (name != NULL)
14388 {
14389 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14390 {
14391 --pos.col;
14392 if (name[0] == '.') /* cursor */
14393 {
14394 if (fnum == curbuf->b_fnum)
14395 {
14396 curwin->w_cursor = pos;
14397 check_cursor();
14398 }
14399 else
14400 EMSG(_(e_invarg));
14401 }
14402 else if (name[0] == '\'') /* mark */
14403 (void)setmark_pos(name[1], &pos, fnum);
14404 else
14405 EMSG(_(e_invarg));
14406 }
14407 }
14408}
14409
14410/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014411 * "setqflist()" function
14412 */
14413/*ARGSUSED*/
14414 static void
14415f_setqflist(argvars, rettv)
14416 typval_T *argvars;
14417 typval_T *rettv;
14418{
14419 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14420}
14421
14422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 * "setreg()" function
14424 */
14425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014427 typval_T *argvars;
14428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429{
14430 int regname;
14431 char_u *strregname;
14432 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 int append;
14435 char_u yank_type;
14436 long block_len;
14437
14438 block_len = -1;
14439 yank_type = MAUTO;
14440 append = FALSE;
14441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014442 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014443 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 if (strregname == NULL)
14446 return; /* type error; errmsg already given */
14447 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 if (regname == 0 || regname == '@')
14449 regname = '"';
14450 else if (regname == '=')
14451 return;
14452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014455 stropt = get_tv_string_chk(&argvars[2]);
14456 if (stropt == NULL)
14457 return; /* type error */
14458 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 switch (*stropt)
14460 {
14461 case 'a': case 'A': /* append */
14462 append = TRUE;
14463 break;
14464 case 'v': case 'c': /* character-wise selection */
14465 yank_type = MCHAR;
14466 break;
14467 case 'V': case 'l': /* line-wise selection */
14468 yank_type = MLINE;
14469 break;
14470#ifdef FEAT_VISUAL
14471 case 'b': case Ctrl_V: /* block-wise selection */
14472 yank_type = MBLOCK;
14473 if (VIM_ISDIGIT(stropt[1]))
14474 {
14475 ++stropt;
14476 block_len = getdigits(&stropt) - 1;
14477 --stropt;
14478 }
14479 break;
14480#endif
14481 }
14482 }
14483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 strval = get_tv_string_chk(&argvars[1]);
14485 if (strval != NULL)
14486 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014488 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489}
14490
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014491/*
14492 * "settabwinvar()" function
14493 */
14494 static void
14495f_settabwinvar(argvars, rettv)
14496 typval_T *argvars;
14497 typval_T *rettv;
14498{
14499 setwinvar(argvars, rettv, 1);
14500}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501
14502/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014503 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014507 typval_T *argvars;
14508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014510 setwinvar(argvars, rettv, 0);
14511}
14512
14513/*
14514 * "setwinvar()" and "settabwinvar()" functions
14515 */
14516 static void
14517setwinvar(argvars, rettv, off)
14518 typval_T *argvars;
14519 typval_T *rettv;
14520 int off;
14521{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 win_T *win;
14523#ifdef FEAT_WINDOWS
14524 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014525 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526#endif
14527 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014528 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014530 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014532 rettv->vval.v_number = 0;
14533
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 if (check_restricted() || check_secure())
14535 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014536
14537#ifdef FEAT_WINDOWS
14538 if (off == 1)
14539 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14540 else
14541 tp = curtab;
14542#endif
14543 win = find_win_by_nr(&argvars[off], tp);
14544 varname = get_tv_string_chk(&argvars[off + 1]);
14545 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546
14547 if (win != NULL && varname != NULL && varp != NULL)
14548 {
14549#ifdef FEAT_WINDOWS
14550 /* set curwin to be our win, temporarily */
14551 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014552 save_curtab = curtab;
14553 goto_tabpage_tp(tp);
14554 if (!win_valid(win))
14555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 curwin = win;
14557 curbuf = curwin->w_buffer;
14558#endif
14559
14560 if (*varname == '&')
14561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 long numval;
14563 char_u *strval;
14564 int error = FALSE;
14565
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 numval = get_tv_number_chk(varp, &error);
14568 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014569 if (!error && strval != NULL)
14570 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571 }
14572 else
14573 {
14574 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14575 if (winvarname != NULL)
14576 {
14577 STRCPY(winvarname, "w:");
14578 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014579 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 vim_free(winvarname);
14581 }
14582 }
14583
14584#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014585 /* Restore current tabpage and window, if still valid (autocomands can
14586 * make them invalid). */
14587 if (valid_tabpage(save_curtab))
14588 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 if (win_valid(save_curwin))
14590 {
14591 curwin = save_curwin;
14592 curbuf = curwin->w_buffer;
14593 }
14594#endif
14595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596}
14597
14598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 */
14601 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014603 typval_T *argvars;
14604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608 p = get_tv_string(&argvars[0]);
14609 rettv->vval.v_string = vim_strsave(p);
14610 simplify_filename(rettv->vval.v_string); /* simplify in place */
14611 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612}
14613
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614static int
14615#ifdef __BORLANDC__
14616 _RTLENTRYF
14617#endif
14618 item_compare __ARGS((const void *s1, const void *s2));
14619static int
14620#ifdef __BORLANDC__
14621 _RTLENTRYF
14622#endif
14623 item_compare2 __ARGS((const void *s1, const void *s2));
14624
14625static int item_compare_ic;
14626static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628#define ITEM_COMPARE_FAIL 999
14629
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 static int
14634#ifdef __BORLANDC__
14635_RTLENTRYF
14636#endif
14637item_compare(s1, s2)
14638 const void *s1;
14639 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641 char_u *p1, *p2;
14642 char_u *tofree1, *tofree2;
14643 int res;
14644 char_u numbuf1[NUMBUFLEN];
14645 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014647 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14648 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 if (item_compare_ic)
14650 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652 res = STRCMP(p1, p2);
14653 vim_free(tofree1);
14654 vim_free(tofree2);
14655 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656}
14657
14658 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659#ifdef __BORLANDC__
14660_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662item_compare2(s1, s2)
14663 const void *s1;
14664 const void *s2;
14665{
14666 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014667 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014668 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 /* shortcut after failure in previous call; compare all items equal */
14672 if (item_compare_func_err)
14673 return 0;
14674
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014675 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14676 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014677 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14678 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679
14680 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014681 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014682 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683 clear_tv(&argv[0]);
14684 clear_tv(&argv[1]);
14685
14686 if (res == FAIL)
14687 res = ITEM_COMPARE_FAIL;
14688 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014689 /* return value has wrong type */
14690 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14691 if (item_compare_func_err)
14692 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693 clear_tv(&rettv);
14694 return res;
14695}
14696
14697/*
14698 * "sort({list})" function
14699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014702 typval_T *argvars;
14703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704{
Bram Moolenaar33570922005-01-25 22:26:29 +000014705 list_T *l;
14706 listitem_T *li;
14707 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708 long len;
14709 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 rettv->vval.v_number = 0;
14712 if (argvars[0].v_type != VAR_LIST)
14713 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714 else
14715 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014717 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014718 return;
14719 rettv->vval.v_list = l;
14720 rettv->v_type = VAR_LIST;
14721 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722
Bram Moolenaar0d660222005-01-07 21:51:51 +000014723 len = list_len(l);
14724 if (len <= 1)
14725 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726
Bram Moolenaar0d660222005-01-07 21:51:51 +000014727 item_compare_ic = FALSE;
14728 item_compare_func = NULL;
14729 if (argvars[1].v_type != VAR_UNKNOWN)
14730 {
14731 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 else
14734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 int error = FALSE;
14736
14737 i = get_tv_number_chk(&argvars[1], &error);
14738 if (error)
14739 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 if (i == 1)
14741 item_compare_ic = TRUE;
14742 else
14743 item_compare_func = get_tv_string(&argvars[1]);
14744 }
14745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749 if (ptrs == NULL)
14750 return;
14751 i = 0;
14752 for (li = l->lv_first; li != NULL; li = li->li_next)
14753 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756 /* test the compare function */
14757 if (item_compare_func != NULL
14758 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14759 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014760 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 {
14763 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765 item_compare_func == NULL ? item_compare : item_compare2);
14766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014767 if (!item_compare_func_err)
14768 {
14769 /* Clear the List and append the items in the sorted order. */
14770 l->lv_first = l->lv_last = NULL;
14771 l->lv_len = 0;
14772 for (i = 0; i < len; ++i)
14773 list_append(l, ptrs[i]);
14774 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775 }
14776
14777 vim_free(ptrs);
14778 }
14779}
14780
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014782 * "soundfold({word})" function
14783 */
14784 static void
14785f_soundfold(argvars, rettv)
14786 typval_T *argvars;
14787 typval_T *rettv;
14788{
14789 char_u *s;
14790
14791 rettv->v_type = VAR_STRING;
14792 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014793#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014794 rettv->vval.v_string = eval_soundfold(s);
14795#else
14796 rettv->vval.v_string = vim_strsave(s);
14797#endif
14798}
14799
14800/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014801 * "spellbadword()" function
14802 */
14803/* ARGSUSED */
14804 static void
14805f_spellbadword(argvars, rettv)
14806 typval_T *argvars;
14807 typval_T *rettv;
14808{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014809 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014810 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014811 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014812
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014813 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014814 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014815
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014816#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014817 if (argvars[0].v_type == VAR_UNKNOWN)
14818 {
14819 /* Find the start and length of the badly spelled word. */
14820 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14821 if (len != 0)
14822 word = ml_get_cursor();
14823 }
14824 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14825 {
14826 char_u *str = get_tv_string_chk(&argvars[0]);
14827 int capcol = -1;
14828
14829 if (str != NULL)
14830 {
14831 /* Check the argument for spelling. */
14832 while (*str != NUL)
14833 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014834 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014835 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014836 {
14837 word = str;
14838 break;
14839 }
14840 str += len;
14841 }
14842 }
14843 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014844#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014845
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014846 list_append_string(rettv->vval.v_list, word, len);
14847 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014848 attr == HLF_SPB ? "bad" :
14849 attr == HLF_SPR ? "rare" :
14850 attr == HLF_SPL ? "local" :
14851 attr == HLF_SPC ? "caps" :
14852 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014853}
14854
14855/*
14856 * "spellsuggest()" function
14857 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014858/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014859 static void
14860f_spellsuggest(argvars, rettv)
14861 typval_T *argvars;
14862 typval_T *rettv;
14863{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014864#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014865 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014866 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014867 int maxcount;
14868 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014869 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014870 listitem_T *li;
14871 int need_capital = FALSE;
14872#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014873
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014874 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014875 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014876
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014877#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014878 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14879 {
14880 str = get_tv_string(&argvars[0]);
14881 if (argvars[1].v_type != VAR_UNKNOWN)
14882 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014883 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014884 if (maxcount <= 0)
14885 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014886 if (argvars[2].v_type != VAR_UNKNOWN)
14887 {
14888 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14889 if (typeerr)
14890 return;
14891 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014892 }
14893 else
14894 maxcount = 25;
14895
Bram Moolenaar4770d092006-01-12 23:22:24 +000014896 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014897
14898 for (i = 0; i < ga.ga_len; ++i)
14899 {
14900 str = ((char_u **)ga.ga_data)[i];
14901
14902 li = listitem_alloc();
14903 if (li == NULL)
14904 vim_free(str);
14905 else
14906 {
14907 li->li_tv.v_type = VAR_STRING;
14908 li->li_tv.v_lock = 0;
14909 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014910 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014911 }
14912 }
14913 ga_clear(&ga);
14914 }
14915#endif
14916}
14917
Bram Moolenaar0d660222005-01-07 21:51:51 +000014918 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014919f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 typval_T *argvars;
14921 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922{
14923 char_u *str;
14924 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014925 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014926 regmatch_T regmatch;
14927 char_u patbuf[NUMBUFLEN];
14928 char_u *save_cpo;
14929 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014930 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014931 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014932 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933
14934 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14935 save_cpo = p_cpo;
14936 p_cpo = (char_u *)"";
14937
14938 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014939 if (argvars[1].v_type != VAR_UNKNOWN)
14940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014941 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14942 if (pat == NULL)
14943 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014945 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014946 }
14947 if (pat == NULL || *pat == NUL)
14948 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014950 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014952 if (typeerr)
14953 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14956 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014959 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014961 if (*str == NUL)
14962 match = FALSE; /* empty item at the end */
14963 else
14964 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965 if (match)
14966 end = regmatch.startp[0];
14967 else
14968 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014969 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14970 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014972 if (list_append_string(rettv->vval.v_list, str,
14973 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014975 }
14976 if (!match)
14977 break;
14978 /* Advance to just after the match. */
14979 if (regmatch.endp[0] > str)
14980 col = 0;
14981 else
14982 {
14983 /* Don't get stuck at the same match. */
14984#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014985 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014986#else
14987 col = 1;
14988#endif
14989 }
14990 str = regmatch.endp[0];
14991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997}
14998
Bram Moolenaar2c932302006-03-18 21:42:09 +000014999/*
15000 * "str2nr()" function
15001 */
15002 static void
15003f_str2nr(argvars, rettv)
15004 typval_T *argvars;
15005 typval_T *rettv;
15006{
15007 int base = 10;
15008 char_u *p;
15009 long n;
15010
15011 if (argvars[1].v_type != VAR_UNKNOWN)
15012 {
15013 base = get_tv_number(&argvars[1]);
15014 if (base != 8 && base != 10 && base != 16)
15015 {
15016 EMSG(_(e_invarg));
15017 return;
15018 }
15019 }
15020
15021 p = skipwhite(get_tv_string(&argvars[0]));
15022 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15023 rettv->vval.v_number = n;
15024}
15025
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026#ifdef HAVE_STRFTIME
15027/*
15028 * "strftime({format}[, {time}])" function
15029 */
15030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 typval_T *argvars;
15033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034{
15035 char_u result_buf[256];
15036 struct tm *curtime;
15037 time_t seconds;
15038 char_u *p;
15039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015043 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 seconds = time(NULL);
15045 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015046 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 curtime = localtime(&seconds);
15048 /* MSVC returns NULL for an invalid value of seconds. */
15049 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 else
15052 {
15053# ifdef FEAT_MBYTE
15054 vimconv_T conv;
15055 char_u *enc;
15056
15057 conv.vc_type = CONV_NONE;
15058 enc = enc_locale();
15059 convert_setup(&conv, p_enc, enc);
15060 if (conv.vc_type != CONV_NONE)
15061 p = string_convert(&conv, p, NULL);
15062# endif
15063 if (p != NULL)
15064 (void)strftime((char *)result_buf, sizeof(result_buf),
15065 (char *)p, curtime);
15066 else
15067 result_buf[0] = NUL;
15068
15069# ifdef FEAT_MBYTE
15070 if (conv.vc_type != CONV_NONE)
15071 vim_free(p);
15072 convert_setup(&conv, enc, p_enc);
15073 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015074 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 else
15076# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078
15079# ifdef FEAT_MBYTE
15080 /* Release conversion descriptors */
15081 convert_setup(&conv, NULL, NULL);
15082 vim_free(enc);
15083# endif
15084 }
15085}
15086#endif
15087
15088/*
15089 * "stridx()" function
15090 */
15091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015093 typval_T *argvars;
15094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095{
15096 char_u buf[NUMBUFLEN];
15097 char_u *needle;
15098 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015099 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 needle = get_tv_string_chk(&argvars[1]);
15104 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015106 if (needle == NULL || haystack == NULL)
15107 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108
Bram Moolenaar33570922005-01-25 22:26:29 +000015109 if (argvars[2].v_type != VAR_UNKNOWN)
15110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015111 int error = FALSE;
15112
15113 start_idx = get_tv_number_chk(&argvars[2], &error);
15114 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015116 if (start_idx >= 0)
15117 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 }
15119
15120 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15121 if (pos != NULL)
15122 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123}
15124
15125/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015126 * "string()" function
15127 */
15128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 typval_T *argvars;
15131 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015132{
15133 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015134 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015137 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015138 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140}
15141
15142/*
15143 * "strlen()" function
15144 */
15145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 typval_T *argvars;
15148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150 rettv->vval.v_number = (varnumber_T)(STRLEN(
15151 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152}
15153
15154/*
15155 * "strpart()" function
15156 */
15157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015158f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015159 typval_T *argvars;
15160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161{
15162 char_u *p;
15163 int n;
15164 int len;
15165 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015168 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 slen = (int)STRLEN(p);
15170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015171 n = get_tv_number_chk(&argvars[1], &error);
15172 if (error)
15173 len = 0;
15174 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 else
15177 len = slen - n; /* default len: all bytes that are available. */
15178
15179 /*
15180 * Only return the overlap between the specified part and the actual
15181 * string.
15182 */
15183 if (n < 0)
15184 {
15185 len += n;
15186 n = 0;
15187 }
15188 else if (n > slen)
15189 n = slen;
15190 if (len < 0)
15191 len = 0;
15192 else if (n + len > slen)
15193 len = slen - n;
15194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015195 rettv->v_type = VAR_STRING;
15196 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197}
15198
15199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 * "strridx()" function
15201 */
15202 static void
15203f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015204 typval_T *argvars;
15205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206{
15207 char_u buf[NUMBUFLEN];
15208 char_u *needle;
15209 char_u *haystack;
15210 char_u *rest;
15211 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015212 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015214 needle = get_tv_string_chk(&argvars[1]);
15215 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216
15217 rettv->vval.v_number = -1;
15218 if (needle == NULL || haystack == NULL)
15219 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015220
15221 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015222 if (argvars[2].v_type != VAR_UNKNOWN)
15223 {
15224 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015226 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015227 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015228 }
15229 else
15230 end_idx = haystack_len;
15231
Bram Moolenaar0d660222005-01-07 21:51:51 +000015232 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015233 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015234 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015235 lastmatch = haystack + end_idx;
15236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239 for (rest = haystack; *rest != '\0'; ++rest)
15240 {
15241 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015242 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015243 break;
15244 lastmatch = rest;
15245 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015246 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247
15248 if (lastmatch == NULL)
15249 rettv->vval.v_number = -1;
15250 else
15251 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15252}
15253
15254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 * "strtrans()" function
15256 */
15257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015258f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015259 typval_T *argvars;
15260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262 rettv->v_type = VAR_STRING;
15263 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264}
15265
15266/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015267 * "submatch()" function
15268 */
15269 static void
15270f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015271 typval_T *argvars;
15272 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015273{
15274 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015275 rettv->vval.v_string =
15276 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277}
15278
15279/*
15280 * "substitute()" function
15281 */
15282 static void
15283f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 typval_T *argvars;
15285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286{
15287 char_u patbuf[NUMBUFLEN];
15288 char_u subbuf[NUMBUFLEN];
15289 char_u flagsbuf[NUMBUFLEN];
15290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 char_u *str = get_tv_string_chk(&argvars[0]);
15292 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15293 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15294 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15295
Bram Moolenaar0d660222005-01-07 21:51:51 +000015296 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015297 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15298 rettv->vval.v_string = NULL;
15299 else
15300 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301}
15302
15303/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015304 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 */
15306/*ARGSUSED*/
15307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015309 typval_T *argvars;
15310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311{
15312 int id = 0;
15313#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015314 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 long col;
15316 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015317 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 lnum = get_tv_lnum(argvars); /* -1 on type error */
15320 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15321 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015324 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015325 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326#endif
15327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329}
15330
15331/*
15332 * "synIDattr(id, what [, mode])" function
15333 */
15334/*ARGSUSED*/
15335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015337 typval_T *argvars;
15338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339{
15340 char_u *p = NULL;
15341#ifdef FEAT_SYN_HL
15342 int id;
15343 char_u *what;
15344 char_u *mode;
15345 char_u modebuf[NUMBUFLEN];
15346 int modec;
15347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015348 id = get_tv_number(&argvars[0]);
15349 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015350 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 modec = TOLOWER_ASC(mode[0]);
15354 if (modec != 't' && modec != 'c'
15355#ifdef FEAT_GUI
15356 && modec != 'g'
15357#endif
15358 )
15359 modec = 0; /* replace invalid with current */
15360 }
15361 else
15362 {
15363#ifdef FEAT_GUI
15364 if (gui.in_use)
15365 modec = 'g';
15366 else
15367#endif
15368 if (t_colors > 1)
15369 modec = 'c';
15370 else
15371 modec = 't';
15372 }
15373
15374
15375 switch (TOLOWER_ASC(what[0]))
15376 {
15377 case 'b':
15378 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15379 p = highlight_color(id, what, modec);
15380 else /* bold */
15381 p = highlight_has_attr(id, HL_BOLD, modec);
15382 break;
15383
15384 case 'f': /* fg[#] */
15385 p = highlight_color(id, what, modec);
15386 break;
15387
15388 case 'i':
15389 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15390 p = highlight_has_attr(id, HL_INVERSE, modec);
15391 else /* italic */
15392 p = highlight_has_attr(id, HL_ITALIC, modec);
15393 break;
15394
15395 case 'n': /* name */
15396 p = get_highlight_name(NULL, id - 1);
15397 break;
15398
15399 case 'r': /* reverse */
15400 p = highlight_has_attr(id, HL_INVERSE, modec);
15401 break;
15402
15403 case 's': /* standout */
15404 p = highlight_has_attr(id, HL_STANDOUT, modec);
15405 break;
15406
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015407 case 'u':
15408 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15409 /* underline */
15410 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15411 else
15412 /* undercurl */
15413 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 break;
15415 }
15416
15417 if (p != NULL)
15418 p = vim_strsave(p);
15419#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->v_type = VAR_STRING;
15421 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422}
15423
15424/*
15425 * "synIDtrans(id)" function
15426 */
15427/*ARGSUSED*/
15428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015430 typval_T *argvars;
15431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432{
15433 int id;
15434
15435#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437
15438 if (id > 0)
15439 id = syn_get_final_id(id);
15440 else
15441#endif
15442 id = 0;
15443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445}
15446
15447/*
15448 * "system()" function
15449 */
15450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015451f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015452 typval_T *argvars;
15453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015455 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015457 char_u *infile = NULL;
15458 char_u buf[NUMBUFLEN];
15459 int err = FALSE;
15460 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015463 {
15464 /*
15465 * Write the string to a temp file, to be used for input of the shell
15466 * command.
15467 */
15468 if ((infile = vim_tempname('i')) == NULL)
15469 {
15470 EMSG(_(e_notmp));
15471 return;
15472 }
15473
15474 fd = mch_fopen((char *)infile, WRITEBIN);
15475 if (fd == NULL)
15476 {
15477 EMSG2(_(e_notopen), infile);
15478 goto done;
15479 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015480 p = get_tv_string_buf_chk(&argvars[1], buf);
15481 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015482 {
15483 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015484 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015485 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015486 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15487 err = TRUE;
15488 if (fclose(fd) != 0)
15489 err = TRUE;
15490 if (err)
15491 {
15492 EMSG(_("E677: Error writing temp file"));
15493 goto done;
15494 }
15495 }
15496
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015497 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15498 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015499
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500#ifdef USE_CR
15501 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015502 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 {
15504 char_u *s;
15505
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015506 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 {
15508 if (*s == CAR)
15509 *s = NL;
15510 }
15511 }
15512#else
15513# ifdef USE_CRNL
15514 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015515 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 {
15517 char_u *s, *d;
15518
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015519 d = res;
15520 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 {
15522 if (s[0] == CAR && s[1] == NL)
15523 ++s;
15524 *d++ = *s;
15525 }
15526 *d = NUL;
15527 }
15528# endif
15529#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015530
15531done:
15532 if (infile != NULL)
15533 {
15534 mch_remove(infile);
15535 vim_free(infile);
15536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->v_type = VAR_STRING;
15538 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
15541/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015542 * "tabpagebuflist()" function
15543 */
15544/* ARGSUSED */
15545 static void
15546f_tabpagebuflist(argvars, rettv)
15547 typval_T *argvars;
15548 typval_T *rettv;
15549{
15550#ifndef FEAT_WINDOWS
15551 rettv->vval.v_number = 0;
15552#else
15553 tabpage_T *tp;
15554 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015555
15556 if (argvars[0].v_type == VAR_UNKNOWN)
15557 wp = firstwin;
15558 else
15559 {
15560 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15561 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015562 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015563 }
15564 if (wp == NULL)
15565 rettv->vval.v_number = 0;
15566 else
15567 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015568 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015569 rettv->vval.v_number = 0;
15570 else
15571 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015572 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015573 if (list_append_number(rettv->vval.v_list,
15574 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015575 break;
15576 }
15577 }
15578#endif
15579}
15580
15581
15582/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015583 * "tabpagenr()" function
15584 */
15585/* ARGSUSED */
15586 static void
15587f_tabpagenr(argvars, rettv)
15588 typval_T *argvars;
15589 typval_T *rettv;
15590{
15591 int nr = 1;
15592#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015593 char_u *arg;
15594
15595 if (argvars[0].v_type != VAR_UNKNOWN)
15596 {
15597 arg = get_tv_string_chk(&argvars[0]);
15598 nr = 0;
15599 if (arg != NULL)
15600 {
15601 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015602 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015603 else
15604 EMSG2(_(e_invexpr2), arg);
15605 }
15606 }
15607 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015608 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015609#endif
15610 rettv->vval.v_number = nr;
15611}
15612
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015613
15614#ifdef FEAT_WINDOWS
15615static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15616
15617/*
15618 * Common code for tabpagewinnr() and winnr().
15619 */
15620 static int
15621get_winnr(tp, argvar)
15622 tabpage_T *tp;
15623 typval_T *argvar;
15624{
15625 win_T *twin;
15626 int nr = 1;
15627 win_T *wp;
15628 char_u *arg;
15629
15630 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15631 if (argvar->v_type != VAR_UNKNOWN)
15632 {
15633 arg = get_tv_string_chk(argvar);
15634 if (arg == NULL)
15635 nr = 0; /* type error; errmsg already given */
15636 else if (STRCMP(arg, "$") == 0)
15637 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15638 else if (STRCMP(arg, "#") == 0)
15639 {
15640 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15641 if (twin == NULL)
15642 nr = 0;
15643 }
15644 else
15645 {
15646 EMSG2(_(e_invexpr2), arg);
15647 nr = 0;
15648 }
15649 }
15650
15651 if (nr > 0)
15652 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15653 wp != twin; wp = wp->w_next)
15654 ++nr;
15655 return nr;
15656}
15657#endif
15658
15659/*
15660 * "tabpagewinnr()" function
15661 */
15662/* ARGSUSED */
15663 static void
15664f_tabpagewinnr(argvars, rettv)
15665 typval_T *argvars;
15666 typval_T *rettv;
15667{
15668 int nr = 1;
15669#ifdef FEAT_WINDOWS
15670 tabpage_T *tp;
15671
15672 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15673 if (tp == NULL)
15674 nr = 0;
15675 else
15676 nr = get_winnr(tp, &argvars[1]);
15677#endif
15678 rettv->vval.v_number = nr;
15679}
15680
15681
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015682/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015683 * "tagfiles()" function
15684 */
15685/*ARGSUSED*/
15686 static void
15687f_tagfiles(argvars, rettv)
15688 typval_T *argvars;
15689 typval_T *rettv;
15690{
15691 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015692 tagname_T tn;
15693 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015694
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015695 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015696 {
15697 rettv->vval.v_number = 0;
15698 return;
15699 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 for (first = TRUE; ; first = FALSE)
15702 if (get_tagfname(&tn, first, fname) == FAIL
15703 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015704 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015705 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015706}
15707
15708/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015709 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015710 */
15711 static void
15712f_taglist(argvars, rettv)
15713 typval_T *argvars;
15714 typval_T *rettv;
15715{
15716 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015717
15718 tag_pattern = get_tv_string(&argvars[0]);
15719
15720 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 if (*tag_pattern == NUL)
15722 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015723
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015724 if (rettv_list_alloc(rettv) == OK)
15725 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015726}
15727
15728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 * "tempname()" function
15730 */
15731/*ARGSUSED*/
15732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015733f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 typval_T *argvars;
15735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736{
15737 static int x = 'A';
15738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015739 rettv->v_type = VAR_STRING;
15740 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741
15742 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15743 * names. Skip 'I' and 'O', they are used for shell redirection. */
15744 do
15745 {
15746 if (x == 'Z')
15747 x = '0';
15748 else if (x == '9')
15749 x = 'A';
15750 else
15751 {
15752#ifdef EBCDIC
15753 if (x == 'I')
15754 x = 'J';
15755 else if (x == 'R')
15756 x = 'S';
15757 else
15758#endif
15759 ++x;
15760 }
15761 } while (x == 'I' || x == 'O');
15762}
15763
15764/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015765 * "test(list)" function: Just checking the walls...
15766 */
15767/*ARGSUSED*/
15768 static void
15769f_test(argvars, rettv)
15770 typval_T *argvars;
15771 typval_T *rettv;
15772{
15773 /* Used for unit testing. Change the code below to your liking. */
15774#if 0
15775 listitem_T *li;
15776 list_T *l;
15777 char_u *bad, *good;
15778
15779 if (argvars[0].v_type != VAR_LIST)
15780 return;
15781 l = argvars[0].vval.v_list;
15782 if (l == NULL)
15783 return;
15784 li = l->lv_first;
15785 if (li == NULL)
15786 return;
15787 bad = get_tv_string(&li->li_tv);
15788 li = li->li_next;
15789 if (li == NULL)
15790 return;
15791 good = get_tv_string(&li->li_tv);
15792 rettv->vval.v_number = test_edit_score(bad, good);
15793#endif
15794}
15795
15796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 * "tolower(string)" function
15798 */
15799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 typval_T *argvars;
15802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803{
15804 char_u *p;
15805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015806 p = vim_strsave(get_tv_string(&argvars[0]));
15807 rettv->v_type = VAR_STRING;
15808 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809
15810 if (p != NULL)
15811 while (*p != NUL)
15812 {
15813#ifdef FEAT_MBYTE
15814 int l;
15815
15816 if (enc_utf8)
15817 {
15818 int c, lc;
15819
15820 c = utf_ptr2char(p);
15821 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015822 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 /* TODO: reallocate string when byte count changes. */
15824 if (utf_char2len(lc) == l)
15825 utf_char2bytes(lc, p);
15826 p += l;
15827 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015828 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 p += l; /* skip multi-byte character */
15830 else
15831#endif
15832 {
15833 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15834 ++p;
15835 }
15836 }
15837}
15838
15839/*
15840 * "toupper(string)" function
15841 */
15842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015843f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015844 typval_T *argvars;
15845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015847 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849}
15850
15851/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015852 * "tr(string, fromstr, tostr)" function
15853 */
15854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015855f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015856 typval_T *argvars;
15857 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015858{
15859 char_u *instr;
15860 char_u *fromstr;
15861 char_u *tostr;
15862 char_u *p;
15863#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015864 int inlen;
15865 int fromlen;
15866 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015867 int idx;
15868 char_u *cpstr;
15869 int cplen;
15870 int first = TRUE;
15871#endif
15872 char_u buf[NUMBUFLEN];
15873 char_u buf2[NUMBUFLEN];
15874 garray_T ga;
15875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015876 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015877 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15878 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015879
15880 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015881 rettv->v_type = VAR_STRING;
15882 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015883 if (fromstr == NULL || tostr == NULL)
15884 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015885 ga_init2(&ga, (int)sizeof(char), 80);
15886
15887#ifdef FEAT_MBYTE
15888 if (!has_mbyte)
15889#endif
15890 /* not multi-byte: fromstr and tostr must be the same length */
15891 if (STRLEN(fromstr) != STRLEN(tostr))
15892 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015893#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015894error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015895#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015896 EMSG2(_(e_invarg2), fromstr);
15897 ga_clear(&ga);
15898 return;
15899 }
15900
15901 /* fromstr and tostr have to contain the same number of chars */
15902 while (*instr != NUL)
15903 {
15904#ifdef FEAT_MBYTE
15905 if (has_mbyte)
15906 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015907 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015908 cpstr = instr;
15909 cplen = inlen;
15910 idx = 0;
15911 for (p = fromstr; *p != NUL; p += fromlen)
15912 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015913 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015914 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15915 {
15916 for (p = tostr; *p != NUL; p += tolen)
15917 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015918 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015919 if (idx-- == 0)
15920 {
15921 cplen = tolen;
15922 cpstr = p;
15923 break;
15924 }
15925 }
15926 if (*p == NUL) /* tostr is shorter than fromstr */
15927 goto error;
15928 break;
15929 }
15930 ++idx;
15931 }
15932
15933 if (first && cpstr == instr)
15934 {
15935 /* Check that fromstr and tostr have the same number of
15936 * (multi-byte) characters. Done only once when a character
15937 * of instr doesn't appear in fromstr. */
15938 first = FALSE;
15939 for (p = tostr; *p != NUL; p += tolen)
15940 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015941 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015942 --idx;
15943 }
15944 if (idx != 0)
15945 goto error;
15946 }
15947
15948 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015949 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015950 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015951
15952 instr += inlen;
15953 }
15954 else
15955#endif
15956 {
15957 /* When not using multi-byte chars we can do it faster. */
15958 p = vim_strchr(fromstr, *instr);
15959 if (p != NULL)
15960 ga_append(&ga, tostr[p - fromstr]);
15961 else
15962 ga_append(&ga, *instr);
15963 ++instr;
15964 }
15965 }
15966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015967 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015968}
15969
15970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 * "type(expr)" function
15972 */
15973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015974f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015975 typval_T *argvars;
15976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015978 int n;
15979
15980 switch (argvars[0].v_type)
15981 {
15982 case VAR_NUMBER: n = 0; break;
15983 case VAR_STRING: n = 1; break;
15984 case VAR_FUNC: n = 2; break;
15985 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015986 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015987 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15988 }
15989 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990}
15991
15992/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015993 * "values(dict)" function
15994 */
15995 static void
15996f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *argvars;
15998 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015999{
16000 dict_list(argvars, rettv, 1);
16001}
16002
16003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 * "virtcol(string)" function
16005 */
16006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016007f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016008 typval_T *argvars;
16009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010{
16011 colnr_T vcol = 0;
16012 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016013 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016015 fp = var2fpos(&argvars[0], FALSE, &fnum);
16016 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16017 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 {
16019 getvvcol(curwin, fp, NULL, NULL, &vcol);
16020 ++vcol;
16021 }
16022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016023 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024}
16025
16026/*
16027 * "visualmode()" function
16028 */
16029/*ARGSUSED*/
16030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016031f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 typval_T *argvars;
16033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034{
16035#ifdef FEAT_VISUAL
16036 char_u str[2];
16037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016038 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 str[0] = curbuf->b_visual_mode_eval;
16040 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016041 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042
16043 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016044 if ((argvars[0].v_type == VAR_NUMBER
16045 && argvars[0].vval.v_number != 0)
16046 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016047 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 curbuf->b_visual_mode_eval = NUL;
16049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051#endif
16052}
16053
16054/*
16055 * "winbufnr(nr)" function
16056 */
16057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016058f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 typval_T *argvars;
16060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061{
16062 win_T *wp;
16063
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016064 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016068 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069}
16070
16071/*
16072 * "wincol()" function
16073 */
16074/*ARGSUSED*/
16075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 typval_T *argvars;
16078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079{
16080 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082}
16083
16084/*
16085 * "winheight(nr)" function
16086 */
16087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016088f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 typval_T *argvars;
16090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091{
16092 win_T *wp;
16093
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016094 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016098 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099}
16100
16101/*
16102 * "winline()" function
16103 */
16104/*ARGSUSED*/
16105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016106f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016107 typval_T *argvars;
16108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109{
16110 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112}
16113
16114/*
16115 * "winnr()" function
16116 */
16117/* ARGSUSED */
16118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016119f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016120 typval_T *argvars;
16121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122{
16123 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016124
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016126 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016128 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129}
16130
16131/*
16132 * "winrestcmd()" function
16133 */
16134/* ARGSUSED */
16135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016137 typval_T *argvars;
16138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139{
16140#ifdef FEAT_WINDOWS
16141 win_T *wp;
16142 int winnr = 1;
16143 garray_T ga;
16144 char_u buf[50];
16145
16146 ga_init2(&ga, (int)sizeof(char), 70);
16147 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16148 {
16149 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16150 ga_concat(&ga, buf);
16151# ifdef FEAT_VERTSPLIT
16152 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16153 ga_concat(&ga, buf);
16154# endif
16155 ++winnr;
16156 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016157 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164}
16165
16166/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016167 * "winrestview()" function
16168 */
16169/* ARGSUSED */
16170 static void
16171f_winrestview(argvars, rettv)
16172 typval_T *argvars;
16173 typval_T *rettv;
16174{
16175 dict_T *dict;
16176
16177 if (argvars[0].v_type != VAR_DICT
16178 || (dict = argvars[0].vval.v_dict) == NULL)
16179 EMSG(_(e_invarg));
16180 else
16181 {
16182 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16183 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16184#ifdef FEAT_VIRTUALEDIT
16185 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16186#endif
16187 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016188 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016189
16190 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16191#ifdef FEAT_DIFF
16192 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16193#endif
16194 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16195 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16196
16197 check_cursor();
16198 changed_cline_bef_curs();
16199 invalidate_botline();
16200 redraw_later(VALID);
16201
16202 if (curwin->w_topline == 0)
16203 curwin->w_topline = 1;
16204 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16205 curwin->w_topline = curbuf->b_ml.ml_line_count;
16206#ifdef FEAT_DIFF
16207 check_topfill(curwin, TRUE);
16208#endif
16209 }
16210}
16211
16212/*
16213 * "winsaveview()" function
16214 */
16215/* ARGSUSED */
16216 static void
16217f_winsaveview(argvars, rettv)
16218 typval_T *argvars;
16219 typval_T *rettv;
16220{
16221 dict_T *dict;
16222
16223 dict = dict_alloc();
16224 if (dict == NULL)
16225 return;
16226 rettv->v_type = VAR_DICT;
16227 rettv->vval.v_dict = dict;
16228 ++dict->dv_refcount;
16229
16230 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16231 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16232#ifdef FEAT_VIRTUALEDIT
16233 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16234#endif
16235 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16236
16237 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16238#ifdef FEAT_DIFF
16239 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16240#endif
16241 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16242 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16243}
16244
16245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 * "winwidth(nr)" function
16247 */
16248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016249f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016250 typval_T *argvars;
16251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252{
16253 win_T *wp;
16254
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016255 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 else
16259#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263#endif
16264}
16265
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016267 * "writefile()" function
16268 */
16269 static void
16270f_writefile(argvars, rettv)
16271 typval_T *argvars;
16272 typval_T *rettv;
16273{
16274 int binary = FALSE;
16275 char_u *fname;
16276 FILE *fd;
16277 listitem_T *li;
16278 char_u *s;
16279 int ret = 0;
16280 int c;
16281
16282 if (argvars[0].v_type != VAR_LIST)
16283 {
16284 EMSG2(_(e_listarg), "writefile()");
16285 return;
16286 }
16287 if (argvars[0].vval.v_list == NULL)
16288 return;
16289
16290 if (argvars[2].v_type != VAR_UNKNOWN
16291 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16292 binary = TRUE;
16293
16294 /* Always open the file in binary mode, library functions have a mind of
16295 * their own about CR-LF conversion. */
16296 fname = get_tv_string(&argvars[1]);
16297 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16298 {
16299 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16300 ret = -1;
16301 }
16302 else
16303 {
16304 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16305 li = li->li_next)
16306 {
16307 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16308 {
16309 if (*s == '\n')
16310 c = putc(NUL, fd);
16311 else
16312 c = putc(*s, fd);
16313 if (c == EOF)
16314 {
16315 ret = -1;
16316 break;
16317 }
16318 }
16319 if (!binary || li->li_next != NULL)
16320 if (putc('\n', fd) == EOF)
16321 {
16322 ret = -1;
16323 break;
16324 }
16325 if (ret < 0)
16326 {
16327 EMSG(_(e_write));
16328 break;
16329 }
16330 }
16331 fclose(fd);
16332 }
16333
16334 rettv->vval.v_number = ret;
16335}
16336
16337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016339 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 */
16341 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016342var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016345 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016347 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016349 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350
Bram Moolenaara5525202006-03-02 22:52:09 +000016351 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016352 if (varp->v_type == VAR_LIST)
16353 {
16354 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016355 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016356 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016357
16358 l = varp->vval.v_list;
16359 if (l == NULL)
16360 return NULL;
16361
16362 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016363 pos.lnum = list_find_nr(l, 0L, &error);
16364 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016365 return NULL; /* invalid line number */
16366
16367 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016368 pos.col = list_find_nr(l, 1L, &error);
16369 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016370 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016371 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016372 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016373 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016374 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016375 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016376
Bram Moolenaara5525202006-03-02 22:52:09 +000016377#ifdef FEAT_VIRTUALEDIT
16378 /* Get the virtual offset. Defaults to zero. */
16379 pos.coladd = list_find_nr(l, 2L, &error);
16380 if (error)
16381 pos.coladd = 0;
16382#endif
16383
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016384 return &pos;
16385 }
16386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016387 name = get_tv_string_chk(varp);
16388 if (name == NULL)
16389 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 if (name[0] == '.') /* cursor */
16391 return &curwin->w_cursor;
16392 if (name[0] == '\'') /* mark */
16393 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016394 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16396 return NULL;
16397 return pp;
16398 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016399
16400#ifdef FEAT_VIRTUALEDIT
16401 pos.coladd = 0;
16402#endif
16403
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016404 if (name[0] == 'w' && lnum)
16405 {
16406 pos.col = 0;
16407 if (name[1] == '0') /* "w0": first visible line */
16408 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016409 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016410 pos.lnum = curwin->w_topline;
16411 return &pos;
16412 }
16413 else if (name[1] == '$') /* "w$": last visible line */
16414 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016415 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016416 pos.lnum = curwin->w_botline - 1;
16417 return &pos;
16418 }
16419 }
16420 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 {
16422 if (lnum)
16423 {
16424 pos.lnum = curbuf->b_ml.ml_line_count;
16425 pos.col = 0;
16426 }
16427 else
16428 {
16429 pos.lnum = curwin->w_cursor.lnum;
16430 pos.col = (colnr_T)STRLEN(ml_get_curline());
16431 }
16432 return &pos;
16433 }
16434 return NULL;
16435}
16436
16437/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016438 * Convert list in "arg" into a position and optional file number.
16439 * When "fnump" is NULL there is no file number, only 3 items.
16440 * Note that the column is passed on as-is, the caller may want to decrement
16441 * it to use 1 for the first column.
16442 * Return FAIL when conversion is not possible, doesn't check the position for
16443 * validity.
16444 */
16445 static int
16446list2fpos(arg, posp, fnump)
16447 typval_T *arg;
16448 pos_T *posp;
16449 int *fnump;
16450{
16451 list_T *l = arg->vval.v_list;
16452 long i = 0;
16453 long n;
16454
Bram Moolenaarbde35262006-07-23 20:12:24 +000016455 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16456 * when "fnump" isn't NULL and "coladd" is optional. */
16457 if (arg->v_type != VAR_LIST
16458 || l == NULL
16459 || l->lv_len < (fnump == NULL ? 2 : 3)
16460 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016461 return FAIL;
16462
16463 if (fnump != NULL)
16464 {
16465 n = list_find_nr(l, i++, NULL); /* fnum */
16466 if (n < 0)
16467 return FAIL;
16468 if (n == 0)
16469 n = curbuf->b_fnum; /* current buffer */
16470 *fnump = n;
16471 }
16472
16473 n = list_find_nr(l, i++, NULL); /* lnum */
16474 if (n < 0)
16475 return FAIL;
16476 posp->lnum = n;
16477
16478 n = list_find_nr(l, i++, NULL); /* col */
16479 if (n < 0)
16480 return FAIL;
16481 posp->col = n;
16482
16483#ifdef FEAT_VIRTUALEDIT
16484 n = list_find_nr(l, i, NULL);
16485 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016486 posp->coladd = 0;
16487 else
16488 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016489#endif
16490
16491 return OK;
16492}
16493
16494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 * Get the length of an environment variable name.
16496 * Advance "arg" to the first character after the name.
16497 * Return 0 for error.
16498 */
16499 static int
16500get_env_len(arg)
16501 char_u **arg;
16502{
16503 char_u *p;
16504 int len;
16505
16506 for (p = *arg; vim_isIDc(*p); ++p)
16507 ;
16508 if (p == *arg) /* no name found */
16509 return 0;
16510
16511 len = (int)(p - *arg);
16512 *arg = p;
16513 return len;
16514}
16515
16516/*
16517 * Get the length of the name of a function or internal variable.
16518 * "arg" is advanced to the first non-white character after the name.
16519 * Return 0 if something is wrong.
16520 */
16521 static int
16522get_id_len(arg)
16523 char_u **arg;
16524{
16525 char_u *p;
16526 int len;
16527
16528 /* Find the end of the name. */
16529 for (p = *arg; eval_isnamec(*p); ++p)
16530 ;
16531 if (p == *arg) /* no name found */
16532 return 0;
16533
16534 len = (int)(p - *arg);
16535 *arg = skipwhite(p);
16536
16537 return len;
16538}
16539
16540/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016541 * Get the length of the name of a variable or function.
16542 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016544 * Return -1 if curly braces expansion failed.
16545 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 * If the name contains 'magic' {}'s, expand them and return the
16547 * expanded name in an allocated string via 'alias' - caller must free.
16548 */
16549 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 char_u **arg;
16552 char_u **alias;
16553 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555{
16556 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 char_u *p;
16558 char_u *expr_start;
16559 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560
16561 *alias = NULL; /* default to no alias */
16562
16563 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16564 && (*arg)[2] == (int)KE_SNR)
16565 {
16566 /* hard coded <SNR>, already translated */
16567 *arg += 3;
16568 return get_id_len(arg) + 3;
16569 }
16570 len = eval_fname_script(*arg);
16571 if (len > 0)
16572 {
16573 /* literal "<SID>", "s:" or "<SNR>" */
16574 *arg += len;
16575 }
16576
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016578 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016580 p = find_name_end(*arg, &expr_start, &expr_end,
16581 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 if (expr_start != NULL)
16583 {
16584 char_u *temp_string;
16585
16586 if (!evaluate)
16587 {
16588 len += (int)(p - *arg);
16589 *arg = skipwhite(p);
16590 return len;
16591 }
16592
16593 /*
16594 * Include any <SID> etc in the expanded string:
16595 * Thus the -len here.
16596 */
16597 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16598 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 *alias = temp_string;
16601 *arg = skipwhite(p);
16602 return (int)STRLEN(temp_string);
16603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604
16605 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016606 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 EMSG2(_(e_invexpr2), *arg);
16608
16609 return len;
16610}
16611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016612/*
16613 * Find the end of a variable or function name, taking care of magic braces.
16614 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16615 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016616 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617 * Return a pointer to just after the name. Equal to "arg" if there is no
16618 * valid name.
16619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016621find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 char_u *arg;
16623 char_u **expr_start;
16624 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016625 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016627 int mb_nest = 0;
16628 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 char_u *p;
16630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016631 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 *expr_start = NULL;
16634 *expr_end = NULL;
16635 }
16636
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016637 /* Quick check for valid starting character. */
16638 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16639 return arg;
16640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641 for (p = arg; *p != NUL
16642 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016643 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016644 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016646 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016648 if (*p == '\'')
16649 {
16650 /* skip over 'string' to avoid counting [ and ] inside it. */
16651 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16652 ;
16653 if (*p == NUL)
16654 break;
16655 }
16656 else if (*p == '"')
16657 {
16658 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16659 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16660 if (*p == '\\' && p[1] != NUL)
16661 ++p;
16662 if (*p == NUL)
16663 break;
16664 }
16665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016668 if (*p == '[')
16669 ++br_nest;
16670 else if (*p == ']')
16671 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016674 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 if (*p == '{')
16677 {
16678 mb_nest++;
16679 if (expr_start != NULL && *expr_start == NULL)
16680 *expr_start = p;
16681 }
16682 else if (*p == '}')
16683 {
16684 mb_nest--;
16685 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16686 *expr_end = p;
16687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 }
16690
16691 return p;
16692}
16693
16694/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016695 * Expands out the 'magic' {}'s in a variable/function name.
16696 * Note that this can call itself recursively, to deal with
16697 * constructs like foo{bar}{baz}{bam}
16698 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16699 * "in_start" ^
16700 * "expr_start" ^
16701 * "expr_end" ^
16702 * "in_end" ^
16703 *
16704 * Returns a new allocated string, which the caller must free.
16705 * Returns NULL for failure.
16706 */
16707 static char_u *
16708make_expanded_name(in_start, expr_start, expr_end, in_end)
16709 char_u *in_start;
16710 char_u *expr_start;
16711 char_u *expr_end;
16712 char_u *in_end;
16713{
16714 char_u c1;
16715 char_u *retval = NULL;
16716 char_u *temp_result;
16717 char_u *nextcmd = NULL;
16718
16719 if (expr_end == NULL || in_end == NULL)
16720 return NULL;
16721 *expr_start = NUL;
16722 *expr_end = NUL;
16723 c1 = *in_end;
16724 *in_end = NUL;
16725
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016726 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016727 if (temp_result != NULL && nextcmd == NULL)
16728 {
16729 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16730 + (in_end - expr_end) + 1));
16731 if (retval != NULL)
16732 {
16733 STRCPY(retval, in_start);
16734 STRCAT(retval, temp_result);
16735 STRCAT(retval, expr_end + 1);
16736 }
16737 }
16738 vim_free(temp_result);
16739
16740 *in_end = c1; /* put char back for error messages */
16741 *expr_start = '{';
16742 *expr_end = '}';
16743
16744 if (retval != NULL)
16745 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016746 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016747 if (expr_start != NULL)
16748 {
16749 /* Further expansion! */
16750 temp_result = make_expanded_name(retval, expr_start,
16751 expr_end, temp_result);
16752 vim_free(retval);
16753 retval = temp_result;
16754 }
16755 }
16756
16757 return retval;
16758}
16759
16760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016762 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 */
16764 static int
16765eval_isnamec(c)
16766 int c;
16767{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016768 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16769}
16770
16771/*
16772 * Return TRUE if character "c" can be used as the first character in a
16773 * variable or function name (excluding '{' and '}').
16774 */
16775 static int
16776eval_isnamec1(c)
16777 int c;
16778{
16779 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780}
16781
16782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 * Set number v: variable to "val".
16784 */
16785 void
16786set_vim_var_nr(idx, val)
16787 int idx;
16788 long val;
16789{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016790 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791}
16792
16793/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016794 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 */
16796 long
16797get_vim_var_nr(idx)
16798 int idx;
16799{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016800 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801}
16802
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016803#if defined(FEAT_AUTOCMD) || defined(PROTO)
16804/*
16805 * Get string v: variable value. Uses a static buffer, can only be used once.
16806 */
16807 char_u *
16808get_vim_var_str(idx)
16809 int idx;
16810{
16811 return get_tv_string(&vimvars[idx].vv_tv);
16812}
16813#endif
16814
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815/*
16816 * Set v:count, v:count1 and v:prevcount.
16817 */
16818 void
16819set_vcount(count, count1)
16820 long count;
16821 long count1;
16822{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016823 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16824 vimvars[VV_COUNT].vv_nr = count;
16825 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826}
16827
16828/*
16829 * Set string v: variable to a copy of "val".
16830 */
16831 void
16832set_vim_var_string(idx, val, len)
16833 int idx;
16834 char_u *val;
16835 int len; /* length of "val" to use or -1 (whole string) */
16836{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016837 /* Need to do this (at least) once, since we can't initialize a union.
16838 * Will always be invoked when "v:progname" is set. */
16839 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16840
Bram Moolenaare9a41262005-01-15 22:18:47 +000016841 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016843 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016845 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016847 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848}
16849
16850/*
16851 * Set v:register if needed.
16852 */
16853 void
16854set_reg_var(c)
16855 int c;
16856{
16857 char_u regname;
16858
16859 if (c == 0 || c == ' ')
16860 regname = '"';
16861 else
16862 regname = c;
16863 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016864 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 set_vim_var_string(VV_REG, &regname, 1);
16866}
16867
16868/*
16869 * Get or set v:exception. If "oldval" == NULL, return the current value.
16870 * Otherwise, restore the value to "oldval" and return NULL.
16871 * Must always be called in pairs to save and restore v:exception! Does not
16872 * take care of memory allocations.
16873 */
16874 char_u *
16875v_exception(oldval)
16876 char_u *oldval;
16877{
16878 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016879 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880
Bram Moolenaare9a41262005-01-15 22:18:47 +000016881 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 return NULL;
16883}
16884
16885/*
16886 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16887 * Otherwise, restore the value to "oldval" and return NULL.
16888 * Must always be called in pairs to save and restore v:throwpoint! Does not
16889 * take care of memory allocations.
16890 */
16891 char_u *
16892v_throwpoint(oldval)
16893 char_u *oldval;
16894{
16895 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016896 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897
Bram Moolenaare9a41262005-01-15 22:18:47 +000016898 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 return NULL;
16900}
16901
16902#if defined(FEAT_AUTOCMD) || defined(PROTO)
16903/*
16904 * Set v:cmdarg.
16905 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16906 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16907 * Must always be called in pairs!
16908 */
16909 char_u *
16910set_cmdarg(eap, oldarg)
16911 exarg_T *eap;
16912 char_u *oldarg;
16913{
16914 char_u *oldval;
16915 char_u *newval;
16916 unsigned len;
16917
Bram Moolenaare9a41262005-01-15 22:18:47 +000016918 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016919 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016921 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016922 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016923 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 }
16925
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016926 if (eap->force_bin == FORCE_BIN)
16927 len = 6;
16928 else if (eap->force_bin == FORCE_NOBIN)
16929 len = 8;
16930 else
16931 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016932
16933 if (eap->read_edit)
16934 len += 7;
16935
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016936 if (eap->force_ff != 0)
16937 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16938# ifdef FEAT_MBYTE
16939 if (eap->force_enc != 0)
16940 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016941 if (eap->bad_char != 0)
16942 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016943# endif
16944
16945 newval = alloc(len + 1);
16946 if (newval == NULL)
16947 return NULL;
16948
16949 if (eap->force_bin == FORCE_BIN)
16950 sprintf((char *)newval, " ++bin");
16951 else if (eap->force_bin == FORCE_NOBIN)
16952 sprintf((char *)newval, " ++nobin");
16953 else
16954 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016955
16956 if (eap->read_edit)
16957 STRCAT(newval, " ++edit");
16958
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016959 if (eap->force_ff != 0)
16960 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16961 eap->cmd + eap->force_ff);
16962# ifdef FEAT_MBYTE
16963 if (eap->force_enc != 0)
16964 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16965 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016966 if (eap->bad_char != 0)
16967 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16968 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016969# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016970 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016971 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973#endif
16974
16975/*
16976 * Get the value of internal variable "name".
16977 * Return OK or FAIL.
16978 */
16979 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016980get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 char_u *name;
16982 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016984 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985{
16986 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016987 typval_T *tv = NULL;
16988 typval_T atv;
16989 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
16992 /* truncate the name, so that we can use strcmp() */
16993 cc = name[len];
16994 name[len] = NUL;
16995
16996 /*
16997 * Check for "b:changedtick".
16998 */
16999 if (STRCMP(name, "b:changedtick") == 0)
17000 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017001 atv.v_type = VAR_NUMBER;
17002 atv.vval.v_number = curbuf->b_changedtick;
17003 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 }
17005
17006 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 * Check for user-defined variables.
17008 */
17009 else
17010 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017011 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 }
17015
Bram Moolenaare9a41262005-01-15 22:18:47 +000017016 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017018 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017019 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 ret = FAIL;
17021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017023 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024
17025 name[len] = cc;
17026
17027 return ret;
17028}
17029
17030/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017031 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17032 * Also handle function call with Funcref variable: func(expr)
17033 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17034 */
17035 static int
17036handle_subscript(arg, rettv, evaluate, verbose)
17037 char_u **arg;
17038 typval_T *rettv;
17039 int evaluate; /* do more than finding the end */
17040 int verbose; /* give error messages */
17041{
17042 int ret = OK;
17043 dict_T *selfdict = NULL;
17044 char_u *s;
17045 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017046 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017047
17048 while (ret == OK
17049 && (**arg == '['
17050 || (**arg == '.' && rettv->v_type == VAR_DICT)
17051 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17052 && !vim_iswhite(*(*arg - 1)))
17053 {
17054 if (**arg == '(')
17055 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017056 /* need to copy the funcref so that we can clear rettv */
17057 functv = *rettv;
17058 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017059
17060 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017061 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017062 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017063 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17064 &len, evaluate, selfdict);
17065
17066 /* Clear the funcref afterwards, so that deleting it while
17067 * evaluating the arguments is possible (see test55). */
17068 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017069
17070 /* Stop the expression evaluation when immediately aborting on
17071 * error, or when an interrupt occurred or an exception was thrown
17072 * but not caught. */
17073 if (aborting())
17074 {
17075 if (ret == OK)
17076 clear_tv(rettv);
17077 ret = FAIL;
17078 }
17079 dict_unref(selfdict);
17080 selfdict = NULL;
17081 }
17082 else /* **arg == '[' || **arg == '.' */
17083 {
17084 dict_unref(selfdict);
17085 if (rettv->v_type == VAR_DICT)
17086 {
17087 selfdict = rettv->vval.v_dict;
17088 if (selfdict != NULL)
17089 ++selfdict->dv_refcount;
17090 }
17091 else
17092 selfdict = NULL;
17093 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17094 {
17095 clear_tv(rettv);
17096 ret = FAIL;
17097 }
17098 }
17099 }
17100 dict_unref(selfdict);
17101 return ret;
17102}
17103
17104/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17106 * value).
17107 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017109alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017110{
Bram Moolenaar33570922005-01-25 22:26:29 +000017111 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017112}
17113
17114/*
17115 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 * The string "s" must have been allocated, it is consumed.
17117 * Return NULL for out of memory, the variable otherwise.
17118 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017119 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 char_u *s;
17122{
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 rettv = alloc_tv();
17126 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 rettv->v_type = VAR_STRING;
17129 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 }
17131 else
17132 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134}
17135
17136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017137 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017139 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017140free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017141 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142{
17143 if (varp != NULL)
17144 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017145 switch (varp->v_type)
17146 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017147 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017148 func_unref(varp->vval.v_string);
17149 /*FALLTHROUGH*/
17150 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017151 vim_free(varp->vval.v_string);
17152 break;
17153 case VAR_LIST:
17154 list_unref(varp->vval.v_list);
17155 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017156 case VAR_DICT:
17157 dict_unref(varp->vval.v_dict);
17158 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017159 case VAR_NUMBER:
17160 case VAR_UNKNOWN:
17161 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017162 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017163 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017164 break;
17165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 vim_free(varp);
17167 }
17168}
17169
17170/*
17171 * Free the memory for a variable value and set the value to NULL or 0.
17172 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017173 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176{
17177 if (varp != NULL)
17178 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017179 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017181 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017182 func_unref(varp->vval.v_string);
17183 /*FALLTHROUGH*/
17184 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017185 vim_free(varp->vval.v_string);
17186 varp->vval.v_string = NULL;
17187 break;
17188 case VAR_LIST:
17189 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017190 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017191 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017192 case VAR_DICT:
17193 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017194 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017195 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017196 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017197 varp->vval.v_number = 0;
17198 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 case VAR_UNKNOWN:
17200 break;
17201 default:
17202 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017204 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 }
17206}
17207
17208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017209 * Set the value of a variable to NULL without freeing items.
17210 */
17211 static void
17212init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214{
17215 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017217}
17218
17219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 * Get the number value of a variable.
17221 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017222 * For incompatible types, return 0.
17223 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17224 * caller of incompatible types: it sets *denote to TRUE if "denote"
17225 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 */
17227 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017228get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017231 int error = FALSE;
17232
17233 return get_tv_number_chk(varp, &error); /* return 0L on error */
17234}
17235
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017236 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017237get_tv_number_chk(varp, denote)
17238 typval_T *varp;
17239 int *denote;
17240{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017241 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017243 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017246 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017247 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017248 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017249 break;
17250 case VAR_STRING:
17251 if (varp->vval.v_string != NULL)
17252 vim_str2nr(varp->vval.v_string, NULL, NULL,
17253 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017254 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017255 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017256 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017257 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017258 case VAR_DICT:
17259 EMSG(_("E728: Using a Dictionary as a number"));
17260 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017261 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017262 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017265 if (denote == NULL) /* useful for values that must be unsigned */
17266 n = -1;
17267 else
17268 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017269 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270}
17271
17272/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017273 * Get the lnum from the first argument.
17274 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017275 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 */
17277 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017279 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280{
Bram Moolenaar33570922005-01-25 22:26:29 +000017281 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 linenr_T lnum;
17283
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 if (lnum == 0) /* no valid number, try using line() */
17286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287 rettv.v_type = VAR_NUMBER;
17288 f_line(argvars, &rettv);
17289 lnum = rettv.vval.v_number;
17290 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 }
17292 return lnum;
17293}
17294
17295/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017296 * Get the lnum from the first argument.
17297 * Also accepts "$", then "buf" is used.
17298 * Returns 0 on error.
17299 */
17300 static linenr_T
17301get_tv_lnum_buf(argvars, buf)
17302 typval_T *argvars;
17303 buf_T *buf;
17304{
17305 if (argvars[0].v_type == VAR_STRING
17306 && argvars[0].vval.v_string != NULL
17307 && argvars[0].vval.v_string[0] == '$'
17308 && buf != NULL)
17309 return buf->b_ml.ml_line_count;
17310 return get_tv_number_chk(&argvars[0], NULL);
17311}
17312
17313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 * Get the string value of a variable.
17315 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017316 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17317 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 * If the String variable has never been set, return an empty string.
17319 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017320 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17321 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 */
17323 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017324get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326{
17327 static char_u mybuf[NUMBUFLEN];
17328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330}
17331
17332 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017335 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 char_u *res = get_tv_string_buf_chk(varp, buf);
17338
17339 return res != NULL ? res : (char_u *)"";
17340}
17341
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017342 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017343get_tv_string_chk(varp)
17344 typval_T *varp;
17345{
17346 static char_u mybuf[NUMBUFLEN];
17347
17348 return get_tv_string_buf_chk(varp, mybuf);
17349}
17350
17351 static char_u *
17352get_tv_string_buf_chk(varp, buf)
17353 typval_T *varp;
17354 char_u *buf;
17355{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017356 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017358 case VAR_NUMBER:
17359 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17360 return buf;
17361 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017362 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017363 break;
17364 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017365 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017366 break;
17367 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017368 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017369 break;
17370 case VAR_STRING:
17371 if (varp->vval.v_string != NULL)
17372 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017374 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017378 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379}
17380
17381/*
17382 * Find variable "name" in the list of variables.
17383 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017384 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017385 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017386 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017389find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395
Bram Moolenaara7043832005-01-21 11:56:39 +000017396 ht = find_var_ht(name, &varname);
17397 if (htp != NULL)
17398 *htp = ht;
17399 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017401 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402}
17403
17404/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017405 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017406 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017409find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017411 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017412 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017413{
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 hashitem_T *hi;
17415
17416 if (*varname == NUL)
17417 {
17418 /* Must be something like "s:", otherwise "ht" would be NULL. */
17419 switch (varname[-2])
17420 {
17421 case 's': return &SCRIPT_SV(current_SID).sv_var;
17422 case 'g': return &globvars_var;
17423 case 'v': return &vimvars_var;
17424 case 'b': return &curbuf->b_bufvar;
17425 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017426#ifdef FEAT_WINDOWS
17427 case 't': return &curtab->tp_winvar;
17428#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017429 case 'l': return current_funccal == NULL
17430 ? NULL : &current_funccal->l_vars_var;
17431 case 'a': return current_funccal == NULL
17432 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017433 }
17434 return NULL;
17435 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017436
17437 hi = hash_find(ht, varname);
17438 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017439 {
17440 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017441 * worked find the variable again. Don't auto-load a script if it was
17442 * loaded already, otherwise it would be loaded every time when
17443 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017444 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017445 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017446 hi = hash_find(ht, varname);
17447 if (HASHITEM_EMPTY(hi))
17448 return NULL;
17449 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017450 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017451}
17452
17453/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017454 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017455 * Set "varname" to the start of name without ':'.
17456 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017458find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 char_u *name;
17460 char_u **varname;
17461{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017462 hashitem_T *hi;
17463
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 if (name[1] != ':')
17465 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017466 /* The name must not start with a colon or #. */
17467 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 return NULL;
17469 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017470
17471 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017472 hi = hash_find(&compat_hashtab, name);
17473 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017474 return &compat_hashtab;
17475
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017477 return &globvarht; /* global variable */
17478 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 }
17480 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017481 if (*name == 'g') /* global variable */
17482 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017483 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17484 */
17485 if (vim_strchr(name + 2, ':') != NULL
17486 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017487 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017492#ifdef FEAT_WINDOWS
17493 if (*name == 't') /* tab page variable */
17494 return &curtab->tp_vars.dv_hashtab;
17495#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 if (*name == 'v') /* v: variable */
17497 return &vimvarht;
17498 if (*name == 'a' && current_funccal != NULL) /* function argument */
17499 return &current_funccal->l_avars.dv_hashtab;
17500 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17501 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 if (*name == 's' /* script variable */
17503 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17504 return &SCRIPT_VARS(current_SID);
17505 return NULL;
17506}
17507
17508/*
17509 * Get the string value of a (global/local) variable.
17510 * Returns NULL when it doesn't exist.
17511 */
17512 char_u *
17513get_var_value(name)
17514 char_u *name;
17515{
Bram Moolenaar33570922005-01-25 22:26:29 +000017516 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517
Bram Moolenaara7043832005-01-21 11:56:39 +000017518 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 if (v == NULL)
17520 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017521 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522}
17523
17524/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 * sourcing this script and when executing functions defined in the script.
17527 */
17528 void
17529new_script_vars(id)
17530 scid_T id;
17531{
Bram Moolenaara7043832005-01-21 11:56:39 +000017532 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017533 hashtab_T *ht;
17534 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017535
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17537 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017538 /* Re-allocating ga_data means that an ht_array pointing to
17539 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017541 for (i = 1; i <= ga_scripts.ga_len; ++i)
17542 {
17543 ht = &SCRIPT_VARS(i);
17544 if (ht->ht_mask == HT_INIT_SIZE - 1)
17545 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 sv = &SCRIPT_SV(i);
17547 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017548 }
17549
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 while (ga_scripts.ga_len < id)
17551 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017552 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17553 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 }
17556 }
17557}
17558
17559/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17561 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 */
17563 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017564init_var_dict(dict, dict_var)
17565 dict_T *dict;
17566 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567{
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 hash_init(&dict->dv_hashtab);
17569 dict->dv_refcount = 99999;
17570 dict_var->di_tv.vval.v_dict = dict;
17571 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017572 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17574 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575}
17576
17577/*
17578 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017579 * Frees all allocated variables and the value they contain.
17580 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 */
17582 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017583vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 hashtab_T *ht;
17585{
17586 vars_clear_ext(ht, TRUE);
17587}
17588
17589/*
17590 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17591 */
17592 static void
17593vars_clear_ext(ht, free_val)
17594 hashtab_T *ht;
17595 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596{
Bram Moolenaara7043832005-01-21 11:56:39 +000017597 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 hashitem_T *hi;
17599 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600
Bram Moolenaar33570922005-01-25 22:26:29 +000017601 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017602 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017603 for (hi = ht->ht_array; todo > 0; ++hi)
17604 {
17605 if (!HASHITEM_EMPTY(hi))
17606 {
17607 --todo;
17608
Bram Moolenaar33570922005-01-25 22:26:29 +000017609 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017610 * ht_array might change then. hash_clear() takes care of it
17611 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 v = HI2DI(hi);
17613 if (free_val)
17614 clear_tv(&v->di_tv);
17615 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17616 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017617 }
17618 }
17619 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017620 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621}
17622
Bram Moolenaara7043832005-01-21 11:56:39 +000017623/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 * Delete a variable from hashtab "ht" at item "hi".
17625 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017628delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017629 hashtab_T *ht;
17630 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631{
Bram Moolenaar33570922005-01-25 22:26:29 +000017632 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017633
17634 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017635 clear_tv(&di->di_tv);
17636 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637}
17638
17639/*
17640 * List the value of one internal variable.
17641 */
17642 static void
17643list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017644 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 char_u *prefix;
17646{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647 char_u *tofree;
17648 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017649 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017650
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017651 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017653 s == NULL ? (char_u *)"" : s);
17654 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655}
17656
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 static void
17658list_one_var_a(prefix, name, type, string)
17659 char_u *prefix;
17660 char_u *name;
17661 int type;
17662 char_u *string;
17663{
17664 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17665 if (name != NULL) /* "a:" vars don't have a name stored */
17666 msg_puts(name);
17667 msg_putchar(' ');
17668 msg_advance(22);
17669 if (type == VAR_NUMBER)
17670 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671 else if (type == VAR_FUNC)
17672 msg_putchar('*');
17673 else if (type == VAR_LIST)
17674 {
17675 msg_putchar('[');
17676 if (*string == '[')
17677 ++string;
17678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017679 else if (type == VAR_DICT)
17680 {
17681 msg_putchar('{');
17682 if (*string == '{')
17683 ++string;
17684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 else
17686 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017687
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017689
17690 if (type == VAR_FUNC)
17691 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692}
17693
17694/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 * If the variable already exists, the value is updated.
17697 * Otherwise the variable is created.
17698 */
17699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017702 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017703 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704{
Bram Moolenaar33570922005-01-25 22:26:29 +000017705 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017708 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017711 {
17712 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17713 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17714 ? name[2] : name[0]))
17715 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017716 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017717 return;
17718 }
17719 if (function_exists(name))
17720 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017721 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017722 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017723 return;
17724 }
17725 }
17726
Bram Moolenaara7043832005-01-21 11:56:39 +000017727 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017728 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017729 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017730 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017731 return;
17732 }
17733
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017734 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017738 if (var_check_ro(v->di_flags, name)
17739 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 return;
17741 if (v->di_tv.v_type != tv->v_type
17742 && !((v->di_tv.v_type == VAR_STRING
17743 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017744 && (tv->v_type == VAR_STRING
17745 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017747 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017748 return;
17749 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017750
17751 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017752 * Handle setting internal v: variables separately: we don't change
17753 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 */
17755 if (ht == &vimvarht)
17756 {
17757 if (v->di_tv.v_type == VAR_STRING)
17758 {
17759 vim_free(v->di_tv.vval.v_string);
17760 if (copy || tv->v_type != VAR_STRING)
17761 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17762 else
17763 {
17764 /* Take over the string to avoid an extra alloc/free. */
17765 v->di_tv.vval.v_string = tv->vval.v_string;
17766 tv->vval.v_string = NULL;
17767 }
17768 }
17769 else if (v->di_tv.v_type != VAR_NUMBER)
17770 EMSG2(_(e_intern2), "set_var()");
17771 else
17772 v->di_tv.vval.v_number = get_tv_number(tv);
17773 return;
17774 }
17775
17776 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 }
17778 else /* add a new variable */
17779 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017780 /* Can't add "v:" variable. */
17781 if (ht == &vimvarht)
17782 {
17783 EMSG2(_(e_illvar), name);
17784 return;
17785 }
17786
Bram Moolenaar92124a32005-06-17 22:03:40 +000017787 /* Make sure the variable name is valid. */
17788 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017789 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17790 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017791 {
17792 EMSG2(_(e_illvar), varname);
17793 return;
17794 }
17795
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017796 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17797 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017798 if (v == NULL)
17799 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017803 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 return;
17805 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017806 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017811 else
17812 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017813 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017814 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017815 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817}
17818
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017820 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17821 * Also give an error message.
17822 */
17823 static int
17824var_check_ro(flags, name)
17825 int flags;
17826 char_u *name;
17827{
17828 if (flags & DI_FLAGS_RO)
17829 {
17830 EMSG2(_(e_readonlyvar), name);
17831 return TRUE;
17832 }
17833 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17834 {
17835 EMSG2(_(e_readonlysbx), name);
17836 return TRUE;
17837 }
17838 return FALSE;
17839}
17840
17841/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017842 * Return TRUE if typeval "tv" is set to be locked (immutable).
17843 * Also give an error message, using "name".
17844 */
17845 static int
17846tv_check_lock(lock, name)
17847 int lock;
17848 char_u *name;
17849{
17850 if (lock & VAR_LOCKED)
17851 {
17852 EMSG2(_("E741: Value is locked: %s"),
17853 name == NULL ? (char_u *)_("Unknown") : name);
17854 return TRUE;
17855 }
17856 if (lock & VAR_FIXED)
17857 {
17858 EMSG2(_("E742: Cannot change value of %s"),
17859 name == NULL ? (char_u *)_("Unknown") : name);
17860 return TRUE;
17861 }
17862 return FALSE;
17863}
17864
17865/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017866 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017867 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017868 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017871copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017872 typval_T *from;
17873 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017875 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017876 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017877 switch (from->v_type)
17878 {
17879 case VAR_NUMBER:
17880 to->vval.v_number = from->vval.v_number;
17881 break;
17882 case VAR_STRING:
17883 case VAR_FUNC:
17884 if (from->vval.v_string == NULL)
17885 to->vval.v_string = NULL;
17886 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017887 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017888 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017889 if (from->v_type == VAR_FUNC)
17890 func_ref(to->vval.v_string);
17891 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017892 break;
17893 case VAR_LIST:
17894 if (from->vval.v_list == NULL)
17895 to->vval.v_list = NULL;
17896 else
17897 {
17898 to->vval.v_list = from->vval.v_list;
17899 ++to->vval.v_list->lv_refcount;
17900 }
17901 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017902 case VAR_DICT:
17903 if (from->vval.v_dict == NULL)
17904 to->vval.v_dict = NULL;
17905 else
17906 {
17907 to->vval.v_dict = from->vval.v_dict;
17908 ++to->vval.v_dict->dv_refcount;
17909 }
17910 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017911 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017912 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017913 break;
17914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915}
17916
17917/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017918 * Make a copy of an item.
17919 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017920 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17921 * reference to an already copied list/dict can be used.
17922 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017923 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017924 static int
17925item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 typval_T *from;
17927 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017928 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017929 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017930{
17931 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017932 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017933
Bram Moolenaar33570922005-01-25 22:26:29 +000017934 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017935 {
17936 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017937 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017938 }
17939 ++recurse;
17940
17941 switch (from->v_type)
17942 {
17943 case VAR_NUMBER:
17944 case VAR_STRING:
17945 case VAR_FUNC:
17946 copy_tv(from, to);
17947 break;
17948 case VAR_LIST:
17949 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017950 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017951 if (from->vval.v_list == NULL)
17952 to->vval.v_list = NULL;
17953 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17954 {
17955 /* use the copy made earlier */
17956 to->vval.v_list = from->vval.v_list->lv_copylist;
17957 ++to->vval.v_list->lv_refcount;
17958 }
17959 else
17960 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17961 if (to->vval.v_list == NULL)
17962 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017963 break;
17964 case VAR_DICT:
17965 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017966 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017967 if (from->vval.v_dict == NULL)
17968 to->vval.v_dict = NULL;
17969 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17970 {
17971 /* use the copy made earlier */
17972 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17973 ++to->vval.v_dict->dv_refcount;
17974 }
17975 else
17976 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17977 if (to->vval.v_dict == NULL)
17978 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017979 break;
17980 default:
17981 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017982 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017983 }
17984 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017985 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017986}
17987
17988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 * ":echo expr1 ..." print each argument separated with a space, add a
17990 * newline at the end.
17991 * ":echon expr1 ..." print each argument plain.
17992 */
17993 void
17994ex_echo(eap)
17995 exarg_T *eap;
17996{
17997 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017999 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 char_u *p;
18001 int needclr = TRUE;
18002 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018003 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004
18005 if (eap->skip)
18006 ++emsg_skip;
18007 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18008 {
18009 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018010 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 {
18012 /*
18013 * Report the invalid expression unless the expression evaluation
18014 * has been cancelled due to an aborting error, an interrupt, or an
18015 * exception.
18016 */
18017 if (!aborting())
18018 EMSG2(_(e_invexpr2), p);
18019 break;
18020 }
18021 if (!eap->skip)
18022 {
18023 if (atstart)
18024 {
18025 atstart = FALSE;
18026 /* Call msg_start() after eval1(), evaluating the expression
18027 * may cause a message to appear. */
18028 if (eap->cmdidx == CMD_echo)
18029 msg_start();
18030 }
18031 else if (eap->cmdidx == CMD_echo)
18032 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018033 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018034 if (p != NULL)
18035 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018037 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018039 if (*p != TAB && needclr)
18040 {
18041 /* remove any text still there from the command */
18042 msg_clr_eos();
18043 needclr = FALSE;
18044 }
18045 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 }
18047 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018048 {
18049#ifdef FEAT_MBYTE
18050 if (has_mbyte)
18051 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018052 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018053
18054 (void)msg_outtrans_len_attr(p, i, echo_attr);
18055 p += i - 1;
18056 }
18057 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018059 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018062 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018064 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 arg = skipwhite(arg);
18066 }
18067 eap->nextcmd = check_nextcmd(arg);
18068
18069 if (eap->skip)
18070 --emsg_skip;
18071 else
18072 {
18073 /* remove text that may still be there from the command */
18074 if (needclr)
18075 msg_clr_eos();
18076 if (eap->cmdidx == CMD_echo)
18077 msg_end();
18078 }
18079}
18080
18081/*
18082 * ":echohl {name}".
18083 */
18084 void
18085ex_echohl(eap)
18086 exarg_T *eap;
18087{
18088 int id;
18089
18090 id = syn_name2id(eap->arg);
18091 if (id == 0)
18092 echo_attr = 0;
18093 else
18094 echo_attr = syn_id2attr(id);
18095}
18096
18097/*
18098 * ":execute expr1 ..." execute the result of an expression.
18099 * ":echomsg expr1 ..." Print a message
18100 * ":echoerr expr1 ..." Print an error
18101 * Each gets spaces around each argument and a newline at the end for
18102 * echo commands
18103 */
18104 void
18105ex_execute(eap)
18106 exarg_T *eap;
18107{
18108 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018109 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 int ret = OK;
18111 char_u *p;
18112 garray_T ga;
18113 int len;
18114 int save_did_emsg;
18115
18116 ga_init2(&ga, 1, 80);
18117
18118 if (eap->skip)
18119 ++emsg_skip;
18120 while (*arg != NUL && *arg != '|' && *arg != '\n')
18121 {
18122 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 {
18125 /*
18126 * Report the invalid expression unless the expression evaluation
18127 * has been cancelled due to an aborting error, an interrupt, or an
18128 * exception.
18129 */
18130 if (!aborting())
18131 EMSG2(_(e_invexpr2), p);
18132 ret = FAIL;
18133 break;
18134 }
18135
18136 if (!eap->skip)
18137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 len = (int)STRLEN(p);
18140 if (ga_grow(&ga, len + 2) == FAIL)
18141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018142 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 ret = FAIL;
18144 break;
18145 }
18146 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 ga.ga_len += len;
18150 }
18151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018152 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 arg = skipwhite(arg);
18154 }
18155
18156 if (ret != FAIL && ga.ga_data != NULL)
18157 {
18158 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018161 out_flush();
18162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 else if (eap->cmdidx == CMD_echoerr)
18164 {
18165 /* We don't want to abort following commands, restore did_emsg. */
18166 save_did_emsg = did_emsg;
18167 EMSG((char_u *)ga.ga_data);
18168 if (!force_abort)
18169 did_emsg = save_did_emsg;
18170 }
18171 else if (eap->cmdidx == CMD_execute)
18172 do_cmdline((char_u *)ga.ga_data,
18173 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18174 }
18175
18176 ga_clear(&ga);
18177
18178 if (eap->skip)
18179 --emsg_skip;
18180
18181 eap->nextcmd = check_nextcmd(arg);
18182}
18183
18184/*
18185 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18186 * "arg" points to the "&" or '+' when called, to "option" when returning.
18187 * Returns NULL when no option name found. Otherwise pointer to the char
18188 * after the option name.
18189 */
18190 static char_u *
18191find_option_end(arg, opt_flags)
18192 char_u **arg;
18193 int *opt_flags;
18194{
18195 char_u *p = *arg;
18196
18197 ++p;
18198 if (*p == 'g' && p[1] == ':')
18199 {
18200 *opt_flags = OPT_GLOBAL;
18201 p += 2;
18202 }
18203 else if (*p == 'l' && p[1] == ':')
18204 {
18205 *opt_flags = OPT_LOCAL;
18206 p += 2;
18207 }
18208 else
18209 *opt_flags = 0;
18210
18211 if (!ASCII_ISALPHA(*p))
18212 return NULL;
18213 *arg = p;
18214
18215 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18216 p += 4; /* termcap option */
18217 else
18218 while (ASCII_ISALPHA(*p))
18219 ++p;
18220 return p;
18221}
18222
18223/*
18224 * ":function"
18225 */
18226 void
18227ex_function(eap)
18228 exarg_T *eap;
18229{
18230 char_u *theline;
18231 int j;
18232 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 char_u *name = NULL;
18235 char_u *p;
18236 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018237 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 garray_T newargs;
18239 garray_T newlines;
18240 int varargs = FALSE;
18241 int mustend = FALSE;
18242 int flags = 0;
18243 ufunc_T *fp;
18244 int indent;
18245 int nesting;
18246 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018247 dictitem_T *v;
18248 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018249 static int func_nr = 0; /* number for nameless function */
18250 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018251 hashtab_T *ht;
18252 int todo;
18253 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018254 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255
18256 /*
18257 * ":function" without argument: list functions.
18258 */
18259 if (ends_excmd(*eap->arg))
18260 {
18261 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018262 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018263 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018264 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018265 {
18266 if (!HASHITEM_EMPTY(hi))
18267 {
18268 --todo;
18269 fp = HI2UF(hi);
18270 if (!isdigit(*fp->uf_name))
18271 list_func_head(fp, FALSE);
18272 }
18273 }
18274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 eap->nextcmd = check_nextcmd(eap->arg);
18276 return;
18277 }
18278
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018279 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018280 * ":function /pat": list functions matching pattern.
18281 */
18282 if (*eap->arg == '/')
18283 {
18284 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18285 if (!eap->skip)
18286 {
18287 regmatch_T regmatch;
18288
18289 c = *p;
18290 *p = NUL;
18291 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18292 *p = c;
18293 if (regmatch.regprog != NULL)
18294 {
18295 regmatch.rm_ic = p_ic;
18296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018297 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018298 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18299 {
18300 if (!HASHITEM_EMPTY(hi))
18301 {
18302 --todo;
18303 fp = HI2UF(hi);
18304 if (!isdigit(*fp->uf_name)
18305 && vim_regexec(&regmatch, fp->uf_name, 0))
18306 list_func_head(fp, FALSE);
18307 }
18308 }
18309 }
18310 }
18311 if (*p == '/')
18312 ++p;
18313 eap->nextcmd = check_nextcmd(p);
18314 return;
18315 }
18316
18317 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018318 * Get the function name. There are these situations:
18319 * func normal function name
18320 * "name" == func, "fudi.fd_dict" == NULL
18321 * dict.func new dictionary entry
18322 * "name" == NULL, "fudi.fd_dict" set,
18323 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18324 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018325 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018326 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18327 * dict.func existing dict entry that's not a Funcref
18328 * "name" == NULL, "fudi.fd_dict" set,
18329 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018332 name = trans_function_name(&p, eap->skip, 0, &fudi);
18333 paren = (vim_strchr(p, '(') != NULL);
18334 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 {
18336 /*
18337 * Return on an invalid expression in braces, unless the expression
18338 * evaluation has been cancelled due to an aborting error, an
18339 * interrupt, or an exception.
18340 */
18341 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018343 if (!eap->skip && fudi.fd_newkey != NULL)
18344 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018345 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 else
18349 eap->skip = TRUE;
18350 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018351
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 /* An error in a function call during evaluation of an expression in magic
18353 * braces should not cause the function not to be defined. */
18354 saved_did_emsg = did_emsg;
18355 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356
18357 /*
18358 * ":function func" with only function name: list function.
18359 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018360 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 {
18362 if (!ends_excmd(*skipwhite(p)))
18363 {
18364 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018365 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 }
18367 eap->nextcmd = check_nextcmd(p);
18368 if (eap->nextcmd != NULL)
18369 *p = NUL;
18370 if (!eap->skip && !got_int)
18371 {
18372 fp = find_func(name);
18373 if (fp != NULL)
18374 {
18375 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018376 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018378 if (FUNCLINE(fp, j) == NULL)
18379 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 msg_putchar('\n');
18381 msg_outnum((long)(j + 1));
18382 if (j < 9)
18383 msg_putchar(' ');
18384 if (j < 99)
18385 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018386 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 out_flush(); /* show a line at a time */
18388 ui_breakcheck();
18389 }
18390 if (!got_int)
18391 {
18392 msg_putchar('\n');
18393 msg_puts((char_u *)" endfunction");
18394 }
18395 }
18396 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018397 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018399 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 }
18401
18402 /*
18403 * ":function name(arg1, arg2)" Define function.
18404 */
18405 p = skipwhite(p);
18406 if (*p != '(')
18407 {
18408 if (!eap->skip)
18409 {
18410 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018411 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 }
18413 /* attempt to continue by skipping some text */
18414 if (vim_strchr(p, '(') != NULL)
18415 p = vim_strchr(p, '(');
18416 }
18417 p = skipwhite(p + 1);
18418
18419 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18420 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18421
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018422 if (!eap->skip)
18423 {
18424 /* Check the name of the function. */
18425 if (name != NULL)
18426 arg = name;
18427 else
18428 arg = fudi.fd_newkey;
18429 if (arg != NULL)
18430 {
18431 if (*arg == K_SPECIAL)
18432 j = 3;
18433 else
18434 j = 0;
18435 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18436 : eval_isnamec(arg[j])))
18437 ++j;
18438 if (arg[j] != NUL)
18439 emsg_funcname(_(e_invarg2), arg);
18440 }
18441 }
18442
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443 /*
18444 * Isolate the arguments: "arg1, arg2, ...)"
18445 */
18446 while (*p != ')')
18447 {
18448 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18449 {
18450 varargs = TRUE;
18451 p += 3;
18452 mustend = TRUE;
18453 }
18454 else
18455 {
18456 arg = p;
18457 while (ASCII_ISALNUM(*p) || *p == '_')
18458 ++p;
18459 if (arg == p || isdigit(*arg)
18460 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18461 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18462 {
18463 if (!eap->skip)
18464 EMSG2(_("E125: Illegal argument: %s"), arg);
18465 break;
18466 }
18467 if (ga_grow(&newargs, 1) == FAIL)
18468 goto erret;
18469 c = *p;
18470 *p = NUL;
18471 arg = vim_strsave(arg);
18472 if (arg == NULL)
18473 goto erret;
18474 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18475 *p = c;
18476 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 if (*p == ',')
18478 ++p;
18479 else
18480 mustend = TRUE;
18481 }
18482 p = skipwhite(p);
18483 if (mustend && *p != ')')
18484 {
18485 if (!eap->skip)
18486 EMSG2(_(e_invarg2), eap->arg);
18487 break;
18488 }
18489 }
18490 ++p; /* skip the ')' */
18491
Bram Moolenaare9a41262005-01-15 22:18:47 +000018492 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 for (;;)
18494 {
18495 p = skipwhite(p);
18496 if (STRNCMP(p, "range", 5) == 0)
18497 {
18498 flags |= FC_RANGE;
18499 p += 5;
18500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018501 else if (STRNCMP(p, "dict", 4) == 0)
18502 {
18503 flags |= FC_DICT;
18504 p += 4;
18505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 else if (STRNCMP(p, "abort", 5) == 0)
18507 {
18508 flags |= FC_ABORT;
18509 p += 5;
18510 }
18511 else
18512 break;
18513 }
18514
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018515 /* When there is a line break use what follows for the function body.
18516 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18517 if (*p == '\n')
18518 line_arg = p + 1;
18519 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 EMSG(_(e_trailing));
18521
18522 /*
18523 * Read the body of the function, until ":endfunction" is found.
18524 */
18525 if (KeyTyped)
18526 {
18527 /* Check if the function already exists, don't let the user type the
18528 * whole function before telling him it doesn't work! For a script we
18529 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018530 if (!eap->skip && !eap->forceit)
18531 {
18532 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18533 EMSG(_(e_funcdict));
18534 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018535 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018538 if (!eap->skip && did_emsg)
18539 goto erret;
18540
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 msg_putchar('\n'); /* don't overwrite the function name */
18542 cmdline_row = msg_row;
18543 }
18544
18545 indent = 2;
18546 nesting = 0;
18547 for (;;)
18548 {
18549 msg_scroll = TRUE;
18550 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018551 sourcing_lnum_off = sourcing_lnum;
18552
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018553 if (line_arg != NULL)
18554 {
18555 /* Use eap->arg, split up in parts by line breaks. */
18556 theline = line_arg;
18557 p = vim_strchr(theline, '\n');
18558 if (p == NULL)
18559 line_arg += STRLEN(line_arg);
18560 else
18561 {
18562 *p = NUL;
18563 line_arg = p + 1;
18564 }
18565 }
18566 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 theline = getcmdline(':', 0L, indent);
18568 else
18569 theline = eap->getline(':', eap->cookie, indent);
18570 if (KeyTyped)
18571 lines_left = Rows - 1;
18572 if (theline == NULL)
18573 {
18574 EMSG(_("E126: Missing :endfunction"));
18575 goto erret;
18576 }
18577
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018578 /* Detect line continuation: sourcing_lnum increased more than one. */
18579 if (sourcing_lnum > sourcing_lnum_off + 1)
18580 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18581 else
18582 sourcing_lnum_off = 0;
18583
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 if (skip_until != NULL)
18585 {
18586 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18587 * don't check for ":endfunc". */
18588 if (STRCMP(theline, skip_until) == 0)
18589 {
18590 vim_free(skip_until);
18591 skip_until = NULL;
18592 }
18593 }
18594 else
18595 {
18596 /* skip ':' and blanks*/
18597 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18598 ;
18599
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018600 /* Check for "endfunction". */
18601 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018603 if (line_arg == NULL)
18604 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 break;
18606 }
18607
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018608 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 * at "end". */
18610 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18611 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018612 else if (STRNCMP(p, "if", 2) == 0
18613 || STRNCMP(p, "wh", 2) == 0
18614 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 || STRNCMP(p, "try", 3) == 0)
18616 indent += 2;
18617
18618 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018619 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018621 if (*p == '!')
18622 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 p += eval_fname_script(p);
18624 if (ASCII_ISALPHA(*p))
18625 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018626 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 if (*skipwhite(p) == '(')
18628 {
18629 ++nesting;
18630 indent += 2;
18631 }
18632 }
18633 }
18634
18635 /* Check for ":append" or ":insert". */
18636 p = skip_range(p, NULL);
18637 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18638 || (p[0] == 'i'
18639 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18640 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18641 skip_until = vim_strsave((char_u *)".");
18642
18643 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18644 arg = skipwhite(skiptowhite(p));
18645 if (arg[0] == '<' && arg[1] =='<'
18646 && ((p[0] == 'p' && p[1] == 'y'
18647 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18648 || (p[0] == 'p' && p[1] == 'e'
18649 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18650 || (p[0] == 't' && p[1] == 'c'
18651 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18652 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18653 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018654 || (p[0] == 'm' && p[1] == 'z'
18655 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 ))
18657 {
18658 /* ":python <<" continues until a dot, like ":append" */
18659 p = skipwhite(arg + 2);
18660 if (*p == NUL)
18661 skip_until = vim_strsave((char_u *)".");
18662 else
18663 skip_until = vim_strsave(p);
18664 }
18665 }
18666
18667 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018668 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018669 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018670 if (line_arg == NULL)
18671 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018673 }
18674
18675 /* Copy the line to newly allocated memory. get_one_sourceline()
18676 * allocates 250 bytes per line, this saves 80% on average. The cost
18677 * is an extra alloc/free. */
18678 p = vim_strsave(theline);
18679 if (p != NULL)
18680 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018681 if (line_arg == NULL)
18682 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018683 theline = p;
18684 }
18685
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018686 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18687
18688 /* Add NULL lines for continuation lines, so that the line count is
18689 * equal to the index in the growarray. */
18690 while (sourcing_lnum_off-- > 0)
18691 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018692
18693 /* Check for end of eap->arg. */
18694 if (line_arg != NULL && *line_arg == NUL)
18695 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 }
18697
18698 /* Don't define the function when skipping commands or when an error was
18699 * detected. */
18700 if (eap->skip || did_emsg)
18701 goto erret;
18702
18703 /*
18704 * If there are no errors, add the function
18705 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018706 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018707 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018708 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018709 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018710 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018711 emsg_funcname("E707: Function name conflicts with variable: %s",
18712 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018713 goto erret;
18714 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018715
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018716 fp = find_func(name);
18717 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018719 if (!eap->forceit)
18720 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018721 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018722 goto erret;
18723 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018724 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018725 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018726 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018727 name);
18728 goto erret;
18729 }
18730 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018731 ga_clear_strings(&(fp->uf_args));
18732 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018733 vim_free(name);
18734 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 }
18737 else
18738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018739 char numbuf[20];
18740
18741 fp = NULL;
18742 if (fudi.fd_newkey == NULL && !eap->forceit)
18743 {
18744 EMSG(_(e_funcdict));
18745 goto erret;
18746 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018747 if (fudi.fd_di == NULL)
18748 {
18749 /* Can't add a function to a locked dictionary */
18750 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18751 goto erret;
18752 }
18753 /* Can't change an existing function if it is locked */
18754 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18755 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018756
18757 /* Give the function a sequential number. Can only be used with a
18758 * Funcref! */
18759 vim_free(name);
18760 sprintf(numbuf, "%d", ++func_nr);
18761 name = vim_strsave((char_u *)numbuf);
18762 if (name == NULL)
18763 goto erret;
18764 }
18765
18766 if (fp == NULL)
18767 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018768 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018769 {
18770 int slen, plen;
18771 char_u *scriptname;
18772
18773 /* Check that the autoload name matches the script name. */
18774 j = FAIL;
18775 if (sourcing_name != NULL)
18776 {
18777 scriptname = autoload_name(name);
18778 if (scriptname != NULL)
18779 {
18780 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018781 plen = (int)STRLEN(p);
18782 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018783 if (slen > plen && fnamecmp(p,
18784 sourcing_name + slen - plen) == 0)
18785 j = OK;
18786 vim_free(scriptname);
18787 }
18788 }
18789 if (j == FAIL)
18790 {
18791 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18792 goto erret;
18793 }
18794 }
18795
18796 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 if (fp == NULL)
18798 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018799
18800 if (fudi.fd_dict != NULL)
18801 {
18802 if (fudi.fd_di == NULL)
18803 {
18804 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018805 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018806 if (fudi.fd_di == NULL)
18807 {
18808 vim_free(fp);
18809 goto erret;
18810 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018811 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18812 {
18813 vim_free(fudi.fd_di);
18814 goto erret;
18815 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018816 }
18817 else
18818 /* overwrite existing dict entry */
18819 clear_tv(&fudi.fd_di->di_tv);
18820 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018821 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018822 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018823 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018824
18825 /* behave like "dict" was used */
18826 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018827 }
18828
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018830 STRCPY(fp->uf_name, name);
18831 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018833 fp->uf_args = newargs;
18834 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018835#ifdef FEAT_PROFILE
18836 fp->uf_tml_count = NULL;
18837 fp->uf_tml_total = NULL;
18838 fp->uf_tml_self = NULL;
18839 fp->uf_profiling = FALSE;
18840 if (prof_def_func())
18841 func_do_profile(fp);
18842#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018843 fp->uf_varargs = varargs;
18844 fp->uf_flags = flags;
18845 fp->uf_calls = 0;
18846 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018847 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848
18849erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 ga_clear_strings(&newargs);
18851 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018852ret_free:
18853 vim_free(skip_until);
18854 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857}
18858
18859/*
18860 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018861 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018863 * flags:
18864 * TFN_INT: internal function name OK
18865 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 * Advances "pp" to just after the function name (if no error).
18867 */
18868 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018869trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 char_u **pp;
18871 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018872 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018875 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 char_u *start;
18877 char_u *end;
18878 int lead;
18879 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018882
18883 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018886
18887 /* Check for hard coded <SNR>: already translated function ID (from a user
18888 * command). */
18889 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18890 && (*pp)[2] == (int)KE_SNR)
18891 {
18892 *pp += 3;
18893 len = get_id_len(pp) + 3;
18894 return vim_strnsave(start, len);
18895 }
18896
18897 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18898 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018900 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018902
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018903 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18904 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018905 if (end == start)
18906 {
18907 if (!skip)
18908 EMSG(_("E129: Function name required"));
18909 goto theend;
18910 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018911 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018912 {
18913 /*
18914 * Report an invalid expression in braces, unless the expression
18915 * evaluation has been cancelled due to an aborting error, an
18916 * interrupt, or an exception.
18917 */
18918 if (!aborting())
18919 {
18920 if (end != NULL)
18921 EMSG2(_(e_invarg2), start);
18922 }
18923 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018924 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018925 goto theend;
18926 }
18927
18928 if (lv.ll_tv != NULL)
18929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018930 if (fdp != NULL)
18931 {
18932 fdp->fd_dict = lv.ll_dict;
18933 fdp->fd_newkey = lv.ll_newkey;
18934 lv.ll_newkey = NULL;
18935 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018937 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18938 {
18939 name = vim_strsave(lv.ll_tv->vval.v_string);
18940 *pp = end;
18941 }
18942 else
18943 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018944 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18945 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018946 EMSG(_(e_funcref));
18947 else
18948 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018949 name = NULL;
18950 }
18951 goto theend;
18952 }
18953
18954 if (lv.ll_name == NULL)
18955 {
18956 /* Error found, but continue after the function name. */
18957 *pp = end;
18958 goto theend;
18959 }
18960
18961 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018962 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018963 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018964 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18965 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18966 {
18967 /* When there was "s:" already or the name expanded to get a
18968 * leading "s:" then remove it. */
18969 lv.ll_name += 2;
18970 len -= 2;
18971 lead = 2;
18972 }
18973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018974 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018975 {
18976 if (lead == 2) /* skip over "s:" */
18977 lv.ll_name += 2;
18978 len = (int)(end - lv.ll_name);
18979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018980
18981 /*
18982 * Copy the function name to allocated memory.
18983 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18984 * Accept <SNR>123_name() outside a script.
18985 */
18986 if (skip)
18987 lead = 0; /* do nothing */
18988 else if (lead > 0)
18989 {
18990 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000018991 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
18992 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018993 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018994 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018995 if (current_SID <= 0)
18996 {
18997 EMSG(_(e_usingsid));
18998 goto theend;
18999 }
19000 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19001 lead += (int)STRLEN(sid_buf);
19002 }
19003 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019004 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019005 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019006 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019007 goto theend;
19008 }
19009 name = alloc((unsigned)(len + lead + 1));
19010 if (name != NULL)
19011 {
19012 if (lead > 0)
19013 {
19014 name[0] = K_SPECIAL;
19015 name[1] = KS_EXTRA;
19016 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019017 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019018 STRCPY(name + 3, sid_buf);
19019 }
19020 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19021 name[len + lead] = NUL;
19022 }
19023 *pp = end;
19024
19025theend:
19026 clear_lval(&lv);
19027 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028}
19029
19030/*
19031 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19032 * Return 2 if "p" starts with "s:".
19033 * Return 0 otherwise.
19034 */
19035 static int
19036eval_fname_script(p)
19037 char_u *p;
19038{
19039 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19040 || STRNICMP(p + 1, "SNR>", 4) == 0))
19041 return 5;
19042 if (p[0] == 's' && p[1] == ':')
19043 return 2;
19044 return 0;
19045}
19046
19047/*
19048 * Return TRUE if "p" starts with "<SID>" or "s:".
19049 * Only works if eval_fname_script() returned non-zero for "p"!
19050 */
19051 static int
19052eval_fname_sid(p)
19053 char_u *p;
19054{
19055 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19056}
19057
19058/*
19059 * List the head of the function: "name(arg1, arg2)".
19060 */
19061 static void
19062list_func_head(fp, indent)
19063 ufunc_T *fp;
19064 int indent;
19065{
19066 int j;
19067
19068 msg_start();
19069 if (indent)
19070 MSG_PUTS(" ");
19071 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019072 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 {
19074 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019075 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 }
19077 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019078 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019080 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 {
19082 if (j)
19083 MSG_PUTS(", ");
19084 msg_puts(FUNCARG(fp, j));
19085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019086 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 {
19088 if (j)
19089 MSG_PUTS(", ");
19090 MSG_PUTS("...");
19091 }
19092 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019093 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019094 if (p_verbose > 0)
19095 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096}
19097
19098/*
19099 * Find a function by name, return pointer to it in ufuncs.
19100 * Return NULL for unknown function.
19101 */
19102 static ufunc_T *
19103find_func(name)
19104 char_u *name;
19105{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019106 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019108 hi = hash_find(&func_hashtab, name);
19109 if (!HASHITEM_EMPTY(hi))
19110 return HI2UF(hi);
19111 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112}
19113
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019114#if defined(EXITFREE) || defined(PROTO)
19115 void
19116free_all_functions()
19117{
19118 hashitem_T *hi;
19119
19120 /* Need to start all over every time, because func_free() may change the
19121 * hash table. */
19122 while (func_hashtab.ht_used > 0)
19123 for (hi = func_hashtab.ht_array; ; ++hi)
19124 if (!HASHITEM_EMPTY(hi))
19125 {
19126 func_free(HI2UF(hi));
19127 break;
19128 }
19129}
19130#endif
19131
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132/*
19133 * Return TRUE if a function "name" exists.
19134 */
19135 static int
19136function_exists(name)
19137 char_u *name;
19138{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019139 char_u *nm = name;
19140 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141 int n = FALSE;
19142
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019143 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019144 nm = skipwhite(nm);
19145
19146 /* Only accept "funcname", "funcname ", "funcname (..." and
19147 * "funcname(...", not "funcname!...". */
19148 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019149 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019150 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019151 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019152 else
19153 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019154 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019155 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019156 return n;
19157}
19158
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019159/*
19160 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019161 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019162 */
19163 static int
19164builtin_function(name)
19165 char_u *name;
19166{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019167 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19168 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019169}
19170
Bram Moolenaar05159a02005-02-26 23:04:13 +000019171#if defined(FEAT_PROFILE) || defined(PROTO)
19172/*
19173 * Start profiling function "fp".
19174 */
19175 static void
19176func_do_profile(fp)
19177 ufunc_T *fp;
19178{
19179 fp->uf_tm_count = 0;
19180 profile_zero(&fp->uf_tm_self);
19181 profile_zero(&fp->uf_tm_total);
19182 if (fp->uf_tml_count == NULL)
19183 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19184 (sizeof(int) * fp->uf_lines.ga_len));
19185 if (fp->uf_tml_total == NULL)
19186 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19187 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19188 if (fp->uf_tml_self == NULL)
19189 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19190 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19191 fp->uf_tml_idx = -1;
19192 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19193 || fp->uf_tml_self == NULL)
19194 return; /* out of memory */
19195
19196 fp->uf_profiling = TRUE;
19197}
19198
19199/*
19200 * Dump the profiling results for all functions in file "fd".
19201 */
19202 void
19203func_dump_profile(fd)
19204 FILE *fd;
19205{
19206 hashitem_T *hi;
19207 int todo;
19208 ufunc_T *fp;
19209 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019210 ufunc_T **sorttab;
19211 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019213 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019214 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19215
Bram Moolenaar05159a02005-02-26 23:04:13 +000019216 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19217 {
19218 if (!HASHITEM_EMPTY(hi))
19219 {
19220 --todo;
19221 fp = HI2UF(hi);
19222 if (fp->uf_profiling)
19223 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019224 if (sorttab != NULL)
19225 sorttab[st_len++] = fp;
19226
Bram Moolenaar05159a02005-02-26 23:04:13 +000019227 if (fp->uf_name[0] == K_SPECIAL)
19228 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19229 else
19230 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19231 if (fp->uf_tm_count == 1)
19232 fprintf(fd, "Called 1 time\n");
19233 else
19234 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19235 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19236 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19237 fprintf(fd, "\n");
19238 fprintf(fd, "count total (s) self (s)\n");
19239
19240 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19241 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019242 if (FUNCLINE(fp, i) == NULL)
19243 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019244 prof_func_line(fd, fp->uf_tml_count[i],
19245 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019246 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19247 }
19248 fprintf(fd, "\n");
19249 }
19250 }
19251 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019252
19253 if (sorttab != NULL && st_len > 0)
19254 {
19255 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19256 prof_total_cmp);
19257 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19258 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19259 prof_self_cmp);
19260 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19261 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019262}
Bram Moolenaar73830342005-02-28 22:48:19 +000019263
19264 static void
19265prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19266 FILE *fd;
19267 ufunc_T **sorttab;
19268 int st_len;
19269 char *title;
19270 int prefer_self; /* when equal print only self time */
19271{
19272 int i;
19273 ufunc_T *fp;
19274
19275 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19276 fprintf(fd, "count total (s) self (s) function\n");
19277 for (i = 0; i < 20 && i < st_len; ++i)
19278 {
19279 fp = sorttab[i];
19280 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19281 prefer_self);
19282 if (fp->uf_name[0] == K_SPECIAL)
19283 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19284 else
19285 fprintf(fd, " %s()\n", fp->uf_name);
19286 }
19287 fprintf(fd, "\n");
19288}
19289
19290/*
19291 * Print the count and times for one function or function line.
19292 */
19293 static void
19294prof_func_line(fd, count, total, self, prefer_self)
19295 FILE *fd;
19296 int count;
19297 proftime_T *total;
19298 proftime_T *self;
19299 int prefer_self; /* when equal print only self time */
19300{
19301 if (count > 0)
19302 {
19303 fprintf(fd, "%5d ", count);
19304 if (prefer_self && profile_equal(total, self))
19305 fprintf(fd, " ");
19306 else
19307 fprintf(fd, "%s ", profile_msg(total));
19308 if (!prefer_self && profile_equal(total, self))
19309 fprintf(fd, " ");
19310 else
19311 fprintf(fd, "%s ", profile_msg(self));
19312 }
19313 else
19314 fprintf(fd, " ");
19315}
19316
19317/*
19318 * Compare function for total time sorting.
19319 */
19320 static int
19321#ifdef __BORLANDC__
19322_RTLENTRYF
19323#endif
19324prof_total_cmp(s1, s2)
19325 const void *s1;
19326 const void *s2;
19327{
19328 ufunc_T *p1, *p2;
19329
19330 p1 = *(ufunc_T **)s1;
19331 p2 = *(ufunc_T **)s2;
19332 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19333}
19334
19335/*
19336 * Compare function for self time sorting.
19337 */
19338 static int
19339#ifdef __BORLANDC__
19340_RTLENTRYF
19341#endif
19342prof_self_cmp(s1, s2)
19343 const void *s1;
19344 const void *s2;
19345{
19346 ufunc_T *p1, *p2;
19347
19348 p1 = *(ufunc_T **)s1;
19349 p2 = *(ufunc_T **)s2;
19350 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19351}
19352
Bram Moolenaar05159a02005-02-26 23:04:13 +000019353#endif
19354
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019355/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019356 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019357 * Return TRUE if a package was loaded.
19358 */
19359 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019360script_autoload(name, reload)
19361 char_u *name;
19362 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019363{
19364 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019365 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019366 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019367 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019368
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019369 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019370 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019371 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019372 return FALSE;
19373
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019374 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019375
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019376 /* Find the name in the list of previously loaded package names. Skip
19377 * "autoload/", it's always the same. */
19378 for (i = 0; i < ga_loaded.ga_len; ++i)
19379 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19380 break;
19381 if (!reload && i < ga_loaded.ga_len)
19382 ret = FALSE; /* was loaded already */
19383 else
19384 {
19385 /* Remember the name if it wasn't loaded already. */
19386 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19387 {
19388 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19389 tofree = NULL;
19390 }
19391
19392 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019393 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019394 ret = TRUE;
19395 }
19396
19397 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019398 return ret;
19399}
19400
19401/*
19402 * Return the autoload script name for a function or variable name.
19403 * Returns NULL when out of memory.
19404 */
19405 static char_u *
19406autoload_name(name)
19407 char_u *name;
19408{
19409 char_u *p;
19410 char_u *scriptname;
19411
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019412 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019413 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19414 if (scriptname == NULL)
19415 return FALSE;
19416 STRCPY(scriptname, "autoload/");
19417 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019418 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019419 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019420 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019421 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019422 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019423}
19424
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19426
19427/*
19428 * Function given to ExpandGeneric() to obtain the list of user defined
19429 * function names.
19430 */
19431 char_u *
19432get_user_func_name(xp, idx)
19433 expand_T *xp;
19434 int idx;
19435{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019436 static long_u done;
19437 static hashitem_T *hi;
19438 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
19440 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019442 done = 0;
19443 hi = func_hashtab.ht_array;
19444 }
19445 if (done < func_hashtab.ht_used)
19446 {
19447 if (done++ > 0)
19448 ++hi;
19449 while (HASHITEM_EMPTY(hi))
19450 ++hi;
19451 fp = HI2UF(hi);
19452
19453 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19454 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455
19456 cat_func_name(IObuff, fp);
19457 if (xp->xp_context != EXPAND_USER_FUNC)
19458 {
19459 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019460 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 STRCAT(IObuff, ")");
19462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 return IObuff;
19464 }
19465 return NULL;
19466}
19467
19468#endif /* FEAT_CMDL_COMPL */
19469
19470/*
19471 * Copy the function name of "fp" to buffer "buf".
19472 * "buf" must be able to hold the function name plus three bytes.
19473 * Takes care of script-local function names.
19474 */
19475 static void
19476cat_func_name(buf, fp)
19477 char_u *buf;
19478 ufunc_T *fp;
19479{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019480 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 {
19482 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019483 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 }
19485 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019486 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487}
19488
19489/*
19490 * ":delfunction {name}"
19491 */
19492 void
19493ex_delfunction(eap)
19494 exarg_T *eap;
19495{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019496 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 char_u *p;
19498 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019499 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500
19501 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019502 name = trans_function_name(&p, eap->skip, 0, &fudi);
19503 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019505 {
19506 if (fudi.fd_dict != NULL && !eap->skip)
19507 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 if (!ends_excmd(*skipwhite(p)))
19511 {
19512 vim_free(name);
19513 EMSG(_(e_trailing));
19514 return;
19515 }
19516 eap->nextcmd = check_nextcmd(p);
19517 if (eap->nextcmd != NULL)
19518 *p = NUL;
19519
19520 if (!eap->skip)
19521 fp = find_func(name);
19522 vim_free(name);
19523
19524 if (!eap->skip)
19525 {
19526 if (fp == NULL)
19527 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019528 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 return;
19530 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019531 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 {
19533 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19534 return;
19535 }
19536
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019537 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019539 /* Delete the dict item that refers to the function, it will
19540 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019541 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019543 else
19544 func_free(fp);
19545 }
19546}
19547
19548/*
19549 * Free a function and remove it from the list of functions.
19550 */
19551 static void
19552func_free(fp)
19553 ufunc_T *fp;
19554{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019555 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019556
19557 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019558 ga_clear_strings(&(fp->uf_args));
19559 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019560#ifdef FEAT_PROFILE
19561 vim_free(fp->uf_tml_count);
19562 vim_free(fp->uf_tml_total);
19563 vim_free(fp->uf_tml_self);
19564#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019565
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019566 /* remove the function from the function hashtable */
19567 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19568 if (HASHITEM_EMPTY(hi))
19569 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019570 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019571 hash_remove(&func_hashtab, hi);
19572
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019573 vim_free(fp);
19574}
19575
19576/*
19577 * Unreference a Function: decrement the reference count and free it when it
19578 * becomes zero. Only for numbered functions.
19579 */
19580 static void
19581func_unref(name)
19582 char_u *name;
19583{
19584 ufunc_T *fp;
19585
19586 if (name != NULL && isdigit(*name))
19587 {
19588 fp = find_func(name);
19589 if (fp == NULL)
19590 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019591 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019592 {
19593 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019594 * when "uf_calls" becomes zero. */
19595 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019596 func_free(fp);
19597 }
19598 }
19599}
19600
19601/*
19602 * Count a reference to a Function.
19603 */
19604 static void
19605func_ref(name)
19606 char_u *name;
19607{
19608 ufunc_T *fp;
19609
19610 if (name != NULL && isdigit(*name))
19611 {
19612 fp = find_func(name);
19613 if (fp == NULL)
19614 EMSG2(_(e_intern2), "func_ref()");
19615 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 }
19618}
19619
19620/*
19621 * Call a user function.
19622 */
19623 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019624call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 ufunc_T *fp; /* pointer to function */
19626 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 typval_T *argvars; /* arguments */
19628 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 linenr_T firstline; /* first line of range */
19630 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019631 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632{
Bram Moolenaar33570922005-01-25 22:26:29 +000019633 char_u *save_sourcing_name;
19634 linenr_T save_sourcing_lnum;
19635 scid_T save_current_SID;
19636 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 int save_did_emsg;
19638 static int depth = 0;
19639 dictitem_T *v;
19640 int fixvar_idx = 0; /* index in fixvar[] */
19641 int i;
19642 int ai;
19643 char_u numbuf[NUMBUFLEN];
19644 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019645#ifdef FEAT_PROFILE
19646 proftime_T wait_start;
19647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648
19649 /* If depth of calling is getting too high, don't execute the function */
19650 if (depth >= p_mfd)
19651 {
19652 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019653 rettv->v_type = VAR_NUMBER;
19654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 return;
19656 }
19657 ++depth;
19658
19659 line_breakcheck(); /* check for CTRL-C hit */
19660
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019661 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664 fc.rettv = rettv;
19665 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 fc.linenr = 0;
19667 fc.returned = FALSE;
19668 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019670 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 fc.dbg_tick = debug_tick;
19672
Bram Moolenaar33570922005-01-25 22:26:29 +000019673 /*
19674 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19675 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19676 * each argument variable and saves a lot of time.
19677 */
19678 /*
19679 * Init l: variables.
19680 */
19681 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019682 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019683 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019684 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19685 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019687 name = v->di_key;
19688 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019689 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19690 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19691 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019692 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 v->di_tv.vval.v_dict = selfdict;
19694 ++selfdict->dv_refcount;
19695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019696
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 /*
19698 * Init a: variables.
19699 * Set a:0 to "argcount".
19700 * Set a:000 to a list with room for the "..." arguments.
19701 */
19702 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19703 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019704 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 v = &fc.fixvar[fixvar_idx++].var;
19706 STRCPY(v->di_key, "000");
19707 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19708 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19709 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019710 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 v->di_tv.vval.v_list = &fc.l_varlist;
19712 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19713 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019714 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019715
19716 /*
19717 * Set a:firstline to "firstline" and a:lastline to "lastline".
19718 * Set a:name to named arguments.
19719 * Set a:N to the "..." arguments.
19720 */
19721 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19722 (varnumber_T)firstline);
19723 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19724 (varnumber_T)lastline);
19725 for (i = 0; i < argcount; ++i)
19726 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019727 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019728 if (ai < 0)
19729 /* named argument a:name */
19730 name = FUNCARG(fp, i);
19731 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019732 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019733 /* "..." argument a:1, a:2, etc. */
19734 sprintf((char *)numbuf, "%d", ai + 1);
19735 name = numbuf;
19736 }
19737 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19738 {
19739 v = &fc.fixvar[fixvar_idx++].var;
19740 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19741 }
19742 else
19743 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019744 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19745 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 if (v == NULL)
19747 break;
19748 v->di_flags = DI_FLAGS_RO;
19749 }
19750 STRCPY(v->di_key, name);
19751 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19752
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019753 /* Note: the values are copied directly to avoid alloc/free.
19754 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019756 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019757
19758 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19759 {
19760 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19761 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019762 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019763 }
19764 }
19765
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 /* Don't redraw while executing the function. */
19767 ++RedrawingDisabled;
19768 save_sourcing_name = sourcing_name;
19769 save_sourcing_lnum = sourcing_lnum;
19770 sourcing_lnum = 1;
19771 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019772 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 if (sourcing_name != NULL)
19774 {
19775 if (save_sourcing_name != NULL
19776 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19777 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19778 else
19779 STRCPY(sourcing_name, "function ");
19780 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19781
19782 if (p_verbose >= 12)
19783 {
19784 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019785 verbose_enter_scroll();
19786
Bram Moolenaar555b2802005-05-19 21:08:39 +000019787 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 if (p_verbose >= 14)
19789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019791 char_u numbuf[NUMBUFLEN];
19792 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793
19794 msg_puts((char_u *)"(");
19795 for (i = 0; i < argcount; ++i)
19796 {
19797 if (i > 0)
19798 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799 if (argvars[i].v_type == VAR_NUMBER)
19800 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 else
19802 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019803 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019804 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019806 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 }
19808 }
19809 msg_puts((char_u *)")");
19810 }
19811 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019812
19813 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 --no_wait_return;
19815 }
19816 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019817#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019818 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019819 {
19820 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19821 func_do_profile(fp);
19822 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019823 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019824 {
19825 ++fp->uf_tm_count;
19826 profile_start(&fp->uf_tm_start);
19827 profile_zero(&fp->uf_tm_children);
19828 }
19829 script_prof_save(&wait_start);
19830 }
19831#endif
19832
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019834 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 save_did_emsg = did_emsg;
19836 did_emsg = FALSE;
19837
19838 /* call do_cmdline() to execute the lines */
19839 do_cmdline(NULL, get_func_line, (void *)&fc,
19840 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19841
19842 --RedrawingDisabled;
19843
19844 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019845 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847 clear_tv(rettv);
19848 rettv->v_type = VAR_NUMBER;
19849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 }
19851
Bram Moolenaar05159a02005-02-26 23:04:13 +000019852#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019853 if (do_profiling == PROF_YES && (fp->uf_profiling
19854 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019855 {
19856 profile_end(&fp->uf_tm_start);
19857 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19858 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019859 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019860 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019861 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019862 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19863 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019864 }
19865 }
19866#endif
19867
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 /* when being verbose, mention the return value */
19869 if (p_verbose >= 12)
19870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019872 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019875 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019876 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019877 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19878 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019881 char_u buf[MSG_BUF_LEN];
19882 char_u numbuf[NUMBUFLEN];
19883 char_u *tofree;
19884
Bram Moolenaar555b2802005-05-19 21:08:39 +000019885 /* The value may be very long. Skip the middle part, so that we
19886 * have some idea how it starts and ends. smsg() would always
19887 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019888 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019889 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019890 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019891 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 }
19893 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019894
19895 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 --no_wait_return;
19897 }
19898
19899 vim_free(sourcing_name);
19900 sourcing_name = save_sourcing_name;
19901 sourcing_lnum = save_sourcing_lnum;
19902 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019903#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019904 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019905 script_prof_restore(&wait_start);
19906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907
19908 if (p_verbose >= 12 && sourcing_name != NULL)
19909 {
19910 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019911 verbose_enter_scroll();
19912
Bram Moolenaar555b2802005-05-19 21:08:39 +000019913 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019915
19916 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 --no_wait_return;
19918 }
19919
19920 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019921 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922
Bram Moolenaar33570922005-01-25 22:26:29 +000019923 /* The a: variables typevals were not alloced, only free the allocated
19924 * variables. */
19925 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19926
19927 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 --depth;
19929}
19930
19931/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 * Add a number variable "name" to dict "dp" with value "nr".
19933 */
19934 static void
19935add_nr_var(dp, v, name, nr)
19936 dict_T *dp;
19937 dictitem_T *v;
19938 char *name;
19939 varnumber_T nr;
19940{
19941 STRCPY(v->di_key, name);
19942 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19943 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19944 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019945 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 v->di_tv.vval.v_number = nr;
19947}
19948
19949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 * ":return [expr]"
19951 */
19952 void
19953ex_return(eap)
19954 exarg_T *eap;
19955{
19956 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019957 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 int returning = FALSE;
19959
19960 if (current_funccal == NULL)
19961 {
19962 EMSG(_("E133: :return not inside a function"));
19963 return;
19964 }
19965
19966 if (eap->skip)
19967 ++emsg_skip;
19968
19969 eap->nextcmd = NULL;
19970 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 {
19973 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 }
19978 /* It's safer to return also on error. */
19979 else if (!eap->skip)
19980 {
19981 /*
19982 * Return unless the expression evaluation has been cancelled due to an
19983 * aborting error, an interrupt, or an exception.
19984 */
19985 if (!aborting())
19986 returning = do_return(eap, FALSE, TRUE, NULL);
19987 }
19988
19989 /* When skipping or the return gets pending, advance to the next command
19990 * in this line (!returning). Otherwise, ignore the rest of the line.
19991 * Following lines will be ignored by get_func_line(). */
19992 if (returning)
19993 eap->nextcmd = NULL;
19994 else if (eap->nextcmd == NULL) /* no argument */
19995 eap->nextcmd = check_nextcmd(arg);
19996
19997 if (eap->skip)
19998 --emsg_skip;
19999}
20000
20001/*
20002 * Return from a function. Possibly makes the return pending. Also called
20003 * for a pending return at the ":endtry" or after returning from an extra
20004 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020005 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 * FALSE when the return gets pending.
20008 */
20009 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 exarg_T *eap;
20012 int reanimate;
20013 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020014 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015{
20016 int idx;
20017 struct condstack *cstack = eap->cstack;
20018
20019 if (reanimate)
20020 /* Undo the return. */
20021 current_funccal->returned = FALSE;
20022
20023 /*
20024 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20025 * not in its finally clause (which then is to be executed next) is found.
20026 * In this case, make the ":return" pending for execution at the ":endtry".
20027 * Otherwise, return normally.
20028 */
20029 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20030 if (idx >= 0)
20031 {
20032 cstack->cs_pending[idx] = CSTP_RETURN;
20033
20034 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 /* A pending return again gets pending. "rettv" points to an
20036 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 else
20040 {
20041 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020046 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 {
20048 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020050 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 else
20052 EMSG(_(e_outofmem));
20053 }
20054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056
20057 if (reanimate)
20058 {
20059 /* The pending return value could be overwritten by a ":return"
20060 * without argument in a finally clause; reset the default
20061 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 current_funccal->rettv->v_type = VAR_NUMBER;
20063 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 }
20065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 }
20068 else
20069 {
20070 current_funccal->returned = TRUE;
20071
20072 /* If the return is carried out now, store the return value. For
20073 * a return immediately after reanimation, the value is already
20074 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020080 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 }
20082 }
20083
20084 return idx < 0;
20085}
20086
20087/*
20088 * Free the variable with a pending return value.
20089 */
20090 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091discard_pending_return(rettv)
20092 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093{
Bram Moolenaar33570922005-01-25 22:26:29 +000020094 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095}
20096
20097/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 * is an allocated string. Used by report_pending() for verbose messages.
20100 */
20101 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102get_return_cmd(rettv)
20103 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020105 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020107 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020109 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020110 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020111 if (s == NULL)
20112 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020113
20114 STRCPY(IObuff, ":return ");
20115 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20116 if (STRLEN(s) + 8 >= IOSIZE)
20117 STRCPY(IObuff + IOSIZE - 4, "...");
20118 vim_free(tofree);
20119 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120}
20121
20122/*
20123 * Get next function line.
20124 * Called by do_cmdline() to get the next line.
20125 * Returns allocated string, or NULL for end of function.
20126 */
20127/* ARGSUSED */
20128 char_u *
20129get_func_line(c, cookie, indent)
20130 int c; /* not used */
20131 void *cookie;
20132 int indent; /* not used */
20133{
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020135 ufunc_T *fp = fcp->func;
20136 char_u *retval;
20137 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138
20139 /* If breakpoints have been added/deleted need to check for it. */
20140 if (fcp->dbg_tick != debug_tick)
20141 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020142 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 sourcing_lnum);
20144 fcp->dbg_tick = debug_tick;
20145 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020146#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020147 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020148 func_line_end(cookie);
20149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150
Bram Moolenaar05159a02005-02-26 23:04:13 +000020151 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020152 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20153 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 retval = NULL;
20155 else
20156 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020157 /* Skip NULL lines (continuation lines). */
20158 while (fcp->linenr < gap->ga_len
20159 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20160 ++fcp->linenr;
20161 if (fcp->linenr >= gap->ga_len)
20162 retval = NULL;
20163 else
20164 {
20165 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20166 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020167#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020168 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020169 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020170#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 }
20173
20174 /* Did we encounter a breakpoint? */
20175 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20176 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020177 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020179 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 sourcing_lnum);
20181 fcp->dbg_tick = debug_tick;
20182 }
20183
20184 return retval;
20185}
20186
Bram Moolenaar05159a02005-02-26 23:04:13 +000020187#if defined(FEAT_PROFILE) || defined(PROTO)
20188/*
20189 * Called when starting to read a function line.
20190 * "sourcing_lnum" must be correct!
20191 * When skipping lines it may not actually be executed, but we won't find out
20192 * until later and we need to store the time now.
20193 */
20194 void
20195func_line_start(cookie)
20196 void *cookie;
20197{
20198 funccall_T *fcp = (funccall_T *)cookie;
20199 ufunc_T *fp = fcp->func;
20200
20201 if (fp->uf_profiling && sourcing_lnum >= 1
20202 && sourcing_lnum <= fp->uf_lines.ga_len)
20203 {
20204 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020205 /* Skip continuation lines. */
20206 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20207 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020208 fp->uf_tml_execed = FALSE;
20209 profile_start(&fp->uf_tml_start);
20210 profile_zero(&fp->uf_tml_children);
20211 profile_get_wait(&fp->uf_tml_wait);
20212 }
20213}
20214
20215/*
20216 * Called when actually executing a function line.
20217 */
20218 void
20219func_line_exec(cookie)
20220 void *cookie;
20221{
20222 funccall_T *fcp = (funccall_T *)cookie;
20223 ufunc_T *fp = fcp->func;
20224
20225 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20226 fp->uf_tml_execed = TRUE;
20227}
20228
20229/*
20230 * Called when done with a function line.
20231 */
20232 void
20233func_line_end(cookie)
20234 void *cookie;
20235{
20236 funccall_T *fcp = (funccall_T *)cookie;
20237 ufunc_T *fp = fcp->func;
20238
20239 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20240 {
20241 if (fp->uf_tml_execed)
20242 {
20243 ++fp->uf_tml_count[fp->uf_tml_idx];
20244 profile_end(&fp->uf_tml_start);
20245 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020246 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020247 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20248 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020249 }
20250 fp->uf_tml_idx = -1;
20251 }
20252}
20253#endif
20254
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255/*
20256 * Return TRUE if the currently active function should be ended, because a
20257 * return was encountered or an error occured. Used inside a ":while".
20258 */
20259 int
20260func_has_ended(cookie)
20261 void *cookie;
20262{
Bram Moolenaar33570922005-01-25 22:26:29 +000020263 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264
20265 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20266 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020267 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 || fcp->returned);
20269}
20270
20271/*
20272 * return TRUE if cookie indicates a function which "abort"s on errors.
20273 */
20274 int
20275func_has_abort(cookie)
20276 void *cookie;
20277{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020278 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279}
20280
20281#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20282typedef enum
20283{
20284 VAR_FLAVOUR_DEFAULT,
20285 VAR_FLAVOUR_SESSION,
20286 VAR_FLAVOUR_VIMINFO
20287} var_flavour_T;
20288
20289static var_flavour_T var_flavour __ARGS((char_u *varname));
20290
20291 static var_flavour_T
20292var_flavour(varname)
20293 char_u *varname;
20294{
20295 char_u *p = varname;
20296
20297 if (ASCII_ISUPPER(*p))
20298 {
20299 while (*(++p))
20300 if (ASCII_ISLOWER(*p))
20301 return VAR_FLAVOUR_SESSION;
20302 return VAR_FLAVOUR_VIMINFO;
20303 }
20304 else
20305 return VAR_FLAVOUR_DEFAULT;
20306}
20307#endif
20308
20309#if defined(FEAT_VIMINFO) || defined(PROTO)
20310/*
20311 * Restore global vars that start with a capital from the viminfo file
20312 */
20313 int
20314read_viminfo_varlist(virp, writing)
20315 vir_T *virp;
20316 int writing;
20317{
20318 char_u *tab;
20319 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020320 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321
20322 if (!writing && (find_viminfo_parameter('!') != NULL))
20323 {
20324 tab = vim_strchr(virp->vir_line + 1, '\t');
20325 if (tab != NULL)
20326 {
20327 *tab++ = '\0'; /* isolate the variable name */
20328 if (*tab == 'S') /* string var */
20329 is_string = TRUE;
20330
20331 tab = vim_strchr(tab, '\t');
20332 if (tab != NULL)
20333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 if (is_string)
20335 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020336 tv.v_type = VAR_STRING;
20337 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 }
20340 else
20341 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020342 tv.v_type = VAR_NUMBER;
20343 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020345 set_var(virp->vir_line + 1, &tv, FALSE);
20346 if (is_string)
20347 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 }
20349 }
20350 }
20351
20352 return viminfo_readline(virp);
20353}
20354
20355/*
20356 * Write global vars that start with a capital to the viminfo file
20357 */
20358 void
20359write_viminfo_varlist(fp)
20360 FILE *fp;
20361{
Bram Moolenaar33570922005-01-25 22:26:29 +000020362 hashitem_T *hi;
20363 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020364 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020366 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020368 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
20370 if (find_viminfo_parameter('!') == NULL)
20371 return;
20372
20373 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020374
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020375 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020378 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020380 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 this_var = HI2DI(hi);
20382 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020384 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020385 {
20386 case VAR_STRING: s = "STR"; break;
20387 case VAR_NUMBER: s = "NUM"; break;
20388 default: continue;
20389 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020391 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020392 if (p != NULL)
20393 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020394 vim_free(tofree);
20395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 }
20397 }
20398}
20399#endif
20400
20401#if defined(FEAT_SESSION) || defined(PROTO)
20402 int
20403store_session_globals(fd)
20404 FILE *fd;
20405{
Bram Moolenaar33570922005-01-25 22:26:29 +000020406 hashitem_T *hi;
20407 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020408 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 char_u *p, *t;
20410
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020411 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020414 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020416 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 this_var = HI2DI(hi);
20418 if ((this_var->di_tv.v_type == VAR_NUMBER
20419 || this_var->di_tv.v_type == VAR_STRING)
20420 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020421 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020422 /* Escape special characters with a backslash. Turn a LF and
20423 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020425 (char_u *)"\\\"\n\r");
20426 if (p == NULL) /* out of memory */
20427 break;
20428 for (t = p; *t != NUL; ++t)
20429 if (*t == '\n')
20430 *t = 'n';
20431 else if (*t == '\r')
20432 *t = 'r';
20433 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020434 this_var->di_key,
20435 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20436 : ' ',
20437 p,
20438 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20439 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020440 || put_eol(fd) == FAIL)
20441 {
20442 vim_free(p);
20443 return FAIL;
20444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 vim_free(p);
20446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 }
20448 }
20449 return OK;
20450}
20451#endif
20452
Bram Moolenaar661b1822005-07-28 22:36:45 +000020453/*
20454 * Display script name where an item was last set.
20455 * Should only be invoked when 'verbose' is non-zero.
20456 */
20457 void
20458last_set_msg(scriptID)
20459 scid_T scriptID;
20460{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020461 char_u *p;
20462
Bram Moolenaar661b1822005-07-28 22:36:45 +000020463 if (scriptID != 0)
20464 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020465 p = home_replace_save(NULL, get_scriptname(scriptID));
20466 if (p != NULL)
20467 {
20468 verbose_enter();
20469 MSG_PUTS(_("\n\tLast set from "));
20470 MSG_PUTS(p);
20471 vim_free(p);
20472 verbose_leave();
20473 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020474 }
20475}
20476
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477#endif /* FEAT_EVAL */
20478
20479#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20480
20481
20482#ifdef WIN3264
20483/*
20484 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20485 */
20486static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20487static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20488static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20489
20490/*
20491 * Get the short pathname of a file.
20492 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20493 */
20494 static int
20495get_short_pathname(fnamep, bufp, fnamelen)
20496 char_u **fnamep;
20497 char_u **bufp;
20498 int *fnamelen;
20499{
20500 int l,len;
20501 char_u *newbuf;
20502
20503 len = *fnamelen;
20504
20505 l = GetShortPathName(*fnamep, *fnamep, len);
20506 if (l > len - 1)
20507 {
20508 /* If that doesn't work (not enough space), then save the string
20509 * and try again with a new buffer big enough
20510 */
20511 newbuf = vim_strnsave(*fnamep, l);
20512 if (newbuf == NULL)
20513 return 0;
20514
20515 vim_free(*bufp);
20516 *fnamep = *bufp = newbuf;
20517
20518 l = GetShortPathName(*fnamep,*fnamep,l+1);
20519
20520 /* Really should always succeed, as the buffer is big enough */
20521 }
20522
20523 *fnamelen = l;
20524 return 1;
20525}
20526
20527/*
20528 * Create a short path name. Returns the length of the buffer it needs.
20529 * Doesn't copy over the end of the buffer passed in.
20530 */
20531 static int
20532shortpath_for_invalid_fname(fname, bufp, fnamelen)
20533 char_u **fname;
20534 char_u **bufp;
20535 int *fnamelen;
20536{
20537 char_u *s, *p, *pbuf2, *pbuf3;
20538 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020539 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540
20541 /* Make a copy */
20542 len2 = *fnamelen;
20543 pbuf2 = vim_strnsave(*fname, len2);
20544 pbuf3 = NULL;
20545
20546 s = pbuf2 + len2 - 1; /* Find the end */
20547 slen = 1;
20548 plen = len2;
20549
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020550 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 {
20552 --s;
20553 ++slen;
20554 --plen;
20555 }
20556
20557 do
20558 {
20559 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020560 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 {
20562 --s;
20563 ++slen;
20564 --plen;
20565 }
20566 if (s <= pbuf2)
20567 break;
20568
20569 /* Remeber the character that is about to be blatted */
20570 ch = *s;
20571 *s = 0; /* get_short_pathname requires a null-terminated string */
20572
20573 /* Try it in situ */
20574 p = pbuf2;
20575 if (!get_short_pathname(&p, &pbuf3, &plen))
20576 {
20577 vim_free(pbuf2);
20578 return -1;
20579 }
20580 *s = ch; /* Preserve the string */
20581 } while (plen == 0);
20582
20583 if (plen > 0)
20584 {
20585 /* Remeber the length of the new string. */
20586 *fnamelen = len = plen + slen;
20587 vim_free(*bufp);
20588 if (len > len2)
20589 {
20590 /* If there's not enough space in the currently allocated string,
20591 * then copy it to a buffer big enough.
20592 */
20593 *fname= *bufp = vim_strnsave(p, len);
20594 if (*fname == NULL)
20595 return -1;
20596 }
20597 else
20598 {
20599 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20600 *fname = *bufp = pbuf2;
20601 if (p != pbuf2)
20602 strncpy(*fname, p, plen);
20603 pbuf2 = NULL;
20604 }
20605 /* Concat the next bit */
20606 strncpy(*fname + plen, s, slen);
20607 (*fname)[len] = '\0';
20608 }
20609 vim_free(pbuf3);
20610 vim_free(pbuf2);
20611 return 0;
20612}
20613
20614/*
20615 * Get a pathname for a partial path.
20616 */
20617 static int
20618shortpath_for_partial(fnamep, bufp, fnamelen)
20619 char_u **fnamep;
20620 char_u **bufp;
20621 int *fnamelen;
20622{
20623 int sepcount, len, tflen;
20624 char_u *p;
20625 char_u *pbuf, *tfname;
20626 int hasTilde;
20627
20628 /* Count up the path seperators from the RHS.. so we know which part
20629 * of the path to return.
20630 */
20631 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020632 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 if (vim_ispathsep(*p))
20634 ++sepcount;
20635
20636 /* Need full path first (use expand_env() to remove a "~/") */
20637 hasTilde = (**fnamep == '~');
20638 if (hasTilde)
20639 pbuf = tfname = expand_env_save(*fnamep);
20640 else
20641 pbuf = tfname = FullName_save(*fnamep, FALSE);
20642
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020643 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644
20645 if (!get_short_pathname(&tfname, &pbuf, &len))
20646 return -1;
20647
20648 if (len == 0)
20649 {
20650 /* Don't have a valid filename, so shorten the rest of the
20651 * path if we can. This CAN give us invalid 8.3 filenames, but
20652 * there's not a lot of point in guessing what it might be.
20653 */
20654 len = tflen;
20655 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20656 return -1;
20657 }
20658
20659 /* Count the paths backward to find the beginning of the desired string. */
20660 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020661 {
20662#ifdef FEAT_MBYTE
20663 if (has_mbyte)
20664 p -= mb_head_off(tfname, p);
20665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 if (vim_ispathsep(*p))
20667 {
20668 if (sepcount == 0 || (hasTilde && sepcount == 1))
20669 break;
20670 else
20671 sepcount --;
20672 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 if (hasTilde)
20675 {
20676 --p;
20677 if (p >= tfname)
20678 *p = '~';
20679 else
20680 return -1;
20681 }
20682 else
20683 ++p;
20684
20685 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20686 vim_free(*bufp);
20687 *fnamelen = (int)STRLEN(p);
20688 *bufp = pbuf;
20689 *fnamep = p;
20690
20691 return 0;
20692}
20693#endif /* WIN3264 */
20694
20695/*
20696 * Adjust a filename, according to a string of modifiers.
20697 * *fnamep must be NUL terminated when called. When returning, the length is
20698 * determined by *fnamelen.
20699 * Returns valid flags.
20700 * When there is an error, *fnamep is set to NULL.
20701 */
20702 int
20703modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20704 char_u *src; /* string with modifiers */
20705 int *usedlen; /* characters after src that are used */
20706 char_u **fnamep; /* file name so far */
20707 char_u **bufp; /* buffer for allocated file name or NULL */
20708 int *fnamelen; /* length of fnamep */
20709{
20710 int valid = 0;
20711 char_u *tail;
20712 char_u *s, *p, *pbuf;
20713 char_u dirname[MAXPATHL];
20714 int c;
20715 int has_fullname = 0;
20716#ifdef WIN3264
20717 int has_shortname = 0;
20718#endif
20719
20720repeat:
20721 /* ":p" - full path/file_name */
20722 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20723 {
20724 has_fullname = 1;
20725
20726 valid |= VALID_PATH;
20727 *usedlen += 2;
20728
20729 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20730 if ((*fnamep)[0] == '~'
20731#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20732 && ((*fnamep)[1] == '/'
20733# ifdef BACKSLASH_IN_FILENAME
20734 || (*fnamep)[1] == '\\'
20735# endif
20736 || (*fnamep)[1] == NUL)
20737
20738#endif
20739 )
20740 {
20741 *fnamep = expand_env_save(*fnamep);
20742 vim_free(*bufp); /* free any allocated file name */
20743 *bufp = *fnamep;
20744 if (*fnamep == NULL)
20745 return -1;
20746 }
20747
20748 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020749 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 {
20751 if (vim_ispathsep(*p)
20752 && p[1] == '.'
20753 && (p[2] == NUL
20754 || vim_ispathsep(p[2])
20755 || (p[2] == '.'
20756 && (p[3] == NUL || vim_ispathsep(p[3])))))
20757 break;
20758 }
20759
20760 /* FullName_save() is slow, don't use it when not needed. */
20761 if (*p != NUL || !vim_isAbsName(*fnamep))
20762 {
20763 *fnamep = FullName_save(*fnamep, *p != NUL);
20764 vim_free(*bufp); /* free any allocated file name */
20765 *bufp = *fnamep;
20766 if (*fnamep == NULL)
20767 return -1;
20768 }
20769
20770 /* Append a path separator to a directory. */
20771 if (mch_isdir(*fnamep))
20772 {
20773 /* Make room for one or two extra characters. */
20774 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20775 vim_free(*bufp); /* free any allocated file name */
20776 *bufp = *fnamep;
20777 if (*fnamep == NULL)
20778 return -1;
20779 add_pathsep(*fnamep);
20780 }
20781 }
20782
20783 /* ":." - path relative to the current directory */
20784 /* ":~" - path relative to the home directory */
20785 /* ":8" - shortname path - postponed till after */
20786 while (src[*usedlen] == ':'
20787 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20788 {
20789 *usedlen += 2;
20790 if (c == '8')
20791 {
20792#ifdef WIN3264
20793 has_shortname = 1; /* Postpone this. */
20794#endif
20795 continue;
20796 }
20797 pbuf = NULL;
20798 /* Need full path first (use expand_env() to remove a "~/") */
20799 if (!has_fullname)
20800 {
20801 if (c == '.' && **fnamep == '~')
20802 p = pbuf = expand_env_save(*fnamep);
20803 else
20804 p = pbuf = FullName_save(*fnamep, FALSE);
20805 }
20806 else
20807 p = *fnamep;
20808
20809 has_fullname = 0;
20810
20811 if (p != NULL)
20812 {
20813 if (c == '.')
20814 {
20815 mch_dirname(dirname, MAXPATHL);
20816 s = shorten_fname(p, dirname);
20817 if (s != NULL)
20818 {
20819 *fnamep = s;
20820 if (pbuf != NULL)
20821 {
20822 vim_free(*bufp); /* free any allocated file name */
20823 *bufp = pbuf;
20824 pbuf = NULL;
20825 }
20826 }
20827 }
20828 else
20829 {
20830 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20831 /* Only replace it when it starts with '~' */
20832 if (*dirname == '~')
20833 {
20834 s = vim_strsave(dirname);
20835 if (s != NULL)
20836 {
20837 *fnamep = s;
20838 vim_free(*bufp);
20839 *bufp = s;
20840 }
20841 }
20842 }
20843 vim_free(pbuf);
20844 }
20845 }
20846
20847 tail = gettail(*fnamep);
20848 *fnamelen = (int)STRLEN(*fnamep);
20849
20850 /* ":h" - head, remove "/file_name", can be repeated */
20851 /* Don't remove the first "/" or "c:\" */
20852 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20853 {
20854 valid |= VALID_HEAD;
20855 *usedlen += 2;
20856 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020857 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 --tail;
20859 *fnamelen = (int)(tail - *fnamep);
20860#ifdef VMS
20861 if (*fnamelen > 0)
20862 *fnamelen += 1; /* the path separator is part of the path */
20863#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020864 while (tail > s && !after_pathsep(s, tail))
20865 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 }
20867
20868 /* ":8" - shortname */
20869 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20870 {
20871 *usedlen += 2;
20872#ifdef WIN3264
20873 has_shortname = 1;
20874#endif
20875 }
20876
20877#ifdef WIN3264
20878 /* Check shortname after we have done 'heads' and before we do 'tails'
20879 */
20880 if (has_shortname)
20881 {
20882 pbuf = NULL;
20883 /* Copy the string if it is shortened by :h */
20884 if (*fnamelen < (int)STRLEN(*fnamep))
20885 {
20886 p = vim_strnsave(*fnamep, *fnamelen);
20887 if (p == 0)
20888 return -1;
20889 vim_free(*bufp);
20890 *bufp = *fnamep = p;
20891 }
20892
20893 /* Split into two implementations - makes it easier. First is where
20894 * there isn't a full name already, second is where there is.
20895 */
20896 if (!has_fullname && !vim_isAbsName(*fnamep))
20897 {
20898 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20899 return -1;
20900 }
20901 else
20902 {
20903 int l;
20904
20905 /* Simple case, already have the full-name
20906 * Nearly always shorter, so try first time. */
20907 l = *fnamelen;
20908 if (!get_short_pathname(fnamep, bufp, &l))
20909 return -1;
20910
20911 if (l == 0)
20912 {
20913 /* Couldn't find the filename.. search the paths.
20914 */
20915 l = *fnamelen;
20916 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20917 return -1;
20918 }
20919 *fnamelen = l;
20920 }
20921 }
20922#endif /* WIN3264 */
20923
20924 /* ":t" - tail, just the basename */
20925 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20926 {
20927 *usedlen += 2;
20928 *fnamelen -= (int)(tail - *fnamep);
20929 *fnamep = tail;
20930 }
20931
20932 /* ":e" - extension, can be repeated */
20933 /* ":r" - root, without extension, can be repeated */
20934 while (src[*usedlen] == ':'
20935 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20936 {
20937 /* find a '.' in the tail:
20938 * - for second :e: before the current fname
20939 * - otherwise: The last '.'
20940 */
20941 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20942 s = *fnamep - 2;
20943 else
20944 s = *fnamep + *fnamelen - 1;
20945 for ( ; s > tail; --s)
20946 if (s[0] == '.')
20947 break;
20948 if (src[*usedlen + 1] == 'e') /* :e */
20949 {
20950 if (s > tail)
20951 {
20952 *fnamelen += (int)(*fnamep - (s + 1));
20953 *fnamep = s + 1;
20954#ifdef VMS
20955 /* cut version from the extension */
20956 s = *fnamep + *fnamelen - 1;
20957 for ( ; s > *fnamep; --s)
20958 if (s[0] == ';')
20959 break;
20960 if (s > *fnamep)
20961 *fnamelen = s - *fnamep;
20962#endif
20963 }
20964 else if (*fnamep <= tail)
20965 *fnamelen = 0;
20966 }
20967 else /* :r */
20968 {
20969 if (s > tail) /* remove one extension */
20970 *fnamelen = (int)(s - *fnamep);
20971 }
20972 *usedlen += 2;
20973 }
20974
20975 /* ":s?pat?foo?" - substitute */
20976 /* ":gs?pat?foo?" - global substitute */
20977 if (src[*usedlen] == ':'
20978 && (src[*usedlen + 1] == 's'
20979 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20980 {
20981 char_u *str;
20982 char_u *pat;
20983 char_u *sub;
20984 int sep;
20985 char_u *flags;
20986 int didit = FALSE;
20987
20988 flags = (char_u *)"";
20989 s = src + *usedlen + 2;
20990 if (src[*usedlen + 1] == 'g')
20991 {
20992 flags = (char_u *)"g";
20993 ++s;
20994 }
20995
20996 sep = *s++;
20997 if (sep)
20998 {
20999 /* find end of pattern */
21000 p = vim_strchr(s, sep);
21001 if (p != NULL)
21002 {
21003 pat = vim_strnsave(s, (int)(p - s));
21004 if (pat != NULL)
21005 {
21006 s = p + 1;
21007 /* find end of substitution */
21008 p = vim_strchr(s, sep);
21009 if (p != NULL)
21010 {
21011 sub = vim_strnsave(s, (int)(p - s));
21012 str = vim_strnsave(*fnamep, *fnamelen);
21013 if (sub != NULL && str != NULL)
21014 {
21015 *usedlen = (int)(p + 1 - src);
21016 s = do_string_sub(str, pat, sub, flags);
21017 if (s != NULL)
21018 {
21019 *fnamep = s;
21020 *fnamelen = (int)STRLEN(s);
21021 vim_free(*bufp);
21022 *bufp = s;
21023 didit = TRUE;
21024 }
21025 }
21026 vim_free(sub);
21027 vim_free(str);
21028 }
21029 vim_free(pat);
21030 }
21031 }
21032 /* after using ":s", repeat all the modifiers */
21033 if (didit)
21034 goto repeat;
21035 }
21036 }
21037
21038 return valid;
21039}
21040
21041/*
21042 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21043 * "flags" can be "g" to do a global substitute.
21044 * Returns an allocated string, NULL for error.
21045 */
21046 char_u *
21047do_string_sub(str, pat, sub, flags)
21048 char_u *str;
21049 char_u *pat;
21050 char_u *sub;
21051 char_u *flags;
21052{
21053 int sublen;
21054 regmatch_T regmatch;
21055 int i;
21056 int do_all;
21057 char_u *tail;
21058 garray_T ga;
21059 char_u *ret;
21060 char_u *save_cpo;
21061
21062 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21063 save_cpo = p_cpo;
21064 p_cpo = (char_u *)"";
21065
21066 ga_init2(&ga, 1, 200);
21067
21068 do_all = (flags[0] == 'g');
21069
21070 regmatch.rm_ic = p_ic;
21071 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21072 if (regmatch.regprog != NULL)
21073 {
21074 tail = str;
21075 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21076 {
21077 /*
21078 * Get some space for a temporary buffer to do the substitution
21079 * into. It will contain:
21080 * - The text up to where the match is.
21081 * - The substituted text.
21082 * - The text after the match.
21083 */
21084 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21085 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21086 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21087 {
21088 ga_clear(&ga);
21089 break;
21090 }
21091
21092 /* copy the text up to where the match is */
21093 i = (int)(regmatch.startp[0] - tail);
21094 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21095 /* add the substituted text */
21096 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21097 + ga.ga_len + i, TRUE, TRUE, FALSE);
21098 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 /* avoid getting stuck on a match with an empty string */
21100 if (tail == regmatch.endp[0])
21101 {
21102 if (*tail == NUL)
21103 break;
21104 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21105 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 }
21107 else
21108 {
21109 tail = regmatch.endp[0];
21110 if (*tail == NUL)
21111 break;
21112 }
21113 if (!do_all)
21114 break;
21115 }
21116
21117 if (ga.ga_data != NULL)
21118 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21119
21120 vim_free(regmatch.regprog);
21121 }
21122
21123 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21124 ga_clear(&ga);
21125 p_cpo = save_cpo;
21126
21127 return ret;
21128}
21129
21130#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */