blob: b01c5e0799c005fa69b83ec4a0f45f438963cb65 [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 }
8255 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8256 {
8257 EMSG(_(e_invarg));
8258 return;
8259 }
8260
8261 startcol = get_tv_number_chk(&argvars[0], NULL);
8262 if (startcol <= 0)
8263 return;
8264
8265 set_completion(startcol - 1, argvars[1].vval.v_list);
8266}
8267
8268/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008269 * "complete_add()" function
8270 */
8271/*ARGSUSED*/
8272 static void
8273f_complete_add(argvars, rettv)
8274 typval_T *argvars;
8275 typval_T *rettv;
8276{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008277 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008278}
8279
8280/*
8281 * "complete_check()" function
8282 */
8283/*ARGSUSED*/
8284 static void
8285f_complete_check(argvars, rettv)
8286 typval_T *argvars;
8287 typval_T *rettv;
8288{
8289 int saved = RedrawingDisabled;
8290
8291 RedrawingDisabled = 0;
8292 ins_compl_check_keys(0);
8293 rettv->vval.v_number = compl_interrupted;
8294 RedrawingDisabled = saved;
8295}
8296#endif
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298/*
8299 * "confirm(message, buttons[, default [, type]])" function
8300 */
8301/*ARGSUSED*/
8302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008303f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 typval_T *argvars;
8305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8308 char_u *message;
8309 char_u *buttons = NULL;
8310 char_u buf[NUMBUFLEN];
8311 char_u buf2[NUMBUFLEN];
8312 int def = 1;
8313 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008314 char_u *typestr;
8315 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317 message = get_tv_string_chk(&argvars[0]);
8318 if (message == NULL)
8319 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8323 if (buttons == NULL)
8324 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8331 if (typestr == NULL)
8332 error = TRUE;
8333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335 switch (TOUPPER_ASC(*typestr))
8336 {
8337 case 'E': type = VIM_ERROR; break;
8338 case 'Q': type = VIM_QUESTION; break;
8339 case 'I': type = VIM_INFO; break;
8340 case 'W': type = VIM_WARNING; break;
8341 case 'G': type = VIM_GENERIC; break;
8342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344 }
8345 }
8346 }
8347
8348 if (buttons == NULL || *buttons == NUL)
8349 buttons = (char_u *)_("&Ok");
8350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008351 if (error)
8352 rettv->vval.v_number = 0;
8353 else
8354 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 def, NULL);
8356#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#endif
8359}
8360
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361/*
8362 * "copy()" function
8363 */
8364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008366 typval_T *argvars;
8367 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008368{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008369 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
8372/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008373 * "count()" function
8374 */
8375 static void
8376f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 typval_T *argvars;
8378 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008379{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008380 long n = 0;
8381 int ic = FALSE;
8382
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008384 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 listitem_T *li;
8386 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008388
Bram Moolenaare9a41262005-01-15 22:18:47 +00008389 if ((l = argvars[0].vval.v_list) != NULL)
8390 {
8391 li = l->lv_first;
8392 if (argvars[2].v_type != VAR_UNKNOWN)
8393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008394 int error = FALSE;
8395
8396 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008397 if (argvars[3].v_type != VAR_UNKNOWN)
8398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 idx = get_tv_number_chk(&argvars[3], &error);
8400 if (!error)
8401 {
8402 li = list_find(l, idx);
8403 if (li == NULL)
8404 EMSGN(_(e_listidx), idx);
8405 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 if (error)
8408 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409 }
8410
8411 for ( ; li != NULL; li = li->li_next)
8412 if (tv_equal(&li->li_tv, &argvars[1], ic))
8413 ++n;
8414 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008415 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008416 else if (argvars[0].v_type == VAR_DICT)
8417 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 int todo;
8419 dict_T *d;
8420 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008421
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008422 if ((d = argvars[0].vval.v_dict) != NULL)
8423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008424 int error = FALSE;
8425
Bram Moolenaare9a41262005-01-15 22:18:47 +00008426 if (argvars[2].v_type != VAR_UNKNOWN)
8427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008429 if (argvars[3].v_type != VAR_UNKNOWN)
8430 EMSG(_(e_invarg));
8431 }
8432
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008433 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008435 {
8436 if (!HASHITEM_EMPTY(hi))
8437 {
8438 --todo;
8439 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8440 ++n;
8441 }
8442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008443 }
8444 }
8445 else
8446 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008447 rettv->vval.v_number = n;
8448}
8449
8450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8452 *
8453 * Checks the existence of a cscope connection.
8454 */
8455/*ARGSUSED*/
8456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 typval_T *argvars;
8459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461#ifdef FEAT_CSCOPE
8462 int num = 0;
8463 char_u *dbpath = NULL;
8464 char_u *prepend = NULL;
8465 char_u buf[NUMBUFLEN];
8466
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008467 if (argvars[0].v_type != VAR_UNKNOWN
8468 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008470 num = (int)get_tv_number(&argvars[0]);
8471 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 }
8475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479#endif
8480}
8481
8482/*
8483 * "cursor(lnum, col)" function
8484 *
8485 * Moves the cursor to the specified line and column
8486 */
8487/*ARGSUSED*/
8488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 typval_T *argvars;
8491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008494#ifdef FEAT_VIRTUALEDIT
8495 long coladd = 0;
8496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
Bram Moolenaara5525202006-03-02 22:52:09 +00008498 if (argvars[1].v_type == VAR_UNKNOWN)
8499 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008500 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008501
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008502 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008503 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008504 line = pos.lnum;
8505 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008506#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008507 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008508#endif
8509 }
8510 else
8511 {
8512 line = get_tv_lnum(argvars);
8513 col = get_tv_number_chk(&argvars[1], NULL);
8514#ifdef FEAT_VIRTUALEDIT
8515 if (argvars[2].v_type != VAR_UNKNOWN)
8516 coladd = get_tv_number_chk(&argvars[2], NULL);
8517#endif
8518 }
8519 if (line < 0 || col < 0
8520#ifdef FEAT_VIRTUALEDIT
8521 || coladd < 0
8522#endif
8523 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008524 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (line > 0)
8526 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (col > 0)
8528 curwin->w_cursor.col = col - 1;
8529#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008530 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531#endif
8532
8533 /* Make sure the cursor is in a valid position. */
8534 check_cursor();
8535#ifdef FEAT_MBYTE
8536 /* Correct cursor for multi-byte character. */
8537 if (has_mbyte)
8538 mb_adjust_cursor();
8539#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008540
8541 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 */
8547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008548f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 typval_T *argvars;
8550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008552 int noref = 0;
8553
8554 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008555 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008556 if (noref < 0 || noref > 1)
8557 EMSG(_(e_invarg));
8558 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008559 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561
8562/*
8563 * "delete()" function
8564 */
8565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 typval_T *argvars;
8568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569{
8570 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574}
8575
8576/*
8577 * "did_filetype()" function
8578 */
8579/*ARGSUSED*/
8580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *argvars;
8583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
8585#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589#endif
8590}
8591
8592/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008593 * "diff_filler()" function
8594 */
8595/*ARGSUSED*/
8596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 typval_T *argvars;
8599 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008600{
8601#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008603#endif
8604}
8605
8606/*
8607 * "diff_hlID()" function
8608 */
8609/*ARGSUSED*/
8610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 typval_T *argvars;
8613 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008614{
8615#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008617 static linenr_T prev_lnum = 0;
8618 static int changedtick = 0;
8619 static int fnum = 0;
8620 static int change_start = 0;
8621 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008622 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008623 int filler_lines;
8624 int col;
8625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 if (lnum < 0) /* ignore type error in {lnum} arg */
8627 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008628 if (lnum != prev_lnum
8629 || changedtick != curbuf->b_changedtick
8630 || fnum != curbuf->b_fnum)
8631 {
8632 /* New line, buffer, change: need to get the values. */
8633 filler_lines = diff_check(curwin, lnum);
8634 if (filler_lines < 0)
8635 {
8636 if (filler_lines == -1)
8637 {
8638 change_start = MAXCOL;
8639 change_end = -1;
8640 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8641 hlID = HLF_ADD; /* added line */
8642 else
8643 hlID = HLF_CHD; /* changed line */
8644 }
8645 else
8646 hlID = HLF_ADD; /* added line */
8647 }
8648 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008649 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008650 prev_lnum = lnum;
8651 changedtick = curbuf->b_changedtick;
8652 fnum = curbuf->b_fnum;
8653 }
8654
8655 if (hlID == HLF_CHD || hlID == HLF_TXD)
8656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008657 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008658 if (col >= change_start && col <= change_end)
8659 hlID = HLF_TXD; /* changed text */
8660 else
8661 hlID = HLF_CHD; /* changed line */
8662 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008663 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008664#endif
8665}
8666
8667/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008668 * "empty({expr})" function
8669 */
8670 static void
8671f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 typval_T *argvars;
8673 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008674{
8675 int n;
8676
8677 switch (argvars[0].v_type)
8678 {
8679 case VAR_STRING:
8680 case VAR_FUNC:
8681 n = argvars[0].vval.v_string == NULL
8682 || *argvars[0].vval.v_string == NUL;
8683 break;
8684 case VAR_NUMBER:
8685 n = argvars[0].vval.v_number == 0;
8686 break;
8687 case VAR_LIST:
8688 n = argvars[0].vval.v_list == NULL
8689 || argvars[0].vval.v_list->lv_first == NULL;
8690 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008691 case VAR_DICT:
8692 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008694 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008695 default:
8696 EMSG2(_(e_intern2), "f_empty()");
8697 n = 0;
8698 }
8699
8700 rettv->vval.v_number = n;
8701}
8702
8703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 * "escape({string}, {chars})" function
8705 */
8706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 typval_T *argvars;
8709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 char_u buf[NUMBUFLEN];
8712
Bram Moolenaar758711c2005-02-02 23:11:38 +00008713 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8714 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716}
8717
8718/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008719 * "eval()" function
8720 */
8721/*ARGSUSED*/
8722 static void
8723f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008726{
8727 char_u *s;
8728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008729 s = get_tv_string_chk(&argvars[0]);
8730 if (s != NULL)
8731 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008733 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8734 {
8735 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008736 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008738 else if (*s != NUL)
8739 EMSG(_(e_trailing));
8740}
8741
8742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 * "eventhandler()" function
8744 */
8745/*ARGSUSED*/
8746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
8754/*
8755 * "executable()" function
8756 */
8757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008759 typval_T *argvars;
8760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
8766 * "exists()" function
8767 */
8768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008770 typval_T *argvars;
8771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 char_u *p;
8774 char_u *name;
8775 int n = FALSE;
8776 int len = 0;
8777
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 if (*p == '$') /* environment variable */
8780 {
8781 /* first try "normal" environment variables (fast) */
8782 if (mch_getenv(p + 1) != NULL)
8783 n = TRUE;
8784 else
8785 {
8786 /* try expanding things like $VIM and ${HOME} */
8787 p = expand_env_save(p);
8788 if (p != NULL && *p != '$')
8789 n = TRUE;
8790 vim_free(p);
8791 }
8792 }
8793 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 else if (*p == '*') /* internal or user defined function */
8796 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008797 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 }
8799 else if (*p == ':')
8800 {
8801 n = cmd_exists(p + 1);
8802 }
8803 else if (*p == '#')
8804 {
8805#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008806 if (p[1] == '#')
8807 n = autocmd_supported(p + 2);
8808 else
8809 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810#endif
8811 }
8812 else /* internal variable */
8813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008814 char_u *tofree;
8815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008817 /* get_name_len() takes care of expanding curly braces */
8818 name = p;
8819 len = get_name_len(&p, &tofree, TRUE, FALSE);
8820 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008822 if (tofree != NULL)
8823 name = tofree;
8824 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8825 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008827 /* handle d.key, l[idx], f(expr) */
8828 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8829 if (n)
8830 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
8832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008834 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838}
8839
8840/*
8841 * "expand()" function
8842 */
8843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *argvars;
8846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 char_u *s;
8849 int len;
8850 char_u *errormsg;
8851 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8852 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->v_type = VAR_STRING;
8856 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 if (*s == '%' || *s == '#' || *s == '<')
8858 {
8859 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 --emsg_off;
8862 }
8863 else
8864 {
8865 /* When the optional second argument is non-zero, don't remove matches
8866 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008867 if (argvars[1].v_type != VAR_UNKNOWN
8868 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 if (!error)
8871 {
8872 ExpandInit(&xpc);
8873 xpc.xp_context = EXPAND_FILES;
8874 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008875 }
8876 else
8877 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879}
8880
8881/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008882 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008884 */
8885 static void
8886f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008887 typval_T *argvars;
8888 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008889{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008890 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008891 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008892 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 list_T *l1, *l2;
8894 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008895 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008897
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 l1 = argvars[0].vval.v_list;
8899 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008900 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8901 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008902 {
8903 if (argvars[2].v_type != VAR_UNKNOWN)
8904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 before = get_tv_number_chk(&argvars[2], &error);
8906 if (error)
8907 return; /* type error; errmsg already given */
8908
Bram Moolenaar758711c2005-02-02 23:11:38 +00008909 if (before == l1->lv_len)
8910 item = NULL;
8911 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008912 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008913 item = list_find(l1, before);
8914 if (item == NULL)
8915 {
8916 EMSGN(_(e_listidx), before);
8917 return;
8918 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 }
8920 }
8921 else
8922 item = NULL;
8923 list_extend(l1, l2, item);
8924
Bram Moolenaare9a41262005-01-15 22:18:47 +00008925 copy_tv(&argvars[0], rettv);
8926 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008927 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008928 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8929 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 dict_T *d1, *d2;
8931 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008932 char_u *action;
8933 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008935 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008936
8937 d1 = argvars[0].vval.v_dict;
8938 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008939 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8940 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 {
8942 /* Check the third argument. */
8943 if (argvars[2].v_type != VAR_UNKNOWN)
8944 {
8945 static char *(av[]) = {"keep", "force", "error"};
8946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 action = get_tv_string_chk(&argvars[2]);
8948 if (action == NULL)
8949 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008950 for (i = 0; i < 3; ++i)
8951 if (STRCMP(action, av[i]) == 0)
8952 break;
8953 if (i == 3)
8954 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008955 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008956 return;
8957 }
8958 }
8959 else
8960 action = (char_u *)"force";
8961
8962 /* Go over all entries in the second dict and add them to the
8963 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008964 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008966 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008967 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008969 --todo;
8970 di1 = dict_find(d1, hi2->hi_key, -1);
8971 if (di1 == NULL)
8972 {
8973 di1 = dictitem_copy(HI2DI(hi2));
8974 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8975 dictitem_free(di1);
8976 }
8977 else if (*action == 'e')
8978 {
8979 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8980 break;
8981 }
8982 else if (*action == 'f')
8983 {
8984 clear_tv(&di1->di_tv);
8985 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008987 }
8988 }
8989
Bram Moolenaare9a41262005-01-15 22:18:47 +00008990 copy_tv(&argvars[0], rettv);
8991 }
8992 }
8993 else
8994 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995}
8996
8997/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008998 * "feedkeys()" function
8999 */
9000/*ARGSUSED*/
9001 static void
9002f_feedkeys(argvars, rettv)
9003 typval_T *argvars;
9004 typval_T *rettv;
9005{
9006 int remap = TRUE;
9007 char_u *keys, *flags;
9008 char_u nbuf[NUMBUFLEN];
9009 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009010 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009011
9012 rettv->vval.v_number = 0;
9013 keys = get_tv_string(&argvars[0]);
9014 if (*keys != NUL)
9015 {
9016 if (argvars[1].v_type != VAR_UNKNOWN)
9017 {
9018 flags = get_tv_string_buf(&argvars[1], nbuf);
9019 for ( ; *flags != NUL; ++flags)
9020 {
9021 switch (*flags)
9022 {
9023 case 'n': remap = FALSE; break;
9024 case 'm': remap = TRUE; break;
9025 case 't': typed = TRUE; break;
9026 }
9027 }
9028 }
9029
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009030 /* Need to escape K_SPECIAL and CSI before putting the string in the
9031 * typeahead buffer. */
9032 keys_esc = vim_strsave_escape_csi(keys);
9033 if (keys_esc != NULL)
9034 {
9035 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009036 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009037 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009038 if (vgetc_busy)
9039 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009040 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009041 }
9042}
9043
9044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 * "filereadable()" function
9046 */
9047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 typval_T *argvars;
9050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
9052 FILE *fd;
9053 char_u *p;
9054 int n;
9055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9058 {
9059 n = TRUE;
9060 fclose(fd);
9061 }
9062 else
9063 n = FALSE;
9064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066}
9067
9068/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009069 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 * rights to write into.
9071 */
9072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *argvars;
9075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009077 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078}
9079
Bram Moolenaar33570922005-01-25 22:26:29 +00009080static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009081
9082 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009083findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009086 int dir;
9087{
9088#ifdef FEAT_SEARCHPATH
9089 char_u *fname;
9090 char_u *fresult = NULL;
9091 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9092 char_u *p;
9093 char_u pathbuf[NUMBUFLEN];
9094 int count = 1;
9095 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009096 int error = FALSE;
9097#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009098
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009099 rettv->vval.v_string = NULL;
9100 rettv->v_type = VAR_STRING;
9101
9102#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009104
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009105 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009107 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9108 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009109 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 else
9111 {
9112 if (*p != NUL)
9113 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009116 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009118 }
9119
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009120 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9121 error = TRUE;
9122
9123 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 do
9126 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009127 if (rettv->v_type == VAR_STRING)
9128 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 fresult = find_file_in_path_option(first ? fname : NULL,
9130 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009131 0, first, path, dir, NULL,
9132 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009133 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009134
9135 if (fresult != NULL && rettv->v_type == VAR_LIST)
9136 list_append_string(rettv->vval.v_list, fresult, -1);
9137
9138 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009140
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009141 if (rettv->v_type == VAR_STRING)
9142 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009143#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009144}
9145
Bram Moolenaar33570922005-01-25 22:26:29 +00009146static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9147static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009148
9149/*
9150 * Implementation of map() and filter().
9151 */
9152 static void
9153filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009156 int map;
9157{
9158 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009159 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009160 listitem_T *li, *nli;
9161 list_T *l = NULL;
9162 dictitem_T *di;
9163 hashtab_T *ht;
9164 hashitem_T *hi;
9165 dict_T *d = NULL;
9166 typval_T save_val;
9167 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009169 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009170 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009171 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009172
9173 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009174 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009175 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009176 if ((l = argvars[0].vval.v_list) == NULL
9177 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009178 return;
9179 }
9180 else if (argvars[0].v_type == VAR_DICT)
9181 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009182 if ((d = argvars[0].vval.v_dict) == NULL
9183 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009184 return;
9185 }
9186 else
9187 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009188 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009189 return;
9190 }
9191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 expr = get_tv_string_buf_chk(&argvars[1], buf);
9193 /* On type errors, the preceding call has already displayed an error
9194 * message. Avoid a misleading error message for an empty string that
9195 * was not passed as argument. */
9196 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 prepare_vimvar(VV_VAL, &save_val);
9199 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009200
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009201 /* We reset "did_emsg" to be able to detect whether an error
9202 * occurred during evaluation of the expression. */
9203 save_did_emsg = did_emsg;
9204 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 prepare_vimvar(VV_KEY, &save_key);
9209 vimvars[VV_KEY].vv_type = VAR_STRING;
9210
9211 ht = &d->dv_hashtab;
9212 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009213 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009216 if (!HASHITEM_EMPTY(hi))
9217 {
9218 --todo;
9219 di = HI2DI(hi);
9220 if (tv_check_lock(di->di_tv.v_lock, msg))
9221 break;
9222 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009223 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009224 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 break;
9226 if (!map && rem)
9227 dictitem_remove(d, di);
9228 clear_tv(&vimvars[VV_KEY].vv_tv);
9229 }
9230 }
9231 hash_unlock(ht);
9232
9233 restore_vimvar(VV_KEY, &save_key);
9234 }
9235 else
9236 {
9237 for (li = l->lv_first; li != NULL; li = nli)
9238 {
9239 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009240 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009242 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009243 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009244 break;
9245 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009247 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009248 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009251
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009252 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009254
9255 copy_tv(&argvars[0], rettv);
9256}
9257
9258 static int
9259filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009260 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009261 char_u *expr;
9262 int map;
9263 int *remp;
9264{
Bram Moolenaar33570922005-01-25 22:26:29 +00009265 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266 char_u *s;
9267
Bram Moolenaar33570922005-01-25 22:26:29 +00009268 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009269 s = expr;
9270 if (eval1(&s, &rettv, TRUE) == FAIL)
9271 return FAIL;
9272 if (*s != NUL) /* check for trailing chars after expr */
9273 {
9274 EMSG2(_(e_invexpr2), s);
9275 return FAIL;
9276 }
9277 if (map)
9278 {
9279 /* map(): replace the list item value */
9280 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009281 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 *tv = rettv;
9283 }
9284 else
9285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009286 int error = FALSE;
9287
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 /* On type error, nothing has been removed; return FAIL to stop the
9292 * loop. The error message was given by get_tv_number_chk(). */
9293 if (error)
9294 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009295 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009296 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009298}
9299
9300/*
9301 * "filter()" function
9302 */
9303 static void
9304f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009305 typval_T *argvars;
9306 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009307{
9308 filter_map(argvars, rettv, FALSE);
9309}
9310
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312 * "finddir({fname}[, {path}[, {count}]])" function
9313 */
9314 static void
9315f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318{
9319 findfilendir(argvars, rettv, TRUE);
9320}
9321
9322/*
9323 * "findfile({fname}[, {path}[, {count}]])" function
9324 */
9325 static void
9326f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *argvars;
9328 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009329{
9330 findfilendir(argvars, rettv, FALSE);
9331}
9332
9333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 * "fnamemodify({fname}, {mods})" function
9335 */
9336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009338 typval_T *argvars;
9339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 char_u *fname;
9342 char_u *mods;
9343 int usedlen = 0;
9344 int len;
9345 char_u *fbuf = NULL;
9346 char_u buf[NUMBUFLEN];
9347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 fname = get_tv_string_chk(&argvars[0]);
9349 mods = get_tv_string_buf_chk(&argvars[1], buf);
9350 if (fname == NULL || mods == NULL)
9351 fname = NULL;
9352 else
9353 {
9354 len = (int)STRLEN(fname);
9355 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 vim_free(fbuf);
9364}
9365
Bram Moolenaar33570922005-01-25 22:26:29 +00009366static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
9368/*
9369 * "foldclosed()" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 int end;
9376{
9377#ifdef FEAT_FOLDING
9378 linenr_T lnum;
9379 linenr_T first, last;
9380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9383 {
9384 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9385 {
9386 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 return;
9391 }
9392 }
9393#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
9397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009398 * "foldclosed()" function
9399 */
9400 static void
9401f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 typval_T *argvars;
9403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404{
9405 foldclosed_both(argvars, rettv, FALSE);
9406}
9407
9408/*
9409 * "foldclosedend()" function
9410 */
9411 static void
9412f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *argvars;
9414 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009415{
9416 foldclosed_both(argvars, rettv, TRUE);
9417}
9418
9419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 * "foldlevel()" function
9421 */
9422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 typval_T *argvars;
9425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426{
9427#ifdef FEAT_FOLDING
9428 linenr_T lnum;
9429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 else
9434#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436}
9437
9438/*
9439 * "foldtext()" function
9440 */
9441/*ARGSUSED*/
9442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009444 typval_T *argvars;
9445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447#ifdef FEAT_FOLDING
9448 linenr_T lnum;
9449 char_u *s;
9450 char_u *r;
9451 int len;
9452 char *txt;
9453#endif
9454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->v_type = VAR_STRING;
9456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009458 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9459 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9460 <= curbuf->b_ml.ml_line_count
9461 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 {
9463 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009464 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9465 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 if (!linewhite(lnum))
9468 break;
9469 ++lnum;
9470 }
9471
9472 /* Find interesting text in this line. */
9473 s = skipwhite(ml_get(lnum));
9474 /* skip C comment-start */
9475 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009478 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009479 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009480 {
9481 s = skipwhite(ml_get(lnum + 1));
9482 if (*s == '*')
9483 s = skipwhite(s + 1);
9484 }
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 txt = _("+-%s%3ld lines: ");
9487 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009488 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 + 20 /* for %3ld */
9490 + STRLEN(s))); /* concatenated */
9491 if (r != NULL)
9492 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009493 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9494 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9495 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 len = (int)STRLEN(r);
9497 STRCAT(r, s);
9498 /* remove 'foldmarker' and 'commentstring' */
9499 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 }
9502 }
9503#endif
9504}
9505
9506/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009507 * "foldtextresult(lnum)" function
9508 */
9509/*ARGSUSED*/
9510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009512 typval_T *argvars;
9513 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009514{
9515#ifdef FEAT_FOLDING
9516 linenr_T lnum;
9517 char_u *text;
9518 char_u buf[51];
9519 foldinfo_T foldinfo;
9520 int fold_count;
9521#endif
9522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->v_type = VAR_STRING;
9524 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009525#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 /* treat illegal types and illegal string values for {lnum} the same */
9528 if (lnum < 0)
9529 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009530 fold_count = foldedCount(curwin, lnum, &foldinfo);
9531 if (fold_count > 0)
9532 {
9533 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9534 &foldinfo, buf);
9535 if (text == buf)
9536 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009538 }
9539#endif
9540}
9541
9542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 * "foreground()" function
9544 */
9545/*ARGSUSED*/
9546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552#ifdef FEAT_GUI
9553 if (gui.in_use)
9554 gui_mch_set_foreground();
9555#else
9556# ifdef WIN32
9557 win32_set_foreground();
9558# endif
9559#endif
9560}
9561
9562/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009563 * "function()" function
9564 */
9565/*ARGSUSED*/
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009570{
9571 char_u *s;
9572
Bram Moolenaara7043832005-01-21 11:56:39 +00009573 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009575 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009576 EMSG2(_(e_invarg2), s);
9577 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009578 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009579 else
9580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_string = vim_strsave(s);
9582 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009583 }
9584}
9585
9586/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009587 * "garbagecollect()" function
9588 */
9589/*ARGSUSED*/
9590 static void
9591f_garbagecollect(argvars, rettv)
9592 typval_T *argvars;
9593 typval_T *rettv;
9594{
9595 garbage_collect();
9596}
9597
9598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009599 * "get()" function
9600 */
9601 static void
9602f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009603 typval_T *argvars;
9604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009605{
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 listitem_T *li;
9607 list_T *l;
9608 dictitem_T *di;
9609 dict_T *d;
9610 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009611
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009613 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 int error = FALSE;
9617
9618 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9619 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009621 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009622 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 else if (argvars[0].v_type == VAR_DICT)
9624 {
9625 if ((d = argvars[0].vval.v_dict) != NULL)
9626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009627 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628 if (di != NULL)
9629 tv = &di->di_tv;
9630 }
9631 }
9632 else
9633 EMSG2(_(e_listdictarg), "get()");
9634
9635 if (tv == NULL)
9636 {
9637 if (argvars[2].v_type == VAR_UNKNOWN)
9638 rettv->vval.v_number = 0;
9639 else
9640 copy_tv(&argvars[2], rettv);
9641 }
9642 else
9643 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009644}
9645
Bram Moolenaar342337a2005-07-21 21:11:17 +00009646static 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 +00009647
9648/*
9649 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009650 * Return a range (from start to end) of lines in rettv from the specified
9651 * buffer.
9652 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009653 */
9654 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009655get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009656 buf_T *buf;
9657 linenr_T start;
9658 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009659 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009660 typval_T *rettv;
9661{
9662 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009663
Bram Moolenaar342337a2005-07-21 21:11:17 +00009664 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009665 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009666 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009667 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009668 }
9669 else
9670 rettv->vval.v_number = 0;
9671
9672 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9673 return;
9674
9675 if (!retlist)
9676 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009677 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9678 p = ml_get_buf(buf, start, FALSE);
9679 else
9680 p = (char_u *)"";
9681
9682 rettv->v_type = VAR_STRING;
9683 rettv->vval.v_string = vim_strsave(p);
9684 }
9685 else
9686 {
9687 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009688 return;
9689
9690 if (start < 1)
9691 start = 1;
9692 if (end > buf->b_ml.ml_line_count)
9693 end = buf->b_ml.ml_line_count;
9694 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009695 if (list_append_string(rettv->vval.v_list,
9696 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009697 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009698 }
9699}
9700
9701/*
9702 * "getbufline()" function
9703 */
9704 static void
9705f_getbufline(argvars, rettv)
9706 typval_T *argvars;
9707 typval_T *rettv;
9708{
9709 linenr_T lnum;
9710 linenr_T end;
9711 buf_T *buf;
9712
9713 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9714 ++emsg_off;
9715 buf = get_buf_tv(&argvars[0]);
9716 --emsg_off;
9717
Bram Moolenaar661b1822005-07-28 22:36:45 +00009718 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 if (argvars[2].v_type == VAR_UNKNOWN)
9720 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009721 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009722 end = get_tv_lnum_buf(&argvars[2], buf);
9723
Bram Moolenaar342337a2005-07-21 21:11:17 +00009724 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009725}
9726
Bram Moolenaar0d660222005-01-07 21:51:51 +00009727/*
9728 * "getbufvar()" function
9729 */
9730 static void
9731f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 typval_T *argvars;
9733 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009734{
9735 buf_T *buf;
9736 buf_T *save_curbuf;
9737 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9741 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009742 ++emsg_off;
9743 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009744
9745 rettv->v_type = VAR_STRING;
9746 rettv->vval.v_string = NULL;
9747
9748 if (buf != NULL && varname != NULL)
9749 {
9750 if (*varname == '&') /* buffer-local-option */
9751 {
9752 /* set curbuf to be our buf, temporarily */
9753 save_curbuf = curbuf;
9754 curbuf = buf;
9755
9756 get_option_tv(&varname, rettv, TRUE);
9757
9758 /* restore previous notion of curbuf */
9759 curbuf = save_curbuf;
9760 }
9761 else
9762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 if (*varname == NUL)
9764 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9765 * scope prefix before the NUL byte is required by
9766 * find_var_in_ht(). */
9767 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009768 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009769 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009770 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009771 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009772 }
9773 }
9774
9775 --emsg_off;
9776}
9777
9778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 * "getchar()" function
9780 */
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
9789 ++no_mapping;
9790 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009791 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 /* getchar(): blocking wait. */
9793 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 /* getchar(1): only check if char avail */
9796 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009797 else if (error || vpeekc() == NUL)
9798 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 n = 0;
9800 else
9801 /* getchar(0) and char avail: return char */
9802 n = safe_vgetc();
9803 --no_mapping;
9804 --allow_keys;
9805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 if (IS_SPECIAL(n) || mod_mask != 0)
9808 {
9809 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9810 int i = 0;
9811
9812 /* Turn a special key into three bytes, plus modifier. */
9813 if (mod_mask != 0)
9814 {
9815 temp[i++] = K_SPECIAL;
9816 temp[i++] = KS_MODIFIER;
9817 temp[i++] = mod_mask;
9818 }
9819 if (IS_SPECIAL(n))
9820 {
9821 temp[i++] = K_SPECIAL;
9822 temp[i++] = K_SECOND(n);
9823 temp[i++] = K_THIRD(n);
9824 }
9825#ifdef FEAT_MBYTE
9826 else if (has_mbyte)
9827 i += (*mb_char2bytes)(n, temp + i);
9828#endif
9829 else
9830 temp[i++] = n;
9831 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->v_type = VAR_STRING;
9833 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835}
9836
9837/*
9838 * "getcharmod()" function
9839 */
9840/*ARGSUSED*/
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847}
9848
9849/*
9850 * "getcmdline()" function
9851 */
9852/*ARGSUSED*/
9853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009855 typval_T *argvars;
9856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->v_type = VAR_STRING;
9859 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860}
9861
9862/*
9863 * "getcmdpos()" function
9864 */
9865/*ARGSUSED*/
9866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 typval_T *argvars;
9869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009871 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872}
9873
9874/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009875 * "getcmdtype()" function
9876 */
9877/*ARGSUSED*/
9878 static void
9879f_getcmdtype(argvars, rettv)
9880 typval_T *argvars;
9881 typval_T *rettv;
9882{
9883 rettv->v_type = VAR_STRING;
9884 rettv->vval.v_string = alloc(2);
9885 if (rettv->vval.v_string != NULL)
9886 {
9887 rettv->vval.v_string[0] = get_cmdline_type();
9888 rettv->vval.v_string[1] = NUL;
9889 }
9890}
9891
9892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 * "getcwd()" function
9894 */
9895/*ARGSUSED*/
9896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
9901 char_u cwd[MAXPATHL];
9902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 else
9907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009910 if (rettv->vval.v_string != NULL)
9911 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#endif
9913 }
9914}
9915
9916/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009917 * "getfontname()" function
9918 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009919/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009924{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925 rettv->v_type = VAR_STRING;
9926 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009927#ifdef FEAT_GUI
9928 if (gui.in_use)
9929 {
9930 GuiFont font;
9931 char_u *name = NULL;
9932
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009933 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009934 {
9935 /* Get the "Normal" font. Either the name saved by
9936 * hl_set_font_name() or from the font ID. */
9937 font = gui.norm_font;
9938 name = hl_get_font_name();
9939 }
9940 else
9941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009943 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9944 return;
9945 font = gui_mch_get_font(name, FALSE);
9946 if (font == NOFONT)
9947 return; /* Invalid font name, return empty string. */
9948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009950 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009951 gui_mch_free_font(font);
9952 }
9953#endif
9954}
9955
9956/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009957 * "getfperm({fname})" function
9958 */
9959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *argvars;
9962 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009963{
9964 char_u *fname;
9965 struct stat st;
9966 char_u *perm = NULL;
9967 char_u flags[] = "rwx";
9968 int i;
9969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009973 if (mch_stat((char *)fname, &st) >= 0)
9974 {
9975 perm = vim_strsave((char_u *)"---------");
9976 if (perm != NULL)
9977 {
9978 for (i = 0; i < 9; i++)
9979 {
9980 if (st.st_mode & (1 << (8 - i)))
9981 perm[i] = flags[i % 3];
9982 }
9983 }
9984 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009986}
9987
9988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 * "getfsize({fname})" function
9990 */
9991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009993 typval_T *argvars;
9994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995{
9996 char_u *fname;
9997 struct stat st;
9998
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
10003 if (mch_stat((char *)fname, &st) >= 0)
10004 {
10005 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 }
10010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012}
10013
10014/*
10015 * "getftime({fname})" function
10016 */
10017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 typval_T *argvars;
10020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 char_u *fname;
10023 struct stat st;
10024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026
10027 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031}
10032
10033/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010034 * "getftype({fname})" function
10035 */
10036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010038 typval_T *argvars;
10039 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010040{
10041 char_u *fname;
10042 struct stat st;
10043 char_u *type = NULL;
10044 char *t;
10045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010049 if (mch_lstat((char *)fname, &st) >= 0)
10050 {
10051#ifdef S_ISREG
10052 if (S_ISREG(st.st_mode))
10053 t = "file";
10054 else if (S_ISDIR(st.st_mode))
10055 t = "dir";
10056# ifdef S_ISLNK
10057 else if (S_ISLNK(st.st_mode))
10058 t = "link";
10059# endif
10060# ifdef S_ISBLK
10061 else if (S_ISBLK(st.st_mode))
10062 t = "bdev";
10063# endif
10064# ifdef S_ISCHR
10065 else if (S_ISCHR(st.st_mode))
10066 t = "cdev";
10067# endif
10068# ifdef S_ISFIFO
10069 else if (S_ISFIFO(st.st_mode))
10070 t = "fifo";
10071# endif
10072# ifdef S_ISSOCK
10073 else if (S_ISSOCK(st.st_mode))
10074 t = "fifo";
10075# endif
10076 else
10077 t = "other";
10078#else
10079# ifdef S_IFMT
10080 switch (st.st_mode & S_IFMT)
10081 {
10082 case S_IFREG: t = "file"; break;
10083 case S_IFDIR: t = "dir"; break;
10084# ifdef S_IFLNK
10085 case S_IFLNK: t = "link"; break;
10086# endif
10087# ifdef S_IFBLK
10088 case S_IFBLK: t = "bdev"; break;
10089# endif
10090# ifdef S_IFCHR
10091 case S_IFCHR: t = "cdev"; break;
10092# endif
10093# ifdef S_IFIFO
10094 case S_IFIFO: t = "fifo"; break;
10095# endif
10096# ifdef S_IFSOCK
10097 case S_IFSOCK: t = "socket"; break;
10098# endif
10099 default: t = "other";
10100 }
10101# else
10102 if (mch_isdir(fname))
10103 t = "dir";
10104 else
10105 t = "file";
10106# endif
10107#endif
10108 type = vim_strsave((char_u *)t);
10109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010111}
10112
10113/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010114 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010115 */
10116 static void
10117f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010118 typval_T *argvars;
10119 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010120{
10121 linenr_T lnum;
10122 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010123 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010124
10125 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010126 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010127 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010128 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010129 retlist = FALSE;
10130 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010131 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010132 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010133 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010134 retlist = TRUE;
10135 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010136
Bram Moolenaar342337a2005-07-21 21:11:17 +000010137 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010138}
10139
10140/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010141 * "getpos(string)" function
10142 */
10143 static void
10144f_getpos(argvars, rettv)
10145 typval_T *argvars;
10146 typval_T *rettv;
10147{
10148 pos_T *fp;
10149 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010150 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010151
10152 if (rettv_list_alloc(rettv) == OK)
10153 {
10154 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010155 fp = var2fpos(&argvars[0], TRUE, &fnum);
10156 if (fnum != -1)
10157 list_append_number(l, (varnumber_T)fnum);
10158 else
10159 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010160 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10161 : (varnumber_T)0);
10162 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10163 : (varnumber_T)0);
10164 list_append_number(l,
10165#ifdef FEAT_VIRTUALEDIT
10166 (fp != NULL) ? (varnumber_T)fp->coladd :
10167#endif
10168 (varnumber_T)0);
10169 }
10170 else
10171 rettv->vval.v_number = FALSE;
10172}
10173
10174/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010175 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010176 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010177/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010178 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010179f_getqflist(argvars, rettv)
10180 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010181 typval_T *rettv;
10182{
10183#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010184 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010185#endif
10186
10187 rettv->vval.v_number = FALSE;
10188#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010189 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010190 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010191 wp = NULL;
10192 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10193 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010194 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010195 if (wp == NULL)
10196 return;
10197 }
10198
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010199 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010200 }
10201#endif
10202}
10203
10204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 * "getreg()" function
10206 */
10207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 typval_T *argvars;
10210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212 char_u *strregname;
10213 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010214 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010217 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 strregname = get_tv_string_chk(&argvars[0]);
10220 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010221 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 regname = (strregname == NULL ? '"' : *strregname);
10227 if (regname == 0)
10228 regname = '"';
10229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 rettv->vval.v_string = error ? NULL :
10232 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233}
10234
10235/*
10236 * "getregtype()" function
10237 */
10238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010240 typval_T *argvars;
10241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242{
10243 char_u *strregname;
10244 int regname;
10245 char_u buf[NUMBUFLEN + 2];
10246 long reglen = 0;
10247
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010248 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 {
10250 strregname = get_tv_string_chk(&argvars[0]);
10251 if (strregname == NULL) /* type error; errmsg already given */
10252 {
10253 rettv->v_type = VAR_STRING;
10254 rettv->vval.v_string = NULL;
10255 return;
10256 }
10257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 else
10259 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
10262 regname = (strregname == NULL ? '"' : *strregname);
10263 if (regname == 0)
10264 regname = '"';
10265
10266 buf[0] = NUL;
10267 buf[1] = NUL;
10268 switch (get_reg_type(regname, &reglen))
10269 {
10270 case MLINE: buf[0] = 'V'; break;
10271 case MCHAR: buf[0] = 'v'; break;
10272#ifdef FEAT_VISUAL
10273 case MBLOCK:
10274 buf[0] = Ctrl_V;
10275 sprintf((char *)buf + 1, "%ld", reglen + 1);
10276 break;
10277#endif
10278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 rettv->v_type = VAR_STRING;
10280 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281}
10282
10283/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010284 * "gettabwinvar()" function
10285 */
10286 static void
10287f_gettabwinvar(argvars, rettv)
10288 typval_T *argvars;
10289 typval_T *rettv;
10290{
10291 getwinvar(argvars, rettv, 1);
10292}
10293
10294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 * "getwinposx()" function
10296 */
10297/*ARGSUSED*/
10298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010300 typval_T *argvars;
10301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304#ifdef FEAT_GUI
10305 if (gui.in_use)
10306 {
10307 int x, y;
10308
10309 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 }
10312#endif
10313}
10314
10315/*
10316 * "getwinposy()" function
10317 */
10318/*ARGSUSED*/
10319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325#ifdef FEAT_GUI
10326 if (gui.in_use)
10327 {
10328 int x, y;
10329
10330 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 }
10333#endif
10334}
10335
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010336/*
10337 * Find window specifed by "vp" in tabpage "tp".
10338 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010339 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010340find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010341 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010342 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010343{
10344#ifdef FEAT_WINDOWS
10345 win_T *wp;
10346#endif
10347 int nr;
10348
10349 nr = get_tv_number_chk(vp, NULL);
10350
10351#ifdef FEAT_WINDOWS
10352 if (nr < 0)
10353 return NULL;
10354 if (nr == 0)
10355 return curwin;
10356
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010357 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10358 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010359 if (--nr <= 0)
10360 break;
10361 return wp;
10362#else
10363 if (nr == 0 || nr == 1)
10364 return curwin;
10365 return NULL;
10366#endif
10367}
10368
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369/*
10370 * "getwinvar()" function
10371 */
10372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010374 typval_T *argvars;
10375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010377 getwinvar(argvars, rettv, 0);
10378}
10379
10380/*
10381 * getwinvar() and gettabwinvar()
10382 */
10383 static void
10384getwinvar(argvars, rettv, off)
10385 typval_T *argvars;
10386 typval_T *rettv;
10387 int off; /* 1 for gettabwinvar() */
10388{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 win_T *win, *oldcurwin;
10390 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010392 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010394#ifdef FEAT_WINDOWS
10395 if (off == 1)
10396 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10397 else
10398 tp = curtab;
10399#endif
10400 win = find_win_by_nr(&argvars[off], tp);
10401 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404 rettv->v_type = VAR_STRING;
10405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406
10407 if (win != NULL && varname != NULL)
10408 {
10409 if (*varname == '&') /* window-local-option */
10410 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010411 /* Set curwin to be our win, temporarily. Also set curbuf, so
10412 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 oldcurwin = curwin;
10414 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010415 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
10419 /* restore previous notion of curwin */
10420 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010421 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 }
10423 else
10424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 if (*varname == NUL)
10426 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10427 * scope prefix before the NUL byte is required by
10428 * find_var_in_ht(). */
10429 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010431 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 }
10435 }
10436
10437 --emsg_off;
10438}
10439
10440/*
10441 * "glob()" function
10442 */
10443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010445 typval_T *argvars;
10446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
10448 expand_T xpc;
10449
10450 ExpandInit(&xpc);
10451 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->v_type = VAR_STRING;
10453 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455}
10456
10457/*
10458 * "globpath()" function
10459 */
10460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010462 typval_T *argvars;
10463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
10465 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 if (file == NULL)
10470 rettv->vval.v_string = NULL;
10471 else
10472 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473}
10474
10475/*
10476 * "has()" function
10477 */
10478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 typval_T *argvars;
10481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 int i;
10484 char_u *name;
10485 int n = FALSE;
10486 static char *(has_list[]) =
10487 {
10488#ifdef AMIGA
10489 "amiga",
10490# ifdef FEAT_ARP
10491 "arp",
10492# endif
10493#endif
10494#ifdef __BEOS__
10495 "beos",
10496#endif
10497#ifdef MSDOS
10498# ifdef DJGPP
10499 "dos32",
10500# else
10501 "dos16",
10502# endif
10503#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010504#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 "mac",
10506#endif
10507#if defined(MACOS_X_UNIX)
10508 "macunix",
10509#endif
10510#ifdef OS2
10511 "os2",
10512#endif
10513#ifdef __QNX__
10514 "qnx",
10515#endif
10516#ifdef RISCOS
10517 "riscos",
10518#endif
10519#ifdef UNIX
10520 "unix",
10521#endif
10522#ifdef VMS
10523 "vms",
10524#endif
10525#ifdef WIN16
10526 "win16",
10527#endif
10528#ifdef WIN32
10529 "win32",
10530#endif
10531#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10532 "win32unix",
10533#endif
10534#ifdef WIN64
10535 "win64",
10536#endif
10537#ifdef EBCDIC
10538 "ebcdic",
10539#endif
10540#ifndef CASE_INSENSITIVE_FILENAME
10541 "fname_case",
10542#endif
10543#ifdef FEAT_ARABIC
10544 "arabic",
10545#endif
10546#ifdef FEAT_AUTOCMD
10547 "autocmd",
10548#endif
10549#ifdef FEAT_BEVAL
10550 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010551# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10552 "balloon_multiline",
10553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554#endif
10555#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10556 "builtin_terms",
10557# ifdef ALL_BUILTIN_TCAPS
10558 "all_builtin_terms",
10559# endif
10560#endif
10561#ifdef FEAT_BYTEOFF
10562 "byte_offset",
10563#endif
10564#ifdef FEAT_CINDENT
10565 "cindent",
10566#endif
10567#ifdef FEAT_CLIENTSERVER
10568 "clientserver",
10569#endif
10570#ifdef FEAT_CLIPBOARD
10571 "clipboard",
10572#endif
10573#ifdef FEAT_CMDL_COMPL
10574 "cmdline_compl",
10575#endif
10576#ifdef FEAT_CMDHIST
10577 "cmdline_hist",
10578#endif
10579#ifdef FEAT_COMMENTS
10580 "comments",
10581#endif
10582#ifdef FEAT_CRYPT
10583 "cryptv",
10584#endif
10585#ifdef FEAT_CSCOPE
10586 "cscope",
10587#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010588#ifdef CURSOR_SHAPE
10589 "cursorshape",
10590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591#ifdef DEBUG
10592 "debug",
10593#endif
10594#ifdef FEAT_CON_DIALOG
10595 "dialog_con",
10596#endif
10597#ifdef FEAT_GUI_DIALOG
10598 "dialog_gui",
10599#endif
10600#ifdef FEAT_DIFF
10601 "diff",
10602#endif
10603#ifdef FEAT_DIGRAPHS
10604 "digraphs",
10605#endif
10606#ifdef FEAT_DND
10607 "dnd",
10608#endif
10609#ifdef FEAT_EMACS_TAGS
10610 "emacs_tags",
10611#endif
10612 "eval", /* always present, of course! */
10613#ifdef FEAT_EX_EXTRA
10614 "ex_extra",
10615#endif
10616#ifdef FEAT_SEARCH_EXTRA
10617 "extra_search",
10618#endif
10619#ifdef FEAT_FKMAP
10620 "farsi",
10621#endif
10622#ifdef FEAT_SEARCHPATH
10623 "file_in_path",
10624#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010625#if defined(UNIX) && !defined(USE_SYSTEM)
10626 "filterpipe",
10627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628#ifdef FEAT_FIND_ID
10629 "find_in_path",
10630#endif
10631#ifdef FEAT_FOLDING
10632 "folding",
10633#endif
10634#ifdef FEAT_FOOTER
10635 "footer",
10636#endif
10637#if !defined(USE_SYSTEM) && defined(UNIX)
10638 "fork",
10639#endif
10640#ifdef FEAT_GETTEXT
10641 "gettext",
10642#endif
10643#ifdef FEAT_GUI
10644 "gui",
10645#endif
10646#ifdef FEAT_GUI_ATHENA
10647# ifdef FEAT_GUI_NEXTAW
10648 "gui_neXtaw",
10649# else
10650 "gui_athena",
10651# endif
10652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653#ifdef FEAT_GUI_GTK
10654 "gui_gtk",
10655# ifdef HAVE_GTK2
10656 "gui_gtk2",
10657# endif
10658#endif
10659#ifdef FEAT_GUI_MAC
10660 "gui_mac",
10661#endif
10662#ifdef FEAT_GUI_MOTIF
10663 "gui_motif",
10664#endif
10665#ifdef FEAT_GUI_PHOTON
10666 "gui_photon",
10667#endif
10668#ifdef FEAT_GUI_W16
10669 "gui_win16",
10670#endif
10671#ifdef FEAT_GUI_W32
10672 "gui_win32",
10673#endif
10674#ifdef FEAT_HANGULIN
10675 "hangul_input",
10676#endif
10677#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10678 "iconv",
10679#endif
10680#ifdef FEAT_INS_EXPAND
10681 "insert_expand",
10682#endif
10683#ifdef FEAT_JUMPLIST
10684 "jumplist",
10685#endif
10686#ifdef FEAT_KEYMAP
10687 "keymap",
10688#endif
10689#ifdef FEAT_LANGMAP
10690 "langmap",
10691#endif
10692#ifdef FEAT_LIBCALL
10693 "libcall",
10694#endif
10695#ifdef FEAT_LINEBREAK
10696 "linebreak",
10697#endif
10698#ifdef FEAT_LISP
10699 "lispindent",
10700#endif
10701#ifdef FEAT_LISTCMDS
10702 "listcmds",
10703#endif
10704#ifdef FEAT_LOCALMAP
10705 "localmap",
10706#endif
10707#ifdef FEAT_MENU
10708 "menu",
10709#endif
10710#ifdef FEAT_SESSION
10711 "mksession",
10712#endif
10713#ifdef FEAT_MODIFY_FNAME
10714 "modify_fname",
10715#endif
10716#ifdef FEAT_MOUSE
10717 "mouse",
10718#endif
10719#ifdef FEAT_MOUSESHAPE
10720 "mouseshape",
10721#endif
10722#if defined(UNIX) || defined(VMS)
10723# ifdef FEAT_MOUSE_DEC
10724 "mouse_dec",
10725# endif
10726# ifdef FEAT_MOUSE_GPM
10727 "mouse_gpm",
10728# endif
10729# ifdef FEAT_MOUSE_JSB
10730 "mouse_jsbterm",
10731# endif
10732# ifdef FEAT_MOUSE_NET
10733 "mouse_netterm",
10734# endif
10735# ifdef FEAT_MOUSE_PTERM
10736 "mouse_pterm",
10737# endif
10738# ifdef FEAT_MOUSE_XTERM
10739 "mouse_xterm",
10740# endif
10741#endif
10742#ifdef FEAT_MBYTE
10743 "multi_byte",
10744#endif
10745#ifdef FEAT_MBYTE_IME
10746 "multi_byte_ime",
10747#endif
10748#ifdef FEAT_MULTI_LANG
10749 "multi_lang",
10750#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010751#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010752#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010753 "mzscheme",
10754#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756#ifdef FEAT_OLE
10757 "ole",
10758#endif
10759#ifdef FEAT_OSFILETYPE
10760 "osfiletype",
10761#endif
10762#ifdef FEAT_PATH_EXTRA
10763 "path_extra",
10764#endif
10765#ifdef FEAT_PERL
10766#ifndef DYNAMIC_PERL
10767 "perl",
10768#endif
10769#endif
10770#ifdef FEAT_PYTHON
10771#ifndef DYNAMIC_PYTHON
10772 "python",
10773#endif
10774#endif
10775#ifdef FEAT_POSTSCRIPT
10776 "postscript",
10777#endif
10778#ifdef FEAT_PRINTER
10779 "printer",
10780#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010781#ifdef FEAT_PROFILE
10782 "profile",
10783#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010784#ifdef FEAT_RELTIME
10785 "reltime",
10786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787#ifdef FEAT_QUICKFIX
10788 "quickfix",
10789#endif
10790#ifdef FEAT_RIGHTLEFT
10791 "rightleft",
10792#endif
10793#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10794 "ruby",
10795#endif
10796#ifdef FEAT_SCROLLBIND
10797 "scrollbind",
10798#endif
10799#ifdef FEAT_CMDL_INFO
10800 "showcmd",
10801 "cmdline_info",
10802#endif
10803#ifdef FEAT_SIGNS
10804 "signs",
10805#endif
10806#ifdef FEAT_SMARTINDENT
10807 "smartindent",
10808#endif
10809#ifdef FEAT_SNIFF
10810 "sniff",
10811#endif
10812#ifdef FEAT_STL_OPT
10813 "statusline",
10814#endif
10815#ifdef FEAT_SUN_WORKSHOP
10816 "sun_workshop",
10817#endif
10818#ifdef FEAT_NETBEANS_INTG
10819 "netbeans_intg",
10820#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010821#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010822 "spell",
10823#endif
10824#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 "syntax",
10826#endif
10827#if defined(USE_SYSTEM) || !defined(UNIX)
10828 "system",
10829#endif
10830#ifdef FEAT_TAG_BINS
10831 "tag_binary",
10832#endif
10833#ifdef FEAT_TAG_OLDSTATIC
10834 "tag_old_static",
10835#endif
10836#ifdef FEAT_TAG_ANYWHITE
10837 "tag_any_white",
10838#endif
10839#ifdef FEAT_TCL
10840# ifndef DYNAMIC_TCL
10841 "tcl",
10842# endif
10843#endif
10844#ifdef TERMINFO
10845 "terminfo",
10846#endif
10847#ifdef FEAT_TERMRESPONSE
10848 "termresponse",
10849#endif
10850#ifdef FEAT_TEXTOBJ
10851 "textobjects",
10852#endif
10853#ifdef HAVE_TGETENT
10854 "tgetent",
10855#endif
10856#ifdef FEAT_TITLE
10857 "title",
10858#endif
10859#ifdef FEAT_TOOLBAR
10860 "toolbar",
10861#endif
10862#ifdef FEAT_USR_CMDS
10863 "user-commands", /* was accidentally included in 5.4 */
10864 "user_commands",
10865#endif
10866#ifdef FEAT_VIMINFO
10867 "viminfo",
10868#endif
10869#ifdef FEAT_VERTSPLIT
10870 "vertsplit",
10871#endif
10872#ifdef FEAT_VIRTUALEDIT
10873 "virtualedit",
10874#endif
10875#ifdef FEAT_VISUAL
10876 "visual",
10877#endif
10878#ifdef FEAT_VISUALEXTRA
10879 "visualextra",
10880#endif
10881#ifdef FEAT_VREPLACE
10882 "vreplace",
10883#endif
10884#ifdef FEAT_WILDIGN
10885 "wildignore",
10886#endif
10887#ifdef FEAT_WILDMENU
10888 "wildmenu",
10889#endif
10890#ifdef FEAT_WINDOWS
10891 "windows",
10892#endif
10893#ifdef FEAT_WAK
10894 "winaltkeys",
10895#endif
10896#ifdef FEAT_WRITEBACKUP
10897 "writebackup",
10898#endif
10899#ifdef FEAT_XIM
10900 "xim",
10901#endif
10902#ifdef FEAT_XFONTSET
10903 "xfontset",
10904#endif
10905#ifdef USE_XSMP
10906 "xsmp",
10907#endif
10908#ifdef USE_XSMP_INTERACT
10909 "xsmp_interact",
10910#endif
10911#ifdef FEAT_XCLIPBOARD
10912 "xterm_clipboard",
10913#endif
10914#ifdef FEAT_XTERM_SAVE
10915 "xterm_save",
10916#endif
10917#if defined(UNIX) && defined(FEAT_X11)
10918 "X11",
10919#endif
10920 NULL
10921 };
10922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 for (i = 0; has_list[i] != NULL; ++i)
10925 if (STRICMP(name, has_list[i]) == 0)
10926 {
10927 n = TRUE;
10928 break;
10929 }
10930
10931 if (n == FALSE)
10932 {
10933 if (STRNICMP(name, "patch", 5) == 0)
10934 n = has_patch(atoi((char *)name + 5));
10935 else if (STRICMP(name, "vim_starting") == 0)
10936 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010937#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10938 else if (STRICMP(name, "balloon_multiline") == 0)
10939 n = multiline_balloon_available();
10940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941#ifdef DYNAMIC_TCL
10942 else if (STRICMP(name, "tcl") == 0)
10943 n = tcl_enabled(FALSE);
10944#endif
10945#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10946 else if (STRICMP(name, "iconv") == 0)
10947 n = iconv_enabled(FALSE);
10948#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010949#ifdef DYNAMIC_MZSCHEME
10950 else if (STRICMP(name, "mzscheme") == 0)
10951 n = mzscheme_enabled(FALSE);
10952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#ifdef DYNAMIC_RUBY
10954 else if (STRICMP(name, "ruby") == 0)
10955 n = ruby_enabled(FALSE);
10956#endif
10957#ifdef DYNAMIC_PYTHON
10958 else if (STRICMP(name, "python") == 0)
10959 n = python_enabled(FALSE);
10960#endif
10961#ifdef DYNAMIC_PERL
10962 else if (STRICMP(name, "perl") == 0)
10963 n = perl_enabled(FALSE);
10964#endif
10965#ifdef FEAT_GUI
10966 else if (STRICMP(name, "gui_running") == 0)
10967 n = (gui.in_use || gui.starting);
10968# ifdef FEAT_GUI_W32
10969 else if (STRICMP(name, "gui_win32s") == 0)
10970 n = gui_is_win32s();
10971# endif
10972# ifdef FEAT_BROWSE
10973 else if (STRICMP(name, "browse") == 0)
10974 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10975# endif
10976#endif
10977#ifdef FEAT_SYN_HL
10978 else if (STRICMP(name, "syntax_items") == 0)
10979 n = syntax_present(curbuf);
10980#endif
10981#if defined(WIN3264)
10982 else if (STRICMP(name, "win95") == 0)
10983 n = mch_windows95();
10984#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010985#ifdef FEAT_NETBEANS_INTG
10986 else if (STRICMP(name, "netbeans_enabled") == 0)
10987 n = usingNetbeans;
10988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 }
10990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992}
10993
10994/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 * "has_key()" function
10996 */
10997 static void
10998f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 typval_T *argvars;
11000 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001{
11002 rettv->vval.v_number = 0;
11003 if (argvars[0].v_type != VAR_DICT)
11004 {
11005 EMSG(_(e_dictreq));
11006 return;
11007 }
11008 if (argvars[0].vval.v_dict == NULL)
11009 return;
11010
11011 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011012 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013}
11014
11015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 * "hasmapto()" function
11017 */
11018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011020 typval_T *argvars;
11021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022{
11023 char_u *name;
11024 char_u *mode;
11025 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011026 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011029 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 mode = (char_u *)"nvo";
11031 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011034 if (argvars[2].v_type != VAR_UNKNOWN)
11035 abbr = get_tv_number(&argvars[2]);
11036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037
Bram Moolenaar2c932302006-03-18 21:42:09 +000011038 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044/*
11045 * "histadd()" function
11046 */
11047/*ARGSUSED*/
11048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011050 typval_T *argvars;
11051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052{
11053#ifdef FEAT_CMDHIST
11054 int histype;
11055 char_u *str;
11056 char_u buf[NUMBUFLEN];
11057#endif
11058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 if (check_restricted() || check_secure())
11061 return;
11062#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11064 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 if (histype >= 0)
11066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 if (*str != NUL)
11069 {
11070 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 return;
11073 }
11074 }
11075#endif
11076}
11077
11078/*
11079 * "histdel()" function
11080 */
11081/*ARGSUSED*/
11082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011084 typval_T *argvars;
11085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086{
11087#ifdef FEAT_CMDHIST
11088 int n;
11089 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11093 if (str == NULL)
11094 n = 0;
11095 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011098 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 else
11103 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011104 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 get_tv_string_buf(&argvars[1], buf));
11106 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109#endif
11110}
11111
11112/*
11113 * "histget()" function
11114 */
11115/*ARGSUSED*/
11116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 typval_T *argvars;
11119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120{
11121#ifdef FEAT_CMDHIST
11122 int type;
11123 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11127 if (str == NULL)
11128 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 {
11131 type = get_histtype(str);
11132 if (argvars[1].v_type == VAR_UNKNOWN)
11133 idx = get_history_idx(type);
11134 else
11135 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11136 /* -1 on type error */
11137 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143}
11144
11145/*
11146 * "histnr()" function
11147 */
11148/*ARGSUSED*/
11149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011151 typval_T *argvars;
11152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153{
11154 int i;
11155
11156#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157 char_u *history = get_tv_string_chk(&argvars[0]);
11158
11159 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 if (i >= HIST_CMD && i < HIST_COUNT)
11161 i = get_history_idx(i);
11162 else
11163#endif
11164 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166}
11167
11168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 * "highlightID(name)" function
11170 */
11171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 typval_T *argvars;
11174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180 * "highlight_exists()" function
11181 */
11182 static void
11183f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011184 typval_T *argvars;
11185 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011186{
11187 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11188}
11189
11190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 * "hostname()" function
11192 */
11193/*ARGSUSED*/
11194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *argvars;
11197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
11199 char_u hostname[256];
11200
11201 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->v_type = VAR_STRING;
11203 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204}
11205
11206/*
11207 * iconv() function
11208 */
11209/*ARGSUSED*/
11210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T *argvars;
11213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214{
11215#ifdef FEAT_MBYTE
11216 char_u buf1[NUMBUFLEN];
11217 char_u buf2[NUMBUFLEN];
11218 char_u *from, *to, *str;
11219 vimconv_T vimconv;
11220#endif
11221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 rettv->v_type = VAR_STRING;
11223 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
11225#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 str = get_tv_string(&argvars[0]);
11227 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11228 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 vimconv.vc_type = CONV_NONE;
11230 convert_setup(&vimconv, from, to);
11231
11232 /* If the encodings are equal, no conversion needed. */
11233 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237
11238 convert_setup(&vimconv, NULL, NULL);
11239 vim_free(from);
11240 vim_free(to);
11241#endif
11242}
11243
11244/*
11245 * "indent()" function
11246 */
11247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011249 typval_T *argvars;
11250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
11252 linenr_T lnum;
11253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259}
11260
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011261/*
11262 * "index()" function
11263 */
11264 static void
11265f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011268{
Bram Moolenaar33570922005-01-25 22:26:29 +000011269 list_T *l;
11270 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011271 long idx = 0;
11272 int ic = FALSE;
11273
11274 rettv->vval.v_number = -1;
11275 if (argvars[0].v_type != VAR_LIST)
11276 {
11277 EMSG(_(e_listreq));
11278 return;
11279 }
11280 l = argvars[0].vval.v_list;
11281 if (l != NULL)
11282 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011283 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011284 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011286 int error = FALSE;
11287
Bram Moolenaar758711c2005-02-02 23:11:38 +000011288 /* Start at specified item. Use the cached index that list_find()
11289 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011291 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011292 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 ic = get_tv_number_chk(&argvars[3], &error);
11294 if (error)
11295 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011296 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011297
Bram Moolenaar758711c2005-02-02 23:11:38 +000011298 for ( ; item != NULL; item = item->li_next, ++idx)
11299 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011300 {
11301 rettv->vval.v_number = idx;
11302 break;
11303 }
11304 }
11305}
11306
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307static int inputsecret_flag = 0;
11308
11309/*
11310 * "input()" function
11311 * Also handles inputsecret() when inputsecret is set.
11312 */
11313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 char_u *p = NULL;
11320 int c;
11321 char_u buf[NUMBUFLEN];
11322 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011324 int xp_type = EXPAND_NOTHING;
11325 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328
11329#ifdef NO_CONSOLE_INPUT
11330 /* While starting up, there is no place to enter text. */
11331 if (no_console_input())
11332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 return;
11335 }
11336#endif
11337
11338 cmd_silent = FALSE; /* Want to see the prompt. */
11339 if (prompt != NULL)
11340 {
11341 /* Only the part of the message after the last NL is considered as
11342 * prompt for the command line */
11343 p = vim_strrchr(prompt, '\n');
11344 if (p == NULL)
11345 p = prompt;
11346 else
11347 {
11348 ++p;
11349 c = *p;
11350 *p = NUL;
11351 msg_start();
11352 msg_clr_eos();
11353 msg_puts_attr(prompt, echo_attr);
11354 msg_didout = FALSE;
11355 msg_starthere();
11356 *p = c;
11357 }
11358 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 if (argvars[1].v_type != VAR_UNKNOWN)
11361 {
11362 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11363 if (defstr != NULL)
11364 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
Bram Moolenaar4463f292005-09-25 22:20:24 +000011366 if (argvars[2].v_type != VAR_UNKNOWN)
11367 {
11368 char_u *xp_name;
11369 int xp_namelen;
11370 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011371
Bram Moolenaar4463f292005-09-25 22:20:24 +000011372 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011373
Bram Moolenaar4463f292005-09-25 22:20:24 +000011374 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11375 if (xp_name == NULL)
11376 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011377
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011378 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011379
Bram Moolenaar4463f292005-09-25 22:20:24 +000011380 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11381 &xp_arg) == FAIL)
11382 return;
11383 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011384 }
11385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 if (defstr != NULL)
11387 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011388 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11389 xp_type, xp_arg);
11390
11391 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 /* since the user typed this, no need to wait for return */
11394 need_wait_return = FALSE;
11395 msg_didout = FALSE;
11396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 cmd_silent = cmd_silent_save;
11398}
11399
11400/*
11401 * "inputdialog()" function
11402 */
11403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011405 typval_T *argvars;
11406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407{
11408#if defined(FEAT_GUI_TEXTDIALOG)
11409 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11410 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11411 {
11412 char_u *message;
11413 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 message = get_tv_string_chk(&argvars[0]);
11417 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011418 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011419 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 else
11421 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 if (message != NULL && defstr != NULL
11423 && do_dialog(VIM_QUESTION, NULL, message,
11424 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 else
11427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 if (message != NULL && defstr != NULL
11429 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011430 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_string = vim_strsave(
11432 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 }
11438 else
11439#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441}
11442
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011443/*
11444 * "inputlist()" function
11445 */
11446 static void
11447f_inputlist(argvars, rettv)
11448 typval_T *argvars;
11449 typval_T *rettv;
11450{
11451 listitem_T *li;
11452 int selected;
11453 int mouse_used;
11454
11455 rettv->vval.v_number = 0;
11456#ifdef NO_CONSOLE_INPUT
11457 /* While starting up, there is no place to enter text. */
11458 if (no_console_input())
11459 return;
11460#endif
11461 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11462 {
11463 EMSG2(_(e_listarg), "inputlist()");
11464 return;
11465 }
11466
11467 msg_start();
11468 lines_left = Rows; /* avoid more prompt */
11469 msg_scroll = TRUE;
11470 msg_clr_eos();
11471
11472 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11473 {
11474 msg_puts(get_tv_string(&li->li_tv));
11475 msg_putchar('\n');
11476 }
11477
11478 /* Ask for choice. */
11479 selected = prompt_for_number(&mouse_used);
11480 if (mouse_used)
11481 selected -= lines_left;
11482
11483 rettv->vval.v_number = selected;
11484}
11485
11486
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11488
11489/*
11490 * "inputrestore()" function
11491 */
11492/*ARGSUSED*/
11493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *argvars;
11496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497{
11498 if (ga_userinput.ga_len > 0)
11499 {
11500 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11502 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 }
11505 else if (p_verbose > 1)
11506 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011507 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 }
11510}
11511
11512/*
11513 * "inputsave()" function
11514 */
11515/*ARGSUSED*/
11516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011518 typval_T *argvars;
11519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520{
11521 /* Add an entry to the stack of typehead storage. */
11522 if (ga_grow(&ga_userinput, 1) == OK)
11523 {
11524 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11525 + ga_userinput.ga_len);
11526 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 }
11529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531}
11532
11533/*
11534 * "inputsecret()" function
11535 */
11536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011538 typval_T *argvars;
11539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540{
11541 ++cmdline_star;
11542 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 --cmdline_star;
11545 --inputsecret_flag;
11546}
11547
11548/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011549 * "insert()" function
11550 */
11551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 typval_T *argvars;
11554 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011555{
11556 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 listitem_T *item;
11558 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011560
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011561 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011562 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011563 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011564 else if ((l = argvars[0].vval.v_list) != NULL
11565 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011566 {
11567 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 before = get_tv_number_chk(&argvars[2], &error);
11569 if (error)
11570 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011571
Bram Moolenaar758711c2005-02-02 23:11:38 +000011572 if (before == l->lv_len)
11573 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011574 else
11575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011576 item = list_find(l, before);
11577 if (item == NULL)
11578 {
11579 EMSGN(_(e_listidx), before);
11580 l = NULL;
11581 }
11582 }
11583 if (l != NULL)
11584 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011585 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011586 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011587 }
11588 }
11589}
11590
11591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 * "isdirectory()" function
11593 */
11594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 typval_T *argvars;
11597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600}
11601
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011602/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011603 * "islocked()" function
11604 */
11605 static void
11606f_islocked(argvars, rettv)
11607 typval_T *argvars;
11608 typval_T *rettv;
11609{
11610 lval_T lv;
11611 char_u *end;
11612 dictitem_T *di;
11613
11614 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011615 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11616 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011617 if (end != NULL && lv.ll_name != NULL)
11618 {
11619 if (*end != NUL)
11620 EMSG(_(e_trailing));
11621 else
11622 {
11623 if (lv.ll_tv == NULL)
11624 {
11625 if (check_changedtick(lv.ll_name))
11626 rettv->vval.v_number = 1; /* always locked */
11627 else
11628 {
11629 di = find_var(lv.ll_name, NULL);
11630 if (di != NULL)
11631 {
11632 /* Consider a variable locked when:
11633 * 1. the variable itself is locked
11634 * 2. the value of the variable is locked.
11635 * 3. the List or Dict value is locked.
11636 */
11637 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11638 || tv_islocked(&di->di_tv));
11639 }
11640 }
11641 }
11642 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011643 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011644 else if (lv.ll_newkey != NULL)
11645 EMSG2(_(e_dictkey), lv.ll_newkey);
11646 else if (lv.ll_list != NULL)
11647 /* List item. */
11648 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11649 else
11650 /* Dictionary item. */
11651 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11652 }
11653 }
11654
11655 clear_lval(&lv);
11656}
11657
Bram Moolenaar33570922005-01-25 22:26:29 +000011658static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011659
11660/*
11661 * Turn a dict into a list:
11662 * "what" == 0: list of keys
11663 * "what" == 1: list of values
11664 * "what" == 2: list of items
11665 */
11666 static void
11667dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *argvars;
11669 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011670 int what;
11671{
Bram Moolenaar33570922005-01-25 22:26:29 +000011672 list_T *l2;
11673 dictitem_T *di;
11674 hashitem_T *hi;
11675 listitem_T *li;
11676 listitem_T *li2;
11677 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011678 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011679
11680 rettv->vval.v_number = 0;
11681 if (argvars[0].v_type != VAR_DICT)
11682 {
11683 EMSG(_(e_dictreq));
11684 return;
11685 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011686 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011687 return;
11688
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011689 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011690 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011691
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011692 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011693 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011695 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011697 --todo;
11698 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011699
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011700 li = listitem_alloc();
11701 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011702 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011703 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011704
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011705 if (what == 0)
11706 {
11707 /* keys() */
11708 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011709 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011710 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11711 }
11712 else if (what == 1)
11713 {
11714 /* values() */
11715 copy_tv(&di->di_tv, &li->li_tv);
11716 }
11717 else
11718 {
11719 /* items() */
11720 l2 = list_alloc();
11721 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011722 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011723 li->li_tv.vval.v_list = l2;
11724 if (l2 == NULL)
11725 break;
11726 ++l2->lv_refcount;
11727
11728 li2 = listitem_alloc();
11729 if (li2 == NULL)
11730 break;
11731 list_append(l2, li2);
11732 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011733 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011734 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11735
11736 li2 = listitem_alloc();
11737 if (li2 == NULL)
11738 break;
11739 list_append(l2, li2);
11740 copy_tv(&di->di_tv, &li2->li_tv);
11741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011742 }
11743 }
11744}
11745
11746/*
11747 * "items(dict)" function
11748 */
11749 static void
11750f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011751 typval_T *argvars;
11752 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011753{
11754 dict_list(argvars, rettv, 2);
11755}
11756
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011758 * "join()" function
11759 */
11760 static void
11761f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011762 typval_T *argvars;
11763 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011764{
11765 garray_T ga;
11766 char_u *sep;
11767
11768 rettv->vval.v_number = 0;
11769 if (argvars[0].v_type != VAR_LIST)
11770 {
11771 EMSG(_(e_listreq));
11772 return;
11773 }
11774 if (argvars[0].vval.v_list == NULL)
11775 return;
11776 if (argvars[1].v_type == VAR_UNKNOWN)
11777 sep = (char_u *)" ";
11778 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011780
11781 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782
11783 if (sep != NULL)
11784 {
11785 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011786 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 ga_append(&ga, NUL);
11788 rettv->vval.v_string = (char_u *)ga.ga_data;
11789 }
11790 else
11791 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011792}
11793
11794/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011795 * "keys()" function
11796 */
11797 static void
11798f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011799 typval_T *argvars;
11800 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011801{
11802 dict_list(argvars, rettv, 0);
11803}
11804
11805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 * "last_buffer_nr()" function.
11807 */
11808/*ARGSUSED*/
11809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 typval_T *argvars;
11812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813{
11814 int n = 0;
11815 buf_T *buf;
11816
11817 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11818 if (n < buf->b_fnum)
11819 n = buf->b_fnum;
11820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011822}
11823
11824/*
11825 * "len()" function
11826 */
11827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *argvars;
11830 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831{
11832 switch (argvars[0].v_type)
11833 {
11834 case VAR_STRING:
11835 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->vval.v_number = (varnumber_T)STRLEN(
11837 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011838 break;
11839 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011841 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011842 case VAR_DICT:
11843 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11844 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011845 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011846 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 break;
11848 }
11849}
11850
Bram Moolenaar33570922005-01-25 22:26:29 +000011851static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011852
11853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *argvars;
11856 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857 int type;
11858{
11859#ifdef FEAT_LIBCALL
11860 char_u *string_in;
11861 char_u **string_result;
11862 int nr_result;
11863#endif
11864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011866 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011870
11871 if (check_restricted() || check_secure())
11872 return;
11873
11874#ifdef FEAT_LIBCALL
11875 /* The first two args must be strings, otherwise its meaningless */
11876 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011878 string_in = NULL;
11879 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011880 string_in = argvars[2].vval.v_string;
11881 if (type == VAR_NUMBER)
11882 string_result = NULL;
11883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011885 if (mch_libcall(argvars[0].vval.v_string,
11886 argvars[1].vval.v_string,
11887 string_in,
11888 argvars[2].vval.v_number,
11889 string_result,
11890 &nr_result) == OK
11891 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011893 }
11894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
11897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898 * "libcall()" function
11899 */
11900 static void
11901f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904{
11905 libcall_common(argvars, rettv, VAR_STRING);
11906}
11907
11908/*
11909 * "libcallnr()" function
11910 */
11911 static void
11912f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011913 typval_T *argvars;
11914 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011915{
11916 libcall_common(argvars, rettv, VAR_NUMBER);
11917}
11918
11919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 * "line(string)" function
11921 */
11922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011923f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011924 typval_T *argvars;
11925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926{
11927 linenr_T lnum = 0;
11928 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011929 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011931 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 if (fp != NULL)
11933 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935}
11936
11937/*
11938 * "line2byte(lnum)" function
11939 */
11940/*ARGSUSED*/
11941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011943 typval_T *argvars;
11944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945{
11946#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948#else
11949 linenr_T lnum;
11950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11956 if (rettv->vval.v_number >= 0)
11957 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958#endif
11959}
11960
11961/*
11962 * "lispindent(lnum)" function
11963 */
11964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011966 typval_T *argvars;
11967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968{
11969#ifdef FEAT_LISP
11970 pos_T pos;
11971 linenr_T lnum;
11972
11973 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11976 {
11977 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 curwin->w_cursor = pos;
11980 }
11981 else
11982#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984}
11985
11986/*
11987 * "localtime()" function
11988 */
11989/*ARGSUSED*/
11990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011992 typval_T *argvars;
11993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996}
11997
Bram Moolenaar33570922005-01-25 22:26:29 +000011998static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
12000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 int exact;
12005{
12006 char_u *keys;
12007 char_u *which;
12008 char_u buf[NUMBUFLEN];
12009 char_u *keys_buf = NULL;
12010 char_u *rhs;
12011 int mode;
12012 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012013 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014
12015 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->v_type = VAR_STRING;
12017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (*keys == NUL)
12021 return;
12022
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012026 if (argvars[2].v_type != VAR_UNKNOWN)
12027 abbr = get_tv_number(&argvars[2]);
12028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 else
12030 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 if (which == NULL)
12032 return;
12033
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 mode = get_map_mode(&which, 0);
12035
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012036 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012037 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 vim_free(keys_buf);
12039 if (rhs != NULL)
12040 {
12041 ga_init(&ga);
12042 ga.ga_itemsize = 1;
12043 ga.ga_growsize = 40;
12044
12045 while (*rhs != NUL)
12046 ga_concat(&ga, str2special(&rhs, FALSE));
12047
12048 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 }
12051}
12052
12053/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012054 * "map()" function
12055 */
12056 static void
12057f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012058 typval_T *argvars;
12059 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060{
12061 filter_map(argvars, rettv, TRUE);
12062}
12063
12064/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 */
12067 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012069 typval_T *argvars;
12070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073}
12074
12075/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 */
12078 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012080 typval_T *argvars;
12081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084}
12085
Bram Moolenaar33570922005-01-25 22:26:29 +000012086static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087
12088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012090 typval_T *argvars;
12091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 int type;
12093{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012094 char_u *str = NULL;
12095 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 char_u *pat;
12097 regmatch_T regmatch;
12098 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012099 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 char_u *save_cpo;
12101 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012102 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012103 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012104 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 list_T *l = NULL;
12106 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012107 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012108 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109
12110 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12111 save_cpo = p_cpo;
12112 p_cpo = (char_u *)"";
12113
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012114 rettv->vval.v_number = -1;
12115 if (type == 3)
12116 {
12117 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012119 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012120 }
12121 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->v_type = VAR_STRING;
12124 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012127 if (argvars[0].v_type == VAR_LIST)
12128 {
12129 if ((l = argvars[0].vval.v_list) == NULL)
12130 goto theend;
12131 li = l->lv_first;
12132 }
12133 else
12134 expr = str = get_tv_string(&argvars[0]);
12135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012136 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12137 if (pat == NULL)
12138 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012139
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012140 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 int error = FALSE;
12143
12144 start = get_tv_number_chk(&argvars[2], &error);
12145 if (error)
12146 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012147 if (l != NULL)
12148 {
12149 li = list_find(l, start);
12150 if (li == NULL)
12151 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012152 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012153 }
12154 else
12155 {
12156 if (start < 0)
12157 start = 0;
12158 if (start > (long)STRLEN(str))
12159 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012160 /* When "count" argument is there ignore matches before "start",
12161 * otherwise skip part of the string. Differs when pattern is "^"
12162 * or "\<". */
12163 if (argvars[3].v_type != VAR_UNKNOWN)
12164 startcol = start;
12165 else
12166 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012167 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012168
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012169 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 nth = get_tv_number_chk(&argvars[3], &error);
12171 if (error)
12172 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 }
12174
12175 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12176 if (regmatch.regprog != NULL)
12177 {
12178 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012179
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012180 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012181 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012182 if (l != NULL)
12183 {
12184 if (li == NULL)
12185 {
12186 match = FALSE;
12187 break;
12188 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012189 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012190 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012191 if (str == NULL)
12192 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012193 }
12194
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012195 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012196
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012197 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012198 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012199 if (l == NULL && !match)
12200 break;
12201
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012202 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012203 if (l != NULL)
12204 {
12205 li = li->li_next;
12206 ++idx;
12207 }
12208 else
12209 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012210#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012211 startcol = (colnr_T)(regmatch.startp[0]
12212 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012213#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012214 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012215#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012216 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012217 }
12218
12219 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012221 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012223 int i;
12224
12225 /* return list with matched string and submatches */
12226 for (i = 0; i < NSUBEXP; ++i)
12227 {
12228 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012229 {
12230 if (list_append_string(rettv->vval.v_list,
12231 (char_u *)"", 0) == FAIL)
12232 break;
12233 }
12234 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012235 regmatch.startp[i],
12236 (int)(regmatch.endp[i] - regmatch.startp[i]))
12237 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012238 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012239 }
12240 }
12241 else if (type == 2)
12242 {
12243 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012244 if (l != NULL)
12245 copy_tv(&li->li_tv, rettv);
12246 else
12247 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012249 }
12250 else if (l != NULL)
12251 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 else
12253 {
12254 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 (varnumber_T)(regmatch.startp[0] - str);
12257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012260 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 }
12262 }
12263 vim_free(regmatch.regprog);
12264 }
12265
12266theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012267 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 p_cpo = save_cpo;
12269}
12270
12271/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012272 * "match()" function
12273 */
12274 static void
12275f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012276 typval_T *argvars;
12277 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278{
12279 find_some_match(argvars, rettv, 1);
12280}
12281
12282/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012283 * "matcharg()" function
12284 */
12285 static void
12286f_matcharg(argvars, rettv)
12287 typval_T *argvars;
12288 typval_T *rettv;
12289{
12290 if (rettv_list_alloc(rettv) == OK)
12291 {
12292#ifdef FEAT_SEARCH_EXTRA
12293 int mi = get_tv_number(&argvars[0]);
12294
12295 if (mi >= 1 && mi <= 3)
12296 {
12297 list_append_string(rettv->vval.v_list,
12298 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12299 list_append_string(rettv->vval.v_list,
12300 curwin->w_match_pat[mi - 1], -1);
12301 }
12302#endif
12303 }
12304}
12305
12306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012307 * "matchend()" function
12308 */
12309 static void
12310f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012311 typval_T *argvars;
12312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313{
12314 find_some_match(argvars, rettv, 0);
12315}
12316
12317/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012318 * "matchlist()" function
12319 */
12320 static void
12321f_matchlist(argvars, rettv)
12322 typval_T *argvars;
12323 typval_T *rettv;
12324{
12325 find_some_match(argvars, rettv, 3);
12326}
12327
12328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012329 * "matchstr()" function
12330 */
12331 static void
12332f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012333 typval_T *argvars;
12334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335{
12336 find_some_match(argvars, rettv, 2);
12337}
12338
Bram Moolenaar33570922005-01-25 22:26:29 +000012339static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012340
12341 static void
12342max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012343 typval_T *argvars;
12344 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012345 int domax;
12346{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012347 long n = 0;
12348 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012349 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012350
12351 if (argvars[0].v_type == VAR_LIST)
12352 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 list_T *l;
12354 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012355
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012356 l = argvars[0].vval.v_list;
12357 if (l != NULL)
12358 {
12359 li = l->lv_first;
12360 if (li != NULL)
12361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012363 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012364 {
12365 li = li->li_next;
12366 if (li == NULL)
12367 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012369 if (domax ? i > n : i < n)
12370 n = i;
12371 }
12372 }
12373 }
12374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012375 else if (argvars[0].v_type == VAR_DICT)
12376 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012378 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012380 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012381
12382 d = argvars[0].vval.v_dict;
12383 if (d != NULL)
12384 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012385 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012386 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012390 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012392 if (first)
12393 {
12394 n = i;
12395 first = FALSE;
12396 }
12397 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012398 n = i;
12399 }
12400 }
12401 }
12402 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012403 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012404 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012406}
12407
12408/*
12409 * "max()" function
12410 */
12411 static void
12412f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012413 typval_T *argvars;
12414 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012415{
12416 max_min(argvars, rettv, TRUE);
12417}
12418
12419/*
12420 * "min()" function
12421 */
12422 static void
12423f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012424 typval_T *argvars;
12425 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012426{
12427 max_min(argvars, rettv, FALSE);
12428}
12429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012430static int mkdir_recurse __ARGS((char_u *dir, int prot));
12431
12432/*
12433 * Create the directory in which "dir" is located, and higher levels when
12434 * needed.
12435 */
12436 static int
12437mkdir_recurse(dir, prot)
12438 char_u *dir;
12439 int prot;
12440{
12441 char_u *p;
12442 char_u *updir;
12443 int r = FAIL;
12444
12445 /* Get end of directory name in "dir".
12446 * We're done when it's "/" or "c:/". */
12447 p = gettail_sep(dir);
12448 if (p <= get_past_head(dir))
12449 return OK;
12450
12451 /* If the directory exists we're done. Otherwise: create it.*/
12452 updir = vim_strnsave(dir, (int)(p - dir));
12453 if (updir == NULL)
12454 return FAIL;
12455 if (mch_isdir(updir))
12456 r = OK;
12457 else if (mkdir_recurse(updir, prot) == OK)
12458 r = vim_mkdir_emsg(updir, prot);
12459 vim_free(updir);
12460 return r;
12461}
12462
12463#ifdef vim_mkdir
12464/*
12465 * "mkdir()" function
12466 */
12467 static void
12468f_mkdir(argvars, rettv)
12469 typval_T *argvars;
12470 typval_T *rettv;
12471{
12472 char_u *dir;
12473 char_u buf[NUMBUFLEN];
12474 int prot = 0755;
12475
12476 rettv->vval.v_number = FAIL;
12477 if (check_restricted() || check_secure())
12478 return;
12479
12480 dir = get_tv_string_buf(&argvars[0], buf);
12481 if (argvars[1].v_type != VAR_UNKNOWN)
12482 {
12483 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 prot = get_tv_number_chk(&argvars[2], NULL);
12485 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012486 mkdir_recurse(dir, prot);
12487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012488 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012489}
12490#endif
12491
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 * "mode()" function
12494 */
12495/*ARGSUSED*/
12496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012498 typval_T *argvars;
12499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500{
12501 char_u buf[2];
12502
12503#ifdef FEAT_VISUAL
12504 if (VIsual_active)
12505 {
12506 if (VIsual_select)
12507 buf[0] = VIsual_mode + 's' - 'v';
12508 else
12509 buf[0] = VIsual_mode;
12510 }
12511 else
12512#endif
12513 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12514 buf[0] = 'r';
12515 else if (State & INSERT)
12516 {
12517 if (State & REPLACE_FLAG)
12518 buf[0] = 'R';
12519 else
12520 buf[0] = 'i';
12521 }
12522 else if (State & CMDLINE)
12523 buf[0] = 'c';
12524 else
12525 buf[0] = 'n';
12526
12527 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_string = vim_strsave(buf);
12529 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530}
12531
12532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 * "nextnonblank()" function
12534 */
12535 static void
12536f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 typval_T *argvars;
12538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539{
12540 linenr_T lnum;
12541
12542 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545 {
12546 lnum = 0;
12547 break;
12548 }
12549 if (*skipwhite(ml_get(lnum)) != NUL)
12550 break;
12551 }
12552 rettv->vval.v_number = lnum;
12553}
12554
12555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 * "nr2char()" function
12557 */
12558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 typval_T *argvars;
12561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
12563 char_u buf[NUMBUFLEN];
12564
12565#ifdef FEAT_MBYTE
12566 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568 else
12569#endif
12570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 buf[1] = NUL;
12573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012574 rettv->v_type = VAR_STRING;
12575 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012576}
12577
12578/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012579 * "pathshorten()" function
12580 */
12581 static void
12582f_pathshorten(argvars, rettv)
12583 typval_T *argvars;
12584 typval_T *rettv;
12585{
12586 char_u *p;
12587
12588 rettv->v_type = VAR_STRING;
12589 p = get_tv_string_chk(&argvars[0]);
12590 if (p == NULL)
12591 rettv->vval.v_string = NULL;
12592 else
12593 {
12594 p = vim_strsave(p);
12595 rettv->vval.v_string = p;
12596 if (p != NULL)
12597 shorten_dir(p);
12598 }
12599}
12600
12601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012602 * "prevnonblank()" function
12603 */
12604 static void
12605f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012606 typval_T *argvars;
12607 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608{
12609 linenr_T lnum;
12610
12611 lnum = get_tv_lnum(argvars);
12612 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12613 lnum = 0;
12614 else
12615 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12616 --lnum;
12617 rettv->vval.v_number = lnum;
12618}
12619
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012620#ifdef HAVE_STDARG_H
12621/* This dummy va_list is here because:
12622 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12623 * - locally in the function results in a "used before set" warning
12624 * - using va_start() to initialize it gives "function with fixed args" error */
12625static va_list ap;
12626#endif
12627
Bram Moolenaar8c711452005-01-14 21:53:12 +000012628/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012629 * "printf()" function
12630 */
12631 static void
12632f_printf(argvars, rettv)
12633 typval_T *argvars;
12634 typval_T *rettv;
12635{
12636 rettv->v_type = VAR_STRING;
12637 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012638#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012639 {
12640 char_u buf[NUMBUFLEN];
12641 int len;
12642 char_u *s;
12643 int saved_did_emsg = did_emsg;
12644 char *fmt;
12645
12646 /* Get the required length, allocate the buffer and do it for real. */
12647 did_emsg = FALSE;
12648 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012649 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012650 if (!did_emsg)
12651 {
12652 s = alloc(len + 1);
12653 if (s != NULL)
12654 {
12655 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012656 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012657 }
12658 }
12659 did_emsg |= saved_did_emsg;
12660 }
12661#endif
12662}
12663
12664/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012665 * "pumvisible()" function
12666 */
12667/*ARGSUSED*/
12668 static void
12669f_pumvisible(argvars, rettv)
12670 typval_T *argvars;
12671 typval_T *rettv;
12672{
12673 rettv->vval.v_number = 0;
12674#ifdef FEAT_INS_EXPAND
12675 if (pum_visible())
12676 rettv->vval.v_number = 1;
12677#endif
12678}
12679
12680/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012681 * "range()" function
12682 */
12683 static void
12684f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012685 typval_T *argvars;
12686 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012687{
12688 long start;
12689 long end;
12690 long stride = 1;
12691 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012695 if (argvars[1].v_type == VAR_UNKNOWN)
12696 {
12697 end = start - 1;
12698 start = 0;
12699 }
12700 else
12701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012705 }
12706
12707 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 if (error)
12709 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012710 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012711 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012712 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012713 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012714 else
12715 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012716 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012717 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012718 if (list_append_number(rettv->vval.v_list,
12719 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012720 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012721 }
12722}
12723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012724/*
12725 * "readfile()" function
12726 */
12727 static void
12728f_readfile(argvars, rettv)
12729 typval_T *argvars;
12730 typval_T *rettv;
12731{
12732 int binary = FALSE;
12733 char_u *fname;
12734 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012735 listitem_T *li;
12736#define FREAD_SIZE 200 /* optimized for text lines */
12737 char_u buf[FREAD_SIZE];
12738 int readlen; /* size of last fread() */
12739 int buflen; /* nr of valid chars in buf[] */
12740 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12741 int tolist; /* first byte in buf[] still to be put in list */
12742 int chop; /* how many CR to chop off */
12743 char_u *prev = NULL; /* previously read bytes, if any */
12744 int prevlen = 0; /* length of "prev" if not NULL */
12745 char_u *s;
12746 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012747 long maxline = MAXLNUM;
12748 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012749
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012750 if (argvars[1].v_type != VAR_UNKNOWN)
12751 {
12752 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12753 binary = TRUE;
12754 if (argvars[2].v_type != VAR_UNKNOWN)
12755 maxline = get_tv_number(&argvars[2]);
12756 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012757
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012758 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012759 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012760
12761 /* Always open the file in binary mode, library functions have a mind of
12762 * their own about CR-LF conversion. */
12763 fname = get_tv_string(&argvars[0]);
12764 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12765 {
12766 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12767 return;
12768 }
12769
12770 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012771 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012772 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012773 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012774 buflen = filtd + readlen;
12775 tolist = 0;
12776 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12777 {
12778 if (buf[filtd] == '\n' || readlen <= 0)
12779 {
12780 /* Only when in binary mode add an empty list item when the
12781 * last line ends in a '\n'. */
12782 if (!binary && readlen == 0 && filtd == 0)
12783 break;
12784
12785 /* Found end-of-line or end-of-file: add a text line to the
12786 * list. */
12787 chop = 0;
12788 if (!binary)
12789 while (filtd - chop - 1 >= tolist
12790 && buf[filtd - chop - 1] == '\r')
12791 ++chop;
12792 len = filtd - tolist - chop;
12793 if (prev == NULL)
12794 s = vim_strnsave(buf + tolist, len);
12795 else
12796 {
12797 s = alloc((unsigned)(prevlen + len + 1));
12798 if (s != NULL)
12799 {
12800 mch_memmove(s, prev, prevlen);
12801 vim_free(prev);
12802 prev = NULL;
12803 mch_memmove(s + prevlen, buf + tolist, len);
12804 s[prevlen + len] = NUL;
12805 }
12806 }
12807 tolist = filtd + 1;
12808
12809 li = listitem_alloc();
12810 if (li == NULL)
12811 {
12812 vim_free(s);
12813 break;
12814 }
12815 li->li_tv.v_type = VAR_STRING;
12816 li->li_tv.v_lock = 0;
12817 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012818 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012819
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012820 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012821 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012822 if (readlen <= 0)
12823 break;
12824 }
12825 else if (buf[filtd] == NUL)
12826 buf[filtd] = '\n';
12827 }
12828 if (readlen <= 0)
12829 break;
12830
12831 if (tolist == 0)
12832 {
12833 /* "buf" is full, need to move text to an allocated buffer */
12834 if (prev == NULL)
12835 {
12836 prev = vim_strnsave(buf, buflen);
12837 prevlen = buflen;
12838 }
12839 else
12840 {
12841 s = alloc((unsigned)(prevlen + buflen));
12842 if (s != NULL)
12843 {
12844 mch_memmove(s, prev, prevlen);
12845 mch_memmove(s + prevlen, buf, buflen);
12846 vim_free(prev);
12847 prev = s;
12848 prevlen += buflen;
12849 }
12850 }
12851 filtd = 0;
12852 }
12853 else
12854 {
12855 mch_memmove(buf, buf + tolist, buflen - tolist);
12856 filtd -= tolist;
12857 }
12858 }
12859
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012860 /*
12861 * For a negative line count use only the lines at the end of the file,
12862 * free the rest.
12863 */
12864 if (maxline < 0)
12865 while (cnt > -maxline)
12866 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012867 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012868 --cnt;
12869 }
12870
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012871 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012872 fclose(fd);
12873}
12874
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012875#if defined(FEAT_RELTIME)
12876static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12877
12878/*
12879 * Convert a List to proftime_T.
12880 * Return FAIL when there is something wrong.
12881 */
12882 static int
12883list2proftime(arg, tm)
12884 typval_T *arg;
12885 proftime_T *tm;
12886{
12887 long n1, n2;
12888 int error = FALSE;
12889
12890 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12891 || arg->vval.v_list->lv_len != 2)
12892 return FAIL;
12893 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12894 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12895# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012896 tm->HighPart = n1;
12897 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012898# else
12899 tm->tv_sec = n1;
12900 tm->tv_usec = n2;
12901# endif
12902 return error ? FAIL : OK;
12903}
12904#endif /* FEAT_RELTIME */
12905
12906/*
12907 * "reltime()" function
12908 */
12909 static void
12910f_reltime(argvars, rettv)
12911 typval_T *argvars;
12912 typval_T *rettv;
12913{
12914#ifdef FEAT_RELTIME
12915 proftime_T res;
12916 proftime_T start;
12917
12918 if (argvars[0].v_type == VAR_UNKNOWN)
12919 {
12920 /* No arguments: get current time. */
12921 profile_start(&res);
12922 }
12923 else if (argvars[1].v_type == VAR_UNKNOWN)
12924 {
12925 if (list2proftime(&argvars[0], &res) == FAIL)
12926 return;
12927 profile_end(&res);
12928 }
12929 else
12930 {
12931 /* Two arguments: compute the difference. */
12932 if (list2proftime(&argvars[0], &start) == FAIL
12933 || list2proftime(&argvars[1], &res) == FAIL)
12934 return;
12935 profile_sub(&res, &start);
12936 }
12937
12938 if (rettv_list_alloc(rettv) == OK)
12939 {
12940 long n1, n2;
12941
12942# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012943 n1 = res.HighPart;
12944 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012945# else
12946 n1 = res.tv_sec;
12947 n2 = res.tv_usec;
12948# endif
12949 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12950 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12951 }
12952#endif
12953}
12954
12955/*
12956 * "reltimestr()" function
12957 */
12958 static void
12959f_reltimestr(argvars, rettv)
12960 typval_T *argvars;
12961 typval_T *rettv;
12962{
12963#ifdef FEAT_RELTIME
12964 proftime_T tm;
12965#endif
12966
12967 rettv->v_type = VAR_STRING;
12968 rettv->vval.v_string = NULL;
12969#ifdef FEAT_RELTIME
12970 if (list2proftime(&argvars[0], &tm) == OK)
12971 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12972#endif
12973}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012974
Bram Moolenaar0d660222005-01-07 21:51:51 +000012975#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12976static void make_connection __ARGS((void));
12977static int check_connection __ARGS((void));
12978
12979 static void
12980make_connection()
12981{
12982 if (X_DISPLAY == NULL
12983# ifdef FEAT_GUI
12984 && !gui.in_use
12985# endif
12986 )
12987 {
12988 x_force_connect = TRUE;
12989 setup_term_clip();
12990 x_force_connect = FALSE;
12991 }
12992}
12993
12994 static int
12995check_connection()
12996{
12997 make_connection();
12998 if (X_DISPLAY == NULL)
12999 {
13000 EMSG(_("E240: No connection to Vim server"));
13001 return FAIL;
13002 }
13003 return OK;
13004}
13005#endif
13006
13007#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013008static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013009
13010 static void
13011remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013012 typval_T *argvars;
13013 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013014 int expr;
13015{
13016 char_u *server_name;
13017 char_u *keys;
13018 char_u *r = NULL;
13019 char_u buf[NUMBUFLEN];
13020# ifdef WIN32
13021 HWND w;
13022# else
13023 Window w;
13024# endif
13025
13026 if (check_restricted() || check_secure())
13027 return;
13028
13029# ifdef FEAT_X11
13030 if (check_connection() == FAIL)
13031 return;
13032# endif
13033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 server_name = get_tv_string_chk(&argvars[0]);
13035 if (server_name == NULL)
13036 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013037 keys = get_tv_string_buf(&argvars[1], buf);
13038# ifdef WIN32
13039 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13040# else
13041 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13042 < 0)
13043# endif
13044 {
13045 if (r != NULL)
13046 EMSG(r); /* sending worked but evaluation failed */
13047 else
13048 EMSG2(_("E241: Unable to send to %s"), server_name);
13049 return;
13050 }
13051
13052 rettv->vval.v_string = r;
13053
13054 if (argvars[2].v_type != VAR_UNKNOWN)
13055 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013057 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013059
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013060 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013061 v.di_tv.v_type = VAR_STRING;
13062 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013063 idvar = get_tv_string_chk(&argvars[2]);
13064 if (idvar != NULL)
13065 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013067 }
13068}
13069#endif
13070
13071/*
13072 * "remote_expr()" function
13073 */
13074/*ARGSUSED*/
13075 static void
13076f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 typval_T *argvars;
13078 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013079{
13080 rettv->v_type = VAR_STRING;
13081 rettv->vval.v_string = NULL;
13082#ifdef FEAT_CLIENTSERVER
13083 remote_common(argvars, rettv, TRUE);
13084#endif
13085}
13086
13087/*
13088 * "remote_foreground()" function
13089 */
13090/*ARGSUSED*/
13091 static void
13092f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 typval_T *argvars;
13094 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013095{
13096 rettv->vval.v_number = 0;
13097#ifdef FEAT_CLIENTSERVER
13098# ifdef WIN32
13099 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 {
13101 char_u *server_name = get_tv_string_chk(&argvars[0]);
13102
13103 if (server_name != NULL)
13104 serverForeground(server_name);
13105 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013106# else
13107 /* Send a foreground() expression to the server. */
13108 argvars[1].v_type = VAR_STRING;
13109 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13110 argvars[2].v_type = VAR_UNKNOWN;
13111 remote_common(argvars, rettv, TRUE);
13112 vim_free(argvars[1].vval.v_string);
13113# endif
13114#endif
13115}
13116
13117/*ARGSUSED*/
13118 static void
13119f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122{
13123#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013124 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013125 char_u *s = NULL;
13126# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013127 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013129 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013130
13131 if (check_restricted() || check_secure())
13132 {
13133 rettv->vval.v_number = -1;
13134 return;
13135 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013136 serverid = get_tv_string_chk(&argvars[0]);
13137 if (serverid == NULL)
13138 {
13139 rettv->vval.v_number = -1;
13140 return; /* type error; errmsg already given */
13141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013143 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013144 if (n == 0)
13145 rettv->vval.v_number = -1;
13146 else
13147 {
13148 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13149 rettv->vval.v_number = (s != NULL);
13150 }
13151# else
13152 rettv->vval.v_number = 0;
13153 if (check_connection() == FAIL)
13154 return;
13155
13156 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013157 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158# endif
13159
13160 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013162 char_u *retvar;
13163
Bram Moolenaar33570922005-01-25 22:26:29 +000013164 v.di_tv.v_type = VAR_STRING;
13165 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013166 retvar = get_tv_string_chk(&argvars[1]);
13167 if (retvar != NULL)
13168 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013169 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013170 }
13171#else
13172 rettv->vval.v_number = -1;
13173#endif
13174}
13175
13176/*ARGSUSED*/
13177 static void
13178f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 typval_T *argvars;
13180 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013181{
13182 char_u *r = NULL;
13183
13184#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013185 char_u *serverid = get_tv_string_chk(&argvars[0]);
13186
13187 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188 {
13189# ifdef WIN32
13190 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013191 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013193 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013194 if (n != 0)
13195 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13196 if (r == NULL)
13197# else
13198 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200# endif
13201 EMSG(_("E277: Unable to read a server reply"));
13202 }
13203#endif
13204 rettv->v_type = VAR_STRING;
13205 rettv->vval.v_string = r;
13206}
13207
13208/*
13209 * "remote_send()" function
13210 */
13211/*ARGSUSED*/
13212 static void
13213f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 typval_T *argvars;
13215 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013216{
13217 rettv->v_type = VAR_STRING;
13218 rettv->vval.v_string = NULL;
13219#ifdef FEAT_CLIENTSERVER
13220 remote_common(argvars, rettv, FALSE);
13221#endif
13222}
13223
13224/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013225 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013226 */
13227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013231{
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 list_T *l;
13233 listitem_T *item, *item2;
13234 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013235 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013236 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013237 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013238 dict_T *d;
13239 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013240
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013241 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013242 if (argvars[0].v_type == VAR_DICT)
13243 {
13244 if (argvars[2].v_type != VAR_UNKNOWN)
13245 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013246 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013247 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 key = get_tv_string_chk(&argvars[1]);
13250 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013252 di = dict_find(d, key, -1);
13253 if (di == NULL)
13254 EMSG2(_(e_dictkey), key);
13255 else
13256 {
13257 *rettv = di->di_tv;
13258 init_tv(&di->di_tv);
13259 dictitem_remove(d, di);
13260 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013262 }
13263 }
13264 else if (argvars[0].v_type != VAR_LIST)
13265 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013266 else if ((l = argvars[0].vval.v_list) != NULL
13267 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013269 int error = FALSE;
13270
13271 idx = get_tv_number_chk(&argvars[1], &error);
13272 if (error)
13273 ; /* type error: do nothing, errmsg already given */
13274 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013275 EMSGN(_(e_listidx), idx);
13276 else
13277 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013278 if (argvars[2].v_type == VAR_UNKNOWN)
13279 {
13280 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013281 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013282 *rettv = item->li_tv;
13283 vim_free(item);
13284 }
13285 else
13286 {
13287 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288 end = get_tv_number_chk(&argvars[2], &error);
13289 if (error)
13290 ; /* type error: do nothing */
13291 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013292 EMSGN(_(e_listidx), end);
13293 else
13294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013295 int cnt = 0;
13296
13297 for (li = item; li != NULL; li = li->li_next)
13298 {
13299 ++cnt;
13300 if (li == item2)
13301 break;
13302 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013303 if (li == NULL) /* didn't find "item2" after "item" */
13304 EMSG(_(e_invrange));
13305 else
13306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013307 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013308 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013309 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013310 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013311 l->lv_first = item;
13312 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013313 item->li_prev = NULL;
13314 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013315 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013316 }
13317 }
13318 }
13319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013320 }
13321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322}
13323
13324/*
13325 * "rename({from}, {to})" function
13326 */
13327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 typval_T *argvars;
13330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
13332 char_u buf[NUMBUFLEN];
13333
13334 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13338 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339}
13340
13341/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013342 * "repeat()" function
13343 */
13344/*ARGSUSED*/
13345 static void
13346f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *argvars;
13348 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013349{
13350 char_u *p;
13351 int n;
13352 int slen;
13353 int len;
13354 char_u *r;
13355 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013356
13357 n = get_tv_number(&argvars[1]);
13358 if (argvars[0].v_type == VAR_LIST)
13359 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013360 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013361 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013362 if (list_extend(rettv->vval.v_list,
13363 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013364 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013365 }
13366 else
13367 {
13368 p = get_tv_string(&argvars[0]);
13369 rettv->v_type = VAR_STRING;
13370 rettv->vval.v_string = NULL;
13371
13372 slen = (int)STRLEN(p);
13373 len = slen * n;
13374 if (len <= 0)
13375 return;
13376
13377 r = alloc(len + 1);
13378 if (r != NULL)
13379 {
13380 for (i = 0; i < n; i++)
13381 mch_memmove(r + i * slen, p, (size_t)slen);
13382 r[len] = NUL;
13383 }
13384
13385 rettv->vval.v_string = r;
13386 }
13387}
13388
13389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 * "resolve()" function
13391 */
13392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013393f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013394 typval_T *argvars;
13395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396{
13397 char_u *p;
13398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400#ifdef FEAT_SHORTCUT
13401 {
13402 char_u *v = NULL;
13403
13404 v = mch_resolve_shortcut(p);
13405 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 }
13410#else
13411# ifdef HAVE_READLINK
13412 {
13413 char_u buf[MAXPATHL + 1];
13414 char_u *cpy;
13415 int len;
13416 char_u *remain = NULL;
13417 char_u *q;
13418 int is_relative_to_current = FALSE;
13419 int has_trailing_pathsep = FALSE;
13420 int limit = 100;
13421
13422 p = vim_strsave(p);
13423
13424 if (p[0] == '.' && (vim_ispathsep(p[1])
13425 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13426 is_relative_to_current = TRUE;
13427
13428 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013429 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 has_trailing_pathsep = TRUE;
13431
13432 q = getnextcomp(p);
13433 if (*q != NUL)
13434 {
13435 /* Separate the first path component in "p", and keep the
13436 * remainder (beginning with the path separator). */
13437 remain = vim_strsave(q - 1);
13438 q[-1] = NUL;
13439 }
13440
13441 for (;;)
13442 {
13443 for (;;)
13444 {
13445 len = readlink((char *)p, (char *)buf, MAXPATHL);
13446 if (len <= 0)
13447 break;
13448 buf[len] = NUL;
13449
13450 if (limit-- == 0)
13451 {
13452 vim_free(p);
13453 vim_free(remain);
13454 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 goto fail;
13457 }
13458
13459 /* Ensure that the result will have a trailing path separator
13460 * if the argument has one. */
13461 if (remain == NULL && has_trailing_pathsep)
13462 add_pathsep(buf);
13463
13464 /* Separate the first path component in the link value and
13465 * concatenate the remainders. */
13466 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13467 if (*q != NUL)
13468 {
13469 if (remain == NULL)
13470 remain = vim_strsave(q - 1);
13471 else
13472 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013473 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 if (cpy != NULL)
13475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 vim_free(remain);
13477 remain = cpy;
13478 }
13479 }
13480 q[-1] = NUL;
13481 }
13482
13483 q = gettail(p);
13484 if (q > p && *q == NUL)
13485 {
13486 /* Ignore trailing path separator. */
13487 q[-1] = NUL;
13488 q = gettail(p);
13489 }
13490 if (q > p && !mch_isFullName(buf))
13491 {
13492 /* symlink is relative to directory of argument */
13493 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13494 if (cpy != NULL)
13495 {
13496 STRCPY(cpy, p);
13497 STRCPY(gettail(cpy), buf);
13498 vim_free(p);
13499 p = cpy;
13500 }
13501 }
13502 else
13503 {
13504 vim_free(p);
13505 p = vim_strsave(buf);
13506 }
13507 }
13508
13509 if (remain == NULL)
13510 break;
13511
13512 /* Append the first path component of "remain" to "p". */
13513 q = getnextcomp(remain + 1);
13514 len = q - remain - (*q != NUL);
13515 cpy = vim_strnsave(p, STRLEN(p) + len);
13516 if (cpy != NULL)
13517 {
13518 STRNCAT(cpy, remain, len);
13519 vim_free(p);
13520 p = cpy;
13521 }
13522 /* Shorten "remain". */
13523 if (*q != NUL)
13524 STRCPY(remain, q - 1);
13525 else
13526 {
13527 vim_free(remain);
13528 remain = NULL;
13529 }
13530 }
13531
13532 /* If the result is a relative path name, make it explicitly relative to
13533 * the current directory if and only if the argument had this form. */
13534 if (!vim_ispathsep(*p))
13535 {
13536 if (is_relative_to_current
13537 && *p != NUL
13538 && !(p[0] == '.'
13539 && (p[1] == NUL
13540 || vim_ispathsep(p[1])
13541 || (p[1] == '.'
13542 && (p[2] == NUL
13543 || vim_ispathsep(p[2]))))))
13544 {
13545 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013546 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 if (cpy != NULL)
13548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 vim_free(p);
13550 p = cpy;
13551 }
13552 }
13553 else if (!is_relative_to_current)
13554 {
13555 /* Strip leading "./". */
13556 q = p;
13557 while (q[0] == '.' && vim_ispathsep(q[1]))
13558 q += 2;
13559 if (q > p)
13560 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13561 }
13562 }
13563
13564 /* Ensure that the result will have no trailing path separator
13565 * if the argument had none. But keep "/" or "//". */
13566 if (!has_trailing_pathsep)
13567 {
13568 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013569 if (after_pathsep(p, q))
13570 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 }
13572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 }
13575# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577# endif
13578#endif
13579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
13582#ifdef HAVE_READLINK
13583fail:
13584#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013589 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 */
13591 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 list_T *l;
13597 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598
Bram Moolenaar0d660222005-01-07 21:51:51 +000013599 rettv->vval.v_number = 0;
13600 if (argvars[0].v_type != VAR_LIST)
13601 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013602 else if ((l = argvars[0].vval.v_list) != NULL
13603 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013604 {
13605 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013606 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013607 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013608 while (li != NULL)
13609 {
13610 ni = li->li_prev;
13611 list_append(l, li);
13612 li = ni;
13613 }
13614 rettv->vval.v_list = l;
13615 rettv->v_type = VAR_LIST;
13616 ++l->lv_refcount;
13617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013620#define SP_NOMOVE 0x01 /* don't move cursor */
13621#define SP_REPEAT 0x02 /* repeat to find outer pair */
13622#define SP_RETCOUNT 0x04 /* return matchcount */
13623#define SP_SETPCMARK 0x08 /* set previous context mark */
13624#define SP_START 0x10 /* accept match at start position */
13625#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13626#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013627
Bram Moolenaar33570922005-01-25 22:26:29 +000013628static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013629
13630/*
13631 * Get flags for a search function.
13632 * Possibly sets "p_ws".
13633 * Returns BACKWARD, FORWARD or zero (for an error).
13634 */
13635 static int
13636get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638 int *flagsp;
13639{
13640 int dir = FORWARD;
13641 char_u *flags;
13642 char_u nbuf[NUMBUFLEN];
13643 int mask;
13644
13645 if (varp->v_type != VAR_UNKNOWN)
13646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013647 flags = get_tv_string_buf_chk(varp, nbuf);
13648 if (flags == NULL)
13649 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013650 while (*flags != NUL)
13651 {
13652 switch (*flags)
13653 {
13654 case 'b': dir = BACKWARD; break;
13655 case 'w': p_ws = TRUE; break;
13656 case 'W': p_ws = FALSE; break;
13657 default: mask = 0;
13658 if (flagsp != NULL)
13659 switch (*flags)
13660 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013661 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013662 case 'e': mask = SP_END; break;
13663 case 'm': mask = SP_RETCOUNT; break;
13664 case 'n': mask = SP_NOMOVE; break;
13665 case 'p': mask = SP_SUBPAT; break;
13666 case 'r': mask = SP_REPEAT; break;
13667 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013668 }
13669 if (mask == 0)
13670 {
13671 EMSG2(_(e_invarg2), flags);
13672 dir = 0;
13673 }
13674 else
13675 *flagsp |= mask;
13676 }
13677 if (dir == 0)
13678 break;
13679 ++flags;
13680 }
13681 }
13682 return dir;
13683}
13684
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013686 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013688 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013689search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013691 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013692 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013694 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 char_u *pat;
13696 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013697 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 int save_p_ws = p_ws;
13699 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013700 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013701 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013702 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013703 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013706 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013707 if (dir == 0)
13708 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013709 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013710 if (flags & SP_START)
13711 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013712 if (flags & SP_END)
13713 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013714
13715 /* Optional extra argument: line number to stop searching. */
13716 if (argvars[1].v_type != VAR_UNKNOWN
13717 && argvars[2].v_type != VAR_UNKNOWN)
13718 {
13719 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13720 if (lnum_stop < 0)
13721 goto theend;
13722 }
13723
Bram Moolenaar231334e2005-07-25 20:46:57 +000013724 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013725 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013726 * Check to make sure only those flags are set.
13727 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13728 * flags cannot be set. Check for that condition also.
13729 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013730 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013731 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013734 goto theend;
13735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013737 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013738 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13739 options, RE_SEARCH, (linenr_T)lnum_stop);
13740 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013742 if (flags & SP_SUBPAT)
13743 retval = subpatnum;
13744 else
13745 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013746 if (flags & SP_SETPCMARK)
13747 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013749 if (match_pos != NULL)
13750 {
13751 /* Store the match cursor position */
13752 match_pos->lnum = pos.lnum;
13753 match_pos->col = pos.col + 1;
13754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 /* "/$" will put the cursor after the end of the line, may need to
13756 * correct that here */
13757 check_cursor();
13758 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013759
13760 /* If 'n' flag is used: restore cursor position. */
13761 if (flags & SP_NOMOVE)
13762 curwin->w_cursor = save_cursor;
13763theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013765
13766 return retval;
13767}
13768
13769/*
13770 * "search()" function
13771 */
13772 static void
13773f_search(argvars, rettv)
13774 typval_T *argvars;
13775 typval_T *rettv;
13776{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013777 int flags = 0;
13778
13779 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780}
13781
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013783 * "searchdecl()" function
13784 */
13785 static void
13786f_searchdecl(argvars, rettv)
13787 typval_T *argvars;
13788 typval_T *rettv;
13789{
13790 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013791 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013792 int error = FALSE;
13793 char_u *name;
13794
13795 rettv->vval.v_number = 1; /* default: FAIL */
13796
13797 name = get_tv_string_chk(&argvars[0]);
13798 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013799 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013800 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013801 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13802 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13803 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013804 if (!error && name != NULL)
13805 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013806 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013807}
13808
13809/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013810 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013812 static int
13813searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013815 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
13817 char_u *spat, *mpat, *epat;
13818 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 int dir;
13821 int flags = 0;
13822 char_u nbuf1[NUMBUFLEN];
13823 char_u nbuf2[NUMBUFLEN];
13824 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013825 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013826 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 spat = get_tv_string_chk(&argvars[0]);
13830 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13831 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13832 if (spat == NULL || mpat == NULL || epat == NULL)
13833 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 /* Handle the optional fourth argument: flags */
13836 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013837 if (dir == 0)
13838 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013839
13840 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013841 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13842 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013843 if ((flags & (SP_END | SP_SUBPAT)) != 0
13844 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013845 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013846 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013847 goto theend;
13848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013850 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013851 if (argvars[3].v_type == VAR_UNKNOWN
13852 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 skip = (char_u *)"";
13854 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013857 if (argvars[5].v_type != VAR_UNKNOWN)
13858 {
13859 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13860 if (lnum_stop < 0)
13861 goto theend;
13862 }
13863 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 if (skip == NULL)
13865 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013867 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13868 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013869
13870theend:
13871 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013872
13873 return retval;
13874}
13875
13876/*
13877 * "searchpair()" function
13878 */
13879 static void
13880f_searchpair(argvars, rettv)
13881 typval_T *argvars;
13882 typval_T *rettv;
13883{
13884 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13885}
13886
13887/*
13888 * "searchpairpos()" function
13889 */
13890 static void
13891f_searchpairpos(argvars, rettv)
13892 typval_T *argvars;
13893 typval_T *rettv;
13894{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013895 pos_T match_pos;
13896 int lnum = 0;
13897 int col = 0;
13898
13899 rettv->vval.v_number = 0;
13900
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013901 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013902 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013903
13904 if (searchpair_cmn(argvars, &match_pos) > 0)
13905 {
13906 lnum = match_pos.lnum;
13907 col = match_pos.col;
13908 }
13909
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013910 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13911 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013912}
13913
13914/*
13915 * Search for a start/middle/end thing.
13916 * Used by searchpair(), see its documentation for the details.
13917 * Returns 0 or -1 for no match,
13918 */
13919 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013920do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013921 char_u *spat; /* start pattern */
13922 char_u *mpat; /* middle pattern */
13923 char_u *epat; /* end pattern */
13924 int dir; /* BACKWARD or FORWARD */
13925 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013926 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013927 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013928 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013929{
13930 char_u *save_cpo;
13931 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13932 long retval = 0;
13933 pos_T pos;
13934 pos_T firstpos;
13935 pos_T foundpos;
13936 pos_T save_cursor;
13937 pos_T save_pos;
13938 int n;
13939 int r;
13940 int nest = 1;
13941 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013942 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013943
13944 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13945 save_cpo = p_cpo;
13946 p_cpo = (char_u *)"";
13947
13948 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13949 * start/middle/end (pat3, for the top pair). */
13950 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13951 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13952 if (pat2 == NULL || pat3 == NULL)
13953 goto theend;
13954 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13955 if (*mpat == NUL)
13956 STRCPY(pat3, pat2);
13957 else
13958 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13959 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013960 if (flags & SP_START)
13961 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013962
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 save_cursor = curwin->w_cursor;
13964 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013965 clearpos(&firstpos);
13966 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 pat = pat3;
13968 for (;;)
13969 {
13970 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013971 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13973 /* didn't find it or found the first match again: FAIL */
13974 break;
13975
13976 if (firstpos.lnum == 0)
13977 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013978 if (equalpos(pos, foundpos))
13979 {
13980 /* Found the same position again. Can happen with a pattern that
13981 * has "\zs" at the end and searching backwards. Advance one
13982 * character and try again. */
13983 if (dir == BACKWARD)
13984 decl(&pos);
13985 else
13986 incl(&pos);
13987 }
13988 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989
13990 /* If the skip pattern matches, ignore this match. */
13991 if (*skip != NUL)
13992 {
13993 save_pos = curwin->w_cursor;
13994 curwin->w_cursor = pos;
13995 r = eval_to_bool(skip, &err, NULL, FALSE);
13996 curwin->w_cursor = save_pos;
13997 if (err)
13998 {
13999 /* Evaluating {skip} caused an error, break here. */
14000 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014001 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 break;
14003 }
14004 if (r)
14005 continue;
14006 }
14007
14008 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14009 {
14010 /* Found end when searching backwards or start when searching
14011 * forward: nested pair. */
14012 ++nest;
14013 pat = pat2; /* nested, don't search for middle */
14014 }
14015 else
14016 {
14017 /* Found end when searching forward or start when searching
14018 * backward: end of (nested) pair; or found middle in outer pair. */
14019 if (--nest == 1)
14020 pat = pat3; /* outer level, search for middle */
14021 }
14022
14023 if (nest == 0)
14024 {
14025 /* Found the match: return matchcount or line number. */
14026 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014027 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014029 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014030 if (flags & SP_SETPCMARK)
14031 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 curwin->w_cursor = pos;
14033 if (!(flags & SP_REPEAT))
14034 break;
14035 nest = 1; /* search for next unmatched */
14036 }
14037 }
14038
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014039 if (match_pos != NULL)
14040 {
14041 /* Store the match cursor position */
14042 match_pos->lnum = curwin->w_cursor.lnum;
14043 match_pos->col = curwin->w_cursor.col + 1;
14044 }
14045
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014047 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 curwin->w_cursor = save_cursor;
14049
14050theend:
14051 vim_free(pat2);
14052 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014054
14055 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056}
14057
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014058/*
14059 * "searchpos()" function
14060 */
14061 static void
14062f_searchpos(argvars, rettv)
14063 typval_T *argvars;
14064 typval_T *rettv;
14065{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014066 pos_T match_pos;
14067 int lnum = 0;
14068 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014069 int n;
14070 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014071
14072 rettv->vval.v_number = 0;
14073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014074 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014075 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014076
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014077 n = search_cmn(argvars, &match_pos, &flags);
14078 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014079 {
14080 lnum = match_pos.lnum;
14081 col = match_pos.col;
14082 }
14083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014084 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14085 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014086 if (flags & SP_SUBPAT)
14087 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014088}
14089
14090
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091/*ARGSUSED*/
14092 static void
14093f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014094 typval_T *argvars;
14095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097#ifdef FEAT_CLIENTSERVER
14098 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 char_u *server = get_tv_string_chk(&argvars[0]);
14100 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101
Bram Moolenaar0d660222005-01-07 21:51:51 +000014102 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 if (server == NULL || reply == NULL)
14104 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105 if (check_restricted() || check_secure())
14106 return;
14107# ifdef FEAT_X11
14108 if (check_connection() == FAIL)
14109 return;
14110# endif
14111
14112 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 EMSG(_("E258: Unable to send to client"));
14115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014117 rettv->vval.v_number = 0;
14118#else
14119 rettv->vval.v_number = -1;
14120#endif
14121}
14122
14123/*ARGSUSED*/
14124 static void
14125f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *argvars;
14127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014128{
14129 char_u *r = NULL;
14130
14131#ifdef FEAT_CLIENTSERVER
14132# ifdef WIN32
14133 r = serverGetVimNames();
14134# else
14135 make_connection();
14136 if (X_DISPLAY != NULL)
14137 r = serverGetVimNames(X_DISPLAY);
14138# endif
14139#endif
14140 rettv->v_type = VAR_STRING;
14141 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142}
14143
14144/*
14145 * "setbufvar()" function
14146 */
14147/*ARGSUSED*/
14148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014150 typval_T *argvars;
14151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152{
14153 buf_T *buf;
14154#ifdef FEAT_AUTOCMD
14155 aco_save_T aco;
14156#else
14157 buf_T *save_curbuf;
14158#endif
14159 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 char_u nbuf[NUMBUFLEN];
14162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014163 rettv->vval.v_number = 0;
14164
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165 if (check_restricted() || check_secure())
14166 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14168 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014169 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 varp = &argvars[2];
14171
14172 if (buf != NULL && varname != NULL && varp != NULL)
14173 {
14174 /* set curbuf to be our buf, temporarily */
14175#ifdef FEAT_AUTOCMD
14176 aucmd_prepbuf(&aco, buf);
14177#else
14178 save_curbuf = curbuf;
14179 curbuf = buf;
14180#endif
14181
14182 if (*varname == '&')
14183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 long numval;
14185 char_u *strval;
14186 int error = FALSE;
14187
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 numval = get_tv_number_chk(varp, &error);
14190 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 if (!error && strval != NULL)
14192 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 }
14194 else
14195 {
14196 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14197 if (bufvarname != NULL)
14198 {
14199 STRCPY(bufvarname, "b:");
14200 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014201 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 vim_free(bufvarname);
14203 }
14204 }
14205
14206 /* reset notion of buffer */
14207#ifdef FEAT_AUTOCMD
14208 aucmd_restbuf(&aco);
14209#else
14210 curbuf = save_curbuf;
14211#endif
14212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213}
14214
14215/*
14216 * "setcmdpos()" function
14217 */
14218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014220 typval_T *argvars;
14221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 int pos = (int)get_tv_number(&argvars[0]) - 1;
14224
14225 if (pos >= 0)
14226 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229/*
14230 * "setline()" function
14231 */
14232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014234 typval_T *argvars;
14235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236{
14237 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014238 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014239 list_T *l = NULL;
14240 listitem_T *li = NULL;
14241 long added = 0;
14242 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014244 lnum = get_tv_lnum(&argvars[0]);
14245 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014247 l = argvars[1].vval.v_list;
14248 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014250 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014252
14253 rettv->vval.v_number = 0; /* OK */
14254 for (;;)
14255 {
14256 if (l != NULL)
14257 {
14258 /* list argument, get next string */
14259 if (li == NULL)
14260 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014262 li = li->li_next;
14263 }
14264
14265 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014267 break;
14268 if (lnum <= curbuf->b_ml.ml_line_count)
14269 {
14270 /* existing line, replace it */
14271 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14272 {
14273 changed_bytes(lnum, 0);
14274 check_cursor_col();
14275 rettv->vval.v_number = 0; /* OK */
14276 }
14277 }
14278 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14279 {
14280 /* lnum is one past the last line, append the line */
14281 ++added;
14282 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14283 rettv->vval.v_number = 0; /* OK */
14284 }
14285
14286 if (l == NULL) /* only one string argument */
14287 break;
14288 ++lnum;
14289 }
14290
14291 if (added > 0)
14292 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293}
14294
14295/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014296 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014297 */
14298/*ARGSUSED*/
14299 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014300set_qf_ll_list(wp, list_arg, action_arg, rettv)
14301 win_T *wp;
14302 typval_T *list_arg;
14303 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014304 typval_T *rettv;
14305{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014306#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014307 char_u *act;
14308 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014309#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014310
Bram Moolenaar2641f772005-03-25 21:58:17 +000014311 rettv->vval.v_number = -1;
14312
14313#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014314 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014315 EMSG(_(e_listreq));
14316 else
14317 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014318 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014319
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014320 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014321 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014322 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 if (act == NULL)
14324 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014325 if (*act == 'a' || *act == 'r')
14326 action = *act;
14327 }
14328
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014329 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014330 rettv->vval.v_number = 0;
14331 }
14332#endif
14333}
14334
14335/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014336 * "setloclist()" function
14337 */
14338/*ARGSUSED*/
14339 static void
14340f_setloclist(argvars, rettv)
14341 typval_T *argvars;
14342 typval_T *rettv;
14343{
14344 win_T *win;
14345
14346 rettv->vval.v_number = -1;
14347
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014348 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014349 if (win != NULL)
14350 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14351}
14352
14353/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014354 * "setpos()" function
14355 */
14356/*ARGSUSED*/
14357 static void
14358f_setpos(argvars, rettv)
14359 typval_T *argvars;
14360 typval_T *rettv;
14361{
14362 pos_T pos;
14363 int fnum;
14364 char_u *name;
14365
14366 name = get_tv_string_chk(argvars);
14367 if (name != NULL)
14368 {
14369 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14370 {
14371 --pos.col;
14372 if (name[0] == '.') /* cursor */
14373 {
14374 if (fnum == curbuf->b_fnum)
14375 {
14376 curwin->w_cursor = pos;
14377 check_cursor();
14378 }
14379 else
14380 EMSG(_(e_invarg));
14381 }
14382 else if (name[0] == '\'') /* mark */
14383 (void)setmark_pos(name[1], &pos, fnum);
14384 else
14385 EMSG(_(e_invarg));
14386 }
14387 }
14388}
14389
14390/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014391 * "setqflist()" function
14392 */
14393/*ARGSUSED*/
14394 static void
14395f_setqflist(argvars, rettv)
14396 typval_T *argvars;
14397 typval_T *rettv;
14398{
14399 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14400}
14401
14402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 * "setreg()" function
14404 */
14405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *argvars;
14408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409{
14410 int regname;
14411 char_u *strregname;
14412 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 int append;
14415 char_u yank_type;
14416 long block_len;
14417
14418 block_len = -1;
14419 yank_type = MAUTO;
14420 append = FALSE;
14421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 if (strregname == NULL)
14426 return; /* type error; errmsg already given */
14427 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 if (regname == 0 || regname == '@')
14429 regname = '"';
14430 else if (regname == '=')
14431 return;
14432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 stropt = get_tv_string_chk(&argvars[2]);
14436 if (stropt == NULL)
14437 return; /* type error */
14438 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 switch (*stropt)
14440 {
14441 case 'a': case 'A': /* append */
14442 append = TRUE;
14443 break;
14444 case 'v': case 'c': /* character-wise selection */
14445 yank_type = MCHAR;
14446 break;
14447 case 'V': case 'l': /* line-wise selection */
14448 yank_type = MLINE;
14449 break;
14450#ifdef FEAT_VISUAL
14451 case 'b': case Ctrl_V: /* block-wise selection */
14452 yank_type = MBLOCK;
14453 if (VIM_ISDIGIT(stropt[1]))
14454 {
14455 ++stropt;
14456 block_len = getdigits(&stropt) - 1;
14457 --stropt;
14458 }
14459 break;
14460#endif
14461 }
14462 }
14463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 strval = get_tv_string_chk(&argvars[1]);
14465 if (strval != NULL)
14466 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469}
14470
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014471/*
14472 * "settabwinvar()" function
14473 */
14474 static void
14475f_settabwinvar(argvars, rettv)
14476 typval_T *argvars;
14477 typval_T *rettv;
14478{
14479 setwinvar(argvars, rettv, 1);
14480}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481
14482/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014483 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 typval_T *argvars;
14488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014490 setwinvar(argvars, rettv, 0);
14491}
14492
14493/*
14494 * "setwinvar()" and "settabwinvar()" functions
14495 */
14496 static void
14497setwinvar(argvars, rettv, off)
14498 typval_T *argvars;
14499 typval_T *rettv;
14500 int off;
14501{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 win_T *win;
14503#ifdef FEAT_WINDOWS
14504 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014505 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506#endif
14507 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014510 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014512 rettv->vval.v_number = 0;
14513
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 if (check_restricted() || check_secure())
14515 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014516
14517#ifdef FEAT_WINDOWS
14518 if (off == 1)
14519 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14520 else
14521 tp = curtab;
14522#endif
14523 win = find_win_by_nr(&argvars[off], tp);
14524 varname = get_tv_string_chk(&argvars[off + 1]);
14525 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526
14527 if (win != NULL && varname != NULL && varp != NULL)
14528 {
14529#ifdef FEAT_WINDOWS
14530 /* set curwin to be our win, temporarily */
14531 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014532 save_curtab = curtab;
14533 goto_tabpage_tp(tp);
14534 if (!win_valid(win))
14535 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 curwin = win;
14537 curbuf = curwin->w_buffer;
14538#endif
14539
14540 if (*varname == '&')
14541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 long numval;
14543 char_u *strval;
14544 int error = FALSE;
14545
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 numval = get_tv_number_chk(varp, &error);
14548 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 if (!error && strval != NULL)
14550 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 }
14552 else
14553 {
14554 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14555 if (winvarname != NULL)
14556 {
14557 STRCPY(winvarname, "w:");
14558 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014559 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 vim_free(winvarname);
14561 }
14562 }
14563
14564#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014565 /* Restore current tabpage and window, if still valid (autocomands can
14566 * make them invalid). */
14567 if (valid_tabpage(save_curtab))
14568 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 if (win_valid(save_curwin))
14570 {
14571 curwin = save_curwin;
14572 curbuf = curwin->w_buffer;
14573 }
14574#endif
14575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576}
14577
14578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 */
14581 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 typval_T *argvars;
14584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 p = get_tv_string(&argvars[0]);
14589 rettv->vval.v_string = vim_strsave(p);
14590 simplify_filename(rettv->vval.v_string); /* simplify in place */
14591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592}
14593
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594static int
14595#ifdef __BORLANDC__
14596 _RTLENTRYF
14597#endif
14598 item_compare __ARGS((const void *s1, const void *s2));
14599static int
14600#ifdef __BORLANDC__
14601 _RTLENTRYF
14602#endif
14603 item_compare2 __ARGS((const void *s1, const void *s2));
14604
14605static int item_compare_ic;
14606static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608#define ITEM_COMPARE_FAIL 999
14609
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613 static int
14614#ifdef __BORLANDC__
14615_RTLENTRYF
14616#endif
14617item_compare(s1, s2)
14618 const void *s1;
14619 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621 char_u *p1, *p2;
14622 char_u *tofree1, *tofree2;
14623 int res;
14624 char_u numbuf1[NUMBUFLEN];
14625 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014627 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14628 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629 if (item_compare_ic)
14630 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 res = STRCMP(p1, p2);
14633 vim_free(tofree1);
14634 vim_free(tofree2);
14635 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636}
14637
14638 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639#ifdef __BORLANDC__
14640_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642item_compare2(s1, s2)
14643 const void *s1;
14644 const void *s2;
14645{
14646 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014647 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014648 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 /* shortcut after failure in previous call; compare all items equal */
14652 if (item_compare_func_err)
14653 return 0;
14654
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014655 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14656 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014657 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14658 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659
14660 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014661 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014662 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663 clear_tv(&argv[0]);
14664 clear_tv(&argv[1]);
14665
14666 if (res == FAIL)
14667 res = ITEM_COMPARE_FAIL;
14668 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 /* return value has wrong type */
14670 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14671 if (item_compare_func_err)
14672 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 clear_tv(&rettv);
14674 return res;
14675}
14676
14677/*
14678 * "sort({list})" function
14679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 typval_T *argvars;
14683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684{
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 list_T *l;
14686 listitem_T *li;
14687 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688 long len;
14689 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 rettv->vval.v_number = 0;
14692 if (argvars[0].v_type != VAR_LIST)
14693 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694 else
14695 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014697 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698 return;
14699 rettv->vval.v_list = l;
14700 rettv->v_type = VAR_LIST;
14701 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703 len = list_len(l);
14704 if (len <= 1)
14705 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 item_compare_ic = FALSE;
14708 item_compare_func = NULL;
14709 if (argvars[1].v_type != VAR_UNKNOWN)
14710 {
14711 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 else
14714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 int error = FALSE;
14716
14717 i = get_tv_number_chk(&argvars[1], &error);
14718 if (error)
14719 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 if (i == 1)
14721 item_compare_ic = TRUE;
14722 else
14723 item_compare_func = get_tv_string(&argvars[1]);
14724 }
14725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726
Bram Moolenaar0d660222005-01-07 21:51:51 +000014727 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014728 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 if (ptrs == NULL)
14730 return;
14731 i = 0;
14732 for (li = l->lv_first; li != NULL; li = li->li_next)
14733 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 /* test the compare function */
14737 if (item_compare_func != NULL
14738 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14739 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014740 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742 {
14743 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014744 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014745 item_compare_func == NULL ? item_compare : item_compare2);
14746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014747 if (!item_compare_func_err)
14748 {
14749 /* Clear the List and append the items in the sorted order. */
14750 l->lv_first = l->lv_last = NULL;
14751 l->lv_len = 0;
14752 for (i = 0; i < len; ++i)
14753 list_append(l, ptrs[i]);
14754 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014755 }
14756
14757 vim_free(ptrs);
14758 }
14759}
14760
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014761/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014762 * "soundfold({word})" function
14763 */
14764 static void
14765f_soundfold(argvars, rettv)
14766 typval_T *argvars;
14767 typval_T *rettv;
14768{
14769 char_u *s;
14770
14771 rettv->v_type = VAR_STRING;
14772 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014773#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014774 rettv->vval.v_string = eval_soundfold(s);
14775#else
14776 rettv->vval.v_string = vim_strsave(s);
14777#endif
14778}
14779
14780/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781 * "spellbadword()" function
14782 */
14783/* ARGSUSED */
14784 static void
14785f_spellbadword(argvars, rettv)
14786 typval_T *argvars;
14787 typval_T *rettv;
14788{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014789 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014790 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014791 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014792
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014793 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014794 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014795
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014796#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014797 if (argvars[0].v_type == VAR_UNKNOWN)
14798 {
14799 /* Find the start and length of the badly spelled word. */
14800 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14801 if (len != 0)
14802 word = ml_get_cursor();
14803 }
14804 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14805 {
14806 char_u *str = get_tv_string_chk(&argvars[0]);
14807 int capcol = -1;
14808
14809 if (str != NULL)
14810 {
14811 /* Check the argument for spelling. */
14812 while (*str != NUL)
14813 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014814 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014815 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014816 {
14817 word = str;
14818 break;
14819 }
14820 str += len;
14821 }
14822 }
14823 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014824#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014826 list_append_string(rettv->vval.v_list, word, len);
14827 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014828 attr == HLF_SPB ? "bad" :
14829 attr == HLF_SPR ? "rare" :
14830 attr == HLF_SPL ? "local" :
14831 attr == HLF_SPC ? "caps" :
14832 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014833}
14834
14835/*
14836 * "spellsuggest()" function
14837 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014838/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014839 static void
14840f_spellsuggest(argvars, rettv)
14841 typval_T *argvars;
14842 typval_T *rettv;
14843{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014844#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014845 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014846 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014847 int maxcount;
14848 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014849 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014850 listitem_T *li;
14851 int need_capital = FALSE;
14852#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014853
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014854 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014855 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014856
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014857#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014858 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14859 {
14860 str = get_tv_string(&argvars[0]);
14861 if (argvars[1].v_type != VAR_UNKNOWN)
14862 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014863 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014864 if (maxcount <= 0)
14865 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014866 if (argvars[2].v_type != VAR_UNKNOWN)
14867 {
14868 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14869 if (typeerr)
14870 return;
14871 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014872 }
14873 else
14874 maxcount = 25;
14875
Bram Moolenaar4770d092006-01-12 23:22:24 +000014876 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014877
14878 for (i = 0; i < ga.ga_len; ++i)
14879 {
14880 str = ((char_u **)ga.ga_data)[i];
14881
14882 li = listitem_alloc();
14883 if (li == NULL)
14884 vim_free(str);
14885 else
14886 {
14887 li->li_tv.v_type = VAR_STRING;
14888 li->li_tv.v_lock = 0;
14889 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014890 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014891 }
14892 }
14893 ga_clear(&ga);
14894 }
14895#endif
14896}
14897
Bram Moolenaar0d660222005-01-07 21:51:51 +000014898 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014899f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014900 typval_T *argvars;
14901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014902{
14903 char_u *str;
14904 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014905 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014906 regmatch_T regmatch;
14907 char_u patbuf[NUMBUFLEN];
14908 char_u *save_cpo;
14909 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014910 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014911 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014912 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014913
14914 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14915 save_cpo = p_cpo;
14916 p_cpo = (char_u *)"";
14917
14918 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014919 if (argvars[1].v_type != VAR_UNKNOWN)
14920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014921 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14922 if (pat == NULL)
14923 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014926 }
14927 if (pat == NULL || *pat == NUL)
14928 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014929
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014930 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014932 if (typeerr)
14933 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14936 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014939 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014941 if (*str == NUL)
14942 match = FALSE; /* empty item at the end */
14943 else
14944 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014945 if (match)
14946 end = regmatch.startp[0];
14947 else
14948 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014949 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14950 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014952 if (list_append_string(rettv->vval.v_list, str,
14953 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 }
14956 if (!match)
14957 break;
14958 /* Advance to just after the match. */
14959 if (regmatch.endp[0] > str)
14960 col = 0;
14961 else
14962 {
14963 /* Don't get stuck at the same match. */
14964#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014965 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966#else
14967 col = 1;
14968#endif
14969 }
14970 str = regmatch.endp[0];
14971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972
Bram Moolenaar0d660222005-01-07 21:51:51 +000014973 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975
Bram Moolenaar0d660222005-01-07 21:51:51 +000014976 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977}
14978
Bram Moolenaar2c932302006-03-18 21:42:09 +000014979/*
14980 * "str2nr()" function
14981 */
14982 static void
14983f_str2nr(argvars, rettv)
14984 typval_T *argvars;
14985 typval_T *rettv;
14986{
14987 int base = 10;
14988 char_u *p;
14989 long n;
14990
14991 if (argvars[1].v_type != VAR_UNKNOWN)
14992 {
14993 base = get_tv_number(&argvars[1]);
14994 if (base != 8 && base != 10 && base != 16)
14995 {
14996 EMSG(_(e_invarg));
14997 return;
14998 }
14999 }
15000
15001 p = skipwhite(get_tv_string(&argvars[0]));
15002 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15003 rettv->vval.v_number = n;
15004}
15005
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006#ifdef HAVE_STRFTIME
15007/*
15008 * "strftime({format}[, {time}])" function
15009 */
15010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015011f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015012 typval_T *argvars;
15013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014{
15015 char_u result_buf[256];
15016 struct tm *curtime;
15017 time_t seconds;
15018 char_u *p;
15019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015020 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015023 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 seconds = time(NULL);
15025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 curtime = localtime(&seconds);
15028 /* MSVC returns NULL for an invalid value of seconds. */
15029 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 else
15032 {
15033# ifdef FEAT_MBYTE
15034 vimconv_T conv;
15035 char_u *enc;
15036
15037 conv.vc_type = CONV_NONE;
15038 enc = enc_locale();
15039 convert_setup(&conv, p_enc, enc);
15040 if (conv.vc_type != CONV_NONE)
15041 p = string_convert(&conv, p, NULL);
15042# endif
15043 if (p != NULL)
15044 (void)strftime((char *)result_buf, sizeof(result_buf),
15045 (char *)p, curtime);
15046 else
15047 result_buf[0] = NUL;
15048
15049# ifdef FEAT_MBYTE
15050 if (conv.vc_type != CONV_NONE)
15051 vim_free(p);
15052 convert_setup(&conv, enc, p_enc);
15053 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 else
15056# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058
15059# ifdef FEAT_MBYTE
15060 /* Release conversion descriptors */
15061 convert_setup(&conv, NULL, NULL);
15062 vim_free(enc);
15063# endif
15064 }
15065}
15066#endif
15067
15068/*
15069 * "stridx()" function
15070 */
15071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015073 typval_T *argvars;
15074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075{
15076 char_u buf[NUMBUFLEN];
15077 char_u *needle;
15078 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015083 needle = get_tv_string_chk(&argvars[1]);
15084 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015086 if (needle == NULL || haystack == NULL)
15087 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088
Bram Moolenaar33570922005-01-25 22:26:29 +000015089 if (argvars[2].v_type != VAR_UNKNOWN)
15090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 int error = FALSE;
15092
15093 start_idx = get_tv_number_chk(&argvars[2], &error);
15094 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015096 if (start_idx >= 0)
15097 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 }
15099
15100 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15101 if (pos != NULL)
15102 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103}
15104
15105/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015106 * "string()" function
15107 */
15108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015110 typval_T *argvars;
15111 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015112{
15113 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015114 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015116 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015117 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015118 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120}
15121
15122/*
15123 * "strlen()" function
15124 */
15125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015126f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 typval_T *argvars;
15128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 rettv->vval.v_number = (varnumber_T)(STRLEN(
15131 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132}
15133
15134/*
15135 * "strpart()" function
15136 */
15137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015138f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 typval_T *argvars;
15140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141{
15142 char_u *p;
15143 int n;
15144 int len;
15145 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015146 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 slen = (int)STRLEN(p);
15150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015151 n = get_tv_number_chk(&argvars[1], &error);
15152 if (error)
15153 len = 0;
15154 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 else
15157 len = slen - n; /* default len: all bytes that are available. */
15158
15159 /*
15160 * Only return the overlap between the specified part and the actual
15161 * string.
15162 */
15163 if (n < 0)
15164 {
15165 len += n;
15166 n = 0;
15167 }
15168 else if (n > slen)
15169 n = slen;
15170 if (len < 0)
15171 len = 0;
15172 else if (n + len > slen)
15173 len = slen - n;
15174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175 rettv->v_type = VAR_STRING;
15176 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177}
15178
15179/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180 * "strridx()" function
15181 */
15182 static void
15183f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015184 typval_T *argvars;
15185 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186{
15187 char_u buf[NUMBUFLEN];
15188 char_u *needle;
15189 char_u *haystack;
15190 char_u *rest;
15191 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015192 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015194 needle = get_tv_string_chk(&argvars[1]);
15195 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196
15197 rettv->vval.v_number = -1;
15198 if (needle == NULL || haystack == NULL)
15199 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015200
15201 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015202 if (argvars[2].v_type != VAR_UNKNOWN)
15203 {
15204 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015206 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015207 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015208 }
15209 else
15210 end_idx = haystack_len;
15211
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015213 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015215 lastmatch = haystack + end_idx;
15216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015217 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015218 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 for (rest = haystack; *rest != '\0'; ++rest)
15220 {
15221 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015222 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 break;
15224 lastmatch = rest;
15225 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015226 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227
15228 if (lastmatch == NULL)
15229 rettv->vval.v_number = -1;
15230 else
15231 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15232}
15233
15234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 * "strtrans()" function
15236 */
15237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015239 typval_T *argvars;
15240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242 rettv->v_type = VAR_STRING;
15243 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244}
15245
15246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247 * "submatch()" function
15248 */
15249 static void
15250f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *argvars;
15252 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253{
15254 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 rettv->vval.v_string =
15256 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257}
15258
15259/*
15260 * "substitute()" function
15261 */
15262 static void
15263f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015264 typval_T *argvars;
15265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015266{
15267 char_u patbuf[NUMBUFLEN];
15268 char_u subbuf[NUMBUFLEN];
15269 char_u flagsbuf[NUMBUFLEN];
15270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015271 char_u *str = get_tv_string_chk(&argvars[0]);
15272 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15273 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15274 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15275
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015277 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15278 rettv->vval.v_string = NULL;
15279 else
15280 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281}
15282
15283/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015284 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285 */
15286/*ARGSUSED*/
15287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 typval_T *argvars;
15290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291{
15292 int id = 0;
15293#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015294 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295 long col;
15296 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015297 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 lnum = get_tv_lnum(argvars); /* -1 on type error */
15300 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15301 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015303 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015304 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015305 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306#endif
15307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309}
15310
15311/*
15312 * "synIDattr(id, what [, mode])" function
15313 */
15314/*ARGSUSED*/
15315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 typval_T *argvars;
15318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319{
15320 char_u *p = NULL;
15321#ifdef FEAT_SYN_HL
15322 int id;
15323 char_u *what;
15324 char_u *mode;
15325 char_u modebuf[NUMBUFLEN];
15326 int modec;
15327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 id = get_tv_number(&argvars[0]);
15329 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015330 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 modec = TOLOWER_ASC(mode[0]);
15334 if (modec != 't' && modec != 'c'
15335#ifdef FEAT_GUI
15336 && modec != 'g'
15337#endif
15338 )
15339 modec = 0; /* replace invalid with current */
15340 }
15341 else
15342 {
15343#ifdef FEAT_GUI
15344 if (gui.in_use)
15345 modec = 'g';
15346 else
15347#endif
15348 if (t_colors > 1)
15349 modec = 'c';
15350 else
15351 modec = 't';
15352 }
15353
15354
15355 switch (TOLOWER_ASC(what[0]))
15356 {
15357 case 'b':
15358 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15359 p = highlight_color(id, what, modec);
15360 else /* bold */
15361 p = highlight_has_attr(id, HL_BOLD, modec);
15362 break;
15363
15364 case 'f': /* fg[#] */
15365 p = highlight_color(id, what, modec);
15366 break;
15367
15368 case 'i':
15369 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15370 p = highlight_has_attr(id, HL_INVERSE, modec);
15371 else /* italic */
15372 p = highlight_has_attr(id, HL_ITALIC, modec);
15373 break;
15374
15375 case 'n': /* name */
15376 p = get_highlight_name(NULL, id - 1);
15377 break;
15378
15379 case 'r': /* reverse */
15380 p = highlight_has_attr(id, HL_INVERSE, modec);
15381 break;
15382
15383 case 's': /* standout */
15384 p = highlight_has_attr(id, HL_STANDOUT, modec);
15385 break;
15386
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015387 case 'u':
15388 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15389 /* underline */
15390 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15391 else
15392 /* undercurl */
15393 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 break;
15395 }
15396
15397 if (p != NULL)
15398 p = vim_strsave(p);
15399#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->v_type = VAR_STRING;
15401 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402}
15403
15404/*
15405 * "synIDtrans(id)" function
15406 */
15407/*ARGSUSED*/
15408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 typval_T *argvars;
15411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412{
15413 int id;
15414
15415#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417
15418 if (id > 0)
15419 id = syn_get_final_id(id);
15420 else
15421#endif
15422 id = 0;
15423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425}
15426
15427/*
15428 * "system()" function
15429 */
15430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 typval_T *argvars;
15433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015435 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015437 char_u *infile = NULL;
15438 char_u buf[NUMBUFLEN];
15439 int err = FALSE;
15440 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015442 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015443 {
15444 /*
15445 * Write the string to a temp file, to be used for input of the shell
15446 * command.
15447 */
15448 if ((infile = vim_tempname('i')) == NULL)
15449 {
15450 EMSG(_(e_notmp));
15451 return;
15452 }
15453
15454 fd = mch_fopen((char *)infile, WRITEBIN);
15455 if (fd == NULL)
15456 {
15457 EMSG2(_(e_notopen), infile);
15458 goto done;
15459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015460 p = get_tv_string_buf_chk(&argvars[1], buf);
15461 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015462 {
15463 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015464 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015465 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015466 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15467 err = TRUE;
15468 if (fclose(fd) != 0)
15469 err = TRUE;
15470 if (err)
15471 {
15472 EMSG(_("E677: Error writing temp file"));
15473 goto done;
15474 }
15475 }
15476
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015477 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15478 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015479
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480#ifdef USE_CR
15481 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015482 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 {
15484 char_u *s;
15485
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015486 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 {
15488 if (*s == CAR)
15489 *s = NL;
15490 }
15491 }
15492#else
15493# ifdef USE_CRNL
15494 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015495 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 {
15497 char_u *s, *d;
15498
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015499 d = res;
15500 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 {
15502 if (s[0] == CAR && s[1] == NL)
15503 ++s;
15504 *d++ = *s;
15505 }
15506 *d = NUL;
15507 }
15508# endif
15509#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015510
15511done:
15512 if (infile != NULL)
15513 {
15514 mch_remove(infile);
15515 vim_free(infile);
15516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015517 rettv->v_type = VAR_STRING;
15518 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519}
15520
15521/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015522 * "tabpagebuflist()" function
15523 */
15524/* ARGSUSED */
15525 static void
15526f_tabpagebuflist(argvars, rettv)
15527 typval_T *argvars;
15528 typval_T *rettv;
15529{
15530#ifndef FEAT_WINDOWS
15531 rettv->vval.v_number = 0;
15532#else
15533 tabpage_T *tp;
15534 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015535
15536 if (argvars[0].v_type == VAR_UNKNOWN)
15537 wp = firstwin;
15538 else
15539 {
15540 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15541 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015542 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015543 }
15544 if (wp == NULL)
15545 rettv->vval.v_number = 0;
15546 else
15547 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015548 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015549 rettv->vval.v_number = 0;
15550 else
15551 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015552 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015553 if (list_append_number(rettv->vval.v_list,
15554 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015555 break;
15556 }
15557 }
15558#endif
15559}
15560
15561
15562/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015563 * "tabpagenr()" function
15564 */
15565/* ARGSUSED */
15566 static void
15567f_tabpagenr(argvars, rettv)
15568 typval_T *argvars;
15569 typval_T *rettv;
15570{
15571 int nr = 1;
15572#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015573 char_u *arg;
15574
15575 if (argvars[0].v_type != VAR_UNKNOWN)
15576 {
15577 arg = get_tv_string_chk(&argvars[0]);
15578 nr = 0;
15579 if (arg != NULL)
15580 {
15581 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015582 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015583 else
15584 EMSG2(_(e_invexpr2), arg);
15585 }
15586 }
15587 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015588 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015589#endif
15590 rettv->vval.v_number = nr;
15591}
15592
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015593
15594#ifdef FEAT_WINDOWS
15595static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15596
15597/*
15598 * Common code for tabpagewinnr() and winnr().
15599 */
15600 static int
15601get_winnr(tp, argvar)
15602 tabpage_T *tp;
15603 typval_T *argvar;
15604{
15605 win_T *twin;
15606 int nr = 1;
15607 win_T *wp;
15608 char_u *arg;
15609
15610 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15611 if (argvar->v_type != VAR_UNKNOWN)
15612 {
15613 arg = get_tv_string_chk(argvar);
15614 if (arg == NULL)
15615 nr = 0; /* type error; errmsg already given */
15616 else if (STRCMP(arg, "$") == 0)
15617 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15618 else if (STRCMP(arg, "#") == 0)
15619 {
15620 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15621 if (twin == NULL)
15622 nr = 0;
15623 }
15624 else
15625 {
15626 EMSG2(_(e_invexpr2), arg);
15627 nr = 0;
15628 }
15629 }
15630
15631 if (nr > 0)
15632 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15633 wp != twin; wp = wp->w_next)
15634 ++nr;
15635 return nr;
15636}
15637#endif
15638
15639/*
15640 * "tabpagewinnr()" function
15641 */
15642/* ARGSUSED */
15643 static void
15644f_tabpagewinnr(argvars, rettv)
15645 typval_T *argvars;
15646 typval_T *rettv;
15647{
15648 int nr = 1;
15649#ifdef FEAT_WINDOWS
15650 tabpage_T *tp;
15651
15652 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15653 if (tp == NULL)
15654 nr = 0;
15655 else
15656 nr = get_winnr(tp, &argvars[1]);
15657#endif
15658 rettv->vval.v_number = nr;
15659}
15660
15661
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015662/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015663 * "tagfiles()" function
15664 */
15665/*ARGSUSED*/
15666 static void
15667f_tagfiles(argvars, rettv)
15668 typval_T *argvars;
15669 typval_T *rettv;
15670{
15671 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015672 tagname_T tn;
15673 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015674
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015675 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015676 {
15677 rettv->vval.v_number = 0;
15678 return;
15679 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015681 for (first = TRUE; ; first = FALSE)
15682 if (get_tagfname(&tn, first, fname) == FAIL
15683 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015684 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015685 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015686}
15687
15688/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015689 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015690 */
15691 static void
15692f_taglist(argvars, rettv)
15693 typval_T *argvars;
15694 typval_T *rettv;
15695{
15696 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015697
15698 tag_pattern = get_tv_string(&argvars[0]);
15699
15700 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015701 if (*tag_pattern == NUL)
15702 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 if (rettv_list_alloc(rettv) == OK)
15705 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015706}
15707
15708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 * "tempname()" function
15710 */
15711/*ARGSUSED*/
15712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015713f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015714 typval_T *argvars;
15715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716{
15717 static int x = 'A';
15718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719 rettv->v_type = VAR_STRING;
15720 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721
15722 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15723 * names. Skip 'I' and 'O', they are used for shell redirection. */
15724 do
15725 {
15726 if (x == 'Z')
15727 x = '0';
15728 else if (x == '9')
15729 x = 'A';
15730 else
15731 {
15732#ifdef EBCDIC
15733 if (x == 'I')
15734 x = 'J';
15735 else if (x == 'R')
15736 x = 'S';
15737 else
15738#endif
15739 ++x;
15740 }
15741 } while (x == 'I' || x == 'O');
15742}
15743
15744/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015745 * "test(list)" function: Just checking the walls...
15746 */
15747/*ARGSUSED*/
15748 static void
15749f_test(argvars, rettv)
15750 typval_T *argvars;
15751 typval_T *rettv;
15752{
15753 /* Used for unit testing. Change the code below to your liking. */
15754#if 0
15755 listitem_T *li;
15756 list_T *l;
15757 char_u *bad, *good;
15758
15759 if (argvars[0].v_type != VAR_LIST)
15760 return;
15761 l = argvars[0].vval.v_list;
15762 if (l == NULL)
15763 return;
15764 li = l->lv_first;
15765 if (li == NULL)
15766 return;
15767 bad = get_tv_string(&li->li_tv);
15768 li = li->li_next;
15769 if (li == NULL)
15770 return;
15771 good = get_tv_string(&li->li_tv);
15772 rettv->vval.v_number = test_edit_score(bad, good);
15773#endif
15774}
15775
15776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 * "tolower(string)" function
15778 */
15779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 typval_T *argvars;
15782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783{
15784 char_u *p;
15785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 p = vim_strsave(get_tv_string(&argvars[0]));
15787 rettv->v_type = VAR_STRING;
15788 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789
15790 if (p != NULL)
15791 while (*p != NUL)
15792 {
15793#ifdef FEAT_MBYTE
15794 int l;
15795
15796 if (enc_utf8)
15797 {
15798 int c, lc;
15799
15800 c = utf_ptr2char(p);
15801 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015802 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 /* TODO: reallocate string when byte count changes. */
15804 if (utf_char2len(lc) == l)
15805 utf_char2bytes(lc, p);
15806 p += l;
15807 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015808 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 p += l; /* skip multi-byte character */
15810 else
15811#endif
15812 {
15813 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15814 ++p;
15815 }
15816 }
15817}
15818
15819/*
15820 * "toupper(string)" function
15821 */
15822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015824 typval_T *argvars;
15825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015828 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829}
15830
15831/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015832 * "tr(string, fromstr, tostr)" function
15833 */
15834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015835f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015836 typval_T *argvars;
15837 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015838{
15839 char_u *instr;
15840 char_u *fromstr;
15841 char_u *tostr;
15842 char_u *p;
15843#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015844 int inlen;
15845 int fromlen;
15846 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015847 int idx;
15848 char_u *cpstr;
15849 int cplen;
15850 int first = TRUE;
15851#endif
15852 char_u buf[NUMBUFLEN];
15853 char_u buf2[NUMBUFLEN];
15854 garray_T ga;
15855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015857 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15858 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015859
15860 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015861 rettv->v_type = VAR_STRING;
15862 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 if (fromstr == NULL || tostr == NULL)
15864 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015865 ga_init2(&ga, (int)sizeof(char), 80);
15866
15867#ifdef FEAT_MBYTE
15868 if (!has_mbyte)
15869#endif
15870 /* not multi-byte: fromstr and tostr must be the same length */
15871 if (STRLEN(fromstr) != STRLEN(tostr))
15872 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015873#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015874error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015875#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015876 EMSG2(_(e_invarg2), fromstr);
15877 ga_clear(&ga);
15878 return;
15879 }
15880
15881 /* fromstr and tostr have to contain the same number of chars */
15882 while (*instr != NUL)
15883 {
15884#ifdef FEAT_MBYTE
15885 if (has_mbyte)
15886 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015887 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015888 cpstr = instr;
15889 cplen = inlen;
15890 idx = 0;
15891 for (p = fromstr; *p != NUL; p += fromlen)
15892 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015893 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015894 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15895 {
15896 for (p = tostr; *p != NUL; p += tolen)
15897 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015898 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015899 if (idx-- == 0)
15900 {
15901 cplen = tolen;
15902 cpstr = p;
15903 break;
15904 }
15905 }
15906 if (*p == NUL) /* tostr is shorter than fromstr */
15907 goto error;
15908 break;
15909 }
15910 ++idx;
15911 }
15912
15913 if (first && cpstr == instr)
15914 {
15915 /* Check that fromstr and tostr have the same number of
15916 * (multi-byte) characters. Done only once when a character
15917 * of instr doesn't appear in fromstr. */
15918 first = FALSE;
15919 for (p = tostr; *p != NUL; p += tolen)
15920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015921 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015922 --idx;
15923 }
15924 if (idx != 0)
15925 goto error;
15926 }
15927
15928 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015929 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015930 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015931
15932 instr += inlen;
15933 }
15934 else
15935#endif
15936 {
15937 /* When not using multi-byte chars we can do it faster. */
15938 p = vim_strchr(fromstr, *instr);
15939 if (p != NULL)
15940 ga_append(&ga, tostr[p - fromstr]);
15941 else
15942 ga_append(&ga, *instr);
15943 ++instr;
15944 }
15945 }
15946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015947 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015948}
15949
15950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 * "type(expr)" function
15952 */
15953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015954f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015955 typval_T *argvars;
15956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015958 int n;
15959
15960 switch (argvars[0].v_type)
15961 {
15962 case VAR_NUMBER: n = 0; break;
15963 case VAR_STRING: n = 1; break;
15964 case VAR_FUNC: n = 2; break;
15965 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015966 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015967 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15968 }
15969 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970}
15971
15972/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015973 * "values(dict)" function
15974 */
15975 static void
15976f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015977 typval_T *argvars;
15978 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015979{
15980 dict_list(argvars, rettv, 1);
15981}
15982
15983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 * "virtcol(string)" function
15985 */
15986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015987f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 typval_T *argvars;
15989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990{
15991 colnr_T vcol = 0;
15992 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015993 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015995 fp = var2fpos(&argvars[0], FALSE, &fnum);
15996 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15997 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 {
15999 getvvcol(curwin, fp, NULL, NULL, &vcol);
16000 ++vcol;
16001 }
16002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004}
16005
16006/*
16007 * "visualmode()" function
16008 */
16009/*ARGSUSED*/
16010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016011f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 typval_T *argvars;
16013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014{
16015#ifdef FEAT_VISUAL
16016 char_u str[2];
16017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 str[0] = curbuf->b_visual_mode_eval;
16020 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016021 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022
16023 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016024 if ((argvars[0].v_type == VAR_NUMBER
16025 && argvars[0].vval.v_number != 0)
16026 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016027 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 curbuf->b_visual_mode_eval = NUL;
16029#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016030 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031#endif
16032}
16033
16034/*
16035 * "winbufnr(nr)" function
16036 */
16037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016038f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016039 typval_T *argvars;
16040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041{
16042 win_T *wp;
16043
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016044 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016048 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049}
16050
16051/*
16052 * "wincol()" function
16053 */
16054/*ARGSUSED*/
16055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016056f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016057 typval_T *argvars;
16058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059{
16060 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016061 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062}
16063
16064/*
16065 * "winheight(nr)" function
16066 */
16067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016068f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 typval_T *argvars;
16070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071{
16072 win_T *wp;
16073
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016074 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016078 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079}
16080
16081/*
16082 * "winline()" function
16083 */
16084/*ARGSUSED*/
16085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016086f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 typval_T *argvars;
16088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089{
16090 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016091 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092}
16093
16094/*
16095 * "winnr()" function
16096 */
16097/* ARGSUSED */
16098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016100 typval_T *argvars;
16101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102{
16103 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016104
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016106 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016108 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109}
16110
16111/*
16112 * "winrestcmd()" function
16113 */
16114/* ARGSUSED */
16115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016117 typval_T *argvars;
16118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119{
16120#ifdef FEAT_WINDOWS
16121 win_T *wp;
16122 int winnr = 1;
16123 garray_T ga;
16124 char_u buf[50];
16125
16126 ga_init2(&ga, (int)sizeof(char), 70);
16127 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16128 {
16129 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16130 ga_concat(&ga, buf);
16131# ifdef FEAT_VERTSPLIT
16132 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16133 ga_concat(&ga, buf);
16134# endif
16135 ++winnr;
16136 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016137 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016143 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144}
16145
16146/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016147 * "winrestview()" function
16148 */
16149/* ARGSUSED */
16150 static void
16151f_winrestview(argvars, rettv)
16152 typval_T *argvars;
16153 typval_T *rettv;
16154{
16155 dict_T *dict;
16156
16157 if (argvars[0].v_type != VAR_DICT
16158 || (dict = argvars[0].vval.v_dict) == NULL)
16159 EMSG(_(e_invarg));
16160 else
16161 {
16162 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16163 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16164#ifdef FEAT_VIRTUALEDIT
16165 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16166#endif
16167 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016168 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016169
16170 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16171#ifdef FEAT_DIFF
16172 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16173#endif
16174 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16175 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16176
16177 check_cursor();
16178 changed_cline_bef_curs();
16179 invalidate_botline();
16180 redraw_later(VALID);
16181
16182 if (curwin->w_topline == 0)
16183 curwin->w_topline = 1;
16184 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16185 curwin->w_topline = curbuf->b_ml.ml_line_count;
16186#ifdef FEAT_DIFF
16187 check_topfill(curwin, TRUE);
16188#endif
16189 }
16190}
16191
16192/*
16193 * "winsaveview()" function
16194 */
16195/* ARGSUSED */
16196 static void
16197f_winsaveview(argvars, rettv)
16198 typval_T *argvars;
16199 typval_T *rettv;
16200{
16201 dict_T *dict;
16202
16203 dict = dict_alloc();
16204 if (dict == NULL)
16205 return;
16206 rettv->v_type = VAR_DICT;
16207 rettv->vval.v_dict = dict;
16208 ++dict->dv_refcount;
16209
16210 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16211 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16212#ifdef FEAT_VIRTUALEDIT
16213 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16214#endif
16215 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16216
16217 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16218#ifdef FEAT_DIFF
16219 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16220#endif
16221 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16222 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16223}
16224
16225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 * "winwidth(nr)" function
16227 */
16228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 typval_T *argvars;
16231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232{
16233 win_T *wp;
16234
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016235 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 else
16239#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016240 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016242 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243#endif
16244}
16245
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016247 * "writefile()" function
16248 */
16249 static void
16250f_writefile(argvars, rettv)
16251 typval_T *argvars;
16252 typval_T *rettv;
16253{
16254 int binary = FALSE;
16255 char_u *fname;
16256 FILE *fd;
16257 listitem_T *li;
16258 char_u *s;
16259 int ret = 0;
16260 int c;
16261
16262 if (argvars[0].v_type != VAR_LIST)
16263 {
16264 EMSG2(_(e_listarg), "writefile()");
16265 return;
16266 }
16267 if (argvars[0].vval.v_list == NULL)
16268 return;
16269
16270 if (argvars[2].v_type != VAR_UNKNOWN
16271 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16272 binary = TRUE;
16273
16274 /* Always open the file in binary mode, library functions have a mind of
16275 * their own about CR-LF conversion. */
16276 fname = get_tv_string(&argvars[1]);
16277 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16278 {
16279 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16280 ret = -1;
16281 }
16282 else
16283 {
16284 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16285 li = li->li_next)
16286 {
16287 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16288 {
16289 if (*s == '\n')
16290 c = putc(NUL, fd);
16291 else
16292 c = putc(*s, fd);
16293 if (c == EOF)
16294 {
16295 ret = -1;
16296 break;
16297 }
16298 }
16299 if (!binary || li->li_next != NULL)
16300 if (putc('\n', fd) == EOF)
16301 {
16302 ret = -1;
16303 break;
16304 }
16305 if (ret < 0)
16306 {
16307 EMSG(_(e_write));
16308 break;
16309 }
16310 }
16311 fclose(fd);
16312 }
16313
16314 rettv->vval.v_number = ret;
16315}
16316
16317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016319 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 */
16321 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016322var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016323 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016325 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016327 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016329 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330
Bram Moolenaara5525202006-03-02 22:52:09 +000016331 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016332 if (varp->v_type == VAR_LIST)
16333 {
16334 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016335 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016336 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016337
16338 l = varp->vval.v_list;
16339 if (l == NULL)
16340 return NULL;
16341
16342 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016343 pos.lnum = list_find_nr(l, 0L, &error);
16344 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016345 return NULL; /* invalid line number */
16346
16347 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016348 pos.col = list_find_nr(l, 1L, &error);
16349 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016350 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016351 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016352 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016353 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016354 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016355 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016356
Bram Moolenaara5525202006-03-02 22:52:09 +000016357#ifdef FEAT_VIRTUALEDIT
16358 /* Get the virtual offset. Defaults to zero. */
16359 pos.coladd = list_find_nr(l, 2L, &error);
16360 if (error)
16361 pos.coladd = 0;
16362#endif
16363
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016364 return &pos;
16365 }
16366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016367 name = get_tv_string_chk(varp);
16368 if (name == NULL)
16369 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 if (name[0] == '.') /* cursor */
16371 return &curwin->w_cursor;
16372 if (name[0] == '\'') /* mark */
16373 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016374 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16376 return NULL;
16377 return pp;
16378 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016379
16380#ifdef FEAT_VIRTUALEDIT
16381 pos.coladd = 0;
16382#endif
16383
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016384 if (name[0] == 'w' && lnum)
16385 {
16386 pos.col = 0;
16387 if (name[1] == '0') /* "w0": first visible line */
16388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016389 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016390 pos.lnum = curwin->w_topline;
16391 return &pos;
16392 }
16393 else if (name[1] == '$') /* "w$": last visible line */
16394 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016395 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016396 pos.lnum = curwin->w_botline - 1;
16397 return &pos;
16398 }
16399 }
16400 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 {
16402 if (lnum)
16403 {
16404 pos.lnum = curbuf->b_ml.ml_line_count;
16405 pos.col = 0;
16406 }
16407 else
16408 {
16409 pos.lnum = curwin->w_cursor.lnum;
16410 pos.col = (colnr_T)STRLEN(ml_get_curline());
16411 }
16412 return &pos;
16413 }
16414 return NULL;
16415}
16416
16417/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016418 * Convert list in "arg" into a position and optional file number.
16419 * When "fnump" is NULL there is no file number, only 3 items.
16420 * Note that the column is passed on as-is, the caller may want to decrement
16421 * it to use 1 for the first column.
16422 * Return FAIL when conversion is not possible, doesn't check the position for
16423 * validity.
16424 */
16425 static int
16426list2fpos(arg, posp, fnump)
16427 typval_T *arg;
16428 pos_T *posp;
16429 int *fnump;
16430{
16431 list_T *l = arg->vval.v_list;
16432 long i = 0;
16433 long n;
16434
16435 /* List must be: [fnum, lnum, col, coladd] */
16436 if (arg->v_type != VAR_LIST || l == NULL
16437 || l->lv_len != (fnump == NULL ? 3 : 4))
16438 return FAIL;
16439
16440 if (fnump != NULL)
16441 {
16442 n = list_find_nr(l, i++, NULL); /* fnum */
16443 if (n < 0)
16444 return FAIL;
16445 if (n == 0)
16446 n = curbuf->b_fnum; /* current buffer */
16447 *fnump = n;
16448 }
16449
16450 n = list_find_nr(l, i++, NULL); /* lnum */
16451 if (n < 0)
16452 return FAIL;
16453 posp->lnum = n;
16454
16455 n = list_find_nr(l, i++, NULL); /* col */
16456 if (n < 0)
16457 return FAIL;
16458 posp->col = n;
16459
16460#ifdef FEAT_VIRTUALEDIT
16461 n = list_find_nr(l, i, NULL);
16462 if (n < 0)
16463 return FAIL;
16464 posp->coladd = n;
16465#endif
16466
16467 return OK;
16468}
16469
16470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 * Get the length of an environment variable name.
16472 * Advance "arg" to the first character after the name.
16473 * Return 0 for error.
16474 */
16475 static int
16476get_env_len(arg)
16477 char_u **arg;
16478{
16479 char_u *p;
16480 int len;
16481
16482 for (p = *arg; vim_isIDc(*p); ++p)
16483 ;
16484 if (p == *arg) /* no name found */
16485 return 0;
16486
16487 len = (int)(p - *arg);
16488 *arg = p;
16489 return len;
16490}
16491
16492/*
16493 * Get the length of the name of a function or internal variable.
16494 * "arg" is advanced to the first non-white character after the name.
16495 * Return 0 if something is wrong.
16496 */
16497 static int
16498get_id_len(arg)
16499 char_u **arg;
16500{
16501 char_u *p;
16502 int len;
16503
16504 /* Find the end of the name. */
16505 for (p = *arg; eval_isnamec(*p); ++p)
16506 ;
16507 if (p == *arg) /* no name found */
16508 return 0;
16509
16510 len = (int)(p - *arg);
16511 *arg = skipwhite(p);
16512
16513 return len;
16514}
16515
16516/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016517 * Get the length of the name of a variable or function.
16518 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520 * Return -1 if curly braces expansion failed.
16521 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 * If the name contains 'magic' {}'s, expand them and return the
16523 * expanded name in an allocated string via 'alias' - caller must free.
16524 */
16525 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016526get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527 char_u **arg;
16528 char_u **alias;
16529 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016530 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531{
16532 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 char_u *p;
16534 char_u *expr_start;
16535 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536
16537 *alias = NULL; /* default to no alias */
16538
16539 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16540 && (*arg)[2] == (int)KE_SNR)
16541 {
16542 /* hard coded <SNR>, already translated */
16543 *arg += 3;
16544 return get_id_len(arg) + 3;
16545 }
16546 len = eval_fname_script(*arg);
16547 if (len > 0)
16548 {
16549 /* literal "<SID>", "s:" or "<SNR>" */
16550 *arg += len;
16551 }
16552
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016556 p = find_name_end(*arg, &expr_start, &expr_end,
16557 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 if (expr_start != NULL)
16559 {
16560 char_u *temp_string;
16561
16562 if (!evaluate)
16563 {
16564 len += (int)(p - *arg);
16565 *arg = skipwhite(p);
16566 return len;
16567 }
16568
16569 /*
16570 * Include any <SID> etc in the expanded string:
16571 * Thus the -len here.
16572 */
16573 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16574 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 *alias = temp_string;
16577 *arg = skipwhite(p);
16578 return (int)STRLEN(temp_string);
16579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580
16581 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016582 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 EMSG2(_(e_invexpr2), *arg);
16584
16585 return len;
16586}
16587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588/*
16589 * Find the end of a variable or function name, taking care of magic braces.
16590 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16591 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016592 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593 * Return a pointer to just after the name. Equal to "arg" if there is no
16594 * valid name.
16595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016597find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 char_u *arg;
16599 char_u **expr_start;
16600 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016601 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016603 int mb_nest = 0;
16604 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 char_u *p;
16606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016607 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016609 *expr_start = NULL;
16610 *expr_end = NULL;
16611 }
16612
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016613 /* Quick check for valid starting character. */
16614 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16615 return arg;
16616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617 for (p = arg; *p != NUL
16618 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016619 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016620 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016622 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016624 if (*p == '\'')
16625 {
16626 /* skip over 'string' to avoid counting [ and ] inside it. */
16627 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16628 ;
16629 if (*p == NUL)
16630 break;
16631 }
16632 else if (*p == '"')
16633 {
16634 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16635 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16636 if (*p == '\\' && p[1] != NUL)
16637 ++p;
16638 if (*p == NUL)
16639 break;
16640 }
16641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016644 if (*p == '[')
16645 ++br_nest;
16646 else if (*p == ']')
16647 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016652 if (*p == '{')
16653 {
16654 mb_nest++;
16655 if (expr_start != NULL && *expr_start == NULL)
16656 *expr_start = p;
16657 }
16658 else if (*p == '}')
16659 {
16660 mb_nest--;
16661 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16662 *expr_end = p;
16663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 }
16666
16667 return p;
16668}
16669
16670/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016671 * Expands out the 'magic' {}'s in a variable/function name.
16672 * Note that this can call itself recursively, to deal with
16673 * constructs like foo{bar}{baz}{bam}
16674 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16675 * "in_start" ^
16676 * "expr_start" ^
16677 * "expr_end" ^
16678 * "in_end" ^
16679 *
16680 * Returns a new allocated string, which the caller must free.
16681 * Returns NULL for failure.
16682 */
16683 static char_u *
16684make_expanded_name(in_start, expr_start, expr_end, in_end)
16685 char_u *in_start;
16686 char_u *expr_start;
16687 char_u *expr_end;
16688 char_u *in_end;
16689{
16690 char_u c1;
16691 char_u *retval = NULL;
16692 char_u *temp_result;
16693 char_u *nextcmd = NULL;
16694
16695 if (expr_end == NULL || in_end == NULL)
16696 return NULL;
16697 *expr_start = NUL;
16698 *expr_end = NUL;
16699 c1 = *in_end;
16700 *in_end = NUL;
16701
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016702 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016703 if (temp_result != NULL && nextcmd == NULL)
16704 {
16705 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16706 + (in_end - expr_end) + 1));
16707 if (retval != NULL)
16708 {
16709 STRCPY(retval, in_start);
16710 STRCAT(retval, temp_result);
16711 STRCAT(retval, expr_end + 1);
16712 }
16713 }
16714 vim_free(temp_result);
16715
16716 *in_end = c1; /* put char back for error messages */
16717 *expr_start = '{';
16718 *expr_end = '}';
16719
16720 if (retval != NULL)
16721 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016722 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016723 if (expr_start != NULL)
16724 {
16725 /* Further expansion! */
16726 temp_result = make_expanded_name(retval, expr_start,
16727 expr_end, temp_result);
16728 vim_free(retval);
16729 retval = temp_result;
16730 }
16731 }
16732
16733 return retval;
16734}
16735
16736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016738 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 */
16740 static int
16741eval_isnamec(c)
16742 int c;
16743{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016744 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16745}
16746
16747/*
16748 * Return TRUE if character "c" can be used as the first character in a
16749 * variable or function name (excluding '{' and '}').
16750 */
16751 static int
16752eval_isnamec1(c)
16753 int c;
16754{
16755 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756}
16757
16758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 * Set number v: variable to "val".
16760 */
16761 void
16762set_vim_var_nr(idx, val)
16763 int idx;
16764 long val;
16765{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016766 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767}
16768
16769/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016770 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 */
16772 long
16773get_vim_var_nr(idx)
16774 int idx;
16775{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016776 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777}
16778
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016779#if defined(FEAT_AUTOCMD) || defined(PROTO)
16780/*
16781 * Get string v: variable value. Uses a static buffer, can only be used once.
16782 */
16783 char_u *
16784get_vim_var_str(idx)
16785 int idx;
16786{
16787 return get_tv_string(&vimvars[idx].vv_tv);
16788}
16789#endif
16790
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791/*
16792 * Set v:count, v:count1 and v:prevcount.
16793 */
16794 void
16795set_vcount(count, count1)
16796 long count;
16797 long count1;
16798{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016799 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16800 vimvars[VV_COUNT].vv_nr = count;
16801 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802}
16803
16804/*
16805 * Set string v: variable to a copy of "val".
16806 */
16807 void
16808set_vim_var_string(idx, val, len)
16809 int idx;
16810 char_u *val;
16811 int len; /* length of "val" to use or -1 (whole string) */
16812{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016813 /* Need to do this (at least) once, since we can't initialize a union.
16814 * Will always be invoked when "v:progname" is set. */
16815 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16816
Bram Moolenaare9a41262005-01-15 22:18:47 +000016817 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016819 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016821 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016823 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824}
16825
16826/*
16827 * Set v:register if needed.
16828 */
16829 void
16830set_reg_var(c)
16831 int c;
16832{
16833 char_u regname;
16834
16835 if (c == 0 || c == ' ')
16836 regname = '"';
16837 else
16838 regname = c;
16839 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016840 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 set_vim_var_string(VV_REG, &regname, 1);
16842}
16843
16844/*
16845 * Get or set v:exception. If "oldval" == NULL, return the current value.
16846 * Otherwise, restore the value to "oldval" and return NULL.
16847 * Must always be called in pairs to save and restore v:exception! Does not
16848 * take care of memory allocations.
16849 */
16850 char_u *
16851v_exception(oldval)
16852 char_u *oldval;
16853{
16854 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016855 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856
Bram Moolenaare9a41262005-01-15 22:18:47 +000016857 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 return NULL;
16859}
16860
16861/*
16862 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16863 * Otherwise, restore the value to "oldval" and return NULL.
16864 * Must always be called in pairs to save and restore v:throwpoint! Does not
16865 * take care of memory allocations.
16866 */
16867 char_u *
16868v_throwpoint(oldval)
16869 char_u *oldval;
16870{
16871 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016872 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873
Bram Moolenaare9a41262005-01-15 22:18:47 +000016874 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 return NULL;
16876}
16877
16878#if defined(FEAT_AUTOCMD) || defined(PROTO)
16879/*
16880 * Set v:cmdarg.
16881 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16882 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16883 * Must always be called in pairs!
16884 */
16885 char_u *
16886set_cmdarg(eap, oldarg)
16887 exarg_T *eap;
16888 char_u *oldarg;
16889{
16890 char_u *oldval;
16891 char_u *newval;
16892 unsigned len;
16893
Bram Moolenaare9a41262005-01-15 22:18:47 +000016894 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016895 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016897 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016898 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016899 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 }
16901
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016902 if (eap->force_bin == FORCE_BIN)
16903 len = 6;
16904 else if (eap->force_bin == FORCE_NOBIN)
16905 len = 8;
16906 else
16907 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016908
16909 if (eap->read_edit)
16910 len += 7;
16911
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016912 if (eap->force_ff != 0)
16913 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16914# ifdef FEAT_MBYTE
16915 if (eap->force_enc != 0)
16916 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016917 if (eap->bad_char != 0)
16918 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016919# endif
16920
16921 newval = alloc(len + 1);
16922 if (newval == NULL)
16923 return NULL;
16924
16925 if (eap->force_bin == FORCE_BIN)
16926 sprintf((char *)newval, " ++bin");
16927 else if (eap->force_bin == FORCE_NOBIN)
16928 sprintf((char *)newval, " ++nobin");
16929 else
16930 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016931
16932 if (eap->read_edit)
16933 STRCAT(newval, " ++edit");
16934
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016935 if (eap->force_ff != 0)
16936 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16937 eap->cmd + eap->force_ff);
16938# ifdef FEAT_MBYTE
16939 if (eap->force_enc != 0)
16940 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16941 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016942 if (eap->bad_char != 0)
16943 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16944 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016945# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016946 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016947 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948}
16949#endif
16950
16951/*
16952 * Get the value of internal variable "name".
16953 * Return OK or FAIL.
16954 */
16955 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016956get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 char_u *name;
16958 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016960 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961{
16962 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016963 typval_T *tv = NULL;
16964 typval_T atv;
16965 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967
16968 /* truncate the name, so that we can use strcmp() */
16969 cc = name[len];
16970 name[len] = NUL;
16971
16972 /*
16973 * Check for "b:changedtick".
16974 */
16975 if (STRCMP(name, "b:changedtick") == 0)
16976 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016977 atv.v_type = VAR_NUMBER;
16978 atv.vval.v_number = curbuf->b_changedtick;
16979 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 }
16981
16982 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 * Check for user-defined variables.
16984 */
16985 else
16986 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016987 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 }
16991
Bram Moolenaare9a41262005-01-15 22:18:47 +000016992 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016994 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016995 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 ret = FAIL;
16997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016999 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000
17001 name[len] = cc;
17002
17003 return ret;
17004}
17005
17006/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017007 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17008 * Also handle function call with Funcref variable: func(expr)
17009 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17010 */
17011 static int
17012handle_subscript(arg, rettv, evaluate, verbose)
17013 char_u **arg;
17014 typval_T *rettv;
17015 int evaluate; /* do more than finding the end */
17016 int verbose; /* give error messages */
17017{
17018 int ret = OK;
17019 dict_T *selfdict = NULL;
17020 char_u *s;
17021 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017022 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017023
17024 while (ret == OK
17025 && (**arg == '['
17026 || (**arg == '.' && rettv->v_type == VAR_DICT)
17027 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17028 && !vim_iswhite(*(*arg - 1)))
17029 {
17030 if (**arg == '(')
17031 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017032 /* need to copy the funcref so that we can clear rettv */
17033 functv = *rettv;
17034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017035
17036 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017037 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017038 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017039 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17040 &len, evaluate, selfdict);
17041
17042 /* Clear the funcref afterwards, so that deleting it while
17043 * evaluating the arguments is possible (see test55). */
17044 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017045
17046 /* Stop the expression evaluation when immediately aborting on
17047 * error, or when an interrupt occurred or an exception was thrown
17048 * but not caught. */
17049 if (aborting())
17050 {
17051 if (ret == OK)
17052 clear_tv(rettv);
17053 ret = FAIL;
17054 }
17055 dict_unref(selfdict);
17056 selfdict = NULL;
17057 }
17058 else /* **arg == '[' || **arg == '.' */
17059 {
17060 dict_unref(selfdict);
17061 if (rettv->v_type == VAR_DICT)
17062 {
17063 selfdict = rettv->vval.v_dict;
17064 if (selfdict != NULL)
17065 ++selfdict->dv_refcount;
17066 }
17067 else
17068 selfdict = NULL;
17069 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17070 {
17071 clear_tv(rettv);
17072 ret = FAIL;
17073 }
17074 }
17075 }
17076 dict_unref(selfdict);
17077 return ret;
17078}
17079
17080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17082 * value).
17083 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017084 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017085alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017086{
Bram Moolenaar33570922005-01-25 22:26:29 +000017087 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017088}
17089
17090/*
17091 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 * The string "s" must have been allocated, it is consumed.
17093 * Return NULL for out of memory, the variable otherwise.
17094 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017096alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 char_u *s;
17098{
Bram Moolenaar33570922005-01-25 22:26:29 +000017099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017101 rettv = alloc_tv();
17102 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017104 rettv->v_type = VAR_STRING;
17105 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 }
17107 else
17108 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017109 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110}
17111
17112/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017113 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017115 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118{
17119 if (varp != NULL)
17120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017121 switch (varp->v_type)
17122 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017123 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017124 func_unref(varp->vval.v_string);
17125 /*FALLTHROUGH*/
17126 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017127 vim_free(varp->vval.v_string);
17128 break;
17129 case VAR_LIST:
17130 list_unref(varp->vval.v_list);
17131 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017132 case VAR_DICT:
17133 dict_unref(varp->vval.v_dict);
17134 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017135 case VAR_NUMBER:
17136 case VAR_UNKNOWN:
17137 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017138 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017139 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017140 break;
17141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 vim_free(varp);
17143 }
17144}
17145
17146/*
17147 * Free the memory for a variable value and set the value to NULL or 0.
17148 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017149 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152{
17153 if (varp != NULL)
17154 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017155 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017157 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017158 func_unref(varp->vval.v_string);
17159 /*FALLTHROUGH*/
17160 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 vim_free(varp->vval.v_string);
17162 varp->vval.v_string = NULL;
17163 break;
17164 case VAR_LIST:
17165 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017166 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017167 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017168 case VAR_DICT:
17169 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017170 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017171 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017172 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173 varp->vval.v_number = 0;
17174 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017175 case VAR_UNKNOWN:
17176 break;
17177 default:
17178 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017180 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 }
17182}
17183
17184/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 * Set the value of a variable to NULL without freeing items.
17186 */
17187 static void
17188init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017190{
17191 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017192 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193}
17194
17195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 * Get the number value of a variable.
17197 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017198 * For incompatible types, return 0.
17199 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17200 * caller of incompatible types: it sets *denote to TRUE if "denote"
17201 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 */
17203 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017205 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017207 int error = FALSE;
17208
17209 return get_tv_number_chk(varp, &error); /* return 0L on error */
17210}
17211
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017212 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017213get_tv_number_chk(varp, denote)
17214 typval_T *varp;
17215 int *denote;
17216{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017217 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017219 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017221 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017222 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017223 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017224 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017225 break;
17226 case VAR_STRING:
17227 if (varp->vval.v_string != NULL)
17228 vim_str2nr(varp->vval.v_string, NULL, NULL,
17229 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017230 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017231 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017232 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017233 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017234 case VAR_DICT:
17235 EMSG(_("E728: Using a Dictionary as a number"));
17236 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017237 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017238 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 if (denote == NULL) /* useful for values that must be unsigned */
17242 n = -1;
17243 else
17244 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246}
17247
17248/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017249 * Get the lnum from the first argument.
17250 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 */
17253 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017254get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017255 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256{
Bram Moolenaar33570922005-01-25 22:26:29 +000017257 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 linenr_T lnum;
17259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 if (lnum == 0) /* no valid number, try using line() */
17262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263 rettv.v_type = VAR_NUMBER;
17264 f_line(argvars, &rettv);
17265 lnum = rettv.vval.v_number;
17266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 }
17268 return lnum;
17269}
17270
17271/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017272 * Get the lnum from the first argument.
17273 * Also accepts "$", then "buf" is used.
17274 * Returns 0 on error.
17275 */
17276 static linenr_T
17277get_tv_lnum_buf(argvars, buf)
17278 typval_T *argvars;
17279 buf_T *buf;
17280{
17281 if (argvars[0].v_type == VAR_STRING
17282 && argvars[0].vval.v_string != NULL
17283 && argvars[0].vval.v_string[0] == '$'
17284 && buf != NULL)
17285 return buf->b_ml.ml_line_count;
17286 return get_tv_number_chk(&argvars[0], NULL);
17287}
17288
17289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 * Get the string value of a variable.
17291 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017292 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17293 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 * If the String variable has never been set, return an empty string.
17295 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17297 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 */
17299 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017301 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302{
17303 static char_u mybuf[NUMBUFLEN];
17304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306}
17307
17308 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017310 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017311 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017313 char_u *res = get_tv_string_buf_chk(varp, buf);
17314
17315 return res != NULL ? res : (char_u *)"";
17316}
17317
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017318 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017319get_tv_string_chk(varp)
17320 typval_T *varp;
17321{
17322 static char_u mybuf[NUMBUFLEN];
17323
17324 return get_tv_string_buf_chk(varp, mybuf);
17325}
17326
17327 static char_u *
17328get_tv_string_buf_chk(varp, buf)
17329 typval_T *varp;
17330 char_u *buf;
17331{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017332 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017334 case VAR_NUMBER:
17335 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17336 return buf;
17337 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017338 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017339 break;
17340 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017341 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017342 break;
17343 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017344 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345 break;
17346 case VAR_STRING:
17347 if (varp->vval.v_string != NULL)
17348 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017350 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017352 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017354 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355}
17356
17357/*
17358 * Find variable "name" in the list of variables.
17359 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017360 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017361 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017365find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaara7043832005-01-21 11:56:39 +000017372 ht = find_var_ht(name, &varname);
17373 if (htp != NULL)
17374 *htp = ht;
17375 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017377 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378}
17379
17380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017381 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017382 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017385find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017386 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017387 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017388 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017389{
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 hashitem_T *hi;
17391
17392 if (*varname == NUL)
17393 {
17394 /* Must be something like "s:", otherwise "ht" would be NULL. */
17395 switch (varname[-2])
17396 {
17397 case 's': return &SCRIPT_SV(current_SID).sv_var;
17398 case 'g': return &globvars_var;
17399 case 'v': return &vimvars_var;
17400 case 'b': return &curbuf->b_bufvar;
17401 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017402#ifdef FEAT_WINDOWS
17403 case 't': return &curtab->tp_winvar;
17404#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017405 case 'l': return current_funccal == NULL
17406 ? NULL : &current_funccal->l_vars_var;
17407 case 'a': return current_funccal == NULL
17408 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 }
17410 return NULL;
17411 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017412
17413 hi = hash_find(ht, varname);
17414 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017415 {
17416 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017417 * worked find the variable again. Don't auto-load a script if it was
17418 * loaded already, otherwise it would be loaded every time when
17419 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017420 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017421 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017422 hi = hash_find(ht, varname);
17423 if (HASHITEM_EMPTY(hi))
17424 return NULL;
17425 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017426 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017427}
17428
17429/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017431 * Set "varname" to the start of name without ':'.
17432 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017433 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017434find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 char_u *name;
17436 char_u **varname;
17437{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017438 hashitem_T *hi;
17439
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 if (name[1] != ':')
17441 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017442 /* The name must not start with a colon or #. */
17443 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 return NULL;
17445 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017446
17447 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017448 hi = hash_find(&compat_hashtab, name);
17449 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017450 return &compat_hashtab;
17451
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017453 return &globvarht; /* global variable */
17454 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 }
17456 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017457 if (*name == 'g') /* global variable */
17458 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017459 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17460 */
17461 if (vim_strchr(name + 2, ':') != NULL
17462 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017463 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017467 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017468#ifdef FEAT_WINDOWS
17469 if (*name == 't') /* tab page variable */
17470 return &curtab->tp_vars.dv_hashtab;
17471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017472 if (*name == 'v') /* v: variable */
17473 return &vimvarht;
17474 if (*name == 'a' && current_funccal != NULL) /* function argument */
17475 return &current_funccal->l_avars.dv_hashtab;
17476 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17477 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 if (*name == 's' /* script variable */
17479 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17480 return &SCRIPT_VARS(current_SID);
17481 return NULL;
17482}
17483
17484/*
17485 * Get the string value of a (global/local) variable.
17486 * Returns NULL when it doesn't exist.
17487 */
17488 char_u *
17489get_var_value(name)
17490 char_u *name;
17491{
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493
Bram Moolenaara7043832005-01-21 11:56:39 +000017494 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 if (v == NULL)
17496 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498}
17499
17500/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 * sourcing this script and when executing functions defined in the script.
17503 */
17504 void
17505new_script_vars(id)
17506 scid_T id;
17507{
Bram Moolenaara7043832005-01-21 11:56:39 +000017508 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 hashtab_T *ht;
17510 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017511
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17513 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017514 /* Re-allocating ga_data means that an ht_array pointing to
17515 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017516 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017517 for (i = 1; i <= ga_scripts.ga_len; ++i)
17518 {
17519 ht = &SCRIPT_VARS(i);
17520 if (ht->ht_mask == HT_INIT_SIZE - 1)
17521 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017522 sv = &SCRIPT_SV(i);
17523 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017524 }
17525
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 while (ga_scripts.ga_len < id)
17527 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17529 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 }
17532 }
17533}
17534
17535/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017536 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17537 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 */
17539 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017540init_var_dict(dict, dict_var)
17541 dict_T *dict;
17542 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543{
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 hash_init(&dict->dv_hashtab);
17545 dict->dv_refcount = 99999;
17546 dict_var->di_tv.vval.v_dict = dict;
17547 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017548 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17550 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551}
17552
17553/*
17554 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 * Frees all allocated variables and the value they contain.
17556 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 */
17558 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017559vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 hashtab_T *ht;
17561{
17562 vars_clear_ext(ht, TRUE);
17563}
17564
17565/*
17566 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17567 */
17568 static void
17569vars_clear_ext(ht, free_val)
17570 hashtab_T *ht;
17571 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572{
Bram Moolenaara7043832005-01-21 11:56:39 +000017573 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017574 hashitem_T *hi;
17575 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017578 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017579 for (hi = ht->ht_array; todo > 0; ++hi)
17580 {
17581 if (!HASHITEM_EMPTY(hi))
17582 {
17583 --todo;
17584
Bram Moolenaar33570922005-01-25 22:26:29 +000017585 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017586 * ht_array might change then. hash_clear() takes care of it
17587 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 v = HI2DI(hi);
17589 if (free_val)
17590 clear_tv(&v->di_tv);
17591 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17592 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017593 }
17594 }
17595 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017596 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597}
17598
Bram Moolenaara7043832005-01-21 11:56:39 +000017599/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017600 * Delete a variable from hashtab "ht" at item "hi".
17601 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017604delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017605 hashtab_T *ht;
17606 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607{
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017609
17610 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 clear_tv(&di->di_tv);
17612 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613}
17614
17615/*
17616 * List the value of one internal variable.
17617 */
17618 static void
17619list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017620 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 char_u *prefix;
17622{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017623 char_u *tofree;
17624 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017625 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017626
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017627 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017629 s == NULL ? (char_u *)"" : s);
17630 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631}
17632
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 static void
17634list_one_var_a(prefix, name, type, string)
17635 char_u *prefix;
17636 char_u *name;
17637 int type;
17638 char_u *string;
17639{
17640 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17641 if (name != NULL) /* "a:" vars don't have a name stored */
17642 msg_puts(name);
17643 msg_putchar(' ');
17644 msg_advance(22);
17645 if (type == VAR_NUMBER)
17646 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647 else if (type == VAR_FUNC)
17648 msg_putchar('*');
17649 else if (type == VAR_LIST)
17650 {
17651 msg_putchar('[');
17652 if (*string == '[')
17653 ++string;
17654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017655 else if (type == VAR_DICT)
17656 {
17657 msg_putchar('{');
17658 if (*string == '{')
17659 ++string;
17660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 else
17662 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017663
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017665
17666 if (type == VAR_FUNC)
17667 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668}
17669
17670/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 * If the variable already exists, the value is updated.
17673 * Otherwise the variable is created.
17674 */
17675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680{
Bram Moolenaar33570922005-01-25 22:26:29 +000017681 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017684 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017686 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017687 {
17688 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17689 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17690 ? name[2] : name[0]))
17691 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017692 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017693 return;
17694 }
17695 if (function_exists(name))
17696 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017697 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017698 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017699 return;
17700 }
17701 }
17702
Bram Moolenaara7043832005-01-21 11:56:39 +000017703 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017704 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017705 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017706 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017707 return;
17708 }
17709
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017710 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017714 if (var_check_ro(v->di_flags, name)
17715 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017716 return;
17717 if (v->di_tv.v_type != tv->v_type
17718 && !((v->di_tv.v_type == VAR_STRING
17719 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720 && (tv->v_type == VAR_STRING
17721 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017722 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017723 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017724 return;
17725 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017726
17727 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017728 * Handle setting internal v: variables separately: we don't change
17729 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 */
17731 if (ht == &vimvarht)
17732 {
17733 if (v->di_tv.v_type == VAR_STRING)
17734 {
17735 vim_free(v->di_tv.vval.v_string);
17736 if (copy || tv->v_type != VAR_STRING)
17737 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17738 else
17739 {
17740 /* Take over the string to avoid an extra alloc/free. */
17741 v->di_tv.vval.v_string = tv->vval.v_string;
17742 tv->vval.v_string = NULL;
17743 }
17744 }
17745 else if (v->di_tv.v_type != VAR_NUMBER)
17746 EMSG2(_(e_intern2), "set_var()");
17747 else
17748 v->di_tv.vval.v_number = get_tv_number(tv);
17749 return;
17750 }
17751
17752 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 }
17754 else /* add a new variable */
17755 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017756 /* Make sure the variable name is valid. */
17757 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017758 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17759 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017760 {
17761 EMSG2(_(e_illvar), varname);
17762 return;
17763 }
17764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017765 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17766 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017767 if (v == NULL)
17768 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017772 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 return;
17774 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017775 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017778 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017779 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017780 else
17781 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017783 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017784 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786}
17787
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017788/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17790 * Also give an error message.
17791 */
17792 static int
17793var_check_ro(flags, name)
17794 int flags;
17795 char_u *name;
17796{
17797 if (flags & DI_FLAGS_RO)
17798 {
17799 EMSG2(_(e_readonlyvar), name);
17800 return TRUE;
17801 }
17802 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17803 {
17804 EMSG2(_(e_readonlysbx), name);
17805 return TRUE;
17806 }
17807 return FALSE;
17808}
17809
17810/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017811 * Return TRUE if typeval "tv" is set to be locked (immutable).
17812 * Also give an error message, using "name".
17813 */
17814 static int
17815tv_check_lock(lock, name)
17816 int lock;
17817 char_u *name;
17818{
17819 if (lock & VAR_LOCKED)
17820 {
17821 EMSG2(_("E741: Value is locked: %s"),
17822 name == NULL ? (char_u *)_("Unknown") : name);
17823 return TRUE;
17824 }
17825 if (lock & VAR_FIXED)
17826 {
17827 EMSG2(_("E742: Cannot change value of %s"),
17828 name == NULL ? (char_u *)_("Unknown") : name);
17829 return TRUE;
17830 }
17831 return FALSE;
17832}
17833
17834/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017835 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017836 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017837 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017840copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 typval_T *from;
17842 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017844 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017845 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017846 switch (from->v_type)
17847 {
17848 case VAR_NUMBER:
17849 to->vval.v_number = from->vval.v_number;
17850 break;
17851 case VAR_STRING:
17852 case VAR_FUNC:
17853 if (from->vval.v_string == NULL)
17854 to->vval.v_string = NULL;
17855 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017856 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017857 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017858 if (from->v_type == VAR_FUNC)
17859 func_ref(to->vval.v_string);
17860 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017861 break;
17862 case VAR_LIST:
17863 if (from->vval.v_list == NULL)
17864 to->vval.v_list = NULL;
17865 else
17866 {
17867 to->vval.v_list = from->vval.v_list;
17868 ++to->vval.v_list->lv_refcount;
17869 }
17870 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017871 case VAR_DICT:
17872 if (from->vval.v_dict == NULL)
17873 to->vval.v_dict = NULL;
17874 else
17875 {
17876 to->vval.v_dict = from->vval.v_dict;
17877 ++to->vval.v_dict->dv_refcount;
17878 }
17879 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017880 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017882 break;
17883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884}
17885
17886/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017887 * Make a copy of an item.
17888 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017889 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17890 * reference to an already copied list/dict can be used.
17891 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017892 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017893 static int
17894item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017895 typval_T *from;
17896 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017897 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017898 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017899{
17900 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017901 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017902
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017904 {
17905 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017906 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017907 }
17908 ++recurse;
17909
17910 switch (from->v_type)
17911 {
17912 case VAR_NUMBER:
17913 case VAR_STRING:
17914 case VAR_FUNC:
17915 copy_tv(from, to);
17916 break;
17917 case VAR_LIST:
17918 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017919 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017920 if (from->vval.v_list == NULL)
17921 to->vval.v_list = NULL;
17922 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17923 {
17924 /* use the copy made earlier */
17925 to->vval.v_list = from->vval.v_list->lv_copylist;
17926 ++to->vval.v_list->lv_refcount;
17927 }
17928 else
17929 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17930 if (to->vval.v_list == NULL)
17931 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017932 break;
17933 case VAR_DICT:
17934 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017935 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017936 if (from->vval.v_dict == NULL)
17937 to->vval.v_dict = NULL;
17938 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17939 {
17940 /* use the copy made earlier */
17941 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17942 ++to->vval.v_dict->dv_refcount;
17943 }
17944 else
17945 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17946 if (to->vval.v_dict == NULL)
17947 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017948 break;
17949 default:
17950 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017951 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017952 }
17953 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017954 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017955}
17956
17957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 * ":echo expr1 ..." print each argument separated with a space, add a
17959 * newline at the end.
17960 * ":echon expr1 ..." print each argument plain.
17961 */
17962 void
17963ex_echo(eap)
17964 exarg_T *eap;
17965{
17966 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017968 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 char_u *p;
17970 int needclr = TRUE;
17971 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017972 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973
17974 if (eap->skip)
17975 ++emsg_skip;
17976 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17977 {
17978 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017979 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 {
17981 /*
17982 * Report the invalid expression unless the expression evaluation
17983 * has been cancelled due to an aborting error, an interrupt, or an
17984 * exception.
17985 */
17986 if (!aborting())
17987 EMSG2(_(e_invexpr2), p);
17988 break;
17989 }
17990 if (!eap->skip)
17991 {
17992 if (atstart)
17993 {
17994 atstart = FALSE;
17995 /* Call msg_start() after eval1(), evaluating the expression
17996 * may cause a message to appear. */
17997 if (eap->cmdidx == CMD_echo)
17998 msg_start();
17999 }
18000 else if (eap->cmdidx == CMD_echo)
18001 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018002 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018003 if (p != NULL)
18004 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018006 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018008 if (*p != TAB && needclr)
18009 {
18010 /* remove any text still there from the command */
18011 msg_clr_eos();
18012 needclr = FALSE;
18013 }
18014 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 }
18016 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018017 {
18018#ifdef FEAT_MBYTE
18019 if (has_mbyte)
18020 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018021 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018022
18023 (void)msg_outtrans_len_attr(p, i, echo_attr);
18024 p += i - 1;
18025 }
18026 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018028 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018031 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018033 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 arg = skipwhite(arg);
18035 }
18036 eap->nextcmd = check_nextcmd(arg);
18037
18038 if (eap->skip)
18039 --emsg_skip;
18040 else
18041 {
18042 /* remove text that may still be there from the command */
18043 if (needclr)
18044 msg_clr_eos();
18045 if (eap->cmdidx == CMD_echo)
18046 msg_end();
18047 }
18048}
18049
18050/*
18051 * ":echohl {name}".
18052 */
18053 void
18054ex_echohl(eap)
18055 exarg_T *eap;
18056{
18057 int id;
18058
18059 id = syn_name2id(eap->arg);
18060 if (id == 0)
18061 echo_attr = 0;
18062 else
18063 echo_attr = syn_id2attr(id);
18064}
18065
18066/*
18067 * ":execute expr1 ..." execute the result of an expression.
18068 * ":echomsg expr1 ..." Print a message
18069 * ":echoerr expr1 ..." Print an error
18070 * Each gets spaces around each argument and a newline at the end for
18071 * echo commands
18072 */
18073 void
18074ex_execute(eap)
18075 exarg_T *eap;
18076{
18077 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 int ret = OK;
18080 char_u *p;
18081 garray_T ga;
18082 int len;
18083 int save_did_emsg;
18084
18085 ga_init2(&ga, 1, 80);
18086
18087 if (eap->skip)
18088 ++emsg_skip;
18089 while (*arg != NUL && *arg != '|' && *arg != '\n')
18090 {
18091 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 {
18094 /*
18095 * Report the invalid expression unless the expression evaluation
18096 * has been cancelled due to an aborting error, an interrupt, or an
18097 * exception.
18098 */
18099 if (!aborting())
18100 EMSG2(_(e_invexpr2), p);
18101 ret = FAIL;
18102 break;
18103 }
18104
18105 if (!eap->skip)
18106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 len = (int)STRLEN(p);
18109 if (ga_grow(&ga, len + 2) == FAIL)
18110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 ret = FAIL;
18113 break;
18114 }
18115 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 ga.ga_len += len;
18119 }
18120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018121 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 arg = skipwhite(arg);
18123 }
18124
18125 if (ret != FAIL && ga.ga_data != NULL)
18126 {
18127 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018130 out_flush();
18131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 else if (eap->cmdidx == CMD_echoerr)
18133 {
18134 /* We don't want to abort following commands, restore did_emsg. */
18135 save_did_emsg = did_emsg;
18136 EMSG((char_u *)ga.ga_data);
18137 if (!force_abort)
18138 did_emsg = save_did_emsg;
18139 }
18140 else if (eap->cmdidx == CMD_execute)
18141 do_cmdline((char_u *)ga.ga_data,
18142 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18143 }
18144
18145 ga_clear(&ga);
18146
18147 if (eap->skip)
18148 --emsg_skip;
18149
18150 eap->nextcmd = check_nextcmd(arg);
18151}
18152
18153/*
18154 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18155 * "arg" points to the "&" or '+' when called, to "option" when returning.
18156 * Returns NULL when no option name found. Otherwise pointer to the char
18157 * after the option name.
18158 */
18159 static char_u *
18160find_option_end(arg, opt_flags)
18161 char_u **arg;
18162 int *opt_flags;
18163{
18164 char_u *p = *arg;
18165
18166 ++p;
18167 if (*p == 'g' && p[1] == ':')
18168 {
18169 *opt_flags = OPT_GLOBAL;
18170 p += 2;
18171 }
18172 else if (*p == 'l' && p[1] == ':')
18173 {
18174 *opt_flags = OPT_LOCAL;
18175 p += 2;
18176 }
18177 else
18178 *opt_flags = 0;
18179
18180 if (!ASCII_ISALPHA(*p))
18181 return NULL;
18182 *arg = p;
18183
18184 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18185 p += 4; /* termcap option */
18186 else
18187 while (ASCII_ISALPHA(*p))
18188 ++p;
18189 return p;
18190}
18191
18192/*
18193 * ":function"
18194 */
18195 void
18196ex_function(eap)
18197 exarg_T *eap;
18198{
18199 char_u *theline;
18200 int j;
18201 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 char_u *name = NULL;
18204 char_u *p;
18205 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018206 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 garray_T newargs;
18208 garray_T newlines;
18209 int varargs = FALSE;
18210 int mustend = FALSE;
18211 int flags = 0;
18212 ufunc_T *fp;
18213 int indent;
18214 int nesting;
18215 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 dictitem_T *v;
18217 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018218 static int func_nr = 0; /* number for nameless function */
18219 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018220 hashtab_T *ht;
18221 int todo;
18222 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018223 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224
18225 /*
18226 * ":function" without argument: list functions.
18227 */
18228 if (ends_excmd(*eap->arg))
18229 {
18230 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018231 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018232 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018233 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018234 {
18235 if (!HASHITEM_EMPTY(hi))
18236 {
18237 --todo;
18238 fp = HI2UF(hi);
18239 if (!isdigit(*fp->uf_name))
18240 list_func_head(fp, FALSE);
18241 }
18242 }
18243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 eap->nextcmd = check_nextcmd(eap->arg);
18245 return;
18246 }
18247
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018248 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018249 * ":function /pat": list functions matching pattern.
18250 */
18251 if (*eap->arg == '/')
18252 {
18253 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18254 if (!eap->skip)
18255 {
18256 regmatch_T regmatch;
18257
18258 c = *p;
18259 *p = NUL;
18260 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18261 *p = c;
18262 if (regmatch.regprog != NULL)
18263 {
18264 regmatch.rm_ic = p_ic;
18265
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018266 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018267 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18268 {
18269 if (!HASHITEM_EMPTY(hi))
18270 {
18271 --todo;
18272 fp = HI2UF(hi);
18273 if (!isdigit(*fp->uf_name)
18274 && vim_regexec(&regmatch, fp->uf_name, 0))
18275 list_func_head(fp, FALSE);
18276 }
18277 }
18278 }
18279 }
18280 if (*p == '/')
18281 ++p;
18282 eap->nextcmd = check_nextcmd(p);
18283 return;
18284 }
18285
18286 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018287 * Get the function name. There are these situations:
18288 * func normal function name
18289 * "name" == func, "fudi.fd_dict" == NULL
18290 * dict.func new dictionary entry
18291 * "name" == NULL, "fudi.fd_dict" set,
18292 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18293 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018294 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018295 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18296 * dict.func existing dict entry that's not a Funcref
18297 * "name" == NULL, "fudi.fd_dict" set,
18298 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018301 name = trans_function_name(&p, eap->skip, 0, &fudi);
18302 paren = (vim_strchr(p, '(') != NULL);
18303 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 {
18305 /*
18306 * Return on an invalid expression in braces, unless the expression
18307 * evaluation has been cancelled due to an aborting error, an
18308 * interrupt, or an exception.
18309 */
18310 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018312 if (!eap->skip && fudi.fd_newkey != NULL)
18313 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018314 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 else
18318 eap->skip = TRUE;
18319 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018320
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 /* An error in a function call during evaluation of an expression in magic
18322 * braces should not cause the function not to be defined. */
18323 saved_did_emsg = did_emsg;
18324 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325
18326 /*
18327 * ":function func" with only function name: list function.
18328 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018329 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 {
18331 if (!ends_excmd(*skipwhite(p)))
18332 {
18333 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018334 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 }
18336 eap->nextcmd = check_nextcmd(p);
18337 if (eap->nextcmd != NULL)
18338 *p = NUL;
18339 if (!eap->skip && !got_int)
18340 {
18341 fp = find_func(name);
18342 if (fp != NULL)
18343 {
18344 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018345 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018347 if (FUNCLINE(fp, j) == NULL)
18348 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 msg_putchar('\n');
18350 msg_outnum((long)(j + 1));
18351 if (j < 9)
18352 msg_putchar(' ');
18353 if (j < 99)
18354 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018355 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 out_flush(); /* show a line at a time */
18357 ui_breakcheck();
18358 }
18359 if (!got_int)
18360 {
18361 msg_putchar('\n');
18362 msg_puts((char_u *)" endfunction");
18363 }
18364 }
18365 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018366 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018368 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 }
18370
18371 /*
18372 * ":function name(arg1, arg2)" Define function.
18373 */
18374 p = skipwhite(p);
18375 if (*p != '(')
18376 {
18377 if (!eap->skip)
18378 {
18379 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018380 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 }
18382 /* attempt to continue by skipping some text */
18383 if (vim_strchr(p, '(') != NULL)
18384 p = vim_strchr(p, '(');
18385 }
18386 p = skipwhite(p + 1);
18387
18388 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18389 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18390
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018391 if (!eap->skip)
18392 {
18393 /* Check the name of the function. */
18394 if (name != NULL)
18395 arg = name;
18396 else
18397 arg = fudi.fd_newkey;
18398 if (arg != NULL)
18399 {
18400 if (*arg == K_SPECIAL)
18401 j = 3;
18402 else
18403 j = 0;
18404 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18405 : eval_isnamec(arg[j])))
18406 ++j;
18407 if (arg[j] != NUL)
18408 emsg_funcname(_(e_invarg2), arg);
18409 }
18410 }
18411
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 /*
18413 * Isolate the arguments: "arg1, arg2, ...)"
18414 */
18415 while (*p != ')')
18416 {
18417 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18418 {
18419 varargs = TRUE;
18420 p += 3;
18421 mustend = TRUE;
18422 }
18423 else
18424 {
18425 arg = p;
18426 while (ASCII_ISALNUM(*p) || *p == '_')
18427 ++p;
18428 if (arg == p || isdigit(*arg)
18429 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18430 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18431 {
18432 if (!eap->skip)
18433 EMSG2(_("E125: Illegal argument: %s"), arg);
18434 break;
18435 }
18436 if (ga_grow(&newargs, 1) == FAIL)
18437 goto erret;
18438 c = *p;
18439 *p = NUL;
18440 arg = vim_strsave(arg);
18441 if (arg == NULL)
18442 goto erret;
18443 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18444 *p = c;
18445 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 if (*p == ',')
18447 ++p;
18448 else
18449 mustend = TRUE;
18450 }
18451 p = skipwhite(p);
18452 if (mustend && *p != ')')
18453 {
18454 if (!eap->skip)
18455 EMSG2(_(e_invarg2), eap->arg);
18456 break;
18457 }
18458 }
18459 ++p; /* skip the ')' */
18460
Bram Moolenaare9a41262005-01-15 22:18:47 +000018461 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 for (;;)
18463 {
18464 p = skipwhite(p);
18465 if (STRNCMP(p, "range", 5) == 0)
18466 {
18467 flags |= FC_RANGE;
18468 p += 5;
18469 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018470 else if (STRNCMP(p, "dict", 4) == 0)
18471 {
18472 flags |= FC_DICT;
18473 p += 4;
18474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 else if (STRNCMP(p, "abort", 5) == 0)
18476 {
18477 flags |= FC_ABORT;
18478 p += 5;
18479 }
18480 else
18481 break;
18482 }
18483
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018484 /* When there is a line break use what follows for the function body.
18485 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18486 if (*p == '\n')
18487 line_arg = p + 1;
18488 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 EMSG(_(e_trailing));
18490
18491 /*
18492 * Read the body of the function, until ":endfunction" is found.
18493 */
18494 if (KeyTyped)
18495 {
18496 /* Check if the function already exists, don't let the user type the
18497 * whole function before telling him it doesn't work! For a script we
18498 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018499 if (!eap->skip && !eap->forceit)
18500 {
18501 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18502 EMSG(_(e_funcdict));
18503 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018504 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018507 if (!eap->skip && did_emsg)
18508 goto erret;
18509
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 msg_putchar('\n'); /* don't overwrite the function name */
18511 cmdline_row = msg_row;
18512 }
18513
18514 indent = 2;
18515 nesting = 0;
18516 for (;;)
18517 {
18518 msg_scroll = TRUE;
18519 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018520 sourcing_lnum_off = sourcing_lnum;
18521
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018522 if (line_arg != NULL)
18523 {
18524 /* Use eap->arg, split up in parts by line breaks. */
18525 theline = line_arg;
18526 p = vim_strchr(theline, '\n');
18527 if (p == NULL)
18528 line_arg += STRLEN(line_arg);
18529 else
18530 {
18531 *p = NUL;
18532 line_arg = p + 1;
18533 }
18534 }
18535 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 theline = getcmdline(':', 0L, indent);
18537 else
18538 theline = eap->getline(':', eap->cookie, indent);
18539 if (KeyTyped)
18540 lines_left = Rows - 1;
18541 if (theline == NULL)
18542 {
18543 EMSG(_("E126: Missing :endfunction"));
18544 goto erret;
18545 }
18546
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018547 /* Detect line continuation: sourcing_lnum increased more than one. */
18548 if (sourcing_lnum > sourcing_lnum_off + 1)
18549 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18550 else
18551 sourcing_lnum_off = 0;
18552
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 if (skip_until != NULL)
18554 {
18555 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18556 * don't check for ":endfunc". */
18557 if (STRCMP(theline, skip_until) == 0)
18558 {
18559 vim_free(skip_until);
18560 skip_until = NULL;
18561 }
18562 }
18563 else
18564 {
18565 /* skip ':' and blanks*/
18566 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18567 ;
18568
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018569 /* Check for "endfunction". */
18570 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018572 if (line_arg == NULL)
18573 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 break;
18575 }
18576
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018577 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 * at "end". */
18579 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18580 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018581 else if (STRNCMP(p, "if", 2) == 0
18582 || STRNCMP(p, "wh", 2) == 0
18583 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 || STRNCMP(p, "try", 3) == 0)
18585 indent += 2;
18586
18587 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018588 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018590 if (*p == '!')
18591 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 p += eval_fname_script(p);
18593 if (ASCII_ISALPHA(*p))
18594 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018595 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 if (*skipwhite(p) == '(')
18597 {
18598 ++nesting;
18599 indent += 2;
18600 }
18601 }
18602 }
18603
18604 /* Check for ":append" or ":insert". */
18605 p = skip_range(p, NULL);
18606 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18607 || (p[0] == 'i'
18608 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18609 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18610 skip_until = vim_strsave((char_u *)".");
18611
18612 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18613 arg = skipwhite(skiptowhite(p));
18614 if (arg[0] == '<' && arg[1] =='<'
18615 && ((p[0] == 'p' && p[1] == 'y'
18616 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18617 || (p[0] == 'p' && p[1] == 'e'
18618 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18619 || (p[0] == 't' && p[1] == 'c'
18620 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18621 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18622 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018623 || (p[0] == 'm' && p[1] == 'z'
18624 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 ))
18626 {
18627 /* ":python <<" continues until a dot, like ":append" */
18628 p = skipwhite(arg + 2);
18629 if (*p == NUL)
18630 skip_until = vim_strsave((char_u *)".");
18631 else
18632 skip_until = vim_strsave(p);
18633 }
18634 }
18635
18636 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018637 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018638 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018639 if (line_arg == NULL)
18640 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018642 }
18643
18644 /* Copy the line to newly allocated memory. get_one_sourceline()
18645 * allocates 250 bytes per line, this saves 80% on average. The cost
18646 * is an extra alloc/free. */
18647 p = vim_strsave(theline);
18648 if (p != NULL)
18649 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018650 if (line_arg == NULL)
18651 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018652 theline = p;
18653 }
18654
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018655 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18656
18657 /* Add NULL lines for continuation lines, so that the line count is
18658 * equal to the index in the growarray. */
18659 while (sourcing_lnum_off-- > 0)
18660 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018661
18662 /* Check for end of eap->arg. */
18663 if (line_arg != NULL && *line_arg == NUL)
18664 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 }
18666
18667 /* Don't define the function when skipping commands or when an error was
18668 * detected. */
18669 if (eap->skip || did_emsg)
18670 goto erret;
18671
18672 /*
18673 * If there are no errors, add the function
18674 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018675 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018676 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018677 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018679 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018680 emsg_funcname("E707: Function name conflicts with variable: %s",
18681 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018682 goto erret;
18683 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018684
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018685 fp = find_func(name);
18686 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018688 if (!eap->forceit)
18689 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018690 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018691 goto erret;
18692 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018693 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018694 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018695 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018696 name);
18697 goto erret;
18698 }
18699 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018700 ga_clear_strings(&(fp->uf_args));
18701 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018702 vim_free(name);
18703 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 }
18706 else
18707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018708 char numbuf[20];
18709
18710 fp = NULL;
18711 if (fudi.fd_newkey == NULL && !eap->forceit)
18712 {
18713 EMSG(_(e_funcdict));
18714 goto erret;
18715 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018716 if (fudi.fd_di == NULL)
18717 {
18718 /* Can't add a function to a locked dictionary */
18719 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18720 goto erret;
18721 }
18722 /* Can't change an existing function if it is locked */
18723 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18724 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018725
18726 /* Give the function a sequential number. Can only be used with a
18727 * Funcref! */
18728 vim_free(name);
18729 sprintf(numbuf, "%d", ++func_nr);
18730 name = vim_strsave((char_u *)numbuf);
18731 if (name == NULL)
18732 goto erret;
18733 }
18734
18735 if (fp == NULL)
18736 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018737 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018738 {
18739 int slen, plen;
18740 char_u *scriptname;
18741
18742 /* Check that the autoload name matches the script name. */
18743 j = FAIL;
18744 if (sourcing_name != NULL)
18745 {
18746 scriptname = autoload_name(name);
18747 if (scriptname != NULL)
18748 {
18749 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018750 plen = (int)STRLEN(p);
18751 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018752 if (slen > plen && fnamecmp(p,
18753 sourcing_name + slen - plen) == 0)
18754 j = OK;
18755 vim_free(scriptname);
18756 }
18757 }
18758 if (j == FAIL)
18759 {
18760 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18761 goto erret;
18762 }
18763 }
18764
18765 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 if (fp == NULL)
18767 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018768
18769 if (fudi.fd_dict != NULL)
18770 {
18771 if (fudi.fd_di == NULL)
18772 {
18773 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018774 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018775 if (fudi.fd_di == NULL)
18776 {
18777 vim_free(fp);
18778 goto erret;
18779 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018780 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18781 {
18782 vim_free(fudi.fd_di);
18783 goto erret;
18784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018785 }
18786 else
18787 /* overwrite existing dict entry */
18788 clear_tv(&fudi.fd_di->di_tv);
18789 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018790 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018791 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018792 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018793
18794 /* behave like "dict" was used */
18795 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018796 }
18797
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018799 STRCPY(fp->uf_name, name);
18800 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018802 fp->uf_args = newargs;
18803 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018804#ifdef FEAT_PROFILE
18805 fp->uf_tml_count = NULL;
18806 fp->uf_tml_total = NULL;
18807 fp->uf_tml_self = NULL;
18808 fp->uf_profiling = FALSE;
18809 if (prof_def_func())
18810 func_do_profile(fp);
18811#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018812 fp->uf_varargs = varargs;
18813 fp->uf_flags = flags;
18814 fp->uf_calls = 0;
18815 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018816 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817
18818erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 ga_clear_strings(&newargs);
18820 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018821ret_free:
18822 vim_free(skip_until);
18823 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826}
18827
18828/*
18829 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018830 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018832 * flags:
18833 * TFN_INT: internal function name OK
18834 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 * Advances "pp" to just after the function name (if no error).
18836 */
18837 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018838trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 char_u **pp;
18840 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018841 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018842 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018844 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 char_u *start;
18846 char_u *end;
18847 int lead;
18848 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018850 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018851
18852 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018853 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018855
18856 /* Check for hard coded <SNR>: already translated function ID (from a user
18857 * command). */
18858 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18859 && (*pp)[2] == (int)KE_SNR)
18860 {
18861 *pp += 3;
18862 len = get_id_len(pp) + 3;
18863 return vim_strnsave(start, len);
18864 }
18865
18866 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18867 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018869 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018871
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018872 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18873 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018874 if (end == start)
18875 {
18876 if (!skip)
18877 EMSG(_("E129: Function name required"));
18878 goto theend;
18879 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018880 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018881 {
18882 /*
18883 * Report an invalid expression in braces, unless the expression
18884 * evaluation has been cancelled due to an aborting error, an
18885 * interrupt, or an exception.
18886 */
18887 if (!aborting())
18888 {
18889 if (end != NULL)
18890 EMSG2(_(e_invarg2), start);
18891 }
18892 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018893 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018894 goto theend;
18895 }
18896
18897 if (lv.ll_tv != NULL)
18898 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018899 if (fdp != NULL)
18900 {
18901 fdp->fd_dict = lv.ll_dict;
18902 fdp->fd_newkey = lv.ll_newkey;
18903 lv.ll_newkey = NULL;
18904 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018905 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018906 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18907 {
18908 name = vim_strsave(lv.ll_tv->vval.v_string);
18909 *pp = end;
18910 }
18911 else
18912 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018913 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18914 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018915 EMSG(_(e_funcref));
18916 else
18917 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018918 name = NULL;
18919 }
18920 goto theend;
18921 }
18922
18923 if (lv.ll_name == NULL)
18924 {
18925 /* Error found, but continue after the function name. */
18926 *pp = end;
18927 goto theend;
18928 }
18929
18930 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018931 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018932 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018933 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18934 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18935 {
18936 /* When there was "s:" already or the name expanded to get a
18937 * leading "s:" then remove it. */
18938 lv.ll_name += 2;
18939 len -= 2;
18940 lead = 2;
18941 }
18942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018943 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018944 {
18945 if (lead == 2) /* skip over "s:" */
18946 lv.ll_name += 2;
18947 len = (int)(end - lv.ll_name);
18948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018949
18950 /*
18951 * Copy the function name to allocated memory.
18952 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18953 * Accept <SNR>123_name() outside a script.
18954 */
18955 if (skip)
18956 lead = 0; /* do nothing */
18957 else if (lead > 0)
18958 {
18959 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018960 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018961 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018962 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018963 if (current_SID <= 0)
18964 {
18965 EMSG(_(e_usingsid));
18966 goto theend;
18967 }
18968 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18969 lead += (int)STRLEN(sid_buf);
18970 }
18971 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018972 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018973 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018974 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018975 goto theend;
18976 }
18977 name = alloc((unsigned)(len + lead + 1));
18978 if (name != NULL)
18979 {
18980 if (lead > 0)
18981 {
18982 name[0] = K_SPECIAL;
18983 name[1] = KS_EXTRA;
18984 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018985 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018986 STRCPY(name + 3, sid_buf);
18987 }
18988 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18989 name[len + lead] = NUL;
18990 }
18991 *pp = end;
18992
18993theend:
18994 clear_lval(&lv);
18995 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996}
18997
18998/*
18999 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19000 * Return 2 if "p" starts with "s:".
19001 * Return 0 otherwise.
19002 */
19003 static int
19004eval_fname_script(p)
19005 char_u *p;
19006{
19007 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19008 || STRNICMP(p + 1, "SNR>", 4) == 0))
19009 return 5;
19010 if (p[0] == 's' && p[1] == ':')
19011 return 2;
19012 return 0;
19013}
19014
19015/*
19016 * Return TRUE if "p" starts with "<SID>" or "s:".
19017 * Only works if eval_fname_script() returned non-zero for "p"!
19018 */
19019 static int
19020eval_fname_sid(p)
19021 char_u *p;
19022{
19023 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19024}
19025
19026/*
19027 * List the head of the function: "name(arg1, arg2)".
19028 */
19029 static void
19030list_func_head(fp, indent)
19031 ufunc_T *fp;
19032 int indent;
19033{
19034 int j;
19035
19036 msg_start();
19037 if (indent)
19038 MSG_PUTS(" ");
19039 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019040 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 {
19042 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019043 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 }
19045 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019046 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019048 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 {
19050 if (j)
19051 MSG_PUTS(", ");
19052 msg_puts(FUNCARG(fp, j));
19053 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019054 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 {
19056 if (j)
19057 MSG_PUTS(", ");
19058 MSG_PUTS("...");
19059 }
19060 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019061 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019062 if (p_verbose > 0)
19063 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064}
19065
19066/*
19067 * Find a function by name, return pointer to it in ufuncs.
19068 * Return NULL for unknown function.
19069 */
19070 static ufunc_T *
19071find_func(name)
19072 char_u *name;
19073{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019074 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019076 hi = hash_find(&func_hashtab, name);
19077 if (!HASHITEM_EMPTY(hi))
19078 return HI2UF(hi);
19079 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080}
19081
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019082#if defined(EXITFREE) || defined(PROTO)
19083 void
19084free_all_functions()
19085{
19086 hashitem_T *hi;
19087
19088 /* Need to start all over every time, because func_free() may change the
19089 * hash table. */
19090 while (func_hashtab.ht_used > 0)
19091 for (hi = func_hashtab.ht_array; ; ++hi)
19092 if (!HASHITEM_EMPTY(hi))
19093 {
19094 func_free(HI2UF(hi));
19095 break;
19096 }
19097}
19098#endif
19099
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019100/*
19101 * Return TRUE if a function "name" exists.
19102 */
19103 static int
19104function_exists(name)
19105 char_u *name;
19106{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019107 char_u *nm = name;
19108 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019109 int n = FALSE;
19110
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019111 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019112 if (p != NULL)
19113 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019114 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019115 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019116 else
19117 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019118 vim_free(p);
19119 }
19120 return n;
19121}
19122
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019123/*
19124 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019125 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019126 */
19127 static int
19128builtin_function(name)
19129 char_u *name;
19130{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019131 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19132 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019133}
19134
Bram Moolenaar05159a02005-02-26 23:04:13 +000019135#if defined(FEAT_PROFILE) || defined(PROTO)
19136/*
19137 * Start profiling function "fp".
19138 */
19139 static void
19140func_do_profile(fp)
19141 ufunc_T *fp;
19142{
19143 fp->uf_tm_count = 0;
19144 profile_zero(&fp->uf_tm_self);
19145 profile_zero(&fp->uf_tm_total);
19146 if (fp->uf_tml_count == NULL)
19147 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19148 (sizeof(int) * fp->uf_lines.ga_len));
19149 if (fp->uf_tml_total == NULL)
19150 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19151 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19152 if (fp->uf_tml_self == NULL)
19153 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19154 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19155 fp->uf_tml_idx = -1;
19156 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19157 || fp->uf_tml_self == NULL)
19158 return; /* out of memory */
19159
19160 fp->uf_profiling = TRUE;
19161}
19162
19163/*
19164 * Dump the profiling results for all functions in file "fd".
19165 */
19166 void
19167func_dump_profile(fd)
19168 FILE *fd;
19169{
19170 hashitem_T *hi;
19171 int todo;
19172 ufunc_T *fp;
19173 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019174 ufunc_T **sorttab;
19175 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019176
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019177 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019178 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19179
Bram Moolenaar05159a02005-02-26 23:04:13 +000019180 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19181 {
19182 if (!HASHITEM_EMPTY(hi))
19183 {
19184 --todo;
19185 fp = HI2UF(hi);
19186 if (fp->uf_profiling)
19187 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019188 if (sorttab != NULL)
19189 sorttab[st_len++] = fp;
19190
Bram Moolenaar05159a02005-02-26 23:04:13 +000019191 if (fp->uf_name[0] == K_SPECIAL)
19192 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19193 else
19194 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19195 if (fp->uf_tm_count == 1)
19196 fprintf(fd, "Called 1 time\n");
19197 else
19198 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19199 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19200 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19201 fprintf(fd, "\n");
19202 fprintf(fd, "count total (s) self (s)\n");
19203
19204 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19205 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019206 if (FUNCLINE(fp, i) == NULL)
19207 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019208 prof_func_line(fd, fp->uf_tml_count[i],
19209 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019210 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19211 }
19212 fprintf(fd, "\n");
19213 }
19214 }
19215 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019216
19217 if (sorttab != NULL && st_len > 0)
19218 {
19219 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19220 prof_total_cmp);
19221 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19222 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19223 prof_self_cmp);
19224 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19225 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019226}
Bram Moolenaar73830342005-02-28 22:48:19 +000019227
19228 static void
19229prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19230 FILE *fd;
19231 ufunc_T **sorttab;
19232 int st_len;
19233 char *title;
19234 int prefer_self; /* when equal print only self time */
19235{
19236 int i;
19237 ufunc_T *fp;
19238
19239 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19240 fprintf(fd, "count total (s) self (s) function\n");
19241 for (i = 0; i < 20 && i < st_len; ++i)
19242 {
19243 fp = sorttab[i];
19244 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19245 prefer_self);
19246 if (fp->uf_name[0] == K_SPECIAL)
19247 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19248 else
19249 fprintf(fd, " %s()\n", fp->uf_name);
19250 }
19251 fprintf(fd, "\n");
19252}
19253
19254/*
19255 * Print the count and times for one function or function line.
19256 */
19257 static void
19258prof_func_line(fd, count, total, self, prefer_self)
19259 FILE *fd;
19260 int count;
19261 proftime_T *total;
19262 proftime_T *self;
19263 int prefer_self; /* when equal print only self time */
19264{
19265 if (count > 0)
19266 {
19267 fprintf(fd, "%5d ", count);
19268 if (prefer_self && profile_equal(total, self))
19269 fprintf(fd, " ");
19270 else
19271 fprintf(fd, "%s ", profile_msg(total));
19272 if (!prefer_self && profile_equal(total, self))
19273 fprintf(fd, " ");
19274 else
19275 fprintf(fd, "%s ", profile_msg(self));
19276 }
19277 else
19278 fprintf(fd, " ");
19279}
19280
19281/*
19282 * Compare function for total time sorting.
19283 */
19284 static int
19285#ifdef __BORLANDC__
19286_RTLENTRYF
19287#endif
19288prof_total_cmp(s1, s2)
19289 const void *s1;
19290 const void *s2;
19291{
19292 ufunc_T *p1, *p2;
19293
19294 p1 = *(ufunc_T **)s1;
19295 p2 = *(ufunc_T **)s2;
19296 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19297}
19298
19299/*
19300 * Compare function for self time sorting.
19301 */
19302 static int
19303#ifdef __BORLANDC__
19304_RTLENTRYF
19305#endif
19306prof_self_cmp(s1, s2)
19307 const void *s1;
19308 const void *s2;
19309{
19310 ufunc_T *p1, *p2;
19311
19312 p1 = *(ufunc_T **)s1;
19313 p2 = *(ufunc_T **)s2;
19314 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19315}
19316
Bram Moolenaar05159a02005-02-26 23:04:13 +000019317#endif
19318
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019319/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019320 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019321 * Return TRUE if a package was loaded.
19322 */
19323 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019324script_autoload(name, reload)
19325 char_u *name;
19326 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019327{
19328 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019329 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019330 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019331 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019332
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019333 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019334 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019335 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019336 return FALSE;
19337
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019338 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019339
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019340 /* Find the name in the list of previously loaded package names. Skip
19341 * "autoload/", it's always the same. */
19342 for (i = 0; i < ga_loaded.ga_len; ++i)
19343 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19344 break;
19345 if (!reload && i < ga_loaded.ga_len)
19346 ret = FALSE; /* was loaded already */
19347 else
19348 {
19349 /* Remember the name if it wasn't loaded already. */
19350 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19351 {
19352 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19353 tofree = NULL;
19354 }
19355
19356 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019357 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019358 ret = TRUE;
19359 }
19360
19361 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019362 return ret;
19363}
19364
19365/*
19366 * Return the autoload script name for a function or variable name.
19367 * Returns NULL when out of memory.
19368 */
19369 static char_u *
19370autoload_name(name)
19371 char_u *name;
19372{
19373 char_u *p;
19374 char_u *scriptname;
19375
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019376 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019377 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19378 if (scriptname == NULL)
19379 return FALSE;
19380 STRCPY(scriptname, "autoload/");
19381 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019382 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019383 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019384 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019385 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019386 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019387}
19388
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19390
19391/*
19392 * Function given to ExpandGeneric() to obtain the list of user defined
19393 * function names.
19394 */
19395 char_u *
19396get_user_func_name(xp, idx)
19397 expand_T *xp;
19398 int idx;
19399{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019400 static long_u done;
19401 static hashitem_T *hi;
19402 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403
19404 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019406 done = 0;
19407 hi = func_hashtab.ht_array;
19408 }
19409 if (done < func_hashtab.ht_used)
19410 {
19411 if (done++ > 0)
19412 ++hi;
19413 while (HASHITEM_EMPTY(hi))
19414 ++hi;
19415 fp = HI2UF(hi);
19416
19417 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19418 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419
19420 cat_func_name(IObuff, fp);
19421 if (xp->xp_context != EXPAND_USER_FUNC)
19422 {
19423 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019424 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 STRCAT(IObuff, ")");
19426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 return IObuff;
19428 }
19429 return NULL;
19430}
19431
19432#endif /* FEAT_CMDL_COMPL */
19433
19434/*
19435 * Copy the function name of "fp" to buffer "buf".
19436 * "buf" must be able to hold the function name plus three bytes.
19437 * Takes care of script-local function names.
19438 */
19439 static void
19440cat_func_name(buf, fp)
19441 char_u *buf;
19442 ufunc_T *fp;
19443{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019444 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 {
19446 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019447 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 }
19449 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019450 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451}
19452
19453/*
19454 * ":delfunction {name}"
19455 */
19456 void
19457ex_delfunction(eap)
19458 exarg_T *eap;
19459{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019460 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 char_u *p;
19462 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464
19465 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019466 name = trans_function_name(&p, eap->skip, 0, &fudi);
19467 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019469 {
19470 if (fudi.fd_dict != NULL && !eap->skip)
19471 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 if (!ends_excmd(*skipwhite(p)))
19475 {
19476 vim_free(name);
19477 EMSG(_(e_trailing));
19478 return;
19479 }
19480 eap->nextcmd = check_nextcmd(p);
19481 if (eap->nextcmd != NULL)
19482 *p = NUL;
19483
19484 if (!eap->skip)
19485 fp = find_func(name);
19486 vim_free(name);
19487
19488 if (!eap->skip)
19489 {
19490 if (fp == NULL)
19491 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019492 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 return;
19494 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019495 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 {
19497 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19498 return;
19499 }
19500
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019501 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019503 /* Delete the dict item that refers to the function, it will
19504 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019505 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019507 else
19508 func_free(fp);
19509 }
19510}
19511
19512/*
19513 * Free a function and remove it from the list of functions.
19514 */
19515 static void
19516func_free(fp)
19517 ufunc_T *fp;
19518{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019519 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019520
19521 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019522 ga_clear_strings(&(fp->uf_args));
19523 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019524#ifdef FEAT_PROFILE
19525 vim_free(fp->uf_tml_count);
19526 vim_free(fp->uf_tml_total);
19527 vim_free(fp->uf_tml_self);
19528#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019529
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019530 /* remove the function from the function hashtable */
19531 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19532 if (HASHITEM_EMPTY(hi))
19533 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019534 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019535 hash_remove(&func_hashtab, hi);
19536
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019537 vim_free(fp);
19538}
19539
19540/*
19541 * Unreference a Function: decrement the reference count and free it when it
19542 * becomes zero. Only for numbered functions.
19543 */
19544 static void
19545func_unref(name)
19546 char_u *name;
19547{
19548 ufunc_T *fp;
19549
19550 if (name != NULL && isdigit(*name))
19551 {
19552 fp = find_func(name);
19553 if (fp == NULL)
19554 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019555 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019556 {
19557 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019558 * when "uf_calls" becomes zero. */
19559 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019560 func_free(fp);
19561 }
19562 }
19563}
19564
19565/*
19566 * Count a reference to a Function.
19567 */
19568 static void
19569func_ref(name)
19570 char_u *name;
19571{
19572 ufunc_T *fp;
19573
19574 if (name != NULL && isdigit(*name))
19575 {
19576 fp = find_func(name);
19577 if (fp == NULL)
19578 EMSG2(_(e_intern2), "func_ref()");
19579 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019580 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 }
19582}
19583
19584/*
19585 * Call a user function.
19586 */
19587 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019588call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 ufunc_T *fp; /* pointer to function */
19590 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 typval_T *argvars; /* arguments */
19592 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 linenr_T firstline; /* first line of range */
19594 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596{
Bram Moolenaar33570922005-01-25 22:26:29 +000019597 char_u *save_sourcing_name;
19598 linenr_T save_sourcing_lnum;
19599 scid_T save_current_SID;
19600 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019601 int save_did_emsg;
19602 static int depth = 0;
19603 dictitem_T *v;
19604 int fixvar_idx = 0; /* index in fixvar[] */
19605 int i;
19606 int ai;
19607 char_u numbuf[NUMBUFLEN];
19608 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019609#ifdef FEAT_PROFILE
19610 proftime_T wait_start;
19611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612
19613 /* If depth of calling is getting too high, don't execute the function */
19614 if (depth >= p_mfd)
19615 {
19616 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019617 rettv->v_type = VAR_NUMBER;
19618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 return;
19620 }
19621 ++depth;
19622
19623 line_breakcheck(); /* check for CTRL-C hit */
19624
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019625 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 fc.rettv = rettv;
19629 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 fc.linenr = 0;
19631 fc.returned = FALSE;
19632 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019634 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 fc.dbg_tick = debug_tick;
19636
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 /*
19638 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19639 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19640 * each argument variable and saves a lot of time.
19641 */
19642 /*
19643 * Init l: variables.
19644 */
19645 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019646 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019647 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019648 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19649 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019651 name = v->di_key;
19652 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19654 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19655 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019656 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019657 v->di_tv.vval.v_dict = selfdict;
19658 ++selfdict->dv_refcount;
19659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019660
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 /*
19662 * Init a: variables.
19663 * Set a:0 to "argcount".
19664 * Set a:000 to a list with room for the "..." arguments.
19665 */
19666 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19667 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019668 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 v = &fc.fixvar[fixvar_idx++].var;
19670 STRCPY(v->di_key, "000");
19671 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19672 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19673 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019674 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 v->di_tv.vval.v_list = &fc.l_varlist;
19676 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19677 fc.l_varlist.lv_refcount = 99999;
19678
19679 /*
19680 * Set a:firstline to "firstline" and a:lastline to "lastline".
19681 * Set a:name to named arguments.
19682 * Set a:N to the "..." arguments.
19683 */
19684 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19685 (varnumber_T)firstline);
19686 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19687 (varnumber_T)lastline);
19688 for (i = 0; i < argcount; ++i)
19689 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019690 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 if (ai < 0)
19692 /* named argument a:name */
19693 name = FUNCARG(fp, i);
19694 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019695 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 /* "..." argument a:1, a:2, etc. */
19697 sprintf((char *)numbuf, "%d", ai + 1);
19698 name = numbuf;
19699 }
19700 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19701 {
19702 v = &fc.fixvar[fixvar_idx++].var;
19703 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19704 }
19705 else
19706 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019707 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19708 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019709 if (v == NULL)
19710 break;
19711 v->di_flags = DI_FLAGS_RO;
19712 }
19713 STRCPY(v->di_key, name);
19714 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19715
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019716 /* Note: the values are copied directly to avoid alloc/free.
19717 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019718 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019719 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019720
19721 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19722 {
19723 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19724 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019725 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019726 }
19727 }
19728
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 /* Don't redraw while executing the function. */
19730 ++RedrawingDisabled;
19731 save_sourcing_name = sourcing_name;
19732 save_sourcing_lnum = sourcing_lnum;
19733 sourcing_lnum = 1;
19734 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019735 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 if (sourcing_name != NULL)
19737 {
19738 if (save_sourcing_name != NULL
19739 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19740 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19741 else
19742 STRCPY(sourcing_name, "function ");
19743 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19744
19745 if (p_verbose >= 12)
19746 {
19747 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019748 verbose_enter_scroll();
19749
Bram Moolenaar555b2802005-05-19 21:08:39 +000019750 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 if (p_verbose >= 14)
19752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019754 char_u numbuf[NUMBUFLEN];
19755 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756
19757 msg_puts((char_u *)"(");
19758 for (i = 0; i < argcount; ++i)
19759 {
19760 if (i > 0)
19761 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019762 if (argvars[i].v_type == VAR_NUMBER)
19763 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 else
19765 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019766 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019767 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019769 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 }
19771 }
19772 msg_puts((char_u *)")");
19773 }
19774 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019775
19776 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 --no_wait_return;
19778 }
19779 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019780#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019781 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019782 {
19783 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19784 func_do_profile(fp);
19785 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019786 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019787 {
19788 ++fp->uf_tm_count;
19789 profile_start(&fp->uf_tm_start);
19790 profile_zero(&fp->uf_tm_children);
19791 }
19792 script_prof_save(&wait_start);
19793 }
19794#endif
19795
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019797 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 save_did_emsg = did_emsg;
19799 did_emsg = FALSE;
19800
19801 /* call do_cmdline() to execute the lines */
19802 do_cmdline(NULL, get_func_line, (void *)&fc,
19803 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19804
19805 --RedrawingDisabled;
19806
19807 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019808 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019810 clear_tv(rettv);
19811 rettv->v_type = VAR_NUMBER;
19812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 }
19814
Bram Moolenaar05159a02005-02-26 23:04:13 +000019815#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019816 if (do_profiling == PROF_YES && (fp->uf_profiling
19817 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019818 {
19819 profile_end(&fp->uf_tm_start);
19820 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19821 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019822 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019823 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019824 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019825 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19826 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019827 }
19828 }
19829#endif
19830
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 /* when being verbose, mention the return value */
19832 if (p_verbose >= 12)
19833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019835 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019838 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019840 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19841 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019844 char_u buf[MSG_BUF_LEN];
19845 char_u numbuf[NUMBUFLEN];
19846 char_u *tofree;
19847
Bram Moolenaar555b2802005-05-19 21:08:39 +000019848 /* The value may be very long. Skip the middle part, so that we
19849 * have some idea how it starts and ends. smsg() would always
19850 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019851 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019852 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019853 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019854 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 }
19856 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019857
19858 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 --no_wait_return;
19860 }
19861
19862 vim_free(sourcing_name);
19863 sourcing_name = save_sourcing_name;
19864 sourcing_lnum = save_sourcing_lnum;
19865 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019866#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019867 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019868 script_prof_restore(&wait_start);
19869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870
19871 if (p_verbose >= 12 && sourcing_name != NULL)
19872 {
19873 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019874 verbose_enter_scroll();
19875
Bram Moolenaar555b2802005-05-19 21:08:39 +000019876 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019878
19879 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 --no_wait_return;
19881 }
19882
19883 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019884 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 /* The a: variables typevals were not alloced, only free the allocated
19887 * variables. */
19888 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19889
19890 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 --depth;
19892}
19893
19894/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 * Add a number variable "name" to dict "dp" with value "nr".
19896 */
19897 static void
19898add_nr_var(dp, v, name, nr)
19899 dict_T *dp;
19900 dictitem_T *v;
19901 char *name;
19902 varnumber_T nr;
19903{
19904 STRCPY(v->di_key, name);
19905 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19906 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19907 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019908 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019909 v->di_tv.vval.v_number = nr;
19910}
19911
19912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 * ":return [expr]"
19914 */
19915 void
19916ex_return(eap)
19917 exarg_T *eap;
19918{
19919 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 int returning = FALSE;
19922
19923 if (current_funccal == NULL)
19924 {
19925 EMSG(_("E133: :return not inside a function"));
19926 return;
19927 }
19928
19929 if (eap->skip)
19930 ++emsg_skip;
19931
19932 eap->nextcmd = NULL;
19933 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019934 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 {
19936 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 }
19941 /* It's safer to return also on error. */
19942 else if (!eap->skip)
19943 {
19944 /*
19945 * Return unless the expression evaluation has been cancelled due to an
19946 * aborting error, an interrupt, or an exception.
19947 */
19948 if (!aborting())
19949 returning = do_return(eap, FALSE, TRUE, NULL);
19950 }
19951
19952 /* When skipping or the return gets pending, advance to the next command
19953 * in this line (!returning). Otherwise, ignore the rest of the line.
19954 * Following lines will be ignored by get_func_line(). */
19955 if (returning)
19956 eap->nextcmd = NULL;
19957 else if (eap->nextcmd == NULL) /* no argument */
19958 eap->nextcmd = check_nextcmd(arg);
19959
19960 if (eap->skip)
19961 --emsg_skip;
19962}
19963
19964/*
19965 * Return from a function. Possibly makes the return pending. Also called
19966 * for a pending return at the ":endtry" or after returning from an extra
19967 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019968 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 * FALSE when the return gets pending.
19971 */
19972 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 exarg_T *eap;
19975 int reanimate;
19976 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019977 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978{
19979 int idx;
19980 struct condstack *cstack = eap->cstack;
19981
19982 if (reanimate)
19983 /* Undo the return. */
19984 current_funccal->returned = FALSE;
19985
19986 /*
19987 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19988 * not in its finally clause (which then is to be executed next) is found.
19989 * In this case, make the ":return" pending for execution at the ":endtry".
19990 * Otherwise, return normally.
19991 */
19992 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19993 if (idx >= 0)
19994 {
19995 cstack->cs_pending[idx] = CSTP_RETURN;
19996
19997 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019998 /* A pending return again gets pending. "rettv" points to an
19999 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020001 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 else
20003 {
20004 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020009 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 {
20011 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020012 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020013 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 else
20015 EMSG(_(e_outofmem));
20016 }
20017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019
20020 if (reanimate)
20021 {
20022 /* The pending return value could be overwritten by a ":return"
20023 * without argument in a finally clause; reset the default
20024 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 current_funccal->rettv->v_type = VAR_NUMBER;
20026 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 }
20028 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 }
20031 else
20032 {
20033 current_funccal->returned = TRUE;
20034
20035 /* If the return is carried out now, store the return value. For
20036 * a return immediately after reanimation, the value is already
20037 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 }
20045 }
20046
20047 return idx < 0;
20048}
20049
20050/*
20051 * Free the variable with a pending return value.
20052 */
20053 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054discard_pending_return(rettv)
20055 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056{
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058}
20059
20060/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 * is an allocated string. Used by report_pending() for verbose messages.
20063 */
20064 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020065get_return_cmd(rettv)
20066 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020068 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020069 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020072 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020073 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020074 if (s == NULL)
20075 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076
20077 STRCPY(IObuff, ":return ");
20078 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20079 if (STRLEN(s) + 8 >= IOSIZE)
20080 STRCPY(IObuff + IOSIZE - 4, "...");
20081 vim_free(tofree);
20082 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083}
20084
20085/*
20086 * Get next function line.
20087 * Called by do_cmdline() to get the next line.
20088 * Returns allocated string, or NULL for end of function.
20089 */
20090/* ARGSUSED */
20091 char_u *
20092get_func_line(c, cookie, indent)
20093 int c; /* not used */
20094 void *cookie;
20095 int indent; /* not used */
20096{
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020098 ufunc_T *fp = fcp->func;
20099 char_u *retval;
20100 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101
20102 /* If breakpoints have been added/deleted need to check for it. */
20103 if (fcp->dbg_tick != debug_tick)
20104 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020105 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 sourcing_lnum);
20107 fcp->dbg_tick = debug_tick;
20108 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020109#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020110 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020111 func_line_end(cookie);
20112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113
Bram Moolenaar05159a02005-02-26 23:04:13 +000020114 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020115 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20116 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 retval = NULL;
20118 else
20119 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020120 /* Skip NULL lines (continuation lines). */
20121 while (fcp->linenr < gap->ga_len
20122 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20123 ++fcp->linenr;
20124 if (fcp->linenr >= gap->ga_len)
20125 retval = NULL;
20126 else
20127 {
20128 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20129 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020130#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020131 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020132 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020133#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 }
20136
20137 /* Did we encounter a breakpoint? */
20138 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20139 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020140 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 /* Find next breakpoint. */
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 }
20146
20147 return retval;
20148}
20149
Bram Moolenaar05159a02005-02-26 23:04:13 +000020150#if defined(FEAT_PROFILE) || defined(PROTO)
20151/*
20152 * Called when starting to read a function line.
20153 * "sourcing_lnum" must be correct!
20154 * When skipping lines it may not actually be executed, but we won't find out
20155 * until later and we need to store the time now.
20156 */
20157 void
20158func_line_start(cookie)
20159 void *cookie;
20160{
20161 funccall_T *fcp = (funccall_T *)cookie;
20162 ufunc_T *fp = fcp->func;
20163
20164 if (fp->uf_profiling && sourcing_lnum >= 1
20165 && sourcing_lnum <= fp->uf_lines.ga_len)
20166 {
20167 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020168 /* Skip continuation lines. */
20169 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20170 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020171 fp->uf_tml_execed = FALSE;
20172 profile_start(&fp->uf_tml_start);
20173 profile_zero(&fp->uf_tml_children);
20174 profile_get_wait(&fp->uf_tml_wait);
20175 }
20176}
20177
20178/*
20179 * Called when actually executing a function line.
20180 */
20181 void
20182func_line_exec(cookie)
20183 void *cookie;
20184{
20185 funccall_T *fcp = (funccall_T *)cookie;
20186 ufunc_T *fp = fcp->func;
20187
20188 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20189 fp->uf_tml_execed = TRUE;
20190}
20191
20192/*
20193 * Called when done with a function line.
20194 */
20195 void
20196func_line_end(cookie)
20197 void *cookie;
20198{
20199 funccall_T *fcp = (funccall_T *)cookie;
20200 ufunc_T *fp = fcp->func;
20201
20202 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20203 {
20204 if (fp->uf_tml_execed)
20205 {
20206 ++fp->uf_tml_count[fp->uf_tml_idx];
20207 profile_end(&fp->uf_tml_start);
20208 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020209 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020210 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20211 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020212 }
20213 fp->uf_tml_idx = -1;
20214 }
20215}
20216#endif
20217
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218/*
20219 * Return TRUE if the currently active function should be ended, because a
20220 * return was encountered or an error occured. Used inside a ":while".
20221 */
20222 int
20223func_has_ended(cookie)
20224 void *cookie;
20225{
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227
20228 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20229 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020230 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 || fcp->returned);
20232}
20233
20234/*
20235 * return TRUE if cookie indicates a function which "abort"s on errors.
20236 */
20237 int
20238func_has_abort(cookie)
20239 void *cookie;
20240{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020241 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242}
20243
20244#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20245typedef enum
20246{
20247 VAR_FLAVOUR_DEFAULT,
20248 VAR_FLAVOUR_SESSION,
20249 VAR_FLAVOUR_VIMINFO
20250} var_flavour_T;
20251
20252static var_flavour_T var_flavour __ARGS((char_u *varname));
20253
20254 static var_flavour_T
20255var_flavour(varname)
20256 char_u *varname;
20257{
20258 char_u *p = varname;
20259
20260 if (ASCII_ISUPPER(*p))
20261 {
20262 while (*(++p))
20263 if (ASCII_ISLOWER(*p))
20264 return VAR_FLAVOUR_SESSION;
20265 return VAR_FLAVOUR_VIMINFO;
20266 }
20267 else
20268 return VAR_FLAVOUR_DEFAULT;
20269}
20270#endif
20271
20272#if defined(FEAT_VIMINFO) || defined(PROTO)
20273/*
20274 * Restore global vars that start with a capital from the viminfo file
20275 */
20276 int
20277read_viminfo_varlist(virp, writing)
20278 vir_T *virp;
20279 int writing;
20280{
20281 char_u *tab;
20282 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284
20285 if (!writing && (find_viminfo_parameter('!') != NULL))
20286 {
20287 tab = vim_strchr(virp->vir_line + 1, '\t');
20288 if (tab != NULL)
20289 {
20290 *tab++ = '\0'; /* isolate the variable name */
20291 if (*tab == 'S') /* string var */
20292 is_string = TRUE;
20293
20294 tab = vim_strchr(tab, '\t');
20295 if (tab != NULL)
20296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 if (is_string)
20298 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020299 tv.v_type = VAR_STRING;
20300 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
20303 else
20304 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020305 tv.v_type = VAR_NUMBER;
20306 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020308 set_var(virp->vir_line + 1, &tv, FALSE);
20309 if (is_string)
20310 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 }
20312 }
20313 }
20314
20315 return viminfo_readline(virp);
20316}
20317
20318/*
20319 * Write global vars that start with a capital to the viminfo file
20320 */
20321 void
20322write_viminfo_varlist(fp)
20323 FILE *fp;
20324{
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 hashitem_T *hi;
20326 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020327 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020328 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020329 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020331 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332
20333 if (find_viminfo_parameter('!') == NULL)
20334 return;
20335
20336 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020338 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020339 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020341 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020343 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020344 this_var = HI2DI(hi);
20345 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020346 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020348 {
20349 case VAR_STRING: s = "STR"; break;
20350 case VAR_NUMBER: s = "NUM"; break;
20351 default: continue;
20352 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020353 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020354 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020355 if (p != NULL)
20356 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020357 vim_free(tofree);
20358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 }
20360 }
20361}
20362#endif
20363
20364#if defined(FEAT_SESSION) || defined(PROTO)
20365 int
20366store_session_globals(fd)
20367 FILE *fd;
20368{
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 hashitem_T *hi;
20370 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020371 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 char_u *p, *t;
20373
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020374 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020377 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020379 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020380 this_var = HI2DI(hi);
20381 if ((this_var->di_tv.v_type == VAR_NUMBER
20382 || this_var->di_tv.v_type == VAR_STRING)
20383 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020384 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020385 /* Escape special characters with a backslash. Turn a LF and
20386 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020388 (char_u *)"\\\"\n\r");
20389 if (p == NULL) /* out of memory */
20390 break;
20391 for (t = p; *t != NUL; ++t)
20392 if (*t == '\n')
20393 *t = 'n';
20394 else if (*t == '\r')
20395 *t = 'r';
20396 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020397 this_var->di_key,
20398 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20399 : ' ',
20400 p,
20401 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20402 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020403 || put_eol(fd) == FAIL)
20404 {
20405 vim_free(p);
20406 return FAIL;
20407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 vim_free(p);
20409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 }
20411 }
20412 return OK;
20413}
20414#endif
20415
Bram Moolenaar661b1822005-07-28 22:36:45 +000020416/*
20417 * Display script name where an item was last set.
20418 * Should only be invoked when 'verbose' is non-zero.
20419 */
20420 void
20421last_set_msg(scriptID)
20422 scid_T scriptID;
20423{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020424 char_u *p;
20425
Bram Moolenaar661b1822005-07-28 22:36:45 +000020426 if (scriptID != 0)
20427 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020428 p = home_replace_save(NULL, get_scriptname(scriptID));
20429 if (p != NULL)
20430 {
20431 verbose_enter();
20432 MSG_PUTS(_("\n\tLast set from "));
20433 MSG_PUTS(p);
20434 vim_free(p);
20435 verbose_leave();
20436 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020437 }
20438}
20439
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440#endif /* FEAT_EVAL */
20441
20442#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20443
20444
20445#ifdef WIN3264
20446/*
20447 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20448 */
20449static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20450static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20451static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20452
20453/*
20454 * Get the short pathname of a file.
20455 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20456 */
20457 static int
20458get_short_pathname(fnamep, bufp, fnamelen)
20459 char_u **fnamep;
20460 char_u **bufp;
20461 int *fnamelen;
20462{
20463 int l,len;
20464 char_u *newbuf;
20465
20466 len = *fnamelen;
20467
20468 l = GetShortPathName(*fnamep, *fnamep, len);
20469 if (l > len - 1)
20470 {
20471 /* If that doesn't work (not enough space), then save the string
20472 * and try again with a new buffer big enough
20473 */
20474 newbuf = vim_strnsave(*fnamep, l);
20475 if (newbuf == NULL)
20476 return 0;
20477
20478 vim_free(*bufp);
20479 *fnamep = *bufp = newbuf;
20480
20481 l = GetShortPathName(*fnamep,*fnamep,l+1);
20482
20483 /* Really should always succeed, as the buffer is big enough */
20484 }
20485
20486 *fnamelen = l;
20487 return 1;
20488}
20489
20490/*
20491 * Create a short path name. Returns the length of the buffer it needs.
20492 * Doesn't copy over the end of the buffer passed in.
20493 */
20494 static int
20495shortpath_for_invalid_fname(fname, bufp, fnamelen)
20496 char_u **fname;
20497 char_u **bufp;
20498 int *fnamelen;
20499{
20500 char_u *s, *p, *pbuf2, *pbuf3;
20501 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020502 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503
20504 /* Make a copy */
20505 len2 = *fnamelen;
20506 pbuf2 = vim_strnsave(*fname, len2);
20507 pbuf3 = NULL;
20508
20509 s = pbuf2 + len2 - 1; /* Find the end */
20510 slen = 1;
20511 plen = len2;
20512
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020513 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 {
20515 --s;
20516 ++slen;
20517 --plen;
20518 }
20519
20520 do
20521 {
20522 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020523 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 {
20525 --s;
20526 ++slen;
20527 --plen;
20528 }
20529 if (s <= pbuf2)
20530 break;
20531
20532 /* Remeber the character that is about to be blatted */
20533 ch = *s;
20534 *s = 0; /* get_short_pathname requires a null-terminated string */
20535
20536 /* Try it in situ */
20537 p = pbuf2;
20538 if (!get_short_pathname(&p, &pbuf3, &plen))
20539 {
20540 vim_free(pbuf2);
20541 return -1;
20542 }
20543 *s = ch; /* Preserve the string */
20544 } while (plen == 0);
20545
20546 if (plen > 0)
20547 {
20548 /* Remeber the length of the new string. */
20549 *fnamelen = len = plen + slen;
20550 vim_free(*bufp);
20551 if (len > len2)
20552 {
20553 /* If there's not enough space in the currently allocated string,
20554 * then copy it to a buffer big enough.
20555 */
20556 *fname= *bufp = vim_strnsave(p, len);
20557 if (*fname == NULL)
20558 return -1;
20559 }
20560 else
20561 {
20562 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20563 *fname = *bufp = pbuf2;
20564 if (p != pbuf2)
20565 strncpy(*fname, p, plen);
20566 pbuf2 = NULL;
20567 }
20568 /* Concat the next bit */
20569 strncpy(*fname + plen, s, slen);
20570 (*fname)[len] = '\0';
20571 }
20572 vim_free(pbuf3);
20573 vim_free(pbuf2);
20574 return 0;
20575}
20576
20577/*
20578 * Get a pathname for a partial path.
20579 */
20580 static int
20581shortpath_for_partial(fnamep, bufp, fnamelen)
20582 char_u **fnamep;
20583 char_u **bufp;
20584 int *fnamelen;
20585{
20586 int sepcount, len, tflen;
20587 char_u *p;
20588 char_u *pbuf, *tfname;
20589 int hasTilde;
20590
20591 /* Count up the path seperators from the RHS.. so we know which part
20592 * of the path to return.
20593 */
20594 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020595 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 if (vim_ispathsep(*p))
20597 ++sepcount;
20598
20599 /* Need full path first (use expand_env() to remove a "~/") */
20600 hasTilde = (**fnamep == '~');
20601 if (hasTilde)
20602 pbuf = tfname = expand_env_save(*fnamep);
20603 else
20604 pbuf = tfname = FullName_save(*fnamep, FALSE);
20605
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020606 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607
20608 if (!get_short_pathname(&tfname, &pbuf, &len))
20609 return -1;
20610
20611 if (len == 0)
20612 {
20613 /* Don't have a valid filename, so shorten the rest of the
20614 * path if we can. This CAN give us invalid 8.3 filenames, but
20615 * there's not a lot of point in guessing what it might be.
20616 */
20617 len = tflen;
20618 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20619 return -1;
20620 }
20621
20622 /* Count the paths backward to find the beginning of the desired string. */
20623 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020624 {
20625#ifdef FEAT_MBYTE
20626 if (has_mbyte)
20627 p -= mb_head_off(tfname, p);
20628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 if (vim_ispathsep(*p))
20630 {
20631 if (sepcount == 0 || (hasTilde && sepcount == 1))
20632 break;
20633 else
20634 sepcount --;
20635 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 if (hasTilde)
20638 {
20639 --p;
20640 if (p >= tfname)
20641 *p = '~';
20642 else
20643 return -1;
20644 }
20645 else
20646 ++p;
20647
20648 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20649 vim_free(*bufp);
20650 *fnamelen = (int)STRLEN(p);
20651 *bufp = pbuf;
20652 *fnamep = p;
20653
20654 return 0;
20655}
20656#endif /* WIN3264 */
20657
20658/*
20659 * Adjust a filename, according to a string of modifiers.
20660 * *fnamep must be NUL terminated when called. When returning, the length is
20661 * determined by *fnamelen.
20662 * Returns valid flags.
20663 * When there is an error, *fnamep is set to NULL.
20664 */
20665 int
20666modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20667 char_u *src; /* string with modifiers */
20668 int *usedlen; /* characters after src that are used */
20669 char_u **fnamep; /* file name so far */
20670 char_u **bufp; /* buffer for allocated file name or NULL */
20671 int *fnamelen; /* length of fnamep */
20672{
20673 int valid = 0;
20674 char_u *tail;
20675 char_u *s, *p, *pbuf;
20676 char_u dirname[MAXPATHL];
20677 int c;
20678 int has_fullname = 0;
20679#ifdef WIN3264
20680 int has_shortname = 0;
20681#endif
20682
20683repeat:
20684 /* ":p" - full path/file_name */
20685 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20686 {
20687 has_fullname = 1;
20688
20689 valid |= VALID_PATH;
20690 *usedlen += 2;
20691
20692 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20693 if ((*fnamep)[0] == '~'
20694#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20695 && ((*fnamep)[1] == '/'
20696# ifdef BACKSLASH_IN_FILENAME
20697 || (*fnamep)[1] == '\\'
20698# endif
20699 || (*fnamep)[1] == NUL)
20700
20701#endif
20702 )
20703 {
20704 *fnamep = expand_env_save(*fnamep);
20705 vim_free(*bufp); /* free any allocated file name */
20706 *bufp = *fnamep;
20707 if (*fnamep == NULL)
20708 return -1;
20709 }
20710
20711 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020712 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 {
20714 if (vim_ispathsep(*p)
20715 && p[1] == '.'
20716 && (p[2] == NUL
20717 || vim_ispathsep(p[2])
20718 || (p[2] == '.'
20719 && (p[3] == NUL || vim_ispathsep(p[3])))))
20720 break;
20721 }
20722
20723 /* FullName_save() is slow, don't use it when not needed. */
20724 if (*p != NUL || !vim_isAbsName(*fnamep))
20725 {
20726 *fnamep = FullName_save(*fnamep, *p != NUL);
20727 vim_free(*bufp); /* free any allocated file name */
20728 *bufp = *fnamep;
20729 if (*fnamep == NULL)
20730 return -1;
20731 }
20732
20733 /* Append a path separator to a directory. */
20734 if (mch_isdir(*fnamep))
20735 {
20736 /* Make room for one or two extra characters. */
20737 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20738 vim_free(*bufp); /* free any allocated file name */
20739 *bufp = *fnamep;
20740 if (*fnamep == NULL)
20741 return -1;
20742 add_pathsep(*fnamep);
20743 }
20744 }
20745
20746 /* ":." - path relative to the current directory */
20747 /* ":~" - path relative to the home directory */
20748 /* ":8" - shortname path - postponed till after */
20749 while (src[*usedlen] == ':'
20750 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20751 {
20752 *usedlen += 2;
20753 if (c == '8')
20754 {
20755#ifdef WIN3264
20756 has_shortname = 1; /* Postpone this. */
20757#endif
20758 continue;
20759 }
20760 pbuf = NULL;
20761 /* Need full path first (use expand_env() to remove a "~/") */
20762 if (!has_fullname)
20763 {
20764 if (c == '.' && **fnamep == '~')
20765 p = pbuf = expand_env_save(*fnamep);
20766 else
20767 p = pbuf = FullName_save(*fnamep, FALSE);
20768 }
20769 else
20770 p = *fnamep;
20771
20772 has_fullname = 0;
20773
20774 if (p != NULL)
20775 {
20776 if (c == '.')
20777 {
20778 mch_dirname(dirname, MAXPATHL);
20779 s = shorten_fname(p, dirname);
20780 if (s != NULL)
20781 {
20782 *fnamep = s;
20783 if (pbuf != NULL)
20784 {
20785 vim_free(*bufp); /* free any allocated file name */
20786 *bufp = pbuf;
20787 pbuf = NULL;
20788 }
20789 }
20790 }
20791 else
20792 {
20793 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20794 /* Only replace it when it starts with '~' */
20795 if (*dirname == '~')
20796 {
20797 s = vim_strsave(dirname);
20798 if (s != NULL)
20799 {
20800 *fnamep = s;
20801 vim_free(*bufp);
20802 *bufp = s;
20803 }
20804 }
20805 }
20806 vim_free(pbuf);
20807 }
20808 }
20809
20810 tail = gettail(*fnamep);
20811 *fnamelen = (int)STRLEN(*fnamep);
20812
20813 /* ":h" - head, remove "/file_name", can be repeated */
20814 /* Don't remove the first "/" or "c:\" */
20815 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20816 {
20817 valid |= VALID_HEAD;
20818 *usedlen += 2;
20819 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020820 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 --tail;
20822 *fnamelen = (int)(tail - *fnamep);
20823#ifdef VMS
20824 if (*fnamelen > 0)
20825 *fnamelen += 1; /* the path separator is part of the path */
20826#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020827 while (tail > s && !after_pathsep(s, tail))
20828 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 }
20830
20831 /* ":8" - shortname */
20832 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20833 {
20834 *usedlen += 2;
20835#ifdef WIN3264
20836 has_shortname = 1;
20837#endif
20838 }
20839
20840#ifdef WIN3264
20841 /* Check shortname after we have done 'heads' and before we do 'tails'
20842 */
20843 if (has_shortname)
20844 {
20845 pbuf = NULL;
20846 /* Copy the string if it is shortened by :h */
20847 if (*fnamelen < (int)STRLEN(*fnamep))
20848 {
20849 p = vim_strnsave(*fnamep, *fnamelen);
20850 if (p == 0)
20851 return -1;
20852 vim_free(*bufp);
20853 *bufp = *fnamep = p;
20854 }
20855
20856 /* Split into two implementations - makes it easier. First is where
20857 * there isn't a full name already, second is where there is.
20858 */
20859 if (!has_fullname && !vim_isAbsName(*fnamep))
20860 {
20861 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20862 return -1;
20863 }
20864 else
20865 {
20866 int l;
20867
20868 /* Simple case, already have the full-name
20869 * Nearly always shorter, so try first time. */
20870 l = *fnamelen;
20871 if (!get_short_pathname(fnamep, bufp, &l))
20872 return -1;
20873
20874 if (l == 0)
20875 {
20876 /* Couldn't find the filename.. search the paths.
20877 */
20878 l = *fnamelen;
20879 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20880 return -1;
20881 }
20882 *fnamelen = l;
20883 }
20884 }
20885#endif /* WIN3264 */
20886
20887 /* ":t" - tail, just the basename */
20888 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20889 {
20890 *usedlen += 2;
20891 *fnamelen -= (int)(tail - *fnamep);
20892 *fnamep = tail;
20893 }
20894
20895 /* ":e" - extension, can be repeated */
20896 /* ":r" - root, without extension, can be repeated */
20897 while (src[*usedlen] == ':'
20898 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20899 {
20900 /* find a '.' in the tail:
20901 * - for second :e: before the current fname
20902 * - otherwise: The last '.'
20903 */
20904 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20905 s = *fnamep - 2;
20906 else
20907 s = *fnamep + *fnamelen - 1;
20908 for ( ; s > tail; --s)
20909 if (s[0] == '.')
20910 break;
20911 if (src[*usedlen + 1] == 'e') /* :e */
20912 {
20913 if (s > tail)
20914 {
20915 *fnamelen += (int)(*fnamep - (s + 1));
20916 *fnamep = s + 1;
20917#ifdef VMS
20918 /* cut version from the extension */
20919 s = *fnamep + *fnamelen - 1;
20920 for ( ; s > *fnamep; --s)
20921 if (s[0] == ';')
20922 break;
20923 if (s > *fnamep)
20924 *fnamelen = s - *fnamep;
20925#endif
20926 }
20927 else if (*fnamep <= tail)
20928 *fnamelen = 0;
20929 }
20930 else /* :r */
20931 {
20932 if (s > tail) /* remove one extension */
20933 *fnamelen = (int)(s - *fnamep);
20934 }
20935 *usedlen += 2;
20936 }
20937
20938 /* ":s?pat?foo?" - substitute */
20939 /* ":gs?pat?foo?" - global substitute */
20940 if (src[*usedlen] == ':'
20941 && (src[*usedlen + 1] == 's'
20942 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20943 {
20944 char_u *str;
20945 char_u *pat;
20946 char_u *sub;
20947 int sep;
20948 char_u *flags;
20949 int didit = FALSE;
20950
20951 flags = (char_u *)"";
20952 s = src + *usedlen + 2;
20953 if (src[*usedlen + 1] == 'g')
20954 {
20955 flags = (char_u *)"g";
20956 ++s;
20957 }
20958
20959 sep = *s++;
20960 if (sep)
20961 {
20962 /* find end of pattern */
20963 p = vim_strchr(s, sep);
20964 if (p != NULL)
20965 {
20966 pat = vim_strnsave(s, (int)(p - s));
20967 if (pat != NULL)
20968 {
20969 s = p + 1;
20970 /* find end of substitution */
20971 p = vim_strchr(s, sep);
20972 if (p != NULL)
20973 {
20974 sub = vim_strnsave(s, (int)(p - s));
20975 str = vim_strnsave(*fnamep, *fnamelen);
20976 if (sub != NULL && str != NULL)
20977 {
20978 *usedlen = (int)(p + 1 - src);
20979 s = do_string_sub(str, pat, sub, flags);
20980 if (s != NULL)
20981 {
20982 *fnamep = s;
20983 *fnamelen = (int)STRLEN(s);
20984 vim_free(*bufp);
20985 *bufp = s;
20986 didit = TRUE;
20987 }
20988 }
20989 vim_free(sub);
20990 vim_free(str);
20991 }
20992 vim_free(pat);
20993 }
20994 }
20995 /* after using ":s", repeat all the modifiers */
20996 if (didit)
20997 goto repeat;
20998 }
20999 }
21000
21001 return valid;
21002}
21003
21004/*
21005 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21006 * "flags" can be "g" to do a global substitute.
21007 * Returns an allocated string, NULL for error.
21008 */
21009 char_u *
21010do_string_sub(str, pat, sub, flags)
21011 char_u *str;
21012 char_u *pat;
21013 char_u *sub;
21014 char_u *flags;
21015{
21016 int sublen;
21017 regmatch_T regmatch;
21018 int i;
21019 int do_all;
21020 char_u *tail;
21021 garray_T ga;
21022 char_u *ret;
21023 char_u *save_cpo;
21024
21025 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21026 save_cpo = p_cpo;
21027 p_cpo = (char_u *)"";
21028
21029 ga_init2(&ga, 1, 200);
21030
21031 do_all = (flags[0] == 'g');
21032
21033 regmatch.rm_ic = p_ic;
21034 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21035 if (regmatch.regprog != NULL)
21036 {
21037 tail = str;
21038 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21039 {
21040 /*
21041 * Get some space for a temporary buffer to do the substitution
21042 * into. It will contain:
21043 * - The text up to where the match is.
21044 * - The substituted text.
21045 * - The text after the match.
21046 */
21047 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21048 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21049 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21050 {
21051 ga_clear(&ga);
21052 break;
21053 }
21054
21055 /* copy the text up to where the match is */
21056 i = (int)(regmatch.startp[0] - tail);
21057 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21058 /* add the substituted text */
21059 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21060 + ga.ga_len + i, TRUE, TRUE, FALSE);
21061 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 /* avoid getting stuck on a match with an empty string */
21063 if (tail == regmatch.endp[0])
21064 {
21065 if (*tail == NUL)
21066 break;
21067 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21068 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 }
21070 else
21071 {
21072 tail = regmatch.endp[0];
21073 if (*tail == NUL)
21074 break;
21075 }
21076 if (!do_all)
21077 break;
21078 }
21079
21080 if (ga.ga_data != NULL)
21081 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21082
21083 vim_free(regmatch.regprog);
21084 }
21085
21086 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21087 ga_clear(&ga);
21088 p_cpo = save_cpo;
21089
21090 return ret;
21091}
21092
21093#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */