blob: 273ac4136052213a72fbcc154bfcfee5a50d59ce [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
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009795 /* Position the cursor. Needed after a message that ends in a space. */
9796 windgoto(msg_row, msg_col);
9797
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 ++no_mapping;
9799 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009800 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 /* getchar(): blocking wait. */
9802 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 /* getchar(1): only check if char avail */
9805 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009806 else if (error || vpeekc() == NUL)
9807 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 n = 0;
9809 else
9810 /* getchar(0) and char avail: return char */
9811 n = safe_vgetc();
9812 --no_mapping;
9813 --allow_keys;
9814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 if (IS_SPECIAL(n) || mod_mask != 0)
9817 {
9818 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9819 int i = 0;
9820
9821 /* Turn a special key into three bytes, plus modifier. */
9822 if (mod_mask != 0)
9823 {
9824 temp[i++] = K_SPECIAL;
9825 temp[i++] = KS_MODIFIER;
9826 temp[i++] = mod_mask;
9827 }
9828 if (IS_SPECIAL(n))
9829 {
9830 temp[i++] = K_SPECIAL;
9831 temp[i++] = K_SECOND(n);
9832 temp[i++] = K_THIRD(n);
9833 }
9834#ifdef FEAT_MBYTE
9835 else if (has_mbyte)
9836 i += (*mb_char2bytes)(n, temp + i);
9837#endif
9838 else
9839 temp[i++] = n;
9840 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->v_type = VAR_STRING;
9842 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
9844}
9845
9846/*
9847 * "getcharmod()" function
9848 */
9849/*ARGSUSED*/
9850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009852 typval_T *argvars;
9853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
9859 * "getcmdline()" function
9860 */
9861/*ARGSUSED*/
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->v_type = VAR_STRING;
9868 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869}
9870
9871/*
9872 * "getcmdpos()" function
9873 */
9874/*ARGSUSED*/
9875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 typval_T *argvars;
9878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881}
9882
9883/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009884 * "getcmdtype()" function
9885 */
9886/*ARGSUSED*/
9887 static void
9888f_getcmdtype(argvars, rettv)
9889 typval_T *argvars;
9890 typval_T *rettv;
9891{
9892 rettv->v_type = VAR_STRING;
9893 rettv->vval.v_string = alloc(2);
9894 if (rettv->vval.v_string != NULL)
9895 {
9896 rettv->vval.v_string[0] = get_cmdline_type();
9897 rettv->vval.v_string[1] = NUL;
9898 }
9899}
9900
9901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 * "getcwd()" function
9903 */
9904/*ARGSUSED*/
9905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009907 typval_T *argvars;
9908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
9910 char_u cwd[MAXPATHL];
9911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 else
9916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009919 if (rettv->vval.v_string != NULL)
9920 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921#endif
9922 }
9923}
9924
9925/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009926 * "getfontname()" function
9927 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009928/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 typval_T *argvars;
9932 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009933{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
9935 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009936#ifdef FEAT_GUI
9937 if (gui.in_use)
9938 {
9939 GuiFont font;
9940 char_u *name = NULL;
9941
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009942 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009943 {
9944 /* Get the "Normal" font. Either the name saved by
9945 * hl_set_font_name() or from the font ID. */
9946 font = gui.norm_font;
9947 name = hl_get_font_name();
9948 }
9949 else
9950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009952 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9953 return;
9954 font = gui_mch_get_font(name, FALSE);
9955 if (font == NOFONT)
9956 return; /* Invalid font name, return empty string. */
9957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009959 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009960 gui_mch_free_font(font);
9961 }
9962#endif
9963}
9964
9965/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009966 * "getfperm({fname})" function
9967 */
9968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009970 typval_T *argvars;
9971 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009972{
9973 char_u *fname;
9974 struct stat st;
9975 char_u *perm = NULL;
9976 char_u flags[] = "rwx";
9977 int i;
9978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009980
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009982 if (mch_stat((char *)fname, &st) >= 0)
9983 {
9984 perm = vim_strsave((char_u *)"---------");
9985 if (perm != NULL)
9986 {
9987 for (i = 0; i < 9; i++)
9988 {
9989 if (st.st_mode & (1 << (8 - i)))
9990 perm[i] = flags[i % 3];
9991 }
9992 }
9993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009995}
9996
9997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 * "getfsize({fname})" function
9999 */
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 char_u *fname;
10006 struct stat st;
10007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
10012 if (mch_stat((char *)fname, &st) >= 0)
10013 {
10014 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 }
10019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
10023/*
10024 * "getftime({fname})" function
10025 */
10026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 char_u *fname;
10032 struct stat st;
10033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035
10036 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
10041
10042/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010043 * "getftype({fname})" function
10044 */
10045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010047 typval_T *argvars;
10048 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010049{
10050 char_u *fname;
10051 struct stat st;
10052 char_u *type = NULL;
10053 char *t;
10054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010058 if (mch_lstat((char *)fname, &st) >= 0)
10059 {
10060#ifdef S_ISREG
10061 if (S_ISREG(st.st_mode))
10062 t = "file";
10063 else if (S_ISDIR(st.st_mode))
10064 t = "dir";
10065# ifdef S_ISLNK
10066 else if (S_ISLNK(st.st_mode))
10067 t = "link";
10068# endif
10069# ifdef S_ISBLK
10070 else if (S_ISBLK(st.st_mode))
10071 t = "bdev";
10072# endif
10073# ifdef S_ISCHR
10074 else if (S_ISCHR(st.st_mode))
10075 t = "cdev";
10076# endif
10077# ifdef S_ISFIFO
10078 else if (S_ISFIFO(st.st_mode))
10079 t = "fifo";
10080# endif
10081# ifdef S_ISSOCK
10082 else if (S_ISSOCK(st.st_mode))
10083 t = "fifo";
10084# endif
10085 else
10086 t = "other";
10087#else
10088# ifdef S_IFMT
10089 switch (st.st_mode & S_IFMT)
10090 {
10091 case S_IFREG: t = "file"; break;
10092 case S_IFDIR: t = "dir"; break;
10093# ifdef S_IFLNK
10094 case S_IFLNK: t = "link"; break;
10095# endif
10096# ifdef S_IFBLK
10097 case S_IFBLK: t = "bdev"; break;
10098# endif
10099# ifdef S_IFCHR
10100 case S_IFCHR: t = "cdev"; break;
10101# endif
10102# ifdef S_IFIFO
10103 case S_IFIFO: t = "fifo"; break;
10104# endif
10105# ifdef S_IFSOCK
10106 case S_IFSOCK: t = "socket"; break;
10107# endif
10108 default: t = "other";
10109 }
10110# else
10111 if (mch_isdir(fname))
10112 t = "dir";
10113 else
10114 t = "file";
10115# endif
10116#endif
10117 type = vim_strsave((char_u *)t);
10118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010120}
10121
10122/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010123 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010124 */
10125 static void
10126f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010127 typval_T *argvars;
10128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010129{
10130 linenr_T lnum;
10131 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010132 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010133
10134 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010135 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010136 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010137 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010138 retlist = FALSE;
10139 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010140 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010141 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010142 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010143 retlist = TRUE;
10144 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010145
Bram Moolenaar342337a2005-07-21 21:11:17 +000010146 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010147}
10148
10149/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010150 * "getpos(string)" function
10151 */
10152 static void
10153f_getpos(argvars, rettv)
10154 typval_T *argvars;
10155 typval_T *rettv;
10156{
10157 pos_T *fp;
10158 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010159 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010160
10161 if (rettv_list_alloc(rettv) == OK)
10162 {
10163 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010164 fp = var2fpos(&argvars[0], TRUE, &fnum);
10165 if (fnum != -1)
10166 list_append_number(l, (varnumber_T)fnum);
10167 else
10168 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010169 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10170 : (varnumber_T)0);
10171 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10172 : (varnumber_T)0);
10173 list_append_number(l,
10174#ifdef FEAT_VIRTUALEDIT
10175 (fp != NULL) ? (varnumber_T)fp->coladd :
10176#endif
10177 (varnumber_T)0);
10178 }
10179 else
10180 rettv->vval.v_number = FALSE;
10181}
10182
10183/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010184 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010185 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010186/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010187 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010188f_getqflist(argvars, rettv)
10189 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010190 typval_T *rettv;
10191{
10192#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010193 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010194#endif
10195
10196 rettv->vval.v_number = FALSE;
10197#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010198 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010199 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010200 wp = NULL;
10201 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10202 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010203 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010204 if (wp == NULL)
10205 return;
10206 }
10207
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010208 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010209 }
10210#endif
10211}
10212
10213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 * "getreg()" function
10215 */
10216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 typval_T *argvars;
10219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221 char_u *strregname;
10222 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010223 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010226 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 strregname = get_tv_string_chk(&argvars[0]);
10229 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010230 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010234 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 regname = (strregname == NULL ? '"' : *strregname);
10236 if (regname == 0)
10237 regname = '"';
10238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 rettv->vval.v_string = error ? NULL :
10241 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242}
10243
10244/*
10245 * "getregtype()" function
10246 */
10247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 typval_T *argvars;
10250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251{
10252 char_u *strregname;
10253 int regname;
10254 char_u buf[NUMBUFLEN + 2];
10255 long reglen = 0;
10256
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010257 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010258 {
10259 strregname = get_tv_string_chk(&argvars[0]);
10260 if (strregname == NULL) /* type error; errmsg already given */
10261 {
10262 rettv->v_type = VAR_STRING;
10263 rettv->vval.v_string = NULL;
10264 return;
10265 }
10266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 else
10268 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010269 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270
10271 regname = (strregname == NULL ? '"' : *strregname);
10272 if (regname == 0)
10273 regname = '"';
10274
10275 buf[0] = NUL;
10276 buf[1] = NUL;
10277 switch (get_reg_type(regname, &reglen))
10278 {
10279 case MLINE: buf[0] = 'V'; break;
10280 case MCHAR: buf[0] = 'v'; break;
10281#ifdef FEAT_VISUAL
10282 case MBLOCK:
10283 buf[0] = Ctrl_V;
10284 sprintf((char *)buf + 1, "%ld", reglen + 1);
10285 break;
10286#endif
10287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 rettv->v_type = VAR_STRING;
10289 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290}
10291
10292/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010293 * "gettabwinvar()" function
10294 */
10295 static void
10296f_gettabwinvar(argvars, rettv)
10297 typval_T *argvars;
10298 typval_T *rettv;
10299{
10300 getwinvar(argvars, rettv, 1);
10301}
10302
10303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 * "getwinposx()" function
10305 */
10306/*ARGSUSED*/
10307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010309 typval_T *argvars;
10310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313#ifdef FEAT_GUI
10314 if (gui.in_use)
10315 {
10316 int x, y;
10317
10318 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010319 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 }
10321#endif
10322}
10323
10324/*
10325 * "getwinposy()" function
10326 */
10327/*ARGSUSED*/
10328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010330 typval_T *argvars;
10331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334#ifdef FEAT_GUI
10335 if (gui.in_use)
10336 {
10337 int x, y;
10338
10339 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010340 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 }
10342#endif
10343}
10344
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010345/*
10346 * Find window specifed by "vp" in tabpage "tp".
10347 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010348 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010349find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010350 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010351 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010352{
10353#ifdef FEAT_WINDOWS
10354 win_T *wp;
10355#endif
10356 int nr;
10357
10358 nr = get_tv_number_chk(vp, NULL);
10359
10360#ifdef FEAT_WINDOWS
10361 if (nr < 0)
10362 return NULL;
10363 if (nr == 0)
10364 return curwin;
10365
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010366 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10367 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010368 if (--nr <= 0)
10369 break;
10370 return wp;
10371#else
10372 if (nr == 0 || nr == 1)
10373 return curwin;
10374 return NULL;
10375#endif
10376}
10377
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378/*
10379 * "getwinvar()" function
10380 */
10381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010383 typval_T *argvars;
10384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010386 getwinvar(argvars, rettv, 0);
10387}
10388
10389/*
10390 * getwinvar() and gettabwinvar()
10391 */
10392 static void
10393getwinvar(argvars, rettv, off)
10394 typval_T *argvars;
10395 typval_T *rettv;
10396 int off; /* 1 for gettabwinvar() */
10397{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 win_T *win, *oldcurwin;
10399 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010401 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010403#ifdef FEAT_WINDOWS
10404 if (off == 1)
10405 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10406 else
10407 tp = curtab;
10408#endif
10409 win = find_win_by_nr(&argvars[off], tp);
10410 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 rettv->v_type = VAR_STRING;
10414 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415
10416 if (win != NULL && varname != NULL)
10417 {
10418 if (*varname == '&') /* window-local-option */
10419 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010420 /* Set curwin to be our win, temporarily. Also set curbuf, so
10421 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 oldcurwin = curwin;
10423 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010424 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427
10428 /* restore previous notion of curwin */
10429 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010430 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 }
10432 else
10433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 if (*varname == NUL)
10435 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10436 * scope prefix before the NUL byte is required by
10437 * find_var_in_ht(). */
10438 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010440 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 }
10444 }
10445
10446 --emsg_off;
10447}
10448
10449/*
10450 * "glob()" function
10451 */
10452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010454 typval_T *argvars;
10455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
10457 expand_T xpc;
10458
10459 ExpandInit(&xpc);
10460 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 rettv->v_type = VAR_STRING;
10462 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
10467 * "globpath()" function
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
10474 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 if (file == NULL)
10479 rettv->vval.v_string = NULL;
10480 else
10481 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482}
10483
10484/*
10485 * "has()" function
10486 */
10487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010488f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 int i;
10493 char_u *name;
10494 int n = FALSE;
10495 static char *(has_list[]) =
10496 {
10497#ifdef AMIGA
10498 "amiga",
10499# ifdef FEAT_ARP
10500 "arp",
10501# endif
10502#endif
10503#ifdef __BEOS__
10504 "beos",
10505#endif
10506#ifdef MSDOS
10507# ifdef DJGPP
10508 "dos32",
10509# else
10510 "dos16",
10511# endif
10512#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010513#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 "mac",
10515#endif
10516#if defined(MACOS_X_UNIX)
10517 "macunix",
10518#endif
10519#ifdef OS2
10520 "os2",
10521#endif
10522#ifdef __QNX__
10523 "qnx",
10524#endif
10525#ifdef RISCOS
10526 "riscos",
10527#endif
10528#ifdef UNIX
10529 "unix",
10530#endif
10531#ifdef VMS
10532 "vms",
10533#endif
10534#ifdef WIN16
10535 "win16",
10536#endif
10537#ifdef WIN32
10538 "win32",
10539#endif
10540#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10541 "win32unix",
10542#endif
10543#ifdef WIN64
10544 "win64",
10545#endif
10546#ifdef EBCDIC
10547 "ebcdic",
10548#endif
10549#ifndef CASE_INSENSITIVE_FILENAME
10550 "fname_case",
10551#endif
10552#ifdef FEAT_ARABIC
10553 "arabic",
10554#endif
10555#ifdef FEAT_AUTOCMD
10556 "autocmd",
10557#endif
10558#ifdef FEAT_BEVAL
10559 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010560# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10561 "balloon_multiline",
10562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563#endif
10564#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10565 "builtin_terms",
10566# ifdef ALL_BUILTIN_TCAPS
10567 "all_builtin_terms",
10568# endif
10569#endif
10570#ifdef FEAT_BYTEOFF
10571 "byte_offset",
10572#endif
10573#ifdef FEAT_CINDENT
10574 "cindent",
10575#endif
10576#ifdef FEAT_CLIENTSERVER
10577 "clientserver",
10578#endif
10579#ifdef FEAT_CLIPBOARD
10580 "clipboard",
10581#endif
10582#ifdef FEAT_CMDL_COMPL
10583 "cmdline_compl",
10584#endif
10585#ifdef FEAT_CMDHIST
10586 "cmdline_hist",
10587#endif
10588#ifdef FEAT_COMMENTS
10589 "comments",
10590#endif
10591#ifdef FEAT_CRYPT
10592 "cryptv",
10593#endif
10594#ifdef FEAT_CSCOPE
10595 "cscope",
10596#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010597#ifdef CURSOR_SHAPE
10598 "cursorshape",
10599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600#ifdef DEBUG
10601 "debug",
10602#endif
10603#ifdef FEAT_CON_DIALOG
10604 "dialog_con",
10605#endif
10606#ifdef FEAT_GUI_DIALOG
10607 "dialog_gui",
10608#endif
10609#ifdef FEAT_DIFF
10610 "diff",
10611#endif
10612#ifdef FEAT_DIGRAPHS
10613 "digraphs",
10614#endif
10615#ifdef FEAT_DND
10616 "dnd",
10617#endif
10618#ifdef FEAT_EMACS_TAGS
10619 "emacs_tags",
10620#endif
10621 "eval", /* always present, of course! */
10622#ifdef FEAT_EX_EXTRA
10623 "ex_extra",
10624#endif
10625#ifdef FEAT_SEARCH_EXTRA
10626 "extra_search",
10627#endif
10628#ifdef FEAT_FKMAP
10629 "farsi",
10630#endif
10631#ifdef FEAT_SEARCHPATH
10632 "file_in_path",
10633#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010634#if defined(UNIX) && !defined(USE_SYSTEM)
10635 "filterpipe",
10636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637#ifdef FEAT_FIND_ID
10638 "find_in_path",
10639#endif
10640#ifdef FEAT_FOLDING
10641 "folding",
10642#endif
10643#ifdef FEAT_FOOTER
10644 "footer",
10645#endif
10646#if !defined(USE_SYSTEM) && defined(UNIX)
10647 "fork",
10648#endif
10649#ifdef FEAT_GETTEXT
10650 "gettext",
10651#endif
10652#ifdef FEAT_GUI
10653 "gui",
10654#endif
10655#ifdef FEAT_GUI_ATHENA
10656# ifdef FEAT_GUI_NEXTAW
10657 "gui_neXtaw",
10658# else
10659 "gui_athena",
10660# endif
10661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662#ifdef FEAT_GUI_GTK
10663 "gui_gtk",
10664# ifdef HAVE_GTK2
10665 "gui_gtk2",
10666# endif
10667#endif
10668#ifdef FEAT_GUI_MAC
10669 "gui_mac",
10670#endif
10671#ifdef FEAT_GUI_MOTIF
10672 "gui_motif",
10673#endif
10674#ifdef FEAT_GUI_PHOTON
10675 "gui_photon",
10676#endif
10677#ifdef FEAT_GUI_W16
10678 "gui_win16",
10679#endif
10680#ifdef FEAT_GUI_W32
10681 "gui_win32",
10682#endif
10683#ifdef FEAT_HANGULIN
10684 "hangul_input",
10685#endif
10686#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10687 "iconv",
10688#endif
10689#ifdef FEAT_INS_EXPAND
10690 "insert_expand",
10691#endif
10692#ifdef FEAT_JUMPLIST
10693 "jumplist",
10694#endif
10695#ifdef FEAT_KEYMAP
10696 "keymap",
10697#endif
10698#ifdef FEAT_LANGMAP
10699 "langmap",
10700#endif
10701#ifdef FEAT_LIBCALL
10702 "libcall",
10703#endif
10704#ifdef FEAT_LINEBREAK
10705 "linebreak",
10706#endif
10707#ifdef FEAT_LISP
10708 "lispindent",
10709#endif
10710#ifdef FEAT_LISTCMDS
10711 "listcmds",
10712#endif
10713#ifdef FEAT_LOCALMAP
10714 "localmap",
10715#endif
10716#ifdef FEAT_MENU
10717 "menu",
10718#endif
10719#ifdef FEAT_SESSION
10720 "mksession",
10721#endif
10722#ifdef FEAT_MODIFY_FNAME
10723 "modify_fname",
10724#endif
10725#ifdef FEAT_MOUSE
10726 "mouse",
10727#endif
10728#ifdef FEAT_MOUSESHAPE
10729 "mouseshape",
10730#endif
10731#if defined(UNIX) || defined(VMS)
10732# ifdef FEAT_MOUSE_DEC
10733 "mouse_dec",
10734# endif
10735# ifdef FEAT_MOUSE_GPM
10736 "mouse_gpm",
10737# endif
10738# ifdef FEAT_MOUSE_JSB
10739 "mouse_jsbterm",
10740# endif
10741# ifdef FEAT_MOUSE_NET
10742 "mouse_netterm",
10743# endif
10744# ifdef FEAT_MOUSE_PTERM
10745 "mouse_pterm",
10746# endif
10747# ifdef FEAT_MOUSE_XTERM
10748 "mouse_xterm",
10749# endif
10750#endif
10751#ifdef FEAT_MBYTE
10752 "multi_byte",
10753#endif
10754#ifdef FEAT_MBYTE_IME
10755 "multi_byte_ime",
10756#endif
10757#ifdef FEAT_MULTI_LANG
10758 "multi_lang",
10759#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010760#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010761#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010762 "mzscheme",
10763#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765#ifdef FEAT_OLE
10766 "ole",
10767#endif
10768#ifdef FEAT_OSFILETYPE
10769 "osfiletype",
10770#endif
10771#ifdef FEAT_PATH_EXTRA
10772 "path_extra",
10773#endif
10774#ifdef FEAT_PERL
10775#ifndef DYNAMIC_PERL
10776 "perl",
10777#endif
10778#endif
10779#ifdef FEAT_PYTHON
10780#ifndef DYNAMIC_PYTHON
10781 "python",
10782#endif
10783#endif
10784#ifdef FEAT_POSTSCRIPT
10785 "postscript",
10786#endif
10787#ifdef FEAT_PRINTER
10788 "printer",
10789#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010790#ifdef FEAT_PROFILE
10791 "profile",
10792#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010793#ifdef FEAT_RELTIME
10794 "reltime",
10795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796#ifdef FEAT_QUICKFIX
10797 "quickfix",
10798#endif
10799#ifdef FEAT_RIGHTLEFT
10800 "rightleft",
10801#endif
10802#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10803 "ruby",
10804#endif
10805#ifdef FEAT_SCROLLBIND
10806 "scrollbind",
10807#endif
10808#ifdef FEAT_CMDL_INFO
10809 "showcmd",
10810 "cmdline_info",
10811#endif
10812#ifdef FEAT_SIGNS
10813 "signs",
10814#endif
10815#ifdef FEAT_SMARTINDENT
10816 "smartindent",
10817#endif
10818#ifdef FEAT_SNIFF
10819 "sniff",
10820#endif
10821#ifdef FEAT_STL_OPT
10822 "statusline",
10823#endif
10824#ifdef FEAT_SUN_WORKSHOP
10825 "sun_workshop",
10826#endif
10827#ifdef FEAT_NETBEANS_INTG
10828 "netbeans_intg",
10829#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010830#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010831 "spell",
10832#endif
10833#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 "syntax",
10835#endif
10836#if defined(USE_SYSTEM) || !defined(UNIX)
10837 "system",
10838#endif
10839#ifdef FEAT_TAG_BINS
10840 "tag_binary",
10841#endif
10842#ifdef FEAT_TAG_OLDSTATIC
10843 "tag_old_static",
10844#endif
10845#ifdef FEAT_TAG_ANYWHITE
10846 "tag_any_white",
10847#endif
10848#ifdef FEAT_TCL
10849# ifndef DYNAMIC_TCL
10850 "tcl",
10851# endif
10852#endif
10853#ifdef TERMINFO
10854 "terminfo",
10855#endif
10856#ifdef FEAT_TERMRESPONSE
10857 "termresponse",
10858#endif
10859#ifdef FEAT_TEXTOBJ
10860 "textobjects",
10861#endif
10862#ifdef HAVE_TGETENT
10863 "tgetent",
10864#endif
10865#ifdef FEAT_TITLE
10866 "title",
10867#endif
10868#ifdef FEAT_TOOLBAR
10869 "toolbar",
10870#endif
10871#ifdef FEAT_USR_CMDS
10872 "user-commands", /* was accidentally included in 5.4 */
10873 "user_commands",
10874#endif
10875#ifdef FEAT_VIMINFO
10876 "viminfo",
10877#endif
10878#ifdef FEAT_VERTSPLIT
10879 "vertsplit",
10880#endif
10881#ifdef FEAT_VIRTUALEDIT
10882 "virtualedit",
10883#endif
10884#ifdef FEAT_VISUAL
10885 "visual",
10886#endif
10887#ifdef FEAT_VISUALEXTRA
10888 "visualextra",
10889#endif
10890#ifdef FEAT_VREPLACE
10891 "vreplace",
10892#endif
10893#ifdef FEAT_WILDIGN
10894 "wildignore",
10895#endif
10896#ifdef FEAT_WILDMENU
10897 "wildmenu",
10898#endif
10899#ifdef FEAT_WINDOWS
10900 "windows",
10901#endif
10902#ifdef FEAT_WAK
10903 "winaltkeys",
10904#endif
10905#ifdef FEAT_WRITEBACKUP
10906 "writebackup",
10907#endif
10908#ifdef FEAT_XIM
10909 "xim",
10910#endif
10911#ifdef FEAT_XFONTSET
10912 "xfontset",
10913#endif
10914#ifdef USE_XSMP
10915 "xsmp",
10916#endif
10917#ifdef USE_XSMP_INTERACT
10918 "xsmp_interact",
10919#endif
10920#ifdef FEAT_XCLIPBOARD
10921 "xterm_clipboard",
10922#endif
10923#ifdef FEAT_XTERM_SAVE
10924 "xterm_save",
10925#endif
10926#if defined(UNIX) && defined(FEAT_X11)
10927 "X11",
10928#endif
10929 NULL
10930 };
10931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 for (i = 0; has_list[i] != NULL; ++i)
10934 if (STRICMP(name, has_list[i]) == 0)
10935 {
10936 n = TRUE;
10937 break;
10938 }
10939
10940 if (n == FALSE)
10941 {
10942 if (STRNICMP(name, "patch", 5) == 0)
10943 n = has_patch(atoi((char *)name + 5));
10944 else if (STRICMP(name, "vim_starting") == 0)
10945 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010946#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10947 else if (STRICMP(name, "balloon_multiline") == 0)
10948 n = multiline_balloon_available();
10949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950#ifdef DYNAMIC_TCL
10951 else if (STRICMP(name, "tcl") == 0)
10952 n = tcl_enabled(FALSE);
10953#endif
10954#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10955 else if (STRICMP(name, "iconv") == 0)
10956 n = iconv_enabled(FALSE);
10957#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010958#ifdef DYNAMIC_MZSCHEME
10959 else if (STRICMP(name, "mzscheme") == 0)
10960 n = mzscheme_enabled(FALSE);
10961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962#ifdef DYNAMIC_RUBY
10963 else if (STRICMP(name, "ruby") == 0)
10964 n = ruby_enabled(FALSE);
10965#endif
10966#ifdef DYNAMIC_PYTHON
10967 else if (STRICMP(name, "python") == 0)
10968 n = python_enabled(FALSE);
10969#endif
10970#ifdef DYNAMIC_PERL
10971 else if (STRICMP(name, "perl") == 0)
10972 n = perl_enabled(FALSE);
10973#endif
10974#ifdef FEAT_GUI
10975 else if (STRICMP(name, "gui_running") == 0)
10976 n = (gui.in_use || gui.starting);
10977# ifdef FEAT_GUI_W32
10978 else if (STRICMP(name, "gui_win32s") == 0)
10979 n = gui_is_win32s();
10980# endif
10981# ifdef FEAT_BROWSE
10982 else if (STRICMP(name, "browse") == 0)
10983 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10984# endif
10985#endif
10986#ifdef FEAT_SYN_HL
10987 else if (STRICMP(name, "syntax_items") == 0)
10988 n = syntax_present(curbuf);
10989#endif
10990#if defined(WIN3264)
10991 else if (STRICMP(name, "win95") == 0)
10992 n = mch_windows95();
10993#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010994#ifdef FEAT_NETBEANS_INTG
10995 else if (STRICMP(name, "netbeans_enabled") == 0)
10996 n = usingNetbeans;
10997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 }
10999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001}
11002
11003/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011004 * "has_key()" function
11005 */
11006 static void
11007f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *argvars;
11009 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010{
11011 rettv->vval.v_number = 0;
11012 if (argvars[0].v_type != VAR_DICT)
11013 {
11014 EMSG(_(e_dictreq));
11015 return;
11016 }
11017 if (argvars[0].vval.v_dict == NULL)
11018 return;
11019
11020 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011021 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022}
11023
11024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 * "hasmapto()" function
11026 */
11027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 typval_T *argvars;
11030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031{
11032 char_u *name;
11033 char_u *mode;
11034 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011035 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011038 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 mode = (char_u *)"nvo";
11040 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011043 if (argvars[2].v_type != VAR_UNKNOWN)
11044 abbr = get_tv_number(&argvars[2]);
11045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046
Bram Moolenaar2c932302006-03-18 21:42:09 +000011047 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051}
11052
11053/*
11054 * "histadd()" function
11055 */
11056/*ARGSUSED*/
11057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011059 typval_T *argvars;
11060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062#ifdef FEAT_CMDHIST
11063 int histype;
11064 char_u *str;
11065 char_u buf[NUMBUFLEN];
11066#endif
11067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 if (check_restricted() || check_secure())
11070 return;
11071#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11073 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 if (histype >= 0)
11075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 if (*str != NUL)
11078 {
11079 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 return;
11082 }
11083 }
11084#endif
11085}
11086
11087/*
11088 * "histdel()" function
11089 */
11090/*ARGSUSED*/
11091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011093 typval_T *argvars;
11094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
11096#ifdef FEAT_CMDHIST
11097 int n;
11098 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11102 if (str == NULL)
11103 n = 0;
11104 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011107 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 else
11112 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 get_tv_string_buf(&argvars[1], buf));
11115 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118#endif
11119}
11120
11121/*
11122 * "histget()" function
11123 */
11124/*ARGSUSED*/
11125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011127 typval_T *argvars;
11128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129{
11130#ifdef FEAT_CMDHIST
11131 int type;
11132 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11136 if (str == NULL)
11137 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 {
11140 type = get_histtype(str);
11141 if (argvars[1].v_type == VAR_UNKNOWN)
11142 idx = get_history_idx(type);
11143 else
11144 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11145 /* -1 on type error */
11146 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152}
11153
11154/*
11155 * "histnr()" function
11156 */
11157/*ARGSUSED*/
11158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *argvars;
11161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162{
11163 int i;
11164
11165#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 char_u *history = get_tv_string_chk(&argvars[0]);
11167
11168 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 if (i >= HIST_CMD && i < HIST_COUNT)
11170 i = get_history_idx(i);
11171 else
11172#endif
11173 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 * "highlightID(name)" function
11179 */
11180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011182 typval_T *argvars;
11183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186}
11187
11188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011189 * "highlight_exists()" function
11190 */
11191 static void
11192f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011193 typval_T *argvars;
11194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011195{
11196 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11197}
11198
11199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 * "hostname()" function
11201 */
11202/*ARGSUSED*/
11203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011205 typval_T *argvars;
11206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
11208 char_u hostname[256];
11209
11210 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->v_type = VAR_STRING;
11212 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213}
11214
11215/*
11216 * iconv() function
11217 */
11218/*ARGSUSED*/
11219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *argvars;
11222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223{
11224#ifdef FEAT_MBYTE
11225 char_u buf1[NUMBUFLEN];
11226 char_u buf2[NUMBUFLEN];
11227 char_u *from, *to, *str;
11228 vimconv_T vimconv;
11229#endif
11230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->v_type = VAR_STRING;
11232 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233
11234#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235 str = get_tv_string(&argvars[0]);
11236 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11237 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 vimconv.vc_type = CONV_NONE;
11239 convert_setup(&vimconv, from, to);
11240
11241 /* If the encodings are equal, no conversion needed. */
11242 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246
11247 convert_setup(&vimconv, NULL, NULL);
11248 vim_free(from);
11249 vim_free(to);
11250#endif
11251}
11252
11253/*
11254 * "indent()" function
11255 */
11256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011258 typval_T *argvars;
11259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260{
11261 linenr_T lnum;
11262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268}
11269
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011270/*
11271 * "index()" function
11272 */
11273 static void
11274f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 typval_T *argvars;
11276 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011277{
Bram Moolenaar33570922005-01-25 22:26:29 +000011278 list_T *l;
11279 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011280 long idx = 0;
11281 int ic = FALSE;
11282
11283 rettv->vval.v_number = -1;
11284 if (argvars[0].v_type != VAR_LIST)
11285 {
11286 EMSG(_(e_listreq));
11287 return;
11288 }
11289 l = argvars[0].vval.v_list;
11290 if (l != NULL)
11291 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011292 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011293 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011295 int error = FALSE;
11296
Bram Moolenaar758711c2005-02-02 23:11:38 +000011297 /* Start at specified item. Use the cached index that list_find()
11298 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011300 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 ic = get_tv_number_chk(&argvars[3], &error);
11303 if (error)
11304 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011305 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011306
Bram Moolenaar758711c2005-02-02 23:11:38 +000011307 for ( ; item != NULL; item = item->li_next, ++idx)
11308 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011309 {
11310 rettv->vval.v_number = idx;
11311 break;
11312 }
11313 }
11314}
11315
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316static int inputsecret_flag = 0;
11317
11318/*
11319 * "input()" function
11320 * Also handles inputsecret() when inputsecret is set.
11321 */
11322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *argvars;
11325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 char_u *p = NULL;
11329 int c;
11330 char_u buf[NUMBUFLEN];
11331 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011333 int xp_type = EXPAND_NOTHING;
11334 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337
11338#ifdef NO_CONSOLE_INPUT
11339 /* While starting up, there is no place to enter text. */
11340 if (no_console_input())
11341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 return;
11344 }
11345#endif
11346
11347 cmd_silent = FALSE; /* Want to see the prompt. */
11348 if (prompt != NULL)
11349 {
11350 /* Only the part of the message after the last NL is considered as
11351 * prompt for the command line */
11352 p = vim_strrchr(prompt, '\n');
11353 if (p == NULL)
11354 p = prompt;
11355 else
11356 {
11357 ++p;
11358 c = *p;
11359 *p = NUL;
11360 msg_start();
11361 msg_clr_eos();
11362 msg_puts_attr(prompt, echo_attr);
11363 msg_didout = FALSE;
11364 msg_starthere();
11365 *p = c;
11366 }
11367 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 if (argvars[1].v_type != VAR_UNKNOWN)
11370 {
11371 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11372 if (defstr != NULL)
11373 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374
Bram Moolenaar4463f292005-09-25 22:20:24 +000011375 if (argvars[2].v_type != VAR_UNKNOWN)
11376 {
11377 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011378 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011379 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011380
Bram Moolenaar4463f292005-09-25 22:20:24 +000011381 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011382
Bram Moolenaar4463f292005-09-25 22:20:24 +000011383 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11384 if (xp_name == NULL)
11385 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011386
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011387 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011388
Bram Moolenaar4463f292005-09-25 22:20:24 +000011389 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11390 &xp_arg) == FAIL)
11391 return;
11392 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011393 }
11394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 if (defstr != NULL)
11396 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011397 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11398 xp_type, xp_arg);
11399
11400 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 /* since the user typed this, no need to wait for return */
11403 need_wait_return = FALSE;
11404 msg_didout = FALSE;
11405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 cmd_silent = cmd_silent_save;
11407}
11408
11409/*
11410 * "inputdialog()" function
11411 */
11412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *argvars;
11415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416{
11417#if defined(FEAT_GUI_TEXTDIALOG)
11418 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11419 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11420 {
11421 char_u *message;
11422 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011423 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425 message = get_tv_string_chk(&argvars[0]);
11426 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011427 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011428 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 else
11430 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 if (message != NULL && defstr != NULL
11432 && do_dialog(VIM_QUESTION, NULL, message,
11433 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 else
11436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 if (message != NULL && defstr != NULL
11438 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011439 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_string = vim_strsave(
11441 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 }
11447 else
11448#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450}
11451
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011452/*
11453 * "inputlist()" function
11454 */
11455 static void
11456f_inputlist(argvars, rettv)
11457 typval_T *argvars;
11458 typval_T *rettv;
11459{
11460 listitem_T *li;
11461 int selected;
11462 int mouse_used;
11463
11464 rettv->vval.v_number = 0;
11465#ifdef NO_CONSOLE_INPUT
11466 /* While starting up, there is no place to enter text. */
11467 if (no_console_input())
11468 return;
11469#endif
11470 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11471 {
11472 EMSG2(_(e_listarg), "inputlist()");
11473 return;
11474 }
11475
11476 msg_start();
11477 lines_left = Rows; /* avoid more prompt */
11478 msg_scroll = TRUE;
11479 msg_clr_eos();
11480
11481 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11482 {
11483 msg_puts(get_tv_string(&li->li_tv));
11484 msg_putchar('\n');
11485 }
11486
11487 /* Ask for choice. */
11488 selected = prompt_for_number(&mouse_used);
11489 if (mouse_used)
11490 selected -= lines_left;
11491
11492 rettv->vval.v_number = selected;
11493}
11494
11495
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11497
11498/*
11499 * "inputrestore()" function
11500 */
11501/*ARGSUSED*/
11502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507 if (ga_userinput.ga_len > 0)
11508 {
11509 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11511 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513 }
11514 else if (p_verbose > 1)
11515 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011516 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519}
11520
11521/*
11522 * "inputsave()" function
11523 */
11524/*ARGSUSED*/
11525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011527 typval_T *argvars;
11528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529{
11530 /* Add an entry to the stack of typehead storage. */
11531 if (ga_grow(&ga_userinput, 1) == OK)
11532 {
11533 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11534 + ga_userinput.ga_len);
11535 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 }
11538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540}
11541
11542/*
11543 * "inputsecret()" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
11550 ++cmdline_star;
11551 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 --cmdline_star;
11554 --inputsecret_flag;
11555}
11556
11557/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011558 * "insert()" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011564{
11565 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 listitem_T *item;
11567 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011569
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011570 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011571 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011573 else if ((l = argvars[0].vval.v_list) != NULL
11574 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011575 {
11576 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 before = get_tv_number_chk(&argvars[2], &error);
11578 if (error)
11579 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011580
Bram Moolenaar758711c2005-02-02 23:11:38 +000011581 if (before == l->lv_len)
11582 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011583 else
11584 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011585 item = list_find(l, before);
11586 if (item == NULL)
11587 {
11588 EMSGN(_(e_listidx), before);
11589 l = NULL;
11590 }
11591 }
11592 if (l != NULL)
11593 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011594 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011595 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011596 }
11597 }
11598}
11599
11600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 * "isdirectory()" function
11602 */
11603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011605 typval_T *argvars;
11606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011608 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609}
11610
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011611/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011612 * "islocked()" function
11613 */
11614 static void
11615f_islocked(argvars, rettv)
11616 typval_T *argvars;
11617 typval_T *rettv;
11618{
11619 lval_T lv;
11620 char_u *end;
11621 dictitem_T *di;
11622
11623 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011624 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11625 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011626 if (end != NULL && lv.ll_name != NULL)
11627 {
11628 if (*end != NUL)
11629 EMSG(_(e_trailing));
11630 else
11631 {
11632 if (lv.ll_tv == NULL)
11633 {
11634 if (check_changedtick(lv.ll_name))
11635 rettv->vval.v_number = 1; /* always locked */
11636 else
11637 {
11638 di = find_var(lv.ll_name, NULL);
11639 if (di != NULL)
11640 {
11641 /* Consider a variable locked when:
11642 * 1. the variable itself is locked
11643 * 2. the value of the variable is locked.
11644 * 3. the List or Dict value is locked.
11645 */
11646 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11647 || tv_islocked(&di->di_tv));
11648 }
11649 }
11650 }
11651 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011652 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011653 else if (lv.ll_newkey != NULL)
11654 EMSG2(_(e_dictkey), lv.ll_newkey);
11655 else if (lv.ll_list != NULL)
11656 /* List item. */
11657 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11658 else
11659 /* Dictionary item. */
11660 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11661 }
11662 }
11663
11664 clear_lval(&lv);
11665}
11666
Bram Moolenaar33570922005-01-25 22:26:29 +000011667static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011668
11669/*
11670 * Turn a dict into a list:
11671 * "what" == 0: list of keys
11672 * "what" == 1: list of values
11673 * "what" == 2: list of items
11674 */
11675 static void
11676dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 typval_T *argvars;
11678 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011679 int what;
11680{
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 list_T *l2;
11682 dictitem_T *di;
11683 hashitem_T *hi;
11684 listitem_T *li;
11685 listitem_T *li2;
11686 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011687 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011688
11689 rettv->vval.v_number = 0;
11690 if (argvars[0].v_type != VAR_DICT)
11691 {
11692 EMSG(_(e_dictreq));
11693 return;
11694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011695 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011696 return;
11697
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011698 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011699 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011700
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011701 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011704 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011706 --todo;
11707 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011708
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011709 li = listitem_alloc();
11710 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011711 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011712 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011713
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011714 if (what == 0)
11715 {
11716 /* keys() */
11717 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011718 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011719 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11720 }
11721 else if (what == 1)
11722 {
11723 /* values() */
11724 copy_tv(&di->di_tv, &li->li_tv);
11725 }
11726 else
11727 {
11728 /* items() */
11729 l2 = list_alloc();
11730 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011731 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011732 li->li_tv.vval.v_list = l2;
11733 if (l2 == NULL)
11734 break;
11735 ++l2->lv_refcount;
11736
11737 li2 = listitem_alloc();
11738 if (li2 == NULL)
11739 break;
11740 list_append(l2, li2);
11741 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011742 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011743 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11744
11745 li2 = listitem_alloc();
11746 if (li2 == NULL)
11747 break;
11748 list_append(l2, li2);
11749 copy_tv(&di->di_tv, &li2->li_tv);
11750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011751 }
11752 }
11753}
11754
11755/*
11756 * "items(dict)" function
11757 */
11758 static void
11759f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011760 typval_T *argvars;
11761 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011762{
11763 dict_list(argvars, rettv, 2);
11764}
11765
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011767 * "join()" function
11768 */
11769 static void
11770f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011771 typval_T *argvars;
11772 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011773{
11774 garray_T ga;
11775 char_u *sep;
11776
11777 rettv->vval.v_number = 0;
11778 if (argvars[0].v_type != VAR_LIST)
11779 {
11780 EMSG(_(e_listreq));
11781 return;
11782 }
11783 if (argvars[0].vval.v_list == NULL)
11784 return;
11785 if (argvars[1].v_type == VAR_UNKNOWN)
11786 sep = (char_u *)" ";
11787 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011789
11790 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011791
11792 if (sep != NULL)
11793 {
11794 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011795 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796 ga_append(&ga, NUL);
11797 rettv->vval.v_string = (char_u *)ga.ga_data;
11798 }
11799 else
11800 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011801}
11802
11803/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011804 * "keys()" function
11805 */
11806 static void
11807f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011808 typval_T *argvars;
11809 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011810{
11811 dict_list(argvars, rettv, 0);
11812}
11813
11814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 * "last_buffer_nr()" function.
11816 */
11817/*ARGSUSED*/
11818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011819f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011820 typval_T *argvars;
11821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822{
11823 int n = 0;
11824 buf_T *buf;
11825
11826 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11827 if (n < buf->b_fnum)
11828 n = buf->b_fnum;
11829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831}
11832
11833/*
11834 * "len()" function
11835 */
11836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 typval_T *argvars;
11839 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011840{
11841 switch (argvars[0].v_type)
11842 {
11843 case VAR_STRING:
11844 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->vval.v_number = (varnumber_T)STRLEN(
11846 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 break;
11848 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011850 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011851 case VAR_DICT:
11852 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11853 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011854 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011855 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011856 break;
11857 }
11858}
11859
Bram Moolenaar33570922005-01-25 22:26:29 +000011860static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011861
11862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 typval_T *argvars;
11865 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011866 int type;
11867{
11868#ifdef FEAT_LIBCALL
11869 char_u *string_in;
11870 char_u **string_result;
11871 int nr_result;
11872#endif
11873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011875 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011879
11880 if (check_restricted() || check_secure())
11881 return;
11882
11883#ifdef FEAT_LIBCALL
11884 /* The first two args must be strings, otherwise its meaningless */
11885 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11886 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011887 string_in = NULL;
11888 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011889 string_in = argvars[2].vval.v_string;
11890 if (type == VAR_NUMBER)
11891 string_result = NULL;
11892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011894 if (mch_libcall(argvars[0].vval.v_string,
11895 argvars[1].vval.v_string,
11896 string_in,
11897 argvars[2].vval.v_number,
11898 string_result,
11899 &nr_result) == OK
11900 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011902 }
11903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904}
11905
11906/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011907 * "libcall()" function
11908 */
11909 static void
11910f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011911 typval_T *argvars;
11912 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011913{
11914 libcall_common(argvars, rettv, VAR_STRING);
11915}
11916
11917/*
11918 * "libcallnr()" function
11919 */
11920 static void
11921f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011922 typval_T *argvars;
11923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924{
11925 libcall_common(argvars, rettv, VAR_NUMBER);
11926}
11927
11928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 * "line(string)" function
11930 */
11931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011933 typval_T *argvars;
11934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935{
11936 linenr_T lnum = 0;
11937 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011938 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011940 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 if (fp != NULL)
11942 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944}
11945
11946/*
11947 * "line2byte(lnum)" function
11948 */
11949/*ARGSUSED*/
11950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *argvars;
11953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
11955#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957#else
11958 linenr_T lnum;
11959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11965 if (rettv->vval.v_number >= 0)
11966 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#endif
11968}
11969
11970/*
11971 * "lispindent(lnum)" function
11972 */
11973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *argvars;
11976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
11978#ifdef FEAT_LISP
11979 pos_T pos;
11980 linenr_T lnum;
11981
11982 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11985 {
11986 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 curwin->w_cursor = pos;
11989 }
11990 else
11991#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993}
11994
11995/*
11996 * "localtime()" function
11997 */
11998/*ARGSUSED*/
11999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T *argvars;
12002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012004 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005}
12006
Bram Moolenaar33570922005-01-25 22:26:29 +000012007static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008
12009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *argvars;
12012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 int exact;
12014{
12015 char_u *keys;
12016 char_u *which;
12017 char_u buf[NUMBUFLEN];
12018 char_u *keys_buf = NULL;
12019 char_u *rhs;
12020 int mode;
12021 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012022 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023
12024 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 rettv->v_type = VAR_STRING;
12026 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 if (*keys == NUL)
12030 return;
12031
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012032 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012035 if (argvars[2].v_type != VAR_UNKNOWN)
12036 abbr = get_tv_number(&argvars[2]);
12037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 else
12039 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 if (which == NULL)
12041 return;
12042
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 mode = get_map_mode(&which, 0);
12044
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012045 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012046 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 vim_free(keys_buf);
12048 if (rhs != NULL)
12049 {
12050 ga_init(&ga);
12051 ga.ga_itemsize = 1;
12052 ga.ga_growsize = 40;
12053
12054 while (*rhs != NUL)
12055 ga_concat(&ga, str2special(&rhs, FALSE));
12056
12057 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 }
12060}
12061
12062/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012063 * "map()" function
12064 */
12065 static void
12066f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012067 typval_T *argvars;
12068 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012069{
12070 filter_map(argvars, rettv, TRUE);
12071}
12072
12073/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012074 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 */
12076 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012077f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012081 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082}
12083
12084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 */
12087 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012088f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012089 typval_T *argvars;
12090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093}
12094
Bram Moolenaar33570922005-01-25 22:26:29 +000012095static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096
12097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012098find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012099 typval_T *argvars;
12100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 int type;
12102{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012103 char_u *str = NULL;
12104 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 char_u *pat;
12106 regmatch_T regmatch;
12107 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012108 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 char_u *save_cpo;
12110 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012111 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012112 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012113 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012114 list_T *l = NULL;
12115 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012116 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012117 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118
12119 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12120 save_cpo = p_cpo;
12121 p_cpo = (char_u *)"";
12122
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012123 rettv->vval.v_number = -1;
12124 if (type == 3)
12125 {
12126 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012127 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012128 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012129 }
12130 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 rettv->v_type = VAR_STRING;
12133 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012136 if (argvars[0].v_type == VAR_LIST)
12137 {
12138 if ((l = argvars[0].vval.v_list) == NULL)
12139 goto theend;
12140 li = l->lv_first;
12141 }
12142 else
12143 expr = str = get_tv_string(&argvars[0]);
12144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012145 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12146 if (pat == NULL)
12147 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012149 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012151 int error = FALSE;
12152
12153 start = get_tv_number_chk(&argvars[2], &error);
12154 if (error)
12155 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012156 if (l != NULL)
12157 {
12158 li = list_find(l, start);
12159 if (li == NULL)
12160 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012161 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012162 }
12163 else
12164 {
12165 if (start < 0)
12166 start = 0;
12167 if (start > (long)STRLEN(str))
12168 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012169 /* When "count" argument is there ignore matches before "start",
12170 * otherwise skip part of the string. Differs when pattern is "^"
12171 * or "\<". */
12172 if (argvars[3].v_type != VAR_UNKNOWN)
12173 startcol = start;
12174 else
12175 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012176 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012177
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012178 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012179 nth = get_tv_number_chk(&argvars[3], &error);
12180 if (error)
12181 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 }
12183
12184 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12185 if (regmatch.regprog != NULL)
12186 {
12187 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012188
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012189 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012190 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012191 if (l != NULL)
12192 {
12193 if (li == NULL)
12194 {
12195 match = FALSE;
12196 break;
12197 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012198 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012199 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012200 if (str == NULL)
12201 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012202 }
12203
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012204 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012205
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012206 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012207 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012208 if (l == NULL && !match)
12209 break;
12210
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012211 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012212 if (l != NULL)
12213 {
12214 li = li->li_next;
12215 ++idx;
12216 }
12217 else
12218 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012219#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012220 startcol = (colnr_T)(regmatch.startp[0]
12221 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012222#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012223 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012224#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012225 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012226 }
12227
12228 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012230 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012232 int i;
12233
12234 /* return list with matched string and submatches */
12235 for (i = 0; i < NSUBEXP; ++i)
12236 {
12237 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012238 {
12239 if (list_append_string(rettv->vval.v_list,
12240 (char_u *)"", 0) == FAIL)
12241 break;
12242 }
12243 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012244 regmatch.startp[i],
12245 (int)(regmatch.endp[i] - regmatch.startp[i]))
12246 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012247 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012248 }
12249 }
12250 else if (type == 2)
12251 {
12252 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012253 if (l != NULL)
12254 copy_tv(&li->li_tv, rettv);
12255 else
12256 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012258 }
12259 else if (l != NULL)
12260 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 else
12262 {
12263 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 (varnumber_T)(regmatch.startp[0] - str);
12266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012269 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 }
12271 }
12272 vim_free(regmatch.regprog);
12273 }
12274
12275theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012276 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 p_cpo = save_cpo;
12278}
12279
12280/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012281 * "match()" function
12282 */
12283 static void
12284f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 typval_T *argvars;
12286 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012287{
12288 find_some_match(argvars, rettv, 1);
12289}
12290
12291/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012292 * "matcharg()" function
12293 */
12294 static void
12295f_matcharg(argvars, rettv)
12296 typval_T *argvars;
12297 typval_T *rettv;
12298{
12299 if (rettv_list_alloc(rettv) == OK)
12300 {
12301#ifdef FEAT_SEARCH_EXTRA
12302 int mi = get_tv_number(&argvars[0]);
12303
12304 if (mi >= 1 && mi <= 3)
12305 {
12306 list_append_string(rettv->vval.v_list,
12307 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12308 list_append_string(rettv->vval.v_list,
12309 curwin->w_match_pat[mi - 1], -1);
12310 }
12311#endif
12312 }
12313}
12314
12315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012316 * "matchend()" function
12317 */
12318 static void
12319f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012320 typval_T *argvars;
12321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012322{
12323 find_some_match(argvars, rettv, 0);
12324}
12325
12326/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012327 * "matchlist()" function
12328 */
12329 static void
12330f_matchlist(argvars, rettv)
12331 typval_T *argvars;
12332 typval_T *rettv;
12333{
12334 find_some_match(argvars, rettv, 3);
12335}
12336
12337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012338 * "matchstr()" function
12339 */
12340 static void
12341f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012342 typval_T *argvars;
12343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012344{
12345 find_some_match(argvars, rettv, 2);
12346}
12347
Bram Moolenaar33570922005-01-25 22:26:29 +000012348static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012349
12350 static void
12351max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012352 typval_T *argvars;
12353 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012354 int domax;
12355{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012356 long n = 0;
12357 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012358 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012359
12360 if (argvars[0].v_type == VAR_LIST)
12361 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012362 list_T *l;
12363 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012364
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012365 l = argvars[0].vval.v_list;
12366 if (l != NULL)
12367 {
12368 li = l->lv_first;
12369 if (li != NULL)
12370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012371 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012372 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012373 {
12374 li = li->li_next;
12375 if (li == NULL)
12376 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012377 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012378 if (domax ? i > n : i < n)
12379 n = i;
12380 }
12381 }
12382 }
12383 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012384 else if (argvars[0].v_type == VAR_DICT)
12385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012386 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012387 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012388 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012389 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012390
12391 d = argvars[0].vval.v_dict;
12392 if (d != NULL)
12393 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012394 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012395 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012399 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012400 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012401 if (first)
12402 {
12403 n = i;
12404 first = FALSE;
12405 }
12406 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012407 n = i;
12408 }
12409 }
12410 }
12411 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012412 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012413 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012414 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012415}
12416
12417/*
12418 * "max()" function
12419 */
12420 static void
12421f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 typval_T *argvars;
12423 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012424{
12425 max_min(argvars, rettv, TRUE);
12426}
12427
12428/*
12429 * "min()" function
12430 */
12431 static void
12432f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012433 typval_T *argvars;
12434 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012435{
12436 max_min(argvars, rettv, FALSE);
12437}
12438
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012439static int mkdir_recurse __ARGS((char_u *dir, int prot));
12440
12441/*
12442 * Create the directory in which "dir" is located, and higher levels when
12443 * needed.
12444 */
12445 static int
12446mkdir_recurse(dir, prot)
12447 char_u *dir;
12448 int prot;
12449{
12450 char_u *p;
12451 char_u *updir;
12452 int r = FAIL;
12453
12454 /* Get end of directory name in "dir".
12455 * We're done when it's "/" or "c:/". */
12456 p = gettail_sep(dir);
12457 if (p <= get_past_head(dir))
12458 return OK;
12459
12460 /* If the directory exists we're done. Otherwise: create it.*/
12461 updir = vim_strnsave(dir, (int)(p - dir));
12462 if (updir == NULL)
12463 return FAIL;
12464 if (mch_isdir(updir))
12465 r = OK;
12466 else if (mkdir_recurse(updir, prot) == OK)
12467 r = vim_mkdir_emsg(updir, prot);
12468 vim_free(updir);
12469 return r;
12470}
12471
12472#ifdef vim_mkdir
12473/*
12474 * "mkdir()" function
12475 */
12476 static void
12477f_mkdir(argvars, rettv)
12478 typval_T *argvars;
12479 typval_T *rettv;
12480{
12481 char_u *dir;
12482 char_u buf[NUMBUFLEN];
12483 int prot = 0755;
12484
12485 rettv->vval.v_number = FAIL;
12486 if (check_restricted() || check_secure())
12487 return;
12488
12489 dir = get_tv_string_buf(&argvars[0], buf);
12490 if (argvars[1].v_type != VAR_UNKNOWN)
12491 {
12492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012493 prot = get_tv_number_chk(&argvars[2], NULL);
12494 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012495 mkdir_recurse(dir, prot);
12496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012498}
12499#endif
12500
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 * "mode()" function
12503 */
12504/*ARGSUSED*/
12505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012507 typval_T *argvars;
12508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509{
12510 char_u buf[2];
12511
12512#ifdef FEAT_VISUAL
12513 if (VIsual_active)
12514 {
12515 if (VIsual_select)
12516 buf[0] = VIsual_mode + 's' - 'v';
12517 else
12518 buf[0] = VIsual_mode;
12519 }
12520 else
12521#endif
12522 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12523 buf[0] = 'r';
12524 else if (State & INSERT)
12525 {
12526 if (State & REPLACE_FLAG)
12527 buf[0] = 'R';
12528 else
12529 buf[0] = 'i';
12530 }
12531 else if (State & CMDLINE)
12532 buf[0] = 'c';
12533 else
12534 buf[0] = 'n';
12535
12536 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012537 rettv->vval.v_string = vim_strsave(buf);
12538 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539}
12540
12541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 * "nextnonblank()" function
12543 */
12544 static void
12545f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 typval_T *argvars;
12547 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548{
12549 linenr_T lnum;
12550
12551 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012553 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012554 {
12555 lnum = 0;
12556 break;
12557 }
12558 if (*skipwhite(ml_get(lnum)) != NUL)
12559 break;
12560 }
12561 rettv->vval.v_number = lnum;
12562}
12563
12564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 * "nr2char()" function
12566 */
12567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
12572 char_u buf[NUMBUFLEN];
12573
12574#ifdef FEAT_MBYTE
12575 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 else
12578#endif
12579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 buf[1] = NUL;
12582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 rettv->v_type = VAR_STRING;
12584 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012585}
12586
12587/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012588 * "pathshorten()" function
12589 */
12590 static void
12591f_pathshorten(argvars, rettv)
12592 typval_T *argvars;
12593 typval_T *rettv;
12594{
12595 char_u *p;
12596
12597 rettv->v_type = VAR_STRING;
12598 p = get_tv_string_chk(&argvars[0]);
12599 if (p == NULL)
12600 rettv->vval.v_string = NULL;
12601 else
12602 {
12603 p = vim_strsave(p);
12604 rettv->vval.v_string = p;
12605 if (p != NULL)
12606 shorten_dir(p);
12607 }
12608}
12609
12610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012611 * "prevnonblank()" function
12612 */
12613 static void
12614f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012615 typval_T *argvars;
12616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617{
12618 linenr_T lnum;
12619
12620 lnum = get_tv_lnum(argvars);
12621 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12622 lnum = 0;
12623 else
12624 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12625 --lnum;
12626 rettv->vval.v_number = lnum;
12627}
12628
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012629#ifdef HAVE_STDARG_H
12630/* This dummy va_list is here because:
12631 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12632 * - locally in the function results in a "used before set" warning
12633 * - using va_start() to initialize it gives "function with fixed args" error */
12634static va_list ap;
12635#endif
12636
Bram Moolenaar8c711452005-01-14 21:53:12 +000012637/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012638 * "printf()" function
12639 */
12640 static void
12641f_printf(argvars, rettv)
12642 typval_T *argvars;
12643 typval_T *rettv;
12644{
12645 rettv->v_type = VAR_STRING;
12646 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012647#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012648 {
12649 char_u buf[NUMBUFLEN];
12650 int len;
12651 char_u *s;
12652 int saved_did_emsg = did_emsg;
12653 char *fmt;
12654
12655 /* Get the required length, allocate the buffer and do it for real. */
12656 did_emsg = FALSE;
12657 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012658 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012659 if (!did_emsg)
12660 {
12661 s = alloc(len + 1);
12662 if (s != NULL)
12663 {
12664 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012665 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012666 }
12667 }
12668 did_emsg |= saved_did_emsg;
12669 }
12670#endif
12671}
12672
12673/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012674 * "pumvisible()" function
12675 */
12676/*ARGSUSED*/
12677 static void
12678f_pumvisible(argvars, rettv)
12679 typval_T *argvars;
12680 typval_T *rettv;
12681{
12682 rettv->vval.v_number = 0;
12683#ifdef FEAT_INS_EXPAND
12684 if (pum_visible())
12685 rettv->vval.v_number = 1;
12686#endif
12687}
12688
12689/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012690 * "range()" function
12691 */
12692 static void
12693f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012694 typval_T *argvars;
12695 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012696{
12697 long start;
12698 long end;
12699 long stride = 1;
12700 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012704 if (argvars[1].v_type == VAR_UNKNOWN)
12705 {
12706 end = start - 1;
12707 start = 0;
12708 }
12709 else
12710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012711 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012714 }
12715
12716 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 if (error)
12718 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012719 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012720 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012721 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012722 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723 else
12724 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012725 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012726 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012727 if (list_append_number(rettv->vval.v_list,
12728 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012729 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012730 }
12731}
12732
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012733/*
12734 * "readfile()" function
12735 */
12736 static void
12737f_readfile(argvars, rettv)
12738 typval_T *argvars;
12739 typval_T *rettv;
12740{
12741 int binary = FALSE;
12742 char_u *fname;
12743 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012744 listitem_T *li;
12745#define FREAD_SIZE 200 /* optimized for text lines */
12746 char_u buf[FREAD_SIZE];
12747 int readlen; /* size of last fread() */
12748 int buflen; /* nr of valid chars in buf[] */
12749 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12750 int tolist; /* first byte in buf[] still to be put in list */
12751 int chop; /* how many CR to chop off */
12752 char_u *prev = NULL; /* previously read bytes, if any */
12753 int prevlen = 0; /* length of "prev" if not NULL */
12754 char_u *s;
12755 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012756 long maxline = MAXLNUM;
12757 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012758
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012759 if (argvars[1].v_type != VAR_UNKNOWN)
12760 {
12761 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12762 binary = TRUE;
12763 if (argvars[2].v_type != VAR_UNKNOWN)
12764 maxline = get_tv_number(&argvars[2]);
12765 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012767 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012768 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012769
12770 /* Always open the file in binary mode, library functions have a mind of
12771 * their own about CR-LF conversion. */
12772 fname = get_tv_string(&argvars[0]);
12773 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12774 {
12775 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12776 return;
12777 }
12778
12779 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012780 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012781 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012782 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012783 buflen = filtd + readlen;
12784 tolist = 0;
12785 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12786 {
12787 if (buf[filtd] == '\n' || readlen <= 0)
12788 {
12789 /* Only when in binary mode add an empty list item when the
12790 * last line ends in a '\n'. */
12791 if (!binary && readlen == 0 && filtd == 0)
12792 break;
12793
12794 /* Found end-of-line or end-of-file: add a text line to the
12795 * list. */
12796 chop = 0;
12797 if (!binary)
12798 while (filtd - chop - 1 >= tolist
12799 && buf[filtd - chop - 1] == '\r')
12800 ++chop;
12801 len = filtd - tolist - chop;
12802 if (prev == NULL)
12803 s = vim_strnsave(buf + tolist, len);
12804 else
12805 {
12806 s = alloc((unsigned)(prevlen + len + 1));
12807 if (s != NULL)
12808 {
12809 mch_memmove(s, prev, prevlen);
12810 vim_free(prev);
12811 prev = NULL;
12812 mch_memmove(s + prevlen, buf + tolist, len);
12813 s[prevlen + len] = NUL;
12814 }
12815 }
12816 tolist = filtd + 1;
12817
12818 li = listitem_alloc();
12819 if (li == NULL)
12820 {
12821 vim_free(s);
12822 break;
12823 }
12824 li->li_tv.v_type = VAR_STRING;
12825 li->li_tv.v_lock = 0;
12826 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012827 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012828
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012829 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012830 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012831 if (readlen <= 0)
12832 break;
12833 }
12834 else if (buf[filtd] == NUL)
12835 buf[filtd] = '\n';
12836 }
12837 if (readlen <= 0)
12838 break;
12839
12840 if (tolist == 0)
12841 {
12842 /* "buf" is full, need to move text to an allocated buffer */
12843 if (prev == NULL)
12844 {
12845 prev = vim_strnsave(buf, buflen);
12846 prevlen = buflen;
12847 }
12848 else
12849 {
12850 s = alloc((unsigned)(prevlen + buflen));
12851 if (s != NULL)
12852 {
12853 mch_memmove(s, prev, prevlen);
12854 mch_memmove(s + prevlen, buf, buflen);
12855 vim_free(prev);
12856 prev = s;
12857 prevlen += buflen;
12858 }
12859 }
12860 filtd = 0;
12861 }
12862 else
12863 {
12864 mch_memmove(buf, buf + tolist, buflen - tolist);
12865 filtd -= tolist;
12866 }
12867 }
12868
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012869 /*
12870 * For a negative line count use only the lines at the end of the file,
12871 * free the rest.
12872 */
12873 if (maxline < 0)
12874 while (cnt > -maxline)
12875 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012876 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012877 --cnt;
12878 }
12879
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012880 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012881 fclose(fd);
12882}
12883
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012884#if defined(FEAT_RELTIME)
12885static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12886
12887/*
12888 * Convert a List to proftime_T.
12889 * Return FAIL when there is something wrong.
12890 */
12891 static int
12892list2proftime(arg, tm)
12893 typval_T *arg;
12894 proftime_T *tm;
12895{
12896 long n1, n2;
12897 int error = FALSE;
12898
12899 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12900 || arg->vval.v_list->lv_len != 2)
12901 return FAIL;
12902 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12903 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12904# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012905 tm->HighPart = n1;
12906 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012907# else
12908 tm->tv_sec = n1;
12909 tm->tv_usec = n2;
12910# endif
12911 return error ? FAIL : OK;
12912}
12913#endif /* FEAT_RELTIME */
12914
12915/*
12916 * "reltime()" function
12917 */
12918 static void
12919f_reltime(argvars, rettv)
12920 typval_T *argvars;
12921 typval_T *rettv;
12922{
12923#ifdef FEAT_RELTIME
12924 proftime_T res;
12925 proftime_T start;
12926
12927 if (argvars[0].v_type == VAR_UNKNOWN)
12928 {
12929 /* No arguments: get current time. */
12930 profile_start(&res);
12931 }
12932 else if (argvars[1].v_type == VAR_UNKNOWN)
12933 {
12934 if (list2proftime(&argvars[0], &res) == FAIL)
12935 return;
12936 profile_end(&res);
12937 }
12938 else
12939 {
12940 /* Two arguments: compute the difference. */
12941 if (list2proftime(&argvars[0], &start) == FAIL
12942 || list2proftime(&argvars[1], &res) == FAIL)
12943 return;
12944 profile_sub(&res, &start);
12945 }
12946
12947 if (rettv_list_alloc(rettv) == OK)
12948 {
12949 long n1, n2;
12950
12951# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012952 n1 = res.HighPart;
12953 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012954# else
12955 n1 = res.tv_sec;
12956 n2 = res.tv_usec;
12957# endif
12958 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12959 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12960 }
12961#endif
12962}
12963
12964/*
12965 * "reltimestr()" function
12966 */
12967 static void
12968f_reltimestr(argvars, rettv)
12969 typval_T *argvars;
12970 typval_T *rettv;
12971{
12972#ifdef FEAT_RELTIME
12973 proftime_T tm;
12974#endif
12975
12976 rettv->v_type = VAR_STRING;
12977 rettv->vval.v_string = NULL;
12978#ifdef FEAT_RELTIME
12979 if (list2proftime(&argvars[0], &tm) == OK)
12980 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12981#endif
12982}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012983
Bram Moolenaar0d660222005-01-07 21:51:51 +000012984#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12985static void make_connection __ARGS((void));
12986static int check_connection __ARGS((void));
12987
12988 static void
12989make_connection()
12990{
12991 if (X_DISPLAY == NULL
12992# ifdef FEAT_GUI
12993 && !gui.in_use
12994# endif
12995 )
12996 {
12997 x_force_connect = TRUE;
12998 setup_term_clip();
12999 x_force_connect = FALSE;
13000 }
13001}
13002
13003 static int
13004check_connection()
13005{
13006 make_connection();
13007 if (X_DISPLAY == NULL)
13008 {
13009 EMSG(_("E240: No connection to Vim server"));
13010 return FAIL;
13011 }
13012 return OK;
13013}
13014#endif
13015
13016#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013017static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013018
13019 static void
13020remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013021 typval_T *argvars;
13022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023 int expr;
13024{
13025 char_u *server_name;
13026 char_u *keys;
13027 char_u *r = NULL;
13028 char_u buf[NUMBUFLEN];
13029# ifdef WIN32
13030 HWND w;
13031# else
13032 Window w;
13033# endif
13034
13035 if (check_restricted() || check_secure())
13036 return;
13037
13038# ifdef FEAT_X11
13039 if (check_connection() == FAIL)
13040 return;
13041# endif
13042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 server_name = get_tv_string_chk(&argvars[0]);
13044 if (server_name == NULL)
13045 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013046 keys = get_tv_string_buf(&argvars[1], buf);
13047# ifdef WIN32
13048 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13049# else
13050 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13051 < 0)
13052# endif
13053 {
13054 if (r != NULL)
13055 EMSG(r); /* sending worked but evaluation failed */
13056 else
13057 EMSG2(_("E241: Unable to send to %s"), server_name);
13058 return;
13059 }
13060
13061 rettv->vval.v_string = r;
13062
13063 if (argvars[2].v_type != VAR_UNKNOWN)
13064 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013066 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013068
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013069 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013070 v.di_tv.v_type = VAR_STRING;
13071 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013072 idvar = get_tv_string_chk(&argvars[2]);
13073 if (idvar != NULL)
13074 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013076 }
13077}
13078#endif
13079
13080/*
13081 * "remote_expr()" function
13082 */
13083/*ARGSUSED*/
13084 static void
13085f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013086 typval_T *argvars;
13087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013088{
13089 rettv->v_type = VAR_STRING;
13090 rettv->vval.v_string = NULL;
13091#ifdef FEAT_CLIENTSERVER
13092 remote_common(argvars, rettv, TRUE);
13093#endif
13094}
13095
13096/*
13097 * "remote_foreground()" function
13098 */
13099/*ARGSUSED*/
13100 static void
13101f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013102 typval_T *argvars;
13103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013104{
13105 rettv->vval.v_number = 0;
13106#ifdef FEAT_CLIENTSERVER
13107# ifdef WIN32
13108 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013109 {
13110 char_u *server_name = get_tv_string_chk(&argvars[0]);
13111
13112 if (server_name != NULL)
13113 serverForeground(server_name);
13114 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013115# else
13116 /* Send a foreground() expression to the server. */
13117 argvars[1].v_type = VAR_STRING;
13118 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13119 argvars[2].v_type = VAR_UNKNOWN;
13120 remote_common(argvars, rettv, TRUE);
13121 vim_free(argvars[1].vval.v_string);
13122# endif
13123#endif
13124}
13125
13126/*ARGSUSED*/
13127 static void
13128f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131{
13132#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013134 char_u *s = NULL;
13135# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013136 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013137# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013138 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139
13140 if (check_restricted() || check_secure())
13141 {
13142 rettv->vval.v_number = -1;
13143 return;
13144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013145 serverid = get_tv_string_chk(&argvars[0]);
13146 if (serverid == NULL)
13147 {
13148 rettv->vval.v_number = -1;
13149 return; /* type error; errmsg already given */
13150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013151# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013152 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013153 if (n == 0)
13154 rettv->vval.v_number = -1;
13155 else
13156 {
13157 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13158 rettv->vval.v_number = (s != NULL);
13159 }
13160# else
13161 rettv->vval.v_number = 0;
13162 if (check_connection() == FAIL)
13163 return;
13164
13165 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013166 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013167# endif
13168
13169 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013171 char_u *retvar;
13172
Bram Moolenaar33570922005-01-25 22:26:29 +000013173 v.di_tv.v_type = VAR_STRING;
13174 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 retvar = get_tv_string_chk(&argvars[1]);
13176 if (retvar != NULL)
13177 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013179 }
13180#else
13181 rettv->vval.v_number = -1;
13182#endif
13183}
13184
13185/*ARGSUSED*/
13186 static void
13187f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 typval_T *argvars;
13189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013190{
13191 char_u *r = NULL;
13192
13193#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194 char_u *serverid = get_tv_string_chk(&argvars[0]);
13195
13196 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013197 {
13198# ifdef WIN32
13199 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013200 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013201
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013202 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013203 if (n != 0)
13204 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13205 if (r == NULL)
13206# else
13207 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209# endif
13210 EMSG(_("E277: Unable to read a server reply"));
13211 }
13212#endif
13213 rettv->v_type = VAR_STRING;
13214 rettv->vval.v_string = r;
13215}
13216
13217/*
13218 * "remote_send()" function
13219 */
13220/*ARGSUSED*/
13221 static void
13222f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 typval_T *argvars;
13224 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225{
13226 rettv->v_type = VAR_STRING;
13227 rettv->vval.v_string = NULL;
13228#ifdef FEAT_CLIENTSERVER
13229 remote_common(argvars, rettv, FALSE);
13230#endif
13231}
13232
13233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013234 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013235 */
13236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013238 typval_T *argvars;
13239 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013240{
Bram Moolenaar33570922005-01-25 22:26:29 +000013241 list_T *l;
13242 listitem_T *item, *item2;
13243 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013245 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013246 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 dict_T *d;
13248 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013249
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013250 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013251 if (argvars[0].v_type == VAR_DICT)
13252 {
13253 if (argvars[2].v_type != VAR_UNKNOWN)
13254 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013255 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013256 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 key = get_tv_string_chk(&argvars[1]);
13259 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 di = dict_find(d, key, -1);
13262 if (di == NULL)
13263 EMSG2(_(e_dictkey), key);
13264 else
13265 {
13266 *rettv = di->di_tv;
13267 init_tv(&di->di_tv);
13268 dictitem_remove(d, di);
13269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013271 }
13272 }
13273 else if (argvars[0].v_type != VAR_LIST)
13274 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013275 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013276 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013278 int error = FALSE;
13279
13280 idx = get_tv_number_chk(&argvars[1], &error);
13281 if (error)
13282 ; /* type error: do nothing, errmsg already given */
13283 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013284 EMSGN(_(e_listidx), idx);
13285 else
13286 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013287 if (argvars[2].v_type == VAR_UNKNOWN)
13288 {
13289 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013290 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013291 *rettv = item->li_tv;
13292 vim_free(item);
13293 }
13294 else
13295 {
13296 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 end = get_tv_number_chk(&argvars[2], &error);
13298 if (error)
13299 ; /* type error: do nothing */
13300 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013301 EMSGN(_(e_listidx), end);
13302 else
13303 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013304 int cnt = 0;
13305
13306 for (li = item; li != NULL; li = li->li_next)
13307 {
13308 ++cnt;
13309 if (li == item2)
13310 break;
13311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013312 if (li == NULL) /* didn't find "item2" after "item" */
13313 EMSG(_(e_invrange));
13314 else
13315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013316 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013317 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013318 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013319 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013320 l->lv_first = item;
13321 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013322 item->li_prev = NULL;
13323 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013324 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013325 }
13326 }
13327 }
13328 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013329 }
13330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331}
13332
13333/*
13334 * "rename({from}, {to})" function
13335 */
13336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013338 typval_T *argvars;
13339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340{
13341 char_u buf[NUMBUFLEN];
13342
13343 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13347 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348}
13349
13350/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013351 * "repeat()" function
13352 */
13353/*ARGSUSED*/
13354 static void
13355f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013356 typval_T *argvars;
13357 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013358{
13359 char_u *p;
13360 int n;
13361 int slen;
13362 int len;
13363 char_u *r;
13364 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013365
13366 n = get_tv_number(&argvars[1]);
13367 if (argvars[0].v_type == VAR_LIST)
13368 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013369 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013370 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013371 if (list_extend(rettv->vval.v_list,
13372 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013373 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013374 }
13375 else
13376 {
13377 p = get_tv_string(&argvars[0]);
13378 rettv->v_type = VAR_STRING;
13379 rettv->vval.v_string = NULL;
13380
13381 slen = (int)STRLEN(p);
13382 len = slen * n;
13383 if (len <= 0)
13384 return;
13385
13386 r = alloc(len + 1);
13387 if (r != NULL)
13388 {
13389 for (i = 0; i < n; i++)
13390 mch_memmove(r + i * slen, p, (size_t)slen);
13391 r[len] = NUL;
13392 }
13393
13394 rettv->vval.v_string = r;
13395 }
13396}
13397
13398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 * "resolve()" function
13400 */
13401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013402f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 typval_T *argvars;
13404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405{
13406 char_u *p;
13407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409#ifdef FEAT_SHORTCUT
13410 {
13411 char_u *v = NULL;
13412
13413 v = mch_resolve_shortcut(p);
13414 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 }
13419#else
13420# ifdef HAVE_READLINK
13421 {
13422 char_u buf[MAXPATHL + 1];
13423 char_u *cpy;
13424 int len;
13425 char_u *remain = NULL;
13426 char_u *q;
13427 int is_relative_to_current = FALSE;
13428 int has_trailing_pathsep = FALSE;
13429 int limit = 100;
13430
13431 p = vim_strsave(p);
13432
13433 if (p[0] == '.' && (vim_ispathsep(p[1])
13434 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13435 is_relative_to_current = TRUE;
13436
13437 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013438 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 has_trailing_pathsep = TRUE;
13440
13441 q = getnextcomp(p);
13442 if (*q != NUL)
13443 {
13444 /* Separate the first path component in "p", and keep the
13445 * remainder (beginning with the path separator). */
13446 remain = vim_strsave(q - 1);
13447 q[-1] = NUL;
13448 }
13449
13450 for (;;)
13451 {
13452 for (;;)
13453 {
13454 len = readlink((char *)p, (char *)buf, MAXPATHL);
13455 if (len <= 0)
13456 break;
13457 buf[len] = NUL;
13458
13459 if (limit-- == 0)
13460 {
13461 vim_free(p);
13462 vim_free(remain);
13463 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 goto fail;
13466 }
13467
13468 /* Ensure that the result will have a trailing path separator
13469 * if the argument has one. */
13470 if (remain == NULL && has_trailing_pathsep)
13471 add_pathsep(buf);
13472
13473 /* Separate the first path component in the link value and
13474 * concatenate the remainders. */
13475 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13476 if (*q != NUL)
13477 {
13478 if (remain == NULL)
13479 remain = vim_strsave(q - 1);
13480 else
13481 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013482 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 if (cpy != NULL)
13484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 vim_free(remain);
13486 remain = cpy;
13487 }
13488 }
13489 q[-1] = NUL;
13490 }
13491
13492 q = gettail(p);
13493 if (q > p && *q == NUL)
13494 {
13495 /* Ignore trailing path separator. */
13496 q[-1] = NUL;
13497 q = gettail(p);
13498 }
13499 if (q > p && !mch_isFullName(buf))
13500 {
13501 /* symlink is relative to directory of argument */
13502 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13503 if (cpy != NULL)
13504 {
13505 STRCPY(cpy, p);
13506 STRCPY(gettail(cpy), buf);
13507 vim_free(p);
13508 p = cpy;
13509 }
13510 }
13511 else
13512 {
13513 vim_free(p);
13514 p = vim_strsave(buf);
13515 }
13516 }
13517
13518 if (remain == NULL)
13519 break;
13520
13521 /* Append the first path component of "remain" to "p". */
13522 q = getnextcomp(remain + 1);
13523 len = q - remain - (*q != NUL);
13524 cpy = vim_strnsave(p, STRLEN(p) + len);
13525 if (cpy != NULL)
13526 {
13527 STRNCAT(cpy, remain, len);
13528 vim_free(p);
13529 p = cpy;
13530 }
13531 /* Shorten "remain". */
13532 if (*q != NUL)
13533 STRCPY(remain, q - 1);
13534 else
13535 {
13536 vim_free(remain);
13537 remain = NULL;
13538 }
13539 }
13540
13541 /* If the result is a relative path name, make it explicitly relative to
13542 * the current directory if and only if the argument had this form. */
13543 if (!vim_ispathsep(*p))
13544 {
13545 if (is_relative_to_current
13546 && *p != NUL
13547 && !(p[0] == '.'
13548 && (p[1] == NUL
13549 || vim_ispathsep(p[1])
13550 || (p[1] == '.'
13551 && (p[2] == NUL
13552 || vim_ispathsep(p[2]))))))
13553 {
13554 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013555 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 if (cpy != NULL)
13557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 vim_free(p);
13559 p = cpy;
13560 }
13561 }
13562 else if (!is_relative_to_current)
13563 {
13564 /* Strip leading "./". */
13565 q = p;
13566 while (q[0] == '.' && vim_ispathsep(q[1]))
13567 q += 2;
13568 if (q > p)
13569 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13570 }
13571 }
13572
13573 /* Ensure that the result will have no trailing path separator
13574 * if the argument had none. But keep "/" or "//". */
13575 if (!has_trailing_pathsep)
13576 {
13577 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013578 if (after_pathsep(p, q))
13579 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 }
13581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 }
13584# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586# endif
13587#endif
13588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590
13591#ifdef HAVE_READLINK
13592fail:
13593#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595}
13596
13597/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013598 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 */
13600 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 typval_T *argvars;
13603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604{
Bram Moolenaar33570922005-01-25 22:26:29 +000013605 list_T *l;
13606 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607
Bram Moolenaar0d660222005-01-07 21:51:51 +000013608 rettv->vval.v_number = 0;
13609 if (argvars[0].v_type != VAR_LIST)
13610 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013611 else if ((l = argvars[0].vval.v_list) != NULL
13612 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013613 {
13614 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013615 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013616 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013617 while (li != NULL)
13618 {
13619 ni = li->li_prev;
13620 list_append(l, li);
13621 li = ni;
13622 }
13623 rettv->vval.v_list = l;
13624 rettv->v_type = VAR_LIST;
13625 ++l->lv_refcount;
13626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627}
13628
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013629#define SP_NOMOVE 0x01 /* don't move cursor */
13630#define SP_REPEAT 0x02 /* repeat to find outer pair */
13631#define SP_RETCOUNT 0x04 /* return matchcount */
13632#define SP_SETPCMARK 0x08 /* set previous context mark */
13633#define SP_START 0x10 /* accept match at start position */
13634#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13635#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013636
Bram Moolenaar33570922005-01-25 22:26:29 +000013637static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638
13639/*
13640 * Get flags for a search function.
13641 * Possibly sets "p_ws".
13642 * Returns BACKWARD, FORWARD or zero (for an error).
13643 */
13644 static int
13645get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013647 int *flagsp;
13648{
13649 int dir = FORWARD;
13650 char_u *flags;
13651 char_u nbuf[NUMBUFLEN];
13652 int mask;
13653
13654 if (varp->v_type != VAR_UNKNOWN)
13655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 flags = get_tv_string_buf_chk(varp, nbuf);
13657 if (flags == NULL)
13658 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013659 while (*flags != NUL)
13660 {
13661 switch (*flags)
13662 {
13663 case 'b': dir = BACKWARD; break;
13664 case 'w': p_ws = TRUE; break;
13665 case 'W': p_ws = FALSE; break;
13666 default: mask = 0;
13667 if (flagsp != NULL)
13668 switch (*flags)
13669 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013670 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013671 case 'e': mask = SP_END; break;
13672 case 'm': mask = SP_RETCOUNT; break;
13673 case 'n': mask = SP_NOMOVE; break;
13674 case 'p': mask = SP_SUBPAT; break;
13675 case 'r': mask = SP_REPEAT; break;
13676 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013677 }
13678 if (mask == 0)
13679 {
13680 EMSG2(_(e_invarg2), flags);
13681 dir = 0;
13682 }
13683 else
13684 *flagsp |= mask;
13685 }
13686 if (dir == 0)
13687 break;
13688 ++flags;
13689 }
13690 }
13691 return dir;
13692}
13693
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013695 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013697 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013698search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013699 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013700 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013701 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013703 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 char_u *pat;
13705 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013706 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 int save_p_ws = p_ws;
13708 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013709 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013710 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013711 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013712 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013715 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013716 if (dir == 0)
13717 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013718 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013719 if (flags & SP_START)
13720 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013721 if (flags & SP_END)
13722 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013723
13724 /* Optional extra argument: line number to stop searching. */
13725 if (argvars[1].v_type != VAR_UNKNOWN
13726 && argvars[2].v_type != VAR_UNKNOWN)
13727 {
13728 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13729 if (lnum_stop < 0)
13730 goto theend;
13731 }
13732
Bram Moolenaar231334e2005-07-25 20:46:57 +000013733 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013734 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013735 * Check to make sure only those flags are set.
13736 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13737 * flags cannot be set. Check for that condition also.
13738 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013739 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013740 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013743 goto theend;
13744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013746 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013747 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13748 options, RE_SEARCH, (linenr_T)lnum_stop);
13749 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013751 if (flags & SP_SUBPAT)
13752 retval = subpatnum;
13753 else
13754 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013755 if (flags & SP_SETPCMARK)
13756 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013758 if (match_pos != NULL)
13759 {
13760 /* Store the match cursor position */
13761 match_pos->lnum = pos.lnum;
13762 match_pos->col = pos.col + 1;
13763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 /* "/$" will put the cursor after the end of the line, may need to
13765 * correct that here */
13766 check_cursor();
13767 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013768
13769 /* If 'n' flag is used: restore cursor position. */
13770 if (flags & SP_NOMOVE)
13771 curwin->w_cursor = save_cursor;
13772theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013774
13775 return retval;
13776}
13777
13778/*
13779 * "search()" function
13780 */
13781 static void
13782f_search(argvars, rettv)
13783 typval_T *argvars;
13784 typval_T *rettv;
13785{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013786 int flags = 0;
13787
13788 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789}
13790
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013792 * "searchdecl()" function
13793 */
13794 static void
13795f_searchdecl(argvars, rettv)
13796 typval_T *argvars;
13797 typval_T *rettv;
13798{
13799 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013800 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013801 int error = FALSE;
13802 char_u *name;
13803
13804 rettv->vval.v_number = 1; /* default: FAIL */
13805
13806 name = get_tv_string_chk(&argvars[0]);
13807 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013808 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013809 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013810 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13811 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13812 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013813 if (!error && name != NULL)
13814 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013815 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013816}
13817
13818/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013819 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013821 static int
13822searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013823 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013824 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825{
13826 char_u *spat, *mpat, *epat;
13827 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 int dir;
13830 int flags = 0;
13831 char_u nbuf1[NUMBUFLEN];
13832 char_u nbuf2[NUMBUFLEN];
13833 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013834 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013835 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013838 spat = get_tv_string_chk(&argvars[0]);
13839 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13840 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13841 if (spat == NULL || mpat == NULL || epat == NULL)
13842 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 /* Handle the optional fourth argument: flags */
13845 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013846 if (dir == 0)
13847 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013848
13849 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013850 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13851 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013852 if ((flags & (SP_END | SP_SUBPAT)) != 0
13853 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013854 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013855 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013856 goto theend;
13857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013859 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860 if (argvars[3].v_type == VAR_UNKNOWN
13861 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 skip = (char_u *)"";
13863 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013866 if (argvars[5].v_type != VAR_UNKNOWN)
13867 {
13868 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13869 if (lnum_stop < 0)
13870 goto theend;
13871 }
13872 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 if (skip == NULL)
13874 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013876 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13877 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013878
13879theend:
13880 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013881
13882 return retval;
13883}
13884
13885/*
13886 * "searchpair()" function
13887 */
13888 static void
13889f_searchpair(argvars, rettv)
13890 typval_T *argvars;
13891 typval_T *rettv;
13892{
13893 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13894}
13895
13896/*
13897 * "searchpairpos()" function
13898 */
13899 static void
13900f_searchpairpos(argvars, rettv)
13901 typval_T *argvars;
13902 typval_T *rettv;
13903{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013904 pos_T match_pos;
13905 int lnum = 0;
13906 int col = 0;
13907
13908 rettv->vval.v_number = 0;
13909
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013910 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013911 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013912
13913 if (searchpair_cmn(argvars, &match_pos) > 0)
13914 {
13915 lnum = match_pos.lnum;
13916 col = match_pos.col;
13917 }
13918
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013919 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13920 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013921}
13922
13923/*
13924 * Search for a start/middle/end thing.
13925 * Used by searchpair(), see its documentation for the details.
13926 * Returns 0 or -1 for no match,
13927 */
13928 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013929do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013930 char_u *spat; /* start pattern */
13931 char_u *mpat; /* middle pattern */
13932 char_u *epat; /* end pattern */
13933 int dir; /* BACKWARD or FORWARD */
13934 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013935 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013936 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013937 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013938{
13939 char_u *save_cpo;
13940 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13941 long retval = 0;
13942 pos_T pos;
13943 pos_T firstpos;
13944 pos_T foundpos;
13945 pos_T save_cursor;
13946 pos_T save_pos;
13947 int n;
13948 int r;
13949 int nest = 1;
13950 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013951 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013952
13953 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13954 save_cpo = p_cpo;
13955 p_cpo = (char_u *)"";
13956
13957 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13958 * start/middle/end (pat3, for the top pair). */
13959 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13960 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13961 if (pat2 == NULL || pat3 == NULL)
13962 goto theend;
13963 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13964 if (*mpat == NUL)
13965 STRCPY(pat3, pat2);
13966 else
13967 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13968 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013969 if (flags & SP_START)
13970 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013971
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 save_cursor = curwin->w_cursor;
13973 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013974 clearpos(&firstpos);
13975 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 pat = pat3;
13977 for (;;)
13978 {
13979 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013980 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13982 /* didn't find it or found the first match again: FAIL */
13983 break;
13984
13985 if (firstpos.lnum == 0)
13986 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013987 if (equalpos(pos, foundpos))
13988 {
13989 /* Found the same position again. Can happen with a pattern that
13990 * has "\zs" at the end and searching backwards. Advance one
13991 * character and try again. */
13992 if (dir == BACKWARD)
13993 decl(&pos);
13994 else
13995 incl(&pos);
13996 }
13997 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998
13999 /* If the skip pattern matches, ignore this match. */
14000 if (*skip != NUL)
14001 {
14002 save_pos = curwin->w_cursor;
14003 curwin->w_cursor = pos;
14004 r = eval_to_bool(skip, &err, NULL, FALSE);
14005 curwin->w_cursor = save_pos;
14006 if (err)
14007 {
14008 /* Evaluating {skip} caused an error, break here. */
14009 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014010 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 break;
14012 }
14013 if (r)
14014 continue;
14015 }
14016
14017 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14018 {
14019 /* Found end when searching backwards or start when searching
14020 * forward: nested pair. */
14021 ++nest;
14022 pat = pat2; /* nested, don't search for middle */
14023 }
14024 else
14025 {
14026 /* Found end when searching forward or start when searching
14027 * backward: end of (nested) pair; or found middle in outer pair. */
14028 if (--nest == 1)
14029 pat = pat3; /* outer level, search for middle */
14030 }
14031
14032 if (nest == 0)
14033 {
14034 /* Found the match: return matchcount or line number. */
14035 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014036 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014038 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014039 if (flags & SP_SETPCMARK)
14040 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 curwin->w_cursor = pos;
14042 if (!(flags & SP_REPEAT))
14043 break;
14044 nest = 1; /* search for next unmatched */
14045 }
14046 }
14047
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014048 if (match_pos != NULL)
14049 {
14050 /* Store the match cursor position */
14051 match_pos->lnum = curwin->w_cursor.lnum;
14052 match_pos->col = curwin->w_cursor.col + 1;
14053 }
14054
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014056 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 curwin->w_cursor = save_cursor;
14058
14059theend:
14060 vim_free(pat2);
14061 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014063
14064 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065}
14066
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014067/*
14068 * "searchpos()" function
14069 */
14070 static void
14071f_searchpos(argvars, rettv)
14072 typval_T *argvars;
14073 typval_T *rettv;
14074{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014075 pos_T match_pos;
14076 int lnum = 0;
14077 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014078 int n;
14079 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014080
14081 rettv->vval.v_number = 0;
14082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014083 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014084 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014085
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014086 n = search_cmn(argvars, &match_pos, &flags);
14087 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014088 {
14089 lnum = match_pos.lnum;
14090 col = match_pos.col;
14091 }
14092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014093 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14094 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014095 if (flags & SP_SUBPAT)
14096 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014097}
14098
14099
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100/*ARGSUSED*/
14101 static void
14102f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 typval_T *argvars;
14104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106#ifdef FEAT_CLIENTSERVER
14107 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 char_u *server = get_tv_string_chk(&argvars[0]);
14109 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 if (server == NULL || reply == NULL)
14113 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 if (check_restricted() || check_secure())
14115 return;
14116# ifdef FEAT_X11
14117 if (check_connection() == FAIL)
14118 return;
14119# endif
14120
14121 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 EMSG(_("E258: Unable to send to client"));
14124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014126 rettv->vval.v_number = 0;
14127#else
14128 rettv->vval.v_number = -1;
14129#endif
14130}
14131
14132/*ARGSUSED*/
14133 static void
14134f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014135 typval_T *argvars;
14136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014137{
14138 char_u *r = NULL;
14139
14140#ifdef FEAT_CLIENTSERVER
14141# ifdef WIN32
14142 r = serverGetVimNames();
14143# else
14144 make_connection();
14145 if (X_DISPLAY != NULL)
14146 r = serverGetVimNames(X_DISPLAY);
14147# endif
14148#endif
14149 rettv->v_type = VAR_STRING;
14150 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151}
14152
14153/*
14154 * "setbufvar()" function
14155 */
14156/*ARGSUSED*/
14157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161{
14162 buf_T *buf;
14163#ifdef FEAT_AUTOCMD
14164 aco_save_T aco;
14165#else
14166 buf_T *save_curbuf;
14167#endif
14168 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 char_u nbuf[NUMBUFLEN];
14171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 rettv->vval.v_number = 0;
14173
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 if (check_restricted() || check_secure())
14175 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14177 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 varp = &argvars[2];
14180
14181 if (buf != NULL && varname != NULL && varp != NULL)
14182 {
14183 /* set curbuf to be our buf, temporarily */
14184#ifdef FEAT_AUTOCMD
14185 aucmd_prepbuf(&aco, buf);
14186#else
14187 save_curbuf = curbuf;
14188 curbuf = buf;
14189#endif
14190
14191 if (*varname == '&')
14192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 long numval;
14194 char_u *strval;
14195 int error = FALSE;
14196
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 numval = get_tv_number_chk(varp, &error);
14199 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 if (!error && strval != NULL)
14201 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 }
14203 else
14204 {
14205 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14206 if (bufvarname != NULL)
14207 {
14208 STRCPY(bufvarname, "b:");
14209 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014210 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 vim_free(bufvarname);
14212 }
14213 }
14214
14215 /* reset notion of buffer */
14216#ifdef FEAT_AUTOCMD
14217 aucmd_restbuf(&aco);
14218#else
14219 curbuf = save_curbuf;
14220#endif
14221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222}
14223
14224/*
14225 * "setcmdpos()" function
14226 */
14227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 int pos = (int)get_tv_number(&argvars[0]) - 1;
14233
14234 if (pos >= 0)
14235 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236}
14237
14238/*
14239 * "setline()" function
14240 */
14241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014243 typval_T *argvars;
14244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245{
14246 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014247 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014248 list_T *l = NULL;
14249 listitem_T *li = NULL;
14250 long added = 0;
14251 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014253 lnum = get_tv_lnum(&argvars[0]);
14254 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014256 l = argvars[1].vval.v_list;
14257 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014259 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014261
14262 rettv->vval.v_number = 0; /* OK */
14263 for (;;)
14264 {
14265 if (l != NULL)
14266 {
14267 /* list argument, get next string */
14268 if (li == NULL)
14269 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014271 li = li->li_next;
14272 }
14273
14274 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014276 break;
14277 if (lnum <= curbuf->b_ml.ml_line_count)
14278 {
14279 /* existing line, replace it */
14280 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14281 {
14282 changed_bytes(lnum, 0);
14283 check_cursor_col();
14284 rettv->vval.v_number = 0; /* OK */
14285 }
14286 }
14287 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14288 {
14289 /* lnum is one past the last line, append the line */
14290 ++added;
14291 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14292 rettv->vval.v_number = 0; /* OK */
14293 }
14294
14295 if (l == NULL) /* only one string argument */
14296 break;
14297 ++lnum;
14298 }
14299
14300 if (added > 0)
14301 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302}
14303
14304/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014305 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014306 */
14307/*ARGSUSED*/
14308 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014309set_qf_ll_list(wp, list_arg, action_arg, rettv)
14310 win_T *wp;
14311 typval_T *list_arg;
14312 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014313 typval_T *rettv;
14314{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014315#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014316 char_u *act;
14317 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014318#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014319
Bram Moolenaar2641f772005-03-25 21:58:17 +000014320 rettv->vval.v_number = -1;
14321
14322#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014323 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014324 EMSG(_(e_listreq));
14325 else
14326 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014327 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014328
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014329 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014330 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014331 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332 if (act == NULL)
14333 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014334 if (*act == 'a' || *act == 'r')
14335 action = *act;
14336 }
14337
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014338 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014339 rettv->vval.v_number = 0;
14340 }
14341#endif
14342}
14343
14344/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014345 * "setloclist()" function
14346 */
14347/*ARGSUSED*/
14348 static void
14349f_setloclist(argvars, rettv)
14350 typval_T *argvars;
14351 typval_T *rettv;
14352{
14353 win_T *win;
14354
14355 rettv->vval.v_number = -1;
14356
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014357 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014358 if (win != NULL)
14359 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14360}
14361
14362/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014363 * "setpos()" function
14364 */
14365/*ARGSUSED*/
14366 static void
14367f_setpos(argvars, rettv)
14368 typval_T *argvars;
14369 typval_T *rettv;
14370{
14371 pos_T pos;
14372 int fnum;
14373 char_u *name;
14374
14375 name = get_tv_string_chk(argvars);
14376 if (name != NULL)
14377 {
14378 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14379 {
14380 --pos.col;
14381 if (name[0] == '.') /* cursor */
14382 {
14383 if (fnum == curbuf->b_fnum)
14384 {
14385 curwin->w_cursor = pos;
14386 check_cursor();
14387 }
14388 else
14389 EMSG(_(e_invarg));
14390 }
14391 else if (name[0] == '\'') /* mark */
14392 (void)setmark_pos(name[1], &pos, fnum);
14393 else
14394 EMSG(_(e_invarg));
14395 }
14396 }
14397}
14398
14399/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014400 * "setqflist()" function
14401 */
14402/*ARGSUSED*/
14403 static void
14404f_setqflist(argvars, rettv)
14405 typval_T *argvars;
14406 typval_T *rettv;
14407{
14408 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14409}
14410
14411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 * "setreg()" function
14413 */
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 int regname;
14420 char_u *strregname;
14421 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 int append;
14424 char_u yank_type;
14425 long block_len;
14426
14427 block_len = -1;
14428 yank_type = MAUTO;
14429 append = FALSE;
14430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 if (strregname == NULL)
14435 return; /* type error; errmsg already given */
14436 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 if (regname == 0 || regname == '@')
14438 regname = '"';
14439 else if (regname == '=')
14440 return;
14441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 stropt = get_tv_string_chk(&argvars[2]);
14445 if (stropt == NULL)
14446 return; /* type error */
14447 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 switch (*stropt)
14449 {
14450 case 'a': case 'A': /* append */
14451 append = TRUE;
14452 break;
14453 case 'v': case 'c': /* character-wise selection */
14454 yank_type = MCHAR;
14455 break;
14456 case 'V': case 'l': /* line-wise selection */
14457 yank_type = MLINE;
14458 break;
14459#ifdef FEAT_VISUAL
14460 case 'b': case Ctrl_V: /* block-wise selection */
14461 yank_type = MBLOCK;
14462 if (VIM_ISDIGIT(stropt[1]))
14463 {
14464 ++stropt;
14465 block_len = getdigits(&stropt) - 1;
14466 --stropt;
14467 }
14468 break;
14469#endif
14470 }
14471 }
14472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 strval = get_tv_string_chk(&argvars[1]);
14474 if (strval != NULL)
14475 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478}
14479
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014480/*
14481 * "settabwinvar()" function
14482 */
14483 static void
14484f_settabwinvar(argvars, rettv)
14485 typval_T *argvars;
14486 typval_T *rettv;
14487{
14488 setwinvar(argvars, rettv, 1);
14489}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490
14491/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014492 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014495f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014496 typval_T *argvars;
14497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014499 setwinvar(argvars, rettv, 0);
14500}
14501
14502/*
14503 * "setwinvar()" and "settabwinvar()" functions
14504 */
14505 static void
14506setwinvar(argvars, rettv, off)
14507 typval_T *argvars;
14508 typval_T *rettv;
14509 int off;
14510{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 win_T *win;
14512#ifdef FEAT_WINDOWS
14513 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014514 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515#endif
14516 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014519 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 rettv->vval.v_number = 0;
14522
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 if (check_restricted() || check_secure())
14524 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014525
14526#ifdef FEAT_WINDOWS
14527 if (off == 1)
14528 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14529 else
14530 tp = curtab;
14531#endif
14532 win = find_win_by_nr(&argvars[off], tp);
14533 varname = get_tv_string_chk(&argvars[off + 1]);
14534 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535
14536 if (win != NULL && varname != NULL && varp != NULL)
14537 {
14538#ifdef FEAT_WINDOWS
14539 /* set curwin to be our win, temporarily */
14540 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014541 save_curtab = curtab;
14542 goto_tabpage_tp(tp);
14543 if (!win_valid(win))
14544 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 curwin = win;
14546 curbuf = curwin->w_buffer;
14547#endif
14548
14549 if (*varname == '&')
14550 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 long numval;
14552 char_u *strval;
14553 int error = FALSE;
14554
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 numval = get_tv_number_chk(varp, &error);
14557 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 if (!error && strval != NULL)
14559 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 }
14561 else
14562 {
14563 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14564 if (winvarname != NULL)
14565 {
14566 STRCPY(winvarname, "w:");
14567 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014568 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 vim_free(winvarname);
14570 }
14571 }
14572
14573#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014574 /* Restore current tabpage and window, if still valid (autocomands can
14575 * make them invalid). */
14576 if (valid_tabpage(save_curtab))
14577 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 if (win_valid(save_curwin))
14579 {
14580 curwin = save_curwin;
14581 curbuf = curwin->w_buffer;
14582 }
14583#endif
14584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585}
14586
14587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 */
14590 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 typval_T *argvars;
14593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597 p = get_tv_string(&argvars[0]);
14598 rettv->vval.v_string = vim_strsave(p);
14599 simplify_filename(rettv->vval.v_string); /* simplify in place */
14600 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601}
14602
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603static int
14604#ifdef __BORLANDC__
14605 _RTLENTRYF
14606#endif
14607 item_compare __ARGS((const void *s1, const void *s2));
14608static int
14609#ifdef __BORLANDC__
14610 _RTLENTRYF
14611#endif
14612 item_compare2 __ARGS((const void *s1, const void *s2));
14613
14614static int item_compare_ic;
14615static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617#define ITEM_COMPARE_FAIL 999
14618
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622 static int
14623#ifdef __BORLANDC__
14624_RTLENTRYF
14625#endif
14626item_compare(s1, s2)
14627 const void *s1;
14628 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630 char_u *p1, *p2;
14631 char_u *tofree1, *tofree2;
14632 int res;
14633 char_u numbuf1[NUMBUFLEN];
14634 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014636 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14637 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 if (item_compare_ic)
14639 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641 res = STRCMP(p1, p2);
14642 vim_free(tofree1);
14643 vim_free(tofree2);
14644 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645}
14646
14647 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648#ifdef __BORLANDC__
14649_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014651item_compare2(s1, s2)
14652 const void *s1;
14653 const void *s2;
14654{
14655 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014656 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014657 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014658 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 /* shortcut after failure in previous call; compare all items equal */
14661 if (item_compare_func_err)
14662 return 0;
14663
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014664 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14665 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14667 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668
14669 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014670 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014671 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 clear_tv(&argv[0]);
14673 clear_tv(&argv[1]);
14674
14675 if (res == FAIL)
14676 res = ITEM_COMPARE_FAIL;
14677 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 /* return value has wrong type */
14679 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14680 if (item_compare_func_err)
14681 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682 clear_tv(&rettv);
14683 return res;
14684}
14685
14686/*
14687 * "sort({list})" function
14688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014691 typval_T *argvars;
14692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693{
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 list_T *l;
14695 listitem_T *li;
14696 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014697 long len;
14698 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700 rettv->vval.v_number = 0;
14701 if (argvars[0].v_type != VAR_LIST)
14702 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 else
14704 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014706 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 return;
14708 rettv->vval.v_list = l;
14709 rettv->v_type = VAR_LIST;
14710 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711
Bram Moolenaar0d660222005-01-07 21:51:51 +000014712 len = list_len(l);
14713 if (len <= 1)
14714 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 item_compare_ic = FALSE;
14717 item_compare_func = NULL;
14718 if (argvars[1].v_type != VAR_UNKNOWN)
14719 {
14720 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722 else
14723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 int error = FALSE;
14725
14726 i = get_tv_number_chk(&argvars[1], &error);
14727 if (error)
14728 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 if (i == 1)
14730 item_compare_ic = TRUE;
14731 else
14732 item_compare_func = get_tv_string(&argvars[1]);
14733 }
14734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738 if (ptrs == NULL)
14739 return;
14740 i = 0;
14741 for (li = l->lv_first; li != NULL; li = li->li_next)
14742 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014745 /* test the compare function */
14746 if (item_compare_func != NULL
14747 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14748 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014749 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 {
14752 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014753 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754 item_compare_func == NULL ? item_compare : item_compare2);
14755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014756 if (!item_compare_func_err)
14757 {
14758 /* Clear the List and append the items in the sorted order. */
14759 l->lv_first = l->lv_last = NULL;
14760 l->lv_len = 0;
14761 for (i = 0; i < len; ++i)
14762 list_append(l, ptrs[i]);
14763 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014764 }
14765
14766 vim_free(ptrs);
14767 }
14768}
14769
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014770/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014771 * "soundfold({word})" function
14772 */
14773 static void
14774f_soundfold(argvars, rettv)
14775 typval_T *argvars;
14776 typval_T *rettv;
14777{
14778 char_u *s;
14779
14780 rettv->v_type = VAR_STRING;
14781 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014782#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014783 rettv->vval.v_string = eval_soundfold(s);
14784#else
14785 rettv->vval.v_string = vim_strsave(s);
14786#endif
14787}
14788
14789/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014790 * "spellbadword()" function
14791 */
14792/* ARGSUSED */
14793 static void
14794f_spellbadword(argvars, rettv)
14795 typval_T *argvars;
14796 typval_T *rettv;
14797{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014798 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014799 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014800 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014801
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014802 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014803 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014804
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014805#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014806 if (argvars[0].v_type == VAR_UNKNOWN)
14807 {
14808 /* Find the start and length of the badly spelled word. */
14809 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14810 if (len != 0)
14811 word = ml_get_cursor();
14812 }
14813 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14814 {
14815 char_u *str = get_tv_string_chk(&argvars[0]);
14816 int capcol = -1;
14817
14818 if (str != NULL)
14819 {
14820 /* Check the argument for spelling. */
14821 while (*str != NUL)
14822 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014823 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014824 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014825 {
14826 word = str;
14827 break;
14828 }
14829 str += len;
14830 }
14831 }
14832 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014833#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014834
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014835 list_append_string(rettv->vval.v_list, word, len);
14836 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014837 attr == HLF_SPB ? "bad" :
14838 attr == HLF_SPR ? "rare" :
14839 attr == HLF_SPL ? "local" :
14840 attr == HLF_SPC ? "caps" :
14841 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014842}
14843
14844/*
14845 * "spellsuggest()" function
14846 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014847/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014848 static void
14849f_spellsuggest(argvars, rettv)
14850 typval_T *argvars;
14851 typval_T *rettv;
14852{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014853#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014854 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014855 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014856 int maxcount;
14857 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014858 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014859 listitem_T *li;
14860 int need_capital = FALSE;
14861#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014862
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014863 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014864 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014865
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014866#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014867 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14868 {
14869 str = get_tv_string(&argvars[0]);
14870 if (argvars[1].v_type != VAR_UNKNOWN)
14871 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014872 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014873 if (maxcount <= 0)
14874 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014875 if (argvars[2].v_type != VAR_UNKNOWN)
14876 {
14877 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14878 if (typeerr)
14879 return;
14880 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014881 }
14882 else
14883 maxcount = 25;
14884
Bram Moolenaar4770d092006-01-12 23:22:24 +000014885 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014886
14887 for (i = 0; i < ga.ga_len; ++i)
14888 {
14889 str = ((char_u **)ga.ga_data)[i];
14890
14891 li = listitem_alloc();
14892 if (li == NULL)
14893 vim_free(str);
14894 else
14895 {
14896 li->li_tv.v_type = VAR_STRING;
14897 li->li_tv.v_lock = 0;
14898 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014899 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014900 }
14901 }
14902 ga_clear(&ga);
14903 }
14904#endif
14905}
14906
Bram Moolenaar0d660222005-01-07 21:51:51 +000014907 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014908f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 typval_T *argvars;
14910 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014911{
14912 char_u *str;
14913 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014914 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014915 regmatch_T regmatch;
14916 char_u patbuf[NUMBUFLEN];
14917 char_u *save_cpo;
14918 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014920 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014921 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922
14923 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14924 save_cpo = p_cpo;
14925 p_cpo = (char_u *)"";
14926
14927 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014928 if (argvars[1].v_type != VAR_UNKNOWN)
14929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014930 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14931 if (pat == NULL)
14932 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014934 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014935 }
14936 if (pat == NULL || *pat == NUL)
14937 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014939 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014941 if (typeerr)
14942 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14945 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014948 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014950 if (*str == NUL)
14951 match = FALSE; /* empty item at the end */
14952 else
14953 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954 if (match)
14955 end = regmatch.startp[0];
14956 else
14957 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014958 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14959 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014961 if (list_append_string(rettv->vval.v_list, str,
14962 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014964 }
14965 if (!match)
14966 break;
14967 /* Advance to just after the match. */
14968 if (regmatch.endp[0] > str)
14969 col = 0;
14970 else
14971 {
14972 /* Don't get stuck at the same match. */
14973#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014974 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014975#else
14976 col = 1;
14977#endif
14978 }
14979 str = regmatch.endp[0];
14980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986}
14987
Bram Moolenaar2c932302006-03-18 21:42:09 +000014988/*
14989 * "str2nr()" function
14990 */
14991 static void
14992f_str2nr(argvars, rettv)
14993 typval_T *argvars;
14994 typval_T *rettv;
14995{
14996 int base = 10;
14997 char_u *p;
14998 long n;
14999
15000 if (argvars[1].v_type != VAR_UNKNOWN)
15001 {
15002 base = get_tv_number(&argvars[1]);
15003 if (base != 8 && base != 10 && base != 16)
15004 {
15005 EMSG(_(e_invarg));
15006 return;
15007 }
15008 }
15009
15010 p = skipwhite(get_tv_string(&argvars[0]));
15011 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15012 rettv->vval.v_number = n;
15013}
15014
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015#ifdef HAVE_STRFTIME
15016/*
15017 * "strftime({format}[, {time}])" function
15018 */
15019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015020f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015021 typval_T *argvars;
15022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023{
15024 char_u result_buf[256];
15025 struct tm *curtime;
15026 time_t seconds;
15027 char_u *p;
15028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015032 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 seconds = time(NULL);
15034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015035 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036 curtime = localtime(&seconds);
15037 /* MSVC returns NULL for an invalid value of seconds. */
15038 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 else
15041 {
15042# ifdef FEAT_MBYTE
15043 vimconv_T conv;
15044 char_u *enc;
15045
15046 conv.vc_type = CONV_NONE;
15047 enc = enc_locale();
15048 convert_setup(&conv, p_enc, enc);
15049 if (conv.vc_type != CONV_NONE)
15050 p = string_convert(&conv, p, NULL);
15051# endif
15052 if (p != NULL)
15053 (void)strftime((char *)result_buf, sizeof(result_buf),
15054 (char *)p, curtime);
15055 else
15056 result_buf[0] = NUL;
15057
15058# ifdef FEAT_MBYTE
15059 if (conv.vc_type != CONV_NONE)
15060 vim_free(p);
15061 convert_setup(&conv, enc, p_enc);
15062 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 else
15065# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015066 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067
15068# ifdef FEAT_MBYTE
15069 /* Release conversion descriptors */
15070 convert_setup(&conv, NULL, NULL);
15071 vim_free(enc);
15072# endif
15073 }
15074}
15075#endif
15076
15077/*
15078 * "stridx()" function
15079 */
15080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 typval_T *argvars;
15083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084{
15085 char_u buf[NUMBUFLEN];
15086 char_u *needle;
15087 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015090 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 needle = get_tv_string_chk(&argvars[1]);
15093 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015094 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 if (needle == NULL || haystack == NULL)
15096 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 if (argvars[2].v_type != VAR_UNKNOWN)
15099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015100 int error = FALSE;
15101
15102 start_idx = get_tv_number_chk(&argvars[2], &error);
15103 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015105 if (start_idx >= 0)
15106 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015107 }
15108
15109 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15110 if (pos != NULL)
15111 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112}
15113
15114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015115 * "string()" function
15116 */
15117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015118f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015119 typval_T *argvars;
15120 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015121{
15122 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015123 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015126 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015127 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129}
15130
15131/*
15132 * "strlen()" function
15133 */
15134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 typval_T *argvars;
15137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_number = (varnumber_T)(STRLEN(
15140 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141}
15142
15143/*
15144 * "strpart()" function
15145 */
15146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 typval_T *argvars;
15149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150{
15151 char_u *p;
15152 int n;
15153 int len;
15154 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 slen = (int)STRLEN(p);
15159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015160 n = get_tv_number_chk(&argvars[1], &error);
15161 if (error)
15162 len = 0;
15163 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015164 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 else
15166 len = slen - n; /* default len: all bytes that are available. */
15167
15168 /*
15169 * Only return the overlap between the specified part and the actual
15170 * string.
15171 */
15172 if (n < 0)
15173 {
15174 len += n;
15175 n = 0;
15176 }
15177 else if (n > slen)
15178 n = slen;
15179 if (len < 0)
15180 len = 0;
15181 else if (n + len > slen)
15182 len = slen - n;
15183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184 rettv->v_type = VAR_STRING;
15185 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
15188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189 * "strridx()" function
15190 */
15191 static void
15192f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *argvars;
15194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195{
15196 char_u buf[NUMBUFLEN];
15197 char_u *needle;
15198 char_u *haystack;
15199 char_u *rest;
15200 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015201 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 needle = get_tv_string_chk(&argvars[1]);
15204 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205
15206 rettv->vval.v_number = -1;
15207 if (needle == NULL || haystack == NULL)
15208 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015209
15210 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015211 if (argvars[2].v_type != VAR_UNKNOWN)
15212 {
15213 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015214 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015215 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015217 }
15218 else
15219 end_idx = haystack_len;
15220
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015222 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015224 lastmatch = haystack + end_idx;
15225 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015226 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015227 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228 for (rest = haystack; *rest != '\0'; ++rest)
15229 {
15230 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015231 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015232 break;
15233 lastmatch = rest;
15234 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015235 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236
15237 if (lastmatch == NULL)
15238 rettv->vval.v_number = -1;
15239 else
15240 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15241}
15242
15243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 * "strtrans()" function
15245 */
15246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T *argvars;
15249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 rettv->v_type = VAR_STRING;
15252 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253}
15254
15255/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256 * "submatch()" function
15257 */
15258 static void
15259f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *argvars;
15261 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015262{
15263 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015264 rettv->vval.v_string =
15265 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015266}
15267
15268/*
15269 * "substitute()" function
15270 */
15271 static void
15272f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 typval_T *argvars;
15274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275{
15276 char_u patbuf[NUMBUFLEN];
15277 char_u subbuf[NUMBUFLEN];
15278 char_u flagsbuf[NUMBUFLEN];
15279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 char_u *str = get_tv_string_chk(&argvars[0]);
15281 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15282 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15283 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15284
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15287 rettv->vval.v_string = NULL;
15288 else
15289 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290}
15291
15292/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015293 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 */
15295/*ARGSUSED*/
15296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015297f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015298 typval_T *argvars;
15299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300{
15301 int id = 0;
15302#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015303 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 long col;
15305 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015306 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015308 lnum = get_tv_lnum(argvars); /* -1 on type error */
15309 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15310 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015312 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015313 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015314 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315#endif
15316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318}
15319
15320/*
15321 * "synIDattr(id, what [, mode])" function
15322 */
15323/*ARGSUSED*/
15324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 typval_T *argvars;
15327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328{
15329 char_u *p = NULL;
15330#ifdef FEAT_SYN_HL
15331 int id;
15332 char_u *what;
15333 char_u *mode;
15334 char_u modebuf[NUMBUFLEN];
15335 int modec;
15336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 id = get_tv_number(&argvars[0]);
15338 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015339 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 modec = TOLOWER_ASC(mode[0]);
15343 if (modec != 't' && modec != 'c'
15344#ifdef FEAT_GUI
15345 && modec != 'g'
15346#endif
15347 )
15348 modec = 0; /* replace invalid with current */
15349 }
15350 else
15351 {
15352#ifdef FEAT_GUI
15353 if (gui.in_use)
15354 modec = 'g';
15355 else
15356#endif
15357 if (t_colors > 1)
15358 modec = 'c';
15359 else
15360 modec = 't';
15361 }
15362
15363
15364 switch (TOLOWER_ASC(what[0]))
15365 {
15366 case 'b':
15367 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15368 p = highlight_color(id, what, modec);
15369 else /* bold */
15370 p = highlight_has_attr(id, HL_BOLD, modec);
15371 break;
15372
15373 case 'f': /* fg[#] */
15374 p = highlight_color(id, what, modec);
15375 break;
15376
15377 case 'i':
15378 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15379 p = highlight_has_attr(id, HL_INVERSE, modec);
15380 else /* italic */
15381 p = highlight_has_attr(id, HL_ITALIC, modec);
15382 break;
15383
15384 case 'n': /* name */
15385 p = get_highlight_name(NULL, id - 1);
15386 break;
15387
15388 case 'r': /* reverse */
15389 p = highlight_has_attr(id, HL_INVERSE, modec);
15390 break;
15391
15392 case 's': /* standout */
15393 p = highlight_has_attr(id, HL_STANDOUT, modec);
15394 break;
15395
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015396 case 'u':
15397 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15398 /* underline */
15399 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15400 else
15401 /* undercurl */
15402 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 break;
15404 }
15405
15406 if (p != NULL)
15407 p = vim_strsave(p);
15408#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 rettv->v_type = VAR_STRING;
15410 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411}
15412
15413/*
15414 * "synIDtrans(id)" function
15415 */
15416/*ARGSUSED*/
15417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015418f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015419 typval_T *argvars;
15420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421{
15422 int id;
15423
15424#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015425 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426
15427 if (id > 0)
15428 id = syn_get_final_id(id);
15429 else
15430#endif
15431 id = 0;
15432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434}
15435
15436/*
15437 * "system()" function
15438 */
15439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015441 typval_T *argvars;
15442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015444 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015446 char_u *infile = NULL;
15447 char_u buf[NUMBUFLEN];
15448 int err = FALSE;
15449 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015451 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015452 {
15453 /*
15454 * Write the string to a temp file, to be used for input of the shell
15455 * command.
15456 */
15457 if ((infile = vim_tempname('i')) == NULL)
15458 {
15459 EMSG(_(e_notmp));
15460 return;
15461 }
15462
15463 fd = mch_fopen((char *)infile, WRITEBIN);
15464 if (fd == NULL)
15465 {
15466 EMSG2(_(e_notopen), infile);
15467 goto done;
15468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015469 p = get_tv_string_buf_chk(&argvars[1], buf);
15470 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015471 {
15472 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015473 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015474 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015475 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15476 err = TRUE;
15477 if (fclose(fd) != 0)
15478 err = TRUE;
15479 if (err)
15480 {
15481 EMSG(_("E677: Error writing temp file"));
15482 goto done;
15483 }
15484 }
15485
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015486 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15487 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015488
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489#ifdef USE_CR
15490 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015491 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 {
15493 char_u *s;
15494
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015495 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 {
15497 if (*s == CAR)
15498 *s = NL;
15499 }
15500 }
15501#else
15502# ifdef USE_CRNL
15503 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015504 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 {
15506 char_u *s, *d;
15507
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015508 d = res;
15509 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 {
15511 if (s[0] == CAR && s[1] == NL)
15512 ++s;
15513 *d++ = *s;
15514 }
15515 *d = NUL;
15516 }
15517# endif
15518#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015519
15520done:
15521 if (infile != NULL)
15522 {
15523 mch_remove(infile);
15524 vim_free(infile);
15525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015526 rettv->v_type = VAR_STRING;
15527 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528}
15529
15530/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015531 * "tabpagebuflist()" function
15532 */
15533/* ARGSUSED */
15534 static void
15535f_tabpagebuflist(argvars, rettv)
15536 typval_T *argvars;
15537 typval_T *rettv;
15538{
15539#ifndef FEAT_WINDOWS
15540 rettv->vval.v_number = 0;
15541#else
15542 tabpage_T *tp;
15543 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015544
15545 if (argvars[0].v_type == VAR_UNKNOWN)
15546 wp = firstwin;
15547 else
15548 {
15549 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15550 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015551 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015552 }
15553 if (wp == NULL)
15554 rettv->vval.v_number = 0;
15555 else
15556 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015557 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015558 rettv->vval.v_number = 0;
15559 else
15560 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015561 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015562 if (list_append_number(rettv->vval.v_list,
15563 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015564 break;
15565 }
15566 }
15567#endif
15568}
15569
15570
15571/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015572 * "tabpagenr()" function
15573 */
15574/* ARGSUSED */
15575 static void
15576f_tabpagenr(argvars, rettv)
15577 typval_T *argvars;
15578 typval_T *rettv;
15579{
15580 int nr = 1;
15581#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015582 char_u *arg;
15583
15584 if (argvars[0].v_type != VAR_UNKNOWN)
15585 {
15586 arg = get_tv_string_chk(&argvars[0]);
15587 nr = 0;
15588 if (arg != NULL)
15589 {
15590 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015591 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015592 else
15593 EMSG2(_(e_invexpr2), arg);
15594 }
15595 }
15596 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015597 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015598#endif
15599 rettv->vval.v_number = nr;
15600}
15601
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015602
15603#ifdef FEAT_WINDOWS
15604static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15605
15606/*
15607 * Common code for tabpagewinnr() and winnr().
15608 */
15609 static int
15610get_winnr(tp, argvar)
15611 tabpage_T *tp;
15612 typval_T *argvar;
15613{
15614 win_T *twin;
15615 int nr = 1;
15616 win_T *wp;
15617 char_u *arg;
15618
15619 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15620 if (argvar->v_type != VAR_UNKNOWN)
15621 {
15622 arg = get_tv_string_chk(argvar);
15623 if (arg == NULL)
15624 nr = 0; /* type error; errmsg already given */
15625 else if (STRCMP(arg, "$") == 0)
15626 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15627 else if (STRCMP(arg, "#") == 0)
15628 {
15629 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15630 if (twin == NULL)
15631 nr = 0;
15632 }
15633 else
15634 {
15635 EMSG2(_(e_invexpr2), arg);
15636 nr = 0;
15637 }
15638 }
15639
15640 if (nr > 0)
15641 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15642 wp != twin; wp = wp->w_next)
15643 ++nr;
15644 return nr;
15645}
15646#endif
15647
15648/*
15649 * "tabpagewinnr()" function
15650 */
15651/* ARGSUSED */
15652 static void
15653f_tabpagewinnr(argvars, rettv)
15654 typval_T *argvars;
15655 typval_T *rettv;
15656{
15657 int nr = 1;
15658#ifdef FEAT_WINDOWS
15659 tabpage_T *tp;
15660
15661 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15662 if (tp == NULL)
15663 nr = 0;
15664 else
15665 nr = get_winnr(tp, &argvars[1]);
15666#endif
15667 rettv->vval.v_number = nr;
15668}
15669
15670
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015671/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015672 * "tagfiles()" function
15673 */
15674/*ARGSUSED*/
15675 static void
15676f_tagfiles(argvars, rettv)
15677 typval_T *argvars;
15678 typval_T *rettv;
15679{
15680 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015681 tagname_T tn;
15682 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015683
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015684 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015685 {
15686 rettv->vval.v_number = 0;
15687 return;
15688 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015690 for (first = TRUE; ; first = FALSE)
15691 if (get_tagfname(&tn, first, fname) == FAIL
15692 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015693 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015694 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015695}
15696
15697/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015698 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015699 */
15700 static void
15701f_taglist(argvars, rettv)
15702 typval_T *argvars;
15703 typval_T *rettv;
15704{
15705 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015706
15707 tag_pattern = get_tv_string(&argvars[0]);
15708
15709 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 if (*tag_pattern == NUL)
15711 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015712
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015713 if (rettv_list_alloc(rettv) == OK)
15714 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015715}
15716
15717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 * "tempname()" function
15719 */
15720/*ARGSUSED*/
15721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 typval_T *argvars;
15724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725{
15726 static int x = 'A';
15727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015728 rettv->v_type = VAR_STRING;
15729 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730
15731 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15732 * names. Skip 'I' and 'O', they are used for shell redirection. */
15733 do
15734 {
15735 if (x == 'Z')
15736 x = '0';
15737 else if (x == '9')
15738 x = 'A';
15739 else
15740 {
15741#ifdef EBCDIC
15742 if (x == 'I')
15743 x = 'J';
15744 else if (x == 'R')
15745 x = 'S';
15746 else
15747#endif
15748 ++x;
15749 }
15750 } while (x == 'I' || x == 'O');
15751}
15752
15753/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015754 * "test(list)" function: Just checking the walls...
15755 */
15756/*ARGSUSED*/
15757 static void
15758f_test(argvars, rettv)
15759 typval_T *argvars;
15760 typval_T *rettv;
15761{
15762 /* Used for unit testing. Change the code below to your liking. */
15763#if 0
15764 listitem_T *li;
15765 list_T *l;
15766 char_u *bad, *good;
15767
15768 if (argvars[0].v_type != VAR_LIST)
15769 return;
15770 l = argvars[0].vval.v_list;
15771 if (l == NULL)
15772 return;
15773 li = l->lv_first;
15774 if (li == NULL)
15775 return;
15776 bad = get_tv_string(&li->li_tv);
15777 li = li->li_next;
15778 if (li == NULL)
15779 return;
15780 good = get_tv_string(&li->li_tv);
15781 rettv->vval.v_number = test_edit_score(bad, good);
15782#endif
15783}
15784
15785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 * "tolower(string)" function
15787 */
15788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015789f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015790 typval_T *argvars;
15791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792{
15793 char_u *p;
15794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015795 p = vim_strsave(get_tv_string(&argvars[0]));
15796 rettv->v_type = VAR_STRING;
15797 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798
15799 if (p != NULL)
15800 while (*p != NUL)
15801 {
15802#ifdef FEAT_MBYTE
15803 int l;
15804
15805 if (enc_utf8)
15806 {
15807 int c, lc;
15808
15809 c = utf_ptr2char(p);
15810 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015811 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 /* TODO: reallocate string when byte count changes. */
15813 if (utf_char2len(lc) == l)
15814 utf_char2bytes(lc, p);
15815 p += l;
15816 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015817 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 p += l; /* skip multi-byte character */
15819 else
15820#endif
15821 {
15822 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15823 ++p;
15824 }
15825 }
15826}
15827
15828/*
15829 * "toupper(string)" function
15830 */
15831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015832f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 typval_T *argvars;
15834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015836 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015837 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838}
15839
15840/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015841 * "tr(string, fromstr, tostr)" function
15842 */
15843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015844f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015845 typval_T *argvars;
15846 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015847{
15848 char_u *instr;
15849 char_u *fromstr;
15850 char_u *tostr;
15851 char_u *p;
15852#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015853 int inlen;
15854 int fromlen;
15855 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015856 int idx;
15857 char_u *cpstr;
15858 int cplen;
15859 int first = TRUE;
15860#endif
15861 char_u buf[NUMBUFLEN];
15862 char_u buf2[NUMBUFLEN];
15863 garray_T ga;
15864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015865 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15867 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015868
15869 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015870 rettv->v_type = VAR_STRING;
15871 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 if (fromstr == NULL || tostr == NULL)
15873 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015874 ga_init2(&ga, (int)sizeof(char), 80);
15875
15876#ifdef FEAT_MBYTE
15877 if (!has_mbyte)
15878#endif
15879 /* not multi-byte: fromstr and tostr must be the same length */
15880 if (STRLEN(fromstr) != STRLEN(tostr))
15881 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015882#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015883error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015884#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015885 EMSG2(_(e_invarg2), fromstr);
15886 ga_clear(&ga);
15887 return;
15888 }
15889
15890 /* fromstr and tostr have to contain the same number of chars */
15891 while (*instr != NUL)
15892 {
15893#ifdef FEAT_MBYTE
15894 if (has_mbyte)
15895 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015896 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015897 cpstr = instr;
15898 cplen = inlen;
15899 idx = 0;
15900 for (p = fromstr; *p != NUL; p += fromlen)
15901 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015902 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015903 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15904 {
15905 for (p = tostr; *p != NUL; p += tolen)
15906 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015907 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015908 if (idx-- == 0)
15909 {
15910 cplen = tolen;
15911 cpstr = p;
15912 break;
15913 }
15914 }
15915 if (*p == NUL) /* tostr is shorter than fromstr */
15916 goto error;
15917 break;
15918 }
15919 ++idx;
15920 }
15921
15922 if (first && cpstr == instr)
15923 {
15924 /* Check that fromstr and tostr have the same number of
15925 * (multi-byte) characters. Done only once when a character
15926 * of instr doesn't appear in fromstr. */
15927 first = FALSE;
15928 for (p = tostr; *p != NUL; p += tolen)
15929 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015930 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015931 --idx;
15932 }
15933 if (idx != 0)
15934 goto error;
15935 }
15936
15937 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015938 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015939 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015940
15941 instr += inlen;
15942 }
15943 else
15944#endif
15945 {
15946 /* When not using multi-byte chars we can do it faster. */
15947 p = vim_strchr(fromstr, *instr);
15948 if (p != NULL)
15949 ga_append(&ga, tostr[p - fromstr]);
15950 else
15951 ga_append(&ga, *instr);
15952 ++instr;
15953 }
15954 }
15955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015956 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015957}
15958
15959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 * "type(expr)" function
15961 */
15962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015963f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 typval_T *argvars;
15965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015967 int n;
15968
15969 switch (argvars[0].v_type)
15970 {
15971 case VAR_NUMBER: n = 0; break;
15972 case VAR_STRING: n = 1; break;
15973 case VAR_FUNC: n = 2; break;
15974 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015975 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015976 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15977 }
15978 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979}
15980
15981/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015982 * "values(dict)" function
15983 */
15984 static void
15985f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015986 typval_T *argvars;
15987 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015988{
15989 dict_list(argvars, rettv, 1);
15990}
15991
15992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 * "virtcol(string)" function
15994 */
15995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *argvars;
15998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999{
16000 colnr_T vcol = 0;
16001 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016002 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016004 fp = var2fpos(&argvars[0], FALSE, &fnum);
16005 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16006 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 {
16008 getvvcol(curwin, fp, NULL, NULL, &vcol);
16009 ++vcol;
16010 }
16011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013}
16014
16015/*
16016 * "visualmode()" function
16017 */
16018/*ARGSUSED*/
16019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016020f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 typval_T *argvars;
16022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023{
16024#ifdef FEAT_VISUAL
16025 char_u str[2];
16026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016027 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 str[0] = curbuf->b_visual_mode_eval;
16029 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016030 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031
16032 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016033 if ((argvars[0].v_type == VAR_NUMBER
16034 && argvars[0].vval.v_number != 0)
16035 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016036 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 curbuf->b_visual_mode_eval = NUL;
16038#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040#endif
16041}
16042
16043/*
16044 * "winbufnr(nr)" function
16045 */
16046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016047f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016048 typval_T *argvars;
16049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050{
16051 win_T *wp;
16052
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016053 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058}
16059
16060/*
16061 * "wincol()" function
16062 */
16063/*ARGSUSED*/
16064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 typval_T *argvars;
16067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068{
16069 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071}
16072
16073/*
16074 * "winheight(nr)" function
16075 */
16076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016077f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016078 typval_T *argvars;
16079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080{
16081 win_T *wp;
16082
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016083 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016087 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088}
16089
16090/*
16091 * "winline()" function
16092 */
16093/*ARGSUSED*/
16094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016096 typval_T *argvars;
16097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098{
16099 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101}
16102
16103/*
16104 * "winnr()" function
16105 */
16106/* ARGSUSED */
16107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016108f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 typval_T *argvars;
16110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111{
16112 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016113
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016115 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016117 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118}
16119
16120/*
16121 * "winrestcmd()" function
16122 */
16123/* ARGSUSED */
16124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016125f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 typval_T *argvars;
16127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128{
16129#ifdef FEAT_WINDOWS
16130 win_T *wp;
16131 int winnr = 1;
16132 garray_T ga;
16133 char_u buf[50];
16134
16135 ga_init2(&ga, (int)sizeof(char), 70);
16136 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16137 {
16138 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16139 ga_concat(&ga, buf);
16140# ifdef FEAT_VERTSPLIT
16141 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16142 ga_concat(&ga, buf);
16143# endif
16144 ++winnr;
16145 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016146 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016152 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153}
16154
16155/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016156 * "winrestview()" function
16157 */
16158/* ARGSUSED */
16159 static void
16160f_winrestview(argvars, rettv)
16161 typval_T *argvars;
16162 typval_T *rettv;
16163{
16164 dict_T *dict;
16165
16166 if (argvars[0].v_type != VAR_DICT
16167 || (dict = argvars[0].vval.v_dict) == NULL)
16168 EMSG(_(e_invarg));
16169 else
16170 {
16171 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16172 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16173#ifdef FEAT_VIRTUALEDIT
16174 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16175#endif
16176 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016177 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016178
16179 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16180#ifdef FEAT_DIFF
16181 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16182#endif
16183 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16184 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16185
16186 check_cursor();
16187 changed_cline_bef_curs();
16188 invalidate_botline();
16189 redraw_later(VALID);
16190
16191 if (curwin->w_topline == 0)
16192 curwin->w_topline = 1;
16193 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16194 curwin->w_topline = curbuf->b_ml.ml_line_count;
16195#ifdef FEAT_DIFF
16196 check_topfill(curwin, TRUE);
16197#endif
16198 }
16199}
16200
16201/*
16202 * "winsaveview()" function
16203 */
16204/* ARGSUSED */
16205 static void
16206f_winsaveview(argvars, rettv)
16207 typval_T *argvars;
16208 typval_T *rettv;
16209{
16210 dict_T *dict;
16211
16212 dict = dict_alloc();
16213 if (dict == NULL)
16214 return;
16215 rettv->v_type = VAR_DICT;
16216 rettv->vval.v_dict = dict;
16217 ++dict->dv_refcount;
16218
16219 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16220 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16221#ifdef FEAT_VIRTUALEDIT
16222 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16223#endif
16224 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16225
16226 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16227#ifdef FEAT_DIFF
16228 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16229#endif
16230 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16231 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16232}
16233
16234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 * "winwidth(nr)" function
16236 */
16237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016238f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 typval_T *argvars;
16240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241{
16242 win_T *wp;
16243
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016244 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 else
16248#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016249 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016251 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252#endif
16253}
16254
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016256 * "writefile()" function
16257 */
16258 static void
16259f_writefile(argvars, rettv)
16260 typval_T *argvars;
16261 typval_T *rettv;
16262{
16263 int binary = FALSE;
16264 char_u *fname;
16265 FILE *fd;
16266 listitem_T *li;
16267 char_u *s;
16268 int ret = 0;
16269 int c;
16270
16271 if (argvars[0].v_type != VAR_LIST)
16272 {
16273 EMSG2(_(e_listarg), "writefile()");
16274 return;
16275 }
16276 if (argvars[0].vval.v_list == NULL)
16277 return;
16278
16279 if (argvars[2].v_type != VAR_UNKNOWN
16280 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16281 binary = TRUE;
16282
16283 /* Always open the file in binary mode, library functions have a mind of
16284 * their own about CR-LF conversion. */
16285 fname = get_tv_string(&argvars[1]);
16286 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16287 {
16288 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16289 ret = -1;
16290 }
16291 else
16292 {
16293 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16294 li = li->li_next)
16295 {
16296 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16297 {
16298 if (*s == '\n')
16299 c = putc(NUL, fd);
16300 else
16301 c = putc(*s, fd);
16302 if (c == EOF)
16303 {
16304 ret = -1;
16305 break;
16306 }
16307 }
16308 if (!binary || li->li_next != NULL)
16309 if (putc('\n', fd) == EOF)
16310 {
16311 ret = -1;
16312 break;
16313 }
16314 if (ret < 0)
16315 {
16316 EMSG(_(e_write));
16317 break;
16318 }
16319 }
16320 fclose(fd);
16321 }
16322
16323 rettv->vval.v_number = ret;
16324}
16325
16326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016328 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 */
16330 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016331var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016334 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016336 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016338 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339
Bram Moolenaara5525202006-03-02 22:52:09 +000016340 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016341 if (varp->v_type == VAR_LIST)
16342 {
16343 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016344 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016345 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016346
16347 l = varp->vval.v_list;
16348 if (l == NULL)
16349 return NULL;
16350
16351 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016352 pos.lnum = list_find_nr(l, 0L, &error);
16353 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016354 return NULL; /* invalid line number */
16355
16356 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016357 pos.col = list_find_nr(l, 1L, &error);
16358 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016359 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016360 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016361 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016362 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016363 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016364 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016365
Bram Moolenaara5525202006-03-02 22:52:09 +000016366#ifdef FEAT_VIRTUALEDIT
16367 /* Get the virtual offset. Defaults to zero. */
16368 pos.coladd = list_find_nr(l, 2L, &error);
16369 if (error)
16370 pos.coladd = 0;
16371#endif
16372
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016373 return &pos;
16374 }
16375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016376 name = get_tv_string_chk(varp);
16377 if (name == NULL)
16378 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 if (name[0] == '.') /* cursor */
16380 return &curwin->w_cursor;
16381 if (name[0] == '\'') /* mark */
16382 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016383 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16385 return NULL;
16386 return pp;
16387 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016388
16389#ifdef FEAT_VIRTUALEDIT
16390 pos.coladd = 0;
16391#endif
16392
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016393 if (name[0] == 'w' && lnum)
16394 {
16395 pos.col = 0;
16396 if (name[1] == '0') /* "w0": first visible line */
16397 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016398 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016399 pos.lnum = curwin->w_topline;
16400 return &pos;
16401 }
16402 else if (name[1] == '$') /* "w$": last visible line */
16403 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016404 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016405 pos.lnum = curwin->w_botline - 1;
16406 return &pos;
16407 }
16408 }
16409 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 {
16411 if (lnum)
16412 {
16413 pos.lnum = curbuf->b_ml.ml_line_count;
16414 pos.col = 0;
16415 }
16416 else
16417 {
16418 pos.lnum = curwin->w_cursor.lnum;
16419 pos.col = (colnr_T)STRLEN(ml_get_curline());
16420 }
16421 return &pos;
16422 }
16423 return NULL;
16424}
16425
16426/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016427 * Convert list in "arg" into a position and optional file number.
16428 * When "fnump" is NULL there is no file number, only 3 items.
16429 * Note that the column is passed on as-is, the caller may want to decrement
16430 * it to use 1 for the first column.
16431 * Return FAIL when conversion is not possible, doesn't check the position for
16432 * validity.
16433 */
16434 static int
16435list2fpos(arg, posp, fnump)
16436 typval_T *arg;
16437 pos_T *posp;
16438 int *fnump;
16439{
16440 list_T *l = arg->vval.v_list;
16441 long i = 0;
16442 long n;
16443
16444 /* List must be: [fnum, lnum, col, coladd] */
16445 if (arg->v_type != VAR_LIST || l == NULL
16446 || l->lv_len != (fnump == NULL ? 3 : 4))
16447 return FAIL;
16448
16449 if (fnump != NULL)
16450 {
16451 n = list_find_nr(l, i++, NULL); /* fnum */
16452 if (n < 0)
16453 return FAIL;
16454 if (n == 0)
16455 n = curbuf->b_fnum; /* current buffer */
16456 *fnump = n;
16457 }
16458
16459 n = list_find_nr(l, i++, NULL); /* lnum */
16460 if (n < 0)
16461 return FAIL;
16462 posp->lnum = n;
16463
16464 n = list_find_nr(l, i++, NULL); /* col */
16465 if (n < 0)
16466 return FAIL;
16467 posp->col = n;
16468
16469#ifdef FEAT_VIRTUALEDIT
16470 n = list_find_nr(l, i, NULL);
16471 if (n < 0)
16472 return FAIL;
16473 posp->coladd = n;
16474#endif
16475
16476 return OK;
16477}
16478
16479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 * Get the length of an environment variable name.
16481 * Advance "arg" to the first character after the name.
16482 * Return 0 for error.
16483 */
16484 static int
16485get_env_len(arg)
16486 char_u **arg;
16487{
16488 char_u *p;
16489 int len;
16490
16491 for (p = *arg; vim_isIDc(*p); ++p)
16492 ;
16493 if (p == *arg) /* no name found */
16494 return 0;
16495
16496 len = (int)(p - *arg);
16497 *arg = p;
16498 return len;
16499}
16500
16501/*
16502 * Get the length of the name of a function or internal variable.
16503 * "arg" is advanced to the first non-white character after the name.
16504 * Return 0 if something is wrong.
16505 */
16506 static int
16507get_id_len(arg)
16508 char_u **arg;
16509{
16510 char_u *p;
16511 int len;
16512
16513 /* Find the end of the name. */
16514 for (p = *arg; eval_isnamec(*p); ++p)
16515 ;
16516 if (p == *arg) /* no name found */
16517 return 0;
16518
16519 len = (int)(p - *arg);
16520 *arg = skipwhite(p);
16521
16522 return len;
16523}
16524
16525/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016526 * Get the length of the name of a variable or function.
16527 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016529 * Return -1 if curly braces expansion failed.
16530 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 * If the name contains 'magic' {}'s, expand them and return the
16532 * expanded name in an allocated string via 'alias' - caller must free.
16533 */
16534 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016535get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 char_u **arg;
16537 char_u **alias;
16538 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016539 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540{
16541 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 char_u *p;
16543 char_u *expr_start;
16544 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545
16546 *alias = NULL; /* default to no alias */
16547
16548 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16549 && (*arg)[2] == (int)KE_SNR)
16550 {
16551 /* hard coded <SNR>, already translated */
16552 *arg += 3;
16553 return get_id_len(arg) + 3;
16554 }
16555 len = eval_fname_script(*arg);
16556 if (len > 0)
16557 {
16558 /* literal "<SID>", "s:" or "<SNR>" */
16559 *arg += len;
16560 }
16561
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016565 p = find_name_end(*arg, &expr_start, &expr_end,
16566 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 if (expr_start != NULL)
16568 {
16569 char_u *temp_string;
16570
16571 if (!evaluate)
16572 {
16573 len += (int)(p - *arg);
16574 *arg = skipwhite(p);
16575 return len;
16576 }
16577
16578 /*
16579 * Include any <SID> etc in the expanded string:
16580 * Thus the -len here.
16581 */
16582 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16583 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016584 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 *alias = temp_string;
16586 *arg = skipwhite(p);
16587 return (int)STRLEN(temp_string);
16588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589
16590 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016591 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 EMSG2(_(e_invexpr2), *arg);
16593
16594 return len;
16595}
16596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016597/*
16598 * Find the end of a variable or function name, taking care of magic braces.
16599 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16600 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016601 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016602 * Return a pointer to just after the name. Equal to "arg" if there is no
16603 * valid name.
16604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016606find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 char_u *arg;
16608 char_u **expr_start;
16609 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016610 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016612 int mb_nest = 0;
16613 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 char_u *p;
16615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016616 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016618 *expr_start = NULL;
16619 *expr_end = NULL;
16620 }
16621
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016622 /* Quick check for valid starting character. */
16623 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16624 return arg;
16625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 for (p = arg; *p != NUL
16627 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016628 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016629 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016631 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016633 if (*p == '\'')
16634 {
16635 /* skip over 'string' to avoid counting [ and ] inside it. */
16636 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16637 ;
16638 if (*p == NUL)
16639 break;
16640 }
16641 else if (*p == '"')
16642 {
16643 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16644 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16645 if (*p == '\\' && p[1] != NUL)
16646 ++p;
16647 if (*p == NUL)
16648 break;
16649 }
16650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016653 if (*p == '[')
16654 ++br_nest;
16655 else if (*p == ']')
16656 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016659 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661 if (*p == '{')
16662 {
16663 mb_nest++;
16664 if (expr_start != NULL && *expr_start == NULL)
16665 *expr_start = p;
16666 }
16667 else if (*p == '}')
16668 {
16669 mb_nest--;
16670 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16671 *expr_end = p;
16672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 }
16675
16676 return p;
16677}
16678
16679/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016680 * Expands out the 'magic' {}'s in a variable/function name.
16681 * Note that this can call itself recursively, to deal with
16682 * constructs like foo{bar}{baz}{bam}
16683 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16684 * "in_start" ^
16685 * "expr_start" ^
16686 * "expr_end" ^
16687 * "in_end" ^
16688 *
16689 * Returns a new allocated string, which the caller must free.
16690 * Returns NULL for failure.
16691 */
16692 static char_u *
16693make_expanded_name(in_start, expr_start, expr_end, in_end)
16694 char_u *in_start;
16695 char_u *expr_start;
16696 char_u *expr_end;
16697 char_u *in_end;
16698{
16699 char_u c1;
16700 char_u *retval = NULL;
16701 char_u *temp_result;
16702 char_u *nextcmd = NULL;
16703
16704 if (expr_end == NULL || in_end == NULL)
16705 return NULL;
16706 *expr_start = NUL;
16707 *expr_end = NUL;
16708 c1 = *in_end;
16709 *in_end = NUL;
16710
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016711 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016712 if (temp_result != NULL && nextcmd == NULL)
16713 {
16714 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16715 + (in_end - expr_end) + 1));
16716 if (retval != NULL)
16717 {
16718 STRCPY(retval, in_start);
16719 STRCAT(retval, temp_result);
16720 STRCAT(retval, expr_end + 1);
16721 }
16722 }
16723 vim_free(temp_result);
16724
16725 *in_end = c1; /* put char back for error messages */
16726 *expr_start = '{';
16727 *expr_end = '}';
16728
16729 if (retval != NULL)
16730 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016731 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016732 if (expr_start != NULL)
16733 {
16734 /* Further expansion! */
16735 temp_result = make_expanded_name(retval, expr_start,
16736 expr_end, temp_result);
16737 vim_free(retval);
16738 retval = temp_result;
16739 }
16740 }
16741
16742 return retval;
16743}
16744
16745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016747 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 */
16749 static int
16750eval_isnamec(c)
16751 int c;
16752{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016753 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16754}
16755
16756/*
16757 * Return TRUE if character "c" can be used as the first character in a
16758 * variable or function name (excluding '{' and '}').
16759 */
16760 static int
16761eval_isnamec1(c)
16762 int c;
16763{
16764 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765}
16766
16767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 * Set number v: variable to "val".
16769 */
16770 void
16771set_vim_var_nr(idx, val)
16772 int idx;
16773 long val;
16774{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016775 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776}
16777
16778/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016779 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 */
16781 long
16782get_vim_var_nr(idx)
16783 int idx;
16784{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016785 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786}
16787
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016788#if defined(FEAT_AUTOCMD) || defined(PROTO)
16789/*
16790 * Get string v: variable value. Uses a static buffer, can only be used once.
16791 */
16792 char_u *
16793get_vim_var_str(idx)
16794 int idx;
16795{
16796 return get_tv_string(&vimvars[idx].vv_tv);
16797}
16798#endif
16799
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800/*
16801 * Set v:count, v:count1 and v:prevcount.
16802 */
16803 void
16804set_vcount(count, count1)
16805 long count;
16806 long count1;
16807{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016808 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16809 vimvars[VV_COUNT].vv_nr = count;
16810 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811}
16812
16813/*
16814 * Set string v: variable to a copy of "val".
16815 */
16816 void
16817set_vim_var_string(idx, val, len)
16818 int idx;
16819 char_u *val;
16820 int len; /* length of "val" to use or -1 (whole string) */
16821{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016822 /* Need to do this (at least) once, since we can't initialize a union.
16823 * Will always be invoked when "v:progname" is set. */
16824 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16825
Bram Moolenaare9a41262005-01-15 22:18:47 +000016826 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016828 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016830 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016832 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833}
16834
16835/*
16836 * Set v:register if needed.
16837 */
16838 void
16839set_reg_var(c)
16840 int c;
16841{
16842 char_u regname;
16843
16844 if (c == 0 || c == ' ')
16845 regname = '"';
16846 else
16847 regname = c;
16848 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016849 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 set_vim_var_string(VV_REG, &regname, 1);
16851}
16852
16853/*
16854 * Get or set v:exception. If "oldval" == NULL, return the current value.
16855 * Otherwise, restore the value to "oldval" and return NULL.
16856 * Must always be called in pairs to save and restore v:exception! Does not
16857 * take care of memory allocations.
16858 */
16859 char_u *
16860v_exception(oldval)
16861 char_u *oldval;
16862{
16863 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016864 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865
Bram Moolenaare9a41262005-01-15 22:18:47 +000016866 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 return NULL;
16868}
16869
16870/*
16871 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16872 * Otherwise, restore the value to "oldval" and return NULL.
16873 * Must always be called in pairs to save and restore v:throwpoint! Does not
16874 * take care of memory allocations.
16875 */
16876 char_u *
16877v_throwpoint(oldval)
16878 char_u *oldval;
16879{
16880 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016881 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882
Bram Moolenaare9a41262005-01-15 22:18:47 +000016883 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 return NULL;
16885}
16886
16887#if defined(FEAT_AUTOCMD) || defined(PROTO)
16888/*
16889 * Set v:cmdarg.
16890 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16891 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16892 * Must always be called in pairs!
16893 */
16894 char_u *
16895set_cmdarg(eap, oldarg)
16896 exarg_T *eap;
16897 char_u *oldarg;
16898{
16899 char_u *oldval;
16900 char_u *newval;
16901 unsigned len;
16902
Bram Moolenaare9a41262005-01-15 22:18:47 +000016903 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016904 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016906 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016907 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016908 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 }
16910
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016911 if (eap->force_bin == FORCE_BIN)
16912 len = 6;
16913 else if (eap->force_bin == FORCE_NOBIN)
16914 len = 8;
16915 else
16916 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016917
16918 if (eap->read_edit)
16919 len += 7;
16920
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016921 if (eap->force_ff != 0)
16922 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16923# ifdef FEAT_MBYTE
16924 if (eap->force_enc != 0)
16925 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016926 if (eap->bad_char != 0)
16927 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016928# endif
16929
16930 newval = alloc(len + 1);
16931 if (newval == NULL)
16932 return NULL;
16933
16934 if (eap->force_bin == FORCE_BIN)
16935 sprintf((char *)newval, " ++bin");
16936 else if (eap->force_bin == FORCE_NOBIN)
16937 sprintf((char *)newval, " ++nobin");
16938 else
16939 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016940
16941 if (eap->read_edit)
16942 STRCAT(newval, " ++edit");
16943
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016944 if (eap->force_ff != 0)
16945 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16946 eap->cmd + eap->force_ff);
16947# ifdef FEAT_MBYTE
16948 if (eap->force_enc != 0)
16949 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16950 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016951 if (eap->bad_char != 0)
16952 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16953 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016954# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016955 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016956 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957}
16958#endif
16959
16960/*
16961 * Get the value of internal variable "name".
16962 * Return OK or FAIL.
16963 */
16964 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016965get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 char_u *name;
16967 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016968 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016969 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970{
16971 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 typval_T *tv = NULL;
16973 typval_T atv;
16974 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
16977 /* truncate the name, so that we can use strcmp() */
16978 cc = name[len];
16979 name[len] = NUL;
16980
16981 /*
16982 * Check for "b:changedtick".
16983 */
16984 if (STRCMP(name, "b:changedtick") == 0)
16985 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016986 atv.v_type = VAR_NUMBER;
16987 atv.vval.v_number = curbuf->b_changedtick;
16988 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 }
16990
16991 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 * Check for user-defined variables.
16993 */
16994 else
16995 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016996 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 }
17000
Bram Moolenaare9a41262005-01-15 22:18:47 +000017001 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017003 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 ret = FAIL;
17006 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017007 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017008 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009
17010 name[len] = cc;
17011
17012 return ret;
17013}
17014
17015/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017016 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17017 * Also handle function call with Funcref variable: func(expr)
17018 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17019 */
17020 static int
17021handle_subscript(arg, rettv, evaluate, verbose)
17022 char_u **arg;
17023 typval_T *rettv;
17024 int evaluate; /* do more than finding the end */
17025 int verbose; /* give error messages */
17026{
17027 int ret = OK;
17028 dict_T *selfdict = NULL;
17029 char_u *s;
17030 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017031 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017032
17033 while (ret == OK
17034 && (**arg == '['
17035 || (**arg == '.' && rettv->v_type == VAR_DICT)
17036 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17037 && !vim_iswhite(*(*arg - 1)))
17038 {
17039 if (**arg == '(')
17040 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017041 /* need to copy the funcref so that we can clear rettv */
17042 functv = *rettv;
17043 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017044
17045 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017046 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017047 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017048 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17049 &len, evaluate, selfdict);
17050
17051 /* Clear the funcref afterwards, so that deleting it while
17052 * evaluating the arguments is possible (see test55). */
17053 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017054
17055 /* Stop the expression evaluation when immediately aborting on
17056 * error, or when an interrupt occurred or an exception was thrown
17057 * but not caught. */
17058 if (aborting())
17059 {
17060 if (ret == OK)
17061 clear_tv(rettv);
17062 ret = FAIL;
17063 }
17064 dict_unref(selfdict);
17065 selfdict = NULL;
17066 }
17067 else /* **arg == '[' || **arg == '.' */
17068 {
17069 dict_unref(selfdict);
17070 if (rettv->v_type == VAR_DICT)
17071 {
17072 selfdict = rettv->vval.v_dict;
17073 if (selfdict != NULL)
17074 ++selfdict->dv_refcount;
17075 }
17076 else
17077 selfdict = NULL;
17078 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17079 {
17080 clear_tv(rettv);
17081 ret = FAIL;
17082 }
17083 }
17084 }
17085 dict_unref(selfdict);
17086 return ret;
17087}
17088
17089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017090 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17091 * value).
17092 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017094alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017095{
Bram Moolenaar33570922005-01-25 22:26:29 +000017096 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017097}
17098
17099/*
17100 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 * The string "s" must have been allocated, it is consumed.
17102 * Return NULL for out of memory, the variable otherwise.
17103 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017104 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017105alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 char_u *s;
17107{
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017110 rettv = alloc_tv();
17111 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113 rettv->v_type = VAR_STRING;
17114 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 }
17116 else
17117 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119}
17120
17121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017122 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017124 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017126 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127{
17128 if (varp != NULL)
17129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017130 switch (varp->v_type)
17131 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017132 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017133 func_unref(varp->vval.v_string);
17134 /*FALLTHROUGH*/
17135 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017136 vim_free(varp->vval.v_string);
17137 break;
17138 case VAR_LIST:
17139 list_unref(varp->vval.v_list);
17140 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017141 case VAR_DICT:
17142 dict_unref(varp->vval.v_dict);
17143 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017144 case VAR_NUMBER:
17145 case VAR_UNKNOWN:
17146 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017147 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017148 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017149 break;
17150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 vim_free(varp);
17152 }
17153}
17154
17155/*
17156 * Free the memory for a variable value and set the value to NULL or 0.
17157 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017158 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017160 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161{
17162 if (varp != NULL)
17163 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017164 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017166 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017167 func_unref(varp->vval.v_string);
17168 /*FALLTHROUGH*/
17169 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017170 vim_free(varp->vval.v_string);
17171 varp->vval.v_string = NULL;
17172 break;
17173 case VAR_LIST:
17174 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017175 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017177 case VAR_DICT:
17178 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017179 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017180 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017181 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017182 varp->vval.v_number = 0;
17183 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 case VAR_UNKNOWN:
17185 break;
17186 default:
17187 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017189 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 }
17191}
17192
17193/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194 * Set the value of a variable to NULL without freeing items.
17195 */
17196 static void
17197init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199{
17200 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017201 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017202}
17203
17204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 * Get the number value of a variable.
17206 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017207 * For incompatible types, return 0.
17208 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17209 * caller of incompatible types: it sets *denote to TRUE if "denote"
17210 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 */
17212 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017213get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017214 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017216 int error = FALSE;
17217
17218 return get_tv_number_chk(varp, &error); /* return 0L on error */
17219}
17220
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017221 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017222get_tv_number_chk(varp, denote)
17223 typval_T *varp;
17224 int *denote;
17225{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017226 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017228 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017230 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017231 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017232 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017233 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017234 break;
17235 case VAR_STRING:
17236 if (varp->vval.v_string != NULL)
17237 vim_str2nr(varp->vval.v_string, NULL, NULL,
17238 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017239 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017240 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017241 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017242 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017243 case VAR_DICT:
17244 EMSG(_("E728: Using a Dictionary as a number"));
17245 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017246 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017247 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017248 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017250 if (denote == NULL) /* useful for values that must be unsigned */
17251 n = -1;
17252 else
17253 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017254 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255}
17256
17257/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017258 * Get the lnum from the first argument.
17259 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 */
17262 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265{
Bram Moolenaar33570922005-01-25 22:26:29 +000017266 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 linenr_T lnum;
17268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017269 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 if (lnum == 0) /* no valid number, try using line() */
17271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272 rettv.v_type = VAR_NUMBER;
17273 f_line(argvars, &rettv);
17274 lnum = rettv.vval.v_number;
17275 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 }
17277 return lnum;
17278}
17279
17280/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017281 * Get the lnum from the first argument.
17282 * Also accepts "$", then "buf" is used.
17283 * Returns 0 on error.
17284 */
17285 static linenr_T
17286get_tv_lnum_buf(argvars, buf)
17287 typval_T *argvars;
17288 buf_T *buf;
17289{
17290 if (argvars[0].v_type == VAR_STRING
17291 && argvars[0].vval.v_string != NULL
17292 && argvars[0].vval.v_string[0] == '$'
17293 && buf != NULL)
17294 return buf->b_ml.ml_line_count;
17295 return get_tv_number_chk(&argvars[0], NULL);
17296}
17297
17298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 * Get the string value of a variable.
17300 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017301 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17302 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 * If the String variable has never been set, return an empty string.
17304 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017305 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17306 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 */
17308 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017310 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311{
17312 static char_u mybuf[NUMBUFLEN];
17313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017314 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315}
17316
17317 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017322 char_u *res = get_tv_string_buf_chk(varp, buf);
17323
17324 return res != NULL ? res : (char_u *)"";
17325}
17326
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017327 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328get_tv_string_chk(varp)
17329 typval_T *varp;
17330{
17331 static char_u mybuf[NUMBUFLEN];
17332
17333 return get_tv_string_buf_chk(varp, mybuf);
17334}
17335
17336 static char_u *
17337get_tv_string_buf_chk(varp, buf)
17338 typval_T *varp;
17339 char_u *buf;
17340{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017341 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343 case VAR_NUMBER:
17344 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17345 return buf;
17346 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017347 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017348 break;
17349 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017350 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017351 break;
17352 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017353 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017354 break;
17355 case VAR_STRING:
17356 if (varp->vval.v_string != NULL)
17357 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017358 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017359 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364}
17365
17366/*
17367 * Find variable "name" in the list of variables.
17368 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017369 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017370 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017374find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
Bram Moolenaara7043832005-01-21 11:56:39 +000017381 ht = find_var_ht(name, &varname);
17382 if (htp != NULL)
17383 *htp = ht;
17384 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017386 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387}
17388
17389/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017391 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017393 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017394find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017395 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017396 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017397 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017398{
Bram Moolenaar33570922005-01-25 22:26:29 +000017399 hashitem_T *hi;
17400
17401 if (*varname == NUL)
17402 {
17403 /* Must be something like "s:", otherwise "ht" would be NULL. */
17404 switch (varname[-2])
17405 {
17406 case 's': return &SCRIPT_SV(current_SID).sv_var;
17407 case 'g': return &globvars_var;
17408 case 'v': return &vimvars_var;
17409 case 'b': return &curbuf->b_bufvar;
17410 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017411#ifdef FEAT_WINDOWS
17412 case 't': return &curtab->tp_winvar;
17413#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017414 case 'l': return current_funccal == NULL
17415 ? NULL : &current_funccal->l_vars_var;
17416 case 'a': return current_funccal == NULL
17417 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017418 }
17419 return NULL;
17420 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017421
17422 hi = hash_find(ht, varname);
17423 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017424 {
17425 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017426 * worked find the variable again. Don't auto-load a script if it was
17427 * loaded already, otherwise it would be loaded every time when
17428 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017429 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017430 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017431 hi = hash_find(ht, varname);
17432 if (HASHITEM_EMPTY(hi))
17433 return NULL;
17434 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017436}
17437
17438/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017440 * Set "varname" to the start of name without ':'.
17441 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017443find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 char_u *name;
17445 char_u **varname;
17446{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017447 hashitem_T *hi;
17448
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449 if (name[1] != ':')
17450 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017451 /* The name must not start with a colon or #. */
17452 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 return NULL;
17454 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017455
17456 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017457 hi = hash_find(&compat_hashtab, name);
17458 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017459 return &compat_hashtab;
17460
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 return &globvarht; /* global variable */
17463 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 }
17465 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017466 if (*name == 'g') /* global variable */
17467 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017468 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17469 */
17470 if (vim_strchr(name + 2, ':') != NULL
17471 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017472 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017476 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017477#ifdef FEAT_WINDOWS
17478 if (*name == 't') /* tab page variable */
17479 return &curtab->tp_vars.dv_hashtab;
17480#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 if (*name == 'v') /* v: variable */
17482 return &vimvarht;
17483 if (*name == 'a' && current_funccal != NULL) /* function argument */
17484 return &current_funccal->l_avars.dv_hashtab;
17485 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17486 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 if (*name == 's' /* script variable */
17488 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17489 return &SCRIPT_VARS(current_SID);
17490 return NULL;
17491}
17492
17493/*
17494 * Get the string value of a (global/local) variable.
17495 * Returns NULL when it doesn't exist.
17496 */
17497 char_u *
17498get_var_value(name)
17499 char_u *name;
17500{
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502
Bram Moolenaara7043832005-01-21 11:56:39 +000017503 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 if (v == NULL)
17505 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507}
17508
17509/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 * sourcing this script and when executing functions defined in the script.
17512 */
17513 void
17514new_script_vars(id)
17515 scid_T id;
17516{
Bram Moolenaara7043832005-01-21 11:56:39 +000017517 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017518 hashtab_T *ht;
17519 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017520
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17522 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017523 /* Re-allocating ga_data means that an ht_array pointing to
17524 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017526 for (i = 1; i <= ga_scripts.ga_len; ++i)
17527 {
17528 ht = &SCRIPT_VARS(i);
17529 if (ht->ht_mask == HT_INIT_SIZE - 1)
17530 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 sv = &SCRIPT_SV(i);
17532 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017533 }
17534
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 while (ga_scripts.ga_len < id)
17536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17538 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 }
17541 }
17542}
17543
17544/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017545 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17546 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 */
17548 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017549init_var_dict(dict, dict_var)
17550 dict_T *dict;
17551 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552{
Bram Moolenaar33570922005-01-25 22:26:29 +000017553 hash_init(&dict->dv_hashtab);
17554 dict->dv_refcount = 99999;
17555 dict_var->di_tv.vval.v_dict = dict;
17556 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017557 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017558 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17559 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560}
17561
17562/*
17563 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 * Frees all allocated variables and the value they contain.
17565 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 */
17567 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017568vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 hashtab_T *ht;
17570{
17571 vars_clear_ext(ht, TRUE);
17572}
17573
17574/*
17575 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17576 */
17577 static void
17578vars_clear_ext(ht, free_val)
17579 hashtab_T *ht;
17580 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581{
Bram Moolenaara7043832005-01-21 11:56:39 +000017582 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 hashitem_T *hi;
17584 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017587 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017588 for (hi = ht->ht_array; todo > 0; ++hi)
17589 {
17590 if (!HASHITEM_EMPTY(hi))
17591 {
17592 --todo;
17593
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017595 * ht_array might change then. hash_clear() takes care of it
17596 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 v = HI2DI(hi);
17598 if (free_val)
17599 clear_tv(&v->di_tv);
17600 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17601 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017602 }
17603 }
17604 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017605 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606}
17607
Bram Moolenaara7043832005-01-21 11:56:39 +000017608/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017609 * Delete a variable from hashtab "ht" at item "hi".
17610 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017613delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 hashtab_T *ht;
17615 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616{
Bram Moolenaar33570922005-01-25 22:26:29 +000017617 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017618
17619 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017620 clear_tv(&di->di_tv);
17621 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622}
17623
17624/*
17625 * List the value of one internal variable.
17626 */
17627 static void
17628list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017629 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 char_u *prefix;
17631{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017632 char_u *tofree;
17633 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017634 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017635
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017636 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017637 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017638 s == NULL ? (char_u *)"" : s);
17639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640}
17641
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 static void
17643list_one_var_a(prefix, name, type, string)
17644 char_u *prefix;
17645 char_u *name;
17646 int type;
17647 char_u *string;
17648{
17649 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17650 if (name != NULL) /* "a:" vars don't have a name stored */
17651 msg_puts(name);
17652 msg_putchar(' ');
17653 msg_advance(22);
17654 if (type == VAR_NUMBER)
17655 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656 else if (type == VAR_FUNC)
17657 msg_putchar('*');
17658 else if (type == VAR_LIST)
17659 {
17660 msg_putchar('[');
17661 if (*string == '[')
17662 ++string;
17663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017664 else if (type == VAR_DICT)
17665 {
17666 msg_putchar('{');
17667 if (*string == '{')
17668 ++string;
17669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 else
17671 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017672
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017674
17675 if (type == VAR_FUNC)
17676 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677}
17678
17679/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 * If the variable already exists, the value is updated.
17682 * Otherwise the variable is created.
17683 */
17684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689{
Bram Moolenaar33570922005-01-25 22:26:29 +000017690 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017692 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017693 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017696 {
17697 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17698 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17699 ? name[2] : name[0]))
17700 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017701 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017702 return;
17703 }
17704 if (function_exists(name))
17705 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017706 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017707 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017708 return;
17709 }
17710 }
17711
Bram Moolenaara7043832005-01-21 11:56:39 +000017712 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017714 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017715 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017716 return;
17717 }
17718
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017719 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017720 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017723 if (var_check_ro(v->di_flags, name)
17724 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017725 return;
17726 if (v->di_tv.v_type != tv->v_type
17727 && !((v->di_tv.v_type == VAR_STRING
17728 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729 && (tv->v_type == VAR_STRING
17730 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017731 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017732 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017733 return;
17734 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017735
17736 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017737 * Handle setting internal v: variables separately: we don't change
17738 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 */
17740 if (ht == &vimvarht)
17741 {
17742 if (v->di_tv.v_type == VAR_STRING)
17743 {
17744 vim_free(v->di_tv.vval.v_string);
17745 if (copy || tv->v_type != VAR_STRING)
17746 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17747 else
17748 {
17749 /* Take over the string to avoid an extra alloc/free. */
17750 v->di_tv.vval.v_string = tv->vval.v_string;
17751 tv->vval.v_string = NULL;
17752 }
17753 }
17754 else if (v->di_tv.v_type != VAR_NUMBER)
17755 EMSG2(_(e_intern2), "set_var()");
17756 else
17757 v->di_tv.vval.v_number = get_tv_number(tv);
17758 return;
17759 }
17760
17761 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 }
17763 else /* add a new variable */
17764 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017765 /* Can't add "v:" variable. */
17766 if (ht == &vimvarht)
17767 {
17768 EMSG2(_(e_illvar), name);
17769 return;
17770 }
17771
Bram Moolenaar92124a32005-06-17 22:03:40 +000017772 /* Make sure the variable name is valid. */
17773 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017774 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17775 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017776 {
17777 EMSG2(_(e_illvar), varname);
17778 return;
17779 }
17780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017781 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17782 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017783 if (v == NULL)
17784 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017785 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017788 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 return;
17790 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017791 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017796 else
17797 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017799 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017800 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802}
17803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017804/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017805 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17806 * Also give an error message.
17807 */
17808 static int
17809var_check_ro(flags, name)
17810 int flags;
17811 char_u *name;
17812{
17813 if (flags & DI_FLAGS_RO)
17814 {
17815 EMSG2(_(e_readonlyvar), name);
17816 return TRUE;
17817 }
17818 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17819 {
17820 EMSG2(_(e_readonlysbx), name);
17821 return TRUE;
17822 }
17823 return FALSE;
17824}
17825
17826/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017827 * Return TRUE if typeval "tv" is set to be locked (immutable).
17828 * Also give an error message, using "name".
17829 */
17830 static int
17831tv_check_lock(lock, name)
17832 int lock;
17833 char_u *name;
17834{
17835 if (lock & VAR_LOCKED)
17836 {
17837 EMSG2(_("E741: Value is locked: %s"),
17838 name == NULL ? (char_u *)_("Unknown") : name);
17839 return TRUE;
17840 }
17841 if (lock & VAR_FIXED)
17842 {
17843 EMSG2(_("E742: Cannot change value of %s"),
17844 name == NULL ? (char_u *)_("Unknown") : name);
17845 return TRUE;
17846 }
17847 return FALSE;
17848}
17849
17850/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017852 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017853 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017856copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017857 typval_T *from;
17858 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017860 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017861 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017862 switch (from->v_type)
17863 {
17864 case VAR_NUMBER:
17865 to->vval.v_number = from->vval.v_number;
17866 break;
17867 case VAR_STRING:
17868 case VAR_FUNC:
17869 if (from->vval.v_string == NULL)
17870 to->vval.v_string = NULL;
17871 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017873 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017874 if (from->v_type == VAR_FUNC)
17875 func_ref(to->vval.v_string);
17876 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017877 break;
17878 case VAR_LIST:
17879 if (from->vval.v_list == NULL)
17880 to->vval.v_list = NULL;
17881 else
17882 {
17883 to->vval.v_list = from->vval.v_list;
17884 ++to->vval.v_list->lv_refcount;
17885 }
17886 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017887 case VAR_DICT:
17888 if (from->vval.v_dict == NULL)
17889 to->vval.v_dict = NULL;
17890 else
17891 {
17892 to->vval.v_dict = from->vval.v_dict;
17893 ++to->vval.v_dict->dv_refcount;
17894 }
17895 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017896 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017897 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017898 break;
17899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900}
17901
17902/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017903 * Make a copy of an item.
17904 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017905 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17906 * reference to an already copied list/dict can be used.
17907 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017908 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017909 static int
17910item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 typval_T *from;
17912 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017913 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017914 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017915{
17916 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017917 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017918
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017920 {
17921 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017922 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017923 }
17924 ++recurse;
17925
17926 switch (from->v_type)
17927 {
17928 case VAR_NUMBER:
17929 case VAR_STRING:
17930 case VAR_FUNC:
17931 copy_tv(from, to);
17932 break;
17933 case VAR_LIST:
17934 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017935 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017936 if (from->vval.v_list == NULL)
17937 to->vval.v_list = NULL;
17938 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17939 {
17940 /* use the copy made earlier */
17941 to->vval.v_list = from->vval.v_list->lv_copylist;
17942 ++to->vval.v_list->lv_refcount;
17943 }
17944 else
17945 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17946 if (to->vval.v_list == NULL)
17947 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017948 break;
17949 case VAR_DICT:
17950 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017951 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017952 if (from->vval.v_dict == NULL)
17953 to->vval.v_dict = NULL;
17954 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17955 {
17956 /* use the copy made earlier */
17957 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17958 ++to->vval.v_dict->dv_refcount;
17959 }
17960 else
17961 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17962 if (to->vval.v_dict == NULL)
17963 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017964 break;
17965 default:
17966 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017967 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017968 }
17969 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017970 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017971}
17972
17973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 * ":echo expr1 ..." print each argument separated with a space, add a
17975 * newline at the end.
17976 * ":echon expr1 ..." print each argument plain.
17977 */
17978 void
17979ex_echo(eap)
17980 exarg_T *eap;
17981{
17982 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017983 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017984 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 char_u *p;
17986 int needclr = TRUE;
17987 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017988 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989
17990 if (eap->skip)
17991 ++emsg_skip;
17992 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17993 {
17994 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017995 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 {
17997 /*
17998 * Report the invalid expression unless the expression evaluation
17999 * has been cancelled due to an aborting error, an interrupt, or an
18000 * exception.
18001 */
18002 if (!aborting())
18003 EMSG2(_(e_invexpr2), p);
18004 break;
18005 }
18006 if (!eap->skip)
18007 {
18008 if (atstart)
18009 {
18010 atstart = FALSE;
18011 /* Call msg_start() after eval1(), evaluating the expression
18012 * may cause a message to appear. */
18013 if (eap->cmdidx == CMD_echo)
18014 msg_start();
18015 }
18016 else if (eap->cmdidx == CMD_echo)
18017 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018018 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018019 if (p != NULL)
18020 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018022 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018024 if (*p != TAB && needclr)
18025 {
18026 /* remove any text still there from the command */
18027 msg_clr_eos();
18028 needclr = FALSE;
18029 }
18030 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 }
18032 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018033 {
18034#ifdef FEAT_MBYTE
18035 if (has_mbyte)
18036 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018037 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018038
18039 (void)msg_outtrans_len_attr(p, i, echo_attr);
18040 p += i - 1;
18041 }
18042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018044 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018047 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 arg = skipwhite(arg);
18051 }
18052 eap->nextcmd = check_nextcmd(arg);
18053
18054 if (eap->skip)
18055 --emsg_skip;
18056 else
18057 {
18058 /* remove text that may still be there from the command */
18059 if (needclr)
18060 msg_clr_eos();
18061 if (eap->cmdidx == CMD_echo)
18062 msg_end();
18063 }
18064}
18065
18066/*
18067 * ":echohl {name}".
18068 */
18069 void
18070ex_echohl(eap)
18071 exarg_T *eap;
18072{
18073 int id;
18074
18075 id = syn_name2id(eap->arg);
18076 if (id == 0)
18077 echo_attr = 0;
18078 else
18079 echo_attr = syn_id2attr(id);
18080}
18081
18082/*
18083 * ":execute expr1 ..." execute the result of an expression.
18084 * ":echomsg expr1 ..." Print a message
18085 * ":echoerr expr1 ..." Print an error
18086 * Each gets spaces around each argument and a newline at the end for
18087 * echo commands
18088 */
18089 void
18090ex_execute(eap)
18091 exarg_T *eap;
18092{
18093 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 int ret = OK;
18096 char_u *p;
18097 garray_T ga;
18098 int len;
18099 int save_did_emsg;
18100
18101 ga_init2(&ga, 1, 80);
18102
18103 if (eap->skip)
18104 ++emsg_skip;
18105 while (*arg != NUL && *arg != '|' && *arg != '\n')
18106 {
18107 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 {
18110 /*
18111 * Report the invalid expression unless the expression evaluation
18112 * has been cancelled due to an aborting error, an interrupt, or an
18113 * exception.
18114 */
18115 if (!aborting())
18116 EMSG2(_(e_invexpr2), p);
18117 ret = FAIL;
18118 break;
18119 }
18120
18121 if (!eap->skip)
18122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 len = (int)STRLEN(p);
18125 if (ga_grow(&ga, len + 2) == FAIL)
18126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018127 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 ret = FAIL;
18129 break;
18130 }
18131 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 ga.ga_len += len;
18135 }
18136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 arg = skipwhite(arg);
18139 }
18140
18141 if (ret != FAIL && ga.ga_data != NULL)
18142 {
18143 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018146 out_flush();
18147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 else if (eap->cmdidx == CMD_echoerr)
18149 {
18150 /* We don't want to abort following commands, restore did_emsg. */
18151 save_did_emsg = did_emsg;
18152 EMSG((char_u *)ga.ga_data);
18153 if (!force_abort)
18154 did_emsg = save_did_emsg;
18155 }
18156 else if (eap->cmdidx == CMD_execute)
18157 do_cmdline((char_u *)ga.ga_data,
18158 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18159 }
18160
18161 ga_clear(&ga);
18162
18163 if (eap->skip)
18164 --emsg_skip;
18165
18166 eap->nextcmd = check_nextcmd(arg);
18167}
18168
18169/*
18170 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18171 * "arg" points to the "&" or '+' when called, to "option" when returning.
18172 * Returns NULL when no option name found. Otherwise pointer to the char
18173 * after the option name.
18174 */
18175 static char_u *
18176find_option_end(arg, opt_flags)
18177 char_u **arg;
18178 int *opt_flags;
18179{
18180 char_u *p = *arg;
18181
18182 ++p;
18183 if (*p == 'g' && p[1] == ':')
18184 {
18185 *opt_flags = OPT_GLOBAL;
18186 p += 2;
18187 }
18188 else if (*p == 'l' && p[1] == ':')
18189 {
18190 *opt_flags = OPT_LOCAL;
18191 p += 2;
18192 }
18193 else
18194 *opt_flags = 0;
18195
18196 if (!ASCII_ISALPHA(*p))
18197 return NULL;
18198 *arg = p;
18199
18200 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18201 p += 4; /* termcap option */
18202 else
18203 while (ASCII_ISALPHA(*p))
18204 ++p;
18205 return p;
18206}
18207
18208/*
18209 * ":function"
18210 */
18211 void
18212ex_function(eap)
18213 exarg_T *eap;
18214{
18215 char_u *theline;
18216 int j;
18217 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 char_u *name = NULL;
18220 char_u *p;
18221 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018222 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 garray_T newargs;
18224 garray_T newlines;
18225 int varargs = FALSE;
18226 int mustend = FALSE;
18227 int flags = 0;
18228 ufunc_T *fp;
18229 int indent;
18230 int nesting;
18231 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018232 dictitem_T *v;
18233 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018234 static int func_nr = 0; /* number for nameless function */
18235 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018236 hashtab_T *ht;
18237 int todo;
18238 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018239 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240
18241 /*
18242 * ":function" without argument: list functions.
18243 */
18244 if (ends_excmd(*eap->arg))
18245 {
18246 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018247 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018248 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018249 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018250 {
18251 if (!HASHITEM_EMPTY(hi))
18252 {
18253 --todo;
18254 fp = HI2UF(hi);
18255 if (!isdigit(*fp->uf_name))
18256 list_func_head(fp, FALSE);
18257 }
18258 }
18259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 eap->nextcmd = check_nextcmd(eap->arg);
18261 return;
18262 }
18263
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018264 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018265 * ":function /pat": list functions matching pattern.
18266 */
18267 if (*eap->arg == '/')
18268 {
18269 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18270 if (!eap->skip)
18271 {
18272 regmatch_T regmatch;
18273
18274 c = *p;
18275 *p = NUL;
18276 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18277 *p = c;
18278 if (regmatch.regprog != NULL)
18279 {
18280 regmatch.rm_ic = p_ic;
18281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018282 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018283 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18284 {
18285 if (!HASHITEM_EMPTY(hi))
18286 {
18287 --todo;
18288 fp = HI2UF(hi);
18289 if (!isdigit(*fp->uf_name)
18290 && vim_regexec(&regmatch, fp->uf_name, 0))
18291 list_func_head(fp, FALSE);
18292 }
18293 }
18294 }
18295 }
18296 if (*p == '/')
18297 ++p;
18298 eap->nextcmd = check_nextcmd(p);
18299 return;
18300 }
18301
18302 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018303 * Get the function name. There are these situations:
18304 * func normal function name
18305 * "name" == func, "fudi.fd_dict" == NULL
18306 * dict.func new dictionary entry
18307 * "name" == NULL, "fudi.fd_dict" set,
18308 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18309 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018310 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018311 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18312 * dict.func existing dict entry that's not a Funcref
18313 * "name" == NULL, "fudi.fd_dict" set,
18314 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018317 name = trans_function_name(&p, eap->skip, 0, &fudi);
18318 paren = (vim_strchr(p, '(') != NULL);
18319 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 {
18321 /*
18322 * Return on an invalid expression in braces, unless the expression
18323 * evaluation has been cancelled due to an aborting error, an
18324 * interrupt, or an exception.
18325 */
18326 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018328 if (!eap->skip && fudi.fd_newkey != NULL)
18329 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018330 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 else
18334 eap->skip = TRUE;
18335 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018336
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337 /* An error in a function call during evaluation of an expression in magic
18338 * braces should not cause the function not to be defined. */
18339 saved_did_emsg = did_emsg;
18340 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341
18342 /*
18343 * ":function func" with only function name: list function.
18344 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018345 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 {
18347 if (!ends_excmd(*skipwhite(p)))
18348 {
18349 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018350 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 }
18352 eap->nextcmd = check_nextcmd(p);
18353 if (eap->nextcmd != NULL)
18354 *p = NUL;
18355 if (!eap->skip && !got_int)
18356 {
18357 fp = find_func(name);
18358 if (fp != NULL)
18359 {
18360 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018361 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018363 if (FUNCLINE(fp, j) == NULL)
18364 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 msg_putchar('\n');
18366 msg_outnum((long)(j + 1));
18367 if (j < 9)
18368 msg_putchar(' ');
18369 if (j < 99)
18370 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018371 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 out_flush(); /* show a line at a time */
18373 ui_breakcheck();
18374 }
18375 if (!got_int)
18376 {
18377 msg_putchar('\n');
18378 msg_puts((char_u *)" endfunction");
18379 }
18380 }
18381 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018382 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018384 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 }
18386
18387 /*
18388 * ":function name(arg1, arg2)" Define function.
18389 */
18390 p = skipwhite(p);
18391 if (*p != '(')
18392 {
18393 if (!eap->skip)
18394 {
18395 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018396 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 }
18398 /* attempt to continue by skipping some text */
18399 if (vim_strchr(p, '(') != NULL)
18400 p = vim_strchr(p, '(');
18401 }
18402 p = skipwhite(p + 1);
18403
18404 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18405 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18406
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018407 if (!eap->skip)
18408 {
18409 /* Check the name of the function. */
18410 if (name != NULL)
18411 arg = name;
18412 else
18413 arg = fudi.fd_newkey;
18414 if (arg != NULL)
18415 {
18416 if (*arg == K_SPECIAL)
18417 j = 3;
18418 else
18419 j = 0;
18420 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18421 : eval_isnamec(arg[j])))
18422 ++j;
18423 if (arg[j] != NUL)
18424 emsg_funcname(_(e_invarg2), arg);
18425 }
18426 }
18427
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 /*
18429 * Isolate the arguments: "arg1, arg2, ...)"
18430 */
18431 while (*p != ')')
18432 {
18433 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18434 {
18435 varargs = TRUE;
18436 p += 3;
18437 mustend = TRUE;
18438 }
18439 else
18440 {
18441 arg = p;
18442 while (ASCII_ISALNUM(*p) || *p == '_')
18443 ++p;
18444 if (arg == p || isdigit(*arg)
18445 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18446 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18447 {
18448 if (!eap->skip)
18449 EMSG2(_("E125: Illegal argument: %s"), arg);
18450 break;
18451 }
18452 if (ga_grow(&newargs, 1) == FAIL)
18453 goto erret;
18454 c = *p;
18455 *p = NUL;
18456 arg = vim_strsave(arg);
18457 if (arg == NULL)
18458 goto erret;
18459 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18460 *p = c;
18461 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 if (*p == ',')
18463 ++p;
18464 else
18465 mustend = TRUE;
18466 }
18467 p = skipwhite(p);
18468 if (mustend && *p != ')')
18469 {
18470 if (!eap->skip)
18471 EMSG2(_(e_invarg2), eap->arg);
18472 break;
18473 }
18474 }
18475 ++p; /* skip the ')' */
18476
Bram Moolenaare9a41262005-01-15 22:18:47 +000018477 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 for (;;)
18479 {
18480 p = skipwhite(p);
18481 if (STRNCMP(p, "range", 5) == 0)
18482 {
18483 flags |= FC_RANGE;
18484 p += 5;
18485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018486 else if (STRNCMP(p, "dict", 4) == 0)
18487 {
18488 flags |= FC_DICT;
18489 p += 4;
18490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 else if (STRNCMP(p, "abort", 5) == 0)
18492 {
18493 flags |= FC_ABORT;
18494 p += 5;
18495 }
18496 else
18497 break;
18498 }
18499
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018500 /* When there is a line break use what follows for the function body.
18501 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18502 if (*p == '\n')
18503 line_arg = p + 1;
18504 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 EMSG(_(e_trailing));
18506
18507 /*
18508 * Read the body of the function, until ":endfunction" is found.
18509 */
18510 if (KeyTyped)
18511 {
18512 /* Check if the function already exists, don't let the user type the
18513 * whole function before telling him it doesn't work! For a script we
18514 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018515 if (!eap->skip && !eap->forceit)
18516 {
18517 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18518 EMSG(_(e_funcdict));
18519 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018520 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018523 if (!eap->skip && did_emsg)
18524 goto erret;
18525
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 msg_putchar('\n'); /* don't overwrite the function name */
18527 cmdline_row = msg_row;
18528 }
18529
18530 indent = 2;
18531 nesting = 0;
18532 for (;;)
18533 {
18534 msg_scroll = TRUE;
18535 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018536 sourcing_lnum_off = sourcing_lnum;
18537
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018538 if (line_arg != NULL)
18539 {
18540 /* Use eap->arg, split up in parts by line breaks. */
18541 theline = line_arg;
18542 p = vim_strchr(theline, '\n');
18543 if (p == NULL)
18544 line_arg += STRLEN(line_arg);
18545 else
18546 {
18547 *p = NUL;
18548 line_arg = p + 1;
18549 }
18550 }
18551 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 theline = getcmdline(':', 0L, indent);
18553 else
18554 theline = eap->getline(':', eap->cookie, indent);
18555 if (KeyTyped)
18556 lines_left = Rows - 1;
18557 if (theline == NULL)
18558 {
18559 EMSG(_("E126: Missing :endfunction"));
18560 goto erret;
18561 }
18562
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018563 /* Detect line continuation: sourcing_lnum increased more than one. */
18564 if (sourcing_lnum > sourcing_lnum_off + 1)
18565 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18566 else
18567 sourcing_lnum_off = 0;
18568
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 if (skip_until != NULL)
18570 {
18571 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18572 * don't check for ":endfunc". */
18573 if (STRCMP(theline, skip_until) == 0)
18574 {
18575 vim_free(skip_until);
18576 skip_until = NULL;
18577 }
18578 }
18579 else
18580 {
18581 /* skip ':' and blanks*/
18582 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18583 ;
18584
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018585 /* Check for "endfunction". */
18586 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018588 if (line_arg == NULL)
18589 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 break;
18591 }
18592
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018593 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 * at "end". */
18595 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18596 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018597 else if (STRNCMP(p, "if", 2) == 0
18598 || STRNCMP(p, "wh", 2) == 0
18599 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 || STRNCMP(p, "try", 3) == 0)
18601 indent += 2;
18602
18603 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018604 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018606 if (*p == '!')
18607 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 p += eval_fname_script(p);
18609 if (ASCII_ISALPHA(*p))
18610 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018611 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 if (*skipwhite(p) == '(')
18613 {
18614 ++nesting;
18615 indent += 2;
18616 }
18617 }
18618 }
18619
18620 /* Check for ":append" or ":insert". */
18621 p = skip_range(p, NULL);
18622 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18623 || (p[0] == 'i'
18624 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18625 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18626 skip_until = vim_strsave((char_u *)".");
18627
18628 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18629 arg = skipwhite(skiptowhite(p));
18630 if (arg[0] == '<' && arg[1] =='<'
18631 && ((p[0] == 'p' && p[1] == 'y'
18632 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18633 || (p[0] == 'p' && p[1] == 'e'
18634 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18635 || (p[0] == 't' && p[1] == 'c'
18636 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18637 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18638 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018639 || (p[0] == 'm' && p[1] == 'z'
18640 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 ))
18642 {
18643 /* ":python <<" continues until a dot, like ":append" */
18644 p = skipwhite(arg + 2);
18645 if (*p == NUL)
18646 skip_until = vim_strsave((char_u *)".");
18647 else
18648 skip_until = vim_strsave(p);
18649 }
18650 }
18651
18652 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018653 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018654 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018655 if (line_arg == NULL)
18656 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018658 }
18659
18660 /* Copy the line to newly allocated memory. get_one_sourceline()
18661 * allocates 250 bytes per line, this saves 80% on average. The cost
18662 * is an extra alloc/free. */
18663 p = vim_strsave(theline);
18664 if (p != NULL)
18665 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018666 if (line_arg == NULL)
18667 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018668 theline = p;
18669 }
18670
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018671 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18672
18673 /* Add NULL lines for continuation lines, so that the line count is
18674 * equal to the index in the growarray. */
18675 while (sourcing_lnum_off-- > 0)
18676 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018677
18678 /* Check for end of eap->arg. */
18679 if (line_arg != NULL && *line_arg == NUL)
18680 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 }
18682
18683 /* Don't define the function when skipping commands or when an error was
18684 * detected. */
18685 if (eap->skip || did_emsg)
18686 goto erret;
18687
18688 /*
18689 * If there are no errors, add the function
18690 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018691 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018692 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018693 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018695 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018696 emsg_funcname("E707: Function name conflicts with variable: %s",
18697 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018698 goto erret;
18699 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018700
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018701 fp = find_func(name);
18702 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018704 if (!eap->forceit)
18705 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018706 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018707 goto erret;
18708 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018709 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018710 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018711 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018712 name);
18713 goto erret;
18714 }
18715 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018716 ga_clear_strings(&(fp->uf_args));
18717 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018718 vim_free(name);
18719 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 }
18722 else
18723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018724 char numbuf[20];
18725
18726 fp = NULL;
18727 if (fudi.fd_newkey == NULL && !eap->forceit)
18728 {
18729 EMSG(_(e_funcdict));
18730 goto erret;
18731 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018732 if (fudi.fd_di == NULL)
18733 {
18734 /* Can't add a function to a locked dictionary */
18735 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18736 goto erret;
18737 }
18738 /* Can't change an existing function if it is locked */
18739 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18740 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018741
18742 /* Give the function a sequential number. Can only be used with a
18743 * Funcref! */
18744 vim_free(name);
18745 sprintf(numbuf, "%d", ++func_nr);
18746 name = vim_strsave((char_u *)numbuf);
18747 if (name == NULL)
18748 goto erret;
18749 }
18750
18751 if (fp == NULL)
18752 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018753 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018754 {
18755 int slen, plen;
18756 char_u *scriptname;
18757
18758 /* Check that the autoload name matches the script name. */
18759 j = FAIL;
18760 if (sourcing_name != NULL)
18761 {
18762 scriptname = autoload_name(name);
18763 if (scriptname != NULL)
18764 {
18765 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018766 plen = (int)STRLEN(p);
18767 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018768 if (slen > plen && fnamecmp(p,
18769 sourcing_name + slen - plen) == 0)
18770 j = OK;
18771 vim_free(scriptname);
18772 }
18773 }
18774 if (j == FAIL)
18775 {
18776 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18777 goto erret;
18778 }
18779 }
18780
18781 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 if (fp == NULL)
18783 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018784
18785 if (fudi.fd_dict != NULL)
18786 {
18787 if (fudi.fd_di == NULL)
18788 {
18789 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018790 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018791 if (fudi.fd_di == NULL)
18792 {
18793 vim_free(fp);
18794 goto erret;
18795 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018796 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18797 {
18798 vim_free(fudi.fd_di);
18799 goto erret;
18800 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018801 }
18802 else
18803 /* overwrite existing dict entry */
18804 clear_tv(&fudi.fd_di->di_tv);
18805 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018806 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018807 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018808 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018809
18810 /* behave like "dict" was used */
18811 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018812 }
18813
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018815 STRCPY(fp->uf_name, name);
18816 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018818 fp->uf_args = newargs;
18819 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018820#ifdef FEAT_PROFILE
18821 fp->uf_tml_count = NULL;
18822 fp->uf_tml_total = NULL;
18823 fp->uf_tml_self = NULL;
18824 fp->uf_profiling = FALSE;
18825 if (prof_def_func())
18826 func_do_profile(fp);
18827#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018828 fp->uf_varargs = varargs;
18829 fp->uf_flags = flags;
18830 fp->uf_calls = 0;
18831 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018832 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833
18834erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 ga_clear_strings(&newargs);
18836 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018837ret_free:
18838 vim_free(skip_until);
18839 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842}
18843
18844/*
18845 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018846 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018848 * flags:
18849 * TFN_INT: internal function name OK
18850 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 * Advances "pp" to just after the function name (if no error).
18852 */
18853 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018854trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 char_u **pp;
18856 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018857 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018858 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018860 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 char_u *start;
18862 char_u *end;
18863 int lead;
18864 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018867
18868 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018871
18872 /* Check for hard coded <SNR>: already translated function ID (from a user
18873 * command). */
18874 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18875 && (*pp)[2] == (int)KE_SNR)
18876 {
18877 *pp += 3;
18878 len = get_id_len(pp) + 3;
18879 return vim_strnsave(start, len);
18880 }
18881
18882 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18883 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018885 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018887
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018888 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18889 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018890 if (end == start)
18891 {
18892 if (!skip)
18893 EMSG(_("E129: Function name required"));
18894 goto theend;
18895 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018896 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018897 {
18898 /*
18899 * Report an invalid expression in braces, unless the expression
18900 * evaluation has been cancelled due to an aborting error, an
18901 * interrupt, or an exception.
18902 */
18903 if (!aborting())
18904 {
18905 if (end != NULL)
18906 EMSG2(_(e_invarg2), start);
18907 }
18908 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018909 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018910 goto theend;
18911 }
18912
18913 if (lv.ll_tv != NULL)
18914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018915 if (fdp != NULL)
18916 {
18917 fdp->fd_dict = lv.ll_dict;
18918 fdp->fd_newkey = lv.ll_newkey;
18919 lv.ll_newkey = NULL;
18920 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018921 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018922 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18923 {
18924 name = vim_strsave(lv.ll_tv->vval.v_string);
18925 *pp = end;
18926 }
18927 else
18928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018929 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18930 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018931 EMSG(_(e_funcref));
18932 else
18933 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018934 name = NULL;
18935 }
18936 goto theend;
18937 }
18938
18939 if (lv.ll_name == NULL)
18940 {
18941 /* Error found, but continue after the function name. */
18942 *pp = end;
18943 goto theend;
18944 }
18945
18946 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018948 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018949 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18950 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18951 {
18952 /* When there was "s:" already or the name expanded to get a
18953 * leading "s:" then remove it. */
18954 lv.ll_name += 2;
18955 len -= 2;
18956 lead = 2;
18957 }
18958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018959 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018960 {
18961 if (lead == 2) /* skip over "s:" */
18962 lv.ll_name += 2;
18963 len = (int)(end - lv.ll_name);
18964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018965
18966 /*
18967 * Copy the function name to allocated memory.
18968 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18969 * Accept <SNR>123_name() outside a script.
18970 */
18971 if (skip)
18972 lead = 0; /* do nothing */
18973 else if (lead > 0)
18974 {
18975 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000018976 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
18977 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018978 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018979 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018980 if (current_SID <= 0)
18981 {
18982 EMSG(_(e_usingsid));
18983 goto theend;
18984 }
18985 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18986 lead += (int)STRLEN(sid_buf);
18987 }
18988 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018989 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018990 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018991 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018992 goto theend;
18993 }
18994 name = alloc((unsigned)(len + lead + 1));
18995 if (name != NULL)
18996 {
18997 if (lead > 0)
18998 {
18999 name[0] = K_SPECIAL;
19000 name[1] = KS_EXTRA;
19001 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019002 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019003 STRCPY(name + 3, sid_buf);
19004 }
19005 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19006 name[len + lead] = NUL;
19007 }
19008 *pp = end;
19009
19010theend:
19011 clear_lval(&lv);
19012 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013}
19014
19015/*
19016 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19017 * Return 2 if "p" starts with "s:".
19018 * Return 0 otherwise.
19019 */
19020 static int
19021eval_fname_script(p)
19022 char_u *p;
19023{
19024 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19025 || STRNICMP(p + 1, "SNR>", 4) == 0))
19026 return 5;
19027 if (p[0] == 's' && p[1] == ':')
19028 return 2;
19029 return 0;
19030}
19031
19032/*
19033 * Return TRUE if "p" starts with "<SID>" or "s:".
19034 * Only works if eval_fname_script() returned non-zero for "p"!
19035 */
19036 static int
19037eval_fname_sid(p)
19038 char_u *p;
19039{
19040 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19041}
19042
19043/*
19044 * List the head of the function: "name(arg1, arg2)".
19045 */
19046 static void
19047list_func_head(fp, indent)
19048 ufunc_T *fp;
19049 int indent;
19050{
19051 int j;
19052
19053 msg_start();
19054 if (indent)
19055 MSG_PUTS(" ");
19056 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019057 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 {
19059 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019060 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 }
19062 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019063 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019065 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 {
19067 if (j)
19068 MSG_PUTS(", ");
19069 msg_puts(FUNCARG(fp, j));
19070 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019071 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 {
19073 if (j)
19074 MSG_PUTS(", ");
19075 MSG_PUTS("...");
19076 }
19077 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019078 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019079 if (p_verbose > 0)
19080 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081}
19082
19083/*
19084 * Find a function by name, return pointer to it in ufuncs.
19085 * Return NULL for unknown function.
19086 */
19087 static ufunc_T *
19088find_func(name)
19089 char_u *name;
19090{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019091 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019093 hi = hash_find(&func_hashtab, name);
19094 if (!HASHITEM_EMPTY(hi))
19095 return HI2UF(hi);
19096 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097}
19098
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019099#if defined(EXITFREE) || defined(PROTO)
19100 void
19101free_all_functions()
19102{
19103 hashitem_T *hi;
19104
19105 /* Need to start all over every time, because func_free() may change the
19106 * hash table. */
19107 while (func_hashtab.ht_used > 0)
19108 for (hi = func_hashtab.ht_array; ; ++hi)
19109 if (!HASHITEM_EMPTY(hi))
19110 {
19111 func_free(HI2UF(hi));
19112 break;
19113 }
19114}
19115#endif
19116
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019117/*
19118 * Return TRUE if a function "name" exists.
19119 */
19120 static int
19121function_exists(name)
19122 char_u *name;
19123{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019124 char_u *nm = name;
19125 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019126 int n = FALSE;
19127
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019128 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019129 nm = skipwhite(nm);
19130
19131 /* Only accept "funcname", "funcname ", "funcname (..." and
19132 * "funcname(...", not "funcname!...". */
19133 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019134 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019135 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019136 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019137 else
19138 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019139 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019140 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141 return n;
19142}
19143
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019144/*
19145 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019146 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019147 */
19148 static int
19149builtin_function(name)
19150 char_u *name;
19151{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019152 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19153 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019154}
19155
Bram Moolenaar05159a02005-02-26 23:04:13 +000019156#if defined(FEAT_PROFILE) || defined(PROTO)
19157/*
19158 * Start profiling function "fp".
19159 */
19160 static void
19161func_do_profile(fp)
19162 ufunc_T *fp;
19163{
19164 fp->uf_tm_count = 0;
19165 profile_zero(&fp->uf_tm_self);
19166 profile_zero(&fp->uf_tm_total);
19167 if (fp->uf_tml_count == NULL)
19168 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19169 (sizeof(int) * fp->uf_lines.ga_len));
19170 if (fp->uf_tml_total == NULL)
19171 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19172 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19173 if (fp->uf_tml_self == NULL)
19174 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19175 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19176 fp->uf_tml_idx = -1;
19177 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19178 || fp->uf_tml_self == NULL)
19179 return; /* out of memory */
19180
19181 fp->uf_profiling = TRUE;
19182}
19183
19184/*
19185 * Dump the profiling results for all functions in file "fd".
19186 */
19187 void
19188func_dump_profile(fd)
19189 FILE *fd;
19190{
19191 hashitem_T *hi;
19192 int todo;
19193 ufunc_T *fp;
19194 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019195 ufunc_T **sorttab;
19196 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019197
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019198 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019199 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19200
Bram Moolenaar05159a02005-02-26 23:04:13 +000019201 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19202 {
19203 if (!HASHITEM_EMPTY(hi))
19204 {
19205 --todo;
19206 fp = HI2UF(hi);
19207 if (fp->uf_profiling)
19208 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019209 if (sorttab != NULL)
19210 sorttab[st_len++] = fp;
19211
Bram Moolenaar05159a02005-02-26 23:04:13 +000019212 if (fp->uf_name[0] == K_SPECIAL)
19213 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19214 else
19215 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19216 if (fp->uf_tm_count == 1)
19217 fprintf(fd, "Called 1 time\n");
19218 else
19219 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19220 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19221 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19222 fprintf(fd, "\n");
19223 fprintf(fd, "count total (s) self (s)\n");
19224
19225 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19226 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019227 if (FUNCLINE(fp, i) == NULL)
19228 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019229 prof_func_line(fd, fp->uf_tml_count[i],
19230 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019231 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19232 }
19233 fprintf(fd, "\n");
19234 }
19235 }
19236 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019237
19238 if (sorttab != NULL && st_len > 0)
19239 {
19240 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19241 prof_total_cmp);
19242 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19243 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19244 prof_self_cmp);
19245 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19246 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019247}
Bram Moolenaar73830342005-02-28 22:48:19 +000019248
19249 static void
19250prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19251 FILE *fd;
19252 ufunc_T **sorttab;
19253 int st_len;
19254 char *title;
19255 int prefer_self; /* when equal print only self time */
19256{
19257 int i;
19258 ufunc_T *fp;
19259
19260 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19261 fprintf(fd, "count total (s) self (s) function\n");
19262 for (i = 0; i < 20 && i < st_len; ++i)
19263 {
19264 fp = sorttab[i];
19265 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19266 prefer_self);
19267 if (fp->uf_name[0] == K_SPECIAL)
19268 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19269 else
19270 fprintf(fd, " %s()\n", fp->uf_name);
19271 }
19272 fprintf(fd, "\n");
19273}
19274
19275/*
19276 * Print the count and times for one function or function line.
19277 */
19278 static void
19279prof_func_line(fd, count, total, self, prefer_self)
19280 FILE *fd;
19281 int count;
19282 proftime_T *total;
19283 proftime_T *self;
19284 int prefer_self; /* when equal print only self time */
19285{
19286 if (count > 0)
19287 {
19288 fprintf(fd, "%5d ", count);
19289 if (prefer_self && profile_equal(total, self))
19290 fprintf(fd, " ");
19291 else
19292 fprintf(fd, "%s ", profile_msg(total));
19293 if (!prefer_self && profile_equal(total, self))
19294 fprintf(fd, " ");
19295 else
19296 fprintf(fd, "%s ", profile_msg(self));
19297 }
19298 else
19299 fprintf(fd, " ");
19300}
19301
19302/*
19303 * Compare function for total time sorting.
19304 */
19305 static int
19306#ifdef __BORLANDC__
19307_RTLENTRYF
19308#endif
19309prof_total_cmp(s1, s2)
19310 const void *s1;
19311 const void *s2;
19312{
19313 ufunc_T *p1, *p2;
19314
19315 p1 = *(ufunc_T **)s1;
19316 p2 = *(ufunc_T **)s2;
19317 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19318}
19319
19320/*
19321 * Compare function for self time sorting.
19322 */
19323 static int
19324#ifdef __BORLANDC__
19325_RTLENTRYF
19326#endif
19327prof_self_cmp(s1, s2)
19328 const void *s1;
19329 const void *s2;
19330{
19331 ufunc_T *p1, *p2;
19332
19333 p1 = *(ufunc_T **)s1;
19334 p2 = *(ufunc_T **)s2;
19335 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19336}
19337
Bram Moolenaar05159a02005-02-26 23:04:13 +000019338#endif
19339
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019340/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019341 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019342 * Return TRUE if a package was loaded.
19343 */
19344 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019345script_autoload(name, reload)
19346 char_u *name;
19347 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019348{
19349 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019350 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019351 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019352 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019353
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019354 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019355 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019356 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019357 return FALSE;
19358
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019359 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019360
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019361 /* Find the name in the list of previously loaded package names. Skip
19362 * "autoload/", it's always the same. */
19363 for (i = 0; i < ga_loaded.ga_len; ++i)
19364 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19365 break;
19366 if (!reload && i < ga_loaded.ga_len)
19367 ret = FALSE; /* was loaded already */
19368 else
19369 {
19370 /* Remember the name if it wasn't loaded already. */
19371 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19372 {
19373 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19374 tofree = NULL;
19375 }
19376
19377 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019378 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019379 ret = TRUE;
19380 }
19381
19382 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019383 return ret;
19384}
19385
19386/*
19387 * Return the autoload script name for a function or variable name.
19388 * Returns NULL when out of memory.
19389 */
19390 static char_u *
19391autoload_name(name)
19392 char_u *name;
19393{
19394 char_u *p;
19395 char_u *scriptname;
19396
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019397 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019398 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19399 if (scriptname == NULL)
19400 return FALSE;
19401 STRCPY(scriptname, "autoload/");
19402 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019403 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019404 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019405 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019406 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019407 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019408}
19409
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19411
19412/*
19413 * Function given to ExpandGeneric() to obtain the list of user defined
19414 * function names.
19415 */
19416 char_u *
19417get_user_func_name(xp, idx)
19418 expand_T *xp;
19419 int idx;
19420{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019421 static long_u done;
19422 static hashitem_T *hi;
19423 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424
19425 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019427 done = 0;
19428 hi = func_hashtab.ht_array;
19429 }
19430 if (done < func_hashtab.ht_used)
19431 {
19432 if (done++ > 0)
19433 ++hi;
19434 while (HASHITEM_EMPTY(hi))
19435 ++hi;
19436 fp = HI2UF(hi);
19437
19438 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19439 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440
19441 cat_func_name(IObuff, fp);
19442 if (xp->xp_context != EXPAND_USER_FUNC)
19443 {
19444 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019445 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 STRCAT(IObuff, ")");
19447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 return IObuff;
19449 }
19450 return NULL;
19451}
19452
19453#endif /* FEAT_CMDL_COMPL */
19454
19455/*
19456 * Copy the function name of "fp" to buffer "buf".
19457 * "buf" must be able to hold the function name plus three bytes.
19458 * Takes care of script-local function names.
19459 */
19460 static void
19461cat_func_name(buf, fp)
19462 char_u *buf;
19463 ufunc_T *fp;
19464{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019465 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 {
19467 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019468 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 }
19470 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019471 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
19475 * ":delfunction {name}"
19476 */
19477 void
19478ex_delfunction(eap)
19479 exarg_T *eap;
19480{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019481 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 char_u *p;
19483 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019484 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485
19486 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019487 name = trans_function_name(&p, eap->skip, 0, &fudi);
19488 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019490 {
19491 if (fudi.fd_dict != NULL && !eap->skip)
19492 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 if (!ends_excmd(*skipwhite(p)))
19496 {
19497 vim_free(name);
19498 EMSG(_(e_trailing));
19499 return;
19500 }
19501 eap->nextcmd = check_nextcmd(p);
19502 if (eap->nextcmd != NULL)
19503 *p = NUL;
19504
19505 if (!eap->skip)
19506 fp = find_func(name);
19507 vim_free(name);
19508
19509 if (!eap->skip)
19510 {
19511 if (fp == NULL)
19512 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019513 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 return;
19515 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019516 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 {
19518 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19519 return;
19520 }
19521
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019522 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019524 /* Delete the dict item that refers to the function, it will
19525 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019526 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019528 else
19529 func_free(fp);
19530 }
19531}
19532
19533/*
19534 * Free a function and remove it from the list of functions.
19535 */
19536 static void
19537func_free(fp)
19538 ufunc_T *fp;
19539{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019540 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019541
19542 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019543 ga_clear_strings(&(fp->uf_args));
19544 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019545#ifdef FEAT_PROFILE
19546 vim_free(fp->uf_tml_count);
19547 vim_free(fp->uf_tml_total);
19548 vim_free(fp->uf_tml_self);
19549#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019551 /* remove the function from the function hashtable */
19552 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19553 if (HASHITEM_EMPTY(hi))
19554 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019555 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019556 hash_remove(&func_hashtab, hi);
19557
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019558 vim_free(fp);
19559}
19560
19561/*
19562 * Unreference a Function: decrement the reference count and free it when it
19563 * becomes zero. Only for numbered functions.
19564 */
19565 static void
19566func_unref(name)
19567 char_u *name;
19568{
19569 ufunc_T *fp;
19570
19571 if (name != NULL && isdigit(*name))
19572 {
19573 fp = find_func(name);
19574 if (fp == NULL)
19575 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019576 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019577 {
19578 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019579 * when "uf_calls" becomes zero. */
19580 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019581 func_free(fp);
19582 }
19583 }
19584}
19585
19586/*
19587 * Count a reference to a Function.
19588 */
19589 static void
19590func_ref(name)
19591 char_u *name;
19592{
19593 ufunc_T *fp;
19594
19595 if (name != NULL && isdigit(*name))
19596 {
19597 fp = find_func(name);
19598 if (fp == NULL)
19599 EMSG2(_(e_intern2), "func_ref()");
19600 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019601 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 }
19603}
19604
19605/*
19606 * Call a user function.
19607 */
19608 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019609call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 ufunc_T *fp; /* pointer to function */
19611 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 typval_T *argvars; /* arguments */
19613 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 linenr_T firstline; /* first line of range */
19615 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617{
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 char_u *save_sourcing_name;
19619 linenr_T save_sourcing_lnum;
19620 scid_T save_current_SID;
19621 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 int save_did_emsg;
19623 static int depth = 0;
19624 dictitem_T *v;
19625 int fixvar_idx = 0; /* index in fixvar[] */
19626 int i;
19627 int ai;
19628 char_u numbuf[NUMBUFLEN];
19629 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019630#ifdef FEAT_PROFILE
19631 proftime_T wait_start;
19632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633
19634 /* If depth of calling is getting too high, don't execute the function */
19635 if (depth >= p_mfd)
19636 {
19637 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638 rettv->v_type = VAR_NUMBER;
19639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 return;
19641 }
19642 ++depth;
19643
19644 line_breakcheck(); /* check for CTRL-C hit */
19645
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019646 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649 fc.rettv = rettv;
19650 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 fc.linenr = 0;
19652 fc.returned = FALSE;
19653 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019655 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 fc.dbg_tick = debug_tick;
19657
Bram Moolenaar33570922005-01-25 22:26:29 +000019658 /*
19659 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19660 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19661 * each argument variable and saves a lot of time.
19662 */
19663 /*
19664 * Init l: variables.
19665 */
19666 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019667 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019668 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019669 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19670 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019672 name = v->di_key;
19673 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19675 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19676 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019677 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 v->di_tv.vval.v_dict = selfdict;
19679 ++selfdict->dv_refcount;
19680 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019681
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 /*
19683 * Init a: variables.
19684 * Set a:0 to "argcount".
19685 * Set a:000 to a list with room for the "..." arguments.
19686 */
19687 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19688 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019689 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019690 v = &fc.fixvar[fixvar_idx++].var;
19691 STRCPY(v->di_key, "000");
19692 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19693 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19694 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019695 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 v->di_tv.vval.v_list = &fc.l_varlist;
19697 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19698 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019699 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019700
19701 /*
19702 * Set a:firstline to "firstline" and a:lastline to "lastline".
19703 * Set a:name to named arguments.
19704 * Set a:N to the "..." arguments.
19705 */
19706 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19707 (varnumber_T)firstline);
19708 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19709 (varnumber_T)lastline);
19710 for (i = 0; i < argcount; ++i)
19711 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019712 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 if (ai < 0)
19714 /* named argument a:name */
19715 name = FUNCARG(fp, i);
19716 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019718 /* "..." argument a:1, a:2, etc. */
19719 sprintf((char *)numbuf, "%d", ai + 1);
19720 name = numbuf;
19721 }
19722 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19723 {
19724 v = &fc.fixvar[fixvar_idx++].var;
19725 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19726 }
19727 else
19728 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019729 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19730 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 if (v == NULL)
19732 break;
19733 v->di_flags = DI_FLAGS_RO;
19734 }
19735 STRCPY(v->di_key, name);
19736 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19737
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019738 /* Note: the values are copied directly to avoid alloc/free.
19739 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019740 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019741 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019742
19743 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19744 {
19745 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19746 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019747 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019748 }
19749 }
19750
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 /* Don't redraw while executing the function. */
19752 ++RedrawingDisabled;
19753 save_sourcing_name = sourcing_name;
19754 save_sourcing_lnum = sourcing_lnum;
19755 sourcing_lnum = 1;
19756 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019757 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 if (sourcing_name != NULL)
19759 {
19760 if (save_sourcing_name != NULL
19761 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19762 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19763 else
19764 STRCPY(sourcing_name, "function ");
19765 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19766
19767 if (p_verbose >= 12)
19768 {
19769 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019770 verbose_enter_scroll();
19771
Bram Moolenaar555b2802005-05-19 21:08:39 +000019772 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 if (p_verbose >= 14)
19774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019776 char_u numbuf[NUMBUFLEN];
19777 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778
19779 msg_puts((char_u *)"(");
19780 for (i = 0; i < argcount; ++i)
19781 {
19782 if (i > 0)
19783 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 if (argvars[i].v_type == VAR_NUMBER)
19785 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 else
19787 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019788 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019789 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019791 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 }
19793 }
19794 msg_puts((char_u *)")");
19795 }
19796 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019797
19798 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 --no_wait_return;
19800 }
19801 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019802#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019803 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019804 {
19805 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19806 func_do_profile(fp);
19807 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019808 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019809 {
19810 ++fp->uf_tm_count;
19811 profile_start(&fp->uf_tm_start);
19812 profile_zero(&fp->uf_tm_children);
19813 }
19814 script_prof_save(&wait_start);
19815 }
19816#endif
19817
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019819 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 save_did_emsg = did_emsg;
19821 did_emsg = FALSE;
19822
19823 /* call do_cmdline() to execute the lines */
19824 do_cmdline(NULL, get_func_line, (void *)&fc,
19825 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19826
19827 --RedrawingDisabled;
19828
19829 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019830 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832 clear_tv(rettv);
19833 rettv->v_type = VAR_NUMBER;
19834 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 }
19836
Bram Moolenaar05159a02005-02-26 23:04:13 +000019837#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019838 if (do_profiling == PROF_YES && (fp->uf_profiling
19839 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019840 {
19841 profile_end(&fp->uf_tm_start);
19842 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19843 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019844 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019845 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019846 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019847 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19848 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019849 }
19850 }
19851#endif
19852
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 /* when being verbose, mention the return value */
19854 if (p_verbose >= 12)
19855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019857 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019860 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019862 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19863 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019866 char_u buf[MSG_BUF_LEN];
19867 char_u numbuf[NUMBUFLEN];
19868 char_u *tofree;
19869
Bram Moolenaar555b2802005-05-19 21:08:39 +000019870 /* The value may be very long. Skip the middle part, so that we
19871 * have some idea how it starts and ends. smsg() would always
19872 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019873 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019874 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019875 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019876 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 }
19878 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019879
19880 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 --no_wait_return;
19882 }
19883
19884 vim_free(sourcing_name);
19885 sourcing_name = save_sourcing_name;
19886 sourcing_lnum = save_sourcing_lnum;
19887 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019888#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019889 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019890 script_prof_restore(&wait_start);
19891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892
19893 if (p_verbose >= 12 && sourcing_name != NULL)
19894 {
19895 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019896 verbose_enter_scroll();
19897
Bram Moolenaar555b2802005-05-19 21:08:39 +000019898 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019900
19901 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 --no_wait_return;
19903 }
19904
19905 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019906 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 /* The a: variables typevals were not alloced, only free the allocated
19909 * variables. */
19910 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19911
19912 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 --depth;
19914}
19915
19916/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019917 * Add a number variable "name" to dict "dp" with value "nr".
19918 */
19919 static void
19920add_nr_var(dp, v, name, nr)
19921 dict_T *dp;
19922 dictitem_T *v;
19923 char *name;
19924 varnumber_T nr;
19925{
19926 STRCPY(v->di_key, name);
19927 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19928 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19929 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019930 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 v->di_tv.vval.v_number = nr;
19932}
19933
19934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 * ":return [expr]"
19936 */
19937 void
19938ex_return(eap)
19939 exarg_T *eap;
19940{
19941 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019942 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 int returning = FALSE;
19944
19945 if (current_funccal == NULL)
19946 {
19947 EMSG(_("E133: :return not inside a function"));
19948 return;
19949 }
19950
19951 if (eap->skip)
19952 ++emsg_skip;
19953
19954 eap->nextcmd = NULL;
19955 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019956 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 {
19958 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019959 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 }
19963 /* It's safer to return also on error. */
19964 else if (!eap->skip)
19965 {
19966 /*
19967 * Return unless the expression evaluation has been cancelled due to an
19968 * aborting error, an interrupt, or an exception.
19969 */
19970 if (!aborting())
19971 returning = do_return(eap, FALSE, TRUE, NULL);
19972 }
19973
19974 /* When skipping or the return gets pending, advance to the next command
19975 * in this line (!returning). Otherwise, ignore the rest of the line.
19976 * Following lines will be ignored by get_func_line(). */
19977 if (returning)
19978 eap->nextcmd = NULL;
19979 else if (eap->nextcmd == NULL) /* no argument */
19980 eap->nextcmd = check_nextcmd(arg);
19981
19982 if (eap->skip)
19983 --emsg_skip;
19984}
19985
19986/*
19987 * Return from a function. Possibly makes the return pending. Also called
19988 * for a pending return at the ":endtry" or after returning from an extra
19989 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 * FALSE when the return gets pending.
19993 */
19994 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 exarg_T *eap;
19997 int reanimate;
19998 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000{
20001 int idx;
20002 struct condstack *cstack = eap->cstack;
20003
20004 if (reanimate)
20005 /* Undo the return. */
20006 current_funccal->returned = FALSE;
20007
20008 /*
20009 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20010 * not in its finally clause (which then is to be executed next) is found.
20011 * In this case, make the ":return" pending for execution at the ":endtry".
20012 * Otherwise, return normally.
20013 */
20014 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20015 if (idx >= 0)
20016 {
20017 cstack->cs_pending[idx] = CSTP_RETURN;
20018
20019 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 /* A pending return again gets pending. "rettv" points to an
20021 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 else
20025 {
20026 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020027 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 {
20033 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020035 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 else
20037 EMSG(_(e_outofmem));
20038 }
20039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041
20042 if (reanimate)
20043 {
20044 /* The pending return value could be overwritten by a ":return"
20045 * without argument in a finally clause; reset the default
20046 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 current_funccal->rettv->v_type = VAR_NUMBER;
20048 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 }
20050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 }
20053 else
20054 {
20055 current_funccal->returned = TRUE;
20056
20057 /* If the return is carried out now, store the return value. For
20058 * a return immediately after reanimation, the value is already
20059 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020065 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 }
20067 }
20068
20069 return idx < 0;
20070}
20071
20072/*
20073 * Free the variable with a pending return value.
20074 */
20075 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076discard_pending_return(rettv)
20077 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078{
Bram Moolenaar33570922005-01-25 22:26:29 +000020079 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080}
20081
20082/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 * is an allocated string. Used by report_pending() for verbose messages.
20085 */
20086 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087get_return_cmd(rettv)
20088 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020090 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020092 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020094 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020095 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020096 if (s == NULL)
20097 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098
20099 STRCPY(IObuff, ":return ");
20100 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20101 if (STRLEN(s) + 8 >= IOSIZE)
20102 STRCPY(IObuff + IOSIZE - 4, "...");
20103 vim_free(tofree);
20104 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105}
20106
20107/*
20108 * Get next function line.
20109 * Called by do_cmdline() to get the next line.
20110 * Returns allocated string, or NULL for end of function.
20111 */
20112/* ARGSUSED */
20113 char_u *
20114get_func_line(c, cookie, indent)
20115 int c; /* not used */
20116 void *cookie;
20117 int indent; /* not used */
20118{
Bram Moolenaar33570922005-01-25 22:26:29 +000020119 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020120 ufunc_T *fp = fcp->func;
20121 char_u *retval;
20122 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123
20124 /* If breakpoints have been added/deleted need to check for it. */
20125 if (fcp->dbg_tick != debug_tick)
20126 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020127 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 sourcing_lnum);
20129 fcp->dbg_tick = debug_tick;
20130 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020131#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020132 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020133 func_line_end(cookie);
20134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135
Bram Moolenaar05159a02005-02-26 23:04:13 +000020136 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020137 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20138 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 retval = NULL;
20140 else
20141 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020142 /* Skip NULL lines (continuation lines). */
20143 while (fcp->linenr < gap->ga_len
20144 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20145 ++fcp->linenr;
20146 if (fcp->linenr >= gap->ga_len)
20147 retval = NULL;
20148 else
20149 {
20150 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20151 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020152#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020153 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020154 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020155#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 }
20158
20159 /* Did we encounter a breakpoint? */
20160 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20161 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020162 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020164 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 sourcing_lnum);
20166 fcp->dbg_tick = debug_tick;
20167 }
20168
20169 return retval;
20170}
20171
Bram Moolenaar05159a02005-02-26 23:04:13 +000020172#if defined(FEAT_PROFILE) || defined(PROTO)
20173/*
20174 * Called when starting to read a function line.
20175 * "sourcing_lnum" must be correct!
20176 * When skipping lines it may not actually be executed, but we won't find out
20177 * until later and we need to store the time now.
20178 */
20179 void
20180func_line_start(cookie)
20181 void *cookie;
20182{
20183 funccall_T *fcp = (funccall_T *)cookie;
20184 ufunc_T *fp = fcp->func;
20185
20186 if (fp->uf_profiling && sourcing_lnum >= 1
20187 && sourcing_lnum <= fp->uf_lines.ga_len)
20188 {
20189 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020190 /* Skip continuation lines. */
20191 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20192 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020193 fp->uf_tml_execed = FALSE;
20194 profile_start(&fp->uf_tml_start);
20195 profile_zero(&fp->uf_tml_children);
20196 profile_get_wait(&fp->uf_tml_wait);
20197 }
20198}
20199
20200/*
20201 * Called when actually executing a function line.
20202 */
20203 void
20204func_line_exec(cookie)
20205 void *cookie;
20206{
20207 funccall_T *fcp = (funccall_T *)cookie;
20208 ufunc_T *fp = fcp->func;
20209
20210 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20211 fp->uf_tml_execed = TRUE;
20212}
20213
20214/*
20215 * Called when done with a function line.
20216 */
20217 void
20218func_line_end(cookie)
20219 void *cookie;
20220{
20221 funccall_T *fcp = (funccall_T *)cookie;
20222 ufunc_T *fp = fcp->func;
20223
20224 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20225 {
20226 if (fp->uf_tml_execed)
20227 {
20228 ++fp->uf_tml_count[fp->uf_tml_idx];
20229 profile_end(&fp->uf_tml_start);
20230 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020231 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020232 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20233 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020234 }
20235 fp->uf_tml_idx = -1;
20236 }
20237}
20238#endif
20239
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240/*
20241 * Return TRUE if the currently active function should be ended, because a
20242 * return was encountered or an error occured. Used inside a ":while".
20243 */
20244 int
20245func_has_ended(cookie)
20246 void *cookie;
20247{
Bram Moolenaar33570922005-01-25 22:26:29 +000020248 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249
20250 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20251 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020252 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 || fcp->returned);
20254}
20255
20256/*
20257 * return TRUE if cookie indicates a function which "abort"s on errors.
20258 */
20259 int
20260func_has_abort(cookie)
20261 void *cookie;
20262{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020263 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264}
20265
20266#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20267typedef enum
20268{
20269 VAR_FLAVOUR_DEFAULT,
20270 VAR_FLAVOUR_SESSION,
20271 VAR_FLAVOUR_VIMINFO
20272} var_flavour_T;
20273
20274static var_flavour_T var_flavour __ARGS((char_u *varname));
20275
20276 static var_flavour_T
20277var_flavour(varname)
20278 char_u *varname;
20279{
20280 char_u *p = varname;
20281
20282 if (ASCII_ISUPPER(*p))
20283 {
20284 while (*(++p))
20285 if (ASCII_ISLOWER(*p))
20286 return VAR_FLAVOUR_SESSION;
20287 return VAR_FLAVOUR_VIMINFO;
20288 }
20289 else
20290 return VAR_FLAVOUR_DEFAULT;
20291}
20292#endif
20293
20294#if defined(FEAT_VIMINFO) || defined(PROTO)
20295/*
20296 * Restore global vars that start with a capital from the viminfo file
20297 */
20298 int
20299read_viminfo_varlist(virp, writing)
20300 vir_T *virp;
20301 int writing;
20302{
20303 char_u *tab;
20304 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306
20307 if (!writing && (find_viminfo_parameter('!') != NULL))
20308 {
20309 tab = vim_strchr(virp->vir_line + 1, '\t');
20310 if (tab != NULL)
20311 {
20312 *tab++ = '\0'; /* isolate the variable name */
20313 if (*tab == 'S') /* string var */
20314 is_string = TRUE;
20315
20316 tab = vim_strchr(tab, '\t');
20317 if (tab != NULL)
20318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (is_string)
20320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020321 tv.v_type = VAR_STRING;
20322 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 }
20325 else
20326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020327 tv.v_type = VAR_NUMBER;
20328 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020330 set_var(virp->vir_line + 1, &tv, FALSE);
20331 if (is_string)
20332 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 }
20334 }
20335 }
20336
20337 return viminfo_readline(virp);
20338}
20339
20340/*
20341 * Write global vars that start with a capital to the viminfo file
20342 */
20343 void
20344write_viminfo_varlist(fp)
20345 FILE *fp;
20346{
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 hashitem_T *hi;
20348 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020349 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020351 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020352 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020353 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354
20355 if (find_viminfo_parameter('!') == NULL)
20356 return;
20357
20358 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020359
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020360 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020365 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 this_var = HI2DI(hi);
20367 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020370 {
20371 case VAR_STRING: s = "STR"; break;
20372 case VAR_NUMBER: s = "NUM"; break;
20373 default: continue;
20374 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020376 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020377 if (p != NULL)
20378 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020379 vim_free(tofree);
20380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 }
20382 }
20383}
20384#endif
20385
20386#if defined(FEAT_SESSION) || defined(PROTO)
20387 int
20388store_session_globals(fd)
20389 FILE *fd;
20390{
Bram Moolenaar33570922005-01-25 22:26:29 +000020391 hashitem_T *hi;
20392 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020393 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 char_u *p, *t;
20395
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020396 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020397 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020401 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020402 this_var = HI2DI(hi);
20403 if ((this_var->di_tv.v_type == VAR_NUMBER
20404 || this_var->di_tv.v_type == VAR_STRING)
20405 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020406 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020407 /* Escape special characters with a backslash. Turn a LF and
20408 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020410 (char_u *)"\\\"\n\r");
20411 if (p == NULL) /* out of memory */
20412 break;
20413 for (t = p; *t != NUL; ++t)
20414 if (*t == '\n')
20415 *t = 'n';
20416 else if (*t == '\r')
20417 *t = 'r';
20418 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020419 this_var->di_key,
20420 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20421 : ' ',
20422 p,
20423 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20424 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020425 || put_eol(fd) == FAIL)
20426 {
20427 vim_free(p);
20428 return FAIL;
20429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 vim_free(p);
20431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 }
20433 }
20434 return OK;
20435}
20436#endif
20437
Bram Moolenaar661b1822005-07-28 22:36:45 +000020438/*
20439 * Display script name where an item was last set.
20440 * Should only be invoked when 'verbose' is non-zero.
20441 */
20442 void
20443last_set_msg(scriptID)
20444 scid_T scriptID;
20445{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020446 char_u *p;
20447
Bram Moolenaar661b1822005-07-28 22:36:45 +000020448 if (scriptID != 0)
20449 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020450 p = home_replace_save(NULL, get_scriptname(scriptID));
20451 if (p != NULL)
20452 {
20453 verbose_enter();
20454 MSG_PUTS(_("\n\tLast set from "));
20455 MSG_PUTS(p);
20456 vim_free(p);
20457 verbose_leave();
20458 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020459 }
20460}
20461
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462#endif /* FEAT_EVAL */
20463
20464#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20465
20466
20467#ifdef WIN3264
20468/*
20469 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20470 */
20471static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20472static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20473static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20474
20475/*
20476 * Get the short pathname of a file.
20477 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20478 */
20479 static int
20480get_short_pathname(fnamep, bufp, fnamelen)
20481 char_u **fnamep;
20482 char_u **bufp;
20483 int *fnamelen;
20484{
20485 int l,len;
20486 char_u *newbuf;
20487
20488 len = *fnamelen;
20489
20490 l = GetShortPathName(*fnamep, *fnamep, len);
20491 if (l > len - 1)
20492 {
20493 /* If that doesn't work (not enough space), then save the string
20494 * and try again with a new buffer big enough
20495 */
20496 newbuf = vim_strnsave(*fnamep, l);
20497 if (newbuf == NULL)
20498 return 0;
20499
20500 vim_free(*bufp);
20501 *fnamep = *bufp = newbuf;
20502
20503 l = GetShortPathName(*fnamep,*fnamep,l+1);
20504
20505 /* Really should always succeed, as the buffer is big enough */
20506 }
20507
20508 *fnamelen = l;
20509 return 1;
20510}
20511
20512/*
20513 * Create a short path name. Returns the length of the buffer it needs.
20514 * Doesn't copy over the end of the buffer passed in.
20515 */
20516 static int
20517shortpath_for_invalid_fname(fname, bufp, fnamelen)
20518 char_u **fname;
20519 char_u **bufp;
20520 int *fnamelen;
20521{
20522 char_u *s, *p, *pbuf2, *pbuf3;
20523 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020524 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525
20526 /* Make a copy */
20527 len2 = *fnamelen;
20528 pbuf2 = vim_strnsave(*fname, len2);
20529 pbuf3 = NULL;
20530
20531 s = pbuf2 + len2 - 1; /* Find the end */
20532 slen = 1;
20533 plen = len2;
20534
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020535 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 {
20537 --s;
20538 ++slen;
20539 --plen;
20540 }
20541
20542 do
20543 {
20544 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020545 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 {
20547 --s;
20548 ++slen;
20549 --plen;
20550 }
20551 if (s <= pbuf2)
20552 break;
20553
20554 /* Remeber the character that is about to be blatted */
20555 ch = *s;
20556 *s = 0; /* get_short_pathname requires a null-terminated string */
20557
20558 /* Try it in situ */
20559 p = pbuf2;
20560 if (!get_short_pathname(&p, &pbuf3, &plen))
20561 {
20562 vim_free(pbuf2);
20563 return -1;
20564 }
20565 *s = ch; /* Preserve the string */
20566 } while (plen == 0);
20567
20568 if (plen > 0)
20569 {
20570 /* Remeber the length of the new string. */
20571 *fnamelen = len = plen + slen;
20572 vim_free(*bufp);
20573 if (len > len2)
20574 {
20575 /* If there's not enough space in the currently allocated string,
20576 * then copy it to a buffer big enough.
20577 */
20578 *fname= *bufp = vim_strnsave(p, len);
20579 if (*fname == NULL)
20580 return -1;
20581 }
20582 else
20583 {
20584 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20585 *fname = *bufp = pbuf2;
20586 if (p != pbuf2)
20587 strncpy(*fname, p, plen);
20588 pbuf2 = NULL;
20589 }
20590 /* Concat the next bit */
20591 strncpy(*fname + plen, s, slen);
20592 (*fname)[len] = '\0';
20593 }
20594 vim_free(pbuf3);
20595 vim_free(pbuf2);
20596 return 0;
20597}
20598
20599/*
20600 * Get a pathname for a partial path.
20601 */
20602 static int
20603shortpath_for_partial(fnamep, bufp, fnamelen)
20604 char_u **fnamep;
20605 char_u **bufp;
20606 int *fnamelen;
20607{
20608 int sepcount, len, tflen;
20609 char_u *p;
20610 char_u *pbuf, *tfname;
20611 int hasTilde;
20612
20613 /* Count up the path seperators from the RHS.. so we know which part
20614 * of the path to return.
20615 */
20616 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020617 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 if (vim_ispathsep(*p))
20619 ++sepcount;
20620
20621 /* Need full path first (use expand_env() to remove a "~/") */
20622 hasTilde = (**fnamep == '~');
20623 if (hasTilde)
20624 pbuf = tfname = expand_env_save(*fnamep);
20625 else
20626 pbuf = tfname = FullName_save(*fnamep, FALSE);
20627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020628 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629
20630 if (!get_short_pathname(&tfname, &pbuf, &len))
20631 return -1;
20632
20633 if (len == 0)
20634 {
20635 /* Don't have a valid filename, so shorten the rest of the
20636 * path if we can. This CAN give us invalid 8.3 filenames, but
20637 * there's not a lot of point in guessing what it might be.
20638 */
20639 len = tflen;
20640 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20641 return -1;
20642 }
20643
20644 /* Count the paths backward to find the beginning of the desired string. */
20645 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020646 {
20647#ifdef FEAT_MBYTE
20648 if (has_mbyte)
20649 p -= mb_head_off(tfname, p);
20650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 if (vim_ispathsep(*p))
20652 {
20653 if (sepcount == 0 || (hasTilde && sepcount == 1))
20654 break;
20655 else
20656 sepcount --;
20657 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 if (hasTilde)
20660 {
20661 --p;
20662 if (p >= tfname)
20663 *p = '~';
20664 else
20665 return -1;
20666 }
20667 else
20668 ++p;
20669
20670 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20671 vim_free(*bufp);
20672 *fnamelen = (int)STRLEN(p);
20673 *bufp = pbuf;
20674 *fnamep = p;
20675
20676 return 0;
20677}
20678#endif /* WIN3264 */
20679
20680/*
20681 * Adjust a filename, according to a string of modifiers.
20682 * *fnamep must be NUL terminated when called. When returning, the length is
20683 * determined by *fnamelen.
20684 * Returns valid flags.
20685 * When there is an error, *fnamep is set to NULL.
20686 */
20687 int
20688modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20689 char_u *src; /* string with modifiers */
20690 int *usedlen; /* characters after src that are used */
20691 char_u **fnamep; /* file name so far */
20692 char_u **bufp; /* buffer for allocated file name or NULL */
20693 int *fnamelen; /* length of fnamep */
20694{
20695 int valid = 0;
20696 char_u *tail;
20697 char_u *s, *p, *pbuf;
20698 char_u dirname[MAXPATHL];
20699 int c;
20700 int has_fullname = 0;
20701#ifdef WIN3264
20702 int has_shortname = 0;
20703#endif
20704
20705repeat:
20706 /* ":p" - full path/file_name */
20707 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20708 {
20709 has_fullname = 1;
20710
20711 valid |= VALID_PATH;
20712 *usedlen += 2;
20713
20714 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20715 if ((*fnamep)[0] == '~'
20716#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20717 && ((*fnamep)[1] == '/'
20718# ifdef BACKSLASH_IN_FILENAME
20719 || (*fnamep)[1] == '\\'
20720# endif
20721 || (*fnamep)[1] == NUL)
20722
20723#endif
20724 )
20725 {
20726 *fnamep = expand_env_save(*fnamep);
20727 vim_free(*bufp); /* free any allocated file name */
20728 *bufp = *fnamep;
20729 if (*fnamep == NULL)
20730 return -1;
20731 }
20732
20733 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020734 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 {
20736 if (vim_ispathsep(*p)
20737 && p[1] == '.'
20738 && (p[2] == NUL
20739 || vim_ispathsep(p[2])
20740 || (p[2] == '.'
20741 && (p[3] == NUL || vim_ispathsep(p[3])))))
20742 break;
20743 }
20744
20745 /* FullName_save() is slow, don't use it when not needed. */
20746 if (*p != NUL || !vim_isAbsName(*fnamep))
20747 {
20748 *fnamep = FullName_save(*fnamep, *p != NUL);
20749 vim_free(*bufp); /* free any allocated file name */
20750 *bufp = *fnamep;
20751 if (*fnamep == NULL)
20752 return -1;
20753 }
20754
20755 /* Append a path separator to a directory. */
20756 if (mch_isdir(*fnamep))
20757 {
20758 /* Make room for one or two extra characters. */
20759 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20760 vim_free(*bufp); /* free any allocated file name */
20761 *bufp = *fnamep;
20762 if (*fnamep == NULL)
20763 return -1;
20764 add_pathsep(*fnamep);
20765 }
20766 }
20767
20768 /* ":." - path relative to the current directory */
20769 /* ":~" - path relative to the home directory */
20770 /* ":8" - shortname path - postponed till after */
20771 while (src[*usedlen] == ':'
20772 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20773 {
20774 *usedlen += 2;
20775 if (c == '8')
20776 {
20777#ifdef WIN3264
20778 has_shortname = 1; /* Postpone this. */
20779#endif
20780 continue;
20781 }
20782 pbuf = NULL;
20783 /* Need full path first (use expand_env() to remove a "~/") */
20784 if (!has_fullname)
20785 {
20786 if (c == '.' && **fnamep == '~')
20787 p = pbuf = expand_env_save(*fnamep);
20788 else
20789 p = pbuf = FullName_save(*fnamep, FALSE);
20790 }
20791 else
20792 p = *fnamep;
20793
20794 has_fullname = 0;
20795
20796 if (p != NULL)
20797 {
20798 if (c == '.')
20799 {
20800 mch_dirname(dirname, MAXPATHL);
20801 s = shorten_fname(p, dirname);
20802 if (s != NULL)
20803 {
20804 *fnamep = s;
20805 if (pbuf != NULL)
20806 {
20807 vim_free(*bufp); /* free any allocated file name */
20808 *bufp = pbuf;
20809 pbuf = NULL;
20810 }
20811 }
20812 }
20813 else
20814 {
20815 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20816 /* Only replace it when it starts with '~' */
20817 if (*dirname == '~')
20818 {
20819 s = vim_strsave(dirname);
20820 if (s != NULL)
20821 {
20822 *fnamep = s;
20823 vim_free(*bufp);
20824 *bufp = s;
20825 }
20826 }
20827 }
20828 vim_free(pbuf);
20829 }
20830 }
20831
20832 tail = gettail(*fnamep);
20833 *fnamelen = (int)STRLEN(*fnamep);
20834
20835 /* ":h" - head, remove "/file_name", can be repeated */
20836 /* Don't remove the first "/" or "c:\" */
20837 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20838 {
20839 valid |= VALID_HEAD;
20840 *usedlen += 2;
20841 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020842 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 --tail;
20844 *fnamelen = (int)(tail - *fnamep);
20845#ifdef VMS
20846 if (*fnamelen > 0)
20847 *fnamelen += 1; /* the path separator is part of the path */
20848#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020849 while (tail > s && !after_pathsep(s, tail))
20850 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 }
20852
20853 /* ":8" - shortname */
20854 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20855 {
20856 *usedlen += 2;
20857#ifdef WIN3264
20858 has_shortname = 1;
20859#endif
20860 }
20861
20862#ifdef WIN3264
20863 /* Check shortname after we have done 'heads' and before we do 'tails'
20864 */
20865 if (has_shortname)
20866 {
20867 pbuf = NULL;
20868 /* Copy the string if it is shortened by :h */
20869 if (*fnamelen < (int)STRLEN(*fnamep))
20870 {
20871 p = vim_strnsave(*fnamep, *fnamelen);
20872 if (p == 0)
20873 return -1;
20874 vim_free(*bufp);
20875 *bufp = *fnamep = p;
20876 }
20877
20878 /* Split into two implementations - makes it easier. First is where
20879 * there isn't a full name already, second is where there is.
20880 */
20881 if (!has_fullname && !vim_isAbsName(*fnamep))
20882 {
20883 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20884 return -1;
20885 }
20886 else
20887 {
20888 int l;
20889
20890 /* Simple case, already have the full-name
20891 * Nearly always shorter, so try first time. */
20892 l = *fnamelen;
20893 if (!get_short_pathname(fnamep, bufp, &l))
20894 return -1;
20895
20896 if (l == 0)
20897 {
20898 /* Couldn't find the filename.. search the paths.
20899 */
20900 l = *fnamelen;
20901 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20902 return -1;
20903 }
20904 *fnamelen = l;
20905 }
20906 }
20907#endif /* WIN3264 */
20908
20909 /* ":t" - tail, just the basename */
20910 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20911 {
20912 *usedlen += 2;
20913 *fnamelen -= (int)(tail - *fnamep);
20914 *fnamep = tail;
20915 }
20916
20917 /* ":e" - extension, can be repeated */
20918 /* ":r" - root, without extension, can be repeated */
20919 while (src[*usedlen] == ':'
20920 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20921 {
20922 /* find a '.' in the tail:
20923 * - for second :e: before the current fname
20924 * - otherwise: The last '.'
20925 */
20926 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20927 s = *fnamep - 2;
20928 else
20929 s = *fnamep + *fnamelen - 1;
20930 for ( ; s > tail; --s)
20931 if (s[0] == '.')
20932 break;
20933 if (src[*usedlen + 1] == 'e') /* :e */
20934 {
20935 if (s > tail)
20936 {
20937 *fnamelen += (int)(*fnamep - (s + 1));
20938 *fnamep = s + 1;
20939#ifdef VMS
20940 /* cut version from the extension */
20941 s = *fnamep + *fnamelen - 1;
20942 for ( ; s > *fnamep; --s)
20943 if (s[0] == ';')
20944 break;
20945 if (s > *fnamep)
20946 *fnamelen = s - *fnamep;
20947#endif
20948 }
20949 else if (*fnamep <= tail)
20950 *fnamelen = 0;
20951 }
20952 else /* :r */
20953 {
20954 if (s > tail) /* remove one extension */
20955 *fnamelen = (int)(s - *fnamep);
20956 }
20957 *usedlen += 2;
20958 }
20959
20960 /* ":s?pat?foo?" - substitute */
20961 /* ":gs?pat?foo?" - global substitute */
20962 if (src[*usedlen] == ':'
20963 && (src[*usedlen + 1] == 's'
20964 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20965 {
20966 char_u *str;
20967 char_u *pat;
20968 char_u *sub;
20969 int sep;
20970 char_u *flags;
20971 int didit = FALSE;
20972
20973 flags = (char_u *)"";
20974 s = src + *usedlen + 2;
20975 if (src[*usedlen + 1] == 'g')
20976 {
20977 flags = (char_u *)"g";
20978 ++s;
20979 }
20980
20981 sep = *s++;
20982 if (sep)
20983 {
20984 /* find end of pattern */
20985 p = vim_strchr(s, sep);
20986 if (p != NULL)
20987 {
20988 pat = vim_strnsave(s, (int)(p - s));
20989 if (pat != NULL)
20990 {
20991 s = p + 1;
20992 /* find end of substitution */
20993 p = vim_strchr(s, sep);
20994 if (p != NULL)
20995 {
20996 sub = vim_strnsave(s, (int)(p - s));
20997 str = vim_strnsave(*fnamep, *fnamelen);
20998 if (sub != NULL && str != NULL)
20999 {
21000 *usedlen = (int)(p + 1 - src);
21001 s = do_string_sub(str, pat, sub, flags);
21002 if (s != NULL)
21003 {
21004 *fnamep = s;
21005 *fnamelen = (int)STRLEN(s);
21006 vim_free(*bufp);
21007 *bufp = s;
21008 didit = TRUE;
21009 }
21010 }
21011 vim_free(sub);
21012 vim_free(str);
21013 }
21014 vim_free(pat);
21015 }
21016 }
21017 /* after using ":s", repeat all the modifiers */
21018 if (didit)
21019 goto repeat;
21020 }
21021 }
21022
21023 return valid;
21024}
21025
21026/*
21027 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21028 * "flags" can be "g" to do a global substitute.
21029 * Returns an allocated string, NULL for error.
21030 */
21031 char_u *
21032do_string_sub(str, pat, sub, flags)
21033 char_u *str;
21034 char_u *pat;
21035 char_u *sub;
21036 char_u *flags;
21037{
21038 int sublen;
21039 regmatch_T regmatch;
21040 int i;
21041 int do_all;
21042 char_u *tail;
21043 garray_T ga;
21044 char_u *ret;
21045 char_u *save_cpo;
21046
21047 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21048 save_cpo = p_cpo;
21049 p_cpo = (char_u *)"";
21050
21051 ga_init2(&ga, 1, 200);
21052
21053 do_all = (flags[0] == 'g');
21054
21055 regmatch.rm_ic = p_ic;
21056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21057 if (regmatch.regprog != NULL)
21058 {
21059 tail = str;
21060 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21061 {
21062 /*
21063 * Get some space for a temporary buffer to do the substitution
21064 * into. It will contain:
21065 * - The text up to where the match is.
21066 * - The substituted text.
21067 * - The text after the match.
21068 */
21069 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21070 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21071 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21072 {
21073 ga_clear(&ga);
21074 break;
21075 }
21076
21077 /* copy the text up to where the match is */
21078 i = (int)(regmatch.startp[0] - tail);
21079 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21080 /* add the substituted text */
21081 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21082 + ga.ga_len + i, TRUE, TRUE, FALSE);
21083 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 /* avoid getting stuck on a match with an empty string */
21085 if (tail == regmatch.endp[0])
21086 {
21087 if (*tail == NUL)
21088 break;
21089 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21090 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 }
21092 else
21093 {
21094 tail = regmatch.endp[0];
21095 if (*tail == NUL)
21096 break;
21097 }
21098 if (!do_all)
21099 break;
21100 }
21101
21102 if (ga.ga_data != NULL)
21103 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21104
21105 vim_free(regmatch.regprog);
21106 }
21107
21108 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21109 ga_clear(&ga);
21110 p_cpo = save_cpo;
21111
21112 return ret;
21113}
21114
21115#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */