blob: 45872be3557b4161d774a327c9a4f74153c11524 [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 Moolenaar79783442006-05-05 21:18:03 +00008794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008796 if (*skipwhite(p) != NUL)
8797 n = FALSE; /* trailing garbage */
8798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 else if (*p == '*') /* internal or user defined function */
8800 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008801 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803 else if (*p == ':')
8804 {
8805 n = cmd_exists(p + 1);
8806 }
8807 else if (*p == '#')
8808 {
8809#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008810 if (p[1] == '#')
8811 n = autocmd_supported(p + 2);
8812 else
8813 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814#endif
8815 }
8816 else /* internal variable */
8817 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008818 char_u *tofree;
8819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008821 /* get_name_len() takes care of expanding curly braces */
8822 name = p;
8823 len = get_name_len(&p, &tofree, TRUE, FALSE);
8824 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008826 if (tofree != NULL)
8827 name = tofree;
8828 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8829 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008831 /* handle d.key, l[idx], f(expr) */
8832 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8833 if (n)
8834 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008837 if (*p != NUL)
8838 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008840 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
8846/*
8847 * "expand()" function
8848 */
8849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 typval_T *argvars;
8852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854 char_u *s;
8855 int len;
8856 char_u *errormsg;
8857 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8858 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 rettv->v_type = VAR_STRING;
8862 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 if (*s == '%' || *s == '#' || *s == '<')
8864 {
8865 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 --emsg_off;
8868 }
8869 else
8870 {
8871 /* When the optional second argument is non-zero, don't remove matches
8872 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008873 if (argvars[1].v_type != VAR_UNKNOWN
8874 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 if (!error)
8877 {
8878 ExpandInit(&xpc);
8879 xpc.xp_context = EXPAND_FILES;
8880 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 }
8882 else
8883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885}
8886
8887/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008888 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008889 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008890 */
8891 static void
8892f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 typval_T *argvars;
8894 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008895{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008896 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008897 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008899 list_T *l1, *l2;
8900 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008901 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008903
Bram Moolenaare9a41262005-01-15 22:18:47 +00008904 l1 = argvars[0].vval.v_list;
8905 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008906 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8907 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008908 {
8909 if (argvars[2].v_type != VAR_UNKNOWN)
8910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 before = get_tv_number_chk(&argvars[2], &error);
8912 if (error)
8913 return; /* type error; errmsg already given */
8914
Bram Moolenaar758711c2005-02-02 23:11:38 +00008915 if (before == l1->lv_len)
8916 item = NULL;
8917 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008919 item = list_find(l1, before);
8920 if (item == NULL)
8921 {
8922 EMSGN(_(e_listidx), before);
8923 return;
8924 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008925 }
8926 }
8927 else
8928 item = NULL;
8929 list_extend(l1, l2, item);
8930
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 copy_tv(&argvars[0], rettv);
8932 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008934 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8935 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 dict_T *d1, *d2;
8937 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 char_u *action;
8939 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008940 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008941 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942
8943 d1 = argvars[0].vval.v_dict;
8944 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008945 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8946 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 {
8948 /* Check the third argument. */
8949 if (argvars[2].v_type != VAR_UNKNOWN)
8950 {
8951 static char *(av[]) = {"keep", "force", "error"};
8952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 action = get_tv_string_chk(&argvars[2]);
8954 if (action == NULL)
8955 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008956 for (i = 0; i < 3; ++i)
8957 if (STRCMP(action, av[i]) == 0)
8958 break;
8959 if (i == 3)
8960 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008961 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 return;
8963 }
8964 }
8965 else
8966 action = (char_u *)"force";
8967
8968 /* Go over all entries in the second dict and add them to the
8969 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008970 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008973 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008975 --todo;
8976 di1 = dict_find(d1, hi2->hi_key, -1);
8977 if (di1 == NULL)
8978 {
8979 di1 = dictitem_copy(HI2DI(hi2));
8980 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8981 dictitem_free(di1);
8982 }
8983 else if (*action == 'e')
8984 {
8985 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8986 break;
8987 }
8988 else if (*action == 'f')
8989 {
8990 clear_tv(&di1->di_tv);
8991 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8992 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008993 }
8994 }
8995
Bram Moolenaare9a41262005-01-15 22:18:47 +00008996 copy_tv(&argvars[0], rettv);
8997 }
8998 }
8999 else
9000 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009001}
9002
9003/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009004 * "feedkeys()" function
9005 */
9006/*ARGSUSED*/
9007 static void
9008f_feedkeys(argvars, rettv)
9009 typval_T *argvars;
9010 typval_T *rettv;
9011{
9012 int remap = TRUE;
9013 char_u *keys, *flags;
9014 char_u nbuf[NUMBUFLEN];
9015 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009016 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009017
9018 rettv->vval.v_number = 0;
9019 keys = get_tv_string(&argvars[0]);
9020 if (*keys != NUL)
9021 {
9022 if (argvars[1].v_type != VAR_UNKNOWN)
9023 {
9024 flags = get_tv_string_buf(&argvars[1], nbuf);
9025 for ( ; *flags != NUL; ++flags)
9026 {
9027 switch (*flags)
9028 {
9029 case 'n': remap = FALSE; break;
9030 case 'm': remap = TRUE; break;
9031 case 't': typed = TRUE; break;
9032 }
9033 }
9034 }
9035
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009036 /* Need to escape K_SPECIAL and CSI before putting the string in the
9037 * typeahead buffer. */
9038 keys_esc = vim_strsave_escape_csi(keys);
9039 if (keys_esc != NULL)
9040 {
9041 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009042 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009043 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009044 if (vgetc_busy)
9045 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009046 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009047 }
9048}
9049
9050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 * "filereadable()" function
9052 */
9053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009055 typval_T *argvars;
9056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 FILE *fd;
9059 char_u *p;
9060 int n;
9061
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9064 {
9065 n = TRUE;
9066 fclose(fd);
9067 }
9068 else
9069 n = FALSE;
9070
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009075 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 * rights to write into.
9077 */
9078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 typval_T *argvars;
9081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009083 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084}
9085
Bram Moolenaar33570922005-01-25 22:26:29 +00009086static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009087
9088 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009089findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 typval_T *argvars;
9091 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009092 int dir;
9093{
9094#ifdef FEAT_SEARCHPATH
9095 char_u *fname;
9096 char_u *fresult = NULL;
9097 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9098 char_u *p;
9099 char_u pathbuf[NUMBUFLEN];
9100 int count = 1;
9101 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009102 int error = FALSE;
9103#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009104
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009105 rettv->vval.v_string = NULL;
9106 rettv->v_type = VAR_STRING;
9107
9108#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009110
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009111 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9114 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009115 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009116 else
9117 {
9118 if (*p != NUL)
9119 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009122 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009124 }
9125
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009126 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9127 error = TRUE;
9128
9129 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 do
9132 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009133 if (rettv->v_type == VAR_STRING)
9134 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 fresult = find_file_in_path_option(first ? fname : NULL,
9136 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009137 0, first, path, dir, NULL,
9138 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009140
9141 if (fresult != NULL && rettv->v_type == VAR_LIST)
9142 list_append_string(rettv->vval.v_list, fresult, -1);
9143
9144 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009146
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009147 if (rettv->v_type == VAR_STRING)
9148 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009149#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009150}
9151
Bram Moolenaar33570922005-01-25 22:26:29 +00009152static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9153static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009154
9155/*
9156 * Implementation of map() and filter().
9157 */
9158 static void
9159filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009160 typval_T *argvars;
9161 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009162 int map;
9163{
9164 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009165 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 listitem_T *li, *nli;
9167 list_T *l = NULL;
9168 dictitem_T *di;
9169 hashtab_T *ht;
9170 hashitem_T *hi;
9171 dict_T *d = NULL;
9172 typval_T save_val;
9173 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009174 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009175 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009176 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009177 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009178
9179 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009180 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009181 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009182 if ((l = argvars[0].vval.v_list) == NULL
9183 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009184 return;
9185 }
9186 else if (argvars[0].v_type == VAR_DICT)
9187 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009188 if ((d = argvars[0].vval.v_dict) == NULL
9189 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009190 return;
9191 }
9192 else
9193 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009194 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009195 return;
9196 }
9197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 expr = get_tv_string_buf_chk(&argvars[1], buf);
9199 /* On type errors, the preceding call has already displayed an error
9200 * message. Avoid a misleading error message for an empty string that
9201 * was not passed as argument. */
9202 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 prepare_vimvar(VV_VAL, &save_val);
9205 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009206
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009207 /* We reset "did_emsg" to be able to detect whether an error
9208 * occurred during evaluation of the expression. */
9209 save_did_emsg = did_emsg;
9210 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009212 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 prepare_vimvar(VV_KEY, &save_key);
9215 vimvars[VV_KEY].vv_type = VAR_STRING;
9216
9217 ht = &d->dv_hashtab;
9218 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009219 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009222 if (!HASHITEM_EMPTY(hi))
9223 {
9224 --todo;
9225 di = HI2DI(hi);
9226 if (tv_check_lock(di->di_tv.v_lock, msg))
9227 break;
9228 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009229 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009230 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 break;
9232 if (!map && rem)
9233 dictitem_remove(d, di);
9234 clear_tv(&vimvars[VV_KEY].vv_tv);
9235 }
9236 }
9237 hash_unlock(ht);
9238
9239 restore_vimvar(VV_KEY, &save_key);
9240 }
9241 else
9242 {
9243 for (li = l->lv_first; li != NULL; li = nli)
9244 {
9245 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009246 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009248 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009249 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009250 break;
9251 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009253 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009254 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009257
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009258 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009260
9261 copy_tv(&argvars[0], rettv);
9262}
9263
9264 static int
9265filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009266 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009267 char_u *expr;
9268 int map;
9269 int *remp;
9270{
Bram Moolenaar33570922005-01-25 22:26:29 +00009271 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 char_u *s;
9273
Bram Moolenaar33570922005-01-25 22:26:29 +00009274 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009275 s = expr;
9276 if (eval1(&s, &rettv, TRUE) == FAIL)
9277 return FAIL;
9278 if (*s != NUL) /* check for trailing chars after expr */
9279 {
9280 EMSG2(_(e_invexpr2), s);
9281 return FAIL;
9282 }
9283 if (map)
9284 {
9285 /* map(): replace the list item value */
9286 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009287 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 *tv = rettv;
9289 }
9290 else
9291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 int error = FALSE;
9293
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009296 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 /* On type error, nothing has been removed; return FAIL to stop the
9298 * loop. The error message was given by get_tv_number_chk(). */
9299 if (error)
9300 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009302 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009304}
9305
9306/*
9307 * "filter()" function
9308 */
9309 static void
9310f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 typval_T *argvars;
9312 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009313{
9314 filter_map(argvars, rettv, FALSE);
9315}
9316
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009317/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318 * "finddir({fname}[, {path}[, {count}]])" function
9319 */
9320 static void
9321f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009322 typval_T *argvars;
9323 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009324{
9325 findfilendir(argvars, rettv, TRUE);
9326}
9327
9328/*
9329 * "findfile({fname}[, {path}[, {count}]])" function
9330 */
9331 static void
9332f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009333 typval_T *argvars;
9334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335{
9336 findfilendir(argvars, rettv, FALSE);
9337}
9338
9339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 * "fnamemodify({fname}, {mods})" function
9341 */
9342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T *argvars;
9345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 char_u *fname;
9348 char_u *mods;
9349 int usedlen = 0;
9350 int len;
9351 char_u *fbuf = NULL;
9352 char_u buf[NUMBUFLEN];
9353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 fname = get_tv_string_chk(&argvars[0]);
9355 mods = get_tv_string_buf_chk(&argvars[1], buf);
9356 if (fname == NULL || mods == NULL)
9357 fname = NULL;
9358 else
9359 {
9360 len = (int)STRLEN(fname);
9361 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 vim_free(fbuf);
9370}
9371
Bram Moolenaar33570922005-01-25 22:26:29 +00009372static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
9374/*
9375 * "foldclosed()" function
9376 */
9377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009379 typval_T *argvars;
9380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 int end;
9382{
9383#ifdef FEAT_FOLDING
9384 linenr_T lnum;
9385 linenr_T first, last;
9386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9389 {
9390 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9391 {
9392 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 return;
9397 }
9398 }
9399#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
9403/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404 * "foldclosed()" function
9405 */
9406 static void
9407f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *argvars;
9409 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009410{
9411 foldclosed_both(argvars, rettv, FALSE);
9412}
9413
9414/*
9415 * "foldclosedend()" function
9416 */
9417 static void
9418f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009421{
9422 foldclosed_both(argvars, rettv, TRUE);
9423}
9424
9425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 * "foldlevel()" function
9427 */
9428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 typval_T *argvars;
9431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433#ifdef FEAT_FOLDING
9434 linenr_T lnum;
9435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 else
9440#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
9445 * "foldtext()" function
9446 */
9447/*ARGSUSED*/
9448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453#ifdef FEAT_FOLDING
9454 linenr_T lnum;
9455 char_u *s;
9456 char_u *r;
9457 int len;
9458 char *txt;
9459#endif
9460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->v_type = VAR_STRING;
9462 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009464 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9465 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9466 <= curbuf->b_ml.ml_line_count
9467 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 {
9469 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009470 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9471 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 {
9473 if (!linewhite(lnum))
9474 break;
9475 ++lnum;
9476 }
9477
9478 /* Find interesting text in this line. */
9479 s = skipwhite(ml_get(lnum));
9480 /* skip C comment-start */
9481 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009484 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009485 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009486 {
9487 s = skipwhite(ml_get(lnum + 1));
9488 if (*s == '*')
9489 s = skipwhite(s + 1);
9490 }
9491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 txt = _("+-%s%3ld lines: ");
9493 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009494 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 + 20 /* for %3ld */
9496 + STRLEN(s))); /* concatenated */
9497 if (r != NULL)
9498 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009499 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9500 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9501 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 len = (int)STRLEN(r);
9503 STRCAT(r, s);
9504 /* remove 'foldmarker' and 'commentstring' */
9505 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 }
9508 }
9509#endif
9510}
9511
9512/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009513 * "foldtextresult(lnum)" function
9514 */
9515/*ARGSUSED*/
9516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009518 typval_T *argvars;
9519 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009520{
9521#ifdef FEAT_FOLDING
9522 linenr_T lnum;
9523 char_u *text;
9524 char_u buf[51];
9525 foldinfo_T foldinfo;
9526 int fold_count;
9527#endif
9528
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 rettv->v_type = VAR_STRING;
9530 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009531#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 /* treat illegal types and illegal string values for {lnum} the same */
9534 if (lnum < 0)
9535 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009536 fold_count = foldedCount(curwin, lnum, &foldinfo);
9537 if (fold_count > 0)
9538 {
9539 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9540 &foldinfo, buf);
9541 if (text == buf)
9542 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009544 }
9545#endif
9546}
9547
9548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 * "foreground()" function
9550 */
9551/*ARGSUSED*/
9552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#ifdef FEAT_GUI
9559 if (gui.in_use)
9560 gui_mch_set_foreground();
9561#else
9562# ifdef WIN32
9563 win32_set_foreground();
9564# endif
9565#endif
9566}
9567
9568/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 * "function()" function
9570 */
9571/*ARGSUSED*/
9572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009574 typval_T *argvars;
9575 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009576{
9577 char_u *s;
9578
Bram Moolenaara7043832005-01-21 11:56:39 +00009579 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009581 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 EMSG2(_(e_invarg2), s);
9583 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009584 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585 else
9586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 rettv->vval.v_string = vim_strsave(s);
9588 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589 }
9590}
9591
9592/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009593 * "garbagecollect()" function
9594 */
9595/*ARGSUSED*/
9596 static void
9597f_garbagecollect(argvars, rettv)
9598 typval_T *argvars;
9599 typval_T *rettv;
9600{
9601 garbage_collect();
9602}
9603
9604/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009605 * "get()" function
9606 */
9607 static void
9608f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 typval_T *argvars;
9610 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009611{
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 listitem_T *li;
9613 list_T *l;
9614 dictitem_T *di;
9615 dict_T *d;
9616 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009617
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009619 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 int error = FALSE;
9623
9624 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9625 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009627 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 else if (argvars[0].v_type == VAR_DICT)
9630 {
9631 if ((d = argvars[0].vval.v_dict) != NULL)
9632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009633 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009634 if (di != NULL)
9635 tv = &di->di_tv;
9636 }
9637 }
9638 else
9639 EMSG2(_(e_listdictarg), "get()");
9640
9641 if (tv == NULL)
9642 {
9643 if (argvars[2].v_type == VAR_UNKNOWN)
9644 rettv->vval.v_number = 0;
9645 else
9646 copy_tv(&argvars[2], rettv);
9647 }
9648 else
9649 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009650}
9651
Bram Moolenaar342337a2005-07-21 21:11:17 +00009652static 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 +00009653
9654/*
9655 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009656 * Return a range (from start to end) of lines in rettv from the specified
9657 * buffer.
9658 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009659 */
9660 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009661get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009662 buf_T *buf;
9663 linenr_T start;
9664 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009665 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009666 typval_T *rettv;
9667{
9668 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009669
Bram Moolenaar342337a2005-07-21 21:11:17 +00009670 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009671 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009672 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009673 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009674 }
9675 else
9676 rettv->vval.v_number = 0;
9677
9678 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9679 return;
9680
9681 if (!retlist)
9682 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009683 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9684 p = ml_get_buf(buf, start, FALSE);
9685 else
9686 p = (char_u *)"";
9687
9688 rettv->v_type = VAR_STRING;
9689 rettv->vval.v_string = vim_strsave(p);
9690 }
9691 else
9692 {
9693 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009694 return;
9695
9696 if (start < 1)
9697 start = 1;
9698 if (end > buf->b_ml.ml_line_count)
9699 end = buf->b_ml.ml_line_count;
9700 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009701 if (list_append_string(rettv->vval.v_list,
9702 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009703 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009704 }
9705}
9706
9707/*
9708 * "getbufline()" function
9709 */
9710 static void
9711f_getbufline(argvars, rettv)
9712 typval_T *argvars;
9713 typval_T *rettv;
9714{
9715 linenr_T lnum;
9716 linenr_T end;
9717 buf_T *buf;
9718
9719 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9720 ++emsg_off;
9721 buf = get_buf_tv(&argvars[0]);
9722 --emsg_off;
9723
Bram Moolenaar661b1822005-07-28 22:36:45 +00009724 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009725 if (argvars[2].v_type == VAR_UNKNOWN)
9726 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009727 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009728 end = get_tv_lnum_buf(&argvars[2], buf);
9729
Bram Moolenaar342337a2005-07-21 21:11:17 +00009730 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009731}
9732
Bram Moolenaar0d660222005-01-07 21:51:51 +00009733/*
9734 * "getbufvar()" function
9735 */
9736 static void
9737f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009740{
9741 buf_T *buf;
9742 buf_T *save_curbuf;
9743 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009744 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9747 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009748 ++emsg_off;
9749 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009750
9751 rettv->v_type = VAR_STRING;
9752 rettv->vval.v_string = NULL;
9753
9754 if (buf != NULL && varname != NULL)
9755 {
9756 if (*varname == '&') /* buffer-local-option */
9757 {
9758 /* set curbuf to be our buf, temporarily */
9759 save_curbuf = curbuf;
9760 curbuf = buf;
9761
9762 get_option_tv(&varname, rettv, TRUE);
9763
9764 /* restore previous notion of curbuf */
9765 curbuf = save_curbuf;
9766 }
9767 else
9768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 if (*varname == NUL)
9770 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9771 * scope prefix before the NUL byte is required by
9772 * find_var_in_ht(). */
9773 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009774 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009775 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009776 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009778 }
9779 }
9780
9781 --emsg_off;
9782}
9783
9784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 * "getchar()" function
9786 */
9787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *argvars;
9790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794
9795 ++no_mapping;
9796 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009797 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 /* getchar(): blocking wait. */
9799 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009800 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 /* getchar(1): only check if char avail */
9802 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 else if (error || vpeekc() == NUL)
9804 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 n = 0;
9806 else
9807 /* getchar(0) and char avail: return char */
9808 n = safe_vgetc();
9809 --no_mapping;
9810 --allow_keys;
9811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 if (IS_SPECIAL(n) || mod_mask != 0)
9814 {
9815 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9816 int i = 0;
9817
9818 /* Turn a special key into three bytes, plus modifier. */
9819 if (mod_mask != 0)
9820 {
9821 temp[i++] = K_SPECIAL;
9822 temp[i++] = KS_MODIFIER;
9823 temp[i++] = mod_mask;
9824 }
9825 if (IS_SPECIAL(n))
9826 {
9827 temp[i++] = K_SPECIAL;
9828 temp[i++] = K_SECOND(n);
9829 temp[i++] = K_THIRD(n);
9830 }
9831#ifdef FEAT_MBYTE
9832 else if (has_mbyte)
9833 i += (*mb_char2bytes)(n, temp + i);
9834#endif
9835 else
9836 temp[i++] = n;
9837 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->v_type = VAR_STRING;
9839 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 }
9841}
9842
9843/*
9844 * "getcharmod()" function
9845 */
9846/*ARGSUSED*/
9847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *argvars;
9850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853}
9854
9855/*
9856 * "getcmdline()" function
9857 */
9858/*ARGSUSED*/
9859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 typval_T *argvars;
9862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->v_type = VAR_STRING;
9865 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866}
9867
9868/*
9869 * "getcmdpos()" function
9870 */
9871/*ARGSUSED*/
9872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009874 typval_T *argvars;
9875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878}
9879
9880/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009881 * "getcmdtype()" function
9882 */
9883/*ARGSUSED*/
9884 static void
9885f_getcmdtype(argvars, rettv)
9886 typval_T *argvars;
9887 typval_T *rettv;
9888{
9889 rettv->v_type = VAR_STRING;
9890 rettv->vval.v_string = alloc(2);
9891 if (rettv->vval.v_string != NULL)
9892 {
9893 rettv->vval.v_string[0] = get_cmdline_type();
9894 rettv->vval.v_string[1] = NUL;
9895 }
9896}
9897
9898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 * "getcwd()" function
9900 */
9901/*ARGSUSED*/
9902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 typval_T *argvars;
9905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907 char_u cwd[MAXPATHL];
9908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 else
9913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009916 if (rettv->vval.v_string != NULL)
9917 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918#endif
9919 }
9920}
9921
9922/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009923 * "getfontname()" function
9924 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009925/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *argvars;
9929 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009930{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->v_type = VAR_STRING;
9932 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009933#ifdef FEAT_GUI
9934 if (gui.in_use)
9935 {
9936 GuiFont font;
9937 char_u *name = NULL;
9938
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009939 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009940 {
9941 /* Get the "Normal" font. Either the name saved by
9942 * hl_set_font_name() or from the font ID. */
9943 font = gui.norm_font;
9944 name = hl_get_font_name();
9945 }
9946 else
9947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009949 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9950 return;
9951 font = gui_mch_get_font(name, FALSE);
9952 if (font == NOFONT)
9953 return; /* Invalid font name, return empty string. */
9954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009956 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009957 gui_mch_free_font(font);
9958 }
9959#endif
9960}
9961
9962/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009963 * "getfperm({fname})" function
9964 */
9965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *argvars;
9968 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009969{
9970 char_u *fname;
9971 struct stat st;
9972 char_u *perm = NULL;
9973 char_u flags[] = "rwx";
9974 int i;
9975
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009977
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009979 if (mch_stat((char *)fname, &st) >= 0)
9980 {
9981 perm = vim_strsave((char_u *)"---------");
9982 if (perm != NULL)
9983 {
9984 for (i = 0; i < 9; i++)
9985 {
9986 if (st.st_mode & (1 << (8 - i)))
9987 perm[i] = flags[i % 3];
9988 }
9989 }
9990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009992}
9993
9994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 * "getfsize({fname})" function
9996 */
9997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009999 typval_T *argvars;
10000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001{
10002 char_u *fname;
10003 struct stat st;
10004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010005 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
10009 if (mch_stat((char *)fname, &st) >= 0)
10010 {
10011 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 }
10016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018}
10019
10020/*
10021 * "getftime({fname})" function
10022 */
10023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010025 typval_T *argvars;
10026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027{
10028 char_u *fname;
10029 struct stat st;
10030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032
10033 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037}
10038
10039/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010040 * "getftype({fname})" function
10041 */
10042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010044 typval_T *argvars;
10045 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010046{
10047 char_u *fname;
10048 struct stat st;
10049 char_u *type = NULL;
10050 char *t;
10051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010055 if (mch_lstat((char *)fname, &st) >= 0)
10056 {
10057#ifdef S_ISREG
10058 if (S_ISREG(st.st_mode))
10059 t = "file";
10060 else if (S_ISDIR(st.st_mode))
10061 t = "dir";
10062# ifdef S_ISLNK
10063 else if (S_ISLNK(st.st_mode))
10064 t = "link";
10065# endif
10066# ifdef S_ISBLK
10067 else if (S_ISBLK(st.st_mode))
10068 t = "bdev";
10069# endif
10070# ifdef S_ISCHR
10071 else if (S_ISCHR(st.st_mode))
10072 t = "cdev";
10073# endif
10074# ifdef S_ISFIFO
10075 else if (S_ISFIFO(st.st_mode))
10076 t = "fifo";
10077# endif
10078# ifdef S_ISSOCK
10079 else if (S_ISSOCK(st.st_mode))
10080 t = "fifo";
10081# endif
10082 else
10083 t = "other";
10084#else
10085# ifdef S_IFMT
10086 switch (st.st_mode & S_IFMT)
10087 {
10088 case S_IFREG: t = "file"; break;
10089 case S_IFDIR: t = "dir"; break;
10090# ifdef S_IFLNK
10091 case S_IFLNK: t = "link"; break;
10092# endif
10093# ifdef S_IFBLK
10094 case S_IFBLK: t = "bdev"; break;
10095# endif
10096# ifdef S_IFCHR
10097 case S_IFCHR: t = "cdev"; break;
10098# endif
10099# ifdef S_IFIFO
10100 case S_IFIFO: t = "fifo"; break;
10101# endif
10102# ifdef S_IFSOCK
10103 case S_IFSOCK: t = "socket"; break;
10104# endif
10105 default: t = "other";
10106 }
10107# else
10108 if (mch_isdir(fname))
10109 t = "dir";
10110 else
10111 t = "file";
10112# endif
10113#endif
10114 type = vim_strsave((char_u *)t);
10115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010117}
10118
10119/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010120 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010121 */
10122 static void
10123f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 typval_T *argvars;
10125 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010126{
10127 linenr_T lnum;
10128 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010129 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010130
10131 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010132 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010133 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010134 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010135 retlist = FALSE;
10136 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010137 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010138 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010139 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010140 retlist = TRUE;
10141 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010142
Bram Moolenaar342337a2005-07-21 21:11:17 +000010143 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010144}
10145
10146/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010147 * "getpos(string)" function
10148 */
10149 static void
10150f_getpos(argvars, rettv)
10151 typval_T *argvars;
10152 typval_T *rettv;
10153{
10154 pos_T *fp;
10155 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010156 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010157
10158 if (rettv_list_alloc(rettv) == OK)
10159 {
10160 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010161 fp = var2fpos(&argvars[0], TRUE, &fnum);
10162 if (fnum != -1)
10163 list_append_number(l, (varnumber_T)fnum);
10164 else
10165 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010166 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10167 : (varnumber_T)0);
10168 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10169 : (varnumber_T)0);
10170 list_append_number(l,
10171#ifdef FEAT_VIRTUALEDIT
10172 (fp != NULL) ? (varnumber_T)fp->coladd :
10173#endif
10174 (varnumber_T)0);
10175 }
10176 else
10177 rettv->vval.v_number = FALSE;
10178}
10179
10180/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010181 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010182 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010183/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010184 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010185f_getqflist(argvars, rettv)
10186 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010187 typval_T *rettv;
10188{
10189#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010190 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010191#endif
10192
10193 rettv->vval.v_number = FALSE;
10194#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010195 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010196 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010197 wp = NULL;
10198 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10199 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010200 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010201 if (wp == NULL)
10202 return;
10203 }
10204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010205 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010206 }
10207#endif
10208}
10209
10210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 * "getreg()" function
10212 */
10213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010215 typval_T *argvars;
10216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
10218 char_u *strregname;
10219 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010220 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010223 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 strregname = get_tv_string_chk(&argvars[0]);
10226 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010227 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010231 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 regname = (strregname == NULL ? '"' : *strregname);
10233 if (regname == 0)
10234 regname = '"';
10235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010236 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 rettv->vval.v_string = error ? NULL :
10238 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239}
10240
10241/*
10242 * "getregtype()" function
10243 */
10244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010246 typval_T *argvars;
10247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248{
10249 char_u *strregname;
10250 int regname;
10251 char_u buf[NUMBUFLEN + 2];
10252 long reglen = 0;
10253
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010254 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 {
10256 strregname = get_tv_string_chk(&argvars[0]);
10257 if (strregname == NULL) /* type error; errmsg already given */
10258 {
10259 rettv->v_type = VAR_STRING;
10260 rettv->vval.v_string = NULL;
10261 return;
10262 }
10263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 else
10265 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267
10268 regname = (strregname == NULL ? '"' : *strregname);
10269 if (regname == 0)
10270 regname = '"';
10271
10272 buf[0] = NUL;
10273 buf[1] = NUL;
10274 switch (get_reg_type(regname, &reglen))
10275 {
10276 case MLINE: buf[0] = 'V'; break;
10277 case MCHAR: buf[0] = 'v'; break;
10278#ifdef FEAT_VISUAL
10279 case MBLOCK:
10280 buf[0] = Ctrl_V;
10281 sprintf((char *)buf + 1, "%ld", reglen + 1);
10282 break;
10283#endif
10284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->v_type = VAR_STRING;
10286 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287}
10288
10289/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010290 * "gettabwinvar()" function
10291 */
10292 static void
10293f_gettabwinvar(argvars, rettv)
10294 typval_T *argvars;
10295 typval_T *rettv;
10296{
10297 getwinvar(argvars, rettv, 1);
10298}
10299
10300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 * "getwinposx()" function
10302 */
10303/*ARGSUSED*/
10304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010306 typval_T *argvars;
10307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310#ifdef FEAT_GUI
10311 if (gui.in_use)
10312 {
10313 int x, y;
10314
10315 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 }
10318#endif
10319}
10320
10321/*
10322 * "getwinposy()" function
10323 */
10324/*ARGSUSED*/
10325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010330 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331#ifdef FEAT_GUI
10332 if (gui.in_use)
10333 {
10334 int x, y;
10335
10336 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 }
10339#endif
10340}
10341
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010342/*
10343 * Find window specifed by "vp" in tabpage "tp".
10344 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010345 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010346find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010347 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010348 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010349{
10350#ifdef FEAT_WINDOWS
10351 win_T *wp;
10352#endif
10353 int nr;
10354
10355 nr = get_tv_number_chk(vp, NULL);
10356
10357#ifdef FEAT_WINDOWS
10358 if (nr < 0)
10359 return NULL;
10360 if (nr == 0)
10361 return curwin;
10362
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010363 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10364 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010365 if (--nr <= 0)
10366 break;
10367 return wp;
10368#else
10369 if (nr == 0 || nr == 1)
10370 return curwin;
10371 return NULL;
10372#endif
10373}
10374
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375/*
10376 * "getwinvar()" function
10377 */
10378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010380 typval_T *argvars;
10381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010383 getwinvar(argvars, rettv, 0);
10384}
10385
10386/*
10387 * getwinvar() and gettabwinvar()
10388 */
10389 static void
10390getwinvar(argvars, rettv, off)
10391 typval_T *argvars;
10392 typval_T *rettv;
10393 int off; /* 1 for gettabwinvar() */
10394{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 win_T *win, *oldcurwin;
10396 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010397 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010398 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010400#ifdef FEAT_WINDOWS
10401 if (off == 1)
10402 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10403 else
10404 tp = curtab;
10405#endif
10406 win = find_win_by_nr(&argvars[off], tp);
10407 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 rettv->v_type = VAR_STRING;
10411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
10413 if (win != NULL && varname != NULL)
10414 {
10415 if (*varname == '&') /* window-local-option */
10416 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010417 /* Set curwin to be our win, temporarily. Also set curbuf, so
10418 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 oldcurwin = curwin;
10420 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010421 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424
10425 /* restore previous notion of curwin */
10426 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010427 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429 else
10430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 if (*varname == NUL)
10432 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10433 * scope prefix before the NUL byte is required by
10434 * find_var_in_ht(). */
10435 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010437 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010439 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 }
10441 }
10442
10443 --emsg_off;
10444}
10445
10446/*
10447 * "glob()" function
10448 */
10449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010451 typval_T *argvars;
10452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
10454 expand_T xpc;
10455
10456 ExpandInit(&xpc);
10457 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 rettv->v_type = VAR_STRING;
10459 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461}
10462
10463/*
10464 * "globpath()" function
10465 */
10466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 typval_T *argvars;
10469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470{
10471 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010474 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 if (file == NULL)
10476 rettv->vval.v_string = NULL;
10477 else
10478 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
10480
10481/*
10482 * "has()" function
10483 */
10484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010486 typval_T *argvars;
10487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 int i;
10490 char_u *name;
10491 int n = FALSE;
10492 static char *(has_list[]) =
10493 {
10494#ifdef AMIGA
10495 "amiga",
10496# ifdef FEAT_ARP
10497 "arp",
10498# endif
10499#endif
10500#ifdef __BEOS__
10501 "beos",
10502#endif
10503#ifdef MSDOS
10504# ifdef DJGPP
10505 "dos32",
10506# else
10507 "dos16",
10508# endif
10509#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010510#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 "mac",
10512#endif
10513#if defined(MACOS_X_UNIX)
10514 "macunix",
10515#endif
10516#ifdef OS2
10517 "os2",
10518#endif
10519#ifdef __QNX__
10520 "qnx",
10521#endif
10522#ifdef RISCOS
10523 "riscos",
10524#endif
10525#ifdef UNIX
10526 "unix",
10527#endif
10528#ifdef VMS
10529 "vms",
10530#endif
10531#ifdef WIN16
10532 "win16",
10533#endif
10534#ifdef WIN32
10535 "win32",
10536#endif
10537#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10538 "win32unix",
10539#endif
10540#ifdef WIN64
10541 "win64",
10542#endif
10543#ifdef EBCDIC
10544 "ebcdic",
10545#endif
10546#ifndef CASE_INSENSITIVE_FILENAME
10547 "fname_case",
10548#endif
10549#ifdef FEAT_ARABIC
10550 "arabic",
10551#endif
10552#ifdef FEAT_AUTOCMD
10553 "autocmd",
10554#endif
10555#ifdef FEAT_BEVAL
10556 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010557# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10558 "balloon_multiline",
10559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560#endif
10561#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10562 "builtin_terms",
10563# ifdef ALL_BUILTIN_TCAPS
10564 "all_builtin_terms",
10565# endif
10566#endif
10567#ifdef FEAT_BYTEOFF
10568 "byte_offset",
10569#endif
10570#ifdef FEAT_CINDENT
10571 "cindent",
10572#endif
10573#ifdef FEAT_CLIENTSERVER
10574 "clientserver",
10575#endif
10576#ifdef FEAT_CLIPBOARD
10577 "clipboard",
10578#endif
10579#ifdef FEAT_CMDL_COMPL
10580 "cmdline_compl",
10581#endif
10582#ifdef FEAT_CMDHIST
10583 "cmdline_hist",
10584#endif
10585#ifdef FEAT_COMMENTS
10586 "comments",
10587#endif
10588#ifdef FEAT_CRYPT
10589 "cryptv",
10590#endif
10591#ifdef FEAT_CSCOPE
10592 "cscope",
10593#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010594#ifdef CURSOR_SHAPE
10595 "cursorshape",
10596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597#ifdef DEBUG
10598 "debug",
10599#endif
10600#ifdef FEAT_CON_DIALOG
10601 "dialog_con",
10602#endif
10603#ifdef FEAT_GUI_DIALOG
10604 "dialog_gui",
10605#endif
10606#ifdef FEAT_DIFF
10607 "diff",
10608#endif
10609#ifdef FEAT_DIGRAPHS
10610 "digraphs",
10611#endif
10612#ifdef FEAT_DND
10613 "dnd",
10614#endif
10615#ifdef FEAT_EMACS_TAGS
10616 "emacs_tags",
10617#endif
10618 "eval", /* always present, of course! */
10619#ifdef FEAT_EX_EXTRA
10620 "ex_extra",
10621#endif
10622#ifdef FEAT_SEARCH_EXTRA
10623 "extra_search",
10624#endif
10625#ifdef FEAT_FKMAP
10626 "farsi",
10627#endif
10628#ifdef FEAT_SEARCHPATH
10629 "file_in_path",
10630#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010631#if defined(UNIX) && !defined(USE_SYSTEM)
10632 "filterpipe",
10633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634#ifdef FEAT_FIND_ID
10635 "find_in_path",
10636#endif
10637#ifdef FEAT_FOLDING
10638 "folding",
10639#endif
10640#ifdef FEAT_FOOTER
10641 "footer",
10642#endif
10643#if !defined(USE_SYSTEM) && defined(UNIX)
10644 "fork",
10645#endif
10646#ifdef FEAT_GETTEXT
10647 "gettext",
10648#endif
10649#ifdef FEAT_GUI
10650 "gui",
10651#endif
10652#ifdef FEAT_GUI_ATHENA
10653# ifdef FEAT_GUI_NEXTAW
10654 "gui_neXtaw",
10655# else
10656 "gui_athena",
10657# endif
10658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659#ifdef FEAT_GUI_GTK
10660 "gui_gtk",
10661# ifdef HAVE_GTK2
10662 "gui_gtk2",
10663# endif
10664#endif
10665#ifdef FEAT_GUI_MAC
10666 "gui_mac",
10667#endif
10668#ifdef FEAT_GUI_MOTIF
10669 "gui_motif",
10670#endif
10671#ifdef FEAT_GUI_PHOTON
10672 "gui_photon",
10673#endif
10674#ifdef FEAT_GUI_W16
10675 "gui_win16",
10676#endif
10677#ifdef FEAT_GUI_W32
10678 "gui_win32",
10679#endif
10680#ifdef FEAT_HANGULIN
10681 "hangul_input",
10682#endif
10683#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10684 "iconv",
10685#endif
10686#ifdef FEAT_INS_EXPAND
10687 "insert_expand",
10688#endif
10689#ifdef FEAT_JUMPLIST
10690 "jumplist",
10691#endif
10692#ifdef FEAT_KEYMAP
10693 "keymap",
10694#endif
10695#ifdef FEAT_LANGMAP
10696 "langmap",
10697#endif
10698#ifdef FEAT_LIBCALL
10699 "libcall",
10700#endif
10701#ifdef FEAT_LINEBREAK
10702 "linebreak",
10703#endif
10704#ifdef FEAT_LISP
10705 "lispindent",
10706#endif
10707#ifdef FEAT_LISTCMDS
10708 "listcmds",
10709#endif
10710#ifdef FEAT_LOCALMAP
10711 "localmap",
10712#endif
10713#ifdef FEAT_MENU
10714 "menu",
10715#endif
10716#ifdef FEAT_SESSION
10717 "mksession",
10718#endif
10719#ifdef FEAT_MODIFY_FNAME
10720 "modify_fname",
10721#endif
10722#ifdef FEAT_MOUSE
10723 "mouse",
10724#endif
10725#ifdef FEAT_MOUSESHAPE
10726 "mouseshape",
10727#endif
10728#if defined(UNIX) || defined(VMS)
10729# ifdef FEAT_MOUSE_DEC
10730 "mouse_dec",
10731# endif
10732# ifdef FEAT_MOUSE_GPM
10733 "mouse_gpm",
10734# endif
10735# ifdef FEAT_MOUSE_JSB
10736 "mouse_jsbterm",
10737# endif
10738# ifdef FEAT_MOUSE_NET
10739 "mouse_netterm",
10740# endif
10741# ifdef FEAT_MOUSE_PTERM
10742 "mouse_pterm",
10743# endif
10744# ifdef FEAT_MOUSE_XTERM
10745 "mouse_xterm",
10746# endif
10747#endif
10748#ifdef FEAT_MBYTE
10749 "multi_byte",
10750#endif
10751#ifdef FEAT_MBYTE_IME
10752 "multi_byte_ime",
10753#endif
10754#ifdef FEAT_MULTI_LANG
10755 "multi_lang",
10756#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010757#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010758#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010759 "mzscheme",
10760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762#ifdef FEAT_OLE
10763 "ole",
10764#endif
10765#ifdef FEAT_OSFILETYPE
10766 "osfiletype",
10767#endif
10768#ifdef FEAT_PATH_EXTRA
10769 "path_extra",
10770#endif
10771#ifdef FEAT_PERL
10772#ifndef DYNAMIC_PERL
10773 "perl",
10774#endif
10775#endif
10776#ifdef FEAT_PYTHON
10777#ifndef DYNAMIC_PYTHON
10778 "python",
10779#endif
10780#endif
10781#ifdef FEAT_POSTSCRIPT
10782 "postscript",
10783#endif
10784#ifdef FEAT_PRINTER
10785 "printer",
10786#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010787#ifdef FEAT_PROFILE
10788 "profile",
10789#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010790#ifdef FEAT_RELTIME
10791 "reltime",
10792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793#ifdef FEAT_QUICKFIX
10794 "quickfix",
10795#endif
10796#ifdef FEAT_RIGHTLEFT
10797 "rightleft",
10798#endif
10799#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10800 "ruby",
10801#endif
10802#ifdef FEAT_SCROLLBIND
10803 "scrollbind",
10804#endif
10805#ifdef FEAT_CMDL_INFO
10806 "showcmd",
10807 "cmdline_info",
10808#endif
10809#ifdef FEAT_SIGNS
10810 "signs",
10811#endif
10812#ifdef FEAT_SMARTINDENT
10813 "smartindent",
10814#endif
10815#ifdef FEAT_SNIFF
10816 "sniff",
10817#endif
10818#ifdef FEAT_STL_OPT
10819 "statusline",
10820#endif
10821#ifdef FEAT_SUN_WORKSHOP
10822 "sun_workshop",
10823#endif
10824#ifdef FEAT_NETBEANS_INTG
10825 "netbeans_intg",
10826#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010827#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010828 "spell",
10829#endif
10830#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 "syntax",
10832#endif
10833#if defined(USE_SYSTEM) || !defined(UNIX)
10834 "system",
10835#endif
10836#ifdef FEAT_TAG_BINS
10837 "tag_binary",
10838#endif
10839#ifdef FEAT_TAG_OLDSTATIC
10840 "tag_old_static",
10841#endif
10842#ifdef FEAT_TAG_ANYWHITE
10843 "tag_any_white",
10844#endif
10845#ifdef FEAT_TCL
10846# ifndef DYNAMIC_TCL
10847 "tcl",
10848# endif
10849#endif
10850#ifdef TERMINFO
10851 "terminfo",
10852#endif
10853#ifdef FEAT_TERMRESPONSE
10854 "termresponse",
10855#endif
10856#ifdef FEAT_TEXTOBJ
10857 "textobjects",
10858#endif
10859#ifdef HAVE_TGETENT
10860 "tgetent",
10861#endif
10862#ifdef FEAT_TITLE
10863 "title",
10864#endif
10865#ifdef FEAT_TOOLBAR
10866 "toolbar",
10867#endif
10868#ifdef FEAT_USR_CMDS
10869 "user-commands", /* was accidentally included in 5.4 */
10870 "user_commands",
10871#endif
10872#ifdef FEAT_VIMINFO
10873 "viminfo",
10874#endif
10875#ifdef FEAT_VERTSPLIT
10876 "vertsplit",
10877#endif
10878#ifdef FEAT_VIRTUALEDIT
10879 "virtualedit",
10880#endif
10881#ifdef FEAT_VISUAL
10882 "visual",
10883#endif
10884#ifdef FEAT_VISUALEXTRA
10885 "visualextra",
10886#endif
10887#ifdef FEAT_VREPLACE
10888 "vreplace",
10889#endif
10890#ifdef FEAT_WILDIGN
10891 "wildignore",
10892#endif
10893#ifdef FEAT_WILDMENU
10894 "wildmenu",
10895#endif
10896#ifdef FEAT_WINDOWS
10897 "windows",
10898#endif
10899#ifdef FEAT_WAK
10900 "winaltkeys",
10901#endif
10902#ifdef FEAT_WRITEBACKUP
10903 "writebackup",
10904#endif
10905#ifdef FEAT_XIM
10906 "xim",
10907#endif
10908#ifdef FEAT_XFONTSET
10909 "xfontset",
10910#endif
10911#ifdef USE_XSMP
10912 "xsmp",
10913#endif
10914#ifdef USE_XSMP_INTERACT
10915 "xsmp_interact",
10916#endif
10917#ifdef FEAT_XCLIPBOARD
10918 "xterm_clipboard",
10919#endif
10920#ifdef FEAT_XTERM_SAVE
10921 "xterm_save",
10922#endif
10923#if defined(UNIX) && defined(FEAT_X11)
10924 "X11",
10925#endif
10926 NULL
10927 };
10928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 for (i = 0; has_list[i] != NULL; ++i)
10931 if (STRICMP(name, has_list[i]) == 0)
10932 {
10933 n = TRUE;
10934 break;
10935 }
10936
10937 if (n == FALSE)
10938 {
10939 if (STRNICMP(name, "patch", 5) == 0)
10940 n = has_patch(atoi((char *)name + 5));
10941 else if (STRICMP(name, "vim_starting") == 0)
10942 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010943#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10944 else if (STRICMP(name, "balloon_multiline") == 0)
10945 n = multiline_balloon_available();
10946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947#ifdef DYNAMIC_TCL
10948 else if (STRICMP(name, "tcl") == 0)
10949 n = tcl_enabled(FALSE);
10950#endif
10951#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10952 else if (STRICMP(name, "iconv") == 0)
10953 n = iconv_enabled(FALSE);
10954#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010955#ifdef DYNAMIC_MZSCHEME
10956 else if (STRICMP(name, "mzscheme") == 0)
10957 n = mzscheme_enabled(FALSE);
10958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959#ifdef DYNAMIC_RUBY
10960 else if (STRICMP(name, "ruby") == 0)
10961 n = ruby_enabled(FALSE);
10962#endif
10963#ifdef DYNAMIC_PYTHON
10964 else if (STRICMP(name, "python") == 0)
10965 n = python_enabled(FALSE);
10966#endif
10967#ifdef DYNAMIC_PERL
10968 else if (STRICMP(name, "perl") == 0)
10969 n = perl_enabled(FALSE);
10970#endif
10971#ifdef FEAT_GUI
10972 else if (STRICMP(name, "gui_running") == 0)
10973 n = (gui.in_use || gui.starting);
10974# ifdef FEAT_GUI_W32
10975 else if (STRICMP(name, "gui_win32s") == 0)
10976 n = gui_is_win32s();
10977# endif
10978# ifdef FEAT_BROWSE
10979 else if (STRICMP(name, "browse") == 0)
10980 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10981# endif
10982#endif
10983#ifdef FEAT_SYN_HL
10984 else if (STRICMP(name, "syntax_items") == 0)
10985 n = syntax_present(curbuf);
10986#endif
10987#if defined(WIN3264)
10988 else if (STRICMP(name, "win95") == 0)
10989 n = mch_windows95();
10990#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010991#ifdef FEAT_NETBEANS_INTG
10992 else if (STRICMP(name, "netbeans_enabled") == 0)
10993 n = usingNetbeans;
10994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 }
10996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010997 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998}
10999
11000/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 * "has_key()" function
11002 */
11003 static void
11004f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011007{
11008 rettv->vval.v_number = 0;
11009 if (argvars[0].v_type != VAR_DICT)
11010 {
11011 EMSG(_(e_dictreq));
11012 return;
11013 }
11014 if (argvars[0].vval.v_dict == NULL)
11015 return;
11016
11017 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011018 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019}
11020
11021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 * "hasmapto()" function
11023 */
11024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *argvars;
11027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028{
11029 char_u *name;
11030 char_u *mode;
11031 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011032 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011035 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 mode = (char_u *)"nvo";
11037 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011040 if (argvars[2].v_type != VAR_UNKNOWN)
11041 abbr = get_tv_number(&argvars[2]);
11042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
Bram Moolenaar2c932302006-03-18 21:42:09 +000011044 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048}
11049
11050/*
11051 * "histadd()" function
11052 */
11053/*ARGSUSED*/
11054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011056 typval_T *argvars;
11057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058{
11059#ifdef FEAT_CMDHIST
11060 int histype;
11061 char_u *str;
11062 char_u buf[NUMBUFLEN];
11063#endif
11064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 if (check_restricted() || check_secure())
11067 return;
11068#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11070 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 if (histype >= 0)
11072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 if (*str != NUL)
11075 {
11076 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 return;
11079 }
11080 }
11081#endif
11082}
11083
11084/*
11085 * "histdel()" function
11086 */
11087/*ARGSUSED*/
11088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011090 typval_T *argvars;
11091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
11093#ifdef FEAT_CMDHIST
11094 int n;
11095 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11099 if (str == NULL)
11100 n = 0;
11101 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011103 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011104 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 else
11109 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 get_tv_string_buf(&argvars[1], buf));
11112 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115#endif
11116}
11117
11118/*
11119 * "histget()" function
11120 */
11121/*ARGSUSED*/
11122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126{
11127#ifdef FEAT_CMDHIST
11128 int type;
11129 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11133 if (str == NULL)
11134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 {
11137 type = get_histtype(str);
11138 if (argvars[1].v_type == VAR_UNKNOWN)
11139 idx = get_history_idx(type);
11140 else
11141 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11142 /* -1 on type error */
11143 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149}
11150
11151/*
11152 * "histnr()" function
11153 */
11154/*ARGSUSED*/
11155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159{
11160 int i;
11161
11162#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 char_u *history = get_tv_string_chk(&argvars[0]);
11164
11165 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 if (i >= HIST_CMD && i < HIST_COUNT)
11167 i = get_history_idx(i);
11168 else
11169#endif
11170 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172}
11173
11174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 * "highlightID(name)" function
11176 */
11177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011179 typval_T *argvars;
11180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183}
11184
11185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011186 * "highlight_exists()" function
11187 */
11188 static void
11189f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192{
11193 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11194}
11195
11196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 * "hostname()" function
11198 */
11199/*ARGSUSED*/
11200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011202 typval_T *argvars;
11203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
11205 char_u hostname[256];
11206
11207 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 rettv->v_type = VAR_STRING;
11209 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210}
11211
11212/*
11213 * iconv() function
11214 */
11215/*ARGSUSED*/
11216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 typval_T *argvars;
11219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220{
11221#ifdef FEAT_MBYTE
11222 char_u buf1[NUMBUFLEN];
11223 char_u buf2[NUMBUFLEN];
11224 char_u *from, *to, *str;
11225 vimconv_T vimconv;
11226#endif
11227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 rettv->v_type = VAR_STRING;
11229 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
11231#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 str = get_tv_string(&argvars[0]);
11233 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11234 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 vimconv.vc_type = CONV_NONE;
11236 convert_setup(&vimconv, from, to);
11237
11238 /* If the encodings are equal, no conversion needed. */
11239 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243
11244 convert_setup(&vimconv, NULL, NULL);
11245 vim_free(from);
11246 vim_free(to);
11247#endif
11248}
11249
11250/*
11251 * "indent()" function
11252 */
11253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011255 typval_T *argvars;
11256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
11258 linenr_T lnum;
11259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265}
11266
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011267/*
11268 * "index()" function
11269 */
11270 static void
11271f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011272 typval_T *argvars;
11273 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011274{
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 list_T *l;
11276 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011277 long idx = 0;
11278 int ic = FALSE;
11279
11280 rettv->vval.v_number = -1;
11281 if (argvars[0].v_type != VAR_LIST)
11282 {
11283 EMSG(_(e_listreq));
11284 return;
11285 }
11286 l = argvars[0].vval.v_list;
11287 if (l != NULL)
11288 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011289 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011290 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 int error = FALSE;
11293
Bram Moolenaar758711c2005-02-02 23:11:38 +000011294 /* Start at specified item. Use the cached index that list_find()
11295 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011297 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011298 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 ic = get_tv_number_chk(&argvars[3], &error);
11300 if (error)
11301 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011302 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011303
Bram Moolenaar758711c2005-02-02 23:11:38 +000011304 for ( ; item != NULL; item = item->li_next, ++idx)
11305 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011306 {
11307 rettv->vval.v_number = idx;
11308 break;
11309 }
11310 }
11311}
11312
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313static int inputsecret_flag = 0;
11314
11315/*
11316 * "input()" function
11317 * Also handles inputsecret() when inputsecret is set.
11318 */
11319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *argvars;
11322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 char_u *p = NULL;
11326 int c;
11327 char_u buf[NUMBUFLEN];
11328 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011329 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011330 int xp_type = EXPAND_NOTHING;
11331 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
11335#ifdef NO_CONSOLE_INPUT
11336 /* While starting up, there is no place to enter text. */
11337 if (no_console_input())
11338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 return;
11341 }
11342#endif
11343
11344 cmd_silent = FALSE; /* Want to see the prompt. */
11345 if (prompt != NULL)
11346 {
11347 /* Only the part of the message after the last NL is considered as
11348 * prompt for the command line */
11349 p = vim_strrchr(prompt, '\n');
11350 if (p == NULL)
11351 p = prompt;
11352 else
11353 {
11354 ++p;
11355 c = *p;
11356 *p = NUL;
11357 msg_start();
11358 msg_clr_eos();
11359 msg_puts_attr(prompt, echo_attr);
11360 msg_didout = FALSE;
11361 msg_starthere();
11362 *p = c;
11363 }
11364 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 if (argvars[1].v_type != VAR_UNKNOWN)
11367 {
11368 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11369 if (defstr != NULL)
11370 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371
Bram Moolenaar4463f292005-09-25 22:20:24 +000011372 if (argvars[2].v_type != VAR_UNKNOWN)
11373 {
11374 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011375 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011376 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011377
Bram Moolenaar4463f292005-09-25 22:20:24 +000011378 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011379
Bram Moolenaar4463f292005-09-25 22:20:24 +000011380 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11381 if (xp_name == NULL)
11382 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011383
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011384 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011385
Bram Moolenaar4463f292005-09-25 22:20:24 +000011386 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11387 &xp_arg) == FAIL)
11388 return;
11389 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011390 }
11391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 if (defstr != NULL)
11393 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011394 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11395 xp_type, xp_arg);
11396
11397 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 /* since the user typed this, no need to wait for return */
11400 need_wait_return = FALSE;
11401 msg_didout = FALSE;
11402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 cmd_silent = cmd_silent_save;
11404}
11405
11406/*
11407 * "inputdialog()" function
11408 */
11409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414#if defined(FEAT_GUI_TEXTDIALOG)
11415 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11416 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11417 {
11418 char_u *message;
11419 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 message = get_tv_string_chk(&argvars[0]);
11423 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011424 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011425 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 else
11427 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 if (message != NULL && defstr != NULL
11429 && do_dialog(VIM_QUESTION, NULL, message,
11430 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 else
11433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011434 if (message != NULL && defstr != NULL
11435 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011436 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->vval.v_string = vim_strsave(
11438 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 }
11444 else
11445#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447}
11448
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011449/*
11450 * "inputlist()" function
11451 */
11452 static void
11453f_inputlist(argvars, rettv)
11454 typval_T *argvars;
11455 typval_T *rettv;
11456{
11457 listitem_T *li;
11458 int selected;
11459 int mouse_used;
11460
11461 rettv->vval.v_number = 0;
11462#ifdef NO_CONSOLE_INPUT
11463 /* While starting up, there is no place to enter text. */
11464 if (no_console_input())
11465 return;
11466#endif
11467 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11468 {
11469 EMSG2(_(e_listarg), "inputlist()");
11470 return;
11471 }
11472
11473 msg_start();
11474 lines_left = Rows; /* avoid more prompt */
11475 msg_scroll = TRUE;
11476 msg_clr_eos();
11477
11478 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11479 {
11480 msg_puts(get_tv_string(&li->li_tv));
11481 msg_putchar('\n');
11482 }
11483
11484 /* Ask for choice. */
11485 selected = prompt_for_number(&mouse_used);
11486 if (mouse_used)
11487 selected -= lines_left;
11488
11489 rettv->vval.v_number = selected;
11490}
11491
11492
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11494
11495/*
11496 * "inputrestore()" function
11497 */
11498/*ARGSUSED*/
11499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
11504 if (ga_userinput.ga_len > 0)
11505 {
11506 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11508 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 }
11511 else if (p_verbose > 1)
11512 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011513 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 }
11516}
11517
11518/*
11519 * "inputsave()" function
11520 */
11521/*ARGSUSED*/
11522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011524 typval_T *argvars;
11525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526{
11527 /* Add an entry to the stack of typehead storage. */
11528 if (ga_grow(&ga_userinput, 1) == OK)
11529 {
11530 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11531 + ga_userinput.ga_len);
11532 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 }
11535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537}
11538
11539/*
11540 * "inputsecret()" function
11541 */
11542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 typval_T *argvars;
11545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546{
11547 ++cmdline_star;
11548 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 --cmdline_star;
11551 --inputsecret_flag;
11552}
11553
11554/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011555 * "insert()" function
11556 */
11557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011559 typval_T *argvars;
11560 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011561{
11562 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011563 listitem_T *item;
11564 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011566
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011567 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011568 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011570 else if ((l = argvars[0].vval.v_list) != NULL
11571 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011572 {
11573 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 before = get_tv_number_chk(&argvars[2], &error);
11575 if (error)
11576 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011577
Bram Moolenaar758711c2005-02-02 23:11:38 +000011578 if (before == l->lv_len)
11579 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011580 else
11581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011582 item = list_find(l, before);
11583 if (item == NULL)
11584 {
11585 EMSGN(_(e_listidx), before);
11586 l = NULL;
11587 }
11588 }
11589 if (l != NULL)
11590 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011591 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011592 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 }
11594 }
11595}
11596
11597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 * "isdirectory()" function
11599 */
11600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 typval_T *argvars;
11603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606}
11607
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011608/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011609 * "islocked()" function
11610 */
11611 static void
11612f_islocked(argvars, rettv)
11613 typval_T *argvars;
11614 typval_T *rettv;
11615{
11616 lval_T lv;
11617 char_u *end;
11618 dictitem_T *di;
11619
11620 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011621 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11622 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011623 if (end != NULL && lv.ll_name != NULL)
11624 {
11625 if (*end != NUL)
11626 EMSG(_(e_trailing));
11627 else
11628 {
11629 if (lv.ll_tv == NULL)
11630 {
11631 if (check_changedtick(lv.ll_name))
11632 rettv->vval.v_number = 1; /* always locked */
11633 else
11634 {
11635 di = find_var(lv.ll_name, NULL);
11636 if (di != NULL)
11637 {
11638 /* Consider a variable locked when:
11639 * 1. the variable itself is locked
11640 * 2. the value of the variable is locked.
11641 * 3. the List or Dict value is locked.
11642 */
11643 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11644 || tv_islocked(&di->di_tv));
11645 }
11646 }
11647 }
11648 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011649 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011650 else if (lv.ll_newkey != NULL)
11651 EMSG2(_(e_dictkey), lv.ll_newkey);
11652 else if (lv.ll_list != NULL)
11653 /* List item. */
11654 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11655 else
11656 /* Dictionary item. */
11657 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11658 }
11659 }
11660
11661 clear_lval(&lv);
11662}
11663
Bram Moolenaar33570922005-01-25 22:26:29 +000011664static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011665
11666/*
11667 * Turn a dict into a list:
11668 * "what" == 0: list of keys
11669 * "what" == 1: list of values
11670 * "what" == 2: list of items
11671 */
11672 static void
11673dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 typval_T *argvars;
11675 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011676 int what;
11677{
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 list_T *l2;
11679 dictitem_T *di;
11680 hashitem_T *hi;
11681 listitem_T *li;
11682 listitem_T *li2;
11683 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011684 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011685
11686 rettv->vval.v_number = 0;
11687 if (argvars[0].v_type != VAR_DICT)
11688 {
11689 EMSG(_(e_dictreq));
11690 return;
11691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011692 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011693 return;
11694
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011695 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011696 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011697
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011698 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011699 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011700 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011701 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011702 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011703 --todo;
11704 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011705
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011706 li = listitem_alloc();
11707 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011708 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011709 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011710
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011711 if (what == 0)
11712 {
11713 /* keys() */
11714 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011715 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011716 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11717 }
11718 else if (what == 1)
11719 {
11720 /* values() */
11721 copy_tv(&di->di_tv, &li->li_tv);
11722 }
11723 else
11724 {
11725 /* items() */
11726 l2 = list_alloc();
11727 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011728 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011729 li->li_tv.vval.v_list = l2;
11730 if (l2 == NULL)
11731 break;
11732 ++l2->lv_refcount;
11733
11734 li2 = listitem_alloc();
11735 if (li2 == NULL)
11736 break;
11737 list_append(l2, li2);
11738 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011739 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011740 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11741
11742 li2 = listitem_alloc();
11743 if (li2 == NULL)
11744 break;
11745 list_append(l2, li2);
11746 copy_tv(&di->di_tv, &li2->li_tv);
11747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011748 }
11749 }
11750}
11751
11752/*
11753 * "items(dict)" function
11754 */
11755 static void
11756f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011757 typval_T *argvars;
11758 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011759{
11760 dict_list(argvars, rettv, 2);
11761}
11762
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011764 * "join()" function
11765 */
11766 static void
11767f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011768 typval_T *argvars;
11769 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011770{
11771 garray_T ga;
11772 char_u *sep;
11773
11774 rettv->vval.v_number = 0;
11775 if (argvars[0].v_type != VAR_LIST)
11776 {
11777 EMSG(_(e_listreq));
11778 return;
11779 }
11780 if (argvars[0].vval.v_list == NULL)
11781 return;
11782 if (argvars[1].v_type == VAR_UNKNOWN)
11783 sep = (char_u *)" ";
11784 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011786
11787 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788
11789 if (sep != NULL)
11790 {
11791 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011792 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793 ga_append(&ga, NUL);
11794 rettv->vval.v_string = (char_u *)ga.ga_data;
11795 }
11796 else
11797 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011798}
11799
11800/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011801 * "keys()" function
11802 */
11803 static void
11804f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011805 typval_T *argvars;
11806 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011807{
11808 dict_list(argvars, rettv, 0);
11809}
11810
11811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 * "last_buffer_nr()" function.
11813 */
11814/*ARGSUSED*/
11815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011817 typval_T *argvars;
11818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819{
11820 int n = 0;
11821 buf_T *buf;
11822
11823 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11824 if (n < buf->b_fnum)
11825 n = buf->b_fnum;
11826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011828}
11829
11830/*
11831 * "len()" function
11832 */
11833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 typval_T *argvars;
11836 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011837{
11838 switch (argvars[0].v_type)
11839 {
11840 case VAR_STRING:
11841 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number = (varnumber_T)STRLEN(
11843 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011844 break;
11845 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 case VAR_DICT:
11849 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11850 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011851 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011852 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 break;
11854 }
11855}
11856
Bram Moolenaar33570922005-01-25 22:26:29 +000011857static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011858
11859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 typval_T *argvars;
11862 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011863 int type;
11864{
11865#ifdef FEAT_LIBCALL
11866 char_u *string_in;
11867 char_u **string_result;
11868 int nr_result;
11869#endif
11870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011872 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011876
11877 if (check_restricted() || check_secure())
11878 return;
11879
11880#ifdef FEAT_LIBCALL
11881 /* The first two args must be strings, otherwise its meaningless */
11882 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11883 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011884 string_in = NULL;
11885 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011886 string_in = argvars[2].vval.v_string;
11887 if (type == VAR_NUMBER)
11888 string_result = NULL;
11889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011891 if (mch_libcall(argvars[0].vval.v_string,
11892 argvars[1].vval.v_string,
11893 string_in,
11894 argvars[2].vval.v_number,
11895 string_result,
11896 &nr_result) == OK
11897 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011899 }
11900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901}
11902
11903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904 * "libcall()" function
11905 */
11906 static void
11907f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910{
11911 libcall_common(argvars, rettv, VAR_STRING);
11912}
11913
11914/*
11915 * "libcallnr()" function
11916 */
11917 static void
11918f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011921{
11922 libcall_common(argvars, rettv, VAR_NUMBER);
11923}
11924
11925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 * "line(string)" function
11927 */
11928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011929f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011930 typval_T *argvars;
11931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932{
11933 linenr_T lnum = 0;
11934 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011935 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011937 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 if (fp != NULL)
11939 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941}
11942
11943/*
11944 * "line2byte(lnum)" function
11945 */
11946/*ARGSUSED*/
11947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011949 typval_T *argvars;
11950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951{
11952#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954#else
11955 linenr_T lnum;
11956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11962 if (rettv->vval.v_number >= 0)
11963 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964#endif
11965}
11966
11967/*
11968 * "lispindent(lnum)" function
11969 */
11970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011971f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 typval_T *argvars;
11973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974{
11975#ifdef FEAT_LISP
11976 pos_T pos;
11977 linenr_T lnum;
11978
11979 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11982 {
11983 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 curwin->w_cursor = pos;
11986 }
11987 else
11988#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
11993 * "localtime()" function
11994 */
11995/*ARGSUSED*/
11996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011998 typval_T *argvars;
11999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002}
12003
Bram Moolenaar33570922005-01-25 22:26:29 +000012004static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005
12006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 int exact;
12011{
12012 char_u *keys;
12013 char_u *which;
12014 char_u buf[NUMBUFLEN];
12015 char_u *keys_buf = NULL;
12016 char_u *rhs;
12017 int mode;
12018 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012019 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020
12021 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->v_type = VAR_STRING;
12023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 if (*keys == NUL)
12027 return;
12028
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012029 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012032 if (argvars[2].v_type != VAR_UNKNOWN)
12033 abbr = get_tv_number(&argvars[2]);
12034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 else
12036 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 if (which == NULL)
12038 return;
12039
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 mode = get_map_mode(&which, 0);
12041
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012042 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012043 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 vim_free(keys_buf);
12045 if (rhs != NULL)
12046 {
12047 ga_init(&ga);
12048 ga.ga_itemsize = 1;
12049 ga.ga_growsize = 40;
12050
12051 while (*rhs != NUL)
12052 ga_concat(&ga, str2special(&rhs, FALSE));
12053
12054 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 }
12057}
12058
12059/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060 * "map()" function
12061 */
12062 static void
12063f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012066{
12067 filter_map(argvars, rettv, TRUE);
12068}
12069
12070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012071 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 */
12073 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012074f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012075 typval_T *argvars;
12076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079}
12080
12081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 */
12084 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 typval_T *argvars;
12087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090}
12091
Bram Moolenaar33570922005-01-25 22:26:29 +000012092static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 int type;
12099{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012100 char_u *str = NULL;
12101 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 char_u *pat;
12103 regmatch_T regmatch;
12104 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012105 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 char_u *save_cpo;
12107 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012108 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012109 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012110 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 list_T *l = NULL;
12112 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012113 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012114 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115
12116 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12117 save_cpo = p_cpo;
12118 p_cpo = (char_u *)"";
12119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012120 rettv->vval.v_number = -1;
12121 if (type == 3)
12122 {
12123 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012125 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012126 }
12127 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012129 rettv->v_type = VAR_STRING;
12130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012133 if (argvars[0].v_type == VAR_LIST)
12134 {
12135 if ((l = argvars[0].vval.v_list) == NULL)
12136 goto theend;
12137 li = l->lv_first;
12138 }
12139 else
12140 expr = str = get_tv_string(&argvars[0]);
12141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12143 if (pat == NULL)
12144 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012145
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012148 int error = FALSE;
12149
12150 start = get_tv_number_chk(&argvars[2], &error);
12151 if (error)
12152 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012153 if (l != NULL)
12154 {
12155 li = list_find(l, start);
12156 if (li == NULL)
12157 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012158 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012159 }
12160 else
12161 {
12162 if (start < 0)
12163 start = 0;
12164 if (start > (long)STRLEN(str))
12165 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012166 /* When "count" argument is there ignore matches before "start",
12167 * otherwise skip part of the string. Differs when pattern is "^"
12168 * or "\<". */
12169 if (argvars[3].v_type != VAR_UNKNOWN)
12170 startcol = start;
12171 else
12172 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012173 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012174
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012175 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012176 nth = get_tv_number_chk(&argvars[3], &error);
12177 if (error)
12178 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 }
12180
12181 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12182 if (regmatch.regprog != NULL)
12183 {
12184 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012185
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012186 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012187 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012188 if (l != NULL)
12189 {
12190 if (li == NULL)
12191 {
12192 match = FALSE;
12193 break;
12194 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012195 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012196 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012197 if (str == NULL)
12198 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012199 }
12200
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012201 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012202
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012203 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012204 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012205 if (l == NULL && !match)
12206 break;
12207
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012208 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012209 if (l != NULL)
12210 {
12211 li = li->li_next;
12212 ++idx;
12213 }
12214 else
12215 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012216#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012217 startcol = (colnr_T)(regmatch.startp[0]
12218 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012219#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012220 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012221#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012222 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012223 }
12224
12225 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012227 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012229 int i;
12230
12231 /* return list with matched string and submatches */
12232 for (i = 0; i < NSUBEXP; ++i)
12233 {
12234 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012235 {
12236 if (list_append_string(rettv->vval.v_list,
12237 (char_u *)"", 0) == FAIL)
12238 break;
12239 }
12240 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012241 regmatch.startp[i],
12242 (int)(regmatch.endp[i] - regmatch.startp[i]))
12243 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012244 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012245 }
12246 }
12247 else if (type == 2)
12248 {
12249 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012250 if (l != NULL)
12251 copy_tv(&li->li_tv, rettv);
12252 else
12253 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012255 }
12256 else if (l != NULL)
12257 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 else
12259 {
12260 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 (varnumber_T)(regmatch.startp[0] - str);
12263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012266 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 }
12268 }
12269 vim_free(regmatch.regprog);
12270 }
12271
12272theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012273 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 p_cpo = save_cpo;
12275}
12276
12277/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278 * "match()" function
12279 */
12280 static void
12281f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012282 typval_T *argvars;
12283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012284{
12285 find_some_match(argvars, rettv, 1);
12286}
12287
12288/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012289 * "matcharg()" function
12290 */
12291 static void
12292f_matcharg(argvars, rettv)
12293 typval_T *argvars;
12294 typval_T *rettv;
12295{
12296 if (rettv_list_alloc(rettv) == OK)
12297 {
12298#ifdef FEAT_SEARCH_EXTRA
12299 int mi = get_tv_number(&argvars[0]);
12300
12301 if (mi >= 1 && mi <= 3)
12302 {
12303 list_append_string(rettv->vval.v_list,
12304 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12305 list_append_string(rettv->vval.v_list,
12306 curwin->w_match_pat[mi - 1], -1);
12307 }
12308#endif
12309 }
12310}
12311
12312/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313 * "matchend()" function
12314 */
12315 static void
12316f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012317 typval_T *argvars;
12318 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319{
12320 find_some_match(argvars, rettv, 0);
12321}
12322
12323/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012324 * "matchlist()" function
12325 */
12326 static void
12327f_matchlist(argvars, rettv)
12328 typval_T *argvars;
12329 typval_T *rettv;
12330{
12331 find_some_match(argvars, rettv, 3);
12332}
12333
12334/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335 * "matchstr()" function
12336 */
12337 static void
12338f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 typval_T *argvars;
12340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012341{
12342 find_some_match(argvars, rettv, 2);
12343}
12344
Bram Moolenaar33570922005-01-25 22:26:29 +000012345static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012346
12347 static void
12348max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012349 typval_T *argvars;
12350 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012351 int domax;
12352{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012353 long n = 0;
12354 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012355 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012356
12357 if (argvars[0].v_type == VAR_LIST)
12358 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012359 list_T *l;
12360 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012361
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012362 l = argvars[0].vval.v_list;
12363 if (l != NULL)
12364 {
12365 li = l->lv_first;
12366 if (li != NULL)
12367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012369 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012370 {
12371 li = li->li_next;
12372 if (li == NULL)
12373 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012374 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012375 if (domax ? i > n : i < n)
12376 n = i;
12377 }
12378 }
12379 }
12380 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012381 else if (argvars[0].v_type == VAR_DICT)
12382 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012383 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012384 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012386 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012387
12388 d = argvars[0].vval.v_dict;
12389 if (d != NULL)
12390 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012391 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012392 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012394 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012396 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012397 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012398 if (first)
12399 {
12400 n = i;
12401 first = FALSE;
12402 }
12403 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012404 n = i;
12405 }
12406 }
12407 }
12408 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012409 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012410 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012412}
12413
12414/*
12415 * "max()" function
12416 */
12417 static void
12418f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012419 typval_T *argvars;
12420 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012421{
12422 max_min(argvars, rettv, TRUE);
12423}
12424
12425/*
12426 * "min()" function
12427 */
12428 static void
12429f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *argvars;
12431 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012432{
12433 max_min(argvars, rettv, FALSE);
12434}
12435
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012436static int mkdir_recurse __ARGS((char_u *dir, int prot));
12437
12438/*
12439 * Create the directory in which "dir" is located, and higher levels when
12440 * needed.
12441 */
12442 static int
12443mkdir_recurse(dir, prot)
12444 char_u *dir;
12445 int prot;
12446{
12447 char_u *p;
12448 char_u *updir;
12449 int r = FAIL;
12450
12451 /* Get end of directory name in "dir".
12452 * We're done when it's "/" or "c:/". */
12453 p = gettail_sep(dir);
12454 if (p <= get_past_head(dir))
12455 return OK;
12456
12457 /* If the directory exists we're done. Otherwise: create it.*/
12458 updir = vim_strnsave(dir, (int)(p - dir));
12459 if (updir == NULL)
12460 return FAIL;
12461 if (mch_isdir(updir))
12462 r = OK;
12463 else if (mkdir_recurse(updir, prot) == OK)
12464 r = vim_mkdir_emsg(updir, prot);
12465 vim_free(updir);
12466 return r;
12467}
12468
12469#ifdef vim_mkdir
12470/*
12471 * "mkdir()" function
12472 */
12473 static void
12474f_mkdir(argvars, rettv)
12475 typval_T *argvars;
12476 typval_T *rettv;
12477{
12478 char_u *dir;
12479 char_u buf[NUMBUFLEN];
12480 int prot = 0755;
12481
12482 rettv->vval.v_number = FAIL;
12483 if (check_restricted() || check_secure())
12484 return;
12485
12486 dir = get_tv_string_buf(&argvars[0], buf);
12487 if (argvars[1].v_type != VAR_UNKNOWN)
12488 {
12489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 prot = get_tv_number_chk(&argvars[2], NULL);
12491 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012492 mkdir_recurse(dir, prot);
12493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012495}
12496#endif
12497
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 * "mode()" function
12500 */
12501/*ARGSUSED*/
12502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012504 typval_T *argvars;
12505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506{
12507 char_u buf[2];
12508
12509#ifdef FEAT_VISUAL
12510 if (VIsual_active)
12511 {
12512 if (VIsual_select)
12513 buf[0] = VIsual_mode + 's' - 'v';
12514 else
12515 buf[0] = VIsual_mode;
12516 }
12517 else
12518#endif
12519 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12520 buf[0] = 'r';
12521 else if (State & INSERT)
12522 {
12523 if (State & REPLACE_FLAG)
12524 buf[0] = 'R';
12525 else
12526 buf[0] = 'i';
12527 }
12528 else if (State & CMDLINE)
12529 buf[0] = 'c';
12530 else
12531 buf[0] = 'n';
12532
12533 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_string = vim_strsave(buf);
12535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536}
12537
12538/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539 * "nextnonblank()" function
12540 */
12541 static void
12542f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 typval_T *argvars;
12544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545{
12546 linenr_T lnum;
12547
12548 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551 {
12552 lnum = 0;
12553 break;
12554 }
12555 if (*skipwhite(ml_get(lnum)) != NUL)
12556 break;
12557 }
12558 rettv->vval.v_number = lnum;
12559}
12560
12561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 * "nr2char()" function
12563 */
12564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568{
12569 char_u buf[NUMBUFLEN];
12570
12571#ifdef FEAT_MBYTE
12572 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 else
12575#endif
12576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 buf[1] = NUL;
12579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580 rettv->v_type = VAR_STRING;
12581 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012582}
12583
12584/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012585 * "pathshorten()" function
12586 */
12587 static void
12588f_pathshorten(argvars, rettv)
12589 typval_T *argvars;
12590 typval_T *rettv;
12591{
12592 char_u *p;
12593
12594 rettv->v_type = VAR_STRING;
12595 p = get_tv_string_chk(&argvars[0]);
12596 if (p == NULL)
12597 rettv->vval.v_string = NULL;
12598 else
12599 {
12600 p = vim_strsave(p);
12601 rettv->vval.v_string = p;
12602 if (p != NULL)
12603 shorten_dir(p);
12604 }
12605}
12606
12607/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 * "prevnonblank()" function
12609 */
12610 static void
12611f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012612 typval_T *argvars;
12613 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012614{
12615 linenr_T lnum;
12616
12617 lnum = get_tv_lnum(argvars);
12618 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12619 lnum = 0;
12620 else
12621 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12622 --lnum;
12623 rettv->vval.v_number = lnum;
12624}
12625
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012626#ifdef HAVE_STDARG_H
12627/* This dummy va_list is here because:
12628 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12629 * - locally in the function results in a "used before set" warning
12630 * - using va_start() to initialize it gives "function with fixed args" error */
12631static va_list ap;
12632#endif
12633
Bram Moolenaar8c711452005-01-14 21:53:12 +000012634/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012635 * "printf()" function
12636 */
12637 static void
12638f_printf(argvars, rettv)
12639 typval_T *argvars;
12640 typval_T *rettv;
12641{
12642 rettv->v_type = VAR_STRING;
12643 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012644#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012645 {
12646 char_u buf[NUMBUFLEN];
12647 int len;
12648 char_u *s;
12649 int saved_did_emsg = did_emsg;
12650 char *fmt;
12651
12652 /* Get the required length, allocate the buffer and do it for real. */
12653 did_emsg = FALSE;
12654 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012655 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012656 if (!did_emsg)
12657 {
12658 s = alloc(len + 1);
12659 if (s != NULL)
12660 {
12661 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012662 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012663 }
12664 }
12665 did_emsg |= saved_did_emsg;
12666 }
12667#endif
12668}
12669
12670/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012671 * "pumvisible()" function
12672 */
12673/*ARGSUSED*/
12674 static void
12675f_pumvisible(argvars, rettv)
12676 typval_T *argvars;
12677 typval_T *rettv;
12678{
12679 rettv->vval.v_number = 0;
12680#ifdef FEAT_INS_EXPAND
12681 if (pum_visible())
12682 rettv->vval.v_number = 1;
12683#endif
12684}
12685
12686/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012687 * "range()" function
12688 */
12689 static void
12690f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 typval_T *argvars;
12692 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012693{
12694 long start;
12695 long end;
12696 long stride = 1;
12697 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012698 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012701 if (argvars[1].v_type == VAR_UNKNOWN)
12702 {
12703 end = start - 1;
12704 start = 0;
12705 }
12706 else
12707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012710 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012711 }
12712
12713 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 if (error)
12715 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012716 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012717 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012718 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012719 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012720 else
12721 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012722 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012724 if (list_append_number(rettv->vval.v_list,
12725 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012726 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012727 }
12728}
12729
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012730/*
12731 * "readfile()" function
12732 */
12733 static void
12734f_readfile(argvars, rettv)
12735 typval_T *argvars;
12736 typval_T *rettv;
12737{
12738 int binary = FALSE;
12739 char_u *fname;
12740 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012741 listitem_T *li;
12742#define FREAD_SIZE 200 /* optimized for text lines */
12743 char_u buf[FREAD_SIZE];
12744 int readlen; /* size of last fread() */
12745 int buflen; /* nr of valid chars in buf[] */
12746 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12747 int tolist; /* first byte in buf[] still to be put in list */
12748 int chop; /* how many CR to chop off */
12749 char_u *prev = NULL; /* previously read bytes, if any */
12750 int prevlen = 0; /* length of "prev" if not NULL */
12751 char_u *s;
12752 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012753 long maxline = MAXLNUM;
12754 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012755
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012756 if (argvars[1].v_type != VAR_UNKNOWN)
12757 {
12758 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12759 binary = TRUE;
12760 if (argvars[2].v_type != VAR_UNKNOWN)
12761 maxline = get_tv_number(&argvars[2]);
12762 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012763
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012764 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012765 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012766
12767 /* Always open the file in binary mode, library functions have a mind of
12768 * their own about CR-LF conversion. */
12769 fname = get_tv_string(&argvars[0]);
12770 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12771 {
12772 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12773 return;
12774 }
12775
12776 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012777 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012778 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012779 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012780 buflen = filtd + readlen;
12781 tolist = 0;
12782 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12783 {
12784 if (buf[filtd] == '\n' || readlen <= 0)
12785 {
12786 /* Only when in binary mode add an empty list item when the
12787 * last line ends in a '\n'. */
12788 if (!binary && readlen == 0 && filtd == 0)
12789 break;
12790
12791 /* Found end-of-line or end-of-file: add a text line to the
12792 * list. */
12793 chop = 0;
12794 if (!binary)
12795 while (filtd - chop - 1 >= tolist
12796 && buf[filtd - chop - 1] == '\r')
12797 ++chop;
12798 len = filtd - tolist - chop;
12799 if (prev == NULL)
12800 s = vim_strnsave(buf + tolist, len);
12801 else
12802 {
12803 s = alloc((unsigned)(prevlen + len + 1));
12804 if (s != NULL)
12805 {
12806 mch_memmove(s, prev, prevlen);
12807 vim_free(prev);
12808 prev = NULL;
12809 mch_memmove(s + prevlen, buf + tolist, len);
12810 s[prevlen + len] = NUL;
12811 }
12812 }
12813 tolist = filtd + 1;
12814
12815 li = listitem_alloc();
12816 if (li == NULL)
12817 {
12818 vim_free(s);
12819 break;
12820 }
12821 li->li_tv.v_type = VAR_STRING;
12822 li->li_tv.v_lock = 0;
12823 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012824 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012825
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012826 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012827 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012828 if (readlen <= 0)
12829 break;
12830 }
12831 else if (buf[filtd] == NUL)
12832 buf[filtd] = '\n';
12833 }
12834 if (readlen <= 0)
12835 break;
12836
12837 if (tolist == 0)
12838 {
12839 /* "buf" is full, need to move text to an allocated buffer */
12840 if (prev == NULL)
12841 {
12842 prev = vim_strnsave(buf, buflen);
12843 prevlen = buflen;
12844 }
12845 else
12846 {
12847 s = alloc((unsigned)(prevlen + buflen));
12848 if (s != NULL)
12849 {
12850 mch_memmove(s, prev, prevlen);
12851 mch_memmove(s + prevlen, buf, buflen);
12852 vim_free(prev);
12853 prev = s;
12854 prevlen += buflen;
12855 }
12856 }
12857 filtd = 0;
12858 }
12859 else
12860 {
12861 mch_memmove(buf, buf + tolist, buflen - tolist);
12862 filtd -= tolist;
12863 }
12864 }
12865
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012866 /*
12867 * For a negative line count use only the lines at the end of the file,
12868 * free the rest.
12869 */
12870 if (maxline < 0)
12871 while (cnt > -maxline)
12872 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012873 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012874 --cnt;
12875 }
12876
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012877 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012878 fclose(fd);
12879}
12880
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012881#if defined(FEAT_RELTIME)
12882static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12883
12884/*
12885 * Convert a List to proftime_T.
12886 * Return FAIL when there is something wrong.
12887 */
12888 static int
12889list2proftime(arg, tm)
12890 typval_T *arg;
12891 proftime_T *tm;
12892{
12893 long n1, n2;
12894 int error = FALSE;
12895
12896 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12897 || arg->vval.v_list->lv_len != 2)
12898 return FAIL;
12899 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12900 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12901# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012902 tm->HighPart = n1;
12903 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012904# else
12905 tm->tv_sec = n1;
12906 tm->tv_usec = n2;
12907# endif
12908 return error ? FAIL : OK;
12909}
12910#endif /* FEAT_RELTIME */
12911
12912/*
12913 * "reltime()" function
12914 */
12915 static void
12916f_reltime(argvars, rettv)
12917 typval_T *argvars;
12918 typval_T *rettv;
12919{
12920#ifdef FEAT_RELTIME
12921 proftime_T res;
12922 proftime_T start;
12923
12924 if (argvars[0].v_type == VAR_UNKNOWN)
12925 {
12926 /* No arguments: get current time. */
12927 profile_start(&res);
12928 }
12929 else if (argvars[1].v_type == VAR_UNKNOWN)
12930 {
12931 if (list2proftime(&argvars[0], &res) == FAIL)
12932 return;
12933 profile_end(&res);
12934 }
12935 else
12936 {
12937 /* Two arguments: compute the difference. */
12938 if (list2proftime(&argvars[0], &start) == FAIL
12939 || list2proftime(&argvars[1], &res) == FAIL)
12940 return;
12941 profile_sub(&res, &start);
12942 }
12943
12944 if (rettv_list_alloc(rettv) == OK)
12945 {
12946 long n1, n2;
12947
12948# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012949 n1 = res.HighPart;
12950 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012951# else
12952 n1 = res.tv_sec;
12953 n2 = res.tv_usec;
12954# endif
12955 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12956 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12957 }
12958#endif
12959}
12960
12961/*
12962 * "reltimestr()" function
12963 */
12964 static void
12965f_reltimestr(argvars, rettv)
12966 typval_T *argvars;
12967 typval_T *rettv;
12968{
12969#ifdef FEAT_RELTIME
12970 proftime_T tm;
12971#endif
12972
12973 rettv->v_type = VAR_STRING;
12974 rettv->vval.v_string = NULL;
12975#ifdef FEAT_RELTIME
12976 if (list2proftime(&argvars[0], &tm) == OK)
12977 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12978#endif
12979}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012980
Bram Moolenaar0d660222005-01-07 21:51:51 +000012981#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12982static void make_connection __ARGS((void));
12983static int check_connection __ARGS((void));
12984
12985 static void
12986make_connection()
12987{
12988 if (X_DISPLAY == NULL
12989# ifdef FEAT_GUI
12990 && !gui.in_use
12991# endif
12992 )
12993 {
12994 x_force_connect = TRUE;
12995 setup_term_clip();
12996 x_force_connect = FALSE;
12997 }
12998}
12999
13000 static int
13001check_connection()
13002{
13003 make_connection();
13004 if (X_DISPLAY == NULL)
13005 {
13006 EMSG(_("E240: No connection to Vim server"));
13007 return FAIL;
13008 }
13009 return OK;
13010}
13011#endif
13012
13013#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013014static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013015
13016 static void
13017remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 typval_T *argvars;
13019 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013020 int expr;
13021{
13022 char_u *server_name;
13023 char_u *keys;
13024 char_u *r = NULL;
13025 char_u buf[NUMBUFLEN];
13026# ifdef WIN32
13027 HWND w;
13028# else
13029 Window w;
13030# endif
13031
13032 if (check_restricted() || check_secure())
13033 return;
13034
13035# ifdef FEAT_X11
13036 if (check_connection() == FAIL)
13037 return;
13038# endif
13039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 server_name = get_tv_string_chk(&argvars[0]);
13041 if (server_name == NULL)
13042 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013043 keys = get_tv_string_buf(&argvars[1], buf);
13044# ifdef WIN32
13045 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13046# else
13047 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13048 < 0)
13049# endif
13050 {
13051 if (r != NULL)
13052 EMSG(r); /* sending worked but evaluation failed */
13053 else
13054 EMSG2(_("E241: Unable to send to %s"), server_name);
13055 return;
13056 }
13057
13058 rettv->vval.v_string = r;
13059
13060 if (argvars[2].v_type != VAR_UNKNOWN)
13061 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013063 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013065
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013066 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013067 v.di_tv.v_type = VAR_STRING;
13068 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013069 idvar = get_tv_string_chk(&argvars[2]);
13070 if (idvar != NULL)
13071 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013072 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013073 }
13074}
13075#endif
13076
13077/*
13078 * "remote_expr()" function
13079 */
13080/*ARGSUSED*/
13081 static void
13082f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 typval_T *argvars;
13084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013085{
13086 rettv->v_type = VAR_STRING;
13087 rettv->vval.v_string = NULL;
13088#ifdef FEAT_CLIENTSERVER
13089 remote_common(argvars, rettv, TRUE);
13090#endif
13091}
13092
13093/*
13094 * "remote_foreground()" function
13095 */
13096/*ARGSUSED*/
13097 static void
13098f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013099 typval_T *argvars;
13100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013101{
13102 rettv->vval.v_number = 0;
13103#ifdef FEAT_CLIENTSERVER
13104# ifdef WIN32
13105 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013106 {
13107 char_u *server_name = get_tv_string_chk(&argvars[0]);
13108
13109 if (server_name != NULL)
13110 serverForeground(server_name);
13111 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112# else
13113 /* Send a foreground() expression to the server. */
13114 argvars[1].v_type = VAR_STRING;
13115 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13116 argvars[2].v_type = VAR_UNKNOWN;
13117 remote_common(argvars, rettv, TRUE);
13118 vim_free(argvars[1].vval.v_string);
13119# endif
13120#endif
13121}
13122
13123/*ARGSUSED*/
13124 static void
13125f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013126 typval_T *argvars;
13127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128{
13129#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131 char_u *s = NULL;
13132# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013133 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013134# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013135 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013136
13137 if (check_restricted() || check_secure())
13138 {
13139 rettv->vval.v_number = -1;
13140 return;
13141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142 serverid = get_tv_string_chk(&argvars[0]);
13143 if (serverid == NULL)
13144 {
13145 rettv->vval.v_number = -1;
13146 return; /* type error; errmsg already given */
13147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013148# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013149 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013150 if (n == 0)
13151 rettv->vval.v_number = -1;
13152 else
13153 {
13154 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13155 rettv->vval.v_number = (s != NULL);
13156 }
13157# else
13158 rettv->vval.v_number = 0;
13159 if (check_connection() == FAIL)
13160 return;
13161
13162 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013163 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013164# endif
13165
13166 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013168 char_u *retvar;
13169
Bram Moolenaar33570922005-01-25 22:26:29 +000013170 v.di_tv.v_type = VAR_STRING;
13171 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013172 retvar = get_tv_string_chk(&argvars[1]);
13173 if (retvar != NULL)
13174 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013175 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013176 }
13177#else
13178 rettv->vval.v_number = -1;
13179#endif
13180}
13181
13182/*ARGSUSED*/
13183 static void
13184f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *argvars;
13186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013187{
13188 char_u *r = NULL;
13189
13190#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013191 char_u *serverid = get_tv_string_chk(&argvars[0]);
13192
13193 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013194 {
13195# ifdef WIN32
13196 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013197 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013198
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013199 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200 if (n != 0)
13201 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13202 if (r == NULL)
13203# else
13204 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206# endif
13207 EMSG(_("E277: Unable to read a server reply"));
13208 }
13209#endif
13210 rettv->v_type = VAR_STRING;
13211 rettv->vval.v_string = r;
13212}
13213
13214/*
13215 * "remote_send()" function
13216 */
13217/*ARGSUSED*/
13218 static void
13219f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *argvars;
13221 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013222{
13223 rettv->v_type = VAR_STRING;
13224 rettv->vval.v_string = NULL;
13225#ifdef FEAT_CLIENTSERVER
13226 remote_common(argvars, rettv, FALSE);
13227#endif
13228}
13229
13230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013231 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013232 */
13233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013235 typval_T *argvars;
13236 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013237{
Bram Moolenaar33570922005-01-25 22:26:29 +000013238 list_T *l;
13239 listitem_T *item, *item2;
13240 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013241 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013242 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013243 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013244 dict_T *d;
13245 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013246
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013247 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013248 if (argvars[0].v_type == VAR_DICT)
13249 {
13250 if (argvars[2].v_type != VAR_UNKNOWN)
13251 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013252 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013253 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013255 key = get_tv_string_chk(&argvars[1]);
13256 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 di = dict_find(d, key, -1);
13259 if (di == NULL)
13260 EMSG2(_(e_dictkey), key);
13261 else
13262 {
13263 *rettv = di->di_tv;
13264 init_tv(&di->di_tv);
13265 dictitem_remove(d, di);
13266 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013268 }
13269 }
13270 else if (argvars[0].v_type != VAR_LIST)
13271 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013272 else if ((l = argvars[0].vval.v_list) != NULL
13273 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 int error = FALSE;
13276
13277 idx = get_tv_number_chk(&argvars[1], &error);
13278 if (error)
13279 ; /* type error: do nothing, errmsg already given */
13280 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013281 EMSGN(_(e_listidx), idx);
13282 else
13283 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013284 if (argvars[2].v_type == VAR_UNKNOWN)
13285 {
13286 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013287 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013288 *rettv = item->li_tv;
13289 vim_free(item);
13290 }
13291 else
13292 {
13293 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 end = get_tv_number_chk(&argvars[2], &error);
13295 if (error)
13296 ; /* type error: do nothing */
13297 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013298 EMSGN(_(e_listidx), end);
13299 else
13300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013301 int cnt = 0;
13302
13303 for (li = item; li != NULL; li = li->li_next)
13304 {
13305 ++cnt;
13306 if (li == item2)
13307 break;
13308 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013309 if (li == NULL) /* didn't find "item2" after "item" */
13310 EMSG(_(e_invrange));
13311 else
13312 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013313 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013314 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013315 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013316 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013317 l->lv_first = item;
13318 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013319 item->li_prev = NULL;
13320 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013321 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013322 }
13323 }
13324 }
13325 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013326 }
13327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328}
13329
13330/*
13331 * "rename({from}, {to})" function
13332 */
13333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *argvars;
13336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337{
13338 char_u buf[NUMBUFLEN];
13339
13340 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13344 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345}
13346
13347/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013348 * "repeat()" function
13349 */
13350/*ARGSUSED*/
13351 static void
13352f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013355{
13356 char_u *p;
13357 int n;
13358 int slen;
13359 int len;
13360 char_u *r;
13361 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013362
13363 n = get_tv_number(&argvars[1]);
13364 if (argvars[0].v_type == VAR_LIST)
13365 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013366 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013367 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013368 if (list_extend(rettv->vval.v_list,
13369 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013370 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013371 }
13372 else
13373 {
13374 p = get_tv_string(&argvars[0]);
13375 rettv->v_type = VAR_STRING;
13376 rettv->vval.v_string = NULL;
13377
13378 slen = (int)STRLEN(p);
13379 len = slen * n;
13380 if (len <= 0)
13381 return;
13382
13383 r = alloc(len + 1);
13384 if (r != NULL)
13385 {
13386 for (i = 0; i < n; i++)
13387 mch_memmove(r + i * slen, p, (size_t)slen);
13388 r[len] = NUL;
13389 }
13390
13391 rettv->vval.v_string = r;
13392 }
13393}
13394
13395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 * "resolve()" function
13397 */
13398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 typval_T *argvars;
13401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402{
13403 char_u *p;
13404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406#ifdef FEAT_SHORTCUT
13407 {
13408 char_u *v = NULL;
13409
13410 v = mch_resolve_shortcut(p);
13411 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 }
13416#else
13417# ifdef HAVE_READLINK
13418 {
13419 char_u buf[MAXPATHL + 1];
13420 char_u *cpy;
13421 int len;
13422 char_u *remain = NULL;
13423 char_u *q;
13424 int is_relative_to_current = FALSE;
13425 int has_trailing_pathsep = FALSE;
13426 int limit = 100;
13427
13428 p = vim_strsave(p);
13429
13430 if (p[0] == '.' && (vim_ispathsep(p[1])
13431 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13432 is_relative_to_current = TRUE;
13433
13434 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013435 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 has_trailing_pathsep = TRUE;
13437
13438 q = getnextcomp(p);
13439 if (*q != NUL)
13440 {
13441 /* Separate the first path component in "p", and keep the
13442 * remainder (beginning with the path separator). */
13443 remain = vim_strsave(q - 1);
13444 q[-1] = NUL;
13445 }
13446
13447 for (;;)
13448 {
13449 for (;;)
13450 {
13451 len = readlink((char *)p, (char *)buf, MAXPATHL);
13452 if (len <= 0)
13453 break;
13454 buf[len] = NUL;
13455
13456 if (limit-- == 0)
13457 {
13458 vim_free(p);
13459 vim_free(remain);
13460 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 goto fail;
13463 }
13464
13465 /* Ensure that the result will have a trailing path separator
13466 * if the argument has one. */
13467 if (remain == NULL && has_trailing_pathsep)
13468 add_pathsep(buf);
13469
13470 /* Separate the first path component in the link value and
13471 * concatenate the remainders. */
13472 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13473 if (*q != NUL)
13474 {
13475 if (remain == NULL)
13476 remain = vim_strsave(q - 1);
13477 else
13478 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013479 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 if (cpy != NULL)
13481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 vim_free(remain);
13483 remain = cpy;
13484 }
13485 }
13486 q[-1] = NUL;
13487 }
13488
13489 q = gettail(p);
13490 if (q > p && *q == NUL)
13491 {
13492 /* Ignore trailing path separator. */
13493 q[-1] = NUL;
13494 q = gettail(p);
13495 }
13496 if (q > p && !mch_isFullName(buf))
13497 {
13498 /* symlink is relative to directory of argument */
13499 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13500 if (cpy != NULL)
13501 {
13502 STRCPY(cpy, p);
13503 STRCPY(gettail(cpy), buf);
13504 vim_free(p);
13505 p = cpy;
13506 }
13507 }
13508 else
13509 {
13510 vim_free(p);
13511 p = vim_strsave(buf);
13512 }
13513 }
13514
13515 if (remain == NULL)
13516 break;
13517
13518 /* Append the first path component of "remain" to "p". */
13519 q = getnextcomp(remain + 1);
13520 len = q - remain - (*q != NUL);
13521 cpy = vim_strnsave(p, STRLEN(p) + len);
13522 if (cpy != NULL)
13523 {
13524 STRNCAT(cpy, remain, len);
13525 vim_free(p);
13526 p = cpy;
13527 }
13528 /* Shorten "remain". */
13529 if (*q != NUL)
13530 STRCPY(remain, q - 1);
13531 else
13532 {
13533 vim_free(remain);
13534 remain = NULL;
13535 }
13536 }
13537
13538 /* If the result is a relative path name, make it explicitly relative to
13539 * the current directory if and only if the argument had this form. */
13540 if (!vim_ispathsep(*p))
13541 {
13542 if (is_relative_to_current
13543 && *p != NUL
13544 && !(p[0] == '.'
13545 && (p[1] == NUL
13546 || vim_ispathsep(p[1])
13547 || (p[1] == '.'
13548 && (p[2] == NUL
13549 || vim_ispathsep(p[2]))))))
13550 {
13551 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013552 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 if (cpy != NULL)
13554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 vim_free(p);
13556 p = cpy;
13557 }
13558 }
13559 else if (!is_relative_to_current)
13560 {
13561 /* Strip leading "./". */
13562 q = p;
13563 while (q[0] == '.' && vim_ispathsep(q[1]))
13564 q += 2;
13565 if (q > p)
13566 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13567 }
13568 }
13569
13570 /* Ensure that the result will have no trailing path separator
13571 * if the argument had none. But keep "/" or "//". */
13572 if (!has_trailing_pathsep)
13573 {
13574 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013575 if (after_pathsep(p, q))
13576 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 }
13578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 }
13581# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583# endif
13584#endif
13585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587
13588#ifdef HAVE_READLINK
13589fail:
13590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592}
13593
13594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 */
13597 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013598f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *argvars;
13600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601{
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 list_T *l;
13603 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605 rettv->vval.v_number = 0;
13606 if (argvars[0].v_type != VAR_LIST)
13607 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013608 else if ((l = argvars[0].vval.v_list) != NULL
13609 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013610 {
13611 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013612 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013613 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013614 while (li != NULL)
13615 {
13616 ni = li->li_prev;
13617 list_append(l, li);
13618 li = ni;
13619 }
13620 rettv->vval.v_list = l;
13621 rettv->v_type = VAR_LIST;
13622 ++l->lv_refcount;
13623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624}
13625
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013626#define SP_NOMOVE 0x01 /* don't move cursor */
13627#define SP_REPEAT 0x02 /* repeat to find outer pair */
13628#define SP_RETCOUNT 0x04 /* return matchcount */
13629#define SP_SETPCMARK 0x08 /* set previous context mark */
13630#define SP_START 0x10 /* accept match at start position */
13631#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13632#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013633
Bram Moolenaar33570922005-01-25 22:26:29 +000013634static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013635
13636/*
13637 * Get flags for a search function.
13638 * Possibly sets "p_ws".
13639 * Returns BACKWARD, FORWARD or zero (for an error).
13640 */
13641 static int
13642get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644 int *flagsp;
13645{
13646 int dir = FORWARD;
13647 char_u *flags;
13648 char_u nbuf[NUMBUFLEN];
13649 int mask;
13650
13651 if (varp->v_type != VAR_UNKNOWN)
13652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 flags = get_tv_string_buf_chk(varp, nbuf);
13654 if (flags == NULL)
13655 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013656 while (*flags != NUL)
13657 {
13658 switch (*flags)
13659 {
13660 case 'b': dir = BACKWARD; break;
13661 case 'w': p_ws = TRUE; break;
13662 case 'W': p_ws = FALSE; break;
13663 default: mask = 0;
13664 if (flagsp != NULL)
13665 switch (*flags)
13666 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013667 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013668 case 'e': mask = SP_END; break;
13669 case 'm': mask = SP_RETCOUNT; break;
13670 case 'n': mask = SP_NOMOVE; break;
13671 case 'p': mask = SP_SUBPAT; break;
13672 case 'r': mask = SP_REPEAT; break;
13673 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013674 }
13675 if (mask == 0)
13676 {
13677 EMSG2(_(e_invarg2), flags);
13678 dir = 0;
13679 }
13680 else
13681 *flagsp |= mask;
13682 }
13683 if (dir == 0)
13684 break;
13685 ++flags;
13686 }
13687 }
13688 return dir;
13689}
13690
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013692 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013694 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013695search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013696 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013697 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013698 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013700 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 char_u *pat;
13702 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013703 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 int save_p_ws = p_ws;
13705 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013706 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013707 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013708 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013709 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013712 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013713 if (dir == 0)
13714 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013715 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013716 if (flags & SP_START)
13717 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013718 if (flags & SP_END)
13719 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013720
13721 /* Optional extra argument: line number to stop searching. */
13722 if (argvars[1].v_type != VAR_UNKNOWN
13723 && argvars[2].v_type != VAR_UNKNOWN)
13724 {
13725 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13726 if (lnum_stop < 0)
13727 goto theend;
13728 }
13729
Bram Moolenaar231334e2005-07-25 20:46:57 +000013730 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013731 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013732 * Check to make sure only those flags are set.
13733 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13734 * flags cannot be set. Check for that condition also.
13735 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013736 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013737 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013740 goto theend;
13741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013743 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013744 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13745 options, RE_SEARCH, (linenr_T)lnum_stop);
13746 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013748 if (flags & SP_SUBPAT)
13749 retval = subpatnum;
13750 else
13751 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013752 if (flags & SP_SETPCMARK)
13753 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013755 if (match_pos != NULL)
13756 {
13757 /* Store the match cursor position */
13758 match_pos->lnum = pos.lnum;
13759 match_pos->col = pos.col + 1;
13760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 /* "/$" will put the cursor after the end of the line, may need to
13762 * correct that here */
13763 check_cursor();
13764 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013765
13766 /* If 'n' flag is used: restore cursor position. */
13767 if (flags & SP_NOMOVE)
13768 curwin->w_cursor = save_cursor;
13769theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013771
13772 return retval;
13773}
13774
13775/*
13776 * "search()" function
13777 */
13778 static void
13779f_search(argvars, rettv)
13780 typval_T *argvars;
13781 typval_T *rettv;
13782{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013783 int flags = 0;
13784
13785 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786}
13787
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013789 * "searchdecl()" function
13790 */
13791 static void
13792f_searchdecl(argvars, rettv)
13793 typval_T *argvars;
13794 typval_T *rettv;
13795{
13796 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013797 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013798 int error = FALSE;
13799 char_u *name;
13800
13801 rettv->vval.v_number = 1; /* default: FAIL */
13802
13803 name = get_tv_string_chk(&argvars[0]);
13804 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013805 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013806 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013807 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13808 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13809 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013810 if (!error && name != NULL)
13811 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013812 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013813}
13814
13815/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013816 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013818 static int
13819searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013821 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822{
13823 char_u *spat, *mpat, *epat;
13824 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 int dir;
13827 int flags = 0;
13828 char_u nbuf1[NUMBUFLEN];
13829 char_u nbuf2[NUMBUFLEN];
13830 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013831 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013832 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 spat = get_tv_string_chk(&argvars[0]);
13836 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13837 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13838 if (spat == NULL || mpat == NULL || epat == NULL)
13839 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 /* Handle the optional fourth argument: flags */
13842 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013843 if (dir == 0)
13844 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013845
13846 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013847 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13848 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013849 if ((flags & (SP_END | SP_SUBPAT)) != 0
13850 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013851 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013852 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013853 goto theend;
13854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013856 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013857 if (argvars[3].v_type == VAR_UNKNOWN
13858 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 skip = (char_u *)"";
13860 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013863 if (argvars[5].v_type != VAR_UNKNOWN)
13864 {
13865 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13866 if (lnum_stop < 0)
13867 goto theend;
13868 }
13869 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 if (skip == NULL)
13871 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013873 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13874 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013875
13876theend:
13877 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013878
13879 return retval;
13880}
13881
13882/*
13883 * "searchpair()" function
13884 */
13885 static void
13886f_searchpair(argvars, rettv)
13887 typval_T *argvars;
13888 typval_T *rettv;
13889{
13890 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13891}
13892
13893/*
13894 * "searchpairpos()" function
13895 */
13896 static void
13897f_searchpairpos(argvars, rettv)
13898 typval_T *argvars;
13899 typval_T *rettv;
13900{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013901 pos_T match_pos;
13902 int lnum = 0;
13903 int col = 0;
13904
13905 rettv->vval.v_number = 0;
13906
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013907 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013908 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013909
13910 if (searchpair_cmn(argvars, &match_pos) > 0)
13911 {
13912 lnum = match_pos.lnum;
13913 col = match_pos.col;
13914 }
13915
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013916 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13917 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013918}
13919
13920/*
13921 * Search for a start/middle/end thing.
13922 * Used by searchpair(), see its documentation for the details.
13923 * Returns 0 or -1 for no match,
13924 */
13925 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013926do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013927 char_u *spat; /* start pattern */
13928 char_u *mpat; /* middle pattern */
13929 char_u *epat; /* end pattern */
13930 int dir; /* BACKWARD or FORWARD */
13931 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013932 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013933 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013934 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013935{
13936 char_u *save_cpo;
13937 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13938 long retval = 0;
13939 pos_T pos;
13940 pos_T firstpos;
13941 pos_T foundpos;
13942 pos_T save_cursor;
13943 pos_T save_pos;
13944 int n;
13945 int r;
13946 int nest = 1;
13947 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013948 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013949
13950 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13951 save_cpo = p_cpo;
13952 p_cpo = (char_u *)"";
13953
13954 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13955 * start/middle/end (pat3, for the top pair). */
13956 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13957 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13958 if (pat2 == NULL || pat3 == NULL)
13959 goto theend;
13960 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13961 if (*mpat == NUL)
13962 STRCPY(pat3, pat2);
13963 else
13964 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13965 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013966 if (flags & SP_START)
13967 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013968
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 save_cursor = curwin->w_cursor;
13970 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013971 clearpos(&firstpos);
13972 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 pat = pat3;
13974 for (;;)
13975 {
13976 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013977 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13979 /* didn't find it or found the first match again: FAIL */
13980 break;
13981
13982 if (firstpos.lnum == 0)
13983 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013984 if (equalpos(pos, foundpos))
13985 {
13986 /* Found the same position again. Can happen with a pattern that
13987 * has "\zs" at the end and searching backwards. Advance one
13988 * character and try again. */
13989 if (dir == BACKWARD)
13990 decl(&pos);
13991 else
13992 incl(&pos);
13993 }
13994 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995
13996 /* If the skip pattern matches, ignore this match. */
13997 if (*skip != NUL)
13998 {
13999 save_pos = curwin->w_cursor;
14000 curwin->w_cursor = pos;
14001 r = eval_to_bool(skip, &err, NULL, FALSE);
14002 curwin->w_cursor = save_pos;
14003 if (err)
14004 {
14005 /* Evaluating {skip} caused an error, break here. */
14006 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014007 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 break;
14009 }
14010 if (r)
14011 continue;
14012 }
14013
14014 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14015 {
14016 /* Found end when searching backwards or start when searching
14017 * forward: nested pair. */
14018 ++nest;
14019 pat = pat2; /* nested, don't search for middle */
14020 }
14021 else
14022 {
14023 /* Found end when searching forward or start when searching
14024 * backward: end of (nested) pair; or found middle in outer pair. */
14025 if (--nest == 1)
14026 pat = pat3; /* outer level, search for middle */
14027 }
14028
14029 if (nest == 0)
14030 {
14031 /* Found the match: return matchcount or line number. */
14032 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014033 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014035 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014036 if (flags & SP_SETPCMARK)
14037 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 curwin->w_cursor = pos;
14039 if (!(flags & SP_REPEAT))
14040 break;
14041 nest = 1; /* search for next unmatched */
14042 }
14043 }
14044
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014045 if (match_pos != NULL)
14046 {
14047 /* Store the match cursor position */
14048 match_pos->lnum = curwin->w_cursor.lnum;
14049 match_pos->col = curwin->w_cursor.col + 1;
14050 }
14051
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014053 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 curwin->w_cursor = save_cursor;
14055
14056theend:
14057 vim_free(pat2);
14058 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014060
14061 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062}
14063
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014064/*
14065 * "searchpos()" function
14066 */
14067 static void
14068f_searchpos(argvars, rettv)
14069 typval_T *argvars;
14070 typval_T *rettv;
14071{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014072 pos_T match_pos;
14073 int lnum = 0;
14074 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014075 int n;
14076 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014077
14078 rettv->vval.v_number = 0;
14079
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014080 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014081 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014082
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014083 n = search_cmn(argvars, &match_pos, &flags);
14084 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014085 {
14086 lnum = match_pos.lnum;
14087 col = match_pos.col;
14088 }
14089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014090 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14091 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014092 if (flags & SP_SUBPAT)
14093 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014094}
14095
14096
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097/*ARGSUSED*/
14098 static void
14099f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014100 typval_T *argvars;
14101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103#ifdef FEAT_CLIENTSERVER
14104 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 char_u *server = get_tv_string_chk(&argvars[0]);
14106 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 if (server == NULL || reply == NULL)
14110 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111 if (check_restricted() || check_secure())
14112 return;
14113# ifdef FEAT_X11
14114 if (check_connection() == FAIL)
14115 return;
14116# endif
14117
14118 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014120 EMSG(_("E258: Unable to send to client"));
14121 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 rettv->vval.v_number = 0;
14124#else
14125 rettv->vval.v_number = -1;
14126#endif
14127}
14128
14129/*ARGSUSED*/
14130 static void
14131f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 typval_T *argvars;
14133 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014134{
14135 char_u *r = NULL;
14136
14137#ifdef FEAT_CLIENTSERVER
14138# ifdef WIN32
14139 r = serverGetVimNames();
14140# else
14141 make_connection();
14142 if (X_DISPLAY != NULL)
14143 r = serverGetVimNames(X_DISPLAY);
14144# endif
14145#endif
14146 rettv->v_type = VAR_STRING;
14147 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148}
14149
14150/*
14151 * "setbufvar()" function
14152 */
14153/*ARGSUSED*/
14154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *argvars;
14157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158{
14159 buf_T *buf;
14160#ifdef FEAT_AUTOCMD
14161 aco_save_T aco;
14162#else
14163 buf_T *save_curbuf;
14164#endif
14165 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 char_u nbuf[NUMBUFLEN];
14168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 rettv->vval.v_number = 0;
14170
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 if (check_restricted() || check_secure())
14172 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14174 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 varp = &argvars[2];
14177
14178 if (buf != NULL && varname != NULL && varp != NULL)
14179 {
14180 /* set curbuf to be our buf, temporarily */
14181#ifdef FEAT_AUTOCMD
14182 aucmd_prepbuf(&aco, buf);
14183#else
14184 save_curbuf = curbuf;
14185 curbuf = buf;
14186#endif
14187
14188 if (*varname == '&')
14189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 long numval;
14191 char_u *strval;
14192 int error = FALSE;
14193
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 numval = get_tv_number_chk(varp, &error);
14196 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 if (!error && strval != NULL)
14198 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 }
14200 else
14201 {
14202 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14203 if (bufvarname != NULL)
14204 {
14205 STRCPY(bufvarname, "b:");
14206 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014207 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 vim_free(bufvarname);
14209 }
14210 }
14211
14212 /* reset notion of buffer */
14213#ifdef FEAT_AUTOCMD
14214 aucmd_restbuf(&aco);
14215#else
14216 curbuf = save_curbuf;
14217#endif
14218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219}
14220
14221/*
14222 * "setcmdpos()" function
14223 */
14224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *argvars;
14227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 int pos = (int)get_tv_number(&argvars[0]) - 1;
14230
14231 if (pos >= 0)
14232 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233}
14234
14235/*
14236 * "setline()" function
14237 */
14238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *argvars;
14241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242{
14243 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014244 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014245 list_T *l = NULL;
14246 listitem_T *li = NULL;
14247 long added = 0;
14248 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014250 lnum = get_tv_lnum(&argvars[0]);
14251 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014253 l = argvars[1].vval.v_list;
14254 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014256 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014258
14259 rettv->vval.v_number = 0; /* OK */
14260 for (;;)
14261 {
14262 if (l != NULL)
14263 {
14264 /* list argument, get next string */
14265 if (li == NULL)
14266 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014268 li = li->li_next;
14269 }
14270
14271 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014273 break;
14274 if (lnum <= curbuf->b_ml.ml_line_count)
14275 {
14276 /* existing line, replace it */
14277 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14278 {
14279 changed_bytes(lnum, 0);
14280 check_cursor_col();
14281 rettv->vval.v_number = 0; /* OK */
14282 }
14283 }
14284 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14285 {
14286 /* lnum is one past the last line, append the line */
14287 ++added;
14288 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14289 rettv->vval.v_number = 0; /* OK */
14290 }
14291
14292 if (l == NULL) /* only one string argument */
14293 break;
14294 ++lnum;
14295 }
14296
14297 if (added > 0)
14298 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299}
14300
14301/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014302 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014303 */
14304/*ARGSUSED*/
14305 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014306set_qf_ll_list(wp, list_arg, action_arg, rettv)
14307 win_T *wp;
14308 typval_T *list_arg;
14309 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014310 typval_T *rettv;
14311{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014312#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014313 char_u *act;
14314 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014315#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014316
Bram Moolenaar2641f772005-03-25 21:58:17 +000014317 rettv->vval.v_number = -1;
14318
14319#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014320 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014321 EMSG(_(e_listreq));
14322 else
14323 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014324 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014325
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014326 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014327 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014328 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 if (act == NULL)
14330 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014331 if (*act == 'a' || *act == 'r')
14332 action = *act;
14333 }
14334
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014335 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014336 rettv->vval.v_number = 0;
14337 }
14338#endif
14339}
14340
14341/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014342 * "setloclist()" function
14343 */
14344/*ARGSUSED*/
14345 static void
14346f_setloclist(argvars, rettv)
14347 typval_T *argvars;
14348 typval_T *rettv;
14349{
14350 win_T *win;
14351
14352 rettv->vval.v_number = -1;
14353
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014354 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014355 if (win != NULL)
14356 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14357}
14358
14359/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014360 * "setpos()" function
14361 */
14362/*ARGSUSED*/
14363 static void
14364f_setpos(argvars, rettv)
14365 typval_T *argvars;
14366 typval_T *rettv;
14367{
14368 pos_T pos;
14369 int fnum;
14370 char_u *name;
14371
14372 name = get_tv_string_chk(argvars);
14373 if (name != NULL)
14374 {
14375 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14376 {
14377 --pos.col;
14378 if (name[0] == '.') /* cursor */
14379 {
14380 if (fnum == curbuf->b_fnum)
14381 {
14382 curwin->w_cursor = pos;
14383 check_cursor();
14384 }
14385 else
14386 EMSG(_(e_invarg));
14387 }
14388 else if (name[0] == '\'') /* mark */
14389 (void)setmark_pos(name[1], &pos, fnum);
14390 else
14391 EMSG(_(e_invarg));
14392 }
14393 }
14394}
14395
14396/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014397 * "setqflist()" function
14398 */
14399/*ARGSUSED*/
14400 static void
14401f_setqflist(argvars, rettv)
14402 typval_T *argvars;
14403 typval_T *rettv;
14404{
14405 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14406}
14407
14408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 * "setreg()" function
14410 */
14411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 typval_T *argvars;
14414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415{
14416 int regname;
14417 char_u *strregname;
14418 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 int append;
14421 char_u yank_type;
14422 long block_len;
14423
14424 block_len = -1;
14425 yank_type = MAUTO;
14426 append = FALSE;
14427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 if (strregname == NULL)
14432 return; /* type error; errmsg already given */
14433 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 if (regname == 0 || regname == '@')
14435 regname = '"';
14436 else if (regname == '=')
14437 return;
14438
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 stropt = get_tv_string_chk(&argvars[2]);
14442 if (stropt == NULL)
14443 return; /* type error */
14444 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 switch (*stropt)
14446 {
14447 case 'a': case 'A': /* append */
14448 append = TRUE;
14449 break;
14450 case 'v': case 'c': /* character-wise selection */
14451 yank_type = MCHAR;
14452 break;
14453 case 'V': case 'l': /* line-wise selection */
14454 yank_type = MLINE;
14455 break;
14456#ifdef FEAT_VISUAL
14457 case 'b': case Ctrl_V: /* block-wise selection */
14458 yank_type = MBLOCK;
14459 if (VIM_ISDIGIT(stropt[1]))
14460 {
14461 ++stropt;
14462 block_len = getdigits(&stropt) - 1;
14463 --stropt;
14464 }
14465 break;
14466#endif
14467 }
14468 }
14469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 strval = get_tv_string_chk(&argvars[1]);
14471 if (strval != NULL)
14472 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475}
14476
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014477/*
14478 * "settabwinvar()" function
14479 */
14480 static void
14481f_settabwinvar(argvars, rettv)
14482 typval_T *argvars;
14483 typval_T *rettv;
14484{
14485 setwinvar(argvars, rettv, 1);
14486}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487
14488/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014489 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T *argvars;
14494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014496 setwinvar(argvars, rettv, 0);
14497}
14498
14499/*
14500 * "setwinvar()" and "settabwinvar()" functions
14501 */
14502 static void
14503setwinvar(argvars, rettv, off)
14504 typval_T *argvars;
14505 typval_T *rettv;
14506 int off;
14507{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 win_T *win;
14509#ifdef FEAT_WINDOWS
14510 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014511 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512#endif
14513 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014514 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014516 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014518 rettv->vval.v_number = 0;
14519
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 if (check_restricted() || check_secure())
14521 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014522
14523#ifdef FEAT_WINDOWS
14524 if (off == 1)
14525 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14526 else
14527 tp = curtab;
14528#endif
14529 win = find_win_by_nr(&argvars[off], tp);
14530 varname = get_tv_string_chk(&argvars[off + 1]);
14531 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532
14533 if (win != NULL && varname != NULL && varp != NULL)
14534 {
14535#ifdef FEAT_WINDOWS
14536 /* set curwin to be our win, temporarily */
14537 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014538 save_curtab = curtab;
14539 goto_tabpage_tp(tp);
14540 if (!win_valid(win))
14541 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 curwin = win;
14543 curbuf = curwin->w_buffer;
14544#endif
14545
14546 if (*varname == '&')
14547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 long numval;
14549 char_u *strval;
14550 int error = FALSE;
14551
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553 numval = get_tv_number_chk(varp, &error);
14554 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 if (!error && strval != NULL)
14556 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 }
14558 else
14559 {
14560 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14561 if (winvarname != NULL)
14562 {
14563 STRCPY(winvarname, "w:");
14564 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014565 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 vim_free(winvarname);
14567 }
14568 }
14569
14570#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014571 /* Restore current tabpage and window, if still valid (autocomands can
14572 * make them invalid). */
14573 if (valid_tabpage(save_curtab))
14574 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 if (win_valid(save_curwin))
14576 {
14577 curwin = save_curwin;
14578 curbuf = curwin->w_buffer;
14579 }
14580#endif
14581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582}
14583
14584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014585 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 */
14587 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 typval_T *argvars;
14590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594 p = get_tv_string(&argvars[0]);
14595 rettv->vval.v_string = vim_strsave(p);
14596 simplify_filename(rettv->vval.v_string); /* simplify in place */
14597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598}
14599
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600static int
14601#ifdef __BORLANDC__
14602 _RTLENTRYF
14603#endif
14604 item_compare __ARGS((const void *s1, const void *s2));
14605static int
14606#ifdef __BORLANDC__
14607 _RTLENTRYF
14608#endif
14609 item_compare2 __ARGS((const void *s1, const void *s2));
14610
14611static int item_compare_ic;
14612static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614#define ITEM_COMPARE_FAIL 999
14615
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619 static int
14620#ifdef __BORLANDC__
14621_RTLENTRYF
14622#endif
14623item_compare(s1, s2)
14624 const void *s1;
14625 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014627 char_u *p1, *p2;
14628 char_u *tofree1, *tofree2;
14629 int res;
14630 char_u numbuf1[NUMBUFLEN];
14631 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014633 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14634 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 if (item_compare_ic)
14636 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 res = STRCMP(p1, p2);
14639 vim_free(tofree1);
14640 vim_free(tofree2);
14641 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642}
14643
14644 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645#ifdef __BORLANDC__
14646_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648item_compare2(s1, s2)
14649 const void *s1;
14650 const void *s2;
14651{
14652 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014653 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014654 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 /* shortcut after failure in previous call; compare all items equal */
14658 if (item_compare_func_err)
14659 return 0;
14660
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014661 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14662 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14664 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665
14666 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014667 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014668 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669 clear_tv(&argv[0]);
14670 clear_tv(&argv[1]);
14671
14672 if (res == FAIL)
14673 res = ITEM_COMPARE_FAIL;
14674 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 /* return value has wrong type */
14676 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14677 if (item_compare_func_err)
14678 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679 clear_tv(&rettv);
14680 return res;
14681}
14682
14683/*
14684 * "sort({list})" function
14685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T *argvars;
14689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690{
Bram Moolenaar33570922005-01-25 22:26:29 +000014691 list_T *l;
14692 listitem_T *li;
14693 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014694 long len;
14695 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696
Bram Moolenaar0d660222005-01-07 21:51:51 +000014697 rettv->vval.v_number = 0;
14698 if (argvars[0].v_type != VAR_LIST)
14699 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 else
14701 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014703 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704 return;
14705 rettv->vval.v_list = l;
14706 rettv->v_type = VAR_LIST;
14707 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709 len = list_len(l);
14710 if (len <= 1)
14711 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 item_compare_ic = FALSE;
14714 item_compare_func = NULL;
14715 if (argvars[1].v_type != VAR_UNKNOWN)
14716 {
14717 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719 else
14720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 int error = FALSE;
14722
14723 i = get_tv_number_chk(&argvars[1], &error);
14724 if (error)
14725 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 if (i == 1)
14727 item_compare_ic = TRUE;
14728 else
14729 item_compare_func = get_tv_string(&argvars[1]);
14730 }
14731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014734 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735 if (ptrs == NULL)
14736 return;
14737 i = 0;
14738 for (li = l->lv_first; li != NULL; li = li->li_next)
14739 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014741 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742 /* test the compare function */
14743 if (item_compare_func != NULL
14744 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14745 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014746 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748 {
14749 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 item_compare_func == NULL ? item_compare : item_compare2);
14752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014753 if (!item_compare_func_err)
14754 {
14755 /* Clear the List and append the items in the sorted order. */
14756 l->lv_first = l->lv_last = NULL;
14757 l->lv_len = 0;
14758 for (i = 0; i < len; ++i)
14759 list_append(l, ptrs[i]);
14760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014761 }
14762
14763 vim_free(ptrs);
14764 }
14765}
14766
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014767/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014768 * "soundfold({word})" function
14769 */
14770 static void
14771f_soundfold(argvars, rettv)
14772 typval_T *argvars;
14773 typval_T *rettv;
14774{
14775 char_u *s;
14776
14777 rettv->v_type = VAR_STRING;
14778 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014779#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014780 rettv->vval.v_string = eval_soundfold(s);
14781#else
14782 rettv->vval.v_string = vim_strsave(s);
14783#endif
14784}
14785
14786/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014787 * "spellbadword()" function
14788 */
14789/* ARGSUSED */
14790 static void
14791f_spellbadword(argvars, rettv)
14792 typval_T *argvars;
14793 typval_T *rettv;
14794{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014795 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014796 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014797 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014798
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014799 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014800 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014801
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014802#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014803 if (argvars[0].v_type == VAR_UNKNOWN)
14804 {
14805 /* Find the start and length of the badly spelled word. */
14806 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14807 if (len != 0)
14808 word = ml_get_cursor();
14809 }
14810 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14811 {
14812 char_u *str = get_tv_string_chk(&argvars[0]);
14813 int capcol = -1;
14814
14815 if (str != NULL)
14816 {
14817 /* Check the argument for spelling. */
14818 while (*str != NUL)
14819 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014820 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014821 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014822 {
14823 word = str;
14824 break;
14825 }
14826 str += len;
14827 }
14828 }
14829 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014830#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014831
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014832 list_append_string(rettv->vval.v_list, word, len);
14833 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014834 attr == HLF_SPB ? "bad" :
14835 attr == HLF_SPR ? "rare" :
14836 attr == HLF_SPL ? "local" :
14837 attr == HLF_SPC ? "caps" :
14838 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014839}
14840
14841/*
14842 * "spellsuggest()" function
14843 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014844/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014845 static void
14846f_spellsuggest(argvars, rettv)
14847 typval_T *argvars;
14848 typval_T *rettv;
14849{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014850#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014851 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014852 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014853 int maxcount;
14854 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014855 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014856 listitem_T *li;
14857 int need_capital = FALSE;
14858#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014859
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014860 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014861 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014862
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014863#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014864 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14865 {
14866 str = get_tv_string(&argvars[0]);
14867 if (argvars[1].v_type != VAR_UNKNOWN)
14868 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014869 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014870 if (maxcount <= 0)
14871 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014872 if (argvars[2].v_type != VAR_UNKNOWN)
14873 {
14874 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14875 if (typeerr)
14876 return;
14877 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014878 }
14879 else
14880 maxcount = 25;
14881
Bram Moolenaar4770d092006-01-12 23:22:24 +000014882 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014883
14884 for (i = 0; i < ga.ga_len; ++i)
14885 {
14886 str = ((char_u **)ga.ga_data)[i];
14887
14888 li = listitem_alloc();
14889 if (li == NULL)
14890 vim_free(str);
14891 else
14892 {
14893 li->li_tv.v_type = VAR_STRING;
14894 li->li_tv.v_lock = 0;
14895 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014896 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014897 }
14898 }
14899 ga_clear(&ga);
14900 }
14901#endif
14902}
14903
Bram Moolenaar0d660222005-01-07 21:51:51 +000014904 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014905f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014906 typval_T *argvars;
14907 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014908{
14909 char_u *str;
14910 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014911 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912 regmatch_T regmatch;
14913 char_u patbuf[NUMBUFLEN];
14914 char_u *save_cpo;
14915 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014916 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014917 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919
14920 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14921 save_cpo = p_cpo;
14922 p_cpo = (char_u *)"";
14923
14924 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014925 if (argvars[1].v_type != VAR_UNKNOWN)
14926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014927 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14928 if (pat == NULL)
14929 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014932 }
14933 if (pat == NULL || *pat == NUL)
14934 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014936 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 if (typeerr)
14939 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940
Bram Moolenaar0d660222005-01-07 21:51:51 +000014941 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14942 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014945 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014947 if (*str == NUL)
14948 match = FALSE; /* empty item at the end */
14949 else
14950 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951 if (match)
14952 end = regmatch.startp[0];
14953 else
14954 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014955 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14956 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014957 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014958 if (list_append_string(rettv->vval.v_list, str,
14959 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 }
14962 if (!match)
14963 break;
14964 /* Advance to just after the match. */
14965 if (regmatch.endp[0] > str)
14966 col = 0;
14967 else
14968 {
14969 /* Don't get stuck at the same match. */
14970#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014971 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972#else
14973 col = 1;
14974#endif
14975 }
14976 str = regmatch.endp[0];
14977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978
Bram Moolenaar0d660222005-01-07 21:51:51 +000014979 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983}
14984
Bram Moolenaar2c932302006-03-18 21:42:09 +000014985/*
14986 * "str2nr()" function
14987 */
14988 static void
14989f_str2nr(argvars, rettv)
14990 typval_T *argvars;
14991 typval_T *rettv;
14992{
14993 int base = 10;
14994 char_u *p;
14995 long n;
14996
14997 if (argvars[1].v_type != VAR_UNKNOWN)
14998 {
14999 base = get_tv_number(&argvars[1]);
15000 if (base != 8 && base != 10 && base != 16)
15001 {
15002 EMSG(_(e_invarg));
15003 return;
15004 }
15005 }
15006
15007 p = skipwhite(get_tv_string(&argvars[0]));
15008 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15009 rettv->vval.v_number = n;
15010}
15011
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012#ifdef HAVE_STRFTIME
15013/*
15014 * "strftime({format}[, {time}])" function
15015 */
15016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015018 typval_T *argvars;
15019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020{
15021 char_u result_buf[256];
15022 struct tm *curtime;
15023 time_t seconds;
15024 char_u *p;
15025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015029 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 seconds = time(NULL);
15031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 curtime = localtime(&seconds);
15034 /* MSVC returns NULL for an invalid value of seconds. */
15035 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015036 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 else
15038 {
15039# ifdef FEAT_MBYTE
15040 vimconv_T conv;
15041 char_u *enc;
15042
15043 conv.vc_type = CONV_NONE;
15044 enc = enc_locale();
15045 convert_setup(&conv, p_enc, enc);
15046 if (conv.vc_type != CONV_NONE)
15047 p = string_convert(&conv, p, NULL);
15048# endif
15049 if (p != NULL)
15050 (void)strftime((char *)result_buf, sizeof(result_buf),
15051 (char *)p, curtime);
15052 else
15053 result_buf[0] = NUL;
15054
15055# ifdef FEAT_MBYTE
15056 if (conv.vc_type != CONV_NONE)
15057 vim_free(p);
15058 convert_setup(&conv, enc, p_enc);
15059 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 else
15062# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064
15065# ifdef FEAT_MBYTE
15066 /* Release conversion descriptors */
15067 convert_setup(&conv, NULL, NULL);
15068 vim_free(enc);
15069# endif
15070 }
15071}
15072#endif
15073
15074/*
15075 * "stridx()" function
15076 */
15077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 typval_T *argvars;
15080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081{
15082 char_u buf[NUMBUFLEN];
15083 char_u *needle;
15084 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 needle = get_tv_string_chk(&argvars[1]);
15090 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 if (needle == NULL || haystack == NULL)
15093 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 if (argvars[2].v_type != VAR_UNKNOWN)
15096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 int error = FALSE;
15098
15099 start_idx = get_tv_number_chk(&argvars[2], &error);
15100 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015102 if (start_idx >= 0)
15103 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 }
15105
15106 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15107 if (pos != NULL)
15108 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109}
15110
15111/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015112 * "string()" function
15113 */
15114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015115f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015116 typval_T *argvars;
15117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015118{
15119 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015120 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015122 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015123 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015124 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126}
15127
15128/*
15129 * "strlen()" function
15130 */
15131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015133 typval_T *argvars;
15134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136 rettv->vval.v_number = (varnumber_T)(STRLEN(
15137 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138}
15139
15140/*
15141 * "strpart()" function
15142 */
15143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *argvars;
15146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147{
15148 char_u *p;
15149 int n;
15150 int len;
15151 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015154 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 slen = (int)STRLEN(p);
15156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015157 n = get_tv_number_chk(&argvars[1], &error);
15158 if (error)
15159 len = 0;
15160 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 else
15163 len = slen - n; /* default len: all bytes that are available. */
15164
15165 /*
15166 * Only return the overlap between the specified part and the actual
15167 * string.
15168 */
15169 if (n < 0)
15170 {
15171 len += n;
15172 n = 0;
15173 }
15174 else if (n > slen)
15175 n = slen;
15176 if (len < 0)
15177 len = 0;
15178 else if (n + len > slen)
15179 len = slen - n;
15180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015181 rettv->v_type = VAR_STRING;
15182 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183}
15184
15185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186 * "strridx()" function
15187 */
15188 static void
15189f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 typval_T *argvars;
15191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192{
15193 char_u buf[NUMBUFLEN];
15194 char_u *needle;
15195 char_u *haystack;
15196 char_u *rest;
15197 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015198 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015200 needle = get_tv_string_chk(&argvars[1]);
15201 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015202
15203 rettv->vval.v_number = -1;
15204 if (needle == NULL || haystack == NULL)
15205 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015206
15207 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015208 if (argvars[2].v_type != VAR_UNKNOWN)
15209 {
15210 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015212 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015214 }
15215 else
15216 end_idx = haystack_len;
15217
Bram Moolenaar0d660222005-01-07 21:51:51 +000015218 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015219 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015220 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015221 lastmatch = haystack + end_idx;
15222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015224 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015225 for (rest = haystack; *rest != '\0'; ++rest)
15226 {
15227 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015228 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229 break;
15230 lastmatch = rest;
15231 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015232 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233
15234 if (lastmatch == NULL)
15235 rettv->vval.v_number = -1;
15236 else
15237 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15238}
15239
15240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 * "strtrans()" function
15242 */
15243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015244f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015245 typval_T *argvars;
15246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015248 rettv->v_type = VAR_STRING;
15249 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250}
15251
15252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253 * "submatch()" function
15254 */
15255 static void
15256f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *argvars;
15258 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259{
15260 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 rettv->vval.v_string =
15262 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263}
15264
15265/*
15266 * "substitute()" function
15267 */
15268 static void
15269f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 typval_T *argvars;
15271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272{
15273 char_u patbuf[NUMBUFLEN];
15274 char_u subbuf[NUMBUFLEN];
15275 char_u flagsbuf[NUMBUFLEN];
15276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015277 char_u *str = get_tv_string_chk(&argvars[0]);
15278 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15279 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15280 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15281
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015283 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15284 rettv->vval.v_string = NULL;
15285 else
15286 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287}
15288
15289/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015290 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 */
15292/*ARGSUSED*/
15293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 typval_T *argvars;
15296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297{
15298 int id = 0;
15299#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015300 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 long col;
15302 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015303 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 lnum = get_tv_lnum(argvars); /* -1 on type error */
15306 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15307 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015310 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015311 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312#endif
15313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015314 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315}
15316
15317/*
15318 * "synIDattr(id, what [, mode])" function
15319 */
15320/*ARGSUSED*/
15321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015322f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 typval_T *argvars;
15324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325{
15326 char_u *p = NULL;
15327#ifdef FEAT_SYN_HL
15328 int id;
15329 char_u *what;
15330 char_u *mode;
15331 char_u modebuf[NUMBUFLEN];
15332 int modec;
15333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015334 id = get_tv_number(&argvars[0]);
15335 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015336 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 modec = TOLOWER_ASC(mode[0]);
15340 if (modec != 't' && modec != 'c'
15341#ifdef FEAT_GUI
15342 && modec != 'g'
15343#endif
15344 )
15345 modec = 0; /* replace invalid with current */
15346 }
15347 else
15348 {
15349#ifdef FEAT_GUI
15350 if (gui.in_use)
15351 modec = 'g';
15352 else
15353#endif
15354 if (t_colors > 1)
15355 modec = 'c';
15356 else
15357 modec = 't';
15358 }
15359
15360
15361 switch (TOLOWER_ASC(what[0]))
15362 {
15363 case 'b':
15364 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15365 p = highlight_color(id, what, modec);
15366 else /* bold */
15367 p = highlight_has_attr(id, HL_BOLD, modec);
15368 break;
15369
15370 case 'f': /* fg[#] */
15371 p = highlight_color(id, what, modec);
15372 break;
15373
15374 case 'i':
15375 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15376 p = highlight_has_attr(id, HL_INVERSE, modec);
15377 else /* italic */
15378 p = highlight_has_attr(id, HL_ITALIC, modec);
15379 break;
15380
15381 case 'n': /* name */
15382 p = get_highlight_name(NULL, id - 1);
15383 break;
15384
15385 case 'r': /* reverse */
15386 p = highlight_has_attr(id, HL_INVERSE, modec);
15387 break;
15388
15389 case 's': /* standout */
15390 p = highlight_has_attr(id, HL_STANDOUT, modec);
15391 break;
15392
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015393 case 'u':
15394 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15395 /* underline */
15396 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15397 else
15398 /* undercurl */
15399 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 break;
15401 }
15402
15403 if (p != NULL)
15404 p = vim_strsave(p);
15405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->v_type = VAR_STRING;
15407 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408}
15409
15410/*
15411 * "synIDtrans(id)" function
15412 */
15413/*ARGSUSED*/
15414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015416 typval_T *argvars;
15417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418{
15419 int id;
15420
15421#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015422 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423
15424 if (id > 0)
15425 id = syn_get_final_id(id);
15426 else
15427#endif
15428 id = 0;
15429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015430 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431}
15432
15433/*
15434 * "system()" function
15435 */
15436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015438 typval_T *argvars;
15439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015441 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015443 char_u *infile = NULL;
15444 char_u buf[NUMBUFLEN];
15445 int err = FALSE;
15446 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015448 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015449 {
15450 /*
15451 * Write the string to a temp file, to be used for input of the shell
15452 * command.
15453 */
15454 if ((infile = vim_tempname('i')) == NULL)
15455 {
15456 EMSG(_(e_notmp));
15457 return;
15458 }
15459
15460 fd = mch_fopen((char *)infile, WRITEBIN);
15461 if (fd == NULL)
15462 {
15463 EMSG2(_(e_notopen), infile);
15464 goto done;
15465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015466 p = get_tv_string_buf_chk(&argvars[1], buf);
15467 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015468 {
15469 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015471 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015472 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15473 err = TRUE;
15474 if (fclose(fd) != 0)
15475 err = TRUE;
15476 if (err)
15477 {
15478 EMSG(_("E677: Error writing temp file"));
15479 goto done;
15480 }
15481 }
15482
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015483 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15484 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015485
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486#ifdef USE_CR
15487 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015488 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 {
15490 char_u *s;
15491
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015492 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 {
15494 if (*s == CAR)
15495 *s = NL;
15496 }
15497 }
15498#else
15499# ifdef USE_CRNL
15500 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015501 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 {
15503 char_u *s, *d;
15504
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015505 d = res;
15506 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 {
15508 if (s[0] == CAR && s[1] == NL)
15509 ++s;
15510 *d++ = *s;
15511 }
15512 *d = NUL;
15513 }
15514# endif
15515#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015516
15517done:
15518 if (infile != NULL)
15519 {
15520 mch_remove(infile);
15521 vim_free(infile);
15522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015523 rettv->v_type = VAR_STRING;
15524 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525}
15526
15527/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015528 * "tabpagebuflist()" function
15529 */
15530/* ARGSUSED */
15531 static void
15532f_tabpagebuflist(argvars, rettv)
15533 typval_T *argvars;
15534 typval_T *rettv;
15535{
15536#ifndef FEAT_WINDOWS
15537 rettv->vval.v_number = 0;
15538#else
15539 tabpage_T *tp;
15540 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015541
15542 if (argvars[0].v_type == VAR_UNKNOWN)
15543 wp = firstwin;
15544 else
15545 {
15546 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15547 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015548 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015549 }
15550 if (wp == NULL)
15551 rettv->vval.v_number = 0;
15552 else
15553 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015554 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015555 rettv->vval.v_number = 0;
15556 else
15557 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015558 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015559 if (list_append_number(rettv->vval.v_list,
15560 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015561 break;
15562 }
15563 }
15564#endif
15565}
15566
15567
15568/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015569 * "tabpagenr()" function
15570 */
15571/* ARGSUSED */
15572 static void
15573f_tabpagenr(argvars, rettv)
15574 typval_T *argvars;
15575 typval_T *rettv;
15576{
15577 int nr = 1;
15578#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015579 char_u *arg;
15580
15581 if (argvars[0].v_type != VAR_UNKNOWN)
15582 {
15583 arg = get_tv_string_chk(&argvars[0]);
15584 nr = 0;
15585 if (arg != NULL)
15586 {
15587 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015588 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015589 else
15590 EMSG2(_(e_invexpr2), arg);
15591 }
15592 }
15593 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015594 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015595#endif
15596 rettv->vval.v_number = nr;
15597}
15598
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015599
15600#ifdef FEAT_WINDOWS
15601static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15602
15603/*
15604 * Common code for tabpagewinnr() and winnr().
15605 */
15606 static int
15607get_winnr(tp, argvar)
15608 tabpage_T *tp;
15609 typval_T *argvar;
15610{
15611 win_T *twin;
15612 int nr = 1;
15613 win_T *wp;
15614 char_u *arg;
15615
15616 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15617 if (argvar->v_type != VAR_UNKNOWN)
15618 {
15619 arg = get_tv_string_chk(argvar);
15620 if (arg == NULL)
15621 nr = 0; /* type error; errmsg already given */
15622 else if (STRCMP(arg, "$") == 0)
15623 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15624 else if (STRCMP(arg, "#") == 0)
15625 {
15626 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15627 if (twin == NULL)
15628 nr = 0;
15629 }
15630 else
15631 {
15632 EMSG2(_(e_invexpr2), arg);
15633 nr = 0;
15634 }
15635 }
15636
15637 if (nr > 0)
15638 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15639 wp != twin; wp = wp->w_next)
15640 ++nr;
15641 return nr;
15642}
15643#endif
15644
15645/*
15646 * "tabpagewinnr()" function
15647 */
15648/* ARGSUSED */
15649 static void
15650f_tabpagewinnr(argvars, rettv)
15651 typval_T *argvars;
15652 typval_T *rettv;
15653{
15654 int nr = 1;
15655#ifdef FEAT_WINDOWS
15656 tabpage_T *tp;
15657
15658 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15659 if (tp == NULL)
15660 nr = 0;
15661 else
15662 nr = get_winnr(tp, &argvars[1]);
15663#endif
15664 rettv->vval.v_number = nr;
15665}
15666
15667
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015668/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015669 * "tagfiles()" function
15670 */
15671/*ARGSUSED*/
15672 static void
15673f_tagfiles(argvars, rettv)
15674 typval_T *argvars;
15675 typval_T *rettv;
15676{
15677 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015678 tagname_T tn;
15679 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015681 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015682 {
15683 rettv->vval.v_number = 0;
15684 return;
15685 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015687 for (first = TRUE; ; first = FALSE)
15688 if (get_tagfname(&tn, first, fname) == FAIL
15689 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015690 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015691 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015692}
15693
15694/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015695 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015696 */
15697 static void
15698f_taglist(argvars, rettv)
15699 typval_T *argvars;
15700 typval_T *rettv;
15701{
15702 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015703
15704 tag_pattern = get_tv_string(&argvars[0]);
15705
15706 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 if (*tag_pattern == NUL)
15708 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 if (rettv_list_alloc(rettv) == OK)
15711 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015712}
15713
15714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 * "tempname()" function
15716 */
15717/*ARGSUSED*/
15718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 typval_T *argvars;
15721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722{
15723 static int x = 'A';
15724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015725 rettv->v_type = VAR_STRING;
15726 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727
15728 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15729 * names. Skip 'I' and 'O', they are used for shell redirection. */
15730 do
15731 {
15732 if (x == 'Z')
15733 x = '0';
15734 else if (x == '9')
15735 x = 'A';
15736 else
15737 {
15738#ifdef EBCDIC
15739 if (x == 'I')
15740 x = 'J';
15741 else if (x == 'R')
15742 x = 'S';
15743 else
15744#endif
15745 ++x;
15746 }
15747 } while (x == 'I' || x == 'O');
15748}
15749
15750/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015751 * "test(list)" function: Just checking the walls...
15752 */
15753/*ARGSUSED*/
15754 static void
15755f_test(argvars, rettv)
15756 typval_T *argvars;
15757 typval_T *rettv;
15758{
15759 /* Used for unit testing. Change the code below to your liking. */
15760#if 0
15761 listitem_T *li;
15762 list_T *l;
15763 char_u *bad, *good;
15764
15765 if (argvars[0].v_type != VAR_LIST)
15766 return;
15767 l = argvars[0].vval.v_list;
15768 if (l == NULL)
15769 return;
15770 li = l->lv_first;
15771 if (li == NULL)
15772 return;
15773 bad = get_tv_string(&li->li_tv);
15774 li = li->li_next;
15775 if (li == NULL)
15776 return;
15777 good = get_tv_string(&li->li_tv);
15778 rettv->vval.v_number = test_edit_score(bad, good);
15779#endif
15780}
15781
15782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 * "tolower(string)" function
15784 */
15785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015787 typval_T *argvars;
15788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789{
15790 char_u *p;
15791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792 p = vim_strsave(get_tv_string(&argvars[0]));
15793 rettv->v_type = VAR_STRING;
15794 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795
15796 if (p != NULL)
15797 while (*p != NUL)
15798 {
15799#ifdef FEAT_MBYTE
15800 int l;
15801
15802 if (enc_utf8)
15803 {
15804 int c, lc;
15805
15806 c = utf_ptr2char(p);
15807 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015808 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 /* TODO: reallocate string when byte count changes. */
15810 if (utf_char2len(lc) == l)
15811 utf_char2bytes(lc, p);
15812 p += l;
15813 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015814 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 p += l; /* skip multi-byte character */
15816 else
15817#endif
15818 {
15819 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15820 ++p;
15821 }
15822 }
15823}
15824
15825/*
15826 * "toupper(string)" function
15827 */
15828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015829f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T *argvars;
15831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015833 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015834 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835}
15836
15837/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015838 * "tr(string, fromstr, tostr)" function
15839 */
15840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 typval_T *argvars;
15843 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015844{
15845 char_u *instr;
15846 char_u *fromstr;
15847 char_u *tostr;
15848 char_u *p;
15849#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015850 int inlen;
15851 int fromlen;
15852 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015853 int idx;
15854 char_u *cpstr;
15855 int cplen;
15856 int first = TRUE;
15857#endif
15858 char_u buf[NUMBUFLEN];
15859 char_u buf2[NUMBUFLEN];
15860 garray_T ga;
15861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015862 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15864 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015865
15866 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015867 rettv->v_type = VAR_STRING;
15868 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015869 if (fromstr == NULL || tostr == NULL)
15870 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015871 ga_init2(&ga, (int)sizeof(char), 80);
15872
15873#ifdef FEAT_MBYTE
15874 if (!has_mbyte)
15875#endif
15876 /* not multi-byte: fromstr and tostr must be the same length */
15877 if (STRLEN(fromstr) != STRLEN(tostr))
15878 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015879#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015880error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015881#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015882 EMSG2(_(e_invarg2), fromstr);
15883 ga_clear(&ga);
15884 return;
15885 }
15886
15887 /* fromstr and tostr have to contain the same number of chars */
15888 while (*instr != NUL)
15889 {
15890#ifdef FEAT_MBYTE
15891 if (has_mbyte)
15892 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015893 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015894 cpstr = instr;
15895 cplen = inlen;
15896 idx = 0;
15897 for (p = fromstr; *p != NUL; p += fromlen)
15898 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015899 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015900 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15901 {
15902 for (p = tostr; *p != NUL; p += tolen)
15903 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015904 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015905 if (idx-- == 0)
15906 {
15907 cplen = tolen;
15908 cpstr = p;
15909 break;
15910 }
15911 }
15912 if (*p == NUL) /* tostr is shorter than fromstr */
15913 goto error;
15914 break;
15915 }
15916 ++idx;
15917 }
15918
15919 if (first && cpstr == instr)
15920 {
15921 /* Check that fromstr and tostr have the same number of
15922 * (multi-byte) characters. Done only once when a character
15923 * of instr doesn't appear in fromstr. */
15924 first = FALSE;
15925 for (p = tostr; *p != NUL; p += tolen)
15926 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015927 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015928 --idx;
15929 }
15930 if (idx != 0)
15931 goto error;
15932 }
15933
15934 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015935 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015936 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015937
15938 instr += inlen;
15939 }
15940 else
15941#endif
15942 {
15943 /* When not using multi-byte chars we can do it faster. */
15944 p = vim_strchr(fromstr, *instr);
15945 if (p != NULL)
15946 ga_append(&ga, tostr[p - fromstr]);
15947 else
15948 ga_append(&ga, *instr);
15949 ++instr;
15950 }
15951 }
15952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015953 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015954}
15955
15956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 * "type(expr)" function
15958 */
15959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015960f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 typval_T *argvars;
15962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015964 int n;
15965
15966 switch (argvars[0].v_type)
15967 {
15968 case VAR_NUMBER: n = 0; break;
15969 case VAR_STRING: n = 1; break;
15970 case VAR_FUNC: n = 2; break;
15971 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015972 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015973 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15974 }
15975 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976}
15977
15978/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015979 * "values(dict)" function
15980 */
15981 static void
15982f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015983 typval_T *argvars;
15984 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015985{
15986 dict_list(argvars, rettv, 1);
15987}
15988
15989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 * "virtcol(string)" function
15991 */
15992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015993f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015994 typval_T *argvars;
15995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996{
15997 colnr_T vcol = 0;
15998 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015999 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016001 fp = var2fpos(&argvars[0], FALSE, &fnum);
16002 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16003 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 {
16005 getvvcol(curwin, fp, NULL, NULL, &vcol);
16006 ++vcol;
16007 }
16008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010}
16011
16012/*
16013 * "visualmode()" function
16014 */
16015/*ARGSUSED*/
16016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 typval_T *argvars;
16019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020{
16021#ifdef FEAT_VISUAL
16022 char_u str[2];
16023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016024 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 str[0] = curbuf->b_visual_mode_eval;
16026 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016027 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028
16029 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016030 if ((argvars[0].v_type == VAR_NUMBER
16031 && argvars[0].vval.v_number != 0)
16032 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016033 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 curbuf->b_visual_mode_eval = NUL;
16035#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016036 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037#endif
16038}
16039
16040/*
16041 * "winbufnr(nr)" function
16042 */
16043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 typval_T *argvars;
16046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047{
16048 win_T *wp;
16049
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016050 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016052 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055}
16056
16057/*
16058 * "wincol()" function
16059 */
16060/*ARGSUSED*/
16061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016062f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 typval_T *argvars;
16064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065{
16066 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016067 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068}
16069
16070/*
16071 * "winheight(nr)" function
16072 */
16073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016075 typval_T *argvars;
16076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077{
16078 win_T *wp;
16079
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016080 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085}
16086
16087/*
16088 * "winline()" function
16089 */
16090/*ARGSUSED*/
16091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016093 typval_T *argvars;
16094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095{
16096 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098}
16099
16100/*
16101 * "winnr()" function
16102 */
16103/* ARGSUSED */
16104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016105f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016106 typval_T *argvars;
16107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108{
16109 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016110
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016112 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016114 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115}
16116
16117/*
16118 * "winrestcmd()" function
16119 */
16120/* ARGSUSED */
16121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016122f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016123 typval_T *argvars;
16124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125{
16126#ifdef FEAT_WINDOWS
16127 win_T *wp;
16128 int winnr = 1;
16129 garray_T ga;
16130 char_u buf[50];
16131
16132 ga_init2(&ga, (int)sizeof(char), 70);
16133 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16134 {
16135 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16136 ga_concat(&ga, buf);
16137# ifdef FEAT_VERTSPLIT
16138 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16139 ga_concat(&ga, buf);
16140# endif
16141 ++winnr;
16142 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016143 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016149 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150}
16151
16152/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016153 * "winrestview()" function
16154 */
16155/* ARGSUSED */
16156 static void
16157f_winrestview(argvars, rettv)
16158 typval_T *argvars;
16159 typval_T *rettv;
16160{
16161 dict_T *dict;
16162
16163 if (argvars[0].v_type != VAR_DICT
16164 || (dict = argvars[0].vval.v_dict) == NULL)
16165 EMSG(_(e_invarg));
16166 else
16167 {
16168 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16169 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16170#ifdef FEAT_VIRTUALEDIT
16171 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16172#endif
16173 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016174 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016175
16176 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16177#ifdef FEAT_DIFF
16178 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16179#endif
16180 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16181 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16182
16183 check_cursor();
16184 changed_cline_bef_curs();
16185 invalidate_botline();
16186 redraw_later(VALID);
16187
16188 if (curwin->w_topline == 0)
16189 curwin->w_topline = 1;
16190 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16191 curwin->w_topline = curbuf->b_ml.ml_line_count;
16192#ifdef FEAT_DIFF
16193 check_topfill(curwin, TRUE);
16194#endif
16195 }
16196}
16197
16198/*
16199 * "winsaveview()" function
16200 */
16201/* ARGSUSED */
16202 static void
16203f_winsaveview(argvars, rettv)
16204 typval_T *argvars;
16205 typval_T *rettv;
16206{
16207 dict_T *dict;
16208
16209 dict = dict_alloc();
16210 if (dict == NULL)
16211 return;
16212 rettv->v_type = VAR_DICT;
16213 rettv->vval.v_dict = dict;
16214 ++dict->dv_refcount;
16215
16216 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16217 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16218#ifdef FEAT_VIRTUALEDIT
16219 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16220#endif
16221 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16222
16223 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16224#ifdef FEAT_DIFF
16225 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16226#endif
16227 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16228 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16229}
16230
16231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 * "winwidth(nr)" function
16233 */
16234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016236 typval_T *argvars;
16237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238{
16239 win_T *wp;
16240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016241 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 else
16245#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249#endif
16250}
16251
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016253 * "writefile()" function
16254 */
16255 static void
16256f_writefile(argvars, rettv)
16257 typval_T *argvars;
16258 typval_T *rettv;
16259{
16260 int binary = FALSE;
16261 char_u *fname;
16262 FILE *fd;
16263 listitem_T *li;
16264 char_u *s;
16265 int ret = 0;
16266 int c;
16267
16268 if (argvars[0].v_type != VAR_LIST)
16269 {
16270 EMSG2(_(e_listarg), "writefile()");
16271 return;
16272 }
16273 if (argvars[0].vval.v_list == NULL)
16274 return;
16275
16276 if (argvars[2].v_type != VAR_UNKNOWN
16277 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16278 binary = TRUE;
16279
16280 /* Always open the file in binary mode, library functions have a mind of
16281 * their own about CR-LF conversion. */
16282 fname = get_tv_string(&argvars[1]);
16283 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16284 {
16285 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16286 ret = -1;
16287 }
16288 else
16289 {
16290 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16291 li = li->li_next)
16292 {
16293 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16294 {
16295 if (*s == '\n')
16296 c = putc(NUL, fd);
16297 else
16298 c = putc(*s, fd);
16299 if (c == EOF)
16300 {
16301 ret = -1;
16302 break;
16303 }
16304 }
16305 if (!binary || li->li_next != NULL)
16306 if (putc('\n', fd) == EOF)
16307 {
16308 ret = -1;
16309 break;
16310 }
16311 if (ret < 0)
16312 {
16313 EMSG(_(e_write));
16314 break;
16315 }
16316 }
16317 fclose(fd);
16318 }
16319
16320 rettv->vval.v_number = ret;
16321}
16322
16323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016325 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 */
16327 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016328var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016331 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016333 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016335 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaara5525202006-03-02 22:52:09 +000016337 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016338 if (varp->v_type == VAR_LIST)
16339 {
16340 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016341 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016342 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016343
16344 l = varp->vval.v_list;
16345 if (l == NULL)
16346 return NULL;
16347
16348 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016349 pos.lnum = list_find_nr(l, 0L, &error);
16350 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016351 return NULL; /* invalid line number */
16352
16353 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016354 pos.col = list_find_nr(l, 1L, &error);
16355 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016356 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016357 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016358 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016359 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016360 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016361 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016362
Bram Moolenaara5525202006-03-02 22:52:09 +000016363#ifdef FEAT_VIRTUALEDIT
16364 /* Get the virtual offset. Defaults to zero. */
16365 pos.coladd = list_find_nr(l, 2L, &error);
16366 if (error)
16367 pos.coladd = 0;
16368#endif
16369
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016370 return &pos;
16371 }
16372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016373 name = get_tv_string_chk(varp);
16374 if (name == NULL)
16375 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 if (name[0] == '.') /* cursor */
16377 return &curwin->w_cursor;
16378 if (name[0] == '\'') /* mark */
16379 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016380 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16382 return NULL;
16383 return pp;
16384 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016385
16386#ifdef FEAT_VIRTUALEDIT
16387 pos.coladd = 0;
16388#endif
16389
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016390 if (name[0] == 'w' && lnum)
16391 {
16392 pos.col = 0;
16393 if (name[1] == '0') /* "w0": first visible line */
16394 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016395 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016396 pos.lnum = curwin->w_topline;
16397 return &pos;
16398 }
16399 else if (name[1] == '$') /* "w$": last visible line */
16400 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016401 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016402 pos.lnum = curwin->w_botline - 1;
16403 return &pos;
16404 }
16405 }
16406 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 {
16408 if (lnum)
16409 {
16410 pos.lnum = curbuf->b_ml.ml_line_count;
16411 pos.col = 0;
16412 }
16413 else
16414 {
16415 pos.lnum = curwin->w_cursor.lnum;
16416 pos.col = (colnr_T)STRLEN(ml_get_curline());
16417 }
16418 return &pos;
16419 }
16420 return NULL;
16421}
16422
16423/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016424 * Convert list in "arg" into a position and optional file number.
16425 * When "fnump" is NULL there is no file number, only 3 items.
16426 * Note that the column is passed on as-is, the caller may want to decrement
16427 * it to use 1 for the first column.
16428 * Return FAIL when conversion is not possible, doesn't check the position for
16429 * validity.
16430 */
16431 static int
16432list2fpos(arg, posp, fnump)
16433 typval_T *arg;
16434 pos_T *posp;
16435 int *fnump;
16436{
16437 list_T *l = arg->vval.v_list;
16438 long i = 0;
16439 long n;
16440
16441 /* List must be: [fnum, lnum, col, coladd] */
16442 if (arg->v_type != VAR_LIST || l == NULL
16443 || l->lv_len != (fnump == NULL ? 3 : 4))
16444 return FAIL;
16445
16446 if (fnump != NULL)
16447 {
16448 n = list_find_nr(l, i++, NULL); /* fnum */
16449 if (n < 0)
16450 return FAIL;
16451 if (n == 0)
16452 n = curbuf->b_fnum; /* current buffer */
16453 *fnump = n;
16454 }
16455
16456 n = list_find_nr(l, i++, NULL); /* lnum */
16457 if (n < 0)
16458 return FAIL;
16459 posp->lnum = n;
16460
16461 n = list_find_nr(l, i++, NULL); /* col */
16462 if (n < 0)
16463 return FAIL;
16464 posp->col = n;
16465
16466#ifdef FEAT_VIRTUALEDIT
16467 n = list_find_nr(l, i, NULL);
16468 if (n < 0)
16469 return FAIL;
16470 posp->coladd = n;
16471#endif
16472
16473 return OK;
16474}
16475
16476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 * Get the length of an environment variable name.
16478 * Advance "arg" to the first character after the name.
16479 * Return 0 for error.
16480 */
16481 static int
16482get_env_len(arg)
16483 char_u **arg;
16484{
16485 char_u *p;
16486 int len;
16487
16488 for (p = *arg; vim_isIDc(*p); ++p)
16489 ;
16490 if (p == *arg) /* no name found */
16491 return 0;
16492
16493 len = (int)(p - *arg);
16494 *arg = p;
16495 return len;
16496}
16497
16498/*
16499 * Get the length of the name of a function or internal variable.
16500 * "arg" is advanced to the first non-white character after the name.
16501 * Return 0 if something is wrong.
16502 */
16503 static int
16504get_id_len(arg)
16505 char_u **arg;
16506{
16507 char_u *p;
16508 int len;
16509
16510 /* Find the end of the name. */
16511 for (p = *arg; eval_isnamec(*p); ++p)
16512 ;
16513 if (p == *arg) /* no name found */
16514 return 0;
16515
16516 len = (int)(p - *arg);
16517 *arg = skipwhite(p);
16518
16519 return len;
16520}
16521
16522/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016523 * Get the length of the name of a variable or function.
16524 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016526 * Return -1 if curly braces expansion failed.
16527 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 * If the name contains 'magic' {}'s, expand them and return the
16529 * expanded name in an allocated string via 'alias' - caller must free.
16530 */
16531 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016532get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 char_u **arg;
16534 char_u **alias;
16535 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016536 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537{
16538 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 char_u *p;
16540 char_u *expr_start;
16541 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542
16543 *alias = NULL; /* default to no alias */
16544
16545 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16546 && (*arg)[2] == (int)KE_SNR)
16547 {
16548 /* hard coded <SNR>, already translated */
16549 *arg += 3;
16550 return get_id_len(arg) + 3;
16551 }
16552 len = eval_fname_script(*arg);
16553 if (len > 0)
16554 {
16555 /* literal "<SID>", "s:" or "<SNR>" */
16556 *arg += len;
16557 }
16558
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016562 p = find_name_end(*arg, &expr_start, &expr_end,
16563 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 if (expr_start != NULL)
16565 {
16566 char_u *temp_string;
16567
16568 if (!evaluate)
16569 {
16570 len += (int)(p - *arg);
16571 *arg = skipwhite(p);
16572 return len;
16573 }
16574
16575 /*
16576 * Include any <SID> etc in the expanded string:
16577 * Thus the -len here.
16578 */
16579 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16580 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016581 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 *alias = temp_string;
16583 *arg = skipwhite(p);
16584 return (int)STRLEN(temp_string);
16585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586
16587 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016588 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 EMSG2(_(e_invexpr2), *arg);
16590
16591 return len;
16592}
16593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594/*
16595 * Find the end of a variable or function name, taking care of magic braces.
16596 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16597 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016598 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016599 * Return a pointer to just after the name. Equal to "arg" if there is no
16600 * valid name.
16601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016603find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 char_u *arg;
16605 char_u **expr_start;
16606 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016607 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016609 int mb_nest = 0;
16610 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 char_u *p;
16612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016615 *expr_start = NULL;
16616 *expr_end = NULL;
16617 }
16618
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016619 /* Quick check for valid starting character. */
16620 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16621 return arg;
16622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623 for (p = arg; *p != NUL
16624 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016625 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016626 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016627 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016628 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016630 if (*p == '\'')
16631 {
16632 /* skip over 'string' to avoid counting [ and ] inside it. */
16633 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16634 ;
16635 if (*p == NUL)
16636 break;
16637 }
16638 else if (*p == '"')
16639 {
16640 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16641 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16642 if (*p == '\\' && p[1] != NUL)
16643 ++p;
16644 if (*p == NUL)
16645 break;
16646 }
16647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016648 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650 if (*p == '[')
16651 ++br_nest;
16652 else if (*p == ']')
16653 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016656 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016658 if (*p == '{')
16659 {
16660 mb_nest++;
16661 if (expr_start != NULL && *expr_start == NULL)
16662 *expr_start = p;
16663 }
16664 else if (*p == '}')
16665 {
16666 mb_nest--;
16667 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16668 *expr_end = p;
16669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 }
16672
16673 return p;
16674}
16675
16676/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016677 * Expands out the 'magic' {}'s in a variable/function name.
16678 * Note that this can call itself recursively, to deal with
16679 * constructs like foo{bar}{baz}{bam}
16680 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16681 * "in_start" ^
16682 * "expr_start" ^
16683 * "expr_end" ^
16684 * "in_end" ^
16685 *
16686 * Returns a new allocated string, which the caller must free.
16687 * Returns NULL for failure.
16688 */
16689 static char_u *
16690make_expanded_name(in_start, expr_start, expr_end, in_end)
16691 char_u *in_start;
16692 char_u *expr_start;
16693 char_u *expr_end;
16694 char_u *in_end;
16695{
16696 char_u c1;
16697 char_u *retval = NULL;
16698 char_u *temp_result;
16699 char_u *nextcmd = NULL;
16700
16701 if (expr_end == NULL || in_end == NULL)
16702 return NULL;
16703 *expr_start = NUL;
16704 *expr_end = NUL;
16705 c1 = *in_end;
16706 *in_end = NUL;
16707
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016708 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016709 if (temp_result != NULL && nextcmd == NULL)
16710 {
16711 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16712 + (in_end - expr_end) + 1));
16713 if (retval != NULL)
16714 {
16715 STRCPY(retval, in_start);
16716 STRCAT(retval, temp_result);
16717 STRCAT(retval, expr_end + 1);
16718 }
16719 }
16720 vim_free(temp_result);
16721
16722 *in_end = c1; /* put char back for error messages */
16723 *expr_start = '{';
16724 *expr_end = '}';
16725
16726 if (retval != NULL)
16727 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016728 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016729 if (expr_start != NULL)
16730 {
16731 /* Further expansion! */
16732 temp_result = make_expanded_name(retval, expr_start,
16733 expr_end, temp_result);
16734 vim_free(retval);
16735 retval = temp_result;
16736 }
16737 }
16738
16739 return retval;
16740}
16741
16742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016744 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 */
16746 static int
16747eval_isnamec(c)
16748 int c;
16749{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016750 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16751}
16752
16753/*
16754 * Return TRUE if character "c" can be used as the first character in a
16755 * variable or function name (excluding '{' and '}').
16756 */
16757 static int
16758eval_isnamec1(c)
16759 int c;
16760{
16761 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762}
16763
16764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765 * Set number v: variable to "val".
16766 */
16767 void
16768set_vim_var_nr(idx, val)
16769 int idx;
16770 long val;
16771{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016772 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773}
16774
16775/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016776 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 */
16778 long
16779get_vim_var_nr(idx)
16780 int idx;
16781{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016782 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783}
16784
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016785#if defined(FEAT_AUTOCMD) || defined(PROTO)
16786/*
16787 * Get string v: variable value. Uses a static buffer, can only be used once.
16788 */
16789 char_u *
16790get_vim_var_str(idx)
16791 int idx;
16792{
16793 return get_tv_string(&vimvars[idx].vv_tv);
16794}
16795#endif
16796
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797/*
16798 * Set v:count, v:count1 and v:prevcount.
16799 */
16800 void
16801set_vcount(count, count1)
16802 long count;
16803 long count1;
16804{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016805 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16806 vimvars[VV_COUNT].vv_nr = count;
16807 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808}
16809
16810/*
16811 * Set string v: variable to a copy of "val".
16812 */
16813 void
16814set_vim_var_string(idx, val, len)
16815 int idx;
16816 char_u *val;
16817 int len; /* length of "val" to use or -1 (whole string) */
16818{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016819 /* Need to do this (at least) once, since we can't initialize a union.
16820 * Will always be invoked when "v:progname" is set. */
16821 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16822
Bram Moolenaare9a41262005-01-15 22:18:47 +000016823 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016825 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016827 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016829 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830}
16831
16832/*
16833 * Set v:register if needed.
16834 */
16835 void
16836set_reg_var(c)
16837 int c;
16838{
16839 char_u regname;
16840
16841 if (c == 0 || c == ' ')
16842 regname = '"';
16843 else
16844 regname = c;
16845 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016846 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 set_vim_var_string(VV_REG, &regname, 1);
16848}
16849
16850/*
16851 * Get or set v:exception. If "oldval" == NULL, return the current value.
16852 * Otherwise, restore the value to "oldval" and return NULL.
16853 * Must always be called in pairs to save and restore v:exception! Does not
16854 * take care of memory allocations.
16855 */
16856 char_u *
16857v_exception(oldval)
16858 char_u *oldval;
16859{
16860 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016861 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
Bram Moolenaare9a41262005-01-15 22:18:47 +000016863 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 return NULL;
16865}
16866
16867/*
16868 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16869 * Otherwise, restore the value to "oldval" and return NULL.
16870 * Must always be called in pairs to save and restore v:throwpoint! Does not
16871 * take care of memory allocations.
16872 */
16873 char_u *
16874v_throwpoint(oldval)
16875 char_u *oldval;
16876{
16877 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016878 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879
Bram Moolenaare9a41262005-01-15 22:18:47 +000016880 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 return NULL;
16882}
16883
16884#if defined(FEAT_AUTOCMD) || defined(PROTO)
16885/*
16886 * Set v:cmdarg.
16887 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16888 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16889 * Must always be called in pairs!
16890 */
16891 char_u *
16892set_cmdarg(eap, oldarg)
16893 exarg_T *eap;
16894 char_u *oldarg;
16895{
16896 char_u *oldval;
16897 char_u *newval;
16898 unsigned len;
16899
Bram Moolenaare9a41262005-01-15 22:18:47 +000016900 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016901 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016903 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016904 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016905 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 }
16907
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016908 if (eap->force_bin == FORCE_BIN)
16909 len = 6;
16910 else if (eap->force_bin == FORCE_NOBIN)
16911 len = 8;
16912 else
16913 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016914
16915 if (eap->read_edit)
16916 len += 7;
16917
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016918 if (eap->force_ff != 0)
16919 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16920# ifdef FEAT_MBYTE
16921 if (eap->force_enc != 0)
16922 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016923 if (eap->bad_char != 0)
16924 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016925# endif
16926
16927 newval = alloc(len + 1);
16928 if (newval == NULL)
16929 return NULL;
16930
16931 if (eap->force_bin == FORCE_BIN)
16932 sprintf((char *)newval, " ++bin");
16933 else if (eap->force_bin == FORCE_NOBIN)
16934 sprintf((char *)newval, " ++nobin");
16935 else
16936 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016937
16938 if (eap->read_edit)
16939 STRCAT(newval, " ++edit");
16940
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016941 if (eap->force_ff != 0)
16942 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16943 eap->cmd + eap->force_ff);
16944# ifdef FEAT_MBYTE
16945 if (eap->force_enc != 0)
16946 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16947 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016948 if (eap->bad_char != 0)
16949 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16950 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016951# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016952 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016953 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954}
16955#endif
16956
16957/*
16958 * Get the value of internal variable "name".
16959 * Return OK or FAIL.
16960 */
16961 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016962get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 char_u *name;
16964 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016966 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967{
16968 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016969 typval_T *tv = NULL;
16970 typval_T atv;
16971 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973
16974 /* truncate the name, so that we can use strcmp() */
16975 cc = name[len];
16976 name[len] = NUL;
16977
16978 /*
16979 * Check for "b:changedtick".
16980 */
16981 if (STRCMP(name, "b:changedtick") == 0)
16982 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016983 atv.v_type = VAR_NUMBER;
16984 atv.vval.v_number = curbuf->b_changedtick;
16985 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 }
16987
16988 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 * Check for user-defined variables.
16990 */
16991 else
16992 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016993 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 }
16997
Bram Moolenaare9a41262005-01-15 22:18:47 +000016998 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017000 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 ret = FAIL;
17003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017005 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
17007 name[len] = cc;
17008
17009 return ret;
17010}
17011
17012/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017013 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17014 * Also handle function call with Funcref variable: func(expr)
17015 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17016 */
17017 static int
17018handle_subscript(arg, rettv, evaluate, verbose)
17019 char_u **arg;
17020 typval_T *rettv;
17021 int evaluate; /* do more than finding the end */
17022 int verbose; /* give error messages */
17023{
17024 int ret = OK;
17025 dict_T *selfdict = NULL;
17026 char_u *s;
17027 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017028 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017029
17030 while (ret == OK
17031 && (**arg == '['
17032 || (**arg == '.' && rettv->v_type == VAR_DICT)
17033 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17034 && !vim_iswhite(*(*arg - 1)))
17035 {
17036 if (**arg == '(')
17037 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017038 /* need to copy the funcref so that we can clear rettv */
17039 functv = *rettv;
17040 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017041
17042 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017043 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017044 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017045 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17046 &len, evaluate, selfdict);
17047
17048 /* Clear the funcref afterwards, so that deleting it while
17049 * evaluating the arguments is possible (see test55). */
17050 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017051
17052 /* Stop the expression evaluation when immediately aborting on
17053 * error, or when an interrupt occurred or an exception was thrown
17054 * but not caught. */
17055 if (aborting())
17056 {
17057 if (ret == OK)
17058 clear_tv(rettv);
17059 ret = FAIL;
17060 }
17061 dict_unref(selfdict);
17062 selfdict = NULL;
17063 }
17064 else /* **arg == '[' || **arg == '.' */
17065 {
17066 dict_unref(selfdict);
17067 if (rettv->v_type == VAR_DICT)
17068 {
17069 selfdict = rettv->vval.v_dict;
17070 if (selfdict != NULL)
17071 ++selfdict->dv_refcount;
17072 }
17073 else
17074 selfdict = NULL;
17075 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17076 {
17077 clear_tv(rettv);
17078 ret = FAIL;
17079 }
17080 }
17081 }
17082 dict_unref(selfdict);
17083 return ret;
17084}
17085
17086/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017087 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17088 * value).
17089 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017091alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017092{
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017094}
17095
17096/*
17097 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 * The string "s" must have been allocated, it is consumed.
17099 * Return NULL for out of memory, the variable otherwise.
17100 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 char_u *s;
17104{
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017107 rettv = alloc_tv();
17108 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017110 rettv->v_type = VAR_STRING;
17111 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 }
17113 else
17114 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116}
17117
17118/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017119 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017121 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017122free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124{
17125 if (varp != NULL)
17126 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017127 switch (varp->v_type)
17128 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017129 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017130 func_unref(varp->vval.v_string);
17131 /*FALLTHROUGH*/
17132 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017133 vim_free(varp->vval.v_string);
17134 break;
17135 case VAR_LIST:
17136 list_unref(varp->vval.v_list);
17137 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017138 case VAR_DICT:
17139 dict_unref(varp->vval.v_dict);
17140 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017141 case VAR_NUMBER:
17142 case VAR_UNKNOWN:
17143 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017144 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017145 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017146 break;
17147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 vim_free(varp);
17149 }
17150}
17151
17152/*
17153 * Free the memory for a variable value and set the value to NULL or 0.
17154 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017155 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158{
17159 if (varp != NULL)
17160 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017163 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017164 func_unref(varp->vval.v_string);
17165 /*FALLTHROUGH*/
17166 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017167 vim_free(varp->vval.v_string);
17168 varp->vval.v_string = NULL;
17169 break;
17170 case VAR_LIST:
17171 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017172 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017174 case VAR_DICT:
17175 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017176 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017177 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017179 varp->vval.v_number = 0;
17180 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017181 case VAR_UNKNOWN:
17182 break;
17183 default:
17184 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017186 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 }
17188}
17189
17190/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191 * Set the value of a variable to NULL without freeing items.
17192 */
17193 static void
17194init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017195 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017196{
17197 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199}
17200
17201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 * Get the number value of a variable.
17203 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 * For incompatible types, return 0.
17205 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17206 * caller of incompatible types: it sets *denote to TRUE if "denote"
17207 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 */
17209 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017211 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017213 int error = FALSE;
17214
17215 return get_tv_number_chk(varp, &error); /* return 0L on error */
17216}
17217
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017218 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017219get_tv_number_chk(varp, denote)
17220 typval_T *varp;
17221 int *denote;
17222{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017223 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017225 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017227 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017228 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017229 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017230 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017231 break;
17232 case VAR_STRING:
17233 if (varp->vval.v_string != NULL)
17234 vim_str2nr(varp->vval.v_string, NULL, NULL,
17235 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017236 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017237 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017238 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017239 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017240 case VAR_DICT:
17241 EMSG(_("E728: Using a Dictionary as a number"));
17242 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017243 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017244 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017247 if (denote == NULL) /* useful for values that must be unsigned */
17248 n = -1;
17249 else
17250 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252}
17253
17254/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017255 * Get the lnum from the first argument.
17256 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 */
17259 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017260get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017261 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 linenr_T lnum;
17265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017266 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 if (lnum == 0) /* no valid number, try using line() */
17268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269 rettv.v_type = VAR_NUMBER;
17270 f_line(argvars, &rettv);
17271 lnum = rettv.vval.v_number;
17272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 }
17274 return lnum;
17275}
17276
17277/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017278 * Get the lnum from the first argument.
17279 * Also accepts "$", then "buf" is used.
17280 * Returns 0 on error.
17281 */
17282 static linenr_T
17283get_tv_lnum_buf(argvars, buf)
17284 typval_T *argvars;
17285 buf_T *buf;
17286{
17287 if (argvars[0].v_type == VAR_STRING
17288 && argvars[0].vval.v_string != NULL
17289 && argvars[0].vval.v_string[0] == '$'
17290 && buf != NULL)
17291 return buf->b_ml.ml_line_count;
17292 return get_tv_number_chk(&argvars[0], NULL);
17293}
17294
17295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 * Get the string value of a variable.
17297 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017298 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17299 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 * If the String variable has never been set, return an empty string.
17301 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17303 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 */
17305 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308{
17309 static char_u mybuf[NUMBUFLEN];
17310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312}
17313
17314 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017316 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017317 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017319 char_u *res = get_tv_string_buf_chk(varp, buf);
17320
17321 return res != NULL ? res : (char_u *)"";
17322}
17323
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017324 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017325get_tv_string_chk(varp)
17326 typval_T *varp;
17327{
17328 static char_u mybuf[NUMBUFLEN];
17329
17330 return get_tv_string_buf_chk(varp, mybuf);
17331}
17332
17333 static char_u *
17334get_tv_string_buf_chk(varp, buf)
17335 typval_T *varp;
17336 char_u *buf;
17337{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017338 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017340 case VAR_NUMBER:
17341 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17342 return buf;
17343 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017344 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345 break;
17346 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017347 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017348 break;
17349 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017350 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017351 break;
17352 case VAR_STRING:
17353 if (varp->vval.v_string != NULL)
17354 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017356 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017357 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017360 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361}
17362
17363/*
17364 * Find variable "name" in the list of variables.
17365 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017366 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017367 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017371find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377
Bram Moolenaara7043832005-01-21 11:56:39 +000017378 ht = find_var_ht(name, &varname);
17379 if (htp != NULL)
17380 *htp = ht;
17381 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017383 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384}
17385
17386/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017387 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017388 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017391find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017392 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017393 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017394 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017395{
Bram Moolenaar33570922005-01-25 22:26:29 +000017396 hashitem_T *hi;
17397
17398 if (*varname == NUL)
17399 {
17400 /* Must be something like "s:", otherwise "ht" would be NULL. */
17401 switch (varname[-2])
17402 {
17403 case 's': return &SCRIPT_SV(current_SID).sv_var;
17404 case 'g': return &globvars_var;
17405 case 'v': return &vimvars_var;
17406 case 'b': return &curbuf->b_bufvar;
17407 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017408#ifdef FEAT_WINDOWS
17409 case 't': return &curtab->tp_winvar;
17410#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017411 case 'l': return current_funccal == NULL
17412 ? NULL : &current_funccal->l_vars_var;
17413 case 'a': return current_funccal == NULL
17414 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 }
17416 return NULL;
17417 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017418
17419 hi = hash_find(ht, varname);
17420 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017421 {
17422 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017423 * worked find the variable again. Don't auto-load a script if it was
17424 * loaded already, otherwise it would be loaded every time when
17425 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017426 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017427 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017428 hi = hash_find(ht, varname);
17429 if (HASHITEM_EMPTY(hi))
17430 return NULL;
17431 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017433}
17434
17435/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017436 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017437 * Set "varname" to the start of name without ':'.
17438 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017440find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 char_u *name;
17442 char_u **varname;
17443{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017444 hashitem_T *hi;
17445
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 if (name[1] != ':')
17447 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017448 /* The name must not start with a colon or #. */
17449 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 return NULL;
17451 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017452
17453 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017454 hi = hash_find(&compat_hashtab, name);
17455 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017456 return &compat_hashtab;
17457
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 return &globvarht; /* global variable */
17460 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 }
17462 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017463 if (*name == 'g') /* global variable */
17464 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017465 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17466 */
17467 if (vim_strchr(name + 2, ':') != NULL
17468 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017469 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017473 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017474#ifdef FEAT_WINDOWS
17475 if (*name == 't') /* tab page variable */
17476 return &curtab->tp_vars.dv_hashtab;
17477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 if (*name == 'v') /* v: variable */
17479 return &vimvarht;
17480 if (*name == 'a' && current_funccal != NULL) /* function argument */
17481 return &current_funccal->l_avars.dv_hashtab;
17482 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17483 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 if (*name == 's' /* script variable */
17485 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17486 return &SCRIPT_VARS(current_SID);
17487 return NULL;
17488}
17489
17490/*
17491 * Get the string value of a (global/local) variable.
17492 * Returns NULL when it doesn't exist.
17493 */
17494 char_u *
17495get_var_value(name)
17496 char_u *name;
17497{
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
Bram Moolenaara7043832005-01-21 11:56:39 +000017500 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 if (v == NULL)
17502 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017503 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504}
17505
17506/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 * sourcing this script and when executing functions defined in the script.
17509 */
17510 void
17511new_script_vars(id)
17512 scid_T id;
17513{
Bram Moolenaara7043832005-01-21 11:56:39 +000017514 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 hashtab_T *ht;
17516 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017517
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17519 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017520 /* Re-allocating ga_data means that an ht_array pointing to
17521 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017522 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017523 for (i = 1; i <= ga_scripts.ga_len; ++i)
17524 {
17525 ht = &SCRIPT_VARS(i);
17526 if (ht->ht_mask == HT_INIT_SIZE - 1)
17527 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 sv = &SCRIPT_SV(i);
17529 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017530 }
17531
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 while (ga_scripts.ga_len < id)
17533 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17535 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 }
17538 }
17539}
17540
17541/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017542 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17543 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 */
17545 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017546init_var_dict(dict, dict_var)
17547 dict_T *dict;
17548 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549{
Bram Moolenaar33570922005-01-25 22:26:29 +000017550 hash_init(&dict->dv_hashtab);
17551 dict->dv_refcount = 99999;
17552 dict_var->di_tv.vval.v_dict = dict;
17553 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017554 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17556 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557}
17558
17559/*
17560 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 * Frees all allocated variables and the value they contain.
17562 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 */
17564 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017565vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017566 hashtab_T *ht;
17567{
17568 vars_clear_ext(ht, TRUE);
17569}
17570
17571/*
17572 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17573 */
17574 static void
17575vars_clear_ext(ht, free_val)
17576 hashtab_T *ht;
17577 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578{
Bram Moolenaara7043832005-01-21 11:56:39 +000017579 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 hashitem_T *hi;
17581 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017584 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017585 for (hi = ht->ht_array; todo > 0; ++hi)
17586 {
17587 if (!HASHITEM_EMPTY(hi))
17588 {
17589 --todo;
17590
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017592 * ht_array might change then. hash_clear() takes care of it
17593 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 v = HI2DI(hi);
17595 if (free_val)
17596 clear_tv(&v->di_tv);
17597 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17598 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017599 }
17600 }
17601 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017602 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603}
17604
Bram Moolenaara7043832005-01-21 11:56:39 +000017605/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017606 * Delete a variable from hashtab "ht" at item "hi".
17607 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017610delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 hashtab_T *ht;
17612 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613{
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017615
17616 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017617 clear_tv(&di->di_tv);
17618 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619}
17620
17621/*
17622 * List the value of one internal variable.
17623 */
17624 static void
17625list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 char_u *prefix;
17628{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017629 char_u *tofree;
17630 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017631 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017632
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017633 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017634 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017635 s == NULL ? (char_u *)"" : s);
17636 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637}
17638
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 static void
17640list_one_var_a(prefix, name, type, string)
17641 char_u *prefix;
17642 char_u *name;
17643 int type;
17644 char_u *string;
17645{
17646 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17647 if (name != NULL) /* "a:" vars don't have a name stored */
17648 msg_puts(name);
17649 msg_putchar(' ');
17650 msg_advance(22);
17651 if (type == VAR_NUMBER)
17652 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017653 else if (type == VAR_FUNC)
17654 msg_putchar('*');
17655 else if (type == VAR_LIST)
17656 {
17657 msg_putchar('[');
17658 if (*string == '[')
17659 ++string;
17660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017661 else if (type == VAR_DICT)
17662 {
17663 msg_putchar('{');
17664 if (*string == '{')
17665 ++string;
17666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 else
17668 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017669
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671
17672 if (type == VAR_FUNC)
17673 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674}
17675
17676/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017677 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 * If the variable already exists, the value is updated.
17679 * Otherwise the variable is created.
17680 */
17681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017684 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686{
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017689 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017690 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017692 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017693 {
17694 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17695 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17696 ? name[2] : name[0]))
17697 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017698 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017699 return;
17700 }
17701 if (function_exists(name))
17702 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017703 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017704 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017705 return;
17706 }
17707 }
17708
Bram Moolenaara7043832005-01-21 11:56:39 +000017709 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017711 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017712 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017713 return;
17714 }
17715
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017716 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017717 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017719 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017720 if (var_check_ro(v->di_flags, name)
17721 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 return;
17723 if (v->di_tv.v_type != tv->v_type
17724 && !((v->di_tv.v_type == VAR_STRING
17725 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017726 && (tv->v_type == VAR_STRING
17727 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017728 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017729 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017730 return;
17731 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017732
17733 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017734 * Handle setting internal v: variables separately: we don't change
17735 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 */
17737 if (ht == &vimvarht)
17738 {
17739 if (v->di_tv.v_type == VAR_STRING)
17740 {
17741 vim_free(v->di_tv.vval.v_string);
17742 if (copy || tv->v_type != VAR_STRING)
17743 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17744 else
17745 {
17746 /* Take over the string to avoid an extra alloc/free. */
17747 v->di_tv.vval.v_string = tv->vval.v_string;
17748 tv->vval.v_string = NULL;
17749 }
17750 }
17751 else if (v->di_tv.v_type != VAR_NUMBER)
17752 EMSG2(_(e_intern2), "set_var()");
17753 else
17754 v->di_tv.vval.v_number = get_tv_number(tv);
17755 return;
17756 }
17757
17758 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 }
17760 else /* add a new variable */
17761 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017762 /* Can't add "v:" variable. */
17763 if (ht == &vimvarht)
17764 {
17765 EMSG2(_(e_illvar), name);
17766 return;
17767 }
17768
Bram Moolenaar92124a32005-06-17 22:03:40 +000017769 /* Make sure the variable name is valid. */
17770 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017771 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17772 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017773 {
17774 EMSG2(_(e_illvar), varname);
17775 return;
17776 }
17777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017778 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17779 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017780 if (v == NULL)
17781 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017785 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 return;
17787 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017788 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017791 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017792 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017793 else
17794 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017796 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799}
17800
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017801/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017802 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17803 * Also give an error message.
17804 */
17805 static int
17806var_check_ro(flags, name)
17807 int flags;
17808 char_u *name;
17809{
17810 if (flags & DI_FLAGS_RO)
17811 {
17812 EMSG2(_(e_readonlyvar), name);
17813 return TRUE;
17814 }
17815 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17816 {
17817 EMSG2(_(e_readonlysbx), name);
17818 return TRUE;
17819 }
17820 return FALSE;
17821}
17822
17823/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017824 * Return TRUE if typeval "tv" is set to be locked (immutable).
17825 * Also give an error message, using "name".
17826 */
17827 static int
17828tv_check_lock(lock, name)
17829 int lock;
17830 char_u *name;
17831{
17832 if (lock & VAR_LOCKED)
17833 {
17834 EMSG2(_("E741: Value is locked: %s"),
17835 name == NULL ? (char_u *)_("Unknown") : name);
17836 return TRUE;
17837 }
17838 if (lock & VAR_FIXED)
17839 {
17840 EMSG2(_("E742: Cannot change value of %s"),
17841 name == NULL ? (char_u *)_("Unknown") : name);
17842 return TRUE;
17843 }
17844 return FALSE;
17845}
17846
17847/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017849 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017850 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 typval_T *from;
17855 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017857 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017858 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017859 switch (from->v_type)
17860 {
17861 case VAR_NUMBER:
17862 to->vval.v_number = from->vval.v_number;
17863 break;
17864 case VAR_STRING:
17865 case VAR_FUNC:
17866 if (from->vval.v_string == NULL)
17867 to->vval.v_string = NULL;
17868 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017869 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017870 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017871 if (from->v_type == VAR_FUNC)
17872 func_ref(to->vval.v_string);
17873 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017874 break;
17875 case VAR_LIST:
17876 if (from->vval.v_list == NULL)
17877 to->vval.v_list = NULL;
17878 else
17879 {
17880 to->vval.v_list = from->vval.v_list;
17881 ++to->vval.v_list->lv_refcount;
17882 }
17883 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017884 case VAR_DICT:
17885 if (from->vval.v_dict == NULL)
17886 to->vval.v_dict = NULL;
17887 else
17888 {
17889 to->vval.v_dict = from->vval.v_dict;
17890 ++to->vval.v_dict->dv_refcount;
17891 }
17892 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017893 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017894 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017895 break;
17896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897}
17898
17899/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017900 * Make a copy of an item.
17901 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017902 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17903 * reference to an already copied list/dict can be used.
17904 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017905 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017906 static int
17907item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 typval_T *from;
17909 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017910 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017911 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017912{
17913 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017914 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017915
Bram Moolenaar33570922005-01-25 22:26:29 +000017916 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017917 {
17918 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017919 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017920 }
17921 ++recurse;
17922
17923 switch (from->v_type)
17924 {
17925 case VAR_NUMBER:
17926 case VAR_STRING:
17927 case VAR_FUNC:
17928 copy_tv(from, to);
17929 break;
17930 case VAR_LIST:
17931 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017932 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017933 if (from->vval.v_list == NULL)
17934 to->vval.v_list = NULL;
17935 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17936 {
17937 /* use the copy made earlier */
17938 to->vval.v_list = from->vval.v_list->lv_copylist;
17939 ++to->vval.v_list->lv_refcount;
17940 }
17941 else
17942 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17943 if (to->vval.v_list == NULL)
17944 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017945 break;
17946 case VAR_DICT:
17947 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017948 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017949 if (from->vval.v_dict == NULL)
17950 to->vval.v_dict = NULL;
17951 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17952 {
17953 /* use the copy made earlier */
17954 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17955 ++to->vval.v_dict->dv_refcount;
17956 }
17957 else
17958 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17959 if (to->vval.v_dict == NULL)
17960 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017961 break;
17962 default:
17963 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017964 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017965 }
17966 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017967 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017968}
17969
17970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 * ":echo expr1 ..." print each argument separated with a space, add a
17972 * newline at the end.
17973 * ":echon expr1 ..." print each argument plain.
17974 */
17975 void
17976ex_echo(eap)
17977 exarg_T *eap;
17978{
17979 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017980 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017981 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 char_u *p;
17983 int needclr = TRUE;
17984 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017985 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986
17987 if (eap->skip)
17988 ++emsg_skip;
17989 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17990 {
17991 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 {
17994 /*
17995 * Report the invalid expression unless the expression evaluation
17996 * has been cancelled due to an aborting error, an interrupt, or an
17997 * exception.
17998 */
17999 if (!aborting())
18000 EMSG2(_(e_invexpr2), p);
18001 break;
18002 }
18003 if (!eap->skip)
18004 {
18005 if (atstart)
18006 {
18007 atstart = FALSE;
18008 /* Call msg_start() after eval1(), evaluating the expression
18009 * may cause a message to appear. */
18010 if (eap->cmdidx == CMD_echo)
18011 msg_start();
18012 }
18013 else if (eap->cmdidx == CMD_echo)
18014 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018015 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018016 if (p != NULL)
18017 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018019 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018021 if (*p != TAB && needclr)
18022 {
18023 /* remove any text still there from the command */
18024 msg_clr_eos();
18025 needclr = FALSE;
18026 }
18027 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 }
18029 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018030 {
18031#ifdef FEAT_MBYTE
18032 if (has_mbyte)
18033 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018034 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018035
18036 (void)msg_outtrans_len_attr(p, i, echo_attr);
18037 p += i - 1;
18038 }
18039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018041 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018044 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018046 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 arg = skipwhite(arg);
18048 }
18049 eap->nextcmd = check_nextcmd(arg);
18050
18051 if (eap->skip)
18052 --emsg_skip;
18053 else
18054 {
18055 /* remove text that may still be there from the command */
18056 if (needclr)
18057 msg_clr_eos();
18058 if (eap->cmdidx == CMD_echo)
18059 msg_end();
18060 }
18061}
18062
18063/*
18064 * ":echohl {name}".
18065 */
18066 void
18067ex_echohl(eap)
18068 exarg_T *eap;
18069{
18070 int id;
18071
18072 id = syn_name2id(eap->arg);
18073 if (id == 0)
18074 echo_attr = 0;
18075 else
18076 echo_attr = syn_id2attr(id);
18077}
18078
18079/*
18080 * ":execute expr1 ..." execute the result of an expression.
18081 * ":echomsg expr1 ..." Print a message
18082 * ":echoerr expr1 ..." Print an error
18083 * Each gets spaces around each argument and a newline at the end for
18084 * echo commands
18085 */
18086 void
18087ex_execute(eap)
18088 exarg_T *eap;
18089{
18090 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018091 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 int ret = OK;
18093 char_u *p;
18094 garray_T ga;
18095 int len;
18096 int save_did_emsg;
18097
18098 ga_init2(&ga, 1, 80);
18099
18100 if (eap->skip)
18101 ++emsg_skip;
18102 while (*arg != NUL && *arg != '|' && *arg != '\n')
18103 {
18104 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018105 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 {
18107 /*
18108 * Report the invalid expression unless the expression evaluation
18109 * has been cancelled due to an aborting error, an interrupt, or an
18110 * exception.
18111 */
18112 if (!aborting())
18113 EMSG2(_(e_invexpr2), p);
18114 ret = FAIL;
18115 break;
18116 }
18117
18118 if (!eap->skip)
18119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018120 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 len = (int)STRLEN(p);
18122 if (ga_grow(&ga, len + 2) == FAIL)
18123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018124 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 ret = FAIL;
18126 break;
18127 }
18128 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 ga.ga_len += len;
18132 }
18133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 arg = skipwhite(arg);
18136 }
18137
18138 if (ret != FAIL && ga.ga_data != NULL)
18139 {
18140 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018143 out_flush();
18144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 else if (eap->cmdidx == CMD_echoerr)
18146 {
18147 /* We don't want to abort following commands, restore did_emsg. */
18148 save_did_emsg = did_emsg;
18149 EMSG((char_u *)ga.ga_data);
18150 if (!force_abort)
18151 did_emsg = save_did_emsg;
18152 }
18153 else if (eap->cmdidx == CMD_execute)
18154 do_cmdline((char_u *)ga.ga_data,
18155 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18156 }
18157
18158 ga_clear(&ga);
18159
18160 if (eap->skip)
18161 --emsg_skip;
18162
18163 eap->nextcmd = check_nextcmd(arg);
18164}
18165
18166/*
18167 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18168 * "arg" points to the "&" or '+' when called, to "option" when returning.
18169 * Returns NULL when no option name found. Otherwise pointer to the char
18170 * after the option name.
18171 */
18172 static char_u *
18173find_option_end(arg, opt_flags)
18174 char_u **arg;
18175 int *opt_flags;
18176{
18177 char_u *p = *arg;
18178
18179 ++p;
18180 if (*p == 'g' && p[1] == ':')
18181 {
18182 *opt_flags = OPT_GLOBAL;
18183 p += 2;
18184 }
18185 else if (*p == 'l' && p[1] == ':')
18186 {
18187 *opt_flags = OPT_LOCAL;
18188 p += 2;
18189 }
18190 else
18191 *opt_flags = 0;
18192
18193 if (!ASCII_ISALPHA(*p))
18194 return NULL;
18195 *arg = p;
18196
18197 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18198 p += 4; /* termcap option */
18199 else
18200 while (ASCII_ISALPHA(*p))
18201 ++p;
18202 return p;
18203}
18204
18205/*
18206 * ":function"
18207 */
18208 void
18209ex_function(eap)
18210 exarg_T *eap;
18211{
18212 char_u *theline;
18213 int j;
18214 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 char_u *name = NULL;
18217 char_u *p;
18218 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018219 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 garray_T newargs;
18221 garray_T newlines;
18222 int varargs = FALSE;
18223 int mustend = FALSE;
18224 int flags = 0;
18225 ufunc_T *fp;
18226 int indent;
18227 int nesting;
18228 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018229 dictitem_T *v;
18230 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018231 static int func_nr = 0; /* number for nameless function */
18232 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018233 hashtab_T *ht;
18234 int todo;
18235 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018236 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237
18238 /*
18239 * ":function" without argument: list functions.
18240 */
18241 if (ends_excmd(*eap->arg))
18242 {
18243 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018244 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018245 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018246 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018247 {
18248 if (!HASHITEM_EMPTY(hi))
18249 {
18250 --todo;
18251 fp = HI2UF(hi);
18252 if (!isdigit(*fp->uf_name))
18253 list_func_head(fp, FALSE);
18254 }
18255 }
18256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 eap->nextcmd = check_nextcmd(eap->arg);
18258 return;
18259 }
18260
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018261 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018262 * ":function /pat": list functions matching pattern.
18263 */
18264 if (*eap->arg == '/')
18265 {
18266 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18267 if (!eap->skip)
18268 {
18269 regmatch_T regmatch;
18270
18271 c = *p;
18272 *p = NUL;
18273 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18274 *p = c;
18275 if (regmatch.regprog != NULL)
18276 {
18277 regmatch.rm_ic = p_ic;
18278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018279 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018280 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18281 {
18282 if (!HASHITEM_EMPTY(hi))
18283 {
18284 --todo;
18285 fp = HI2UF(hi);
18286 if (!isdigit(*fp->uf_name)
18287 && vim_regexec(&regmatch, fp->uf_name, 0))
18288 list_func_head(fp, FALSE);
18289 }
18290 }
18291 }
18292 }
18293 if (*p == '/')
18294 ++p;
18295 eap->nextcmd = check_nextcmd(p);
18296 return;
18297 }
18298
18299 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018300 * Get the function name. There are these situations:
18301 * func normal function name
18302 * "name" == func, "fudi.fd_dict" == NULL
18303 * dict.func new dictionary entry
18304 * "name" == NULL, "fudi.fd_dict" set,
18305 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18306 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018307 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018308 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18309 * dict.func existing dict entry that's not a Funcref
18310 * "name" == NULL, "fudi.fd_dict" set,
18311 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018314 name = trans_function_name(&p, eap->skip, 0, &fudi);
18315 paren = (vim_strchr(p, '(') != NULL);
18316 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 {
18318 /*
18319 * Return on an invalid expression in braces, unless the expression
18320 * evaluation has been cancelled due to an aborting error, an
18321 * interrupt, or an exception.
18322 */
18323 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018325 if (!eap->skip && fudi.fd_newkey != NULL)
18326 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018327 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 else
18331 eap->skip = TRUE;
18332 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018333
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 /* An error in a function call during evaluation of an expression in magic
18335 * braces should not cause the function not to be defined. */
18336 saved_did_emsg = did_emsg;
18337 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338
18339 /*
18340 * ":function func" with only function name: list function.
18341 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018342 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 {
18344 if (!ends_excmd(*skipwhite(p)))
18345 {
18346 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018347 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 }
18349 eap->nextcmd = check_nextcmd(p);
18350 if (eap->nextcmd != NULL)
18351 *p = NUL;
18352 if (!eap->skip && !got_int)
18353 {
18354 fp = find_func(name);
18355 if (fp != NULL)
18356 {
18357 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018358 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018360 if (FUNCLINE(fp, j) == NULL)
18361 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 msg_putchar('\n');
18363 msg_outnum((long)(j + 1));
18364 if (j < 9)
18365 msg_putchar(' ');
18366 if (j < 99)
18367 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018368 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 out_flush(); /* show a line at a time */
18370 ui_breakcheck();
18371 }
18372 if (!got_int)
18373 {
18374 msg_putchar('\n');
18375 msg_puts((char_u *)" endfunction");
18376 }
18377 }
18378 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018379 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018381 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 }
18383
18384 /*
18385 * ":function name(arg1, arg2)" Define function.
18386 */
18387 p = skipwhite(p);
18388 if (*p != '(')
18389 {
18390 if (!eap->skip)
18391 {
18392 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018393 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 }
18395 /* attempt to continue by skipping some text */
18396 if (vim_strchr(p, '(') != NULL)
18397 p = vim_strchr(p, '(');
18398 }
18399 p = skipwhite(p + 1);
18400
18401 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18402 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18403
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018404 if (!eap->skip)
18405 {
18406 /* Check the name of the function. */
18407 if (name != NULL)
18408 arg = name;
18409 else
18410 arg = fudi.fd_newkey;
18411 if (arg != NULL)
18412 {
18413 if (*arg == K_SPECIAL)
18414 j = 3;
18415 else
18416 j = 0;
18417 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18418 : eval_isnamec(arg[j])))
18419 ++j;
18420 if (arg[j] != NUL)
18421 emsg_funcname(_(e_invarg2), arg);
18422 }
18423 }
18424
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 /*
18426 * Isolate the arguments: "arg1, arg2, ...)"
18427 */
18428 while (*p != ')')
18429 {
18430 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18431 {
18432 varargs = TRUE;
18433 p += 3;
18434 mustend = TRUE;
18435 }
18436 else
18437 {
18438 arg = p;
18439 while (ASCII_ISALNUM(*p) || *p == '_')
18440 ++p;
18441 if (arg == p || isdigit(*arg)
18442 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18443 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18444 {
18445 if (!eap->skip)
18446 EMSG2(_("E125: Illegal argument: %s"), arg);
18447 break;
18448 }
18449 if (ga_grow(&newargs, 1) == FAIL)
18450 goto erret;
18451 c = *p;
18452 *p = NUL;
18453 arg = vim_strsave(arg);
18454 if (arg == NULL)
18455 goto erret;
18456 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18457 *p = c;
18458 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 if (*p == ',')
18460 ++p;
18461 else
18462 mustend = TRUE;
18463 }
18464 p = skipwhite(p);
18465 if (mustend && *p != ')')
18466 {
18467 if (!eap->skip)
18468 EMSG2(_(e_invarg2), eap->arg);
18469 break;
18470 }
18471 }
18472 ++p; /* skip the ')' */
18473
Bram Moolenaare9a41262005-01-15 22:18:47 +000018474 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 for (;;)
18476 {
18477 p = skipwhite(p);
18478 if (STRNCMP(p, "range", 5) == 0)
18479 {
18480 flags |= FC_RANGE;
18481 p += 5;
18482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018483 else if (STRNCMP(p, "dict", 4) == 0)
18484 {
18485 flags |= FC_DICT;
18486 p += 4;
18487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 else if (STRNCMP(p, "abort", 5) == 0)
18489 {
18490 flags |= FC_ABORT;
18491 p += 5;
18492 }
18493 else
18494 break;
18495 }
18496
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018497 /* When there is a line break use what follows for the function body.
18498 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18499 if (*p == '\n')
18500 line_arg = p + 1;
18501 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 EMSG(_(e_trailing));
18503
18504 /*
18505 * Read the body of the function, until ":endfunction" is found.
18506 */
18507 if (KeyTyped)
18508 {
18509 /* Check if the function already exists, don't let the user type the
18510 * whole function before telling him it doesn't work! For a script we
18511 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018512 if (!eap->skip && !eap->forceit)
18513 {
18514 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18515 EMSG(_(e_funcdict));
18516 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018517 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018520 if (!eap->skip && did_emsg)
18521 goto erret;
18522
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 msg_putchar('\n'); /* don't overwrite the function name */
18524 cmdline_row = msg_row;
18525 }
18526
18527 indent = 2;
18528 nesting = 0;
18529 for (;;)
18530 {
18531 msg_scroll = TRUE;
18532 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018533 sourcing_lnum_off = sourcing_lnum;
18534
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018535 if (line_arg != NULL)
18536 {
18537 /* Use eap->arg, split up in parts by line breaks. */
18538 theline = line_arg;
18539 p = vim_strchr(theline, '\n');
18540 if (p == NULL)
18541 line_arg += STRLEN(line_arg);
18542 else
18543 {
18544 *p = NUL;
18545 line_arg = p + 1;
18546 }
18547 }
18548 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 theline = getcmdline(':', 0L, indent);
18550 else
18551 theline = eap->getline(':', eap->cookie, indent);
18552 if (KeyTyped)
18553 lines_left = Rows - 1;
18554 if (theline == NULL)
18555 {
18556 EMSG(_("E126: Missing :endfunction"));
18557 goto erret;
18558 }
18559
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018560 /* Detect line continuation: sourcing_lnum increased more than one. */
18561 if (sourcing_lnum > sourcing_lnum_off + 1)
18562 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18563 else
18564 sourcing_lnum_off = 0;
18565
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 if (skip_until != NULL)
18567 {
18568 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18569 * don't check for ":endfunc". */
18570 if (STRCMP(theline, skip_until) == 0)
18571 {
18572 vim_free(skip_until);
18573 skip_until = NULL;
18574 }
18575 }
18576 else
18577 {
18578 /* skip ':' and blanks*/
18579 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18580 ;
18581
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018582 /* Check for "endfunction". */
18583 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018585 if (line_arg == NULL)
18586 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 break;
18588 }
18589
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018590 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 * at "end". */
18592 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18593 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018594 else if (STRNCMP(p, "if", 2) == 0
18595 || STRNCMP(p, "wh", 2) == 0
18596 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 || STRNCMP(p, "try", 3) == 0)
18598 indent += 2;
18599
18600 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018601 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018603 if (*p == '!')
18604 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 p += eval_fname_script(p);
18606 if (ASCII_ISALPHA(*p))
18607 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018608 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 if (*skipwhite(p) == '(')
18610 {
18611 ++nesting;
18612 indent += 2;
18613 }
18614 }
18615 }
18616
18617 /* Check for ":append" or ":insert". */
18618 p = skip_range(p, NULL);
18619 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18620 || (p[0] == 'i'
18621 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18622 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18623 skip_until = vim_strsave((char_u *)".");
18624
18625 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18626 arg = skipwhite(skiptowhite(p));
18627 if (arg[0] == '<' && arg[1] =='<'
18628 && ((p[0] == 'p' && p[1] == 'y'
18629 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18630 || (p[0] == 'p' && p[1] == 'e'
18631 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18632 || (p[0] == 't' && p[1] == 'c'
18633 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18634 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18635 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018636 || (p[0] == 'm' && p[1] == 'z'
18637 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 ))
18639 {
18640 /* ":python <<" continues until a dot, like ":append" */
18641 p = skipwhite(arg + 2);
18642 if (*p == NUL)
18643 skip_until = vim_strsave((char_u *)".");
18644 else
18645 skip_until = vim_strsave(p);
18646 }
18647 }
18648
18649 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018650 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018651 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018652 if (line_arg == NULL)
18653 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018655 }
18656
18657 /* Copy the line to newly allocated memory. get_one_sourceline()
18658 * allocates 250 bytes per line, this saves 80% on average. The cost
18659 * is an extra alloc/free. */
18660 p = vim_strsave(theline);
18661 if (p != NULL)
18662 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018663 if (line_arg == NULL)
18664 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018665 theline = p;
18666 }
18667
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018668 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18669
18670 /* Add NULL lines for continuation lines, so that the line count is
18671 * equal to the index in the growarray. */
18672 while (sourcing_lnum_off-- > 0)
18673 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018674
18675 /* Check for end of eap->arg. */
18676 if (line_arg != NULL && *line_arg == NUL)
18677 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 }
18679
18680 /* Don't define the function when skipping commands or when an error was
18681 * detected. */
18682 if (eap->skip || did_emsg)
18683 goto erret;
18684
18685 /*
18686 * If there are no errors, add the function
18687 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018688 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018689 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018690 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018691 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018692 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018693 emsg_funcname("E707: Function name conflicts with variable: %s",
18694 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018695 goto erret;
18696 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018697
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018698 fp = find_func(name);
18699 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018701 if (!eap->forceit)
18702 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018703 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018704 goto erret;
18705 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018706 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018707 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018708 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018709 name);
18710 goto erret;
18711 }
18712 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018713 ga_clear_strings(&(fp->uf_args));
18714 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018715 vim_free(name);
18716 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 }
18719 else
18720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018721 char numbuf[20];
18722
18723 fp = NULL;
18724 if (fudi.fd_newkey == NULL && !eap->forceit)
18725 {
18726 EMSG(_(e_funcdict));
18727 goto erret;
18728 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018729 if (fudi.fd_di == NULL)
18730 {
18731 /* Can't add a function to a locked dictionary */
18732 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18733 goto erret;
18734 }
18735 /* Can't change an existing function if it is locked */
18736 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18737 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018738
18739 /* Give the function a sequential number. Can only be used with a
18740 * Funcref! */
18741 vim_free(name);
18742 sprintf(numbuf, "%d", ++func_nr);
18743 name = vim_strsave((char_u *)numbuf);
18744 if (name == NULL)
18745 goto erret;
18746 }
18747
18748 if (fp == NULL)
18749 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018750 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018751 {
18752 int slen, plen;
18753 char_u *scriptname;
18754
18755 /* Check that the autoload name matches the script name. */
18756 j = FAIL;
18757 if (sourcing_name != NULL)
18758 {
18759 scriptname = autoload_name(name);
18760 if (scriptname != NULL)
18761 {
18762 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018763 plen = (int)STRLEN(p);
18764 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018765 if (slen > plen && fnamecmp(p,
18766 sourcing_name + slen - plen) == 0)
18767 j = OK;
18768 vim_free(scriptname);
18769 }
18770 }
18771 if (j == FAIL)
18772 {
18773 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18774 goto erret;
18775 }
18776 }
18777
18778 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 if (fp == NULL)
18780 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018781
18782 if (fudi.fd_dict != NULL)
18783 {
18784 if (fudi.fd_di == NULL)
18785 {
18786 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018787 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018788 if (fudi.fd_di == NULL)
18789 {
18790 vim_free(fp);
18791 goto erret;
18792 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018793 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18794 {
18795 vim_free(fudi.fd_di);
18796 goto erret;
18797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018798 }
18799 else
18800 /* overwrite existing dict entry */
18801 clear_tv(&fudi.fd_di->di_tv);
18802 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018803 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018804 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018805 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018806
18807 /* behave like "dict" was used */
18808 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018809 }
18810
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018812 STRCPY(fp->uf_name, name);
18813 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018815 fp->uf_args = newargs;
18816 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018817#ifdef FEAT_PROFILE
18818 fp->uf_tml_count = NULL;
18819 fp->uf_tml_total = NULL;
18820 fp->uf_tml_self = NULL;
18821 fp->uf_profiling = FALSE;
18822 if (prof_def_func())
18823 func_do_profile(fp);
18824#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018825 fp->uf_varargs = varargs;
18826 fp->uf_flags = flags;
18827 fp->uf_calls = 0;
18828 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018829 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830
18831erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 ga_clear_strings(&newargs);
18833 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018834ret_free:
18835 vim_free(skip_until);
18836 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839}
18840
18841/*
18842 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018843 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018845 * flags:
18846 * TFN_INT: internal function name OK
18847 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 * Advances "pp" to just after the function name (if no error).
18849 */
18850 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018851trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 char_u **pp;
18853 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018854 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018857 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 char_u *start;
18859 char_u *end;
18860 int lead;
18861 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018863 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864
18865 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018868
18869 /* Check for hard coded <SNR>: already translated function ID (from a user
18870 * command). */
18871 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18872 && (*pp)[2] == (int)KE_SNR)
18873 {
18874 *pp += 3;
18875 len = get_id_len(pp) + 3;
18876 return vim_strnsave(start, len);
18877 }
18878
18879 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18880 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018882 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018884
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018885 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18886 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018887 if (end == start)
18888 {
18889 if (!skip)
18890 EMSG(_("E129: Function name required"));
18891 goto theend;
18892 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018893 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018894 {
18895 /*
18896 * Report an invalid expression in braces, unless the expression
18897 * evaluation has been cancelled due to an aborting error, an
18898 * interrupt, or an exception.
18899 */
18900 if (!aborting())
18901 {
18902 if (end != NULL)
18903 EMSG2(_(e_invarg2), start);
18904 }
18905 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018906 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018907 goto theend;
18908 }
18909
18910 if (lv.ll_tv != NULL)
18911 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018912 if (fdp != NULL)
18913 {
18914 fdp->fd_dict = lv.ll_dict;
18915 fdp->fd_newkey = lv.ll_newkey;
18916 lv.ll_newkey = NULL;
18917 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018919 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18920 {
18921 name = vim_strsave(lv.ll_tv->vval.v_string);
18922 *pp = end;
18923 }
18924 else
18925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018926 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18927 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018928 EMSG(_(e_funcref));
18929 else
18930 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018931 name = NULL;
18932 }
18933 goto theend;
18934 }
18935
18936 if (lv.ll_name == NULL)
18937 {
18938 /* Error found, but continue after the function name. */
18939 *pp = end;
18940 goto theend;
18941 }
18942
18943 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018944 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018945 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018946 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18947 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18948 {
18949 /* When there was "s:" already or the name expanded to get a
18950 * leading "s:" then remove it. */
18951 lv.ll_name += 2;
18952 len -= 2;
18953 lead = 2;
18954 }
18955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018956 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018957 {
18958 if (lead == 2) /* skip over "s:" */
18959 lv.ll_name += 2;
18960 len = (int)(end - lv.ll_name);
18961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018962
18963 /*
18964 * Copy the function name to allocated memory.
18965 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18966 * Accept <SNR>123_name() outside a script.
18967 */
18968 if (skip)
18969 lead = 0; /* do nothing */
18970 else if (lead > 0)
18971 {
18972 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000018973 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
18974 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018975 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018976 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018977 if (current_SID <= 0)
18978 {
18979 EMSG(_(e_usingsid));
18980 goto theend;
18981 }
18982 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18983 lead += (int)STRLEN(sid_buf);
18984 }
18985 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018986 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018987 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018988 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018989 goto theend;
18990 }
18991 name = alloc((unsigned)(len + lead + 1));
18992 if (name != NULL)
18993 {
18994 if (lead > 0)
18995 {
18996 name[0] = K_SPECIAL;
18997 name[1] = KS_EXTRA;
18998 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018999 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019000 STRCPY(name + 3, sid_buf);
19001 }
19002 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19003 name[len + lead] = NUL;
19004 }
19005 *pp = end;
19006
19007theend:
19008 clear_lval(&lv);
19009 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010}
19011
19012/*
19013 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19014 * Return 2 if "p" starts with "s:".
19015 * Return 0 otherwise.
19016 */
19017 static int
19018eval_fname_script(p)
19019 char_u *p;
19020{
19021 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19022 || STRNICMP(p + 1, "SNR>", 4) == 0))
19023 return 5;
19024 if (p[0] == 's' && p[1] == ':')
19025 return 2;
19026 return 0;
19027}
19028
19029/*
19030 * Return TRUE if "p" starts with "<SID>" or "s:".
19031 * Only works if eval_fname_script() returned non-zero for "p"!
19032 */
19033 static int
19034eval_fname_sid(p)
19035 char_u *p;
19036{
19037 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19038}
19039
19040/*
19041 * List the head of the function: "name(arg1, arg2)".
19042 */
19043 static void
19044list_func_head(fp, indent)
19045 ufunc_T *fp;
19046 int indent;
19047{
19048 int j;
19049
19050 msg_start();
19051 if (indent)
19052 MSG_PUTS(" ");
19053 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019054 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 {
19056 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019057 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 }
19059 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019060 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019062 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 {
19064 if (j)
19065 MSG_PUTS(", ");
19066 msg_puts(FUNCARG(fp, j));
19067 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019068 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 {
19070 if (j)
19071 MSG_PUTS(", ");
19072 MSG_PUTS("...");
19073 }
19074 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019075 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019076 if (p_verbose > 0)
19077 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078}
19079
19080/*
19081 * Find a function by name, return pointer to it in ufuncs.
19082 * Return NULL for unknown function.
19083 */
19084 static ufunc_T *
19085find_func(name)
19086 char_u *name;
19087{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019088 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019090 hi = hash_find(&func_hashtab, name);
19091 if (!HASHITEM_EMPTY(hi))
19092 return HI2UF(hi);
19093 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094}
19095
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019096#if defined(EXITFREE) || defined(PROTO)
19097 void
19098free_all_functions()
19099{
19100 hashitem_T *hi;
19101
19102 /* Need to start all over every time, because func_free() may change the
19103 * hash table. */
19104 while (func_hashtab.ht_used > 0)
19105 for (hi = func_hashtab.ht_array; ; ++hi)
19106 if (!HASHITEM_EMPTY(hi))
19107 {
19108 func_free(HI2UF(hi));
19109 break;
19110 }
19111}
19112#endif
19113
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019114/*
19115 * Return TRUE if a function "name" exists.
19116 */
19117 static int
19118function_exists(name)
19119 char_u *name;
19120{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019121 char_u *nm = name;
19122 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019123 int n = FALSE;
19124
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019125 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019126 nm = skipwhite(nm);
19127
19128 /* Only accept "funcname", "funcname ", "funcname (..." and
19129 * "funcname(...", not "funcname!...". */
19130 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019131 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019132 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019133 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019134 else
19135 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019136 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019137 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019138 return n;
19139}
19140
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019141/*
19142 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019143 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019144 */
19145 static int
19146builtin_function(name)
19147 char_u *name;
19148{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019149 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19150 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019151}
19152
Bram Moolenaar05159a02005-02-26 23:04:13 +000019153#if defined(FEAT_PROFILE) || defined(PROTO)
19154/*
19155 * Start profiling function "fp".
19156 */
19157 static void
19158func_do_profile(fp)
19159 ufunc_T *fp;
19160{
19161 fp->uf_tm_count = 0;
19162 profile_zero(&fp->uf_tm_self);
19163 profile_zero(&fp->uf_tm_total);
19164 if (fp->uf_tml_count == NULL)
19165 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19166 (sizeof(int) * fp->uf_lines.ga_len));
19167 if (fp->uf_tml_total == NULL)
19168 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19169 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19170 if (fp->uf_tml_self == NULL)
19171 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19172 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19173 fp->uf_tml_idx = -1;
19174 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19175 || fp->uf_tml_self == NULL)
19176 return; /* out of memory */
19177
19178 fp->uf_profiling = TRUE;
19179}
19180
19181/*
19182 * Dump the profiling results for all functions in file "fd".
19183 */
19184 void
19185func_dump_profile(fd)
19186 FILE *fd;
19187{
19188 hashitem_T *hi;
19189 int todo;
19190 ufunc_T *fp;
19191 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019192 ufunc_T **sorttab;
19193 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019194
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019195 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019196 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19197
Bram Moolenaar05159a02005-02-26 23:04:13 +000019198 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19199 {
19200 if (!HASHITEM_EMPTY(hi))
19201 {
19202 --todo;
19203 fp = HI2UF(hi);
19204 if (fp->uf_profiling)
19205 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019206 if (sorttab != NULL)
19207 sorttab[st_len++] = fp;
19208
Bram Moolenaar05159a02005-02-26 23:04:13 +000019209 if (fp->uf_name[0] == K_SPECIAL)
19210 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19211 else
19212 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19213 if (fp->uf_tm_count == 1)
19214 fprintf(fd, "Called 1 time\n");
19215 else
19216 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19217 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19218 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19219 fprintf(fd, "\n");
19220 fprintf(fd, "count total (s) self (s)\n");
19221
19222 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19223 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019224 if (FUNCLINE(fp, i) == NULL)
19225 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019226 prof_func_line(fd, fp->uf_tml_count[i],
19227 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019228 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19229 }
19230 fprintf(fd, "\n");
19231 }
19232 }
19233 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019234
19235 if (sorttab != NULL && st_len > 0)
19236 {
19237 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19238 prof_total_cmp);
19239 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19240 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19241 prof_self_cmp);
19242 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19243 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019244}
Bram Moolenaar73830342005-02-28 22:48:19 +000019245
19246 static void
19247prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19248 FILE *fd;
19249 ufunc_T **sorttab;
19250 int st_len;
19251 char *title;
19252 int prefer_self; /* when equal print only self time */
19253{
19254 int i;
19255 ufunc_T *fp;
19256
19257 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19258 fprintf(fd, "count total (s) self (s) function\n");
19259 for (i = 0; i < 20 && i < st_len; ++i)
19260 {
19261 fp = sorttab[i];
19262 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19263 prefer_self);
19264 if (fp->uf_name[0] == K_SPECIAL)
19265 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19266 else
19267 fprintf(fd, " %s()\n", fp->uf_name);
19268 }
19269 fprintf(fd, "\n");
19270}
19271
19272/*
19273 * Print the count and times for one function or function line.
19274 */
19275 static void
19276prof_func_line(fd, count, total, self, prefer_self)
19277 FILE *fd;
19278 int count;
19279 proftime_T *total;
19280 proftime_T *self;
19281 int prefer_self; /* when equal print only self time */
19282{
19283 if (count > 0)
19284 {
19285 fprintf(fd, "%5d ", count);
19286 if (prefer_self && profile_equal(total, self))
19287 fprintf(fd, " ");
19288 else
19289 fprintf(fd, "%s ", profile_msg(total));
19290 if (!prefer_self && profile_equal(total, self))
19291 fprintf(fd, " ");
19292 else
19293 fprintf(fd, "%s ", profile_msg(self));
19294 }
19295 else
19296 fprintf(fd, " ");
19297}
19298
19299/*
19300 * Compare function for total time sorting.
19301 */
19302 static int
19303#ifdef __BORLANDC__
19304_RTLENTRYF
19305#endif
19306prof_total_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_total, &p2->uf_tm_total);
19315}
19316
19317/*
19318 * Compare function for self time sorting.
19319 */
19320 static int
19321#ifdef __BORLANDC__
19322_RTLENTRYF
19323#endif
19324prof_self_cmp(s1, s2)
19325 const void *s1;
19326 const void *s2;
19327{
19328 ufunc_T *p1, *p2;
19329
19330 p1 = *(ufunc_T **)s1;
19331 p2 = *(ufunc_T **)s2;
19332 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19333}
19334
Bram Moolenaar05159a02005-02-26 23:04:13 +000019335#endif
19336
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019337/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019338 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019339 * Return TRUE if a package was loaded.
19340 */
19341 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019342script_autoload(name, reload)
19343 char_u *name;
19344 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019345{
19346 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019347 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019348 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019349 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019350
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019351 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019352 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019353 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019354 return FALSE;
19355
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019356 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019357
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019358 /* Find the name in the list of previously loaded package names. Skip
19359 * "autoload/", it's always the same. */
19360 for (i = 0; i < ga_loaded.ga_len; ++i)
19361 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19362 break;
19363 if (!reload && i < ga_loaded.ga_len)
19364 ret = FALSE; /* was loaded already */
19365 else
19366 {
19367 /* Remember the name if it wasn't loaded already. */
19368 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19369 {
19370 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19371 tofree = NULL;
19372 }
19373
19374 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019375 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019376 ret = TRUE;
19377 }
19378
19379 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019380 return ret;
19381}
19382
19383/*
19384 * Return the autoload script name for a function or variable name.
19385 * Returns NULL when out of memory.
19386 */
19387 static char_u *
19388autoload_name(name)
19389 char_u *name;
19390{
19391 char_u *p;
19392 char_u *scriptname;
19393
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019394 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019395 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19396 if (scriptname == NULL)
19397 return FALSE;
19398 STRCPY(scriptname, "autoload/");
19399 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019400 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019401 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019402 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019403 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019404 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019405}
19406
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19408
19409/*
19410 * Function given to ExpandGeneric() to obtain the list of user defined
19411 * function names.
19412 */
19413 char_u *
19414get_user_func_name(xp, idx)
19415 expand_T *xp;
19416 int idx;
19417{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019418 static long_u done;
19419 static hashitem_T *hi;
19420 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421
19422 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019424 done = 0;
19425 hi = func_hashtab.ht_array;
19426 }
19427 if (done < func_hashtab.ht_used)
19428 {
19429 if (done++ > 0)
19430 ++hi;
19431 while (HASHITEM_EMPTY(hi))
19432 ++hi;
19433 fp = HI2UF(hi);
19434
19435 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19436 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437
19438 cat_func_name(IObuff, fp);
19439 if (xp->xp_context != EXPAND_USER_FUNC)
19440 {
19441 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019442 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 STRCAT(IObuff, ")");
19444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 return IObuff;
19446 }
19447 return NULL;
19448}
19449
19450#endif /* FEAT_CMDL_COMPL */
19451
19452/*
19453 * Copy the function name of "fp" to buffer "buf".
19454 * "buf" must be able to hold the function name plus three bytes.
19455 * Takes care of script-local function names.
19456 */
19457 static void
19458cat_func_name(buf, fp)
19459 char_u *buf;
19460 ufunc_T *fp;
19461{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019462 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 {
19464 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019465 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 }
19467 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019468 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469}
19470
19471/*
19472 * ":delfunction {name}"
19473 */
19474 void
19475ex_delfunction(eap)
19476 exarg_T *eap;
19477{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019478 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 char_u *p;
19480 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019481 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482
19483 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019484 name = trans_function_name(&p, eap->skip, 0, &fudi);
19485 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019487 {
19488 if (fudi.fd_dict != NULL && !eap->skip)
19489 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 if (!ends_excmd(*skipwhite(p)))
19493 {
19494 vim_free(name);
19495 EMSG(_(e_trailing));
19496 return;
19497 }
19498 eap->nextcmd = check_nextcmd(p);
19499 if (eap->nextcmd != NULL)
19500 *p = NUL;
19501
19502 if (!eap->skip)
19503 fp = find_func(name);
19504 vim_free(name);
19505
19506 if (!eap->skip)
19507 {
19508 if (fp == NULL)
19509 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019510 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 return;
19512 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019513 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 {
19515 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19516 return;
19517 }
19518
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019519 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019521 /* Delete the dict item that refers to the function, it will
19522 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019523 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019525 else
19526 func_free(fp);
19527 }
19528}
19529
19530/*
19531 * Free a function and remove it from the list of functions.
19532 */
19533 static void
19534func_free(fp)
19535 ufunc_T *fp;
19536{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019537 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019538
19539 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019540 ga_clear_strings(&(fp->uf_args));
19541 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019542#ifdef FEAT_PROFILE
19543 vim_free(fp->uf_tml_count);
19544 vim_free(fp->uf_tml_total);
19545 vim_free(fp->uf_tml_self);
19546#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019547
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019548 /* remove the function from the function hashtable */
19549 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19550 if (HASHITEM_EMPTY(hi))
19551 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019552 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019553 hash_remove(&func_hashtab, hi);
19554
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019555 vim_free(fp);
19556}
19557
19558/*
19559 * Unreference a Function: decrement the reference count and free it when it
19560 * becomes zero. Only for numbered functions.
19561 */
19562 static void
19563func_unref(name)
19564 char_u *name;
19565{
19566 ufunc_T *fp;
19567
19568 if (name != NULL && isdigit(*name))
19569 {
19570 fp = find_func(name);
19571 if (fp == NULL)
19572 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019573 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019574 {
19575 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019576 * when "uf_calls" becomes zero. */
19577 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019578 func_free(fp);
19579 }
19580 }
19581}
19582
19583/*
19584 * Count a reference to a Function.
19585 */
19586 static void
19587func_ref(name)
19588 char_u *name;
19589{
19590 ufunc_T *fp;
19591
19592 if (name != NULL && isdigit(*name))
19593 {
19594 fp = find_func(name);
19595 if (fp == NULL)
19596 EMSG2(_(e_intern2), "func_ref()");
19597 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019598 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 }
19600}
19601
19602/*
19603 * Call a user function.
19604 */
19605 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019606call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 ufunc_T *fp; /* pointer to function */
19608 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019609 typval_T *argvars; /* arguments */
19610 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 linenr_T firstline; /* first line of range */
19612 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614{
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 char_u *save_sourcing_name;
19616 linenr_T save_sourcing_lnum;
19617 scid_T save_current_SID;
19618 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 int save_did_emsg;
19620 static int depth = 0;
19621 dictitem_T *v;
19622 int fixvar_idx = 0; /* index in fixvar[] */
19623 int i;
19624 int ai;
19625 char_u numbuf[NUMBUFLEN];
19626 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019627#ifdef FEAT_PROFILE
19628 proftime_T wait_start;
19629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630
19631 /* If depth of calling is getting too high, don't execute the function */
19632 if (depth >= p_mfd)
19633 {
19634 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635 rettv->v_type = VAR_NUMBER;
19636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 return;
19638 }
19639 ++depth;
19640
19641 line_breakcheck(); /* check for CTRL-C hit */
19642
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019643 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 fc.rettv = rettv;
19647 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 fc.linenr = 0;
19649 fc.returned = FALSE;
19650 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019652 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 fc.dbg_tick = debug_tick;
19654
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 /*
19656 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19657 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19658 * each argument variable and saves a lot of time.
19659 */
19660 /*
19661 * Init l: variables.
19662 */
19663 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019664 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019665 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019666 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19667 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019669 name = v->di_key;
19670 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19672 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19673 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019674 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 v->di_tv.vval.v_dict = selfdict;
19676 ++selfdict->dv_refcount;
19677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019678
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 /*
19680 * Init a: variables.
19681 * Set a:0 to "argcount".
19682 * Set a:000 to a list with room for the "..." arguments.
19683 */
19684 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19685 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019686 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 v = &fc.fixvar[fixvar_idx++].var;
19688 STRCPY(v->di_key, "000");
19689 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19690 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19691 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019692 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 v->di_tv.vval.v_list = &fc.l_varlist;
19694 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19695 fc.l_varlist.lv_refcount = 99999;
19696
19697 /*
19698 * Set a:firstline to "firstline" and a:lastline to "lastline".
19699 * Set a:name to named arguments.
19700 * Set a:N to the "..." arguments.
19701 */
19702 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19703 (varnumber_T)firstline);
19704 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19705 (varnumber_T)lastline);
19706 for (i = 0; i < argcount; ++i)
19707 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019708 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019709 if (ai < 0)
19710 /* named argument a:name */
19711 name = FUNCARG(fp, i);
19712 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019713 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019714 /* "..." argument a:1, a:2, etc. */
19715 sprintf((char *)numbuf, "%d", ai + 1);
19716 name = numbuf;
19717 }
19718 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19719 {
19720 v = &fc.fixvar[fixvar_idx++].var;
19721 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19722 }
19723 else
19724 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019725 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19726 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 if (v == NULL)
19728 break;
19729 v->di_flags = DI_FLAGS_RO;
19730 }
19731 STRCPY(v->di_key, name);
19732 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19733
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019734 /* Note: the values are copied directly to avoid alloc/free.
19735 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019736 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019737 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019738
19739 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19740 {
19741 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19742 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019743 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019744 }
19745 }
19746
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 /* Don't redraw while executing the function. */
19748 ++RedrawingDisabled;
19749 save_sourcing_name = sourcing_name;
19750 save_sourcing_lnum = sourcing_lnum;
19751 sourcing_lnum = 1;
19752 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019753 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 if (sourcing_name != NULL)
19755 {
19756 if (save_sourcing_name != NULL
19757 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19758 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19759 else
19760 STRCPY(sourcing_name, "function ");
19761 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19762
19763 if (p_verbose >= 12)
19764 {
19765 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019766 verbose_enter_scroll();
19767
Bram Moolenaar555b2802005-05-19 21:08:39 +000019768 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 if (p_verbose >= 14)
19770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019772 char_u numbuf[NUMBUFLEN];
19773 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774
19775 msg_puts((char_u *)"(");
19776 for (i = 0; i < argcount; ++i)
19777 {
19778 if (i > 0)
19779 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019780 if (argvars[i].v_type == VAR_NUMBER)
19781 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 else
19783 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019784 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019785 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019787 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 }
19789 }
19790 msg_puts((char_u *)")");
19791 }
19792 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019793
19794 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 --no_wait_return;
19796 }
19797 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019798#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019800 {
19801 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19802 func_do_profile(fp);
19803 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019804 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019805 {
19806 ++fp->uf_tm_count;
19807 profile_start(&fp->uf_tm_start);
19808 profile_zero(&fp->uf_tm_children);
19809 }
19810 script_prof_save(&wait_start);
19811 }
19812#endif
19813
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019815 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 save_did_emsg = did_emsg;
19817 did_emsg = FALSE;
19818
19819 /* call do_cmdline() to execute the lines */
19820 do_cmdline(NULL, get_func_line, (void *)&fc,
19821 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19822
19823 --RedrawingDisabled;
19824
19825 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019826 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 clear_tv(rettv);
19829 rettv->v_type = VAR_NUMBER;
19830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 }
19832
Bram Moolenaar05159a02005-02-26 23:04:13 +000019833#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019834 if (do_profiling == PROF_YES && (fp->uf_profiling
19835 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019836 {
19837 profile_end(&fp->uf_tm_start);
19838 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19839 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019840 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019841 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019842 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019843 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19844 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019845 }
19846 }
19847#endif
19848
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 /* when being verbose, mention the return value */
19850 if (p_verbose >= 12)
19851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019853 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019856 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019858 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19859 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019862 char_u buf[MSG_BUF_LEN];
19863 char_u numbuf[NUMBUFLEN];
19864 char_u *tofree;
19865
Bram Moolenaar555b2802005-05-19 21:08:39 +000019866 /* The value may be very long. Skip the middle part, so that we
19867 * have some idea how it starts and ends. smsg() would always
19868 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019869 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019870 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019871 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019872 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 }
19874 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019875
19876 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 --no_wait_return;
19878 }
19879
19880 vim_free(sourcing_name);
19881 sourcing_name = save_sourcing_name;
19882 sourcing_lnum = save_sourcing_lnum;
19883 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019884#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019885 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019886 script_prof_restore(&wait_start);
19887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888
19889 if (p_verbose >= 12 && sourcing_name != NULL)
19890 {
19891 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019892 verbose_enter_scroll();
19893
Bram Moolenaar555b2802005-05-19 21:08:39 +000019894 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019896
19897 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 --no_wait_return;
19899 }
19900
19901 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019902 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 /* The a: variables typevals were not alloced, only free the allocated
19905 * variables. */
19906 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19907
19908 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 --depth;
19910}
19911
19912/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019913 * Add a number variable "name" to dict "dp" with value "nr".
19914 */
19915 static void
19916add_nr_var(dp, v, name, nr)
19917 dict_T *dp;
19918 dictitem_T *v;
19919 char *name;
19920 varnumber_T nr;
19921{
19922 STRCPY(v->di_key, name);
19923 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19924 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19925 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019926 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 v->di_tv.vval.v_number = nr;
19928}
19929
19930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 * ":return [expr]"
19932 */
19933 void
19934ex_return(eap)
19935 exarg_T *eap;
19936{
19937 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019938 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 int returning = FALSE;
19940
19941 if (current_funccal == NULL)
19942 {
19943 EMSG(_("E133: :return not inside a function"));
19944 return;
19945 }
19946
19947 if (eap->skip)
19948 ++emsg_skip;
19949
19950 eap->nextcmd = NULL;
19951 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019952 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 {
19954 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 }
19959 /* It's safer to return also on error. */
19960 else if (!eap->skip)
19961 {
19962 /*
19963 * Return unless the expression evaluation has been cancelled due to an
19964 * aborting error, an interrupt, or an exception.
19965 */
19966 if (!aborting())
19967 returning = do_return(eap, FALSE, TRUE, NULL);
19968 }
19969
19970 /* When skipping or the return gets pending, advance to the next command
19971 * in this line (!returning). Otherwise, ignore the rest of the line.
19972 * Following lines will be ignored by get_func_line(). */
19973 if (returning)
19974 eap->nextcmd = NULL;
19975 else if (eap->nextcmd == NULL) /* no argument */
19976 eap->nextcmd = check_nextcmd(arg);
19977
19978 if (eap->skip)
19979 --emsg_skip;
19980}
19981
19982/*
19983 * Return from a function. Possibly makes the return pending. Also called
19984 * for a pending return at the ":endtry" or after returning from an extra
19985 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019986 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 * FALSE when the return gets pending.
19989 */
19990 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 exarg_T *eap;
19993 int reanimate;
19994 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
19997 int idx;
19998 struct condstack *cstack = eap->cstack;
19999
20000 if (reanimate)
20001 /* Undo the return. */
20002 current_funccal->returned = FALSE;
20003
20004 /*
20005 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20006 * not in its finally clause (which then is to be executed next) is found.
20007 * In this case, make the ":return" pending for execution at the ":endtry".
20008 * Otherwise, return normally.
20009 */
20010 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20011 if (idx >= 0)
20012 {
20013 cstack->cs_pending[idx] = CSTP_RETURN;
20014
20015 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016 /* A pending return again gets pending. "rettv" points to an
20017 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 else
20021 {
20022 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020027 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 {
20029 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020030 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 else
20033 EMSG(_(e_outofmem));
20034 }
20035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020036 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037
20038 if (reanimate)
20039 {
20040 /* The pending return value could be overwritten by a ":return"
20041 * without argument in a finally clause; reset the default
20042 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043 current_funccal->rettv->v_type = VAR_NUMBER;
20044 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 }
20046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 }
20049 else
20050 {
20051 current_funccal->returned = TRUE;
20052
20053 /* If the return is carried out now, store the return value. For
20054 * a return immediately after reanimation, the value is already
20055 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020059 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 }
20063 }
20064
20065 return idx < 0;
20066}
20067
20068/*
20069 * Free the variable with a pending return value.
20070 */
20071 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072discard_pending_return(rettv)
20073 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074{
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076}
20077
20078/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020079 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 * is an allocated string. Used by report_pending() for verbose messages.
20081 */
20082 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083get_return_cmd(rettv)
20084 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020086 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020088 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020090 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020091 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020092 if (s == NULL)
20093 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094
20095 STRCPY(IObuff, ":return ");
20096 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20097 if (STRLEN(s) + 8 >= IOSIZE)
20098 STRCPY(IObuff + IOSIZE - 4, "...");
20099 vim_free(tofree);
20100 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101}
20102
20103/*
20104 * Get next function line.
20105 * Called by do_cmdline() to get the next line.
20106 * Returns allocated string, or NULL for end of function.
20107 */
20108/* ARGSUSED */
20109 char_u *
20110get_func_line(c, cookie, indent)
20111 int c; /* not used */
20112 void *cookie;
20113 int indent; /* not used */
20114{
Bram Moolenaar33570922005-01-25 22:26:29 +000020115 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020116 ufunc_T *fp = fcp->func;
20117 char_u *retval;
20118 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119
20120 /* If breakpoints have been added/deleted need to check for it. */
20121 if (fcp->dbg_tick != debug_tick)
20122 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020123 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 sourcing_lnum);
20125 fcp->dbg_tick = debug_tick;
20126 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020127#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020128 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020129 func_line_end(cookie);
20130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131
Bram Moolenaar05159a02005-02-26 23:04:13 +000020132 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020133 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20134 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 retval = NULL;
20136 else
20137 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020138 /* Skip NULL lines (continuation lines). */
20139 while (fcp->linenr < gap->ga_len
20140 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20141 ++fcp->linenr;
20142 if (fcp->linenr >= gap->ga_len)
20143 retval = NULL;
20144 else
20145 {
20146 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20147 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020148#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020149 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020150 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020151#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 }
20154
20155 /* Did we encounter a breakpoint? */
20156 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20157 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020158 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020160 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 sourcing_lnum);
20162 fcp->dbg_tick = debug_tick;
20163 }
20164
20165 return retval;
20166}
20167
Bram Moolenaar05159a02005-02-26 23:04:13 +000020168#if defined(FEAT_PROFILE) || defined(PROTO)
20169/*
20170 * Called when starting to read a function line.
20171 * "sourcing_lnum" must be correct!
20172 * When skipping lines it may not actually be executed, but we won't find out
20173 * until later and we need to store the time now.
20174 */
20175 void
20176func_line_start(cookie)
20177 void *cookie;
20178{
20179 funccall_T *fcp = (funccall_T *)cookie;
20180 ufunc_T *fp = fcp->func;
20181
20182 if (fp->uf_profiling && sourcing_lnum >= 1
20183 && sourcing_lnum <= fp->uf_lines.ga_len)
20184 {
20185 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020186 /* Skip continuation lines. */
20187 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20188 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020189 fp->uf_tml_execed = FALSE;
20190 profile_start(&fp->uf_tml_start);
20191 profile_zero(&fp->uf_tml_children);
20192 profile_get_wait(&fp->uf_tml_wait);
20193 }
20194}
20195
20196/*
20197 * Called when actually executing a function line.
20198 */
20199 void
20200func_line_exec(cookie)
20201 void *cookie;
20202{
20203 funccall_T *fcp = (funccall_T *)cookie;
20204 ufunc_T *fp = fcp->func;
20205
20206 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20207 fp->uf_tml_execed = TRUE;
20208}
20209
20210/*
20211 * Called when done with a function line.
20212 */
20213 void
20214func_line_end(cookie)
20215 void *cookie;
20216{
20217 funccall_T *fcp = (funccall_T *)cookie;
20218 ufunc_T *fp = fcp->func;
20219
20220 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20221 {
20222 if (fp->uf_tml_execed)
20223 {
20224 ++fp->uf_tml_count[fp->uf_tml_idx];
20225 profile_end(&fp->uf_tml_start);
20226 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020227 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020228 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20229 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020230 }
20231 fp->uf_tml_idx = -1;
20232 }
20233}
20234#endif
20235
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236/*
20237 * Return TRUE if the currently active function should be ended, because a
20238 * return was encountered or an error occured. Used inside a ":while".
20239 */
20240 int
20241func_has_ended(cookie)
20242 void *cookie;
20243{
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245
20246 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20247 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020248 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 || fcp->returned);
20250}
20251
20252/*
20253 * return TRUE if cookie indicates a function which "abort"s on errors.
20254 */
20255 int
20256func_has_abort(cookie)
20257 void *cookie;
20258{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020259 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260}
20261
20262#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20263typedef enum
20264{
20265 VAR_FLAVOUR_DEFAULT,
20266 VAR_FLAVOUR_SESSION,
20267 VAR_FLAVOUR_VIMINFO
20268} var_flavour_T;
20269
20270static var_flavour_T var_flavour __ARGS((char_u *varname));
20271
20272 static var_flavour_T
20273var_flavour(varname)
20274 char_u *varname;
20275{
20276 char_u *p = varname;
20277
20278 if (ASCII_ISUPPER(*p))
20279 {
20280 while (*(++p))
20281 if (ASCII_ISLOWER(*p))
20282 return VAR_FLAVOUR_SESSION;
20283 return VAR_FLAVOUR_VIMINFO;
20284 }
20285 else
20286 return VAR_FLAVOUR_DEFAULT;
20287}
20288#endif
20289
20290#if defined(FEAT_VIMINFO) || defined(PROTO)
20291/*
20292 * Restore global vars that start with a capital from the viminfo file
20293 */
20294 int
20295read_viminfo_varlist(virp, writing)
20296 vir_T *virp;
20297 int writing;
20298{
20299 char_u *tab;
20300 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302
20303 if (!writing && (find_viminfo_parameter('!') != NULL))
20304 {
20305 tab = vim_strchr(virp->vir_line + 1, '\t');
20306 if (tab != NULL)
20307 {
20308 *tab++ = '\0'; /* isolate the variable name */
20309 if (*tab == 'S') /* string var */
20310 is_string = TRUE;
20311
20312 tab = vim_strchr(tab, '\t');
20313 if (tab != NULL)
20314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 if (is_string)
20316 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020317 tv.v_type = VAR_STRING;
20318 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 }
20321 else
20322 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020323 tv.v_type = VAR_NUMBER;
20324 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020326 set_var(virp->vir_line + 1, &tv, FALSE);
20327 if (is_string)
20328 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 }
20330 }
20331 }
20332
20333 return viminfo_readline(virp);
20334}
20335
20336/*
20337 * Write global vars that start with a capital to the viminfo file
20338 */
20339 void
20340write_viminfo_varlist(fp)
20341 FILE *fp;
20342{
Bram Moolenaar33570922005-01-25 22:26:29 +000020343 hashitem_T *hi;
20344 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020345 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020346 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020347 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350
20351 if (find_viminfo_parameter('!') == NULL)
20352 return;
20353
20354 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020356 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020357 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020359 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020361 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020362 this_var = HI2DI(hi);
20363 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020365 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020366 {
20367 case VAR_STRING: s = "STR"; break;
20368 case VAR_NUMBER: s = "NUM"; break;
20369 default: continue;
20370 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020371 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020372 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020373 if (p != NULL)
20374 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020375 vim_free(tofree);
20376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 }
20378 }
20379}
20380#endif
20381
20382#if defined(FEAT_SESSION) || defined(PROTO)
20383 int
20384store_session_globals(fd)
20385 FILE *fd;
20386{
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 hashitem_T *hi;
20388 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020389 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 char_u *p, *t;
20391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020392 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020395 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020397 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020398 this_var = HI2DI(hi);
20399 if ((this_var->di_tv.v_type == VAR_NUMBER
20400 || this_var->di_tv.v_type == VAR_STRING)
20401 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020402 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020403 /* Escape special characters with a backslash. Turn a LF and
20404 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020406 (char_u *)"\\\"\n\r");
20407 if (p == NULL) /* out of memory */
20408 break;
20409 for (t = p; *t != NUL; ++t)
20410 if (*t == '\n')
20411 *t = 'n';
20412 else if (*t == '\r')
20413 *t = 'r';
20414 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 this_var->di_key,
20416 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20417 : ' ',
20418 p,
20419 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20420 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020421 || put_eol(fd) == FAIL)
20422 {
20423 vim_free(p);
20424 return FAIL;
20425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 vim_free(p);
20427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 }
20429 }
20430 return OK;
20431}
20432#endif
20433
Bram Moolenaar661b1822005-07-28 22:36:45 +000020434/*
20435 * Display script name where an item was last set.
20436 * Should only be invoked when 'verbose' is non-zero.
20437 */
20438 void
20439last_set_msg(scriptID)
20440 scid_T scriptID;
20441{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020442 char_u *p;
20443
Bram Moolenaar661b1822005-07-28 22:36:45 +000020444 if (scriptID != 0)
20445 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020446 p = home_replace_save(NULL, get_scriptname(scriptID));
20447 if (p != NULL)
20448 {
20449 verbose_enter();
20450 MSG_PUTS(_("\n\tLast set from "));
20451 MSG_PUTS(p);
20452 vim_free(p);
20453 verbose_leave();
20454 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020455 }
20456}
20457
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458#endif /* FEAT_EVAL */
20459
20460#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20461
20462
20463#ifdef WIN3264
20464/*
20465 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20466 */
20467static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20468static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20469static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20470
20471/*
20472 * Get the short pathname of a file.
20473 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20474 */
20475 static int
20476get_short_pathname(fnamep, bufp, fnamelen)
20477 char_u **fnamep;
20478 char_u **bufp;
20479 int *fnamelen;
20480{
20481 int l,len;
20482 char_u *newbuf;
20483
20484 len = *fnamelen;
20485
20486 l = GetShortPathName(*fnamep, *fnamep, len);
20487 if (l > len - 1)
20488 {
20489 /* If that doesn't work (not enough space), then save the string
20490 * and try again with a new buffer big enough
20491 */
20492 newbuf = vim_strnsave(*fnamep, l);
20493 if (newbuf == NULL)
20494 return 0;
20495
20496 vim_free(*bufp);
20497 *fnamep = *bufp = newbuf;
20498
20499 l = GetShortPathName(*fnamep,*fnamep,l+1);
20500
20501 /* Really should always succeed, as the buffer is big enough */
20502 }
20503
20504 *fnamelen = l;
20505 return 1;
20506}
20507
20508/*
20509 * Create a short path name. Returns the length of the buffer it needs.
20510 * Doesn't copy over the end of the buffer passed in.
20511 */
20512 static int
20513shortpath_for_invalid_fname(fname, bufp, fnamelen)
20514 char_u **fname;
20515 char_u **bufp;
20516 int *fnamelen;
20517{
20518 char_u *s, *p, *pbuf2, *pbuf3;
20519 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020520 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521
20522 /* Make a copy */
20523 len2 = *fnamelen;
20524 pbuf2 = vim_strnsave(*fname, len2);
20525 pbuf3 = NULL;
20526
20527 s = pbuf2 + len2 - 1; /* Find the end */
20528 slen = 1;
20529 plen = len2;
20530
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020531 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 {
20533 --s;
20534 ++slen;
20535 --plen;
20536 }
20537
20538 do
20539 {
20540 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020541 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 {
20543 --s;
20544 ++slen;
20545 --plen;
20546 }
20547 if (s <= pbuf2)
20548 break;
20549
20550 /* Remeber the character that is about to be blatted */
20551 ch = *s;
20552 *s = 0; /* get_short_pathname requires a null-terminated string */
20553
20554 /* Try it in situ */
20555 p = pbuf2;
20556 if (!get_short_pathname(&p, &pbuf3, &plen))
20557 {
20558 vim_free(pbuf2);
20559 return -1;
20560 }
20561 *s = ch; /* Preserve the string */
20562 } while (plen == 0);
20563
20564 if (plen > 0)
20565 {
20566 /* Remeber the length of the new string. */
20567 *fnamelen = len = plen + slen;
20568 vim_free(*bufp);
20569 if (len > len2)
20570 {
20571 /* If there's not enough space in the currently allocated string,
20572 * then copy it to a buffer big enough.
20573 */
20574 *fname= *bufp = vim_strnsave(p, len);
20575 if (*fname == NULL)
20576 return -1;
20577 }
20578 else
20579 {
20580 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20581 *fname = *bufp = pbuf2;
20582 if (p != pbuf2)
20583 strncpy(*fname, p, plen);
20584 pbuf2 = NULL;
20585 }
20586 /* Concat the next bit */
20587 strncpy(*fname + plen, s, slen);
20588 (*fname)[len] = '\0';
20589 }
20590 vim_free(pbuf3);
20591 vim_free(pbuf2);
20592 return 0;
20593}
20594
20595/*
20596 * Get a pathname for a partial path.
20597 */
20598 static int
20599shortpath_for_partial(fnamep, bufp, fnamelen)
20600 char_u **fnamep;
20601 char_u **bufp;
20602 int *fnamelen;
20603{
20604 int sepcount, len, tflen;
20605 char_u *p;
20606 char_u *pbuf, *tfname;
20607 int hasTilde;
20608
20609 /* Count up the path seperators from the RHS.. so we know which part
20610 * of the path to return.
20611 */
20612 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020613 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 if (vim_ispathsep(*p))
20615 ++sepcount;
20616
20617 /* Need full path first (use expand_env() to remove a "~/") */
20618 hasTilde = (**fnamep == '~');
20619 if (hasTilde)
20620 pbuf = tfname = expand_env_save(*fnamep);
20621 else
20622 pbuf = tfname = FullName_save(*fnamep, FALSE);
20623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020624 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625
20626 if (!get_short_pathname(&tfname, &pbuf, &len))
20627 return -1;
20628
20629 if (len == 0)
20630 {
20631 /* Don't have a valid filename, so shorten the rest of the
20632 * path if we can. This CAN give us invalid 8.3 filenames, but
20633 * there's not a lot of point in guessing what it might be.
20634 */
20635 len = tflen;
20636 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20637 return -1;
20638 }
20639
20640 /* Count the paths backward to find the beginning of the desired string. */
20641 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020642 {
20643#ifdef FEAT_MBYTE
20644 if (has_mbyte)
20645 p -= mb_head_off(tfname, p);
20646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 if (vim_ispathsep(*p))
20648 {
20649 if (sepcount == 0 || (hasTilde && sepcount == 1))
20650 break;
20651 else
20652 sepcount --;
20653 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 if (hasTilde)
20656 {
20657 --p;
20658 if (p >= tfname)
20659 *p = '~';
20660 else
20661 return -1;
20662 }
20663 else
20664 ++p;
20665
20666 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20667 vim_free(*bufp);
20668 *fnamelen = (int)STRLEN(p);
20669 *bufp = pbuf;
20670 *fnamep = p;
20671
20672 return 0;
20673}
20674#endif /* WIN3264 */
20675
20676/*
20677 * Adjust a filename, according to a string of modifiers.
20678 * *fnamep must be NUL terminated when called. When returning, the length is
20679 * determined by *fnamelen.
20680 * Returns valid flags.
20681 * When there is an error, *fnamep is set to NULL.
20682 */
20683 int
20684modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20685 char_u *src; /* string with modifiers */
20686 int *usedlen; /* characters after src that are used */
20687 char_u **fnamep; /* file name so far */
20688 char_u **bufp; /* buffer for allocated file name or NULL */
20689 int *fnamelen; /* length of fnamep */
20690{
20691 int valid = 0;
20692 char_u *tail;
20693 char_u *s, *p, *pbuf;
20694 char_u dirname[MAXPATHL];
20695 int c;
20696 int has_fullname = 0;
20697#ifdef WIN3264
20698 int has_shortname = 0;
20699#endif
20700
20701repeat:
20702 /* ":p" - full path/file_name */
20703 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20704 {
20705 has_fullname = 1;
20706
20707 valid |= VALID_PATH;
20708 *usedlen += 2;
20709
20710 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20711 if ((*fnamep)[0] == '~'
20712#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20713 && ((*fnamep)[1] == '/'
20714# ifdef BACKSLASH_IN_FILENAME
20715 || (*fnamep)[1] == '\\'
20716# endif
20717 || (*fnamep)[1] == NUL)
20718
20719#endif
20720 )
20721 {
20722 *fnamep = expand_env_save(*fnamep);
20723 vim_free(*bufp); /* free any allocated file name */
20724 *bufp = *fnamep;
20725 if (*fnamep == NULL)
20726 return -1;
20727 }
20728
20729 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020730 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 {
20732 if (vim_ispathsep(*p)
20733 && p[1] == '.'
20734 && (p[2] == NUL
20735 || vim_ispathsep(p[2])
20736 || (p[2] == '.'
20737 && (p[3] == NUL || vim_ispathsep(p[3])))))
20738 break;
20739 }
20740
20741 /* FullName_save() is slow, don't use it when not needed. */
20742 if (*p != NUL || !vim_isAbsName(*fnamep))
20743 {
20744 *fnamep = FullName_save(*fnamep, *p != NUL);
20745 vim_free(*bufp); /* free any allocated file name */
20746 *bufp = *fnamep;
20747 if (*fnamep == NULL)
20748 return -1;
20749 }
20750
20751 /* Append a path separator to a directory. */
20752 if (mch_isdir(*fnamep))
20753 {
20754 /* Make room for one or two extra characters. */
20755 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20756 vim_free(*bufp); /* free any allocated file name */
20757 *bufp = *fnamep;
20758 if (*fnamep == NULL)
20759 return -1;
20760 add_pathsep(*fnamep);
20761 }
20762 }
20763
20764 /* ":." - path relative to the current directory */
20765 /* ":~" - path relative to the home directory */
20766 /* ":8" - shortname path - postponed till after */
20767 while (src[*usedlen] == ':'
20768 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20769 {
20770 *usedlen += 2;
20771 if (c == '8')
20772 {
20773#ifdef WIN3264
20774 has_shortname = 1; /* Postpone this. */
20775#endif
20776 continue;
20777 }
20778 pbuf = NULL;
20779 /* Need full path first (use expand_env() to remove a "~/") */
20780 if (!has_fullname)
20781 {
20782 if (c == '.' && **fnamep == '~')
20783 p = pbuf = expand_env_save(*fnamep);
20784 else
20785 p = pbuf = FullName_save(*fnamep, FALSE);
20786 }
20787 else
20788 p = *fnamep;
20789
20790 has_fullname = 0;
20791
20792 if (p != NULL)
20793 {
20794 if (c == '.')
20795 {
20796 mch_dirname(dirname, MAXPATHL);
20797 s = shorten_fname(p, dirname);
20798 if (s != NULL)
20799 {
20800 *fnamep = s;
20801 if (pbuf != NULL)
20802 {
20803 vim_free(*bufp); /* free any allocated file name */
20804 *bufp = pbuf;
20805 pbuf = NULL;
20806 }
20807 }
20808 }
20809 else
20810 {
20811 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20812 /* Only replace it when it starts with '~' */
20813 if (*dirname == '~')
20814 {
20815 s = vim_strsave(dirname);
20816 if (s != NULL)
20817 {
20818 *fnamep = s;
20819 vim_free(*bufp);
20820 *bufp = s;
20821 }
20822 }
20823 }
20824 vim_free(pbuf);
20825 }
20826 }
20827
20828 tail = gettail(*fnamep);
20829 *fnamelen = (int)STRLEN(*fnamep);
20830
20831 /* ":h" - head, remove "/file_name", can be repeated */
20832 /* Don't remove the first "/" or "c:\" */
20833 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20834 {
20835 valid |= VALID_HEAD;
20836 *usedlen += 2;
20837 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020838 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 --tail;
20840 *fnamelen = (int)(tail - *fnamep);
20841#ifdef VMS
20842 if (*fnamelen > 0)
20843 *fnamelen += 1; /* the path separator is part of the path */
20844#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020845 while (tail > s && !after_pathsep(s, tail))
20846 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 }
20848
20849 /* ":8" - shortname */
20850 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20851 {
20852 *usedlen += 2;
20853#ifdef WIN3264
20854 has_shortname = 1;
20855#endif
20856 }
20857
20858#ifdef WIN3264
20859 /* Check shortname after we have done 'heads' and before we do 'tails'
20860 */
20861 if (has_shortname)
20862 {
20863 pbuf = NULL;
20864 /* Copy the string if it is shortened by :h */
20865 if (*fnamelen < (int)STRLEN(*fnamep))
20866 {
20867 p = vim_strnsave(*fnamep, *fnamelen);
20868 if (p == 0)
20869 return -1;
20870 vim_free(*bufp);
20871 *bufp = *fnamep = p;
20872 }
20873
20874 /* Split into two implementations - makes it easier. First is where
20875 * there isn't a full name already, second is where there is.
20876 */
20877 if (!has_fullname && !vim_isAbsName(*fnamep))
20878 {
20879 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20880 return -1;
20881 }
20882 else
20883 {
20884 int l;
20885
20886 /* Simple case, already have the full-name
20887 * Nearly always shorter, so try first time. */
20888 l = *fnamelen;
20889 if (!get_short_pathname(fnamep, bufp, &l))
20890 return -1;
20891
20892 if (l == 0)
20893 {
20894 /* Couldn't find the filename.. search the paths.
20895 */
20896 l = *fnamelen;
20897 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20898 return -1;
20899 }
20900 *fnamelen = l;
20901 }
20902 }
20903#endif /* WIN3264 */
20904
20905 /* ":t" - tail, just the basename */
20906 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20907 {
20908 *usedlen += 2;
20909 *fnamelen -= (int)(tail - *fnamep);
20910 *fnamep = tail;
20911 }
20912
20913 /* ":e" - extension, can be repeated */
20914 /* ":r" - root, without extension, can be repeated */
20915 while (src[*usedlen] == ':'
20916 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20917 {
20918 /* find a '.' in the tail:
20919 * - for second :e: before the current fname
20920 * - otherwise: The last '.'
20921 */
20922 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20923 s = *fnamep - 2;
20924 else
20925 s = *fnamep + *fnamelen - 1;
20926 for ( ; s > tail; --s)
20927 if (s[0] == '.')
20928 break;
20929 if (src[*usedlen + 1] == 'e') /* :e */
20930 {
20931 if (s > tail)
20932 {
20933 *fnamelen += (int)(*fnamep - (s + 1));
20934 *fnamep = s + 1;
20935#ifdef VMS
20936 /* cut version from the extension */
20937 s = *fnamep + *fnamelen - 1;
20938 for ( ; s > *fnamep; --s)
20939 if (s[0] == ';')
20940 break;
20941 if (s > *fnamep)
20942 *fnamelen = s - *fnamep;
20943#endif
20944 }
20945 else if (*fnamep <= tail)
20946 *fnamelen = 0;
20947 }
20948 else /* :r */
20949 {
20950 if (s > tail) /* remove one extension */
20951 *fnamelen = (int)(s - *fnamep);
20952 }
20953 *usedlen += 2;
20954 }
20955
20956 /* ":s?pat?foo?" - substitute */
20957 /* ":gs?pat?foo?" - global substitute */
20958 if (src[*usedlen] == ':'
20959 && (src[*usedlen + 1] == 's'
20960 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20961 {
20962 char_u *str;
20963 char_u *pat;
20964 char_u *sub;
20965 int sep;
20966 char_u *flags;
20967 int didit = FALSE;
20968
20969 flags = (char_u *)"";
20970 s = src + *usedlen + 2;
20971 if (src[*usedlen + 1] == 'g')
20972 {
20973 flags = (char_u *)"g";
20974 ++s;
20975 }
20976
20977 sep = *s++;
20978 if (sep)
20979 {
20980 /* find end of pattern */
20981 p = vim_strchr(s, sep);
20982 if (p != NULL)
20983 {
20984 pat = vim_strnsave(s, (int)(p - s));
20985 if (pat != NULL)
20986 {
20987 s = p + 1;
20988 /* find end of substitution */
20989 p = vim_strchr(s, sep);
20990 if (p != NULL)
20991 {
20992 sub = vim_strnsave(s, (int)(p - s));
20993 str = vim_strnsave(*fnamep, *fnamelen);
20994 if (sub != NULL && str != NULL)
20995 {
20996 *usedlen = (int)(p + 1 - src);
20997 s = do_string_sub(str, pat, sub, flags);
20998 if (s != NULL)
20999 {
21000 *fnamep = s;
21001 *fnamelen = (int)STRLEN(s);
21002 vim_free(*bufp);
21003 *bufp = s;
21004 didit = TRUE;
21005 }
21006 }
21007 vim_free(sub);
21008 vim_free(str);
21009 }
21010 vim_free(pat);
21011 }
21012 }
21013 /* after using ":s", repeat all the modifiers */
21014 if (didit)
21015 goto repeat;
21016 }
21017 }
21018
21019 return valid;
21020}
21021
21022/*
21023 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21024 * "flags" can be "g" to do a global substitute.
21025 * Returns an allocated string, NULL for error.
21026 */
21027 char_u *
21028do_string_sub(str, pat, sub, flags)
21029 char_u *str;
21030 char_u *pat;
21031 char_u *sub;
21032 char_u *flags;
21033{
21034 int sublen;
21035 regmatch_T regmatch;
21036 int i;
21037 int do_all;
21038 char_u *tail;
21039 garray_T ga;
21040 char_u *ret;
21041 char_u *save_cpo;
21042
21043 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21044 save_cpo = p_cpo;
21045 p_cpo = (char_u *)"";
21046
21047 ga_init2(&ga, 1, 200);
21048
21049 do_all = (flags[0] == 'g');
21050
21051 regmatch.rm_ic = p_ic;
21052 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21053 if (regmatch.regprog != NULL)
21054 {
21055 tail = str;
21056 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21057 {
21058 /*
21059 * Get some space for a temporary buffer to do the substitution
21060 * into. It will contain:
21061 * - The text up to where the match is.
21062 * - The substituted text.
21063 * - The text after the match.
21064 */
21065 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21066 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21067 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21068 {
21069 ga_clear(&ga);
21070 break;
21071 }
21072
21073 /* copy the text up to where the match is */
21074 i = (int)(regmatch.startp[0] - tail);
21075 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21076 /* add the substituted text */
21077 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21078 + ga.ga_len + i, TRUE, TRUE, FALSE);
21079 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 /* avoid getting stuck on a match with an empty string */
21081 if (tail == regmatch.endp[0])
21082 {
21083 if (*tail == NUL)
21084 break;
21085 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21086 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 }
21088 else
21089 {
21090 tail = regmatch.endp[0];
21091 if (*tail == NUL)
21092 break;
21093 }
21094 if (!do_all)
21095 break;
21096 }
21097
21098 if (ga.ga_data != NULL)
21099 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21100
21101 vim_free(regmatch.regprog);
21102 }
21103
21104 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21105 ga_clear(&ga);
21106 p_cpo = save_cpo;
21107
21108 return ret;
21109}
21110
21111#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */