blob: 0762cabacb30765070ca73333b5d6a791cdbb03f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000201/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000202static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
203
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204/* list heads for garbage collection */
205static dict_T *first_dict = NULL; /* list of all dicts */
206static list_T *first_list = NULL; /* list of all lists */
207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000208/* From user function to hashitem and back. */
209static ufunc_T dumuf;
210#define UF2HIKEY(fp) ((fp)->uf_name)
211#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
212#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
213
214#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
215#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
218#define VAR_SHORT_LEN 20 /* short variable name length */
219#define FIXVAR_CNT 12 /* number of fixed variables */
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000222typedef struct funccall_S funccall_T;
223
224struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 ufunc_T *func; /* function being called */
227 int linenr; /* next line to be executed */
228 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000229 struct /* fixed variables for arguments */
230 {
231 dictitem_T var; /* variable (without room for name) */
232 char_u room[VAR_SHORT_LEN]; /* room for the name */
233 } fixvar[FIXVAR_CNT];
234 dict_T l_vars; /* l: local function variables */
235 dictitem_T l_vars_var; /* variable for l: scope */
236 dict_T l_avars; /* a: argument variables */
237 dictitem_T l_avars_var; /* variable for a: scope */
238 list_T l_varlist; /* list for a:000 */
239 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
240 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 linenr_T breakpoint; /* next line with breakpoint or zero */
242 int dbg_tick; /* debug_tick when breakpoint was set */
243 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000244#ifdef FEAT_PROFILE
245 proftime_T prof_child; /* time spent in a child */
246#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000247 funccall_T *caller; /* calling function or NULL */
248};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251 * Info used by a ":for" loop.
252 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000253typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254{
255 int fi_semicolon; /* TRUE if ending in '; var]' */
256 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257 listwatch_T fi_lw; /* keep an eye on the item used. */
258 list_T *fi_list; /* list being used */
259} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000262 * Struct used by trans_function_name()
263 */
264typedef struct
265{
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000267 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 dictitem_T *fd_di; /* Dictionary item used */
269} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270
Bram Moolenaara7043832005-01-21 11:56:39 +0000271
272/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 * Array to hold the value of v: variables.
274 * The value is in a dictitem, so that it can also be used in the v: scope.
275 * The reason to use this table anyway is for very quick access to the
276 * variables with the VV_ defines.
277 */
278#include "version.h"
279
280/* values for vv_flags: */
281#define VV_COMPAT 1 /* compatible, also used without "v:" */
282#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000283#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000286
287static struct vimvar
288{
289 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 dictitem_T vv_di; /* value and name for key */
291 char vv_filler[16]; /* space for LONGEST name below!!! */
292 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
293} vimvars[VV_LEN] =
294{
295 /*
296 * The order here must match the VV_ defines in vim.h!
297 * Initializing a union does not work, leave tv.vval empty to get zero's.
298 */
299 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
300 {VV_NAME("count1", VAR_NUMBER), VV_RO},
301 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
302 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
303 {VV_NAME("warningmsg", VAR_STRING), 0},
304 {VV_NAME("statusmsg", VAR_STRING), 0},
305 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
307 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
309 {VV_NAME("termresponse", VAR_STRING), VV_RO},
310 {VV_NAME("fname", VAR_STRING), VV_RO},
311 {VV_NAME("lang", VAR_STRING), VV_RO},
312 {VV_NAME("lc_time", VAR_STRING), VV_RO},
313 {VV_NAME("ctype", VAR_STRING), VV_RO},
314 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
315 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
316 {VV_NAME("fname_in", VAR_STRING), VV_RO},
317 {VV_NAME("fname_out", VAR_STRING), VV_RO},
318 {VV_NAME("fname_new", VAR_STRING), VV_RO},
319 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
320 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
321 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
324 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("progname", VAR_STRING), VV_RO},
326 {VV_NAME("servername", VAR_STRING), VV_RO},
327 {VV_NAME("dying", VAR_NUMBER), VV_RO},
328 {VV_NAME("exception", VAR_STRING), VV_RO},
329 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
330 {VV_NAME("register", VAR_STRING), VV_RO},
331 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
332 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000333 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
334 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000335 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000336 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
337 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000338 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
341 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000343 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000344 {VV_NAME("swapname", VAR_STRING), VV_RO},
345 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000346 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000347 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
438static void dict_free __ARGS((dict_T *d));
439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
456static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000557static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585#ifdef vim_mkdir
586static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000591static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000593static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000594static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000597static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000610static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000612static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000619static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000620static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000621static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000623static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
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 Moolenaar4e957af2006-09-02 11:41:07 +0000704static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000705static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000707static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
709static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
710static int eval_fname_script __ARGS((char_u *p));
711static int eval_fname_sid __ARGS((char_u *p));
712static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static ufunc_T *find_func __ARGS((char_u *name));
714static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000715static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000716#ifdef FEAT_PROFILE
717static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000718static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
719static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
720static int
721# ifdef __BORLANDC__
722 _RTLENTRYF
723# endif
724 prof_total_cmp __ARGS((const void *s1, const void *s2));
725static int
726# ifdef __BORLANDC__
727 _RTLENTRYF
728# endif
729 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000730#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000731static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000732static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000733static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void func_free __ARGS((ufunc_T *fp));
735static void func_unref __ARGS((char_u *name));
736static void func_ref __ARGS((char_u *name));
737static 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));
738static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000739static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
740static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000741static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000742static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000743static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000745/* Character used as separated in autoload function/variable names. */
746#define AUTOLOAD_CHAR '#'
747
Bram Moolenaar33570922005-01-25 22:26:29 +0000748/*
749 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000750 */
751 void
752eval_init()
753{
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 int i;
755 struct vimvar *p;
756
757 init_var_dict(&globvardict, &globvars_var);
758 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000759 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000760 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761
762 for (i = 0; i < VV_LEN; ++i)
763 {
764 p = &vimvars[i];
765 STRCPY(p->vv_di.di_key, p->vv_name);
766 if (p->vv_flags & VV_RO)
767 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
768 else if (p->vv_flags & VV_RO_SBX)
769 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
770 else
771 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772
773 /* add to v: scope dict, unless the value is not always available */
774 if (p->vv_type != VAR_UNKNOWN)
775 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000777 /* add to compat scope dict */
778 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000780}
781
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782#if defined(EXITFREE) || defined(PROTO)
783 void
784eval_clear()
785{
786 int i;
787 struct vimvar *p;
788
789 for (i = 0; i < VV_LEN; ++i)
790 {
791 p = &vimvars[i];
792 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000793 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000794 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000795 p->vv_di.di_tv.vval.v_string = NULL;
796 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000797 }
798 hash_clear(&vimvarht);
799 hash_clear(&compat_hashtab);
800
801 /* script-local variables */
802 for (i = 1; i <= ga_scripts.ga_len; ++i)
803 vars_clear(&SCRIPT_VARS(i));
804 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000805 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000806
807 /* global variables */
808 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000810 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000811 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000812 hash_clear(&func_hashtab);
813
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000814 /* autoloaded script names */
815 ga_clear_strings(&ga_loaded);
816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* unreferenced lists and dicts */
818 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000819}
820#endif
821
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Return the name of the executed function.
824 */
825 char_u *
826func_name(cookie)
827 void *cookie;
828{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000829 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
832/*
833 * Return the address holding the next breakpoint line for a funccall cookie.
834 */
835 linenr_T *
836func_breakpoint(cookie)
837 void *cookie;
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
842/*
843 * Return the address holding the debug tick for a funccall cookie.
844 */
845 int *
846func_dbg_tick(cookie)
847 void *cookie;
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
853 * Return the nesting level for a funccall cookie.
854 */
855 int
856func_level(cookie)
857 void *cookie;
858{
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
862/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000863funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865/*
866 * Return TRUE when a function was ended by a ":return" command.
867 */
868 int
869current_func_returned()
870{
871 return current_funccal->returned;
872}
873
874
875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Set an internal variable to a string value. Creates the variable if it does
877 * not already exist.
878 */
879 void
880set_internal_string_var(name, value)
881 char_u *name;
882 char_u *value;
883{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000884 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 val = vim_strsave(value);
888 if (val != NULL)
889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 }
897}
898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000899static lval_T *redir_lval = NULL;
900static char_u *redir_endp = NULL;
901static char_u *redir_varname = NULL;
902
903/*
904 * Start recording command output to a variable
905 * Returns OK if successfully completed the setup. FAIL otherwise.
906 */
907 int
908var_redir_start(name, append)
909 char_u *name;
910 int append; /* append to an existing variable */
911{
912 int save_emsg;
913 int err;
914 typval_T tv;
915
916 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000917 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000918 {
919 EMSG(_(e_invarg));
920 return FAIL;
921 }
922
923 redir_varname = vim_strsave(name);
924 if (redir_varname == NULL)
925 return FAIL;
926
927 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
928 if (redir_lval == NULL)
929 {
930 var_redir_stop();
931 return FAIL;
932 }
933
934 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000935 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
936 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000937 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
938 {
939 if (redir_endp != NULL && *redir_endp != NUL)
940 /* Trailing characters are present after the variable name */
941 EMSG(_(e_trailing));
942 else
943 EMSG(_(e_invarg));
944 var_redir_stop();
945 return FAIL;
946 }
947
948 /* check if we can write to the variable: set it to or append an empty
949 * string */
950 save_emsg = did_emsg;
951 did_emsg = FALSE;
952 tv.v_type = VAR_STRING;
953 tv.vval.v_string = (char_u *)"";
954 if (append)
955 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
956 else
957 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
958 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000959 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 if (err)
961 {
962 var_redir_stop();
963 return FAIL;
964 }
965 if (redir_lval->ll_newkey != NULL)
966 {
967 /* Dictionary item was created, don't do it again. */
968 vim_free(redir_lval->ll_newkey);
969 redir_lval->ll_newkey = NULL;
970 }
971
972 return OK;
973}
974
975/*
976 * Append "value[len]" to the variable set by var_redir_start().
977 */
978 void
979var_redir_str(value, len)
980 char_u *value;
981 int len;
982{
983 char_u *val;
984 typval_T tv;
985 int save_emsg;
986 int err;
987
988 if (redir_lval == NULL)
989 return;
990
991 if (len == -1)
992 /* Append the entire string */
993 val = vim_strsave(value);
994 else
995 /* Append only the specified number of characters */
996 val = vim_strnsave(value, len);
997 if (val == NULL)
998 return;
999
1000 tv.v_type = VAR_STRING;
1001 tv.vval.v_string = val;
1002
1003 save_emsg = did_emsg;
1004 did_emsg = FALSE;
1005 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1006 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001007 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 if (err)
1009 var_redir_stop();
1010
1011 vim_free(tv.vval.v_string);
1012}
1013
1014/*
1015 * Stop redirecting command output to a variable.
1016 */
1017 void
1018var_redir_stop()
1019{
1020 if (redir_lval != NULL)
1021 {
1022 clear_lval(redir_lval);
1023 vim_free(redir_lval);
1024 redir_lval = NULL;
1025 }
1026 vim_free(redir_varname);
1027 redir_varname = NULL;
1028}
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030# if defined(FEAT_MBYTE) || defined(PROTO)
1031 int
1032eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1033 char_u *enc_from;
1034 char_u *enc_to;
1035 char_u *fname_from;
1036 char_u *fname_to;
1037{
1038 int err = FALSE;
1039
1040 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1041 set_vim_var_string(VV_CC_TO, enc_to, -1);
1042 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1043 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1044 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1045 err = TRUE;
1046 set_vim_var_string(VV_CC_FROM, NULL, -1);
1047 set_vim_var_string(VV_CC_TO, NULL, -1);
1048 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1049 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1050
1051 if (err)
1052 return FAIL;
1053 return OK;
1054}
1055# endif
1056
1057# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1058 int
1059eval_printexpr(fname, args)
1060 char_u *fname;
1061 char_u *args;
1062{
1063 int err = FALSE;
1064
1065 set_vim_var_string(VV_FNAME_IN, fname, -1);
1066 set_vim_var_string(VV_CMDARG, args, -1);
1067 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1068 err = TRUE;
1069 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1070 set_vim_var_string(VV_CMDARG, NULL, -1);
1071
1072 if (err)
1073 {
1074 mch_remove(fname);
1075 return FAIL;
1076 }
1077 return OK;
1078}
1079# endif
1080
1081# if defined(FEAT_DIFF) || defined(PROTO)
1082 void
1083eval_diff(origfile, newfile, outfile)
1084 char_u *origfile;
1085 char_u *newfile;
1086 char_u *outfile;
1087{
1088 int err = FALSE;
1089
1090 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1091 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1092 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1093 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1094 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1095 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1096 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1097}
1098
1099 void
1100eval_patch(origfile, difffile, outfile)
1101 char_u *origfile;
1102 char_u *difffile;
1103 char_u *outfile;
1104{
1105 int err;
1106
1107 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1108 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1109 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1110 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1111 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1112 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1113 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1114}
1115# endif
1116
1117/*
1118 * Top level evaluation function, returning a boolean.
1119 * Sets "error" to TRUE if there was an error.
1120 * Return TRUE or FALSE.
1121 */
1122 int
1123eval_to_bool(arg, error, nextcmd, skip)
1124 char_u *arg;
1125 int *error;
1126 char_u **nextcmd;
1127 int skip; /* only parse, don't execute */
1128{
Bram Moolenaar33570922005-01-25 22:26:29 +00001129 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int retval = FALSE;
1131
1132 if (skip)
1133 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 else
1137 {
1138 *error = FALSE;
1139 if (!skip)
1140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001141 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144 }
1145 if (skip)
1146 --emsg_skip;
1147
1148 return retval;
1149}
1150
1151/*
1152 * Top level evaluation function, returning a string. If "skip" is TRUE,
1153 * only parsing to "nextcmd" is done, without reporting errors. Return
1154 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1155 */
1156 char_u *
1157eval_to_string_skip(arg, nextcmd, skip)
1158 char_u *arg;
1159 char_u **nextcmd;
1160 int skip; /* only parse, don't execute */
1161{
Bram Moolenaar33570922005-01-25 22:26:29 +00001162 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 char_u *retval;
1164
1165 if (skip)
1166 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 retval = NULL;
1169 else
1170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 retval = vim_strsave(get_tv_string(&tv));
1172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 if (skip)
1175 --emsg_skip;
1176
1177 return retval;
1178}
1179
1180/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001181 * Skip over an expression at "*pp".
1182 * Return FAIL for an error, OK otherwise.
1183 */
1184 int
1185skip_expr(pp)
1186 char_u **pp;
1187{
Bram Moolenaar33570922005-01-25 22:26:29 +00001188 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001189
1190 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001191 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001192}
1193
1194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 * Top level evaluation function, returning a string.
1196 * Return pointer to allocated memory, or NULL for failure.
1197 */
1198 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001199eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 char_u *arg;
1201 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001202 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001206 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 retval = NULL;
1210 else
1211 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001212 if (dolist && tv.v_type == VAR_LIST)
1213 {
1214 ga_init2(&ga, (int)sizeof(char), 80);
1215 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1216 ga_append(&ga, NUL);
1217 retval = (char_u *)ga.ga_data;
1218 }
1219 else
1220 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223
1224 return retval;
1225}
1226
1227/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 * Call eval_to_string() without using current local variables and using
1229 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
1231 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001232eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 char_u *arg;
1234 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001235 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
1237 char_u *retval;
1238 void *save_funccalp;
1239
1240 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001241 if (use_sandbox)
1242 ++sandbox;
1243 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001244 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001245 if (use_sandbox)
1246 --sandbox;
1247 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 restore_funccal(save_funccalp);
1249 return retval;
1250}
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252/*
1253 * Top level evaluation function, returning a number.
1254 * Evaluates "expr" silently.
1255 * Returns -1 for an error.
1256 */
1257 int
1258eval_to_number(expr)
1259 char_u *expr;
1260{
Bram Moolenaar33570922005-01-25 22:26:29 +00001261 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001263 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 ++emsg_off;
1266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 retval = -1;
1269 else
1270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001271 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 --emsg_off;
1275
1276 return retval;
1277}
1278
Bram Moolenaara40058a2005-07-11 22:42:07 +00001279/*
1280 * Prepare v: variable "idx" to be used.
1281 * Save the current typeval in "save_tv".
1282 * When not used yet add the variable to the v: hashtable.
1283 */
1284 static void
1285prepare_vimvar(idx, save_tv)
1286 int idx;
1287 typval_T *save_tv;
1288{
1289 *save_tv = vimvars[idx].vv_tv;
1290 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1291 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1292}
1293
1294/*
1295 * Restore v: variable "idx" to typeval "save_tv".
1296 * When no longer defined, remove the variable from the v: hashtable.
1297 */
1298 static void
1299restore_vimvar(idx, save_tv)
1300 int idx;
1301 typval_T *save_tv;
1302{
1303 hashitem_T *hi;
1304
1305 clear_tv(&vimvars[idx].vv_tv);
1306 vimvars[idx].vv_tv = *save_tv;
1307 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1308 {
1309 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1310 if (HASHITEM_EMPTY(hi))
1311 EMSG2(_(e_intern2), "restore_vimvar()");
1312 else
1313 hash_remove(&vimvarht, hi);
1314 }
1315}
1316
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001317#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001318/*
1319 * Evaluate an expression to a list with suggestions.
1320 * For the "expr:" part of 'spellsuggest'.
1321 */
1322 list_T *
1323eval_spell_expr(badword, expr)
1324 char_u *badword;
1325 char_u *expr;
1326{
1327 typval_T save_val;
1328 typval_T rettv;
1329 list_T *list = NULL;
1330 char_u *p = skipwhite(expr);
1331
1332 /* Set "v:val" to the bad word. */
1333 prepare_vimvar(VV_VAL, &save_val);
1334 vimvars[VV_VAL].vv_type = VAR_STRING;
1335 vimvars[VV_VAL].vv_str = badword;
1336 if (p_verbose == 0)
1337 ++emsg_off;
1338
1339 if (eval1(&p, &rettv, TRUE) == OK)
1340 {
1341 if (rettv.v_type != VAR_LIST)
1342 clear_tv(&rettv);
1343 else
1344 list = rettv.vval.v_list;
1345 }
1346
1347 if (p_verbose == 0)
1348 --emsg_off;
1349 vimvars[VV_VAL].vv_str = NULL;
1350 restore_vimvar(VV_VAL, &save_val);
1351
1352 return list;
1353}
1354
1355/*
1356 * "list" is supposed to contain two items: a word and a number. Return the
1357 * word in "pp" and the number as the return value.
1358 * Return -1 if anything isn't right.
1359 * Used to get the good word and score from the eval_spell_expr() result.
1360 */
1361 int
1362get_spellword(list, pp)
1363 list_T *list;
1364 char_u **pp;
1365{
1366 listitem_T *li;
1367
1368 li = list->lv_first;
1369 if (li == NULL)
1370 return -1;
1371 *pp = get_tv_string(&li->li_tv);
1372
1373 li = li->li_next;
1374 if (li == NULL)
1375 return -1;
1376 return get_tv_number(&li->li_tv);
1377}
1378#endif
1379
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001380/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001381 * Top level evaluation function.
1382 * Returns an allocated typval_T with the result.
1383 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001384 */
1385 typval_T *
1386eval_expr(arg, nextcmd)
1387 char_u *arg;
1388 char_u **nextcmd;
1389{
1390 typval_T *tv;
1391
1392 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001393 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001394 {
1395 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001396 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001397 }
1398
1399 return tv;
1400}
1401
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1404/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 static int
1410call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 char_u *func;
1412 int argc;
1413 char_u **argv;
1414 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 long n;
1419 int len;
1420 int i;
1421 int doesrange;
1422 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001425 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
1429 for (i = 0; i < argc; i++)
1430 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001431 /* Pass a NULL or empty argument as an empty string */
1432 if (argv[i] == NULL || *argv[i] == NUL)
1433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001434 argvars[i].v_type = VAR_STRING;
1435 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001436 continue;
1437 }
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 /* Recognize a number argument, the others must be strings. */
1440 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1441 if (len != 0 && len == (int)STRLEN(argv[i]))
1442 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001443 argvars[i].v_type = VAR_NUMBER;
1444 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 else
1447 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001448 argvars[i].v_type = VAR_STRING;
1449 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451 }
1452
1453 if (safe)
1454 {
1455 save_funccalp = save_funccal();
1456 ++sandbox;
1457 }
1458
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1460 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001462 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (safe)
1464 {
1465 --sandbox;
1466 restore_funccal(save_funccalp);
1467 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001468 vim_free(argvars);
1469
1470 if (ret == FAIL)
1471 clear_tv(rettv);
1472
1473 return ret;
1474}
1475
1476/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001477 * Call vimL function "func" and return the result as a string.
1478 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 * Uses argv[argc] for the function arguments.
1480 */
1481 void *
1482call_func_retstr(func, argc, argv, safe)
1483 char_u *func;
1484 int argc;
1485 char_u **argv;
1486 int safe; /* use the sandbox */
1487{
1488 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001489 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490
1491 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1492 return NULL;
1493
1494 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return retval;
1497}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001498
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001499#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001501 * Call vimL function "func" and return the result as a number.
1502 * Returns -1 when calling the function fails.
1503 * Uses argv[argc] for the function arguments.
1504 */
1505 long
1506call_func_retnr(func, argc, argv, safe)
1507 char_u *func;
1508 int argc;
1509 char_u **argv;
1510 int safe; /* use the sandbox */
1511{
1512 typval_T rettv;
1513 long retval;
1514
1515 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1516 return -1;
1517
1518 retval = get_tv_number_chk(&rettv, NULL);
1519 clear_tv(&rettv);
1520 return retval;
1521}
1522#endif
1523
1524/*
1525 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001526 * Uses argv[argc] for the function arguments.
1527 */
1528 void *
1529call_func_retlist(func, argc, argv, safe)
1530 char_u *func;
1531 int argc;
1532 char_u **argv;
1533 int safe; /* use the sandbox */
1534{
1535 typval_T rettv;
1536
1537 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1538 return NULL;
1539
1540 if (rettv.v_type != VAR_LIST)
1541 {
1542 clear_tv(&rettv);
1543 return NULL;
1544 }
1545
1546 return rettv.vval.v_list;
1547}
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
1550
1551/*
1552 * Save the current function call pointer, and set it to NULL.
1553 * Used when executing autocommands and for ":source".
1554 */
1555 void *
1556save_funccal()
1557{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001558 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 current_funccal = NULL;
1561 return (void *)fc;
1562}
1563
1564 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001565restore_funccal(vfc)
1566 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001568 funccall_T *fc = (funccall_T *)vfc;
1569
1570 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571}
1572
Bram Moolenaar05159a02005-02-26 23:04:13 +00001573#if defined(FEAT_PROFILE) || defined(PROTO)
1574/*
1575 * Prepare profiling for entering a child or something else that is not
1576 * counted for the script/function itself.
1577 * Should always be called in pair with prof_child_exit().
1578 */
1579 void
1580prof_child_enter(tm)
1581 proftime_T *tm; /* place to store waittime */
1582{
1583 funccall_T *fc = current_funccal;
1584
1585 if (fc != NULL && fc->func->uf_profiling)
1586 profile_start(&fc->prof_child);
1587 script_prof_save(tm);
1588}
1589
1590/*
1591 * Take care of time spent in a child.
1592 * Should always be called after prof_child_enter().
1593 */
1594 void
1595prof_child_exit(tm)
1596 proftime_T *tm; /* where waittime was stored */
1597{
1598 funccall_T *fc = current_funccal;
1599
1600 if (fc != NULL && fc->func->uf_profiling)
1601 {
1602 profile_end(&fc->prof_child);
1603 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1604 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1605 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1606 }
1607 script_prof_restore(tm);
1608}
1609#endif
1610
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#ifdef FEAT_FOLDING
1613/*
1614 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1615 * it in "*cp". Doesn't give error messages.
1616 */
1617 int
1618eval_foldexpr(arg, cp)
1619 char_u *arg;
1620 int *cp;
1621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 int retval;
1624 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001625 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1626 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001629 if (use_sandbox)
1630 ++sandbox;
1631 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 retval = 0;
1635 else
1636 {
1637 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 if (tv.v_type == VAR_NUMBER)
1639 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001640 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 retval = 0;
1642 else
1643 {
1644 /* If the result is a string, check if there is a non-digit before
1645 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (!VIM_ISDIGIT(*s) && *s != '-')
1648 *cp = *s++;
1649 retval = atol((char *)s);
1650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 }
1653 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001654 if (use_sandbox)
1655 --sandbox;
1656 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 return retval;
1659}
1660#endif
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 * ":let" list all variable values
1664 * ":let var1 var2" list variable values
1665 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 * ":let var += expr" assignment command.
1667 * ":let var -= expr" assignment command.
1668 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
1671 void
1672ex_let(eap)
1673 exarg_T *eap;
1674{
1675 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 int var_count = 0;
1680 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001682 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaardb552d602006-03-23 22:59:57 +00001684 argend = skip_var_list(arg, &var_count, &semicolon);
1685 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001687 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1688 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001689 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001692 /*
1693 * ":let" without "=": list variables
1694 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 if (*arg == '[')
1696 EMSG(_(e_invarg));
1697 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 /* ":let var1 var2" */
1699 arg = list_arg_vars(eap, arg);
1700 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001703 list_glob_vars();
1704 list_buf_vars();
1705 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001706#ifdef FEAT_WINDOWS
1707 list_tab_vars();
1708#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001709 list_script_vars();
1710 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001711 list_vim_vars();
1712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 eap->nextcmd = check_nextcmd(arg);
1714 }
1715 else
1716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 op[0] = '=';
1718 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001719 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 {
1721 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1722 op[0] = expr[-1]; /* +=, -= or .= */
1723 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (eap->skip)
1727 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if (eap->skip)
1730 {
1731 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 --emsg_skip;
1734 }
1735 else if (i != FAIL)
1736 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741 }
1742}
1743
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744/*
1745 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1746 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001747 * When "nextchars" is not NULL it points to a string with characters that
1748 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1749 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 * Returns OK or FAIL;
1751 */
1752 static int
1753ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1754 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001755 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 int copy; /* copy values from "tv", don't move */
1757 int semicolon; /* from skip_var_list() */
1758 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760{
1761 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 listitem_T *item;
1765 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766
1767 if (*arg != '[')
1768 {
1769 /*
1770 * ":let var = expr" or ":for var in list"
1771 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 return FAIL;
1774 return OK;
1775 }
1776
1777 /*
1778 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1779 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001780 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 {
1782 EMSG(_(e_listreq));
1783 return FAIL;
1784 }
1785
1786 i = list_len(l);
1787 if (semicolon == 0 && var_count < i)
1788 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001789 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 return FAIL;
1791 }
1792 if (var_count - semicolon > i)
1793 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001794 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 return FAIL;
1796 }
1797
1798 item = l->lv_first;
1799 while (*arg != ']')
1800 {
1801 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001802 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 item = item->li_next;
1804 if (arg == NULL)
1805 return FAIL;
1806
1807 arg = skipwhite(arg);
1808 if (*arg == ';')
1809 {
1810 /* Put the rest of the list (may be empty) in the var after ';'.
1811 * Create a new list for this. */
1812 l = list_alloc();
1813 if (l == NULL)
1814 return FAIL;
1815 while (item != NULL)
1816 {
1817 list_append_tv(l, &item->li_tv);
1818 item = item->li_next;
1819 }
1820
1821 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001822 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 ltv.vval.v_list = l;
1824 l->lv_refcount = 1;
1825
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1827 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 clear_tv(&ltv);
1829 if (arg == NULL)
1830 return FAIL;
1831 break;
1832 }
1833 else if (*arg != ',' && *arg != ']')
1834 {
1835 EMSG2(_(e_intern2), "ex_let_vars()");
1836 return FAIL;
1837 }
1838 }
1839
1840 return OK;
1841}
1842
1843/*
1844 * Skip over assignable variable "var" or list of variables "[var, var]".
1845 * Used for ":let varvar = expr" and ":for varvar in expr".
1846 * For "[var, var]" increment "*var_count" for each variable.
1847 * for "[var, var; var]" set "semicolon".
1848 * Return NULL for an error.
1849 */
1850 static char_u *
1851skip_var_list(arg, var_count, semicolon)
1852 char_u *arg;
1853 int *var_count;
1854 int *semicolon;
1855{
1856 char_u *p, *s;
1857
1858 if (*arg == '[')
1859 {
1860 /* "[var, var]": find the matching ']'. */
1861 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001862 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 {
1864 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1865 s = skip_var_one(p);
1866 if (s == p)
1867 {
1868 EMSG2(_(e_invarg2), p);
1869 return NULL;
1870 }
1871 ++*var_count;
1872
1873 p = skipwhite(s);
1874 if (*p == ']')
1875 break;
1876 else if (*p == ';')
1877 {
1878 if (*semicolon == 1)
1879 {
1880 EMSG(_("Double ; in list of variables"));
1881 return NULL;
1882 }
1883 *semicolon = 1;
1884 }
1885 else if (*p != ',')
1886 {
1887 EMSG2(_(e_invarg2), p);
1888 return NULL;
1889 }
1890 }
1891 return p + 1;
1892 }
1893 else
1894 return skip_var_one(arg);
1895}
1896
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001897/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001898 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1899 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001900 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 static char_u *
1902skip_var_one(arg)
1903 char_u *arg;
1904{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001905 if (*arg == '@' && arg[1] != NUL)
1906 return arg + 2;
1907 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1908 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909}
1910
Bram Moolenaara7043832005-01-21 11:56:39 +00001911/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 * List variables for hashtab "ht" with prefix "prefix".
1913 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001916list_hashtable_vars(ht, prefix, empty)
1917 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001918 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001920{
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 hashitem_T *hi;
1922 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001923 int todo;
1924
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001925 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1927 {
1928 if (!HASHITEM_EMPTY(hi))
1929 {
1930 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 di = HI2DI(hi);
1932 if (empty || di->di_tv.v_type != VAR_STRING
1933 || di->di_tv.vval.v_string != NULL)
1934 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 }
1936 }
1937}
1938
1939/*
1940 * List global variables.
1941 */
1942 static void
1943list_glob_vars()
1944{
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001946}
1947
1948/*
1949 * List buffer variables.
1950 */
1951 static void
1952list_buf_vars()
1953{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001954 char_u numbuf[NUMBUFLEN];
1955
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001957
1958 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1959 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001960}
1961
1962/*
1963 * List window variables.
1964 */
1965 static void
1966list_win_vars()
1967{
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969}
1970
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001971#ifdef FEAT_WINDOWS
1972/*
1973 * List tab page variables.
1974 */
1975 static void
1976list_tab_vars()
1977{
1978 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1979}
1980#endif
1981
Bram Moolenaara7043832005-01-21 11:56:39 +00001982/*
1983 * List Vim variables.
1984 */
1985 static void
1986list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987{
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989}
1990
1991/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001992 * List script-local variables, if there is a script.
1993 */
1994 static void
1995list_script_vars()
1996{
1997 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1998 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1999}
2000
2001/*
2002 * List function variables, if there is a function.
2003 */
2004 static void
2005list_func_vars()
2006{
2007 if (current_funccal != NULL)
2008 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2009 (char_u *)"l:", FALSE);
2010}
2011
2012/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 * List variables in "arg".
2014 */
2015 static char_u *
2016list_arg_vars(eap, arg)
2017 exarg_T *eap;
2018 char_u *arg;
2019{
2020 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002021 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002023 char_u *name_start;
2024 char_u *arg_subsc;
2025 char_u *tofree;
2026 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027
2028 while (!ends_excmd(*arg) && !got_int)
2029 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002030 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002032 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2034 {
2035 emsg_severe = TRUE;
2036 EMSG(_(e_trailing));
2037 break;
2038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 /* get_name_len() takes care of expanding curly braces */
2043 name_start = name = arg;
2044 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2045 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002047 /* This is mainly to keep test 49 working: when expanding
2048 * curly braces fails overrule the exception error message. */
2049 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002051 emsg_severe = TRUE;
2052 EMSG2(_(e_invarg2), arg);
2053 break;
2054 }
2055 error = TRUE;
2056 }
2057 else
2058 {
2059 if (tofree != NULL)
2060 name = tofree;
2061 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 else
2064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 /* handle d.key, l[idx], f(expr) */
2066 arg_subsc = arg;
2067 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002068 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002072 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 case 'g': list_glob_vars(); break;
2076 case 'b': list_buf_vars(); break;
2077 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002078#ifdef FEAT_WINDOWS
2079 case 't': list_tab_vars(); break;
2080#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002082 case 's': list_script_vars(); break;
2083 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 default:
2085 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
2088 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002089 {
2090 char_u numbuf[NUMBUFLEN];
2091 char_u *tf;
2092 int c;
2093 char_u *s;
2094
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002095 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096 c = *arg;
2097 *arg = NUL;
2098 list_one_var_a((char_u *)"",
2099 arg == arg_subsc ? name : name_start,
2100 tv.v_type, s == NULL ? (char_u *)"" : s);
2101 *arg = c;
2102 vim_free(tf);
2103 }
2104 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 }
2107 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108
2109 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002111
2112 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 }
2114
2115 return arg;
2116}
2117
2118/*
2119 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2120 * Returns a pointer to the char just after the var name.
2121 * Returns NULL if there is an error.
2122 */
2123 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002124ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002129 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130{
2131 int c1;
2132 char_u *name;
2133 char_u *p;
2134 char_u *arg_end = NULL;
2135 int len;
2136 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002137 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138
2139 /*
2140 * ":let $VAR = expr": Set environment variable.
2141 */
2142 if (*arg == '$')
2143 {
2144 /* Find the end of the name. */
2145 ++arg;
2146 name = arg;
2147 len = get_env_len(&arg);
2148 if (len == 0)
2149 EMSG2(_(e_invarg2), name - 1);
2150 else
2151 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 if (op != NULL && (*op == '+' || *op == '-'))
2153 EMSG2(_(e_letwrong), op);
2154 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 EMSG(_(e_letunexp));
2157 else
2158 {
2159 c1 = name[len];
2160 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002161 p = get_tv_string_chk(tv);
2162 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 {
2164 int mustfree = FALSE;
2165 char_u *s = vim_getenv(name, &mustfree);
2166
2167 if (s != NULL)
2168 {
2169 p = tofree = concat_str(s, p);
2170 if (mustfree)
2171 vim_free(s);
2172 }
2173 }
2174 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002177 if (STRICMP(name, "HOME") == 0)
2178 init_homedir();
2179 else if (didset_vim && STRICMP(name, "VIM") == 0)
2180 didset_vim = FALSE;
2181 else if (didset_vimruntime
2182 && STRICMP(name, "VIMRUNTIME") == 0)
2183 didset_vimruntime = FALSE;
2184 arg_end = arg;
2185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 }
2189 }
2190 }
2191
2192 /*
2193 * ":let &option = expr": Set option value.
2194 * ":let &l:option = expr": Set local option value.
2195 * ":let &g:option = expr": Set global option value.
2196 */
2197 else if (*arg == '&')
2198 {
2199 /* Find the end of the name. */
2200 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002201 if (p == NULL || (endchars != NULL
2202 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 EMSG(_(e_letunexp));
2204 else
2205 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 long n;
2207 int opt_type;
2208 long numval;
2209 char_u *stringval = NULL;
2210 char_u *s;
2211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 c1 = *p;
2213 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002214
2215 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 s = get_tv_string_chk(tv); /* != NULL if number or string */
2217 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002218 {
2219 opt_type = get_option_value(arg, &numval,
2220 &stringval, opt_flags);
2221 if ((opt_type == 1 && *op == '.')
2222 || (opt_type == 0 && *op != '.'))
2223 EMSG2(_(e_letwrong), op);
2224 else
2225 {
2226 if (opt_type == 1) /* number */
2227 {
2228 if (*op == '+')
2229 n = numval + n;
2230 else
2231 n = numval - n;
2232 }
2233 else if (opt_type == 0 && stringval != NULL) /* string */
2234 {
2235 s = concat_str(stringval, s);
2236 vim_free(stringval);
2237 stringval = s;
2238 }
2239 }
2240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002241 if (s != NULL)
2242 {
2243 set_option_value(arg, n, s, opt_flags);
2244 arg_end = p;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 }
2249 }
2250
2251 /*
2252 * ":let @r = expr": Set register contents.
2253 */
2254 else if (*arg == '@')
2255 {
2256 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 if (op != NULL && (*op == '+' || *op == '-'))
2258 EMSG2(_(e_letwrong), op);
2259 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002260 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 EMSG(_(e_letunexp));
2262 else
2263 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002264 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265 char_u *s;
2266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 p = get_tv_string_chk(tv);
2268 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002270 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002271 if (s != NULL)
2272 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002273 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002274 vim_free(s);
2275 }
2276 }
2277 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002279 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 arg_end = arg + 1;
2281 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002282 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
2285
2286 /*
2287 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002290 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002292 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002294 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2298 EMSG(_(e_letunexp));
2299 else
2300 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 arg_end = p;
2303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 }
2307
2308 else
2309 EMSG2(_(e_invarg2), arg);
2310
2311 return arg_end;
2312}
2313
2314/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002315 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2316 */
2317 static int
2318check_changedtick(arg)
2319 char_u *arg;
2320{
2321 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2322 {
2323 EMSG2(_(e_readonlyvar), arg);
2324 return TRUE;
2325 }
2326 return FALSE;
2327}
2328
2329/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 * Get an lval: variable, Dict item or List item that can be assigned a value
2331 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2332 * "name.key", "name.key[expr]" etc.
2333 * Indexing only works if "name" is an existing List or Dictionary.
2334 * "name" points to the start of the name.
2335 * If "rettv" is not NULL it points to the value to be assigned.
2336 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2337 * wrong; must end in space or cmd separator.
2338 *
2339 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002340 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 */
2343 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002344get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002346 typval_T *rettv;
2347 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 int unlet;
2349 int skip;
2350 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002351 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 char_u *expr_start, *expr_end;
2355 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002356 dictitem_T *v;
2357 typval_T var1;
2358 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002360 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002363 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002366 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367
2368 if (skip)
2369 {
2370 /* When skipping just find the end of the name. */
2371 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002372 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 }
2374
2375 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002376 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (expr_start != NULL)
2378 {
2379 /* Don't expand the name when we already know there is an error. */
2380 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2381 && *p != '[' && *p != '.')
2382 {
2383 EMSG(_(e_trailing));
2384 return NULL;
2385 }
2386
2387 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2388 if (lp->ll_exp_name == NULL)
2389 {
2390 /* Report an invalid expression in braces, unless the
2391 * expression evaluation has been cancelled due to an
2392 * aborting error, an interrupt, or an exception. */
2393 if (!aborting() && !quiet)
2394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002395 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 EMSG2(_(e_invarg2), name);
2397 return NULL;
2398 }
2399 }
2400 lp->ll_name = lp->ll_exp_name;
2401 }
2402 else
2403 lp->ll_name = name;
2404
2405 /* Without [idx] or .key we are done. */
2406 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2407 return p;
2408
2409 cc = *p;
2410 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002411 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (v == NULL && !quiet)
2413 EMSG2(_(e_undefvar), lp->ll_name);
2414 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 if (v == NULL)
2416 return NULL;
2417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 /*
2419 * Loop until no more [idx] or .key is following.
2420 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002421 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2425 && !(lp->ll_tv->v_type == VAR_DICT
2426 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 if (!quiet)
2429 EMSG(_("E689: Can only index a List or Dictionary"));
2430 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!quiet)
2435 EMSG(_("E708: [:] must come last"));
2436 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002438
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 len = -1;
2440 if (*p == '.')
2441 {
2442 key = p + 1;
2443 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2444 ;
2445 if (len == 0)
2446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
2448 EMSG(_(e_emptykey));
2449 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 }
2451 p = key + len;
2452 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002453 else
2454 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002456 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (*p == ':')
2458 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002459 else
2460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 empty1 = FALSE;
2462 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (get_tv_string_chk(&var1) == NULL)
2465 {
2466 /* not a number or string */
2467 clear_tv(&var1);
2468 return NULL;
2469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 }
2471
2472 /* Optionally get the second index [ :expr]. */
2473 if (*p == ':')
2474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 if (!empty1)
2480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (rettv != NULL && (rettv->v_type != VAR_LIST
2484 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (!quiet)
2487 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 if (!empty1)
2489 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 }
2492 p = skipwhite(p + 1);
2493 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 else
2496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2499 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 if (!empty1)
2501 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (get_tv_string_chk(&var2) == NULL)
2505 {
2506 /* not a number or string */
2507 if (!empty1)
2508 clear_tv(&var1);
2509 clear_tv(&var2);
2510 return NULL;
2511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
2521 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (!empty1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
2528
2529 /* Skip to past ']'. */
2530 ++p;
2531 }
2532
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 {
2535 if (len == -1)
2536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 if (*key == NUL)
2540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (!quiet)
2542 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 }
2546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 lp->ll_list = NULL;
2548 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002549 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002552 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002556 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 if (len == -1)
2558 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 }
2561 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 if (len == -1)
2566 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 p = NULL;
2569 break;
2570 }
2571 if (len == -1)
2572 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 }
2575 else
2576 {
2577 /*
2578 * Get the number and item for the only or first index of the List.
2579 */
2580 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 else
2583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002584 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 clear_tv(&var1);
2586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 lp->ll_list = lp->ll_tv->vval.v_list;
2589 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2590 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002592 if (lp->ll_n1 < 0)
2593 {
2594 lp->ll_n1 = 0;
2595 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2596 }
2597 }
2598 if (lp->ll_li == NULL)
2599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 }
2604
2605 /*
2606 * May need to find the item or absolute index for the second
2607 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * When no index given: "lp->ll_empty2" is TRUE.
2609 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002613 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 }
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2624 if (lp->ll_n1 < 0)
2625 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2626 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 }
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 }
2633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 return p;
2635}
2636
2637/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 */
2640 static void
2641clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643{
2644 vim_free(lp->ll_exp_name);
2645 vim_free(lp->ll_newkey);
2646}
2647
2648/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002649 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 */
2653 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002655 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660{
2661 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002662 listitem_T *ri;
2663 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664
2665 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 cc = *endp;
2670 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 if (op != NULL && *op != '=')
2672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002675 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002676 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002677 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002678 {
2679 if (tv_op(&tv, rettv, op) == OK)
2680 set_var(lp->ll_name, &tv, FALSE);
2681 clear_tv(&tv);
2682 }
2683 }
2684 else
2685 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002689 else if (tv_check_lock(lp->ll_newkey == NULL
2690 ? lp->ll_tv->v_lock
2691 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2692 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 else if (lp->ll_range)
2694 {
2695 /*
2696 * Assign the List values to the list items.
2697 */
2698 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 if (op != NULL && *op != '=')
2701 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2702 else
2703 {
2704 clear_tv(&lp->ll_li->li_tv);
2705 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2706 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 ri = ri->li_next;
2708 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2709 break;
2710 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002713 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 ri = NULL;
2716 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_li = lp->ll_li->li_next;
2720 ++lp->ll_n1;
2721 }
2722 if (ri != NULL)
2723 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 else if (lp->ll_empty2
2725 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 : lp->ll_n1 != lp->ll_n2)
2727 EMSG(_("E711: List value has not enough items"));
2728 }
2729 else
2730 {
2731 /*
2732 * Assign to a List or Dictionary item.
2733 */
2734 if (lp->ll_newkey != NULL)
2735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 if (op != NULL && *op != '=')
2737 {
2738 EMSG2(_(e_letwrong), op);
2739 return;
2740 }
2741
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002743 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (di == NULL)
2745 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002746 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2747 {
2748 vim_free(di);
2749 return;
2750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 else if (op != NULL && *op != '=')
2754 {
2755 tv_op(lp->ll_tv, rettv, op);
2756 return;
2757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002758 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 /*
2762 * Assign the value to the variable or list item.
2763 */
2764 if (copy)
2765 copy_tv(rettv, lp->ll_tv);
2766 else
2767 {
2768 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002769 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002771 }
2772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773}
2774
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2777 * Returns OK or FAIL.
2778 */
2779 static int
2780tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002781 typval_T *tv1;
2782 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 char_u *op;
2784{
2785 long n;
2786 char_u numbuf[NUMBUFLEN];
2787 char_u *s;
2788
2789 /* Can't do anything with a Funcref or a Dict on the right. */
2790 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2791 {
2792 switch (tv1->v_type)
2793 {
2794 case VAR_DICT:
2795 case VAR_FUNC:
2796 break;
2797
2798 case VAR_LIST:
2799 if (*op != '+' || tv2->v_type != VAR_LIST)
2800 break;
2801 /* List += List */
2802 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2803 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2804 return OK;
2805
2806 case VAR_NUMBER:
2807 case VAR_STRING:
2808 if (tv2->v_type == VAR_LIST)
2809 break;
2810 if (*op == '+' || *op == '-')
2811 {
2812 /* nr += nr or nr -= nr*/
2813 n = get_tv_number(tv1);
2814 if (*op == '+')
2815 n += get_tv_number(tv2);
2816 else
2817 n -= get_tv_number(tv2);
2818 clear_tv(tv1);
2819 tv1->v_type = VAR_NUMBER;
2820 tv1->vval.v_number = n;
2821 }
2822 else
2823 {
2824 /* str .= str */
2825 s = get_tv_string(tv1);
2826 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2827 clear_tv(tv1);
2828 tv1->v_type = VAR_STRING;
2829 tv1->vval.v_string = s;
2830 }
2831 return OK;
2832 }
2833 }
2834
2835 EMSG2(_(e_letwrong), op);
2836 return FAIL;
2837}
2838
2839/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840 * Add a watcher to a list.
2841 */
2842 static void
2843list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 list_T *l;
2845 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846{
2847 lw->lw_next = l->lv_watch;
2848 l->lv_watch = lw;
2849}
2850
2851/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002852 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 * No warning when it isn't found...
2854 */
2855 static void
2856list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 list_T *l;
2858 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859{
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861
2862 lwp = &l->lv_watch;
2863 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2864 {
2865 if (lw == lwrem)
2866 {
2867 *lwp = lw->lw_next;
2868 break;
2869 }
2870 lwp = &lw->lw_next;
2871 }
2872}
2873
2874/*
2875 * Just before removing an item from a list: advance watchers to the next
2876 * item.
2877 */
2878 static void
2879list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 list_T *l;
2881 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882{
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884
2885 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2886 if (lw->lw_item == item)
2887 lw->lw_item = item->li_next;
2888}
2889
2890/*
2891 * Evaluate the expression used in a ":for var in expr" command.
2892 * "arg" points to "var".
2893 * Set "*errp" to TRUE for an error, FALSE otherwise;
2894 * Return a pointer that holds the info. Null when there is an error.
2895 */
2896 void *
2897eval_for_line(arg, errp, nextcmdp, skip)
2898 char_u *arg;
2899 int *errp;
2900 char_u **nextcmdp;
2901 int skip;
2902{
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 typval_T tv;
2906 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907
2908 *errp = TRUE; /* default: there is an error */
2909
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 if (fi == NULL)
2912 return NULL;
2913
2914 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2915 if (expr == NULL)
2916 return fi;
2917
2918 expr = skipwhite(expr);
2919 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2920 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002921 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 return fi;
2923 }
2924
2925 if (skip)
2926 ++emsg_skip;
2927 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2928 {
2929 *errp = FALSE;
2930 if (!skip)
2931 {
2932 l = tv.vval.v_list;
2933 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002934 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002936 clear_tv(&tv);
2937 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002938 else
2939 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002940 /* No need to increment the refcount, it's already set for the
2941 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 fi->fi_list = l;
2943 list_add_watch(l, &fi->fi_lw);
2944 fi->fi_lw.lw_item = l->lv_first;
2945 }
2946 }
2947 }
2948 if (skip)
2949 --emsg_skip;
2950
2951 return fi;
2952}
2953
2954/*
2955 * Use the first item in a ":for" list. Advance to the next.
2956 * Assign the values to the variable (list). "arg" points to the first one.
2957 * Return TRUE when a valid item was found, FALSE when at end of list or
2958 * something wrong.
2959 */
2960 int
2961next_for_item(fi_void, arg)
2962 void *fi_void;
2963 char_u *arg;
2964{
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002966 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002967 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968
2969 item = fi->fi_lw.lw_item;
2970 if (item == NULL)
2971 result = FALSE;
2972 else
2973 {
2974 fi->fi_lw.lw_item = item->li_next;
2975 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2976 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2977 }
2978 return result;
2979}
2980
2981/*
2982 * Free the structure used to store info used by ":for".
2983 */
2984 void
2985free_for_info(fi_void)
2986 void *fi_void;
2987{
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002989
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002990 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002991 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002993 list_unref(fi->fi_list);
2994 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002995 vim_free(fi);
2996}
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2999
3000 void
3001set_context_for_expression(xp, arg, cmdidx)
3002 expand_T *xp;
3003 char_u *arg;
3004 cmdidx_T cmdidx;
3005{
3006 int got_eq = FALSE;
3007 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 if (cmdidx == CMD_let)
3011 {
3012 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003013 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003014 {
3015 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003016 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003017 {
3018 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003020 if (vim_iswhite(*p))
3021 break;
3022 }
3023 return;
3024 }
3025 }
3026 else
3027 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3028 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 while ((xp->xp_pattern = vim_strpbrk(arg,
3030 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3031 {
3032 c = *xp->xp_pattern;
3033 if (c == '&')
3034 {
3035 c = xp->xp_pattern[1];
3036 if (c == '&')
3037 {
3038 ++xp->xp_pattern;
3039 xp->xp_context = cmdidx != CMD_let || got_eq
3040 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3041 }
3042 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003045 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3046 xp->xp_pattern += 2;
3047
3048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 else if (c == '$')
3051 {
3052 /* environment variable */
3053 xp->xp_context = EXPAND_ENV_VARS;
3054 }
3055 else if (c == '=')
3056 {
3057 got_eq = TRUE;
3058 xp->xp_context = EXPAND_EXPRESSION;
3059 }
3060 else if (c == '<'
3061 && xp->xp_context == EXPAND_FUNCTIONS
3062 && vim_strchr(xp->xp_pattern, '(') == NULL)
3063 {
3064 /* Function name can start with "<SNR>" */
3065 break;
3066 }
3067 else if (cmdidx != CMD_let || got_eq)
3068 {
3069 if (c == '"') /* string */
3070 {
3071 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3072 if (c == '\\' && xp->xp_pattern[1] != NUL)
3073 ++xp->xp_pattern;
3074 xp->xp_context = EXPAND_NOTHING;
3075 }
3076 else if (c == '\'') /* literal string */
3077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003078 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3080 /* skip */ ;
3081 xp->xp_context = EXPAND_NOTHING;
3082 }
3083 else if (c == '|')
3084 {
3085 if (xp->xp_pattern[1] == '|')
3086 {
3087 ++xp->xp_pattern;
3088 xp->xp_context = EXPAND_EXPRESSION;
3089 }
3090 else
3091 xp->xp_context = EXPAND_COMMANDS;
3092 }
3093 else
3094 xp->xp_context = EXPAND_EXPRESSION;
3095 }
3096 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097 /* Doesn't look like something valid, expand as an expression
3098 * anyway. */
3099 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 arg = xp->xp_pattern;
3101 if (*arg != NUL)
3102 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3103 /* skip */ ;
3104 }
3105 xp->xp_pattern = arg;
3106}
3107
3108#endif /* FEAT_CMDL_COMPL */
3109
3110/*
3111 * ":1,25call func(arg1, arg2)" function call.
3112 */
3113 void
3114ex_call(eap)
3115 exarg_T *eap;
3116{
3117 char_u *arg = eap->arg;
3118 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 linenr_T lnum;
3124 int doesrange;
3125 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003128 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3129 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 if (tofree == NULL)
3131 return;
3132
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003133 /* Increase refcount on dictionary, it could get deleted when evaluating
3134 * the arguments. */
3135 if (fudi.fd_dict != NULL)
3136 ++fudi.fd_dict->dv_refcount;
3137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003138 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003139 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaar532c7802005-01-27 14:44:31 +00003142 /* Skip white space to allow ":call func ()". Not good, but required for
3143 * backward compatibility. */
3144 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003145 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 if (*startarg != '(')
3148 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003149 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 goto end;
3151 }
3152
3153 /*
3154 * When skipping, evaluate the function once, to find the end of the
3155 * arguments.
3156 * When the function takes a range, this is discovered after the first
3157 * call, and the loop is broken.
3158 */
3159 if (eap->skip)
3160 {
3161 ++emsg_skip;
3162 lnum = eap->line2; /* do it once, also with an invalid range */
3163 }
3164 else
3165 lnum = eap->line1;
3166 for ( ; lnum <= eap->line2; ++lnum)
3167 {
3168 if (!eap->skip && eap->addr_count > 0)
3169 {
3170 curwin->w_cursor.lnum = lnum;
3171 curwin->w_cursor.col = 0;
3172 }
3173 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003174 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003175 eap->line1, eap->line2, &doesrange,
3176 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
3178 failed = TRUE;
3179 break;
3180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003181 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (doesrange || eap->skip)
3183 break;
3184 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003185 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003187 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (aborting())
3189 break;
3190 }
3191 if (eap->skip)
3192 --emsg_skip;
3193
3194 if (!failed)
3195 {
3196 /* Check for trailing illegal characters and a following command. */
3197 if (!ends_excmd(*arg))
3198 {
3199 emsg_severe = TRUE;
3200 EMSG(_(e_trailing));
3201 }
3202 else
3203 eap->nextcmd = check_nextcmd(arg);
3204 }
3205
3206end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003207 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003208 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
3210
3211/*
3212 * ":unlet[!] var1 ... " command.
3213 */
3214 void
3215ex_unlet(eap)
3216 exarg_T *eap;
3217{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 ex_unletlock(eap, eap->arg, 0);
3219}
3220
3221/*
3222 * ":lockvar" and ":unlockvar" commands
3223 */
3224 void
3225ex_lockvar(eap)
3226 exarg_T *eap;
3227{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003229 int deep = 2;
3230
3231 if (eap->forceit)
3232 deep = -1;
3233 else if (vim_isdigit(*arg))
3234 {
3235 deep = getdigits(&arg);
3236 arg = skipwhite(arg);
3237 }
3238
3239 ex_unletlock(eap, arg, deep);
3240}
3241
3242/*
3243 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3244 */
3245 static void
3246ex_unletlock(eap, argstart, deep)
3247 exarg_T *eap;
3248 char_u *argstart;
3249 int deep;
3250{
3251 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 do
3257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003259 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3260 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003261 if (lv.ll_name == NULL)
3262 error = TRUE; /* error but continue parsing */
3263 if (name_end == NULL || (!vim_iswhite(*name_end)
3264 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003266 if (name_end != NULL)
3267 {
3268 emsg_severe = TRUE;
3269 EMSG(_(e_trailing));
3270 }
3271 if (!(eap->skip || error))
3272 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 break;
3274 }
3275
3276 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 {
3278 if (eap->cmdidx == CMD_unlet)
3279 {
3280 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3281 error = TRUE;
3282 }
3283 else
3284 {
3285 if (do_lock_var(&lv, name_end, deep,
3286 eap->cmdidx == CMD_lockvar) == FAIL)
3287 error = TRUE;
3288 }
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003291 if (!eap->skip)
3292 clear_lval(&lv);
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 arg = skipwhite(name_end);
3295 } while (!ends_excmd(*arg));
3296
3297 eap->nextcmd = check_nextcmd(arg);
3298}
3299
Bram Moolenaar8c711452005-01-14 21:53:12 +00003300 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003301do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 int forceit;
3305{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 int ret = OK;
3307 int cc;
3308
3309 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 cc = *name_end;
3312 *name_end = NUL;
3313
3314 /* Normal name or expanded name. */
3315 if (check_changedtick(lp->ll_name))
3316 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003317 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003319 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3322 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003323 else if (lp->ll_range)
3324 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003326
3327 /* Delete a range of List items. */
3328 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3329 {
3330 li = lp->ll_li->li_next;
3331 listitem_remove(lp->ll_list, lp->ll_li);
3332 lp->ll_li = li;
3333 ++lp->ll_n1;
3334 }
3335 }
3336 else
3337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003338 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 /* unlet a List item. */
3340 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003343 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003344 }
3345
3346 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003347}
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349/*
3350 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003351 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003354do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003356 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 hashtab_T *ht;
3359 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003360 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 ht = find_var_ht(name, &varname);
3363 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003365 hi = hash_find(ht, varname);
3366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003367 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003368 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3369 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 if (var_check_ro(HI2DI(hi)->di_flags, name))
3371 return FAIL;
3372 delete_var(ht, hi);
3373 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003376 if (forceit)
3377 return OK;
3378 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 return FAIL;
3380}
3381
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003382/*
3383 * Lock or unlock variable indicated by "lp".
3384 * "deep" is the levels to go (-1 for unlimited);
3385 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3386 */
3387 static int
3388do_lock_var(lp, name_end, deep, lock)
3389 lval_T *lp;
3390 char_u *name_end;
3391 int deep;
3392 int lock;
3393{
3394 int ret = OK;
3395 int cc;
3396 dictitem_T *di;
3397
3398 if (deep == 0) /* nothing to do */
3399 return OK;
3400
3401 if (lp->ll_tv == NULL)
3402 {
3403 cc = *name_end;
3404 *name_end = NUL;
3405
3406 /* Normal name or expanded name. */
3407 if (check_changedtick(lp->ll_name))
3408 ret = FAIL;
3409 else
3410 {
3411 di = find_var(lp->ll_name, NULL);
3412 if (di == NULL)
3413 ret = FAIL;
3414 else
3415 {
3416 if (lock)
3417 di->di_flags |= DI_FLAGS_LOCK;
3418 else
3419 di->di_flags &= ~DI_FLAGS_LOCK;
3420 item_lock(&di->di_tv, deep, lock);
3421 }
3422 }
3423 *name_end = cc;
3424 }
3425 else if (lp->ll_range)
3426 {
3427 listitem_T *li = lp->ll_li;
3428
3429 /* (un)lock a range of List items. */
3430 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3431 {
3432 item_lock(&li->li_tv, deep, lock);
3433 li = li->li_next;
3434 ++lp->ll_n1;
3435 }
3436 }
3437 else if (lp->ll_list != NULL)
3438 /* (un)lock a List item. */
3439 item_lock(&lp->ll_li->li_tv, deep, lock);
3440 else
3441 /* un(lock) a Dictionary item. */
3442 item_lock(&lp->ll_di->di_tv, deep, lock);
3443
3444 return ret;
3445}
3446
3447/*
3448 * Lock or unlock an item. "deep" is nr of levels to go.
3449 */
3450 static void
3451item_lock(tv, deep, lock)
3452 typval_T *tv;
3453 int deep;
3454 int lock;
3455{
3456 static int recurse = 0;
3457 list_T *l;
3458 listitem_T *li;
3459 dict_T *d;
3460 hashitem_T *hi;
3461 int todo;
3462
3463 if (recurse >= DICT_MAXNEST)
3464 {
3465 EMSG(_("E743: variable nested too deep for (un)lock"));
3466 return;
3467 }
3468 if (deep == 0)
3469 return;
3470 ++recurse;
3471
3472 /* lock/unlock the item itself */
3473 if (lock)
3474 tv->v_lock |= VAR_LOCKED;
3475 else
3476 tv->v_lock &= ~VAR_LOCKED;
3477
3478 switch (tv->v_type)
3479 {
3480 case VAR_LIST:
3481 if ((l = tv->vval.v_list) != NULL)
3482 {
3483 if (lock)
3484 l->lv_lock |= VAR_LOCKED;
3485 else
3486 l->lv_lock &= ~VAR_LOCKED;
3487 if (deep < 0 || deep > 1)
3488 /* recursive: lock/unlock the items the List contains */
3489 for (li = l->lv_first; li != NULL; li = li->li_next)
3490 item_lock(&li->li_tv, deep - 1, lock);
3491 }
3492 break;
3493 case VAR_DICT:
3494 if ((d = tv->vval.v_dict) != NULL)
3495 {
3496 if (lock)
3497 d->dv_lock |= VAR_LOCKED;
3498 else
3499 d->dv_lock &= ~VAR_LOCKED;
3500 if (deep < 0 || deep > 1)
3501 {
3502 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003503 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003504 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3505 {
3506 if (!HASHITEM_EMPTY(hi))
3507 {
3508 --todo;
3509 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3510 }
3511 }
3512 }
3513 }
3514 }
3515 --recurse;
3516}
3517
Bram Moolenaara40058a2005-07-11 22:42:07 +00003518/*
3519 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3520 * it refers to a List or Dictionary that is locked.
3521 */
3522 static int
3523tv_islocked(tv)
3524 typval_T *tv;
3525{
3526 return (tv->v_lock & VAR_LOCKED)
3527 || (tv->v_type == VAR_LIST
3528 && tv->vval.v_list != NULL
3529 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3530 || (tv->v_type == VAR_DICT
3531 && tv->vval.v_dict != NULL
3532 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3533}
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3536/*
3537 * Delete all "menutrans_" variables.
3538 */
3539 void
3540del_menutrans_vars()
3541{
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar33570922005-01-25 22:26:29 +00003545 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003546 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003548 {
3549 if (!HASHITEM_EMPTY(hi))
3550 {
3551 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3553 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 }
3555 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557}
3558#endif
3559
3560#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3561
3562/*
3563 * Local string buffer for the next two functions to store a variable name
3564 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3565 * get_user_var_name().
3566 */
3567
3568static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3569
3570static char_u *varnamebuf = NULL;
3571static int varnamebuflen = 0;
3572
3573/*
3574 * Function to concatenate a prefix and a variable name.
3575 */
3576 static char_u *
3577cat_prefix_varname(prefix, name)
3578 int prefix;
3579 char_u *name;
3580{
3581 int len;
3582
3583 len = (int)STRLEN(name) + 3;
3584 if (len > varnamebuflen)
3585 {
3586 vim_free(varnamebuf);
3587 len += 10; /* some additional space */
3588 varnamebuf = alloc(len);
3589 if (varnamebuf == NULL)
3590 {
3591 varnamebuflen = 0;
3592 return NULL;
3593 }
3594 varnamebuflen = len;
3595 }
3596 *varnamebuf = prefix;
3597 varnamebuf[1] = ':';
3598 STRCPY(varnamebuf + 2, name);
3599 return varnamebuf;
3600}
3601
3602/*
3603 * Function given to ExpandGeneric() to obtain the list of user defined
3604 * (global/buffer/window/built-in) variable names.
3605 */
3606/*ARGSUSED*/
3607 char_u *
3608get_user_var_name(xp, idx)
3609 expand_T *xp;
3610 int idx;
3611{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003612 static long_u gdone;
3613 static long_u bdone;
3614 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003615#ifdef FEAT_WINDOWS
3616 static long_u tdone;
3617#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003618 static int vidx;
3619 static hashitem_T *hi;
3620 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003623 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003624 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003625#ifdef FEAT_WINDOWS
3626 tdone = 0;
3627#endif
3628 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003629
3630 /* Global variables */
3631 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003633 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003635 else
3636 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003637 while (HASHITEM_EMPTY(hi))
3638 ++hi;
3639 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3640 return cat_prefix_varname('g', hi->hi_key);
3641 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003643
3644 /* b: variables */
3645 ht = &curbuf->b_vars.dv_hashtab;
3646 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003650 else
3651 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 while (HASHITEM_EMPTY(hi))
3653 ++hi;
3654 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return (char_u *)"b:changedtick";
3660 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003661
3662 /* w: variables */
3663 ht = &curwin->w_vars.dv_hashtab;
3664 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003666 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003668 else
3669 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 while (HASHITEM_EMPTY(hi))
3671 ++hi;
3672 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003674
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675#ifdef FEAT_WINDOWS
3676 /* t: variables */
3677 ht = &curtab->tp_vars.dv_hashtab;
3678 if (tdone < ht->ht_used)
3679 {
3680 if (tdone++ == 0)
3681 hi = ht->ht_array;
3682 else
3683 ++hi;
3684 while (HASHITEM_EMPTY(hi))
3685 ++hi;
3686 return cat_prefix_varname('t', hi->hi_key);
3687 }
3688#endif
3689
Bram Moolenaar33570922005-01-25 22:26:29 +00003690 /* v: variables */
3691 if (vidx < VV_LEN)
3692 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 vim_free(varnamebuf);
3695 varnamebuf = NULL;
3696 varnamebuflen = 0;
3697 return NULL;
3698}
3699
3700#endif /* FEAT_CMDL_COMPL */
3701
3702/*
3703 * types for expressions.
3704 */
3705typedef enum
3706{
3707 TYPE_UNKNOWN = 0
3708 , TYPE_EQUAL /* == */
3709 , TYPE_NEQUAL /* != */
3710 , TYPE_GREATER /* > */
3711 , TYPE_GEQUAL /* >= */
3712 , TYPE_SMALLER /* < */
3713 , TYPE_SEQUAL /* <= */
3714 , TYPE_MATCH /* =~ */
3715 , TYPE_NOMATCH /* !~ */
3716} exptype_T;
3717
3718/*
3719 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3722 */
3723
3724/*
3725 * Handle zero level expression.
3726 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003728 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 * Return OK or FAIL.
3730 */
3731 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 char_u **nextcmd;
3736 int evaluate;
3737{
3738 int ret;
3739 char_u *p;
3740
3741 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (ret == FAIL || !ends_excmd(*p))
3744 {
3745 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /*
3748 * Report the invalid expression unless the expression evaluation has
3749 * been cancelled due to an aborting error, an interrupt, or an
3750 * exception.
3751 */
3752 if (!aborting())
3753 EMSG2(_(e_invexpr2), arg);
3754 ret = FAIL;
3755 }
3756 if (nextcmd != NULL)
3757 *nextcmd = check_nextcmd(p);
3758
3759 return ret;
3760}
3761
3762/*
3763 * Handle top level expression:
3764 * expr1 ? expr0 : expr0
3765 *
3766 * "arg" must point to the first non-white of the expression.
3767 * "arg" is advanced to the next non-white after the recognized expression.
3768 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003769 * Note: "rettv.v_lock" is not set.
3770 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 * Return OK or FAIL.
3772 */
3773 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 int evaluate;
3778{
3779 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * Get the first variable.
3784 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 if ((*arg)[0] == '?')
3789 {
3790 result = FALSE;
3791 if (evaluate)
3792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 int error = FALSE;
3794
3795 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 if (error)
3799 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801
3802 /*
3803 * Get the second variable.
3804 */
3805 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return FAIL;
3808
3809 /*
3810 * Check for the ":".
3811 */
3812 if ((*arg)[0] != ':')
3813 {
3814 EMSG(_("E109: Missing ':' after '?'"));
3815 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return FAIL;
3818 }
3819
3820 /*
3821 * Get the third variable.
3822 */
3823 *arg = skipwhite(*arg + 1);
3824 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3825 {
3826 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return FAIL;
3829 }
3830 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
3834 return OK;
3835}
3836
3837/*
3838 * Handle first level expression:
3839 * expr2 || expr2 || expr2 logical OR
3840 *
3841 * "arg" must point to the first non-white of the expression.
3842 * "arg" is advanced to the next non-white after the recognized expression.
3843 *
3844 * Return OK or FAIL.
3845 */
3846 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 int evaluate;
3851{
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 long result;
3854 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857 /*
3858 * Get the first variable.
3859 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return FAIL;
3862
3863 /*
3864 * Repeat until there is no following "||".
3865 */
3866 first = TRUE;
3867 result = FALSE;
3868 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3869 {
3870 if (evaluate && first)
3871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003875 if (error)
3876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 first = FALSE;
3878 }
3879
3880 /*
3881 * Get the second variable.
3882 */
3883 *arg = skipwhite(*arg + 2);
3884 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3885 return FAIL;
3886
3887 /*
3888 * Compute the result.
3889 */
3890 if (evaluate && !result)
3891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003892 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003894 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003895 if (error)
3896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 if (evaluate)
3899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 rettv->v_type = VAR_NUMBER;
3901 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 }
3904
3905 return OK;
3906}
3907
3908/*
3909 * Handle second level expression:
3910 * expr3 && expr3 && expr3 logical AND
3911 *
3912 * "arg" must point to the first non-white of the expression.
3913 * "arg" is advanced to the next non-white after the recognized expression.
3914 *
3915 * Return OK or FAIL.
3916 */
3917 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 int evaluate;
3922{
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 long result;
3925 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 /*
3929 * Get the first variable.
3930 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003931 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return FAIL;
3933
3934 /*
3935 * Repeat until there is no following "&&".
3936 */
3937 first = TRUE;
3938 result = TRUE;
3939 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3940 {
3941 if (evaluate && first)
3942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 if (error)
3947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 first = FALSE;
3949 }
3950
3951 /*
3952 * Get the second variable.
3953 */
3954 *arg = skipwhite(*arg + 2);
3955 if (eval4(arg, &var2, evaluate && result) == FAIL)
3956 return FAIL;
3957
3958 /*
3959 * Compute the result.
3960 */
3961 if (evaluate && result)
3962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003963 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003966 if (error)
3967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 if (evaluate)
3970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003971 rettv->v_type = VAR_NUMBER;
3972 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 }
3975
3976 return OK;
3977}
3978
3979/*
3980 * Handle third level expression:
3981 * var1 == var2
3982 * var1 =~ var2
3983 * var1 != var2
3984 * var1 !~ var2
3985 * var1 > var2
3986 * var1 >= var2
3987 * var1 < var2
3988 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003989 * var1 is var2
3990 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *
3992 * "arg" must point to the first non-white of the expression.
3993 * "arg" is advanced to the next non-white after the recognized expression.
3994 *
3995 * Return OK or FAIL.
3996 */
3997 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 int evaluate;
4002{
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u *p;
4005 int i;
4006 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004007 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 int len = 2;
4009 long n1, n2;
4010 char_u *s1, *s2;
4011 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4012 regmatch_T regmatch;
4013 int ic;
4014 char_u *save_cpo;
4015
4016 /*
4017 * Get the first variable.
4018 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return FAIL;
4021
4022 p = *arg;
4023 switch (p[0])
4024 {
4025 case '=': if (p[1] == '=')
4026 type = TYPE_EQUAL;
4027 else if (p[1] == '~')
4028 type = TYPE_MATCH;
4029 break;
4030 case '!': if (p[1] == '=')
4031 type = TYPE_NEQUAL;
4032 else if (p[1] == '~')
4033 type = TYPE_NOMATCH;
4034 break;
4035 case '>': if (p[1] != '=')
4036 {
4037 type = TYPE_GREATER;
4038 len = 1;
4039 }
4040 else
4041 type = TYPE_GEQUAL;
4042 break;
4043 case '<': if (p[1] != '=')
4044 {
4045 type = TYPE_SMALLER;
4046 len = 1;
4047 }
4048 else
4049 type = TYPE_SEQUAL;
4050 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 case 'i': if (p[1] == 's')
4052 {
4053 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4054 len = 5;
4055 if (!vim_isIDc(p[len]))
4056 {
4057 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4058 type_is = TRUE;
4059 }
4060 }
4061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063
4064 /*
4065 * If there is a comparitive operator, use it.
4066 */
4067 if (type != TYPE_UNKNOWN)
4068 {
4069 /* extra question mark appended: ignore case */
4070 if (p[len] == '?')
4071 {
4072 ic = TRUE;
4073 ++len;
4074 }
4075 /* extra '#' appended: match case */
4076 else if (p[len] == '#')
4077 {
4078 ic = FALSE;
4079 ++len;
4080 }
4081 /* nothing appened: use 'ignorecase' */
4082 else
4083 ic = p_ic;
4084
4085 /*
4086 * Get the second variable.
4087 */
4088 *arg = skipwhite(p + len);
4089 if (eval5(arg, &var2, evaluate) == FAIL)
4090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093 }
4094
4095 if (evaluate)
4096 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004097 if (type_is && rettv->v_type != var2.v_type)
4098 {
4099 /* For "is" a different type always means FALSE, for "notis"
4100 * it means TRUE. */
4101 n1 = (type == TYPE_NEQUAL);
4102 }
4103 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4104 {
4105 if (type_is)
4106 {
4107 n1 = (rettv->v_type == var2.v_type
4108 && rettv->vval.v_list == var2.vval.v_list);
4109 if (type == TYPE_NEQUAL)
4110 n1 = !n1;
4111 }
4112 else if (rettv->v_type != var2.v_type
4113 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4114 {
4115 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004116 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004117 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004118 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004119 clear_tv(rettv);
4120 clear_tv(&var2);
4121 return FAIL;
4122 }
4123 else
4124 {
4125 /* Compare two Lists for being equal or unequal. */
4126 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4127 if (type == TYPE_NEQUAL)
4128 n1 = !n1;
4129 }
4130 }
4131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004132 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4133 {
4134 if (type_is)
4135 {
4136 n1 = (rettv->v_type == var2.v_type
4137 && rettv->vval.v_dict == var2.vval.v_dict);
4138 if (type == TYPE_NEQUAL)
4139 n1 = !n1;
4140 }
4141 else if (rettv->v_type != var2.v_type
4142 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4143 {
4144 if (rettv->v_type != var2.v_type)
4145 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4146 else
4147 EMSG(_("E736: Invalid operation for Dictionary"));
4148 clear_tv(rettv);
4149 clear_tv(&var2);
4150 return FAIL;
4151 }
4152 else
4153 {
4154 /* Compare two Dictionaries for being equal or unequal. */
4155 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4156 if (type == TYPE_NEQUAL)
4157 n1 = !n1;
4158 }
4159 }
4160
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004161 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4162 {
4163 if (rettv->v_type != var2.v_type
4164 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4165 {
4166 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004167 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004169 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004170 clear_tv(rettv);
4171 clear_tv(&var2);
4172 return FAIL;
4173 }
4174 else
4175 {
4176 /* Compare two Funcrefs for being equal or unequal. */
4177 if (rettv->vval.v_string == NULL
4178 || var2.vval.v_string == NULL)
4179 n1 = FALSE;
4180 else
4181 n1 = STRCMP(rettv->vval.v_string,
4182 var2.vval.v_string) == 0;
4183 if (type == TYPE_NEQUAL)
4184 n1 = !n1;
4185 }
4186 }
4187
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 /*
4189 * If one of the two variables is a number, compare as a number.
4190 * When using "=~" or "!~", always compare as string.
4191 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004192 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 n1 = get_tv_number(rettv);
4196 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 switch (type)
4198 {
4199 case TYPE_EQUAL: n1 = (n1 == n2); break;
4200 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4201 case TYPE_GREATER: n1 = (n1 > n2); break;
4202 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4203 case TYPE_SMALLER: n1 = (n1 < n2); break;
4204 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4205 case TYPE_UNKNOWN:
4206 case TYPE_MATCH:
4207 case TYPE_NOMATCH: break; /* avoid gcc warning */
4208 }
4209 }
4210 else
4211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 s1 = get_tv_string_buf(rettv, buf1);
4213 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4215 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4216 else
4217 i = 0;
4218 n1 = FALSE;
4219 switch (type)
4220 {
4221 case TYPE_EQUAL: n1 = (i == 0); break;
4222 case TYPE_NEQUAL: n1 = (i != 0); break;
4223 case TYPE_GREATER: n1 = (i > 0); break;
4224 case TYPE_GEQUAL: n1 = (i >= 0); break;
4225 case TYPE_SMALLER: n1 = (i < 0); break;
4226 case TYPE_SEQUAL: n1 = (i <= 0); break;
4227
4228 case TYPE_MATCH:
4229 case TYPE_NOMATCH:
4230 /* avoid 'l' flag in 'cpoptions' */
4231 save_cpo = p_cpo;
4232 p_cpo = (char_u *)"";
4233 regmatch.regprog = vim_regcomp(s2,
4234 RE_MAGIC + RE_STRING);
4235 regmatch.rm_ic = ic;
4236 if (regmatch.regprog != NULL)
4237 {
4238 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4239 vim_free(regmatch.regprog);
4240 if (type == TYPE_NOMATCH)
4241 n1 = !n1;
4242 }
4243 p_cpo = save_cpo;
4244 break;
4245
4246 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4247 }
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
4250 clear_tv(&var2);
4251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle fourth level expression:
4261 * + number addition
4262 * - number subtraction
4263 * . string concatenation
4264 *
4265 * "arg" must point to the first non-white of the expression.
4266 * "arg" is advanced to the next non-white after the recognized expression.
4267 *
4268 * Return OK or FAIL.
4269 */
4270 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int evaluate;
4275{
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T var2;
4277 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int op;
4279 long n1, n2;
4280 char_u *s1, *s2;
4281 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4282 char_u *p;
4283
4284 /*
4285 * Get the first variable.
4286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Repeat computing, until no '+', '-' or '.' is following.
4292 */
4293 for (;;)
4294 {
4295 op = **arg;
4296 if (op != '+' && op != '-' && op != '.')
4297 break;
4298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 if (op != '+' || rettv->v_type != VAR_LIST)
4300 {
4301 /* For "list + ...", an illegal use of the first operand as
4302 * a number cannot be determined before evaluating the 2nd
4303 * operand: if this is also a list, all is ok.
4304 * For "something . ...", "something - ..." or "non-list + ...",
4305 * we know that the first operand needs to be a string or number
4306 * without evaluating the 2nd operand. So check before to avoid
4307 * side effects after an error. */
4308 if (evaluate && get_tv_string_chk(rettv) == NULL)
4309 {
4310 clear_tv(rettv);
4311 return FAIL;
4312 }
4313 }
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /*
4316 * Get the second variable.
4317 */
4318 *arg = skipwhite(*arg + 1);
4319 if (eval6(arg, &var2, evaluate) == FAIL)
4320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 return FAIL;
4323 }
4324
4325 if (evaluate)
4326 {
4327 /*
4328 * Compute the result.
4329 */
4330 if (op == '.')
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4333 s2 = get_tv_string_buf_chk(&var2, buf2);
4334 if (s2 == NULL) /* type error ? */
4335 {
4336 clear_tv(rettv);
4337 clear_tv(&var2);
4338 return FAIL;
4339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004340 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
4342 rettv->v_type = VAR_STRING;
4343 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004345 else if (op == '+' && rettv->v_type == VAR_LIST
4346 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004347 {
4348 /* concatenate Lists */
4349 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4350 &var3) == FAIL)
4351 {
4352 clear_tv(rettv);
4353 clear_tv(&var2);
4354 return FAIL;
4355 }
4356 clear_tv(rettv);
4357 *rettv = var3;
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 else
4360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 int error = FALSE;
4362
4363 n1 = get_tv_number_chk(rettv, &error);
4364 if (error)
4365 {
4366 /* This can only happen for "list + non-list".
4367 * For "non-list + ..." or "something - ...", we returned
4368 * before evaluating the 2nd operand. */
4369 clear_tv(rettv);
4370 return FAIL;
4371 }
4372 n2 = get_tv_number_chk(&var2, &error);
4373 if (error)
4374 {
4375 clear_tv(rettv);
4376 clear_tv(&var2);
4377 return FAIL;
4378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (op == '+')
4381 n1 = n1 + n2;
4382 else
4383 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 rettv->v_type = VAR_NUMBER;
4385 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 }
4390 return OK;
4391}
4392
4393/*
4394 * Handle fifth level expression:
4395 * * number multiplication
4396 * / number division
4397 * % number modulo
4398 *
4399 * "arg" must point to the first non-white of the expression.
4400 * "arg" is advanced to the next non-white after the recognized expression.
4401 *
4402 * Return OK or FAIL.
4403 */
4404 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 int evaluate;
4409{
Bram Moolenaar33570922005-01-25 22:26:29 +00004410 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 int op;
4412 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 /*
4416 * Get the first variable.
4417 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004418 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 return FAIL;
4420
4421 /*
4422 * Repeat computing, until no '*', '/' or '%' is following.
4423 */
4424 for (;;)
4425 {
4426 op = **arg;
4427 if (op != '*' && op != '/' && op != '%')
4428 break;
4429
4430 if (evaluate)
4431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004432 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004434 if (error)
4435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 else
4438 n1 = 0;
4439
4440 /*
4441 * Get the second variable.
4442 */
4443 *arg = skipwhite(*arg + 1);
4444 if (eval7(arg, &var2, evaluate) == FAIL)
4445 return FAIL;
4446
4447 if (evaluate)
4448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004451 if (error)
4452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
4455 * Compute the result.
4456 */
4457 if (op == '*')
4458 n1 = n1 * n2;
4459 else if (op == '/')
4460 {
4461 if (n2 == 0) /* give an error message? */
4462 n1 = 0x7fffffffL;
4463 else
4464 n1 = n1 / n2;
4465 }
4466 else
4467 {
4468 if (n2 == 0) /* give an error message? */
4469 n1 = 0;
4470 else
4471 n1 = n1 % n2;
4472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 rettv->v_type = VAR_NUMBER;
4474 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 }
4477
4478 return OK;
4479}
4480
4481/*
4482 * Handle sixth level expression:
4483 * number number constant
4484 * "string" string contstant
4485 * 'string' literal string contstant
4486 * &option-name option value
4487 * @r register contents
4488 * identifier variable value
4489 * function() function call
4490 * $VAR environment variable
4491 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004492 * [expr, expr] List
4493 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 *
4495 * Also handle:
4496 * ! in front logical NOT
4497 * - in front unary minus
4498 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 * trailing [] subscript in String or List
4500 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 *
4502 * "arg" must point to the first non-white of the expression.
4503 * "arg" is advanced to the next non-white after the recognized expression.
4504 *
4505 * Return OK or FAIL.
4506 */
4507 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 int evaluate;
4512{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 long n;
4514 int len;
4515 char_u *s;
4516 int val;
4517 char_u *start_leader, *end_leader;
4518 int ret = OK;
4519 char_u *alias;
4520
4521 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004523 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 /*
4528 * Skip '!' and '-' characters. They are handled later.
4529 */
4530 start_leader = *arg;
4531 while (**arg == '!' || **arg == '-' || **arg == '+')
4532 *arg = skipwhite(*arg + 1);
4533 end_leader = *arg;
4534
4535 switch (**arg)
4536 {
4537 /*
4538 * Number constant.
4539 */
4540 case '0':
4541 case '1':
4542 case '2':
4543 case '3':
4544 case '4':
4545 case '5':
4546 case '6':
4547 case '7':
4548 case '8':
4549 case '9':
4550 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4551 *arg += len;
4552 if (evaluate)
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 rettv->v_type = VAR_NUMBER;
4555 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 break;
4558
4559 /*
4560 * String constant: "string".
4561 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 break;
4564
4565 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004566 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 break;
4570
4571 /*
4572 * List: [expr, expr]
4573 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 break;
4576
4577 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004578 * Dictionary: {key: val, key: val}
4579 */
4580 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4581 break;
4582
4583 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004584 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004586 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
4588
4589 /*
4590 * Environment variable: $VAR.
4591 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 break;
4594
4595 /*
4596 * Register contents: @r.
4597 */
4598 case '@': ++*arg;
4599 if (evaluate)
4600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004602 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604 if (**arg != NUL)
4605 ++*arg;
4606 break;
4607
4608 /*
4609 * nested expression: (expression).
4610 */
4611 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (**arg == ')')
4614 ++*arg;
4615 else if (ret == OK)
4616 {
4617 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 ret = FAIL;
4620 }
4621 break;
4622
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 break;
4625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626
4627 if (ret == NOTDONE)
4628 {
4629 /*
4630 * Must be a variable or function name.
4631 * Can also be a curly-braces kind of name: {expr}.
4632 */
4633 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004634 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 if (alias != NULL)
4636 s = alias;
4637
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004638 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 ret = FAIL;
4640 else
4641 {
4642 if (**arg == '(') /* recursive! */
4643 {
4644 /* If "s" is the name of a variable of type VAR_FUNC
4645 * use its contents. */
4646 s = deref_func_name(s, &len);
4647
4648 /* Invoke the function. */
4649 ret = get_func_tv(s, len, rettv, arg,
4650 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004651 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652 /* Stop the expression evaluation when immediately
4653 * aborting on error, or when an interrupt occurred or
4654 * an exception was thrown but not caught. */
4655 if (aborting())
4656 {
4657 if (ret == OK)
4658 clear_tv(rettv);
4659 ret = FAIL;
4660 }
4661 }
4662 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004664 else
4665 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 }
4667
4668 if (alias != NULL)
4669 vim_free(alias);
4670 }
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 *arg = skipwhite(*arg);
4673
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004674 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4675 * expr(expr). */
4676 if (ret == OK)
4677 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /*
4680 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4681 */
4682 if (ret == OK && evaluate && end_leader > start_leader)
4683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 int error = FALSE;
4685
4686 val = get_tv_number_chk(rettv, &error);
4687 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 clear_tv(rettv);
4690 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 else
4693 {
4694 while (end_leader > start_leader)
4695 {
4696 --end_leader;
4697 if (*end_leader == '!')
4698 val = !val;
4699 else if (*end_leader == '-')
4700 val = -val;
4701 }
4702 clear_tv(rettv);
4703 rettv->v_type = VAR_NUMBER;
4704 rettv->vval.v_number = val;
4705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707
4708 return ret;
4709}
4710
4711/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004712 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4713 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4715 */
4716 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004717eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004721 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722{
4723 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004724 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 long len = -1;
4727 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733 if (verbose)
4734 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 return FAIL;
4736 }
4737
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 /*
4741 * dict.name
4742 */
4743 key = *arg + 1;
4744 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4745 ;
4746 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 }
4750 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 /*
4753 * something[idx]
4754 *
4755 * Get the (first) variable from inside the [].
4756 */
4757 *arg = skipwhite(*arg + 1);
4758 if (**arg == ':')
4759 empty1 = TRUE;
4760 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4761 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4763 {
4764 /* not a number or string */
4765 clear_tv(&var1);
4766 return FAIL;
4767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768
4769 /*
4770 * Get the second variable from inside the [:].
4771 */
4772 if (**arg == ':')
4773 {
4774 range = TRUE;
4775 *arg = skipwhite(*arg + 1);
4776 if (**arg == ']')
4777 empty2 = TRUE;
4778 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 if (!empty1)
4781 clear_tv(&var1);
4782 return FAIL;
4783 }
4784 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4785 {
4786 /* not a number or string */
4787 if (!empty1)
4788 clear_tv(&var1);
4789 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 return FAIL;
4791 }
4792 }
4793
4794 /* Check for the ']'. */
4795 if (**arg != ']')
4796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004797 if (verbose)
4798 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 clear_tv(&var1);
4800 if (range)
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806
4807 if (evaluate)
4808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 n1 = 0;
4810 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 n1 = get_tv_number(&var1);
4813 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 }
4815 if (range)
4816 {
4817 if (empty2)
4818 n2 = -1;
4819 else
4820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 n2 = get_tv_number(&var2);
4822 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 }
4825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 {
4828 case VAR_NUMBER:
4829 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 len = (long)STRLEN(s);
4832 if (range)
4833 {
4834 /* The resulting variable is a substring. If the indexes
4835 * are out of range the result is empty. */
4836 if (n1 < 0)
4837 {
4838 n1 = len + n1;
4839 if (n1 < 0)
4840 n1 = 0;
4841 }
4842 if (n2 < 0)
4843 n2 = len + n2;
4844 else if (n2 >= len)
4845 n2 = len;
4846 if (n1 >= len || n2 < 0 || n1 > n2)
4847 s = NULL;
4848 else
4849 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4850 }
4851 else
4852 {
4853 /* The resulting variable is a string of a single
4854 * character. If the index is too big or negative the
4855 * result is empty. */
4856 if (n1 >= len || n1 < 0)
4857 s = NULL;
4858 else
4859 s = vim_strnsave(s + n1, 1);
4860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004861 clear_tv(rettv);
4862 rettv->v_type = VAR_STRING;
4863 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 break;
4865
4866 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 if (n1 < 0)
4869 n1 = len + n1;
4870 if (!empty1 && (n1 < 0 || n1 >= len))
4871 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004872 /* For a range we allow invalid values and return an empty
4873 * list. A list index out of range is an error. */
4874 if (!range)
4875 {
4876 if (verbose)
4877 EMSGN(_(e_listidx), n1);
4878 return FAIL;
4879 }
4880 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 }
4882 if (range)
4883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 list_T *l;
4885 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886
4887 if (n2 < 0)
4888 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004889 else if (n2 >= len)
4890 n2 = len - 1;
4891 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004892 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 l = list_alloc();
4894 if (l == NULL)
4895 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 n1 <= n2; ++n1)
4898 {
4899 if (list_append_tv(l, &item->li_tv) == FAIL)
4900 {
4901 list_free(l);
4902 return FAIL;
4903 }
4904 item = item->li_next;
4905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 clear_tv(rettv);
4907 rettv->v_type = VAR_LIST;
4908 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004909 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 }
4911 else
4912 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004913 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
4915 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 }
4917 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918
4919 case VAR_DICT:
4920 if (range)
4921 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004922 if (verbose)
4923 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 if (len == -1)
4925 clear_tv(&var1);
4926 return FAIL;
4927 }
4928 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930
4931 if (len == -1)
4932 {
4933 key = get_tv_string(&var1);
4934 if (*key == NUL)
4935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936 if (verbose)
4937 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 clear_tv(&var1);
4939 return FAIL;
4940 }
4941 }
4942
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004943 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004945 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004946 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 if (len == -1)
4948 clear_tv(&var1);
4949 if (item == NULL)
4950 return FAIL;
4951
4952 copy_tv(&item->di_tv, &var1);
4953 clear_tv(rettv);
4954 *rettv = var1;
4955 }
4956 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 }
4958 }
4959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 return OK;
4961}
4962
4963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 * Get an option value.
4965 * "arg" points to the '&' or '+' before the option name.
4966 * "arg" is advanced to character after the option name.
4967 * Return OK or FAIL.
4968 */
4969 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004972 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 int evaluate;
4974{
4975 char_u *option_end;
4976 long numval;
4977 char_u *stringval;
4978 int opt_type;
4979 int c;
4980 int working = (**arg == '+'); /* has("+option") */
4981 int ret = OK;
4982 int opt_flags;
4983
4984 /*
4985 * Isolate the option name and find its value.
4986 */
4987 option_end = find_option_end(arg, &opt_flags);
4988 if (option_end == NULL)
4989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 EMSG2(_("E112: Option name missing: %s"), *arg);
4992 return FAIL;
4993 }
4994
4995 if (!evaluate)
4996 {
4997 *arg = option_end;
4998 return OK;
4999 }
5000
5001 c = *option_end;
5002 *option_end = NUL;
5003 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 if (opt_type == -3) /* invalid name */
5007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 EMSG2(_("E113: Unknown option: %s"), *arg);
5010 ret = FAIL;
5011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 if (opt_type == -2) /* hidden string option */
5015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 rettv->v_type = VAR_STRING;
5017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else if (opt_type == -1) /* hidden number option */
5020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021 rettv->v_type = VAR_NUMBER;
5022 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 else if (opt_type == 1) /* number option */
5025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005026 rettv->v_type = VAR_NUMBER;
5027 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 else /* string option */
5030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 rettv->v_type = VAR_STRING;
5032 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 }
5035 else if (working && (opt_type == -2 || opt_type == -1))
5036 ret = FAIL;
5037
5038 *option_end = c; /* put back for error messages */
5039 *arg = option_end;
5040
5041 return ret;
5042}
5043
5044/*
5045 * Allocate a variable for a string constant.
5046 * Return OK or FAIL.
5047 */
5048 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005049get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 int evaluate;
5053{
5054 char_u *p;
5055 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int extra = 0;
5057
5058 /*
5059 * Find the end of the string, skipping backslashed characters.
5060 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 if (*p == '\\' && p[1] != NUL)
5064 {
5065 ++p;
5066 /* A "\<x>" form occupies at least 4 characters, and produces up
5067 * to 6 characters: reserve space for 2 extra */
5068 if (*p == '<')
5069 extra += 2;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072
5073 if (*p != '"')
5074 {
5075 EMSG2(_("E114: Missing quote: %s"), *arg);
5076 return FAIL;
5077 }
5078
5079 /* If only parsing, set *arg and return here */
5080 if (!evaluate)
5081 {
5082 *arg = p + 1;
5083 return OK;
5084 }
5085
5086 /*
5087 * Copy the string into allocated memory, handling backslashed
5088 * characters.
5089 */
5090 name = alloc((unsigned)(p - *arg + extra));
5091 if (name == NULL)
5092 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 rettv->v_type = VAR_STRING;
5094 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 if (*p == '\\')
5099 {
5100 switch (*++p)
5101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 case 'b': *name++ = BS; ++p; break;
5103 case 'e': *name++ = ESC; ++p; break;
5104 case 'f': *name++ = FF; ++p; break;
5105 case 'n': *name++ = NL; ++p; break;
5106 case 'r': *name++ = CAR; ++p; break;
5107 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 case 'X': /* hex: "\x1", "\x12" */
5110 case 'x':
5111 case 'u': /* Unicode: "\u0023" */
5112 case 'U':
5113 if (vim_isxdigit(p[1]))
5114 {
5115 int n, nr;
5116 int c = toupper(*p);
5117
5118 if (c == 'X')
5119 n = 2;
5120 else
5121 n = 4;
5122 nr = 0;
5123 while (--n >= 0 && vim_isxdigit(p[1]))
5124 {
5125 ++p;
5126 nr = (nr << 4) + hex2nr(*p);
5127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef FEAT_MBYTE
5130 /* For "\u" store the number according to
5131 * 'encoding'. */
5132 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else
5135#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139
5140 /* octal: "\1", "\12", "\123" */
5141 case '0':
5142 case '1':
5143 case '2':
5144 case '3':
5145 case '4':
5146 case '5':
5147 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 case '7': *name = *p++ - '0';
5149 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 *name = (*name << 3) + *p++ - '0';
5152 if (*p >= '0' && *p <= '7')
5153 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157
5158 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (extra != 0)
5161 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164 }
5165 /* FALLTHROUGH */
5166
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169 }
5170 }
5171 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 *arg = p + 1;
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 return OK;
5179}
5180
5181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 * Return OK or FAIL.
5184 */
5185 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 int evaluate;
5190{
5191 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005192 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005193 int reduce = 0;
5194
5195 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005197 */
5198 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005203 break;
5204 ++reduce;
5205 ++p;
5206 }
5207 }
5208
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005210 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005212 return FAIL;
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216 if (!evaluate)
5217 {
5218 *arg = p + 1;
5219 return OK;
5220 }
5221
5222 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005224 */
5225 str = alloc((unsigned)((p - *arg) - reduce));
5226 if (str == NULL)
5227 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 rettv->v_type = VAR_STRING;
5229 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005230
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005232 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005236 break;
5237 ++p;
5238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 *arg = p + 1;
5243
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 return OK;
5245}
5246
5247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 * Allocate a variable for a List and fill it from "*arg".
5249 * Return OK or FAIL.
5250 */
5251 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005254 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 int evaluate;
5256{
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 list_T *l = NULL;
5258 typval_T tv;
5259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260
5261 if (evaluate)
5262 {
5263 l = list_alloc();
5264 if (l == NULL)
5265 return FAIL;
5266 }
5267
5268 *arg = skipwhite(*arg + 1);
5269 while (**arg != ']' && **arg != NUL)
5270 {
5271 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5272 goto failret;
5273 if (evaluate)
5274 {
5275 item = listitem_alloc();
5276 if (item != NULL)
5277 {
5278 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005279 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 list_append(l, item);
5281 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005282 else
5283 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 }
5285
5286 if (**arg == ']')
5287 break;
5288 if (**arg != ',')
5289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 goto failret;
5292 }
5293 *arg = skipwhite(*arg + 1);
5294 }
5295
5296 if (**arg != ']')
5297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299failret:
5300 if (evaluate)
5301 list_free(l);
5302 return FAIL;
5303 }
5304
5305 *arg = skipwhite(*arg + 1);
5306 if (evaluate)
5307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 rettv->v_type = VAR_LIST;
5309 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 ++l->lv_refcount;
5311 }
5312
5313 return OK;
5314}
5315
5316/*
5317 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005318 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005320 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321list_alloc()
5322{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005323 list_T *l;
5324
5325 l = (list_T *)alloc_clear(sizeof(list_T));
5326 if (l != NULL)
5327 {
5328 /* Prepend the list to the list of lists for garbage collection. */
5329 if (first_list != NULL)
5330 first_list->lv_used_prev = l;
5331 l->lv_used_prev = NULL;
5332 l->lv_used_next = first_list;
5333 first_list = l;
5334 }
5335 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336}
5337
5338/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005339 * Allocate an empty list for a return value.
5340 * Returns OK or FAIL.
5341 */
5342 static int
5343rettv_list_alloc(rettv)
5344 typval_T *rettv;
5345{
5346 list_T *l = list_alloc();
5347
5348 if (l == NULL)
5349 return FAIL;
5350
5351 rettv->vval.v_list = l;
5352 rettv->v_type = VAR_LIST;
5353 ++l->lv_refcount;
5354 return OK;
5355}
5356
5357/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 * Unreference a list: decrement the reference count and free it when it
5359 * becomes zero.
5360 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005361 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5366 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367}
5368
5369/*
5370 * Free a list, including all items it points to.
5371 * Ignores the reference count.
5372 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005373 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005375 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378
Bram Moolenaard9fba312005-06-26 22:34:35 +00005379 /* Avoid that recursive reference to the list frees us again. */
5380 l->lv_refcount = DEL_REFCOUNT;
5381
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005382 /* Remove the list from the list of lists for garbage collection. */
5383 if (l->lv_used_prev == NULL)
5384 first_list = l->lv_used_next;
5385 else
5386 l->lv_used_prev->lv_used_next = l->lv_used_next;
5387 if (l->lv_used_next != NULL)
5388 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5389
Bram Moolenaard9fba312005-06-26 22:34:35 +00005390 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005392 /* Remove the item before deleting it. */
5393 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 listitem_free(item);
5395 }
5396 vim_free(l);
5397}
5398
5399/*
5400 * Allocate a list item.
5401 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403listitem_alloc()
5404{
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406}
5407
5408/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005409 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 */
5411 static void
5412listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 vim_free(item);
5417}
5418
5419/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005420 * Remove a list item from a List and free it. Also clears the value.
5421 */
5422 static void
5423listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005424 list_T *l;
5425 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005426{
5427 list_remove(l, item, item);
5428 listitem_free(item);
5429}
5430
5431/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 * Get the number of items in a list.
5433 */
5434 static long
5435list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 if (l == NULL)
5439 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441}
5442
5443/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444 * Return TRUE when two lists have exactly the same values.
5445 */
5446 static int
5447list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 list_T *l1;
5449 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005450 int ic; /* ignore case for strings */
5451{
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005453
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005454 if (list_len(l1) != list_len(l2))
5455 return FALSE;
5456
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005457 for (item1 = l1->lv_first, item2 = l2->lv_first;
5458 item1 != NULL && item2 != NULL;
5459 item1 = item1->li_next, item2 = item2->li_next)
5460 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5461 return FALSE;
5462 return item1 == NULL && item2 == NULL;
5463}
5464
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005465#if defined(FEAT_PYTHON) || defined(PROTO)
5466/*
5467 * Return the dictitem that an entry in a hashtable points to.
5468 */
5469 dictitem_T *
5470dict_lookup(hi)
5471 hashitem_T *hi;
5472{
5473 return HI2DI(hi);
5474}
5475#endif
5476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005477/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005478 * Return TRUE when two dictionaries have exactly the same key/values.
5479 */
5480 static int
5481dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005482 dict_T *d1;
5483 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005484 int ic; /* ignore case for strings */
5485{
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 hashitem_T *hi;
5487 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005488 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005489
5490 if (dict_len(d1) != dict_len(d2))
5491 return FALSE;
5492
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005493 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005496 if (!HASHITEM_EMPTY(hi))
5497 {
5498 item2 = dict_find(d2, hi->hi_key, -1);
5499 if (item2 == NULL)
5500 return FALSE;
5501 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5502 return FALSE;
5503 --todo;
5504 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505 }
5506 return TRUE;
5507}
5508
5509/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005511 * Compares the items just like "==" would compare them, but strings and
5512 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513 */
5514 static int
5515tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 typval_T *tv1;
5517 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005518 int ic; /* ignore case */
5519{
5520 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005521 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005522
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005523 if (tv1->v_type != tv2->v_type)
5524 return FALSE;
5525
5526 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005527 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005528 case VAR_LIST:
5529 /* recursive! */
5530 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5531
5532 case VAR_DICT:
5533 /* recursive! */
5534 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5535
5536 case VAR_FUNC:
5537 return (tv1->vval.v_string != NULL
5538 && tv2->vval.v_string != NULL
5539 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5540
5541 case VAR_NUMBER:
5542 return tv1->vval.v_number == tv2->vval.v_number;
5543
5544 case VAR_STRING:
5545 s1 = get_tv_string_buf(tv1, buf1);
5546 s2 = get_tv_string_buf(tv2, buf2);
5547 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005549
5550 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005551 return TRUE;
5552}
5553
5554/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 * Locate item with index "n" in list "l" and return it.
5556 * A negative index is counted from the end; -1 is the last item.
5557 * Returns NULL when "n" is out of range.
5558 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005559 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 long n;
5563{
Bram Moolenaar33570922005-01-25 22:26:29 +00005564 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 long idx;
5566
5567 if (l == NULL)
5568 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005569
5570 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005572 n = l->lv_len + n;
5573
5574 /* Check for index out of range. */
5575 if (n < 0 || n >= l->lv_len)
5576 return NULL;
5577
5578 /* When there is a cached index may start search from there. */
5579 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005581 if (n < l->lv_idx / 2)
5582 {
5583 /* closest to the start of the list */
5584 item = l->lv_first;
5585 idx = 0;
5586 }
5587 else if (n > (l->lv_idx + l->lv_len) / 2)
5588 {
5589 /* closest to the end of the list */
5590 item = l->lv_last;
5591 idx = l->lv_len - 1;
5592 }
5593 else
5594 {
5595 /* closest to the cached index */
5596 item = l->lv_idx_item;
5597 idx = l->lv_idx;
5598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 }
5600 else
5601 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005602 if (n < l->lv_len / 2)
5603 {
5604 /* closest to the start of the list */
5605 item = l->lv_first;
5606 idx = 0;
5607 }
5608 else
5609 {
5610 /* closest to the end of the list */
5611 item = l->lv_last;
5612 idx = l->lv_len - 1;
5613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005615
5616 while (n > idx)
5617 {
5618 /* search forward */
5619 item = item->li_next;
5620 ++idx;
5621 }
5622 while (n < idx)
5623 {
5624 /* search backward */
5625 item = item->li_prev;
5626 --idx;
5627 }
5628
5629 /* cache the used index */
5630 l->lv_idx = idx;
5631 l->lv_idx_item = item;
5632
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 return item;
5634}
5635
5636/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005637 * Get list item "l[idx]" as a number.
5638 */
5639 static long
5640list_find_nr(l, idx, errorp)
5641 list_T *l;
5642 long idx;
5643 int *errorp; /* set to TRUE when something wrong */
5644{
5645 listitem_T *li;
5646
5647 li = list_find(l, idx);
5648 if (li == NULL)
5649 {
5650 if (errorp != NULL)
5651 *errorp = TRUE;
5652 return -1L;
5653 }
5654 return get_tv_number_chk(&li->li_tv, errorp);
5655}
5656
5657/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005658 * Locate "item" list "l" and return its index.
5659 * Returns -1 when "item" is not in the list.
5660 */
5661 static long
5662list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005663 list_T *l;
5664 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005665{
5666 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005668
5669 if (l == NULL)
5670 return -1;
5671 idx = 0;
5672 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5673 ++idx;
5674 if (li == NULL)
5675 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005676 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005677}
5678
5679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 * Append item "item" to the end of list "l".
5681 */
5682 static void
5683list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005684 list_T *l;
5685 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686{
5687 if (l->lv_last == NULL)
5688 {
5689 /* empty list */
5690 l->lv_first = item;
5691 l->lv_last = item;
5692 item->li_prev = NULL;
5693 }
5694 else
5695 {
5696 l->lv_last->li_next = item;
5697 item->li_prev = l->lv_last;
5698 l->lv_last = item;
5699 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005700 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 item->li_next = NULL;
5702}
5703
5704/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005705 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005706 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707 */
5708 static int
5709list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005710 list_T *l;
5711 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005713 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714
Bram Moolenaar05159a02005-02-26 23:04:13 +00005715 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005717 copy_tv(tv, &li->li_tv);
5718 list_append(l, li);
5719 return OK;
5720}
5721
5722/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005723 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005724 * Return FAIL when out of memory.
5725 */
5726 int
5727list_append_dict(list, dict)
5728 list_T *list;
5729 dict_T *dict;
5730{
5731 listitem_T *li = listitem_alloc();
5732
5733 if (li == NULL)
5734 return FAIL;
5735 li->li_tv.v_type = VAR_DICT;
5736 li->li_tv.v_lock = 0;
5737 li->li_tv.vval.v_dict = dict;
5738 list_append(list, li);
5739 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 return OK;
5741}
5742
5743/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005744 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005745 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005746 * Returns FAIL when out of memory.
5747 */
5748 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005749list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005750 list_T *l;
5751 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005752 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005753{
5754 listitem_T *li = listitem_alloc();
5755
5756 if (li == NULL)
5757 return FAIL;
5758 list_append(l, li);
5759 li->li_tv.v_type = VAR_STRING;
5760 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005761 if (str == NULL)
5762 li->li_tv.vval.v_string = NULL;
5763 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005764 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005765 return FAIL;
5766 return OK;
5767}
5768
5769/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005770 * Append "n" to list "l".
5771 * Returns FAIL when out of memory.
5772 */
5773 static int
5774list_append_number(l, n)
5775 list_T *l;
5776 varnumber_T n;
5777{
5778 listitem_T *li;
5779
5780 li = listitem_alloc();
5781 if (li == NULL)
5782 return FAIL;
5783 li->li_tv.v_type = VAR_NUMBER;
5784 li->li_tv.v_lock = 0;
5785 li->li_tv.vval.v_number = n;
5786 list_append(l, li);
5787 return OK;
5788}
5789
5790/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792 * If "item" is NULL append at the end.
5793 * Return FAIL when out of memory.
5794 */
5795 static int
5796list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 list_T *l;
5798 typval_T *tv;
5799 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005802
5803 if (ni == NULL)
5804 return FAIL;
5805 copy_tv(tv, &ni->li_tv);
5806 if (item == NULL)
5807 /* Append new item at end of list. */
5808 list_append(l, ni);
5809 else
5810 {
5811 /* Insert new item before existing item. */
5812 ni->li_prev = item->li_prev;
5813 ni->li_next = item;
5814 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005815 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005817 ++l->lv_idx;
5818 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005820 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005822 l->lv_idx_item = NULL;
5823 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005824 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005825 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005826 }
5827 return OK;
5828}
5829
5830/*
5831 * Extend "l1" with "l2".
5832 * If "bef" is NULL append at the end, otherwise insert before this item.
5833 * Returns FAIL when out of memory.
5834 */
5835 static int
5836list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l1;
5838 list_T *l2;
5839 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840{
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842
5843 for (item = l2->lv_first; item != NULL; item = item->li_next)
5844 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5845 return FAIL;
5846 return OK;
5847}
5848
5849/*
5850 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5851 * Return FAIL when out of memory.
5852 */
5853 static int
5854list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 list_T *l1;
5856 list_T *l2;
5857 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858{
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005860
5861 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863 if (l == NULL)
5864 return FAIL;
5865 tv->v_type = VAR_LIST;
5866 tv->vval.v_list = l;
5867
5868 /* append all items from the second list */
5869 return list_extend(l, l2, NULL);
5870}
5871
5872/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005873 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005875 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 * Returns NULL when out of memory.
5877 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005882 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883{
Bram Moolenaar33570922005-01-25 22:26:29 +00005884 list_T *copy;
5885 listitem_T *item;
5886 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887
5888 if (orig == NULL)
5889 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890
5891 copy = list_alloc();
5892 if (copy != NULL)
5893 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 if (copyID != 0)
5895 {
5896 /* Do this before adding the items, because one of the items may
5897 * refer back to this list. */
5898 orig->lv_copyID = copyID;
5899 orig->lv_copylist = copy;
5900 }
5901 for (item = orig->lv_first; item != NULL && !got_int;
5902 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 {
5904 ni = listitem_alloc();
5905 if (ni == NULL)
5906 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005907 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005908 {
5909 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5910 {
5911 vim_free(ni);
5912 break;
5913 }
5914 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 list_append(copy, ni);
5918 }
5919 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005920 if (item != NULL)
5921 {
5922 list_unref(copy);
5923 copy = NULL;
5924 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 }
5926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 return copy;
5928}
5929
5930/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005932 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005934 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005935list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
5937 listitem_T *item;
5938 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005939{
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005942 /* notify watchers */
5943 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005945 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005946 list_fix_watch(l, ip);
5947 if (ip == item2)
5948 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005950
5951 if (item2->li_next == NULL)
5952 l->lv_last = item->li_prev;
5953 else
5954 item2->li_next->li_prev = item->li_prev;
5955 if (item->li_prev == NULL)
5956 l->lv_first = item2->li_next;
5957 else
5958 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005959 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960}
5961
5962/*
5963 * Return an allocated string with the string representation of a list.
5964 * May return NULL.
5965 */
5966 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005967list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005968 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005969 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970{
5971 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972
5973 if (tv->vval.v_list == NULL)
5974 return NULL;
5975 ga_init2(&ga, (int)sizeof(char), 80);
5976 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005977 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005978 {
5979 vim_free(ga.ga_data);
5980 return NULL;
5981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 ga_append(&ga, ']');
5983 ga_append(&ga, NUL);
5984 return (char_u *)ga.ga_data;
5985}
5986
5987/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005988 * Join list "l" into a string in "*gap", using separator "sep".
5989 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005993list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005994 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996 char_u *sep;
5997 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005998 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005999{
6000 int first = TRUE;
6001 char_u *tofree;
6002 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006004 char_u *s;
6005
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006006 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006007 {
6008 if (first)
6009 first = FALSE;
6010 else
6011 ga_concat(gap, sep);
6012
6013 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 if (s != NULL)
6018 ga_concat(gap, s);
6019 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 if (s == NULL)
6021 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006022 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006024}
6025
6026/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027 * Garbage collection for lists and dictionaries.
6028 *
6029 * We use reference counts to be able to free most items right away when they
6030 * are no longer used. But for composite items it's possible that it becomes
6031 * unused while the reference count is > 0: When there is a recursive
6032 * reference. Example:
6033 * :let l = [1, 2, 3]
6034 * :let d = {9: l}
6035 * :let l[1] = d
6036 *
6037 * Since this is quite unusual we handle this with garbage collection: every
6038 * once in a while find out which lists and dicts are not referenced from any
6039 * variable.
6040 *
6041 * Here is a good reference text about garbage collection (refers to Python
6042 * but it applies to all reference-counting mechanisms):
6043 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045
6046/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 * Do garbage collection for lists and dicts.
6048 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006050 int
6051garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006052{
6053 dict_T *dd;
6054 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 int copyID = ++current_copyID;
6056 buf_T *buf;
6057 win_T *wp;
6058 int i;
6059 funccall_T *fc;
6060 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006061#ifdef FEAT_WINDOWS
6062 tabpage_T *tp;
6063#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064
6065 /*
6066 * 1. Go through all accessible variables and mark all lists and dicts
6067 * with copyID.
6068 */
6069 /* script-local variables */
6070 for (i = 1; i <= ga_scripts.ga_len; ++i)
6071 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6072
6073 /* buffer-local variables */
6074 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6075 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6076
6077 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006078 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006079 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6080
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006081#ifdef FEAT_WINDOWS
6082 /* tabpage-local variables */
6083 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6084 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6085#endif
6086
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006087 /* global variables */
6088 set_ref_in_ht(&globvarht, copyID);
6089
6090 /* function-local variables */
6091 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6092 {
6093 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6094 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6095 }
6096
6097 /*
6098 * 2. Go through the list of dicts and free items without the copyID.
6099 */
6100 for (dd = first_dict; dd != NULL; )
6101 if (dd->dv_copyID != copyID)
6102 {
6103 dict_free(dd);
6104 did_free = TRUE;
6105
6106 /* restart, next dict may also have been freed */
6107 dd = first_dict;
6108 }
6109 else
6110 dd = dd->dv_used_next;
6111
6112 /*
6113 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006114 * But don't free a list that has a watcher (used in a for loop), these
6115 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 */
6117 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006118 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 {
6120 list_free(ll);
6121 did_free = TRUE;
6122
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006123 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006124 ll = first_list;
6125 }
6126 else
6127 ll = ll->lv_used_next;
6128
6129 return did_free;
6130}
6131
6132/*
6133 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6134 */
6135 static void
6136set_ref_in_ht(ht, copyID)
6137 hashtab_T *ht;
6138 int copyID;
6139{
6140 int todo;
6141 hashitem_T *hi;
6142
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006143 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006144 for (hi = ht->ht_array; todo > 0; ++hi)
6145 if (!HASHITEM_EMPTY(hi))
6146 {
6147 --todo;
6148 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6149 }
6150}
6151
6152/*
6153 * Mark all lists and dicts referenced through list "l" with "copyID".
6154 */
6155 static void
6156set_ref_in_list(l, copyID)
6157 list_T *l;
6158 int copyID;
6159{
6160 listitem_T *li;
6161
6162 for (li = l->lv_first; li != NULL; li = li->li_next)
6163 set_ref_in_item(&li->li_tv, copyID);
6164}
6165
6166/*
6167 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6168 */
6169 static void
6170set_ref_in_item(tv, copyID)
6171 typval_T *tv;
6172 int copyID;
6173{
6174 dict_T *dd;
6175 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006176
6177 switch (tv->v_type)
6178 {
6179 case VAR_DICT:
6180 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006181 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006182 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006183 /* Didn't see this dict yet. */
6184 dd->dv_copyID = copyID;
6185 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006186 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006187 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006188
6189 case VAR_LIST:
6190 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006192 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006193 /* Didn't see this list yet. */
6194 ll->lv_copyID = copyID;
6195 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006196 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006197 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006198 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006200}
6201
6202/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006203 * Allocate an empty header for a dictionary.
6204 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006205 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006206dict_alloc()
6207{
Bram Moolenaar33570922005-01-25 22:26:29 +00006208 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006209
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006212 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006213 /* Add the list to the hashtable for garbage collection. */
6214 if (first_dict != NULL)
6215 first_dict->dv_used_prev = d;
6216 d->dv_used_next = first_dict;
6217 d->dv_used_prev = NULL;
6218
Bram Moolenaar33570922005-01-25 22:26:29 +00006219 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006220 d->dv_lock = 0;
6221 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006222 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225}
6226
6227/*
6228 * Unreference a Dictionary: decrement the reference count and free it when it
6229 * becomes zero.
6230 */
6231 static void
6232dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006235 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6236 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006237}
6238
6239/*
6240 * Free a Dictionary, including all items it contains.
6241 * Ignores the reference count.
6242 */
6243 static void
6244dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006247 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006249 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006250
Bram Moolenaard9fba312005-06-26 22:34:35 +00006251 /* Avoid that recursive reference to the dict frees us again. */
6252 d->dv_refcount = DEL_REFCOUNT;
6253
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006254 /* Remove the dict from the list of dicts for garbage collection. */
6255 if (d->dv_used_prev == NULL)
6256 first_dict = d->dv_used_next;
6257 else
6258 d->dv_used_prev->dv_used_next = d->dv_used_next;
6259 if (d->dv_used_next != NULL)
6260 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6261
6262 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006263 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006264 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006266 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006267 if (!HASHITEM_EMPTY(hi))
6268 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006269 /* Remove the item before deleting it, just in case there is
6270 * something recursive causing trouble. */
6271 di = HI2DI(hi);
6272 hash_remove(&d->dv_hashtab, hi);
6273 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 --todo;
6275 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006278 vim_free(d);
6279}
6280
6281/*
6282 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006283 * The "key" is copied to the new item.
6284 * Note that the value of the item "di_tv" still needs to be initialized!
6285 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006286 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288dictitem_alloc(key)
6289 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290{
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006293 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006294 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006297 di->di_flags = 0;
6298 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300}
6301
6302/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 * Make a copy of a Dictionary item.
6304 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308{
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006311 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6312 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006313 if (di != NULL)
6314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006315 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006316 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006317 copy_tv(&org->di_tv, &di->di_tv);
6318 }
6319 return di;
6320}
6321
6322/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323 * Remove item "item" from Dictionary "dict" and free it.
6324 */
6325 static void
6326dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 dict_T *dict;
6328 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006329{
Bram Moolenaar33570922005-01-25 22:26:29 +00006330 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006331
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006333 if (HASHITEM_EMPTY(hi))
6334 EMSG2(_(e_intern2), "dictitem_remove()");
6335 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006337 dictitem_free(item);
6338}
6339
6340/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341 * Free a dict item. Also clears the value.
6342 */
6343 static void
6344dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 clear_tv(&item->di_tv);
6348 vim_free(item);
6349}
6350
6351/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6353 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006354 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 * Returns NULL when out of memory.
6356 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006361 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006362{
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 dict_T *copy;
6364 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006367
6368 if (orig == NULL)
6369 return NULL;
6370
6371 copy = dict_alloc();
6372 if (copy != NULL)
6373 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006374 if (copyID != 0)
6375 {
6376 orig->dv_copyID = copyID;
6377 orig->dv_copydict = copy;
6378 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006379 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006384 --todo;
6385
6386 di = dictitem_alloc(hi->hi_key);
6387 if (di == NULL)
6388 break;
6389 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006390 {
6391 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6392 copyID) == FAIL)
6393 {
6394 vim_free(di);
6395 break;
6396 }
6397 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398 else
6399 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6400 if (dict_add(copy, di) == FAIL)
6401 {
6402 dictitem_free(di);
6403 break;
6404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006405 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006406 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407
Bram Moolenaare9a41262005-01-15 22:18:47 +00006408 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006409 if (todo > 0)
6410 {
6411 dict_unref(copy);
6412 copy = NULL;
6413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414 }
6415
6416 return copy;
6417}
6418
6419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006421 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006423 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 dict_T *d;
6426 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427{
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429}
6430
Bram Moolenaar8c711452005-01-14 21:53:12 +00006431/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 * Add a number or string entry to dictionary "d".
6433 * When "str" is NULL use number "nr", otherwise use "str".
6434 * Returns FAIL when out of memory and when key already exists.
6435 */
6436 int
6437dict_add_nr_str(d, key, nr, str)
6438 dict_T *d;
6439 char *key;
6440 long nr;
6441 char_u *str;
6442{
6443 dictitem_T *item;
6444
6445 item = dictitem_alloc((char_u *)key);
6446 if (item == NULL)
6447 return FAIL;
6448 item->di_tv.v_lock = 0;
6449 if (str == NULL)
6450 {
6451 item->di_tv.v_type = VAR_NUMBER;
6452 item->di_tv.vval.v_number = nr;
6453 }
6454 else
6455 {
6456 item->di_tv.v_type = VAR_STRING;
6457 item->di_tv.vval.v_string = vim_strsave(str);
6458 }
6459 if (dict_add(d, item) == FAIL)
6460 {
6461 dictitem_free(item);
6462 return FAIL;
6463 }
6464 return OK;
6465}
6466
6467/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 * Get the number of items in a Dictionary.
6469 */
6470 static long
6471dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474 if (d == NULL)
6475 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006476 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477}
6478
6479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480 * Find item "key[len]" in Dictionary "d".
6481 * If "len" is negative use strlen(key).
6482 * Returns NULL when not found.
6483 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006485dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 char_u *key;
6488 int len;
6489{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490#define AKEYLEN 200
6491 char_u buf[AKEYLEN];
6492 char_u *akey;
6493 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 if (len < 0)
6497 akey = key;
6498 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 tofree = akey = vim_strnsave(key, len);
6501 if (akey == NULL)
6502 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006503 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006504 else
6505 {
6506 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006507 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006508 akey = buf;
6509 }
6510
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006512 vim_free(tofree);
6513 if (HASHITEM_EMPTY(hi))
6514 return NULL;
6515 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006516}
6517
6518/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006519 * Get a string item from a dictionary.
6520 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006521 * Returns NULL if the entry doesn't exist or out of memory.
6522 */
6523 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006524get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006525 dict_T *d;
6526 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006527 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006528{
6529 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006530 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006531
6532 di = dict_find(d, key, -1);
6533 if (di == NULL)
6534 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006535 s = get_tv_string(&di->di_tv);
6536 if (save && s != NULL)
6537 s = vim_strsave(s);
6538 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006539}
6540
6541/*
6542 * Get a number item from a dictionary.
6543 * Returns 0 if the entry doesn't exist or out of memory.
6544 */
6545 long
6546get_dict_number(d, key)
6547 dict_T *d;
6548 char_u *key;
6549{
6550 dictitem_T *di;
6551
6552 di = dict_find(d, key, -1);
6553 if (di == NULL)
6554 return 0;
6555 return get_tv_number(&di->di_tv);
6556}
6557
6558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 * Return an allocated string with the string representation of a Dictionary.
6560 * May return NULL.
6561 */
6562 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006563dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006565 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566{
6567 garray_T ga;
6568 int first = TRUE;
6569 char_u *tofree;
6570 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006574 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006577 return NULL;
6578 ga_init2(&ga, (int)sizeof(char), 80);
6579 ga_append(&ga, '{');
6580
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006581 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006582 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006584 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 --todo;
6587
6588 if (first)
6589 first = FALSE;
6590 else
6591 ga_concat(&ga, (char_u *)", ");
6592
6593 tofree = string_quote(hi->hi_key, FALSE);
6594 if (tofree != NULL)
6595 {
6596 ga_concat(&ga, tofree);
6597 vim_free(tofree);
6598 }
6599 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006600 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006601 if (s != NULL)
6602 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 if (s == NULL)
6605 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 if (todo > 0)
6609 {
6610 vim_free(ga.ga_data);
6611 return NULL;
6612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613
6614 ga_append(&ga, '}');
6615 ga_append(&ga, NUL);
6616 return (char_u *)ga.ga_data;
6617}
6618
6619/*
6620 * Allocate a variable for a Dictionary and fill it from "*arg".
6621 * Return OK or FAIL. Returns NOTDONE for {expr}.
6622 */
6623 static int
6624get_dict_tv(arg, rettv, evaluate)
6625 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627 int evaluate;
6628{
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 dict_T *d = NULL;
6630 typval_T tvkey;
6631 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006632 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006635 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636
6637 /*
6638 * First check if it's not a curly-braces thing: {expr}.
6639 * Must do this without evaluating, otherwise a function may be called
6640 * twice. Unfortunately this means we need to call eval1() twice for the
6641 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644 if (*start != '}')
6645 {
6646 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6647 return FAIL;
6648 if (*start == '}')
6649 return NOTDONE;
6650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006651
6652 if (evaluate)
6653 {
6654 d = dict_alloc();
6655 if (d == NULL)
6656 return FAIL;
6657 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006658 tvkey.v_type = VAR_UNKNOWN;
6659 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660
6661 *arg = skipwhite(*arg + 1);
6662 while (**arg != '}' && **arg != NUL)
6663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006664 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665 goto failret;
6666 if (**arg != ':')
6667 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006668 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006669 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006670 goto failret;
6671 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006672 key = get_tv_string_buf_chk(&tvkey, buf);
6673 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006675 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6676 if (key != NULL)
6677 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006678 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 goto failret;
6680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006681
6682 *arg = skipwhite(*arg + 1);
6683 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6684 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006685 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 goto failret;
6687 }
6688 if (evaluate)
6689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006690 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691 if (item != NULL)
6692 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006693 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006694 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006695 clear_tv(&tv);
6696 goto failret;
6697 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006698 item = dictitem_alloc(key);
6699 clear_tv(&tvkey);
6700 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006702 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006703 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006704 if (dict_add(d, item) == FAIL)
6705 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706 }
6707 }
6708
6709 if (**arg == '}')
6710 break;
6711 if (**arg != ',')
6712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006713 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714 goto failret;
6715 }
6716 *arg = skipwhite(*arg + 1);
6717 }
6718
6719 if (**arg != '}')
6720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006721 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722failret:
6723 if (evaluate)
6724 dict_free(d);
6725 return FAIL;
6726 }
6727
6728 *arg = skipwhite(*arg + 1);
6729 if (evaluate)
6730 {
6731 rettv->v_type = VAR_DICT;
6732 rettv->vval.v_dict = d;
6733 ++d->dv_refcount;
6734 }
6735
6736 return OK;
6737}
6738
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 * Return a string with the string representation of a variable.
6741 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006744 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006745 * May return NULL;
6746 */
6747 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006748echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006749 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006751 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006752 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006753{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006754 static int recurse = 0;
6755 char_u *r = NULL;
6756
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006759 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 *tofree = NULL;
6761 return NULL;
6762 }
6763 ++recurse;
6764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765 switch (tv->v_type)
6766 {
6767 case VAR_FUNC:
6768 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 r = tv->vval.v_string;
6770 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006772 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006773 if (tv->vval.v_list == NULL)
6774 {
6775 *tofree = NULL;
6776 r = NULL;
6777 }
6778 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6779 {
6780 *tofree = NULL;
6781 r = (char_u *)"[...]";
6782 }
6783 else
6784 {
6785 tv->vval.v_list->lv_copyID = copyID;
6786 *tofree = list2string(tv, copyID);
6787 r = *tofree;
6788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006789 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006790
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006792 if (tv->vval.v_dict == NULL)
6793 {
6794 *tofree = NULL;
6795 r = NULL;
6796 }
6797 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6798 {
6799 *tofree = NULL;
6800 r = (char_u *)"{...}";
6801 }
6802 else
6803 {
6804 tv->vval.v_dict->dv_copyID = copyID;
6805 *tofree = dict2string(tv, copyID);
6806 r = *tofree;
6807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 case VAR_STRING:
6811 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 *tofree = NULL;
6813 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006816 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006820
6821 --recurse;
6822 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823}
6824
6825/*
6826 * Return a string with the string representation of a variable.
6827 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6828 * "numbuf" is used for a number.
6829 * Puts quotes around strings, so that they can be parsed back by eval().
6830 * May return NULL;
6831 */
6832 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006833tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835 char_u **tofree;
6836 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006837 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838{
6839 switch (tv->v_type)
6840 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006841 case VAR_FUNC:
6842 *tofree = string_quote(tv->vval.v_string, TRUE);
6843 return *tofree;
6844 case VAR_STRING:
6845 *tofree = string_quote(tv->vval.v_string, FALSE);
6846 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006849 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006851 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006853 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006854 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855}
6856
6857/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 * Return string "str" in ' quotes, doubling ' characters.
6859 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006860 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006861 */
6862 static char_u *
6863string_quote(str, function)
6864 char_u *str;
6865 int function;
6866{
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006868 char_u *p, *r, *s;
6869
Bram Moolenaar33570922005-01-25 22:26:29 +00006870 len = (function ? 13 : 3);
6871 if (str != NULL)
6872 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 for (p = str; *p != NUL; mb_ptr_adv(p))
6875 if (*p == '\'')
6876 ++len;
6877 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006878 s = r = alloc(len);
6879 if (r != NULL)
6880 {
6881 if (function)
6882 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006883 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006884 r += 10;
6885 }
6886 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006887 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006888 if (str != NULL)
6889 for (p = str; *p != NUL; )
6890 {
6891 if (*p == '\'')
6892 *r++ = '\'';
6893 MB_COPY_CHAR(p, r);
6894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 if (function)
6897 *r++ = ')';
6898 *r++ = NUL;
6899 }
6900 return s;
6901}
6902
6903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 * Get the value of an environment variable.
6905 * "arg" is pointing to the '$'. It is advanced to after the name.
6906 * If the environment variable was not set, silently assume it is empty.
6907 * Always return OK.
6908 */
6909 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 int evaluate;
6914{
6915 char_u *string = NULL;
6916 int len;
6917 int cc;
6918 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006919 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 ++*arg;
6922 name = *arg;
6923 len = get_env_len(arg);
6924 if (evaluate)
6925 {
6926 if (len != 0)
6927 {
6928 cc = name[len];
6929 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006930 /* first try vim_getenv(), fast for normal environment vars */
6931 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006933 {
6934 if (!mustfree)
6935 string = vim_strsave(string);
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 else
6938 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006939 if (mustfree)
6940 vim_free(string);
6941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 /* next try expanding things like $VIM and ${HOME} */
6943 string = expand_env_save(name - 1);
6944 if (string != NULL && *string == '$')
6945 {
6946 vim_free(string);
6947 string = NULL;
6948 }
6949 }
6950 name[len] = cc;
6951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006952 rettv->v_type = VAR_STRING;
6953 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955
6956 return OK;
6957}
6958
6959/*
6960 * Array with names and number of arguments of all internal functions
6961 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6962 */
6963static struct fst
6964{
6965 char *f_name; /* function name */
6966 char f_min_argc; /* minimal number of arguments */
6967 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006969 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970} functions[] =
6971{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006972 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {"append", 2, 2, f_append},
6974 {"argc", 0, 0, f_argc},
6975 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006976 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006978 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"bufexists", 1, 1, f_bufexists},
6980 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6981 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6982 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6983 {"buflisted", 1, 1, f_buflisted},
6984 {"bufloaded", 1, 1, f_bufloaded},
6985 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006986 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 {"bufwinnr", 1, 1, f_bufwinnr},
6988 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006989 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006990 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006991 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {"char2nr", 1, 1, f_char2nr},
6993 {"cindent", 1, 1, f_cindent},
6994 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006995#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006996 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006997 {"complete_add", 1, 1, f_complete_add},
6998 {"complete_check", 0, 0, f_complete_check},
6999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007004 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007005 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {"delete", 1, 1, f_delete},
7007 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007008 {"diff_filler", 1, 1, f_diff_filler},
7009 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007010 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007012 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {"eventhandler", 0, 0, f_eventhandler},
7014 {"executable", 1, 1, f_executable},
7015 {"exists", 1, 1, f_exists},
7016 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007017 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007018 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7020 {"filereadable", 1, 1, f_filereadable},
7021 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007022 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007023 {"finddir", 1, 3, f_finddir},
7024 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {"fnamemodify", 2, 2, f_fnamemodify},
7026 {"foldclosed", 1, 1, f_foldclosed},
7027 {"foldclosedend", 1, 1, f_foldclosedend},
7028 {"foldlevel", 1, 1, f_foldlevel},
7029 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007030 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007032 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007034 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007035 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {"getbufvar", 2, 2, f_getbufvar},
7037 {"getchar", 0, 1, f_getchar},
7038 {"getcharmod", 0, 0, f_getcharmod},
7039 {"getcmdline", 0, 0, f_getcmdline},
7040 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007041 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007043 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007044 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {"getfsize", 1, 1, f_getfsize},
7046 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007047 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007048 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007049 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007050 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007051 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007052 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007054 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"getwinposx", 0, 0, f_getwinposx},
7056 {"getwinposy", 0, 0, f_getwinposy},
7057 {"getwinvar", 2, 2, f_getwinvar},
7058 {"glob", 1, 1, f_glob},
7059 {"globpath", 2, 2, f_globpath},
7060 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007061 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007062 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7064 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7065 {"histadd", 2, 2, f_histadd},
7066 {"histdel", 1, 2, f_histdel},
7067 {"histget", 1, 2, f_histget},
7068 {"histnr", 1, 1, f_histnr},
7069 {"hlID", 1, 1, f_hlID},
7070 {"hlexists", 1, 1, f_hlexists},
7071 {"hostname", 0, 0, f_hostname},
7072 {"iconv", 3, 3, f_iconv},
7073 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007074 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007075 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007077 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"inputrestore", 0, 0, f_inputrestore},
7079 {"inputsave", 0, 0, f_inputsave},
7080 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007081 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007085 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007088 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"libcall", 3, 3, f_libcall},
7090 {"libcallnr", 3, 3, f_libcallnr},
7091 {"line", 1, 1, f_line},
7092 {"line2byte", 1, 1, f_line2byte},
7093 {"lispindent", 1, 1, f_lispindent},
7094 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007095 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007096 {"maparg", 1, 3, f_maparg},
7097 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007098 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007099 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007100 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007101 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007102 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007103 {"max", 1, 1, f_max},
7104 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007105#ifdef vim_mkdir
7106 {"mkdir", 1, 3, f_mkdir},
7107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"mode", 0, 0, f_mode},
7109 {"nextnonblank", 1, 1, f_nextnonblank},
7110 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007111 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007113 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007114 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007116 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007117 {"reltime", 0, 2, f_reltime},
7118 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {"remote_expr", 2, 3, f_remote_expr},
7120 {"remote_foreground", 1, 1, f_remote_foreground},
7121 {"remote_peek", 1, 2, f_remote_peek},
7122 {"remote_read", 1, 1, f_remote_read},
7123 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007124 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007126 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007128 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007129 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007130 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007131 {"searchpair", 3, 6, f_searchpair},
7132 {"searchpairpos", 3, 6, f_searchpairpos},
7133 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {"server2client", 2, 2, f_server2client},
7135 {"serverlist", 0, 0, f_serverlist},
7136 {"setbufvar", 3, 3, f_setbufvar},
7137 {"setcmdpos", 1, 1, f_setcmdpos},
7138 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007139 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007140 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007141 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007143 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {"setwinvar", 3, 3, f_setwinvar},
7145 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007146 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007147 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007148 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007149 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007150 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007151 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152#ifdef HAVE_STRFTIME
7153 {"strftime", 1, 2, f_strftime},
7154#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {"strlen", 1, 1, f_strlen},
7158 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007159 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"strtrans", 1, 1, f_strtrans},
7161 {"submatch", 1, 1, f_submatch},
7162 {"substitute", 4, 4, f_substitute},
7163 {"synID", 3, 3, f_synID},
7164 {"synIDattr", 2, 3, f_synIDattr},
7165 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007166 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007168 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007170 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007171 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007173 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {"tolower", 1, 1, f_tolower},
7175 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007176 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {"virtcol", 1, 1, f_virtcol},
7180 {"visualmode", 0, 1, f_visualmode},
7181 {"winbufnr", 1, 1, f_winbufnr},
7182 {"wincol", 0, 0, f_wincol},
7183 {"winheight", 1, 1, f_winheight},
7184 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007185 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007187 {"winrestview", 1, 1, f_winrestview},
7188 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007190 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191};
7192
7193#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7194
7195/*
7196 * Function given to ExpandGeneric() to obtain the list of internal
7197 * or user defined function names.
7198 */
7199 char_u *
7200get_function_name(xp, idx)
7201 expand_T *xp;
7202 int idx;
7203{
7204 static int intidx = -1;
7205 char_u *name;
7206
7207 if (idx == 0)
7208 intidx = -1;
7209 if (intidx < 0)
7210 {
7211 name = get_user_func_name(xp, idx);
7212 if (name != NULL)
7213 return name;
7214 }
7215 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7216 {
7217 STRCPY(IObuff, functions[intidx].f_name);
7218 STRCAT(IObuff, "(");
7219 if (functions[intidx].f_max_argc == 0)
7220 STRCAT(IObuff, ")");
7221 return IObuff;
7222 }
7223
7224 return NULL;
7225}
7226
7227/*
7228 * Function given to ExpandGeneric() to obtain the list of internal or
7229 * user defined variable or function names.
7230 */
7231/*ARGSUSED*/
7232 char_u *
7233get_expr_name(xp, idx)
7234 expand_T *xp;
7235 int idx;
7236{
7237 static int intidx = -1;
7238 char_u *name;
7239
7240 if (idx == 0)
7241 intidx = -1;
7242 if (intidx < 0)
7243 {
7244 name = get_function_name(xp, idx);
7245 if (name != NULL)
7246 return name;
7247 }
7248 return get_user_var_name(xp, ++intidx);
7249}
7250
7251#endif /* FEAT_CMDL_COMPL */
7252
7253/*
7254 * Find internal function in table above.
7255 * Return index, or -1 if not found
7256 */
7257 static int
7258find_internal_func(name)
7259 char_u *name; /* name of the function */
7260{
7261 int first = 0;
7262 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7263 int cmp;
7264 int x;
7265
7266 /*
7267 * Find the function name in the table. Binary search.
7268 */
7269 while (first <= last)
7270 {
7271 x = first + ((unsigned)(last - first) >> 1);
7272 cmp = STRCMP(name, functions[x].f_name);
7273 if (cmp < 0)
7274 last = x - 1;
7275 else if (cmp > 0)
7276 first = x + 1;
7277 else
7278 return x;
7279 }
7280 return -1;
7281}
7282
7283/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7285 * name it contains, otherwise return "name".
7286 */
7287 static char_u *
7288deref_func_name(name, lenp)
7289 char_u *name;
7290 int *lenp;
7291{
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007293 int cc;
7294
7295 cc = name[*lenp];
7296 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007297 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007300 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302 {
7303 *lenp = 0;
7304 return (char_u *)""; /* just in case */
7305 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007306 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007308 }
7309
7310 return name;
7311}
7312
7313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 * Allocate a variable for the result of a function.
7315 * Return OK or FAIL.
7316 */
7317 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7319 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 char_u *name; /* name of the function */
7321 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 char_u **arg; /* argument, pointing to the '(' */
7324 linenr_T firstline; /* first line of range */
7325 linenr_T lastline; /* last line of range */
7326 int *doesrange; /* return: function handled range */
7327 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 char_u *argp;
7331 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007332 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 int argcount = 0; /* number of arguments found */
7334
7335 /*
7336 * Get the arguments.
7337 */
7338 argp = *arg;
7339 while (argcount < MAX_FUNC_ARGS)
7340 {
7341 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7342 if (*argp == ')' || *argp == ',' || *argp == NUL)
7343 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7345 {
7346 ret = FAIL;
7347 break;
7348 }
7349 ++argcount;
7350 if (*argp != ',')
7351 break;
7352 }
7353 if (*argp == ')')
7354 ++argp;
7355 else
7356 ret = FAIL;
7357
7358 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 {
7363 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
7369 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007370 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
7372 *arg = skipwhite(argp);
7373 return ret;
7374}
7375
7376
7377/*
7378 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007379 * Return OK when the function can't be called, FAIL otherwise.
7380 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 */
7382 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 char_u *name; /* name of the function */
7386 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007389 typval_T *argvars; /* vars for arguments, must have "argcount"
7390 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 linenr_T firstline; /* first line of range */
7392 linenr_T lastline; /* last line of range */
7393 int *doesrange; /* return: function handled range */
7394 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398#define ERROR_UNKNOWN 0
7399#define ERROR_TOOMANY 1
7400#define ERROR_TOOFEW 2
7401#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402#define ERROR_DICT 4
7403#define ERROR_NONE 5
7404#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 int error = ERROR_NONE;
7406 int i;
7407 int llen;
7408 ufunc_T *fp;
7409 int cc;
7410#define FLEN_FIXED 40
7411 char_u fname_buf[FLEN_FIXED + 1];
7412 char_u *fname;
7413
7414 /*
7415 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7416 * Change <SNR>123_name() to K_SNR 123_name().
7417 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7418 */
7419 cc = name[len];
7420 name[len] = NUL;
7421 llen = eval_fname_script(name);
7422 if (llen > 0)
7423 {
7424 fname_buf[0] = K_SPECIAL;
7425 fname_buf[1] = KS_EXTRA;
7426 fname_buf[2] = (int)KE_SNR;
7427 i = 3;
7428 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7429 {
7430 if (current_SID <= 0)
7431 error = ERROR_SCRIPT;
7432 else
7433 {
7434 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7435 i = (int)STRLEN(fname_buf);
7436 }
7437 }
7438 if (i + STRLEN(name + llen) < FLEN_FIXED)
7439 {
7440 STRCPY(fname_buf + i, name + llen);
7441 fname = fname_buf;
7442 }
7443 else
7444 {
7445 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7446 if (fname == NULL)
7447 error = ERROR_OTHER;
7448 else
7449 {
7450 mch_memmove(fname, fname_buf, (size_t)i);
7451 STRCPY(fname + i, name + llen);
7452 }
7453 }
7454 }
7455 else
7456 fname = name;
7457
7458 *doesrange = FALSE;
7459
7460
7461 /* execute the function if no errors detected and executing */
7462 if (evaluate && error == ERROR_NONE)
7463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 error = ERROR_UNKNOWN;
7466
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007467 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
7469 /*
7470 * User defined function.
7471 */
7472 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007473
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007475 /* Trigger FuncUndefined event, may load the function. */
7476 if (fp == NULL
7477 && apply_autocmds(EVENT_FUNCUNDEFINED,
7478 fname, fname, TRUE, NULL)
7479 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007481 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 fp = find_func(fname);
7483 }
7484#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007485 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007486 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007487 {
7488 /* loaded a package, search for the function again */
7489 fp = find_func(fname);
7490 }
7491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (fp != NULL)
7493 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007494 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007496 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007498 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007500 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 else
7503 {
7504 /*
7505 * Call the user function.
7506 * Save and restore search patterns, script variables and
7507 * redo buffer.
7508 */
7509 save_search_patterns();
7510 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007511 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007513 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007514 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7515 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7516 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007517 /* Function was unreferenced while being used, free it
7518 * now. */
7519 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 restoreRedobuff();
7521 restore_search_patterns();
7522 error = ERROR_NONE;
7523 }
7524 }
7525 }
7526 else
7527 {
7528 /*
7529 * Find the function name in the table, call its implementation.
7530 */
7531 i = find_internal_func(fname);
7532 if (i >= 0)
7533 {
7534 if (argcount < functions[i].f_min_argc)
7535 error = ERROR_TOOFEW;
7536 else if (argcount > functions[i].f_max_argc)
7537 error = ERROR_TOOMANY;
7538 else
7539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 error = ERROR_NONE;
7543 }
7544 }
7545 }
7546 /*
7547 * The function call (or "FuncUndefined" autocommand sequence) might
7548 * have been aborted by an error, an interrupt, or an explicitly thrown
7549 * exception that has not been caught so far. This situation can be
7550 * tested for by calling aborting(). For an error in an internal
7551 * function or for the "E132" error in call_user_func(), however, the
7552 * throw point at which the "force_abort" flag (temporarily reset by
7553 * emsg()) is normally updated has not been reached yet. We need to
7554 * update that flag first to make aborting() reliable.
7555 */
7556 update_force_abort();
7557 }
7558 if (error == ERROR_NONE)
7559 ret = OK;
7560
7561 /*
7562 * Report an error unless the argument evaluation or function call has been
7563 * cancelled due to an aborting error, an interrupt, or an exception.
7564 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 if (!aborting())
7566 {
7567 switch (error)
7568 {
7569 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007570 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 break;
7572 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007573 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 break;
7575 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007576 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 name);
7578 break;
7579 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 name);
7582 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007584 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 name);
7586 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 }
7588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
7590 name[len] = cc;
7591 if (fname != name && fname != fname_buf)
7592 vim_free(fname);
7593
7594 return ret;
7595}
7596
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007597/*
7598 * Give an error message with a function name. Handle <SNR> things.
7599 */
7600 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007601emsg_funcname(ermsg, name)
7602 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 char_u *name;
7604{
7605 char_u *p;
7606
7607 if (*name == K_SPECIAL)
7608 p = concat_str((char_u *)"<SNR>", name + 3);
7609 else
7610 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007611 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007612 if (p != name)
7613 vim_free(p);
7614}
7615
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616/*********************************************
7617 * Implementation of the built-in functions
7618 */
7619
7620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007621 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 */
7623 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007624f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 typval_T *argvars;
7626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007633 if ((l = argvars[0].vval.v_list) != NULL
7634 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7635 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007637 }
7638 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007639 EMSG(_(e_listreq));
7640}
7641
7642/*
7643 * "append(lnum, string/list)" function
7644 */
7645 static void
7646f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 typval_T *argvars;
7648 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007649{
7650 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007651 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 list_T *l = NULL;
7653 listitem_T *li = NULL;
7654 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007655 long added = 0;
7656
Bram Moolenaar0d660222005-01-07 21:51:51 +00007657 lnum = get_tv_lnum(argvars);
7658 if (lnum >= 0
7659 && lnum <= curbuf->b_ml.ml_line_count
7660 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007661 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007662 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007664 l = argvars[1].vval.v_list;
7665 if (l == NULL)
7666 return;
7667 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007669 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007670 for (;;)
7671 {
7672 if (l == NULL)
7673 tv = &argvars[1]; /* append a string */
7674 else if (li == NULL)
7675 break; /* end of list */
7676 else
7677 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 line = get_tv_string_chk(tv);
7679 if (line == NULL) /* type error */
7680 {
7681 rettv->vval.v_number = 1; /* Failed */
7682 break;
7683 }
7684 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007685 ++added;
7686 if (l == NULL)
7687 break;
7688 li = li->li_next;
7689 }
7690
7691 appended_lines_mark(lnum, added);
7692 if (curwin->w_cursor.lnum > lnum)
7693 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007695 else
7696 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697}
7698
7699/*
7700 * "argc()" function
7701 */
7702/* ARGSUSED */
7703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007704f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 typval_T *argvars;
7706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007708 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709}
7710
7711/*
7712 * "argidx()" function
7713 */
7714/* ARGSUSED */
7715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 typval_T *argvars;
7718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721}
7722
7723/*
7724 * "argv(nr)" function
7725 */
7726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 typval_T *argvars;
7729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 int idx;
7732
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007733 if (argvars[0].v_type != VAR_UNKNOWN)
7734 {
7735 idx = get_tv_number_chk(&argvars[0], NULL);
7736 if (idx >= 0 && idx < ARGCOUNT)
7737 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7738 else
7739 rettv->vval.v_string = NULL;
7740 rettv->v_type = VAR_STRING;
7741 }
7742 else if (rettv_list_alloc(rettv) == OK)
7743 for (idx = 0; idx < ARGCOUNT; ++idx)
7744 list_append_string(rettv->vval.v_list,
7745 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746}
7747
7748/*
7749 * "browse(save, title, initdir, default)" function
7750 */
7751/* ARGSUSED */
7752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 typval_T *argvars;
7755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757#ifdef FEAT_BROWSE
7758 int save;
7759 char_u *title;
7760 char_u *initdir;
7761 char_u *defname;
7762 char_u buf[NUMBUFLEN];
7763 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007764 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 save = get_tv_number_chk(&argvars[0], &error);
7767 title = get_tv_string_chk(&argvars[1]);
7768 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7769 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007771 if (error || title == NULL || initdir == NULL || defname == NULL)
7772 rettv->vval.v_string = NULL;
7773 else
7774 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007775 do_browse(save ? BROWSE_SAVE : 0,
7776 title, defname, NULL, initdir, NULL, curbuf);
7777#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007779#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007781}
7782
7783/*
7784 * "browsedir(title, initdir)" function
7785 */
7786/* ARGSUSED */
7787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007791{
7792#ifdef FEAT_BROWSE
7793 char_u *title;
7794 char_u *initdir;
7795 char_u buf[NUMBUFLEN];
7796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007797 title = get_tv_string_chk(&argvars[0]);
7798 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007800 if (title == NULL || initdir == NULL)
7801 rettv->vval.v_string = NULL;
7802 else
7803 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007804 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810
Bram Moolenaar33570922005-01-25 22:26:29 +00007811static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813/*
7814 * Find a buffer by number or exact name.
7815 */
7816 static buf_T *
7817find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822 if (avar->v_type == VAR_NUMBER)
7823 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007824 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007827 if (buf == NULL)
7828 {
7829 /* No full path name match, try a match with a URL or a "nofile"
7830 * buffer, these don't use the full path. */
7831 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7832 if (buf->b_fname != NULL
7833 && (path_with_url(buf->b_fname)
7834#ifdef FEAT_QUICKFIX
7835 || bt_nofile(buf)
7836#endif
7837 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007839 break;
7840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842 return buf;
7843}
7844
7845/*
7846 * "bufexists(expr)" function
7847 */
7848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 typval_T *argvars;
7851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
7857 * "buflisted(expr)" function
7858 */
7859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 typval_T *argvars;
7862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 buf_T *buf;
7865
7866 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868}
7869
7870/*
7871 * "bufloaded(expr)" function
7872 */
7873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 typval_T *argvars;
7876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 buf_T *buf;
7879
7880 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882}
7883
Bram Moolenaar33570922005-01-25 22:26:29 +00007884static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886/*
7887 * Get buffer by number or pattern.
7888 */
7889 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007893 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 int save_magic;
7895 char_u *save_cpo;
7896 buf_T *buf;
7897
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007898 if (tv->v_type == VAR_NUMBER)
7899 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007900 if (tv->v_type != VAR_STRING)
7901 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 if (name == NULL || *name == NUL)
7903 return curbuf;
7904 if (name[0] == '$' && name[1] == NUL)
7905 return lastbuf;
7906
7907 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7908 save_magic = p_magic;
7909 p_magic = TRUE;
7910 save_cpo = p_cpo;
7911 p_cpo = (char_u *)"";
7912
7913 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7914 TRUE, FALSE));
7915
7916 p_magic = save_magic;
7917 p_cpo = save_cpo;
7918
7919 /* If not found, try expanding the name, like done for bufexists(). */
7920 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923 return buf;
7924}
7925
7926/*
7927 * "bufname(expr)" function
7928 */
7929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 typval_T *argvars;
7932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 buf_T *buf;
7935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007936 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938 buf = get_buf_tv(&argvars[0]);
7939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 --emsg_off;
7945}
7946
7947/*
7948 * "bufnr(expr)" function
7949 */
7950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 typval_T *argvars;
7953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007956 int error = FALSE;
7957 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007962 --emsg_off;
7963
7964 /* If the buffer isn't found and the second argument is not zero create a
7965 * new buffer. */
7966 if (buf == NULL
7967 && argvars[1].v_type != VAR_UNKNOWN
7968 && get_tv_number_chk(&argvars[1], &error) != 0
7969 && !error
7970 && (name = get_tv_string_chk(&argvars[0])) != NULL
7971 && !error)
7972 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978}
7979
7980/*
7981 * "bufwinnr(nr)" function
7982 */
7983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 typval_T *argvars;
7986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988#ifdef FEAT_WINDOWS
7989 win_T *wp;
7990 int winnr = 0;
7991#endif
7992 buf_T *buf;
7993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007994 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007996 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997#ifdef FEAT_WINDOWS
7998 for (wp = firstwin; wp; wp = wp->w_next)
7999 {
8000 ++winnr;
8001 if (wp->w_buffer == buf)
8002 break;
8003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007#endif
8008 --emsg_off;
8009}
8010
8011/*
8012 * "byte2line(byte)" function
8013 */
8014/*ARGSUSED*/
8015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 typval_T *argvars;
8018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022#else
8023 long boff = 0;
8024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008025 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 (linenr_T)0, &boff);
8031#endif
8032}
8033
8034/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008035 * "byteidx()" function
8036 */
8037/*ARGSUSED*/
8038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 typval_T *argvars;
8041 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008042{
8043#ifdef FEAT_MBYTE
8044 char_u *t;
8045#endif
8046 char_u *str;
8047 long idx;
8048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 str = get_tv_string_chk(&argvars[0]);
8050 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008051 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008053 return;
8054
8055#ifdef FEAT_MBYTE
8056 t = str;
8057 for ( ; idx > 0; idx--)
8058 {
8059 if (*t == NUL) /* EOL reached */
8060 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008061 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008062 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008063 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008064#else
8065 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008067#endif
8068}
8069
8070/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008071 * "call(func, arglist)" function
8072 */
8073 static void
8074f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 typval_T *argvars;
8076 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077{
8078 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008079 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008080 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008082 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008084
8085 rettv->vval.v_number = 0;
8086 if (argvars[1].v_type != VAR_LIST)
8087 {
8088 EMSG(_(e_listreq));
8089 return;
8090 }
8091 if (argvars[1].vval.v_list == NULL)
8092 return;
8093
8094 if (argvars[0].v_type == VAR_FUNC)
8095 func = argvars[0].vval.v_string;
8096 else
8097 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008098 if (*func == NUL)
8099 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008100
Bram Moolenaare9a41262005-01-15 22:18:47 +00008101 if (argvars[2].v_type != VAR_UNKNOWN)
8102 {
8103 if (argvars[2].v_type != VAR_DICT)
8104 {
8105 EMSG(_(e_dictreq));
8106 return;
8107 }
8108 selfdict = argvars[2].vval.v_dict;
8109 }
8110
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008111 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8112 item = item->li_next)
8113 {
8114 if (argc == MAX_FUNC_ARGS)
8115 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008116 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008117 break;
8118 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008119 /* Make a copy of each argument. This is needed to be able to set
8120 * v_lock to VAR_FIXED in the copy without changing the original list.
8121 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008122 copy_tv(&item->li_tv, &argv[argc++]);
8123 }
8124
8125 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008126 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008127 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8128 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129
8130 /* Free the arguments. */
8131 while (argc > 0)
8132 clear_tv(&argv[--argc]);
8133}
8134
8135/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008136 * "changenr()" function
8137 */
8138/*ARGSUSED*/
8139 static void
8140f_changenr(argvars, rettv)
8141 typval_T *argvars;
8142 typval_T *rettv;
8143{
8144 rettv->vval.v_number = curbuf->b_u_seq_cur;
8145}
8146
8147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 * "char2nr(string)" function
8149 */
8150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008151f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008152 typval_T *argvars;
8153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155#ifdef FEAT_MBYTE
8156 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008157 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 else
8159#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008160 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161}
8162
8163/*
8164 * "cindent(lnum)" function
8165 */
8166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 typval_T *argvars;
8169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171#ifdef FEAT_CINDENT
8172 pos_T pos;
8173 linenr_T lnum;
8174
8175 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8178 {
8179 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008180 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 curwin->w_cursor = pos;
8182 }
8183 else
8184#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008185 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186}
8187
8188/*
8189 * "col(string)" function
8190 */
8191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 typval_T *argvars;
8194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 colnr_T col = 0;
8197 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008198 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008200 fp = var2fpos(&argvars[0], FALSE, &fnum);
8201 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {
8203 if (fp->col == MAXCOL)
8204 {
8205 /* '> can be MAXCOL, get the length of the line then */
8206 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008207 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 else
8209 col = MAXCOL;
8210 }
8211 else
8212 {
8213 col = fp->col + 1;
8214#ifdef FEAT_VIRTUALEDIT
8215 /* col(".") when the cursor is on the NUL at the end of the line
8216 * because of "coladd" can be seen as an extra column. */
8217 if (virtual_active() && fp == &curwin->w_cursor)
8218 {
8219 char_u *p = ml_get_cursor();
8220
8221 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8222 curwin->w_virtcol - curwin->w_cursor.coladd))
8223 {
8224# ifdef FEAT_MBYTE
8225 int l;
8226
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008227 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 col += l;
8229# else
8230 if (*p != NUL && p[1] == NUL)
8231 ++col;
8232# endif
8233 }
8234 }
8235#endif
8236 }
8237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239}
8240
Bram Moolenaar572cb562005-08-05 21:35:02 +00008241#if defined(FEAT_INS_EXPAND)
8242/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008243 * "complete()" function
8244 */
8245/*ARGSUSED*/
8246 static void
8247f_complete(argvars, rettv)
8248 typval_T *argvars;
8249 typval_T *rettv;
8250{
8251 int startcol;
8252
8253 if ((State & INSERT) == 0)
8254 {
8255 EMSG(_("E785: complete() can only be used in Insert mode"));
8256 return;
8257 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008258
8259 /* Check for undo allowed here, because if something was already inserted
8260 * the line was already saved for undo and this check isn't done. */
8261 if (!undo_allowed())
8262 return;
8263
Bram Moolenaarade00832006-03-10 21:46:58 +00008264 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8265 {
8266 EMSG(_(e_invarg));
8267 return;
8268 }
8269
8270 startcol = get_tv_number_chk(&argvars[0], NULL);
8271 if (startcol <= 0)
8272 return;
8273
8274 set_completion(startcol - 1, argvars[1].vval.v_list);
8275}
8276
8277/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008278 * "complete_add()" function
8279 */
8280/*ARGSUSED*/
8281 static void
8282f_complete_add(argvars, rettv)
8283 typval_T *argvars;
8284 typval_T *rettv;
8285{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008286 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008287}
8288
8289/*
8290 * "complete_check()" function
8291 */
8292/*ARGSUSED*/
8293 static void
8294f_complete_check(argvars, rettv)
8295 typval_T *argvars;
8296 typval_T *rettv;
8297{
8298 int saved = RedrawingDisabled;
8299
8300 RedrawingDisabled = 0;
8301 ins_compl_check_keys(0);
8302 rettv->vval.v_number = compl_interrupted;
8303 RedrawingDisabled = saved;
8304}
8305#endif
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307/*
8308 * "confirm(message, buttons[, default [, type]])" function
8309 */
8310/*ARGSUSED*/
8311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 typval_T *argvars;
8314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315{
8316#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8317 char_u *message;
8318 char_u *buttons = NULL;
8319 char_u buf[NUMBUFLEN];
8320 char_u buf2[NUMBUFLEN];
8321 int def = 1;
8322 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008323 char_u *typestr;
8324 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 message = get_tv_string_chk(&argvars[0]);
8327 if (message == NULL)
8328 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008329 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008331 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8332 if (buttons == NULL)
8333 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008334 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008336 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008337 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8340 if (typestr == NULL)
8341 error = TRUE;
8342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008344 switch (TOUPPER_ASC(*typestr))
8345 {
8346 case 'E': type = VIM_ERROR; break;
8347 case 'Q': type = VIM_QUESTION; break;
8348 case 'I': type = VIM_INFO; break;
8349 case 'W': type = VIM_WARNING; break;
8350 case 'G': type = VIM_GENERIC; break;
8351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 }
8353 }
8354 }
8355 }
8356
8357 if (buttons == NULL || *buttons == NUL)
8358 buttons = (char_u *)_("&Ok");
8359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008360 if (error)
8361 rettv->vval.v_number = 0;
8362 else
8363 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 def, NULL);
8365#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367#endif
8368}
8369
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008370/*
8371 * "copy()" function
8372 */
8373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 typval_T *argvars;
8376 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008378 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008379}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
8381/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008382 * "count()" function
8383 */
8384 static void
8385f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008386 typval_T *argvars;
8387 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008388{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008389 long n = 0;
8390 int ic = FALSE;
8391
Bram Moolenaare9a41262005-01-15 22:18:47 +00008392 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008393 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008394 listitem_T *li;
8395 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008396 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008397
Bram Moolenaare9a41262005-01-15 22:18:47 +00008398 if ((l = argvars[0].vval.v_list) != NULL)
8399 {
8400 li = l->lv_first;
8401 if (argvars[2].v_type != VAR_UNKNOWN)
8402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008403 int error = FALSE;
8404
8405 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 if (argvars[3].v_type != VAR_UNKNOWN)
8407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008408 idx = get_tv_number_chk(&argvars[3], &error);
8409 if (!error)
8410 {
8411 li = list_find(l, idx);
8412 if (li == NULL)
8413 EMSGN(_(e_listidx), idx);
8414 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008416 if (error)
8417 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008418 }
8419
8420 for ( ; li != NULL; li = li->li_next)
8421 if (tv_equal(&li->li_tv, &argvars[1], ic))
8422 ++n;
8423 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425 else if (argvars[0].v_type == VAR_DICT)
8426 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008427 int todo;
8428 dict_T *d;
8429 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008431 if ((d = argvars[0].vval.v_dict) != NULL)
8432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 int error = FALSE;
8434
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 if (argvars[2].v_type != VAR_UNKNOWN)
8436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008438 if (argvars[3].v_type != VAR_UNKNOWN)
8439 EMSG(_(e_invarg));
8440 }
8441
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008442 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008443 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008444 {
8445 if (!HASHITEM_EMPTY(hi))
8446 {
8447 --todo;
8448 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8449 ++n;
8450 }
8451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 }
8453 }
8454 else
8455 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008456 rettv->vval.v_number = n;
8457}
8458
8459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8461 *
8462 * Checks the existence of a cscope connection.
8463 */
8464/*ARGSUSED*/
8465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008467 typval_T *argvars;
8468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470#ifdef FEAT_CSCOPE
8471 int num = 0;
8472 char_u *dbpath = NULL;
8473 char_u *prepend = NULL;
8474 char_u buf[NUMBUFLEN];
8475
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008476 if (argvars[0].v_type != VAR_UNKNOWN
8477 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008479 num = (int)get_tv_number(&argvars[0]);
8480 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008485 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008487 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488#endif
8489}
8490
8491/*
8492 * "cursor(lnum, col)" function
8493 *
8494 * Moves the cursor to the specified line and column
8495 */
8496/*ARGSUSED*/
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008503#ifdef FEAT_VIRTUALEDIT
8504 long coladd = 0;
8505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
Bram Moolenaara5525202006-03-02 22:52:09 +00008507 if (argvars[1].v_type == VAR_UNKNOWN)
8508 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008509 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008510
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008511 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008512 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 line = pos.lnum;
8514 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008515#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008516 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008517#endif
8518 }
8519 else
8520 {
8521 line = get_tv_lnum(argvars);
8522 col = get_tv_number_chk(&argvars[1], NULL);
8523#ifdef FEAT_VIRTUALEDIT
8524 if (argvars[2].v_type != VAR_UNKNOWN)
8525 coladd = get_tv_number_chk(&argvars[2], NULL);
8526#endif
8527 }
8528 if (line < 0 || col < 0
8529#ifdef FEAT_VIRTUALEDIT
8530 || coladd < 0
8531#endif
8532 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008533 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (line > 0)
8535 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (col > 0)
8537 curwin->w_cursor.col = col - 1;
8538#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008539 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540#endif
8541
8542 /* Make sure the cursor is in a valid position. */
8543 check_cursor();
8544#ifdef FEAT_MBYTE
8545 /* Correct cursor for multi-byte character. */
8546 if (has_mbyte)
8547 mb_adjust_cursor();
8548#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008549
8550 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 */
8556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008558 typval_T *argvars;
8559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008561 int noref = 0;
8562
8563 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008564 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008565 if (noref < 0 || noref > 1)
8566 EMSG(_(e_invarg));
8567 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008568 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569}
8570
8571/*
8572 * "delete()" function
8573 */
8574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *argvars;
8577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584
8585/*
8586 * "did_filetype()" function
8587 */
8588/*ARGSUSED*/
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598#endif
8599}
8600
8601/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008602 * "diff_filler()" function
8603 */
8604/*ARGSUSED*/
8605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 typval_T *argvars;
8608 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008609{
8610#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008612#endif
8613}
8614
8615/*
8616 * "diff_hlID()" function
8617 */
8618/*ARGSUSED*/
8619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 typval_T *argvars;
8622 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008623{
8624#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008626 static linenr_T prev_lnum = 0;
8627 static int changedtick = 0;
8628 static int fnum = 0;
8629 static int change_start = 0;
8630 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008631 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008632 int filler_lines;
8633 int col;
8634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 if (lnum < 0) /* ignore type error in {lnum} arg */
8636 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008637 if (lnum != prev_lnum
8638 || changedtick != curbuf->b_changedtick
8639 || fnum != curbuf->b_fnum)
8640 {
8641 /* New line, buffer, change: need to get the values. */
8642 filler_lines = diff_check(curwin, lnum);
8643 if (filler_lines < 0)
8644 {
8645 if (filler_lines == -1)
8646 {
8647 change_start = MAXCOL;
8648 change_end = -1;
8649 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8650 hlID = HLF_ADD; /* added line */
8651 else
8652 hlID = HLF_CHD; /* changed line */
8653 }
8654 else
8655 hlID = HLF_ADD; /* added line */
8656 }
8657 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008658 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008659 prev_lnum = lnum;
8660 changedtick = curbuf->b_changedtick;
8661 fnum = curbuf->b_fnum;
8662 }
8663
8664 if (hlID == HLF_CHD || hlID == HLF_TXD)
8665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008667 if (col >= change_start && col <= change_end)
8668 hlID = HLF_TXD; /* changed text */
8669 else
8670 hlID = HLF_CHD; /* changed line */
8671 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008672 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008673#endif
8674}
8675
8676/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008677 * "empty({expr})" function
8678 */
8679 static void
8680f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008683{
8684 int n;
8685
8686 switch (argvars[0].v_type)
8687 {
8688 case VAR_STRING:
8689 case VAR_FUNC:
8690 n = argvars[0].vval.v_string == NULL
8691 || *argvars[0].vval.v_string == NUL;
8692 break;
8693 case VAR_NUMBER:
8694 n = argvars[0].vval.v_number == 0;
8695 break;
8696 case VAR_LIST:
8697 n = argvars[0].vval.v_list == NULL
8698 || argvars[0].vval.v_list->lv_first == NULL;
8699 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 case VAR_DICT:
8701 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008703 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008704 default:
8705 EMSG2(_(e_intern2), "f_empty()");
8706 n = 0;
8707 }
8708
8709 rettv->vval.v_number = n;
8710}
8711
8712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 * "escape({string}, {chars})" function
8714 */
8715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 typval_T *argvars;
8718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 char_u buf[NUMBUFLEN];
8721
Bram Moolenaar758711c2005-02-02 23:11:38 +00008722 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8723 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725}
8726
8727/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008728 * "eval()" function
8729 */
8730/*ARGSUSED*/
8731 static void
8732f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 typval_T *argvars;
8734 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008735{
8736 char_u *s;
8737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008738 s = get_tv_string_chk(&argvars[0]);
8739 if (s != NULL)
8740 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8743 {
8744 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008745 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008747 else if (*s != NUL)
8748 EMSG(_(e_trailing));
8749}
8750
8751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 * "eventhandler()" function
8753 */
8754/*ARGSUSED*/
8755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 typval_T *argvars;
8758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761}
8762
8763/*
8764 * "executable()" function
8765 */
8766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T *argvars;
8769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772}
8773
8774/*
8775 * "exists()" function
8776 */
8777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T *argvars;
8780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 char_u *p;
8783 char_u *name;
8784 int n = FALSE;
8785 int len = 0;
8786
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 if (*p == '$') /* environment variable */
8789 {
8790 /* first try "normal" environment variables (fast) */
8791 if (mch_getenv(p + 1) != NULL)
8792 n = TRUE;
8793 else
8794 {
8795 /* try expanding things like $VIM and ${HOME} */
8796 p = expand_env_save(p);
8797 if (p != NULL && *p != '$')
8798 n = TRUE;
8799 vim_free(p);
8800 }
8801 }
8802 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008804 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008805 if (*skipwhite(p) != NUL)
8806 n = FALSE; /* trailing garbage */
8807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 else if (*p == '*') /* internal or user defined function */
8809 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008810 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
8812 else if (*p == ':')
8813 {
8814 n = cmd_exists(p + 1);
8815 }
8816 else if (*p == '#')
8817 {
8818#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008819 if (p[1] == '#')
8820 n = autocmd_supported(p + 2);
8821 else
8822 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823#endif
8824 }
8825 else /* internal variable */
8826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008827 char_u *tofree;
8828 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008830 /* get_name_len() takes care of expanding curly braces */
8831 name = p;
8832 len = get_name_len(&p, &tofree, TRUE, FALSE);
8833 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008835 if (tofree != NULL)
8836 name = tofree;
8837 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8838 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008840 /* handle d.key, l[idx], f(expr) */
8841 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8842 if (n)
8843 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 }
8845 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008846 if (*p != NUL)
8847 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008849 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853}
8854
8855/*
8856 * "expand()" function
8857 */
8858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008860 typval_T *argvars;
8861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 char_u *s;
8864 int len;
8865 char_u *errormsg;
8866 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8867 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->v_type = VAR_STRING;
8871 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 if (*s == '%' || *s == '#' || *s == '<')
8873 {
8874 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 --emsg_off;
8877 }
8878 else
8879 {
8880 /* When the optional second argument is non-zero, don't remove matches
8881 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 if (argvars[1].v_type != VAR_UNKNOWN
8883 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 if (!error)
8886 {
8887 ExpandInit(&xpc);
8888 xpc.xp_context = EXPAND_FILES;
8889 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 }
8891 else
8892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
8894}
8895
8896/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008897 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008899 */
8900 static void
8901f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *argvars;
8903 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008904{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008905 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008906 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008907 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008908 list_T *l1, *l2;
8909 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008912
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 l1 = argvars[0].vval.v_list;
8914 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008915 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8916 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 {
8918 if (argvars[2].v_type != VAR_UNKNOWN)
8919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 before = get_tv_number_chk(&argvars[2], &error);
8921 if (error)
8922 return; /* type error; errmsg already given */
8923
Bram Moolenaar758711c2005-02-02 23:11:38 +00008924 if (before == l1->lv_len)
8925 item = NULL;
8926 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008927 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008928 item = list_find(l1, before);
8929 if (item == NULL)
8930 {
8931 EMSGN(_(e_listidx), before);
8932 return;
8933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008934 }
8935 }
8936 else
8937 item = NULL;
8938 list_extend(l1, l2, item);
8939
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 copy_tv(&argvars[0], rettv);
8941 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008943 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8944 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 dict_T *d1, *d2;
8946 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 char_u *action;
8948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008950 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951
8952 d1 = argvars[0].vval.v_dict;
8953 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008954 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8955 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008956 {
8957 /* Check the third argument. */
8958 if (argvars[2].v_type != VAR_UNKNOWN)
8959 {
8960 static char *(av[]) = {"keep", "force", "error"};
8961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 action = get_tv_string_chk(&argvars[2]);
8963 if (action == NULL)
8964 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008965 for (i = 0; i < 3; ++i)
8966 if (STRCMP(action, av[i]) == 0)
8967 break;
8968 if (i == 3)
8969 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008970 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 return;
8972 }
8973 }
8974 else
8975 action = (char_u *)"force";
8976
8977 /* Go over all entries in the second dict and add them to the
8978 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008979 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008981 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008982 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008984 --todo;
8985 di1 = dict_find(d1, hi2->hi_key, -1);
8986 if (di1 == NULL)
8987 {
8988 di1 = dictitem_copy(HI2DI(hi2));
8989 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8990 dictitem_free(di1);
8991 }
8992 else if (*action == 'e')
8993 {
8994 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8995 break;
8996 }
8997 else if (*action == 'f')
8998 {
8999 clear_tv(&di1->di_tv);
9000 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009002 }
9003 }
9004
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 copy_tv(&argvars[0], rettv);
9006 }
9007 }
9008 else
9009 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010}
9011
9012/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009013 * "feedkeys()" function
9014 */
9015/*ARGSUSED*/
9016 static void
9017f_feedkeys(argvars, rettv)
9018 typval_T *argvars;
9019 typval_T *rettv;
9020{
9021 int remap = TRUE;
9022 char_u *keys, *flags;
9023 char_u nbuf[NUMBUFLEN];
9024 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009025 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009026
9027 rettv->vval.v_number = 0;
9028 keys = get_tv_string(&argvars[0]);
9029 if (*keys != NUL)
9030 {
9031 if (argvars[1].v_type != VAR_UNKNOWN)
9032 {
9033 flags = get_tv_string_buf(&argvars[1], nbuf);
9034 for ( ; *flags != NUL; ++flags)
9035 {
9036 switch (*flags)
9037 {
9038 case 'n': remap = FALSE; break;
9039 case 'm': remap = TRUE; break;
9040 case 't': typed = TRUE; break;
9041 }
9042 }
9043 }
9044
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009045 /* Need to escape K_SPECIAL and CSI before putting the string in the
9046 * typeahead buffer. */
9047 keys_esc = vim_strsave_escape_csi(keys);
9048 if (keys_esc != NULL)
9049 {
9050 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009051 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009052 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009053 if (vgetc_busy)
9054 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009055 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009056 }
9057}
9058
9059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 * "filereadable()" function
9061 */
9062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 typval_T *argvars;
9065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
9067 FILE *fd;
9068 char_u *p;
9069 int n;
9070
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9073 {
9074 n = TRUE;
9075 fclose(fd);
9076 }
9077 else
9078 n = FALSE;
9079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081}
9082
9083/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009084 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 * rights to write into.
9086 */
9087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 typval_T *argvars;
9090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009092 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093}
9094
Bram Moolenaar33570922005-01-25 22:26:29 +00009095static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009096
9097 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009101 int dir;
9102{
9103#ifdef FEAT_SEARCHPATH
9104 char_u *fname;
9105 char_u *fresult = NULL;
9106 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9107 char_u *p;
9108 char_u pathbuf[NUMBUFLEN];
9109 int count = 1;
9110 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009111 int error = FALSE;
9112#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009113
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009114 rettv->vval.v_string = NULL;
9115 rettv->v_type = VAR_STRING;
9116
9117#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009119
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009120 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009122 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9123 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009124 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 else
9126 {
9127 if (*p != NUL)
9128 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009131 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009133 }
9134
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009135 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9136 error = TRUE;
9137
9138 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 do
9141 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009142 if (rettv->v_type == VAR_STRING)
9143 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 fresult = find_file_in_path_option(first ? fname : NULL,
9145 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009146 0, first, path, dir, NULL,
9147 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009149
9150 if (fresult != NULL && rettv->v_type == VAR_LIST)
9151 list_append_string(rettv->vval.v_list, fresult, -1);
9152
9153 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009155
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009156 if (rettv->v_type == VAR_STRING)
9157 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009158#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009159}
9160
Bram Moolenaar33570922005-01-25 22:26:29 +00009161static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9162static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009163
9164/*
9165 * Implementation of map() and filter().
9166 */
9167 static void
9168filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 typval_T *argvars;
9170 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009171 int map;
9172{
9173 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009174 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009175 listitem_T *li, *nli;
9176 list_T *l = NULL;
9177 dictitem_T *di;
9178 hashtab_T *ht;
9179 hashitem_T *hi;
9180 dict_T *d = NULL;
9181 typval_T save_val;
9182 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009183 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009184 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009185 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009186 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009187
9188 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009189 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009190 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009191 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009192 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009193 return;
9194 }
9195 else if (argvars[0].v_type == VAR_DICT)
9196 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009197 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009198 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199 return;
9200 }
9201 else
9202 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009203 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009204 return;
9205 }
9206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 expr = get_tv_string_buf_chk(&argvars[1], buf);
9208 /* On type errors, the preceding call has already displayed an error
9209 * message. Avoid a misleading error message for an empty string that
9210 * was not passed as argument. */
9211 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 prepare_vimvar(VV_VAL, &save_val);
9214 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009215
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009216 /* We reset "did_emsg" to be able to detect whether an error
9217 * occurred during evaluation of the expression. */
9218 save_did_emsg = did_emsg;
9219 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009223 prepare_vimvar(VV_KEY, &save_key);
9224 vimvars[VV_KEY].vv_type = VAR_STRING;
9225
9226 ht = &d->dv_hashtab;
9227 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009228 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 if (!HASHITEM_EMPTY(hi))
9232 {
9233 --todo;
9234 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009235 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 break;
9237 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009238 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009239 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 break;
9241 if (!map && rem)
9242 dictitem_remove(d, di);
9243 clear_tv(&vimvars[VV_KEY].vv_tv);
9244 }
9245 }
9246 hash_unlock(ht);
9247
9248 restore_vimvar(VV_KEY, &save_key);
9249 }
9250 else
9251 {
9252 for (li = l->lv_first; li != NULL; li = nli)
9253 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009254 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009255 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009257 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009258 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009259 break;
9260 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009262 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009263 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009266
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009267 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009268 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009269
9270 copy_tv(&argvars[0], rettv);
9271}
9272
9273 static int
9274filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009276 char_u *expr;
9277 int map;
9278 int *remp;
9279{
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009281 char_u *s;
9282
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009284 s = expr;
9285 if (eval1(&s, &rettv, TRUE) == FAIL)
9286 return FAIL;
9287 if (*s != NUL) /* check for trailing chars after expr */
9288 {
9289 EMSG2(_(e_invexpr2), s);
9290 return FAIL;
9291 }
9292 if (map)
9293 {
9294 /* map(): replace the list item value */
9295 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009296 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 *tv = rettv;
9298 }
9299 else
9300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 int error = FALSE;
9302
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009304 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009306 /* On type error, nothing has been removed; return FAIL to stop the
9307 * loop. The error message was given by get_tv_number_chk(). */
9308 if (error)
9309 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009313}
9314
9315/*
9316 * "filter()" function
9317 */
9318 static void
9319f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009320 typval_T *argvars;
9321 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009322{
9323 filter_map(argvars, rettv, FALSE);
9324}
9325
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009326/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009327 * "finddir({fname}[, {path}[, {count}]])" function
9328 */
9329 static void
9330f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009331 typval_T *argvars;
9332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009333{
9334 findfilendir(argvars, rettv, TRUE);
9335}
9336
9337/*
9338 * "findfile({fname}[, {path}[, {count}]])" function
9339 */
9340 static void
9341f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009342 typval_T *argvars;
9343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009344{
9345 findfilendir(argvars, rettv, FALSE);
9346}
9347
9348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 * "fnamemodify({fname}, {mods})" function
9350 */
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009353 typval_T *argvars;
9354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 char_u *fname;
9357 char_u *mods;
9358 int usedlen = 0;
9359 int len;
9360 char_u *fbuf = NULL;
9361 char_u buf[NUMBUFLEN];
9362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 fname = get_tv_string_chk(&argvars[0]);
9364 mods = get_tv_string_buf_chk(&argvars[1], buf);
9365 if (fname == NULL || mods == NULL)
9366 fname = NULL;
9367 else
9368 {
9369 len = (int)STRLEN(fname);
9370 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 vim_free(fbuf);
9379}
9380
Bram Moolenaar33570922005-01-25 22:26:29 +00009381static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
9383/*
9384 * "foldclosed()" function
9385 */
9386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T *argvars;
9389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 int end;
9391{
9392#ifdef FEAT_FOLDING
9393 linenr_T lnum;
9394 linenr_T first, last;
9395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9398 {
9399 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9400 {
9401 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 return;
9406 }
9407 }
9408#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410}
9411
9412/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009413 * "foldclosed()" function
9414 */
9415 static void
9416f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 typval_T *argvars;
9418 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419{
9420 foldclosed_both(argvars, rettv, FALSE);
9421}
9422
9423/*
9424 * "foldclosedend()" function
9425 */
9426 static void
9427f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 typval_T *argvars;
9429 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009430{
9431 foldclosed_both(argvars, rettv, TRUE);
9432}
9433
9434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 * "foldlevel()" function
9436 */
9437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442#ifdef FEAT_FOLDING
9443 linenr_T lnum;
9444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 else
9449#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
9454 * "foldtext()" function
9455 */
9456/*ARGSUSED*/
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462#ifdef FEAT_FOLDING
9463 linenr_T lnum;
9464 char_u *s;
9465 char_u *r;
9466 int len;
9467 char *txt;
9468#endif
9469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 rettv->v_type = VAR_STRING;
9471 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009473 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9474 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9475 <= curbuf->b_ml.ml_line_count
9476 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 {
9478 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009479 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9480 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 {
9482 if (!linewhite(lnum))
9483 break;
9484 ++lnum;
9485 }
9486
9487 /* Find interesting text in this line. */
9488 s = skipwhite(ml_get(lnum));
9489 /* skip C comment-start */
9490 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009493 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009494 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009495 {
9496 s = skipwhite(ml_get(lnum + 1));
9497 if (*s == '*')
9498 s = skipwhite(s + 1);
9499 }
9500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 txt = _("+-%s%3ld lines: ");
9502 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009503 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 + 20 /* for %3ld */
9505 + STRLEN(s))); /* concatenated */
9506 if (r != NULL)
9507 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009508 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9509 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9510 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 len = (int)STRLEN(r);
9512 STRCAT(r, s);
9513 /* remove 'foldmarker' and 'commentstring' */
9514 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 }
9517 }
9518#endif
9519}
9520
9521/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009522 * "foldtextresult(lnum)" function
9523 */
9524/*ARGSUSED*/
9525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 typval_T *argvars;
9528 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009529{
9530#ifdef FEAT_FOLDING
9531 linenr_T lnum;
9532 char_u *text;
9533 char_u buf[51];
9534 foldinfo_T foldinfo;
9535 int fold_count;
9536#endif
9537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->v_type = VAR_STRING;
9539 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009540#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 /* treat illegal types and illegal string values for {lnum} the same */
9543 if (lnum < 0)
9544 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009545 fold_count = foldedCount(curwin, lnum, &foldinfo);
9546 if (fold_count > 0)
9547 {
9548 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9549 &foldinfo, buf);
9550 if (text == buf)
9551 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553 }
9554#endif
9555}
9556
9557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 * "foreground()" function
9559 */
9560/*ARGSUSED*/
9561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567#ifdef FEAT_GUI
9568 if (gui.in_use)
9569 gui_mch_set_foreground();
9570#else
9571# ifdef WIN32
9572 win32_set_foreground();
9573# endif
9574#endif
9575}
9576
9577/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009578 * "function()" function
9579 */
9580/*ARGSUSED*/
9581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009583 typval_T *argvars;
9584 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585{
9586 char_u *s;
9587
Bram Moolenaara7043832005-01-21 11:56:39 +00009588 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009590 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 EMSG2(_(e_invarg2), s);
9592 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009593 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009594 else
9595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_string = vim_strsave(s);
9597 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009598 }
9599}
9600
9601/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009602 * "garbagecollect()" function
9603 */
9604/*ARGSUSED*/
9605 static void
9606f_garbagecollect(argvars, rettv)
9607 typval_T *argvars;
9608 typval_T *rettv;
9609{
9610 garbage_collect();
9611}
9612
9613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009614 * "get()" function
9615 */
9616 static void
9617f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 typval_T *argvars;
9619 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009620{
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 listitem_T *li;
9622 list_T *l;
9623 dictitem_T *di;
9624 dict_T *d;
9625 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009626
Bram Moolenaare9a41262005-01-15 22:18:47 +00009627 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009628 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009631 int error = FALSE;
9632
9633 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9634 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009636 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 else if (argvars[0].v_type == VAR_DICT)
9639 {
9640 if ((d = argvars[0].vval.v_dict) != NULL)
9641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009642 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 if (di != NULL)
9644 tv = &di->di_tv;
9645 }
9646 }
9647 else
9648 EMSG2(_(e_listdictarg), "get()");
9649
9650 if (tv == NULL)
9651 {
9652 if (argvars[2].v_type == VAR_UNKNOWN)
9653 rettv->vval.v_number = 0;
9654 else
9655 copy_tv(&argvars[2], rettv);
9656 }
9657 else
9658 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009659}
9660
Bram Moolenaar342337a2005-07-21 21:11:17 +00009661static 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 +00009662
9663/*
9664 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009665 * Return a range (from start to end) of lines in rettv from the specified
9666 * buffer.
9667 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009668 */
9669 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009670get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009671 buf_T *buf;
9672 linenr_T start;
9673 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009674 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009675 typval_T *rettv;
9676{
9677 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009678
Bram Moolenaar342337a2005-07-21 21:11:17 +00009679 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009680 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009681 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009682 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009683 }
9684 else
9685 rettv->vval.v_number = 0;
9686
9687 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9688 return;
9689
9690 if (!retlist)
9691 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009692 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9693 p = ml_get_buf(buf, start, FALSE);
9694 else
9695 p = (char_u *)"";
9696
9697 rettv->v_type = VAR_STRING;
9698 rettv->vval.v_string = vim_strsave(p);
9699 }
9700 else
9701 {
9702 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009703 return;
9704
9705 if (start < 1)
9706 start = 1;
9707 if (end > buf->b_ml.ml_line_count)
9708 end = buf->b_ml.ml_line_count;
9709 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009710 if (list_append_string(rettv->vval.v_list,
9711 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009712 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009713 }
9714}
9715
9716/*
9717 * "getbufline()" function
9718 */
9719 static void
9720f_getbufline(argvars, rettv)
9721 typval_T *argvars;
9722 typval_T *rettv;
9723{
9724 linenr_T lnum;
9725 linenr_T end;
9726 buf_T *buf;
9727
9728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9729 ++emsg_off;
9730 buf = get_buf_tv(&argvars[0]);
9731 --emsg_off;
9732
Bram Moolenaar661b1822005-07-28 22:36:45 +00009733 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009734 if (argvars[2].v_type == VAR_UNKNOWN)
9735 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009736 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009737 end = get_tv_lnum_buf(&argvars[2], buf);
9738
Bram Moolenaar342337a2005-07-21 21:11:17 +00009739 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009740}
9741
Bram Moolenaar0d660222005-01-07 21:51:51 +00009742/*
9743 * "getbufvar()" function
9744 */
9745 static void
9746f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009747 typval_T *argvars;
9748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009749{
9750 buf_T *buf;
9751 buf_T *save_curbuf;
9752 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009753 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9756 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009757 ++emsg_off;
9758 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009759
9760 rettv->v_type = VAR_STRING;
9761 rettv->vval.v_string = NULL;
9762
9763 if (buf != NULL && varname != NULL)
9764 {
9765 if (*varname == '&') /* buffer-local-option */
9766 {
9767 /* set curbuf to be our buf, temporarily */
9768 save_curbuf = curbuf;
9769 curbuf = buf;
9770
9771 get_option_tv(&varname, rettv, TRUE);
9772
9773 /* restore previous notion of curbuf */
9774 curbuf = save_curbuf;
9775 }
9776 else
9777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 if (*varname == NUL)
9779 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9780 * scope prefix before the NUL byte is required by
9781 * find_var_in_ht(). */
9782 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009783 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009784 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009785 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009787 }
9788 }
9789
9790 --emsg_off;
9791}
9792
9793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 * "getchar()" function
9795 */
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009804 /* Position the cursor. Needed after a message that ends in a space. */
9805 windgoto(msg_row, msg_col);
9806
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 ++no_mapping;
9808 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009809 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 /* getchar(): blocking wait. */
9811 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 /* getchar(1): only check if char avail */
9814 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009815 else if (error || vpeekc() == NUL)
9816 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 n = 0;
9818 else
9819 /* getchar(0) and char avail: return char */
9820 n = safe_vgetc();
9821 --no_mapping;
9822 --allow_keys;
9823
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 if (IS_SPECIAL(n) || mod_mask != 0)
9826 {
9827 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9828 int i = 0;
9829
9830 /* Turn a special key into three bytes, plus modifier. */
9831 if (mod_mask != 0)
9832 {
9833 temp[i++] = K_SPECIAL;
9834 temp[i++] = KS_MODIFIER;
9835 temp[i++] = mod_mask;
9836 }
9837 if (IS_SPECIAL(n))
9838 {
9839 temp[i++] = K_SPECIAL;
9840 temp[i++] = K_SECOND(n);
9841 temp[i++] = K_THIRD(n);
9842 }
9843#ifdef FEAT_MBYTE
9844 else if (has_mbyte)
9845 i += (*mb_char2bytes)(n, temp + i);
9846#endif
9847 else
9848 temp[i++] = n;
9849 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->v_type = VAR_STRING;
9851 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 }
9853}
9854
9855/*
9856 * "getcharmod()" function
9857 */
9858/*ARGSUSED*/
9859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 typval_T *argvars;
9862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
9868 * "getcmdline()" function
9869 */
9870/*ARGSUSED*/
9871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 typval_T *argvars;
9874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->v_type = VAR_STRING;
9877 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878}
9879
9880/*
9881 * "getcmdpos()" function
9882 */
9883/*ARGSUSED*/
9884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009886 typval_T *argvars;
9887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009893 * "getcmdtype()" function
9894 */
9895/*ARGSUSED*/
9896 static void
9897f_getcmdtype(argvars, rettv)
9898 typval_T *argvars;
9899 typval_T *rettv;
9900{
9901 rettv->v_type = VAR_STRING;
9902 rettv->vval.v_string = alloc(2);
9903 if (rettv->vval.v_string != NULL)
9904 {
9905 rettv->vval.v_string[0] = get_cmdline_type();
9906 rettv->vval.v_string[1] = NUL;
9907 }
9908}
9909
9910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 * "getcwd()" function
9912 */
9913/*ARGSUSED*/
9914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009916 typval_T *argvars;
9917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
9919 char_u cwd[MAXPATHL];
9920
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 else
9925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009928 if (rettv->vval.v_string != NULL)
9929 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930#endif
9931 }
9932}
9933
9934/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009935 * "getfontname()" function
9936 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009937/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->v_type = VAR_STRING;
9944 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009945#ifdef FEAT_GUI
9946 if (gui.in_use)
9947 {
9948 GuiFont font;
9949 char_u *name = NULL;
9950
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009951 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009952 {
9953 /* Get the "Normal" font. Either the name saved by
9954 * hl_set_font_name() or from the font ID. */
9955 font = gui.norm_font;
9956 name = hl_get_font_name();
9957 }
9958 else
9959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009961 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9962 return;
9963 font = gui_mch_get_font(name, FALSE);
9964 if (font == NOFONT)
9965 return; /* Invalid font name, return empty string. */
9966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009968 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009969 gui_mch_free_font(font);
9970 }
9971#endif
9972}
9973
9974/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009975 * "getfperm({fname})" function
9976 */
9977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009979 typval_T *argvars;
9980 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009981{
9982 char_u *fname;
9983 struct stat st;
9984 char_u *perm = NULL;
9985 char_u flags[] = "rwx";
9986 int i;
9987
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009991 if (mch_stat((char *)fname, &st) >= 0)
9992 {
9993 perm = vim_strsave((char_u *)"---------");
9994 if (perm != NULL)
9995 {
9996 for (i = 0; i < 9; i++)
9997 {
9998 if (st.st_mode & (1 << (8 - i)))
9999 perm[i] = flags[i % 3];
10000 }
10001 }
10002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010004}
10005
10006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 * "getfsize({fname})" function
10008 */
10009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010011 typval_T *argvars;
10012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 char_u *fname;
10015 struct stat st;
10016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
10021 if (mch_stat((char *)fname, &st) >= 0)
10022 {
10023 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 }
10028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030}
10031
10032/*
10033 * "getftime({fname})" function
10034 */
10035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 typval_T *argvars;
10038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
10040 char_u *fname;
10041 struct stat st;
10042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
10045 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
10051/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010052 * "getftype({fname})" function
10053 */
10054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010056 typval_T *argvars;
10057 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010058{
10059 char_u *fname;
10060 struct stat st;
10061 char_u *type = NULL;
10062 char *t;
10063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010067 if (mch_lstat((char *)fname, &st) >= 0)
10068 {
10069#ifdef S_ISREG
10070 if (S_ISREG(st.st_mode))
10071 t = "file";
10072 else if (S_ISDIR(st.st_mode))
10073 t = "dir";
10074# ifdef S_ISLNK
10075 else if (S_ISLNK(st.st_mode))
10076 t = "link";
10077# endif
10078# ifdef S_ISBLK
10079 else if (S_ISBLK(st.st_mode))
10080 t = "bdev";
10081# endif
10082# ifdef S_ISCHR
10083 else if (S_ISCHR(st.st_mode))
10084 t = "cdev";
10085# endif
10086# ifdef S_ISFIFO
10087 else if (S_ISFIFO(st.st_mode))
10088 t = "fifo";
10089# endif
10090# ifdef S_ISSOCK
10091 else if (S_ISSOCK(st.st_mode))
10092 t = "fifo";
10093# endif
10094 else
10095 t = "other";
10096#else
10097# ifdef S_IFMT
10098 switch (st.st_mode & S_IFMT)
10099 {
10100 case S_IFREG: t = "file"; break;
10101 case S_IFDIR: t = "dir"; break;
10102# ifdef S_IFLNK
10103 case S_IFLNK: t = "link"; break;
10104# endif
10105# ifdef S_IFBLK
10106 case S_IFBLK: t = "bdev"; break;
10107# endif
10108# ifdef S_IFCHR
10109 case S_IFCHR: t = "cdev"; break;
10110# endif
10111# ifdef S_IFIFO
10112 case S_IFIFO: t = "fifo"; break;
10113# endif
10114# ifdef S_IFSOCK
10115 case S_IFSOCK: t = "socket"; break;
10116# endif
10117 default: t = "other";
10118 }
10119# else
10120 if (mch_isdir(fname))
10121 t = "dir";
10122 else
10123 t = "file";
10124# endif
10125#endif
10126 type = vim_strsave((char_u *)t);
10127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010129}
10130
10131/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010132 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010133 */
10134 static void
10135f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010136 typval_T *argvars;
10137 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010138{
10139 linenr_T lnum;
10140 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010141 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010142
10143 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010144 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010145 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010146 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010147 retlist = FALSE;
10148 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010149 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010150 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010151 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010152 retlist = TRUE;
10153 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010154
Bram Moolenaar342337a2005-07-21 21:11:17 +000010155 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010156}
10157
10158/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010159 * "getpos(string)" function
10160 */
10161 static void
10162f_getpos(argvars, rettv)
10163 typval_T *argvars;
10164 typval_T *rettv;
10165{
10166 pos_T *fp;
10167 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010168 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010169
10170 if (rettv_list_alloc(rettv) == OK)
10171 {
10172 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010173 fp = var2fpos(&argvars[0], TRUE, &fnum);
10174 if (fnum != -1)
10175 list_append_number(l, (varnumber_T)fnum);
10176 else
10177 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010178 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10179 : (varnumber_T)0);
10180 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10181 : (varnumber_T)0);
10182 list_append_number(l,
10183#ifdef FEAT_VIRTUALEDIT
10184 (fp != NULL) ? (varnumber_T)fp->coladd :
10185#endif
10186 (varnumber_T)0);
10187 }
10188 else
10189 rettv->vval.v_number = FALSE;
10190}
10191
10192/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010193 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010194 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010195/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010196 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010197f_getqflist(argvars, rettv)
10198 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010199 typval_T *rettv;
10200{
10201#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010202 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010203#endif
10204
10205 rettv->vval.v_number = FALSE;
10206#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010207 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010208 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010209 wp = NULL;
10210 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10211 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010212 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010213 if (wp == NULL)
10214 return;
10215 }
10216
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010217 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010218 }
10219#endif
10220}
10221
10222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 * "getreg()" function
10224 */
10225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 typval_T *argvars;
10228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
10230 char_u *strregname;
10231 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010232 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010233 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010235 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 strregname = get_tv_string_chk(&argvars[0]);
10238 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010239 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 regname = (strregname == NULL ? '"' : *strregname);
10245 if (regname == 0)
10246 regname = '"';
10247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 rettv->vval.v_string = error ? NULL :
10250 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251}
10252
10253/*
10254 * "getregtype()" function
10255 */
10256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 typval_T *argvars;
10259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260{
10261 char_u *strregname;
10262 int regname;
10263 char_u buf[NUMBUFLEN + 2];
10264 long reglen = 0;
10265
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010266 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 {
10268 strregname = get_tv_string_chk(&argvars[0]);
10269 if (strregname == NULL) /* type error; errmsg already given */
10270 {
10271 rettv->v_type = VAR_STRING;
10272 rettv->vval.v_string = NULL;
10273 return;
10274 }
10275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 else
10277 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279
10280 regname = (strregname == NULL ? '"' : *strregname);
10281 if (regname == 0)
10282 regname = '"';
10283
10284 buf[0] = NUL;
10285 buf[1] = NUL;
10286 switch (get_reg_type(regname, &reglen))
10287 {
10288 case MLINE: buf[0] = 'V'; break;
10289 case MCHAR: buf[0] = 'v'; break;
10290#ifdef FEAT_VISUAL
10291 case MBLOCK:
10292 buf[0] = Ctrl_V;
10293 sprintf((char *)buf + 1, "%ld", reglen + 1);
10294 break;
10295#endif
10296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 rettv->v_type = VAR_STRING;
10298 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299}
10300
10301/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010302 * "gettabwinvar()" function
10303 */
10304 static void
10305f_gettabwinvar(argvars, rettv)
10306 typval_T *argvars;
10307 typval_T *rettv;
10308{
10309 getwinvar(argvars, rettv, 1);
10310}
10311
10312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 * "getwinposx()" function
10314 */
10315/*ARGSUSED*/
10316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010318 typval_T *argvars;
10319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322#ifdef FEAT_GUI
10323 if (gui.in_use)
10324 {
10325 int x, y;
10326
10327 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 }
10330#endif
10331}
10332
10333/*
10334 * "getwinposy()" function
10335 */
10336/*ARGSUSED*/
10337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010339 typval_T *argvars;
10340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343#ifdef FEAT_GUI
10344 if (gui.in_use)
10345 {
10346 int x, y;
10347
10348 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 }
10351#endif
10352}
10353
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010354/*
10355 * Find window specifed by "vp" in tabpage "tp".
10356 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010357 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010358find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010359 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010360 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010361{
10362#ifdef FEAT_WINDOWS
10363 win_T *wp;
10364#endif
10365 int nr;
10366
10367 nr = get_tv_number_chk(vp, NULL);
10368
10369#ifdef FEAT_WINDOWS
10370 if (nr < 0)
10371 return NULL;
10372 if (nr == 0)
10373 return curwin;
10374
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010375 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10376 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010377 if (--nr <= 0)
10378 break;
10379 return wp;
10380#else
10381 if (nr == 0 || nr == 1)
10382 return curwin;
10383 return NULL;
10384#endif
10385}
10386
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387/*
10388 * "getwinvar()" function
10389 */
10390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010391f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010392 typval_T *argvars;
10393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010395 getwinvar(argvars, rettv, 0);
10396}
10397
10398/*
10399 * getwinvar() and gettabwinvar()
10400 */
10401 static void
10402getwinvar(argvars, rettv, off)
10403 typval_T *argvars;
10404 typval_T *rettv;
10405 int off; /* 1 for gettabwinvar() */
10406{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 win_T *win, *oldcurwin;
10408 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010410 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010412#ifdef FEAT_WINDOWS
10413 if (off == 1)
10414 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10415 else
10416 tp = curtab;
10417#endif
10418 win = find_win_by_nr(&argvars[off], tp);
10419 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 rettv->v_type = VAR_STRING;
10423 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424
10425 if (win != NULL && varname != NULL)
10426 {
10427 if (*varname == '&') /* window-local-option */
10428 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010429 /* Set curwin to be our win, temporarily. Also set curbuf, so
10430 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 oldcurwin = curwin;
10432 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010433 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436
10437 /* restore previous notion of curwin */
10438 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010439 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 }
10441 else
10442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 if (*varname == NUL)
10444 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10445 * scope prefix before the NUL byte is required by
10446 * find_var_in_ht(). */
10447 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010449 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010451 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 }
10453 }
10454
10455 --emsg_off;
10456}
10457
10458/*
10459 * "glob()" function
10460 */
10461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *argvars;
10464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466 expand_T xpc;
10467
10468 ExpandInit(&xpc);
10469 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 rettv->v_type = VAR_STRING;
10471 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473}
10474
10475/*
10476 * "globpath()" function
10477 */
10478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 typval_T *argvars;
10481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010487 if (file == NULL)
10488 rettv->vval.v_string = NULL;
10489 else
10490 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491}
10492
10493/*
10494 * "has()" function
10495 */
10496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 int i;
10502 char_u *name;
10503 int n = FALSE;
10504 static char *(has_list[]) =
10505 {
10506#ifdef AMIGA
10507 "amiga",
10508# ifdef FEAT_ARP
10509 "arp",
10510# endif
10511#endif
10512#ifdef __BEOS__
10513 "beos",
10514#endif
10515#ifdef MSDOS
10516# ifdef DJGPP
10517 "dos32",
10518# else
10519 "dos16",
10520# endif
10521#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010522#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 "mac",
10524#endif
10525#if defined(MACOS_X_UNIX)
10526 "macunix",
10527#endif
10528#ifdef OS2
10529 "os2",
10530#endif
10531#ifdef __QNX__
10532 "qnx",
10533#endif
10534#ifdef RISCOS
10535 "riscos",
10536#endif
10537#ifdef UNIX
10538 "unix",
10539#endif
10540#ifdef VMS
10541 "vms",
10542#endif
10543#ifdef WIN16
10544 "win16",
10545#endif
10546#ifdef WIN32
10547 "win32",
10548#endif
10549#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10550 "win32unix",
10551#endif
10552#ifdef WIN64
10553 "win64",
10554#endif
10555#ifdef EBCDIC
10556 "ebcdic",
10557#endif
10558#ifndef CASE_INSENSITIVE_FILENAME
10559 "fname_case",
10560#endif
10561#ifdef FEAT_ARABIC
10562 "arabic",
10563#endif
10564#ifdef FEAT_AUTOCMD
10565 "autocmd",
10566#endif
10567#ifdef FEAT_BEVAL
10568 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010569# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10570 "balloon_multiline",
10571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572#endif
10573#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10574 "builtin_terms",
10575# ifdef ALL_BUILTIN_TCAPS
10576 "all_builtin_terms",
10577# endif
10578#endif
10579#ifdef FEAT_BYTEOFF
10580 "byte_offset",
10581#endif
10582#ifdef FEAT_CINDENT
10583 "cindent",
10584#endif
10585#ifdef FEAT_CLIENTSERVER
10586 "clientserver",
10587#endif
10588#ifdef FEAT_CLIPBOARD
10589 "clipboard",
10590#endif
10591#ifdef FEAT_CMDL_COMPL
10592 "cmdline_compl",
10593#endif
10594#ifdef FEAT_CMDHIST
10595 "cmdline_hist",
10596#endif
10597#ifdef FEAT_COMMENTS
10598 "comments",
10599#endif
10600#ifdef FEAT_CRYPT
10601 "cryptv",
10602#endif
10603#ifdef FEAT_CSCOPE
10604 "cscope",
10605#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010606#ifdef CURSOR_SHAPE
10607 "cursorshape",
10608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609#ifdef DEBUG
10610 "debug",
10611#endif
10612#ifdef FEAT_CON_DIALOG
10613 "dialog_con",
10614#endif
10615#ifdef FEAT_GUI_DIALOG
10616 "dialog_gui",
10617#endif
10618#ifdef FEAT_DIFF
10619 "diff",
10620#endif
10621#ifdef FEAT_DIGRAPHS
10622 "digraphs",
10623#endif
10624#ifdef FEAT_DND
10625 "dnd",
10626#endif
10627#ifdef FEAT_EMACS_TAGS
10628 "emacs_tags",
10629#endif
10630 "eval", /* always present, of course! */
10631#ifdef FEAT_EX_EXTRA
10632 "ex_extra",
10633#endif
10634#ifdef FEAT_SEARCH_EXTRA
10635 "extra_search",
10636#endif
10637#ifdef FEAT_FKMAP
10638 "farsi",
10639#endif
10640#ifdef FEAT_SEARCHPATH
10641 "file_in_path",
10642#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010643#if defined(UNIX) && !defined(USE_SYSTEM)
10644 "filterpipe",
10645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646#ifdef FEAT_FIND_ID
10647 "find_in_path",
10648#endif
10649#ifdef FEAT_FOLDING
10650 "folding",
10651#endif
10652#ifdef FEAT_FOOTER
10653 "footer",
10654#endif
10655#if !defined(USE_SYSTEM) && defined(UNIX)
10656 "fork",
10657#endif
10658#ifdef FEAT_GETTEXT
10659 "gettext",
10660#endif
10661#ifdef FEAT_GUI
10662 "gui",
10663#endif
10664#ifdef FEAT_GUI_ATHENA
10665# ifdef FEAT_GUI_NEXTAW
10666 "gui_neXtaw",
10667# else
10668 "gui_athena",
10669# endif
10670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671#ifdef FEAT_GUI_GTK
10672 "gui_gtk",
10673# ifdef HAVE_GTK2
10674 "gui_gtk2",
10675# endif
10676#endif
10677#ifdef FEAT_GUI_MAC
10678 "gui_mac",
10679#endif
10680#ifdef FEAT_GUI_MOTIF
10681 "gui_motif",
10682#endif
10683#ifdef FEAT_GUI_PHOTON
10684 "gui_photon",
10685#endif
10686#ifdef FEAT_GUI_W16
10687 "gui_win16",
10688#endif
10689#ifdef FEAT_GUI_W32
10690 "gui_win32",
10691#endif
10692#ifdef FEAT_HANGULIN
10693 "hangul_input",
10694#endif
10695#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10696 "iconv",
10697#endif
10698#ifdef FEAT_INS_EXPAND
10699 "insert_expand",
10700#endif
10701#ifdef FEAT_JUMPLIST
10702 "jumplist",
10703#endif
10704#ifdef FEAT_KEYMAP
10705 "keymap",
10706#endif
10707#ifdef FEAT_LANGMAP
10708 "langmap",
10709#endif
10710#ifdef FEAT_LIBCALL
10711 "libcall",
10712#endif
10713#ifdef FEAT_LINEBREAK
10714 "linebreak",
10715#endif
10716#ifdef FEAT_LISP
10717 "lispindent",
10718#endif
10719#ifdef FEAT_LISTCMDS
10720 "listcmds",
10721#endif
10722#ifdef FEAT_LOCALMAP
10723 "localmap",
10724#endif
10725#ifdef FEAT_MENU
10726 "menu",
10727#endif
10728#ifdef FEAT_SESSION
10729 "mksession",
10730#endif
10731#ifdef FEAT_MODIFY_FNAME
10732 "modify_fname",
10733#endif
10734#ifdef FEAT_MOUSE
10735 "mouse",
10736#endif
10737#ifdef FEAT_MOUSESHAPE
10738 "mouseshape",
10739#endif
10740#if defined(UNIX) || defined(VMS)
10741# ifdef FEAT_MOUSE_DEC
10742 "mouse_dec",
10743# endif
10744# ifdef FEAT_MOUSE_GPM
10745 "mouse_gpm",
10746# endif
10747# ifdef FEAT_MOUSE_JSB
10748 "mouse_jsbterm",
10749# endif
10750# ifdef FEAT_MOUSE_NET
10751 "mouse_netterm",
10752# endif
10753# ifdef FEAT_MOUSE_PTERM
10754 "mouse_pterm",
10755# endif
10756# ifdef FEAT_MOUSE_XTERM
10757 "mouse_xterm",
10758# endif
10759#endif
10760#ifdef FEAT_MBYTE
10761 "multi_byte",
10762#endif
10763#ifdef FEAT_MBYTE_IME
10764 "multi_byte_ime",
10765#endif
10766#ifdef FEAT_MULTI_LANG
10767 "multi_lang",
10768#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010769#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010770#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010771 "mzscheme",
10772#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774#ifdef FEAT_OLE
10775 "ole",
10776#endif
10777#ifdef FEAT_OSFILETYPE
10778 "osfiletype",
10779#endif
10780#ifdef FEAT_PATH_EXTRA
10781 "path_extra",
10782#endif
10783#ifdef FEAT_PERL
10784#ifndef DYNAMIC_PERL
10785 "perl",
10786#endif
10787#endif
10788#ifdef FEAT_PYTHON
10789#ifndef DYNAMIC_PYTHON
10790 "python",
10791#endif
10792#endif
10793#ifdef FEAT_POSTSCRIPT
10794 "postscript",
10795#endif
10796#ifdef FEAT_PRINTER
10797 "printer",
10798#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010799#ifdef FEAT_PROFILE
10800 "profile",
10801#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010802#ifdef FEAT_RELTIME
10803 "reltime",
10804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805#ifdef FEAT_QUICKFIX
10806 "quickfix",
10807#endif
10808#ifdef FEAT_RIGHTLEFT
10809 "rightleft",
10810#endif
10811#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10812 "ruby",
10813#endif
10814#ifdef FEAT_SCROLLBIND
10815 "scrollbind",
10816#endif
10817#ifdef FEAT_CMDL_INFO
10818 "showcmd",
10819 "cmdline_info",
10820#endif
10821#ifdef FEAT_SIGNS
10822 "signs",
10823#endif
10824#ifdef FEAT_SMARTINDENT
10825 "smartindent",
10826#endif
10827#ifdef FEAT_SNIFF
10828 "sniff",
10829#endif
10830#ifdef FEAT_STL_OPT
10831 "statusline",
10832#endif
10833#ifdef FEAT_SUN_WORKSHOP
10834 "sun_workshop",
10835#endif
10836#ifdef FEAT_NETBEANS_INTG
10837 "netbeans_intg",
10838#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010839#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010840 "spell",
10841#endif
10842#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 "syntax",
10844#endif
10845#if defined(USE_SYSTEM) || !defined(UNIX)
10846 "system",
10847#endif
10848#ifdef FEAT_TAG_BINS
10849 "tag_binary",
10850#endif
10851#ifdef FEAT_TAG_OLDSTATIC
10852 "tag_old_static",
10853#endif
10854#ifdef FEAT_TAG_ANYWHITE
10855 "tag_any_white",
10856#endif
10857#ifdef FEAT_TCL
10858# ifndef DYNAMIC_TCL
10859 "tcl",
10860# endif
10861#endif
10862#ifdef TERMINFO
10863 "terminfo",
10864#endif
10865#ifdef FEAT_TERMRESPONSE
10866 "termresponse",
10867#endif
10868#ifdef FEAT_TEXTOBJ
10869 "textobjects",
10870#endif
10871#ifdef HAVE_TGETENT
10872 "tgetent",
10873#endif
10874#ifdef FEAT_TITLE
10875 "title",
10876#endif
10877#ifdef FEAT_TOOLBAR
10878 "toolbar",
10879#endif
10880#ifdef FEAT_USR_CMDS
10881 "user-commands", /* was accidentally included in 5.4 */
10882 "user_commands",
10883#endif
10884#ifdef FEAT_VIMINFO
10885 "viminfo",
10886#endif
10887#ifdef FEAT_VERTSPLIT
10888 "vertsplit",
10889#endif
10890#ifdef FEAT_VIRTUALEDIT
10891 "virtualedit",
10892#endif
10893#ifdef FEAT_VISUAL
10894 "visual",
10895#endif
10896#ifdef FEAT_VISUALEXTRA
10897 "visualextra",
10898#endif
10899#ifdef FEAT_VREPLACE
10900 "vreplace",
10901#endif
10902#ifdef FEAT_WILDIGN
10903 "wildignore",
10904#endif
10905#ifdef FEAT_WILDMENU
10906 "wildmenu",
10907#endif
10908#ifdef FEAT_WINDOWS
10909 "windows",
10910#endif
10911#ifdef FEAT_WAK
10912 "winaltkeys",
10913#endif
10914#ifdef FEAT_WRITEBACKUP
10915 "writebackup",
10916#endif
10917#ifdef FEAT_XIM
10918 "xim",
10919#endif
10920#ifdef FEAT_XFONTSET
10921 "xfontset",
10922#endif
10923#ifdef USE_XSMP
10924 "xsmp",
10925#endif
10926#ifdef USE_XSMP_INTERACT
10927 "xsmp_interact",
10928#endif
10929#ifdef FEAT_XCLIPBOARD
10930 "xterm_clipboard",
10931#endif
10932#ifdef FEAT_XTERM_SAVE
10933 "xterm_save",
10934#endif
10935#if defined(UNIX) && defined(FEAT_X11)
10936 "X11",
10937#endif
10938 NULL
10939 };
10940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 for (i = 0; has_list[i] != NULL; ++i)
10943 if (STRICMP(name, has_list[i]) == 0)
10944 {
10945 n = TRUE;
10946 break;
10947 }
10948
10949 if (n == FALSE)
10950 {
10951 if (STRNICMP(name, "patch", 5) == 0)
10952 n = has_patch(atoi((char *)name + 5));
10953 else if (STRICMP(name, "vim_starting") == 0)
10954 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010955#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10956 else if (STRICMP(name, "balloon_multiline") == 0)
10957 n = multiline_balloon_available();
10958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959#ifdef DYNAMIC_TCL
10960 else if (STRICMP(name, "tcl") == 0)
10961 n = tcl_enabled(FALSE);
10962#endif
10963#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10964 else if (STRICMP(name, "iconv") == 0)
10965 n = iconv_enabled(FALSE);
10966#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010967#ifdef DYNAMIC_MZSCHEME
10968 else if (STRICMP(name, "mzscheme") == 0)
10969 n = mzscheme_enabled(FALSE);
10970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971#ifdef DYNAMIC_RUBY
10972 else if (STRICMP(name, "ruby") == 0)
10973 n = ruby_enabled(FALSE);
10974#endif
10975#ifdef DYNAMIC_PYTHON
10976 else if (STRICMP(name, "python") == 0)
10977 n = python_enabled(FALSE);
10978#endif
10979#ifdef DYNAMIC_PERL
10980 else if (STRICMP(name, "perl") == 0)
10981 n = perl_enabled(FALSE);
10982#endif
10983#ifdef FEAT_GUI
10984 else if (STRICMP(name, "gui_running") == 0)
10985 n = (gui.in_use || gui.starting);
10986# ifdef FEAT_GUI_W32
10987 else if (STRICMP(name, "gui_win32s") == 0)
10988 n = gui_is_win32s();
10989# endif
10990# ifdef FEAT_BROWSE
10991 else if (STRICMP(name, "browse") == 0)
10992 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10993# endif
10994#endif
10995#ifdef FEAT_SYN_HL
10996 else if (STRICMP(name, "syntax_items") == 0)
10997 n = syntax_present(curbuf);
10998#endif
10999#if defined(WIN3264)
11000 else if (STRICMP(name, "win95") == 0)
11001 n = mch_windows95();
11002#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011003#ifdef FEAT_NETBEANS_INTG
11004 else if (STRICMP(name, "netbeans_enabled") == 0)
11005 n = usingNetbeans;
11006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 }
11008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013 * "has_key()" function
11014 */
11015 static void
11016f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011017 typval_T *argvars;
11018 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019{
11020 rettv->vval.v_number = 0;
11021 if (argvars[0].v_type != VAR_DICT)
11022 {
11023 EMSG(_(e_dictreq));
11024 return;
11025 }
11026 if (argvars[0].vval.v_dict == NULL)
11027 return;
11028
11029 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011030 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031}
11032
11033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 * "hasmapto()" function
11035 */
11036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011038 typval_T *argvars;
11039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
11041 char_u *name;
11042 char_u *mode;
11043 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011044 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011047 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 mode = (char_u *)"nvo";
11049 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011052 if (argvars[2].v_type != VAR_UNKNOWN)
11053 abbr = get_tv_number(&argvars[2]);
11054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaar2c932302006-03-18 21:42:09 +000011056 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060}
11061
11062/*
11063 * "histadd()" function
11064 */
11065/*ARGSUSED*/
11066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 typval_T *argvars;
11069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
11071#ifdef FEAT_CMDHIST
11072 int histype;
11073 char_u *str;
11074 char_u buf[NUMBUFLEN];
11075#endif
11076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 if (check_restricted() || check_secure())
11079 return;
11080#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11082 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 if (histype >= 0)
11084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 if (*str != NUL)
11087 {
11088 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 return;
11091 }
11092 }
11093#endif
11094}
11095
11096/*
11097 * "histdel()" function
11098 */
11099/*ARGSUSED*/
11100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *argvars;
11103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
11105#ifdef FEAT_CMDHIST
11106 int n;
11107 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011108 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11111 if (str == NULL)
11112 n = 0;
11113 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011116 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 else
11121 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 get_tv_string_buf(&argvars[1], buf));
11124 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127#endif
11128}
11129
11130/*
11131 * "histget()" function
11132 */
11133/*ARGSUSED*/
11134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 typval_T *argvars;
11137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
11139#ifdef FEAT_CMDHIST
11140 int type;
11141 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011144 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11145 if (str == NULL)
11146 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 {
11149 type = get_histtype(str);
11150 if (argvars[1].v_type == VAR_UNKNOWN)
11151 idx = get_history_idx(type);
11152 else
11153 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11154 /* -1 on type error */
11155 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161}
11162
11163/*
11164 * "histnr()" function
11165 */
11166/*ARGSUSED*/
11167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 typval_T *argvars;
11170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
11172 int i;
11173
11174#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 char_u *history = get_tv_string_chk(&argvars[0]);
11176
11177 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 if (i >= HIST_CMD && i < HIST_COUNT)
11179 i = get_history_idx(i);
11180 else
11181#endif
11182 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184}
11185
11186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 * "highlightID(name)" function
11188 */
11189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011191 typval_T *argvars;
11192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195}
11196
11197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011198 * "highlight_exists()" function
11199 */
11200 static void
11201f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011202 typval_T *argvars;
11203 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011204{
11205 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11206}
11207
11208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 * "hostname()" function
11210 */
11211/*ARGSUSED*/
11212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 typval_T *argvars;
11215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216{
11217 char_u hostname[256];
11218
11219 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 rettv->v_type = VAR_STRING;
11221 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222}
11223
11224/*
11225 * iconv() function
11226 */
11227/*ARGSUSED*/
11228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011230 typval_T *argvars;
11231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233#ifdef FEAT_MBYTE
11234 char_u buf1[NUMBUFLEN];
11235 char_u buf2[NUMBUFLEN];
11236 char_u *from, *to, *str;
11237 vimconv_T vimconv;
11238#endif
11239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->v_type = VAR_STRING;
11241 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242
11243#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 str = get_tv_string(&argvars[0]);
11245 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11246 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 vimconv.vc_type = CONV_NONE;
11248 convert_setup(&vimconv, from, to);
11249
11250 /* If the encodings are equal, no conversion needed. */
11251 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
11256 convert_setup(&vimconv, NULL, NULL);
11257 vim_free(from);
11258 vim_free(to);
11259#endif
11260}
11261
11262/*
11263 * "indent()" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 linenr_T lnum;
11271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277}
11278
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011279/*
11280 * "index()" function
11281 */
11282 static void
11283f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011286{
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 list_T *l;
11288 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011289 long idx = 0;
11290 int ic = FALSE;
11291
11292 rettv->vval.v_number = -1;
11293 if (argvars[0].v_type != VAR_LIST)
11294 {
11295 EMSG(_(e_listreq));
11296 return;
11297 }
11298 l = argvars[0].vval.v_list;
11299 if (l != NULL)
11300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011301 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011302 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 int error = FALSE;
11305
Bram Moolenaar758711c2005-02-02 23:11:38 +000011306 /* Start at specified item. Use the cached index that list_find()
11307 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011309 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011310 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 ic = get_tv_number_chk(&argvars[3], &error);
11312 if (error)
11313 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011314 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011315
Bram Moolenaar758711c2005-02-02 23:11:38 +000011316 for ( ; item != NULL; item = item->li_next, ++idx)
11317 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011318 {
11319 rettv->vval.v_number = idx;
11320 break;
11321 }
11322 }
11323}
11324
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325static int inputsecret_flag = 0;
11326
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011327static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11328
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011330 * This function is used by f_input() and f_inputdialog() functions. The third
11331 * argument to f_input() specifies the type of completion to use at the
11332 * prompt. The third argument to f_inputdialog() specifies the value to return
11333 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 */
11335 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011336get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011337 typval_T *argvars;
11338 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011339 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 char_u *p = NULL;
11343 int c;
11344 char_u buf[NUMBUFLEN];
11345 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011347 int xp_type = EXPAND_NOTHING;
11348 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351
11352#ifdef NO_CONSOLE_INPUT
11353 /* While starting up, there is no place to enter text. */
11354 if (no_console_input())
11355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 return;
11358 }
11359#endif
11360
11361 cmd_silent = FALSE; /* Want to see the prompt. */
11362 if (prompt != NULL)
11363 {
11364 /* Only the part of the message after the last NL is considered as
11365 * prompt for the command line */
11366 p = vim_strrchr(prompt, '\n');
11367 if (p == NULL)
11368 p = prompt;
11369 else
11370 {
11371 ++p;
11372 c = *p;
11373 *p = NUL;
11374 msg_start();
11375 msg_clr_eos();
11376 msg_puts_attr(prompt, echo_attr);
11377 msg_didout = FALSE;
11378 msg_starthere();
11379 *p = c;
11380 }
11381 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 if (argvars[1].v_type != VAR_UNKNOWN)
11384 {
11385 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11386 if (defstr != NULL)
11387 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011389 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011390 {
11391 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011392 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011393 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011394
Bram Moolenaar4463f292005-09-25 22:20:24 +000011395 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011396
Bram Moolenaar4463f292005-09-25 22:20:24 +000011397 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11398 if (xp_name == NULL)
11399 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011400
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011401 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011402
Bram Moolenaar4463f292005-09-25 22:20:24 +000011403 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11404 &xp_arg) == FAIL)
11405 return;
11406 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011407 }
11408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011409 if (defstr != NULL)
11410 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011411 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11412 xp_type, xp_arg);
11413
11414 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 /* since the user typed this, no need to wait for return */
11417 need_wait_return = FALSE;
11418 msg_didout = FALSE;
11419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 cmd_silent = cmd_silent_save;
11421}
11422
11423/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011424 * "input()" function
11425 * Also handles inputsecret() when inputsecret is set.
11426 */
11427 static void
11428f_input(argvars, rettv)
11429 typval_T *argvars;
11430 typval_T *rettv;
11431{
11432 get_user_input(argvars, rettv, FALSE);
11433}
11434
11435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 * "inputdialog()" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443#if defined(FEAT_GUI_TEXTDIALOG)
11444 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11445 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11446 {
11447 char_u *message;
11448 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011451 message = get_tv_string_chk(&argvars[0]);
11452 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011453 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011454 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 else
11456 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 if (message != NULL && defstr != NULL
11458 && do_dialog(VIM_QUESTION, NULL, message,
11459 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 else
11462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 if (message != NULL && defstr != NULL
11464 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011465 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_string = vim_strsave(
11467 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 }
11473 else
11474#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011475 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476}
11477
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011478/*
11479 * "inputlist()" function
11480 */
11481 static void
11482f_inputlist(argvars, rettv)
11483 typval_T *argvars;
11484 typval_T *rettv;
11485{
11486 listitem_T *li;
11487 int selected;
11488 int mouse_used;
11489
11490 rettv->vval.v_number = 0;
11491#ifdef NO_CONSOLE_INPUT
11492 /* While starting up, there is no place to enter text. */
11493 if (no_console_input())
11494 return;
11495#endif
11496 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11497 {
11498 EMSG2(_(e_listarg), "inputlist()");
11499 return;
11500 }
11501
11502 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011503 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011504 lines_left = Rows; /* avoid more prompt */
11505 msg_scroll = TRUE;
11506 msg_clr_eos();
11507
11508 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11509 {
11510 msg_puts(get_tv_string(&li->li_tv));
11511 msg_putchar('\n');
11512 }
11513
11514 /* Ask for choice. */
11515 selected = prompt_for_number(&mouse_used);
11516 if (mouse_used)
11517 selected -= lines_left;
11518
11519 rettv->vval.v_number = selected;
11520}
11521
11522
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11524
11525/*
11526 * "inputrestore()" function
11527 */
11528/*ARGSUSED*/
11529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 typval_T *argvars;
11532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533{
11534 if (ga_userinput.ga_len > 0)
11535 {
11536 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11538 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 }
11541 else if (p_verbose > 1)
11542 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011543 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 }
11546}
11547
11548/*
11549 * "inputsave()" function
11550 */
11551/*ARGSUSED*/
11552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011554 typval_T *argvars;
11555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556{
11557 /* Add an entry to the stack of typehead storage. */
11558 if (ga_grow(&ga_userinput, 1) == OK)
11559 {
11560 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11561 + ga_userinput.ga_len);
11562 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 }
11565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567}
11568
11569/*
11570 * "inputsecret()" function
11571 */
11572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 typval_T *argvars;
11575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576{
11577 ++cmdline_star;
11578 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 --cmdline_star;
11581 --inputsecret_flag;
11582}
11583
11584/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011585 * "insert()" function
11586 */
11587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011589 typval_T *argvars;
11590 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011591{
11592 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 listitem_T *item;
11594 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011595 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011596
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011597 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011598 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011599 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011600 else if ((l = argvars[0].vval.v_list) != NULL
11601 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011602 {
11603 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 before = get_tv_number_chk(&argvars[2], &error);
11605 if (error)
11606 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011607
Bram Moolenaar758711c2005-02-02 23:11:38 +000011608 if (before == l->lv_len)
11609 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610 else
11611 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011612 item = list_find(l, before);
11613 if (item == NULL)
11614 {
11615 EMSGN(_(e_listidx), before);
11616 l = NULL;
11617 }
11618 }
11619 if (l != NULL)
11620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011621 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011622 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623 }
11624 }
11625}
11626
11627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 * "isdirectory()" function
11629 */
11630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011632 typval_T *argvars;
11633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636}
11637
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011638/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011639 * "islocked()" function
11640 */
11641 static void
11642f_islocked(argvars, rettv)
11643 typval_T *argvars;
11644 typval_T *rettv;
11645{
11646 lval_T lv;
11647 char_u *end;
11648 dictitem_T *di;
11649
11650 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011651 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11652 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011653 if (end != NULL && lv.ll_name != NULL)
11654 {
11655 if (*end != NUL)
11656 EMSG(_(e_trailing));
11657 else
11658 {
11659 if (lv.ll_tv == NULL)
11660 {
11661 if (check_changedtick(lv.ll_name))
11662 rettv->vval.v_number = 1; /* always locked */
11663 else
11664 {
11665 di = find_var(lv.ll_name, NULL);
11666 if (di != NULL)
11667 {
11668 /* Consider a variable locked when:
11669 * 1. the variable itself is locked
11670 * 2. the value of the variable is locked.
11671 * 3. the List or Dict value is locked.
11672 */
11673 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11674 || tv_islocked(&di->di_tv));
11675 }
11676 }
11677 }
11678 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011679 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011680 else if (lv.ll_newkey != NULL)
11681 EMSG2(_(e_dictkey), lv.ll_newkey);
11682 else if (lv.ll_list != NULL)
11683 /* List item. */
11684 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11685 else
11686 /* Dictionary item. */
11687 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11688 }
11689 }
11690
11691 clear_lval(&lv);
11692}
11693
Bram Moolenaar33570922005-01-25 22:26:29 +000011694static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011695
11696/*
11697 * Turn a dict into a list:
11698 * "what" == 0: list of keys
11699 * "what" == 1: list of values
11700 * "what" == 2: list of items
11701 */
11702 static void
11703dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011704 typval_T *argvars;
11705 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011706 int what;
11707{
Bram Moolenaar33570922005-01-25 22:26:29 +000011708 list_T *l2;
11709 dictitem_T *di;
11710 hashitem_T *hi;
11711 listitem_T *li;
11712 listitem_T *li2;
11713 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011714 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011715
11716 rettv->vval.v_number = 0;
11717 if (argvars[0].v_type != VAR_DICT)
11718 {
11719 EMSG(_(e_dictreq));
11720 return;
11721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011722 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011723 return;
11724
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011725 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011726 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011727
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011728 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011730 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011731 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011732 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011733 --todo;
11734 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011735
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011736 li = listitem_alloc();
11737 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011738 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011739 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011740
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011741 if (what == 0)
11742 {
11743 /* keys() */
11744 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011745 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011746 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11747 }
11748 else if (what == 1)
11749 {
11750 /* values() */
11751 copy_tv(&di->di_tv, &li->li_tv);
11752 }
11753 else
11754 {
11755 /* items() */
11756 l2 = list_alloc();
11757 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011758 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011759 li->li_tv.vval.v_list = l2;
11760 if (l2 == NULL)
11761 break;
11762 ++l2->lv_refcount;
11763
11764 li2 = listitem_alloc();
11765 if (li2 == NULL)
11766 break;
11767 list_append(l2, li2);
11768 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011769 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011770 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11771
11772 li2 = listitem_alloc();
11773 if (li2 == NULL)
11774 break;
11775 list_append(l2, li2);
11776 copy_tv(&di->di_tv, &li2->li_tv);
11777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011778 }
11779 }
11780}
11781
11782/*
11783 * "items(dict)" function
11784 */
11785 static void
11786f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011787 typval_T *argvars;
11788 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011789{
11790 dict_list(argvars, rettv, 2);
11791}
11792
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011794 * "join()" function
11795 */
11796 static void
11797f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *argvars;
11799 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011800{
11801 garray_T ga;
11802 char_u *sep;
11803
11804 rettv->vval.v_number = 0;
11805 if (argvars[0].v_type != VAR_LIST)
11806 {
11807 EMSG(_(e_listreq));
11808 return;
11809 }
11810 if (argvars[0].vval.v_list == NULL)
11811 return;
11812 if (argvars[1].v_type == VAR_UNKNOWN)
11813 sep = (char_u *)" ";
11814 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011816
11817 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818
11819 if (sep != NULL)
11820 {
11821 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011822 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 ga_append(&ga, NUL);
11824 rettv->vval.v_string = (char_u *)ga.ga_data;
11825 }
11826 else
11827 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011828}
11829
11830/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011831 * "keys()" function
11832 */
11833 static void
11834f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 typval_T *argvars;
11836 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011837{
11838 dict_list(argvars, rettv, 0);
11839}
11840
11841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 * "last_buffer_nr()" function.
11843 */
11844/*ARGSUSED*/
11845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *argvars;
11848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
11850 int n = 0;
11851 buf_T *buf;
11852
11853 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11854 if (n < buf->b_fnum)
11855 n = buf->b_fnum;
11856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011858}
11859
11860/*
11861 * "len()" function
11862 */
11863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011865 typval_T *argvars;
11866 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867{
11868 switch (argvars[0].v_type)
11869 {
11870 case VAR_STRING:
11871 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_number = (varnumber_T)STRLEN(
11873 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 break;
11875 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011877 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 case VAR_DICT:
11879 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11880 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011881 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011882 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011883 break;
11884 }
11885}
11886
Bram Moolenaar33570922005-01-25 22:26:29 +000011887static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011888
11889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 typval_T *argvars;
11892 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011893 int type;
11894{
11895#ifdef FEAT_LIBCALL
11896 char_u *string_in;
11897 char_u **string_result;
11898 int nr_result;
11899#endif
11900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011902 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011904 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011906
11907 if (check_restricted() || check_secure())
11908 return;
11909
11910#ifdef FEAT_LIBCALL
11911 /* The first two args must be strings, otherwise its meaningless */
11912 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11913 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011914 string_in = NULL;
11915 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011916 string_in = argvars[2].vval.v_string;
11917 if (type == VAR_NUMBER)
11918 string_result = NULL;
11919 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011921 if (mch_libcall(argvars[0].vval.v_string,
11922 argvars[1].vval.v_string,
11923 string_in,
11924 argvars[2].vval.v_number,
11925 string_result,
11926 &nr_result) == OK
11927 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011929 }
11930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931}
11932
11933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011934 * "libcall()" function
11935 */
11936 static void
11937f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *argvars;
11939 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011940{
11941 libcall_common(argvars, rettv, VAR_STRING);
11942}
11943
11944/*
11945 * "libcallnr()" function
11946 */
11947 static void
11948f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011949 typval_T *argvars;
11950 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951{
11952 libcall_common(argvars, rettv, VAR_NUMBER);
11953}
11954
11955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 * "line(string)" function
11957 */
11958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011960 typval_T *argvars;
11961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962{
11963 linenr_T lnum = 0;
11964 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011965 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011967 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 if (fp != NULL)
11969 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971}
11972
11973/*
11974 * "line2byte(lnum)" function
11975 */
11976/*ARGSUSED*/
11977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011979 typval_T *argvars;
11980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984#else
11985 linenr_T lnum;
11986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11992 if (rettv->vval.v_number >= 0)
11993 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994#endif
11995}
11996
11997/*
11998 * "lispindent(lnum)" function
11999 */
12000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
12005#ifdef FEAT_LISP
12006 pos_T pos;
12007 linenr_T lnum;
12008
12009 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12012 {
12013 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 curwin->w_cursor = pos;
12016 }
12017 else
12018#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020}
12021
12022/*
12023 * "localtime()" function
12024 */
12025/*ARGSUSED*/
12026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 typval_T *argvars;
12029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032}
12033
Bram Moolenaar33570922005-01-25 22:26:29 +000012034static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035
12036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *argvars;
12039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 int exact;
12041{
12042 char_u *keys;
12043 char_u *which;
12044 char_u buf[NUMBUFLEN];
12045 char_u *keys_buf = NULL;
12046 char_u *rhs;
12047 int mode;
12048 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012049 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050
12051 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->v_type = VAR_STRING;
12053 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 if (*keys == NUL)
12057 return;
12058
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012059 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012062 if (argvars[2].v_type != VAR_UNKNOWN)
12063 abbr = get_tv_number(&argvars[2]);
12064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 else
12066 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 if (which == NULL)
12068 return;
12069
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 mode = get_map_mode(&which, 0);
12071
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012072 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012073 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 vim_free(keys_buf);
12075 if (rhs != NULL)
12076 {
12077 ga_init(&ga);
12078 ga.ga_itemsize = 1;
12079 ga.ga_growsize = 40;
12080
12081 while (*rhs != NUL)
12082 ga_concat(&ga, str2special(&rhs, FALSE));
12083
12084 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 }
12087}
12088
12089/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012090 * "map()" function
12091 */
12092 static void
12093f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012096{
12097 filter_map(argvars, rettv, TRUE);
12098}
12099
12100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012101 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 */
12103 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012104f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 typval_T *argvars;
12106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109}
12110
12111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012112 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 */
12114 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012115f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012116 typval_T *argvars;
12117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012119 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120}
12121
Bram Moolenaar33570922005-01-25 22:26:29 +000012122static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123
12124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 int type;
12129{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012130 char_u *str = NULL;
12131 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 char_u *pat;
12133 regmatch_T regmatch;
12134 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012135 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 char_u *save_cpo;
12137 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012138 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012139 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012140 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012141 list_T *l = NULL;
12142 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012143 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012144 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145
12146 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12147 save_cpo = p_cpo;
12148 p_cpo = (char_u *)"";
12149
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012150 rettv->vval.v_number = -1;
12151 if (type == 3)
12152 {
12153 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012154 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012155 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012156 }
12157 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->v_type = VAR_STRING;
12160 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012163 if (argvars[0].v_type == VAR_LIST)
12164 {
12165 if ((l = argvars[0].vval.v_list) == NULL)
12166 goto theend;
12167 li = l->lv_first;
12168 }
12169 else
12170 expr = str = get_tv_string(&argvars[0]);
12171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012172 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12173 if (pat == NULL)
12174 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012175
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012178 int error = FALSE;
12179
12180 start = get_tv_number_chk(&argvars[2], &error);
12181 if (error)
12182 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012183 if (l != NULL)
12184 {
12185 li = list_find(l, start);
12186 if (li == NULL)
12187 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012188 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012189 }
12190 else
12191 {
12192 if (start < 0)
12193 start = 0;
12194 if (start > (long)STRLEN(str))
12195 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012196 /* When "count" argument is there ignore matches before "start",
12197 * otherwise skip part of the string. Differs when pattern is "^"
12198 * or "\<". */
12199 if (argvars[3].v_type != VAR_UNKNOWN)
12200 startcol = start;
12201 else
12202 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012203 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012204
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012205 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012206 nth = get_tv_number_chk(&argvars[3], &error);
12207 if (error)
12208 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 }
12210
12211 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12212 if (regmatch.regprog != NULL)
12213 {
12214 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012215
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012216 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012217 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012218 if (l != NULL)
12219 {
12220 if (li == NULL)
12221 {
12222 match = FALSE;
12223 break;
12224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012225 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012226 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012227 if (str == NULL)
12228 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012229 }
12230
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012231 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012232
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012233 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012234 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012235 if (l == NULL && !match)
12236 break;
12237
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012238 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012239 if (l != NULL)
12240 {
12241 li = li->li_next;
12242 ++idx;
12243 }
12244 else
12245 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012246#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012247 startcol = (colnr_T)(regmatch.startp[0]
12248 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012249#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012250 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012251#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012252 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012253 }
12254
12255 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012257 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012259 int i;
12260
12261 /* return list with matched string and submatches */
12262 for (i = 0; i < NSUBEXP; ++i)
12263 {
12264 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012265 {
12266 if (list_append_string(rettv->vval.v_list,
12267 (char_u *)"", 0) == FAIL)
12268 break;
12269 }
12270 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012271 regmatch.startp[i],
12272 (int)(regmatch.endp[i] - regmatch.startp[i]))
12273 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012274 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012275 }
12276 }
12277 else if (type == 2)
12278 {
12279 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012280 if (l != NULL)
12281 copy_tv(&li->li_tv, rettv);
12282 else
12283 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012285 }
12286 else if (l != NULL)
12287 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 else
12289 {
12290 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 (varnumber_T)(regmatch.startp[0] - str);
12293 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012296 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 }
12298 }
12299 vim_free(regmatch.regprog);
12300 }
12301
12302theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 p_cpo = save_cpo;
12305}
12306
12307/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012308 * "match()" function
12309 */
12310 static void
12311f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012312 typval_T *argvars;
12313 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314{
12315 find_some_match(argvars, rettv, 1);
12316}
12317
12318/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012319 * "matcharg()" function
12320 */
12321 static void
12322f_matcharg(argvars, rettv)
12323 typval_T *argvars;
12324 typval_T *rettv;
12325{
12326 if (rettv_list_alloc(rettv) == OK)
12327 {
12328#ifdef FEAT_SEARCH_EXTRA
12329 int mi = get_tv_number(&argvars[0]);
12330
12331 if (mi >= 1 && mi <= 3)
12332 {
12333 list_append_string(rettv->vval.v_list,
12334 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12335 list_append_string(rettv->vval.v_list,
12336 curwin->w_match_pat[mi - 1], -1);
12337 }
12338#endif
12339 }
12340}
12341
12342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012343 * "matchend()" function
12344 */
12345 static void
12346f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012347 typval_T *argvars;
12348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012349{
12350 find_some_match(argvars, rettv, 0);
12351}
12352
12353/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012354 * "matchlist()" function
12355 */
12356 static void
12357f_matchlist(argvars, rettv)
12358 typval_T *argvars;
12359 typval_T *rettv;
12360{
12361 find_some_match(argvars, rettv, 3);
12362}
12363
12364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365 * "matchstr()" function
12366 */
12367 static void
12368f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *argvars;
12370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371{
12372 find_some_match(argvars, rettv, 2);
12373}
12374
Bram Moolenaar33570922005-01-25 22:26:29 +000012375static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012376
12377 static void
12378max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 typval_T *argvars;
12380 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012381 int domax;
12382{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012383 long n = 0;
12384 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012386
12387 if (argvars[0].v_type == VAR_LIST)
12388 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012389 list_T *l;
12390 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012391
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012392 l = argvars[0].vval.v_list;
12393 if (l != NULL)
12394 {
12395 li = l->lv_first;
12396 if (li != NULL)
12397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012398 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012399 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012400 {
12401 li = li->li_next;
12402 if (li == NULL)
12403 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012405 if (domax ? i > n : i < n)
12406 n = i;
12407 }
12408 }
12409 }
12410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012411 else if (argvars[0].v_type == VAR_DICT)
12412 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012413 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012414 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012415 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012416 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012417
12418 d = argvars[0].vval.v_dict;
12419 if (d != NULL)
12420 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012421 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012425 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012426 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012427 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012428 if (first)
12429 {
12430 n = i;
12431 first = FALSE;
12432 }
12433 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012434 n = i;
12435 }
12436 }
12437 }
12438 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012439 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012440 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012441 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012442}
12443
12444/*
12445 * "max()" function
12446 */
12447 static void
12448f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012451{
12452 max_min(argvars, rettv, TRUE);
12453}
12454
12455/*
12456 * "min()" function
12457 */
12458 static void
12459f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012460 typval_T *argvars;
12461 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012462{
12463 max_min(argvars, rettv, FALSE);
12464}
12465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012466static int mkdir_recurse __ARGS((char_u *dir, int prot));
12467
12468/*
12469 * Create the directory in which "dir" is located, and higher levels when
12470 * needed.
12471 */
12472 static int
12473mkdir_recurse(dir, prot)
12474 char_u *dir;
12475 int prot;
12476{
12477 char_u *p;
12478 char_u *updir;
12479 int r = FAIL;
12480
12481 /* Get end of directory name in "dir".
12482 * We're done when it's "/" or "c:/". */
12483 p = gettail_sep(dir);
12484 if (p <= get_past_head(dir))
12485 return OK;
12486
12487 /* If the directory exists we're done. Otherwise: create it.*/
12488 updir = vim_strnsave(dir, (int)(p - dir));
12489 if (updir == NULL)
12490 return FAIL;
12491 if (mch_isdir(updir))
12492 r = OK;
12493 else if (mkdir_recurse(updir, prot) == OK)
12494 r = vim_mkdir_emsg(updir, prot);
12495 vim_free(updir);
12496 return r;
12497}
12498
12499#ifdef vim_mkdir
12500/*
12501 * "mkdir()" function
12502 */
12503 static void
12504f_mkdir(argvars, rettv)
12505 typval_T *argvars;
12506 typval_T *rettv;
12507{
12508 char_u *dir;
12509 char_u buf[NUMBUFLEN];
12510 int prot = 0755;
12511
12512 rettv->vval.v_number = FAIL;
12513 if (check_restricted() || check_secure())
12514 return;
12515
12516 dir = get_tv_string_buf(&argvars[0], buf);
12517 if (argvars[1].v_type != VAR_UNKNOWN)
12518 {
12519 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012520 prot = get_tv_number_chk(&argvars[2], NULL);
12521 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012522 mkdir_recurse(dir, prot);
12523 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012524 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012525}
12526#endif
12527
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 * "mode()" function
12530 */
12531/*ARGSUSED*/
12532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
12537 char_u buf[2];
12538
12539#ifdef FEAT_VISUAL
12540 if (VIsual_active)
12541 {
12542 if (VIsual_select)
12543 buf[0] = VIsual_mode + 's' - 'v';
12544 else
12545 buf[0] = VIsual_mode;
12546 }
12547 else
12548#endif
12549 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12550 buf[0] = 'r';
12551 else if (State & INSERT)
12552 {
12553 if (State & REPLACE_FLAG)
12554 buf[0] = 'R';
12555 else
12556 buf[0] = 'i';
12557 }
12558 else if (State & CMDLINE)
12559 buf[0] = 'c';
12560 else
12561 buf[0] = 'n';
12562
12563 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->vval.v_string = vim_strsave(buf);
12565 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566}
12567
12568/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012569 * "nextnonblank()" function
12570 */
12571 static void
12572f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012573 typval_T *argvars;
12574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012575{
12576 linenr_T lnum;
12577
12578 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581 {
12582 lnum = 0;
12583 break;
12584 }
12585 if (*skipwhite(ml_get(lnum)) != NUL)
12586 break;
12587 }
12588 rettv->vval.v_number = lnum;
12589}
12590
12591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 * "nr2char()" function
12593 */
12594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012595f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012596 typval_T *argvars;
12597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598{
12599 char_u buf[NUMBUFLEN];
12600
12601#ifdef FEAT_MBYTE
12602 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604 else
12605#endif
12606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 buf[1] = NUL;
12609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 rettv->v_type = VAR_STRING;
12611 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012612}
12613
12614/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012615 * "pathshorten()" function
12616 */
12617 static void
12618f_pathshorten(argvars, rettv)
12619 typval_T *argvars;
12620 typval_T *rettv;
12621{
12622 char_u *p;
12623
12624 rettv->v_type = VAR_STRING;
12625 p = get_tv_string_chk(&argvars[0]);
12626 if (p == NULL)
12627 rettv->vval.v_string = NULL;
12628 else
12629 {
12630 p = vim_strsave(p);
12631 rettv->vval.v_string = p;
12632 if (p != NULL)
12633 shorten_dir(p);
12634 }
12635}
12636
12637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012638 * "prevnonblank()" function
12639 */
12640 static void
12641f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012642 typval_T *argvars;
12643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012644{
12645 linenr_T lnum;
12646
12647 lnum = get_tv_lnum(argvars);
12648 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12649 lnum = 0;
12650 else
12651 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12652 --lnum;
12653 rettv->vval.v_number = lnum;
12654}
12655
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012656#ifdef HAVE_STDARG_H
12657/* This dummy va_list is here because:
12658 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12659 * - locally in the function results in a "used before set" warning
12660 * - using va_start() to initialize it gives "function with fixed args" error */
12661static va_list ap;
12662#endif
12663
Bram Moolenaar8c711452005-01-14 21:53:12 +000012664/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012665 * "printf()" function
12666 */
12667 static void
12668f_printf(argvars, rettv)
12669 typval_T *argvars;
12670 typval_T *rettv;
12671{
12672 rettv->v_type = VAR_STRING;
12673 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012674#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012675 {
12676 char_u buf[NUMBUFLEN];
12677 int len;
12678 char_u *s;
12679 int saved_did_emsg = did_emsg;
12680 char *fmt;
12681
12682 /* Get the required length, allocate the buffer and do it for real. */
12683 did_emsg = FALSE;
12684 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012685 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012686 if (!did_emsg)
12687 {
12688 s = alloc(len + 1);
12689 if (s != NULL)
12690 {
12691 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012692 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012693 }
12694 }
12695 did_emsg |= saved_did_emsg;
12696 }
12697#endif
12698}
12699
12700/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012701 * "pumvisible()" function
12702 */
12703/*ARGSUSED*/
12704 static void
12705f_pumvisible(argvars, rettv)
12706 typval_T *argvars;
12707 typval_T *rettv;
12708{
12709 rettv->vval.v_number = 0;
12710#ifdef FEAT_INS_EXPAND
12711 if (pum_visible())
12712 rettv->vval.v_number = 1;
12713#endif
12714}
12715
12716/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012717 * "range()" function
12718 */
12719 static void
12720f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012721 typval_T *argvars;
12722 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723{
12724 long start;
12725 long end;
12726 long stride = 1;
12727 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012731 if (argvars[1].v_type == VAR_UNKNOWN)
12732 {
12733 end = start - 1;
12734 start = 0;
12735 }
12736 else
12737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012739 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012741 }
12742
12743 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 if (error)
12745 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012746 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012747 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012748 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012749 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012750 else
12751 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012752 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012754 if (list_append_number(rettv->vval.v_list,
12755 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012756 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012757 }
12758}
12759
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012760/*
12761 * "readfile()" function
12762 */
12763 static void
12764f_readfile(argvars, rettv)
12765 typval_T *argvars;
12766 typval_T *rettv;
12767{
12768 int binary = FALSE;
12769 char_u *fname;
12770 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012771 listitem_T *li;
12772#define FREAD_SIZE 200 /* optimized for text lines */
12773 char_u buf[FREAD_SIZE];
12774 int readlen; /* size of last fread() */
12775 int buflen; /* nr of valid chars in buf[] */
12776 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12777 int tolist; /* first byte in buf[] still to be put in list */
12778 int chop; /* how many CR to chop off */
12779 char_u *prev = NULL; /* previously read bytes, if any */
12780 int prevlen = 0; /* length of "prev" if not NULL */
12781 char_u *s;
12782 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012783 long maxline = MAXLNUM;
12784 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012785
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012786 if (argvars[1].v_type != VAR_UNKNOWN)
12787 {
12788 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12789 binary = TRUE;
12790 if (argvars[2].v_type != VAR_UNKNOWN)
12791 maxline = get_tv_number(&argvars[2]);
12792 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012793
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012794 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012795 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012796
12797 /* Always open the file in binary mode, library functions have a mind of
12798 * their own about CR-LF conversion. */
12799 fname = get_tv_string(&argvars[0]);
12800 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12801 {
12802 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12803 return;
12804 }
12805
12806 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012807 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012808 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012809 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012810 buflen = filtd + readlen;
12811 tolist = 0;
12812 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12813 {
12814 if (buf[filtd] == '\n' || readlen <= 0)
12815 {
12816 /* Only when in binary mode add an empty list item when the
12817 * last line ends in a '\n'. */
12818 if (!binary && readlen == 0 && filtd == 0)
12819 break;
12820
12821 /* Found end-of-line or end-of-file: add a text line to the
12822 * list. */
12823 chop = 0;
12824 if (!binary)
12825 while (filtd - chop - 1 >= tolist
12826 && buf[filtd - chop - 1] == '\r')
12827 ++chop;
12828 len = filtd - tolist - chop;
12829 if (prev == NULL)
12830 s = vim_strnsave(buf + tolist, len);
12831 else
12832 {
12833 s = alloc((unsigned)(prevlen + len + 1));
12834 if (s != NULL)
12835 {
12836 mch_memmove(s, prev, prevlen);
12837 vim_free(prev);
12838 prev = NULL;
12839 mch_memmove(s + prevlen, buf + tolist, len);
12840 s[prevlen + len] = NUL;
12841 }
12842 }
12843 tolist = filtd + 1;
12844
12845 li = listitem_alloc();
12846 if (li == NULL)
12847 {
12848 vim_free(s);
12849 break;
12850 }
12851 li->li_tv.v_type = VAR_STRING;
12852 li->li_tv.v_lock = 0;
12853 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012854 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012855
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012856 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012857 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012858 if (readlen <= 0)
12859 break;
12860 }
12861 else if (buf[filtd] == NUL)
12862 buf[filtd] = '\n';
12863 }
12864 if (readlen <= 0)
12865 break;
12866
12867 if (tolist == 0)
12868 {
12869 /* "buf" is full, need to move text to an allocated buffer */
12870 if (prev == NULL)
12871 {
12872 prev = vim_strnsave(buf, buflen);
12873 prevlen = buflen;
12874 }
12875 else
12876 {
12877 s = alloc((unsigned)(prevlen + buflen));
12878 if (s != NULL)
12879 {
12880 mch_memmove(s, prev, prevlen);
12881 mch_memmove(s + prevlen, buf, buflen);
12882 vim_free(prev);
12883 prev = s;
12884 prevlen += buflen;
12885 }
12886 }
12887 filtd = 0;
12888 }
12889 else
12890 {
12891 mch_memmove(buf, buf + tolist, buflen - tolist);
12892 filtd -= tolist;
12893 }
12894 }
12895
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012896 /*
12897 * For a negative line count use only the lines at the end of the file,
12898 * free the rest.
12899 */
12900 if (maxline < 0)
12901 while (cnt > -maxline)
12902 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012903 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012904 --cnt;
12905 }
12906
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012907 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012908 fclose(fd);
12909}
12910
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012911#if defined(FEAT_RELTIME)
12912static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12913
12914/*
12915 * Convert a List to proftime_T.
12916 * Return FAIL when there is something wrong.
12917 */
12918 static int
12919list2proftime(arg, tm)
12920 typval_T *arg;
12921 proftime_T *tm;
12922{
12923 long n1, n2;
12924 int error = FALSE;
12925
12926 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12927 || arg->vval.v_list->lv_len != 2)
12928 return FAIL;
12929 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12930 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12931# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012932 tm->HighPart = n1;
12933 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012934# else
12935 tm->tv_sec = n1;
12936 tm->tv_usec = n2;
12937# endif
12938 return error ? FAIL : OK;
12939}
12940#endif /* FEAT_RELTIME */
12941
12942/*
12943 * "reltime()" function
12944 */
12945 static void
12946f_reltime(argvars, rettv)
12947 typval_T *argvars;
12948 typval_T *rettv;
12949{
12950#ifdef FEAT_RELTIME
12951 proftime_T res;
12952 proftime_T start;
12953
12954 if (argvars[0].v_type == VAR_UNKNOWN)
12955 {
12956 /* No arguments: get current time. */
12957 profile_start(&res);
12958 }
12959 else if (argvars[1].v_type == VAR_UNKNOWN)
12960 {
12961 if (list2proftime(&argvars[0], &res) == FAIL)
12962 return;
12963 profile_end(&res);
12964 }
12965 else
12966 {
12967 /* Two arguments: compute the difference. */
12968 if (list2proftime(&argvars[0], &start) == FAIL
12969 || list2proftime(&argvars[1], &res) == FAIL)
12970 return;
12971 profile_sub(&res, &start);
12972 }
12973
12974 if (rettv_list_alloc(rettv) == OK)
12975 {
12976 long n1, n2;
12977
12978# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012979 n1 = res.HighPart;
12980 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012981# else
12982 n1 = res.tv_sec;
12983 n2 = res.tv_usec;
12984# endif
12985 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12986 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12987 }
12988#endif
12989}
12990
12991/*
12992 * "reltimestr()" function
12993 */
12994 static void
12995f_reltimestr(argvars, rettv)
12996 typval_T *argvars;
12997 typval_T *rettv;
12998{
12999#ifdef FEAT_RELTIME
13000 proftime_T tm;
13001#endif
13002
13003 rettv->v_type = VAR_STRING;
13004 rettv->vval.v_string = NULL;
13005#ifdef FEAT_RELTIME
13006 if (list2proftime(&argvars[0], &tm) == OK)
13007 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13008#endif
13009}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013010
Bram Moolenaar0d660222005-01-07 21:51:51 +000013011#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13012static void make_connection __ARGS((void));
13013static int check_connection __ARGS((void));
13014
13015 static void
13016make_connection()
13017{
13018 if (X_DISPLAY == NULL
13019# ifdef FEAT_GUI
13020 && !gui.in_use
13021# endif
13022 )
13023 {
13024 x_force_connect = TRUE;
13025 setup_term_clip();
13026 x_force_connect = FALSE;
13027 }
13028}
13029
13030 static int
13031check_connection()
13032{
13033 make_connection();
13034 if (X_DISPLAY == NULL)
13035 {
13036 EMSG(_("E240: No connection to Vim server"));
13037 return FAIL;
13038 }
13039 return OK;
13040}
13041#endif
13042
13043#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013044static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013045
13046 static void
13047remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *argvars;
13049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013050 int expr;
13051{
13052 char_u *server_name;
13053 char_u *keys;
13054 char_u *r = NULL;
13055 char_u buf[NUMBUFLEN];
13056# ifdef WIN32
13057 HWND w;
13058# else
13059 Window w;
13060# endif
13061
13062 if (check_restricted() || check_secure())
13063 return;
13064
13065# ifdef FEAT_X11
13066 if (check_connection() == FAIL)
13067 return;
13068# endif
13069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 server_name = get_tv_string_chk(&argvars[0]);
13071 if (server_name == NULL)
13072 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013073 keys = get_tv_string_buf(&argvars[1], buf);
13074# ifdef WIN32
13075 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13076# else
13077 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13078 < 0)
13079# endif
13080 {
13081 if (r != NULL)
13082 EMSG(r); /* sending worked but evaluation failed */
13083 else
13084 EMSG2(_("E241: Unable to send to %s"), server_name);
13085 return;
13086 }
13087
13088 rettv->vval.v_string = r;
13089
13090 if (argvars[2].v_type != VAR_UNKNOWN)
13091 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013092 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013093 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013095
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013096 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013097 v.di_tv.v_type = VAR_STRING;
13098 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 idvar = get_tv_string_chk(&argvars[2]);
13100 if (idvar != NULL)
13101 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013102 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013103 }
13104}
13105#endif
13106
13107/*
13108 * "remote_expr()" function
13109 */
13110/*ARGSUSED*/
13111 static void
13112f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *argvars;
13114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013115{
13116 rettv->v_type = VAR_STRING;
13117 rettv->vval.v_string = NULL;
13118#ifdef FEAT_CLIENTSERVER
13119 remote_common(argvars, rettv, TRUE);
13120#endif
13121}
13122
13123/*
13124 * "remote_foreground()" function
13125 */
13126/*ARGSUSED*/
13127 static void
13128f_remote_foreground(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 rettv->vval.v_number = 0;
13133#ifdef FEAT_CLIENTSERVER
13134# ifdef WIN32
13135 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013136 {
13137 char_u *server_name = get_tv_string_chk(&argvars[0]);
13138
13139 if (server_name != NULL)
13140 serverForeground(server_name);
13141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142# else
13143 /* Send a foreground() expression to the server. */
13144 argvars[1].v_type = VAR_STRING;
13145 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13146 argvars[2].v_type = VAR_UNKNOWN;
13147 remote_common(argvars, rettv, TRUE);
13148 vim_free(argvars[1].vval.v_string);
13149# endif
13150#endif
13151}
13152
13153/*ARGSUSED*/
13154 static void
13155f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013156 typval_T *argvars;
13157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158{
13159#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013160 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013161 char_u *s = NULL;
13162# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013163 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013164# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013166
13167 if (check_restricted() || check_secure())
13168 {
13169 rettv->vval.v_number = -1;
13170 return;
13171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013172 serverid = get_tv_string_chk(&argvars[0]);
13173 if (serverid == NULL)
13174 {
13175 rettv->vval.v_number = -1;
13176 return; /* type error; errmsg already given */
13177 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013179 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013180 if (n == 0)
13181 rettv->vval.v_number = -1;
13182 else
13183 {
13184 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13185 rettv->vval.v_number = (s != NULL);
13186 }
13187# else
13188 rettv->vval.v_number = 0;
13189 if (check_connection() == FAIL)
13190 return;
13191
13192 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013194# endif
13195
13196 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013198 char_u *retvar;
13199
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 v.di_tv.v_type = VAR_STRING;
13201 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013202 retvar = get_tv_string_chk(&argvars[1]);
13203 if (retvar != NULL)
13204 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013205 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206 }
13207#else
13208 rettv->vval.v_number = -1;
13209#endif
13210}
13211
13212/*ARGSUSED*/
13213 static void
13214f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 typval_T *argvars;
13216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013217{
13218 char_u *r = NULL;
13219
13220#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 char_u *serverid = get_tv_string_chk(&argvars[0]);
13222
13223 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013224 {
13225# ifdef WIN32
13226 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013227 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013228
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013229 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 if (n != 0)
13231 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13232 if (r == NULL)
13233# else
13234 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013236# endif
13237 EMSG(_("E277: Unable to read a server reply"));
13238 }
13239#endif
13240 rettv->v_type = VAR_STRING;
13241 rettv->vval.v_string = r;
13242}
13243
13244/*
13245 * "remote_send()" function
13246 */
13247/*ARGSUSED*/
13248 static void
13249f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *argvars;
13251 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013252{
13253 rettv->v_type = VAR_STRING;
13254 rettv->vval.v_string = NULL;
13255#ifdef FEAT_CLIENTSERVER
13256 remote_common(argvars, rettv, FALSE);
13257#endif
13258}
13259
13260/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013261 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 */
13263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 typval_T *argvars;
13266 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013267{
Bram Moolenaar33570922005-01-25 22:26:29 +000013268 list_T *l;
13269 listitem_T *item, *item2;
13270 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013271 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013272 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013273 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 dict_T *d;
13275 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013276
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013277 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013278 if (argvars[0].v_type == VAR_DICT)
13279 {
13280 if (argvars[2].v_type != VAR_UNKNOWN)
13281 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013282 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013283 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 key = get_tv_string_chk(&argvars[1]);
13286 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288 di = dict_find(d, key, -1);
13289 if (di == NULL)
13290 EMSG2(_(e_dictkey), key);
13291 else
13292 {
13293 *rettv = di->di_tv;
13294 init_tv(&di->di_tv);
13295 dictitem_remove(d, di);
13296 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013297 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013298 }
13299 }
13300 else if (argvars[0].v_type != VAR_LIST)
13301 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013302 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013303 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 int error = FALSE;
13306
13307 idx = get_tv_number_chk(&argvars[1], &error);
13308 if (error)
13309 ; /* type error: do nothing, errmsg already given */
13310 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 EMSGN(_(e_listidx), idx);
13312 else
13313 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013314 if (argvars[2].v_type == VAR_UNKNOWN)
13315 {
13316 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013317 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013318 *rettv = item->li_tv;
13319 vim_free(item);
13320 }
13321 else
13322 {
13323 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 end = get_tv_number_chk(&argvars[2], &error);
13325 if (error)
13326 ; /* type error: do nothing */
13327 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013328 EMSGN(_(e_listidx), end);
13329 else
13330 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013331 int cnt = 0;
13332
13333 for (li = item; li != NULL; li = li->li_next)
13334 {
13335 ++cnt;
13336 if (li == item2)
13337 break;
13338 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013339 if (li == NULL) /* didn't find "item2" after "item" */
13340 EMSG(_(e_invrange));
13341 else
13342 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013343 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013344 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013345 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013346 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013347 l->lv_first = item;
13348 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013349 item->li_prev = NULL;
13350 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013351 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013352 }
13353 }
13354 }
13355 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356 }
13357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358}
13359
13360/*
13361 * "rename({from}, {to})" function
13362 */
13363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *argvars;
13366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367{
13368 char_u buf[NUMBUFLEN];
13369
13370 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013373 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13374 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375}
13376
13377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013378 * "repeat()" function
13379 */
13380/*ARGSUSED*/
13381 static void
13382f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *argvars;
13384 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013385{
13386 char_u *p;
13387 int n;
13388 int slen;
13389 int len;
13390 char_u *r;
13391 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013392
13393 n = get_tv_number(&argvars[1]);
13394 if (argvars[0].v_type == VAR_LIST)
13395 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013396 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013397 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013398 if (list_extend(rettv->vval.v_list,
13399 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013400 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013401 }
13402 else
13403 {
13404 p = get_tv_string(&argvars[0]);
13405 rettv->v_type = VAR_STRING;
13406 rettv->vval.v_string = NULL;
13407
13408 slen = (int)STRLEN(p);
13409 len = slen * n;
13410 if (len <= 0)
13411 return;
13412
13413 r = alloc(len + 1);
13414 if (r != NULL)
13415 {
13416 for (i = 0; i < n; i++)
13417 mch_memmove(r + i * slen, p, (size_t)slen);
13418 r[len] = NUL;
13419 }
13420
13421 rettv->vval.v_string = r;
13422 }
13423}
13424
13425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 * "resolve()" function
13427 */
13428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013430 typval_T *argvars;
13431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432{
13433 char_u *p;
13434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436#ifdef FEAT_SHORTCUT
13437 {
13438 char_u *v = NULL;
13439
13440 v = mch_resolve_shortcut(p);
13441 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 }
13446#else
13447# ifdef HAVE_READLINK
13448 {
13449 char_u buf[MAXPATHL + 1];
13450 char_u *cpy;
13451 int len;
13452 char_u *remain = NULL;
13453 char_u *q;
13454 int is_relative_to_current = FALSE;
13455 int has_trailing_pathsep = FALSE;
13456 int limit = 100;
13457
13458 p = vim_strsave(p);
13459
13460 if (p[0] == '.' && (vim_ispathsep(p[1])
13461 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13462 is_relative_to_current = TRUE;
13463
13464 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013465 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 has_trailing_pathsep = TRUE;
13467
13468 q = getnextcomp(p);
13469 if (*q != NUL)
13470 {
13471 /* Separate the first path component in "p", and keep the
13472 * remainder (beginning with the path separator). */
13473 remain = vim_strsave(q - 1);
13474 q[-1] = NUL;
13475 }
13476
13477 for (;;)
13478 {
13479 for (;;)
13480 {
13481 len = readlink((char *)p, (char *)buf, MAXPATHL);
13482 if (len <= 0)
13483 break;
13484 buf[len] = NUL;
13485
13486 if (limit-- == 0)
13487 {
13488 vim_free(p);
13489 vim_free(remain);
13490 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 goto fail;
13493 }
13494
13495 /* Ensure that the result will have a trailing path separator
13496 * if the argument has one. */
13497 if (remain == NULL && has_trailing_pathsep)
13498 add_pathsep(buf);
13499
13500 /* Separate the first path component in the link value and
13501 * concatenate the remainders. */
13502 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13503 if (*q != NUL)
13504 {
13505 if (remain == NULL)
13506 remain = vim_strsave(q - 1);
13507 else
13508 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013509 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 if (cpy != NULL)
13511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 vim_free(remain);
13513 remain = cpy;
13514 }
13515 }
13516 q[-1] = NUL;
13517 }
13518
13519 q = gettail(p);
13520 if (q > p && *q == NUL)
13521 {
13522 /* Ignore trailing path separator. */
13523 q[-1] = NUL;
13524 q = gettail(p);
13525 }
13526 if (q > p && !mch_isFullName(buf))
13527 {
13528 /* symlink is relative to directory of argument */
13529 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13530 if (cpy != NULL)
13531 {
13532 STRCPY(cpy, p);
13533 STRCPY(gettail(cpy), buf);
13534 vim_free(p);
13535 p = cpy;
13536 }
13537 }
13538 else
13539 {
13540 vim_free(p);
13541 p = vim_strsave(buf);
13542 }
13543 }
13544
13545 if (remain == NULL)
13546 break;
13547
13548 /* Append the first path component of "remain" to "p". */
13549 q = getnextcomp(remain + 1);
13550 len = q - remain - (*q != NUL);
13551 cpy = vim_strnsave(p, STRLEN(p) + len);
13552 if (cpy != NULL)
13553 {
13554 STRNCAT(cpy, remain, len);
13555 vim_free(p);
13556 p = cpy;
13557 }
13558 /* Shorten "remain". */
13559 if (*q != NUL)
13560 STRCPY(remain, q - 1);
13561 else
13562 {
13563 vim_free(remain);
13564 remain = NULL;
13565 }
13566 }
13567
13568 /* If the result is a relative path name, make it explicitly relative to
13569 * the current directory if and only if the argument had this form. */
13570 if (!vim_ispathsep(*p))
13571 {
13572 if (is_relative_to_current
13573 && *p != NUL
13574 && !(p[0] == '.'
13575 && (p[1] == NUL
13576 || vim_ispathsep(p[1])
13577 || (p[1] == '.'
13578 && (p[2] == NUL
13579 || vim_ispathsep(p[2]))))))
13580 {
13581 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013582 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 if (cpy != NULL)
13584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 vim_free(p);
13586 p = cpy;
13587 }
13588 }
13589 else if (!is_relative_to_current)
13590 {
13591 /* Strip leading "./". */
13592 q = p;
13593 while (q[0] == '.' && vim_ispathsep(q[1]))
13594 q += 2;
13595 if (q > p)
13596 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13597 }
13598 }
13599
13600 /* Ensure that the result will have no trailing path separator
13601 * if the argument had none. But keep "/" or "//". */
13602 if (!has_trailing_pathsep)
13603 {
13604 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013605 if (after_pathsep(p, q))
13606 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 }
13608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 }
13611# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613# endif
13614#endif
13615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617
13618#ifdef HAVE_READLINK
13619fail:
13620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622}
13623
13624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 */
13627 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013628f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 typval_T *argvars;
13630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631{
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 list_T *l;
13633 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634
Bram Moolenaar0d660222005-01-07 21:51:51 +000013635 rettv->vval.v_number = 0;
13636 if (argvars[0].v_type != VAR_LIST)
13637 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013638 else if ((l = argvars[0].vval.v_list) != NULL
13639 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013640 {
13641 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013642 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013643 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644 while (li != NULL)
13645 {
13646 ni = li->li_prev;
13647 list_append(l, li);
13648 li = ni;
13649 }
13650 rettv->vval.v_list = l;
13651 rettv->v_type = VAR_LIST;
13652 ++l->lv_refcount;
13653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013656#define SP_NOMOVE 0x01 /* don't move cursor */
13657#define SP_REPEAT 0x02 /* repeat to find outer pair */
13658#define SP_RETCOUNT 0x04 /* return matchcount */
13659#define SP_SETPCMARK 0x08 /* set previous context mark */
13660#define SP_START 0x10 /* accept match at start position */
13661#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13662#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013663
Bram Moolenaar33570922005-01-25 22:26:29 +000013664static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013665
13666/*
13667 * Get flags for a search function.
13668 * Possibly sets "p_ws".
13669 * Returns BACKWARD, FORWARD or zero (for an error).
13670 */
13671 static int
13672get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013674 int *flagsp;
13675{
13676 int dir = FORWARD;
13677 char_u *flags;
13678 char_u nbuf[NUMBUFLEN];
13679 int mask;
13680
13681 if (varp->v_type != VAR_UNKNOWN)
13682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 flags = get_tv_string_buf_chk(varp, nbuf);
13684 if (flags == NULL)
13685 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 while (*flags != NUL)
13687 {
13688 switch (*flags)
13689 {
13690 case 'b': dir = BACKWARD; break;
13691 case 'w': p_ws = TRUE; break;
13692 case 'W': p_ws = FALSE; break;
13693 default: mask = 0;
13694 if (flagsp != NULL)
13695 switch (*flags)
13696 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013697 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013698 case 'e': mask = SP_END; break;
13699 case 'm': mask = SP_RETCOUNT; break;
13700 case 'n': mask = SP_NOMOVE; break;
13701 case 'p': mask = SP_SUBPAT; break;
13702 case 'r': mask = SP_REPEAT; break;
13703 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013704 }
13705 if (mask == 0)
13706 {
13707 EMSG2(_(e_invarg2), flags);
13708 dir = 0;
13709 }
13710 else
13711 *flagsp |= mask;
13712 }
13713 if (dir == 0)
13714 break;
13715 ++flags;
13716 }
13717 }
13718 return dir;
13719}
13720
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013722 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013724 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013725search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013726 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013727 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013728 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013730 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 char_u *pat;
13732 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013733 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 int save_p_ws = p_ws;
13735 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013736 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013737 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013738 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013739 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013742 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013743 if (dir == 0)
13744 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013745 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013746 if (flags & SP_START)
13747 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013748 if (flags & SP_END)
13749 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013750
13751 /* Optional extra argument: line number to stop searching. */
13752 if (argvars[1].v_type != VAR_UNKNOWN
13753 && argvars[2].v_type != VAR_UNKNOWN)
13754 {
13755 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13756 if (lnum_stop < 0)
13757 goto theend;
13758 }
13759
Bram Moolenaar231334e2005-07-25 20:46:57 +000013760 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013761 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013762 * Check to make sure only those flags are set.
13763 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13764 * flags cannot be set. Check for that condition also.
13765 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013766 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013767 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013769 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013770 goto theend;
13771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013773 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013774 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13775 options, RE_SEARCH, (linenr_T)lnum_stop);
13776 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013778 if (flags & SP_SUBPAT)
13779 retval = subpatnum;
13780 else
13781 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013782 if (flags & SP_SETPCMARK)
13783 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013785 if (match_pos != NULL)
13786 {
13787 /* Store the match cursor position */
13788 match_pos->lnum = pos.lnum;
13789 match_pos->col = pos.col + 1;
13790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791 /* "/$" will put the cursor after the end of the line, may need to
13792 * correct that here */
13793 check_cursor();
13794 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013795
13796 /* If 'n' flag is used: restore cursor position. */
13797 if (flags & SP_NOMOVE)
13798 curwin->w_cursor = save_cursor;
13799theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013801
13802 return retval;
13803}
13804
13805/*
13806 * "search()" function
13807 */
13808 static void
13809f_search(argvars, rettv)
13810 typval_T *argvars;
13811 typval_T *rettv;
13812{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013813 int flags = 0;
13814
13815 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816}
13817
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013819 * "searchdecl()" function
13820 */
13821 static void
13822f_searchdecl(argvars, rettv)
13823 typval_T *argvars;
13824 typval_T *rettv;
13825{
13826 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013827 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013828 int error = FALSE;
13829 char_u *name;
13830
13831 rettv->vval.v_number = 1; /* default: FAIL */
13832
13833 name = get_tv_string_chk(&argvars[0]);
13834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013835 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013836 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013837 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13838 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13839 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013840 if (!error && name != NULL)
13841 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013842 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013843}
13844
13845/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013846 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013848 static int
13849searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013851 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852{
13853 char_u *spat, *mpat, *epat;
13854 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 int dir;
13857 int flags = 0;
13858 char_u nbuf1[NUMBUFLEN];
13859 char_u nbuf2[NUMBUFLEN];
13860 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013861 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013862 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 spat = get_tv_string_chk(&argvars[0]);
13866 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13867 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13868 if (spat == NULL || mpat == NULL || epat == NULL)
13869 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 /* Handle the optional fourth argument: flags */
13872 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013873 if (dir == 0)
13874 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013875
13876 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013877 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13878 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013879 if ((flags & (SP_END | SP_SUBPAT)) != 0
13880 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013881 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013882 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013883 goto theend;
13884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013886 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013887 if (argvars[3].v_type == VAR_UNKNOWN
13888 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 skip = (char_u *)"";
13890 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013893 if (argvars[5].v_type != VAR_UNKNOWN)
13894 {
13895 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13896 if (lnum_stop < 0)
13897 goto theend;
13898 }
13899 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 if (skip == NULL)
13901 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013903 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13904 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013905
13906theend:
13907 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013908
13909 return retval;
13910}
13911
13912/*
13913 * "searchpair()" function
13914 */
13915 static void
13916f_searchpair(argvars, rettv)
13917 typval_T *argvars;
13918 typval_T *rettv;
13919{
13920 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13921}
13922
13923/*
13924 * "searchpairpos()" function
13925 */
13926 static void
13927f_searchpairpos(argvars, rettv)
13928 typval_T *argvars;
13929 typval_T *rettv;
13930{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013931 pos_T match_pos;
13932 int lnum = 0;
13933 int col = 0;
13934
13935 rettv->vval.v_number = 0;
13936
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013937 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013938 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013939
13940 if (searchpair_cmn(argvars, &match_pos) > 0)
13941 {
13942 lnum = match_pos.lnum;
13943 col = match_pos.col;
13944 }
13945
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013946 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13947 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013948}
13949
13950/*
13951 * Search for a start/middle/end thing.
13952 * Used by searchpair(), see its documentation for the details.
13953 * Returns 0 or -1 for no match,
13954 */
13955 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013956do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013957 char_u *spat; /* start pattern */
13958 char_u *mpat; /* middle pattern */
13959 char_u *epat; /* end pattern */
13960 int dir; /* BACKWARD or FORWARD */
13961 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013962 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013963 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013964 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013965{
13966 char_u *save_cpo;
13967 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13968 long retval = 0;
13969 pos_T pos;
13970 pos_T firstpos;
13971 pos_T foundpos;
13972 pos_T save_cursor;
13973 pos_T save_pos;
13974 int n;
13975 int r;
13976 int nest = 1;
13977 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013978 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013979
13980 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13981 save_cpo = p_cpo;
13982 p_cpo = (char_u *)"";
13983
13984 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13985 * start/middle/end (pat3, for the top pair). */
13986 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13987 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13988 if (pat2 == NULL || pat3 == NULL)
13989 goto theend;
13990 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13991 if (*mpat == NUL)
13992 STRCPY(pat3, pat2);
13993 else
13994 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13995 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013996 if (flags & SP_START)
13997 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013998
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 save_cursor = curwin->w_cursor;
14000 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014001 clearpos(&firstpos);
14002 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 pat = pat3;
14004 for (;;)
14005 {
14006 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014007 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14009 /* didn't find it or found the first match again: FAIL */
14010 break;
14011
14012 if (firstpos.lnum == 0)
14013 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014014 if (equalpos(pos, foundpos))
14015 {
14016 /* Found the same position again. Can happen with a pattern that
14017 * has "\zs" at the end and searching backwards. Advance one
14018 * character and try again. */
14019 if (dir == BACKWARD)
14020 decl(&pos);
14021 else
14022 incl(&pos);
14023 }
14024 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025
14026 /* If the skip pattern matches, ignore this match. */
14027 if (*skip != NUL)
14028 {
14029 save_pos = curwin->w_cursor;
14030 curwin->w_cursor = pos;
14031 r = eval_to_bool(skip, &err, NULL, FALSE);
14032 curwin->w_cursor = save_pos;
14033 if (err)
14034 {
14035 /* Evaluating {skip} caused an error, break here. */
14036 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014037 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 break;
14039 }
14040 if (r)
14041 continue;
14042 }
14043
14044 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14045 {
14046 /* Found end when searching backwards or start when searching
14047 * forward: nested pair. */
14048 ++nest;
14049 pat = pat2; /* nested, don't search for middle */
14050 }
14051 else
14052 {
14053 /* Found end when searching forward or start when searching
14054 * backward: end of (nested) pair; or found middle in outer pair. */
14055 if (--nest == 1)
14056 pat = pat3; /* outer level, search for middle */
14057 }
14058
14059 if (nest == 0)
14060 {
14061 /* Found the match: return matchcount or line number. */
14062 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014063 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014065 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014066 if (flags & SP_SETPCMARK)
14067 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 curwin->w_cursor = pos;
14069 if (!(flags & SP_REPEAT))
14070 break;
14071 nest = 1; /* search for next unmatched */
14072 }
14073 }
14074
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014075 if (match_pos != NULL)
14076 {
14077 /* Store the match cursor position */
14078 match_pos->lnum = curwin->w_cursor.lnum;
14079 match_pos->col = curwin->w_cursor.col + 1;
14080 }
14081
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014083 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 curwin->w_cursor = save_cursor;
14085
14086theend:
14087 vim_free(pat2);
14088 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014090
14091 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092}
14093
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014094/*
14095 * "searchpos()" function
14096 */
14097 static void
14098f_searchpos(argvars, rettv)
14099 typval_T *argvars;
14100 typval_T *rettv;
14101{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014102 pos_T match_pos;
14103 int lnum = 0;
14104 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014105 int n;
14106 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014107
14108 rettv->vval.v_number = 0;
14109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014111 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014112
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014113 n = search_cmn(argvars, &match_pos, &flags);
14114 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014115 {
14116 lnum = match_pos.lnum;
14117 col = match_pos.col;
14118 }
14119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014120 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14121 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014122 if (flags & SP_SUBPAT)
14123 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014124}
14125
14126
Bram Moolenaar0d660222005-01-07 21:51:51 +000014127/*ARGSUSED*/
14128 static void
14129f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *argvars;
14131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133#ifdef FEAT_CLIENTSERVER
14134 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 char_u *server = get_tv_string_chk(&argvars[0]);
14136 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 if (server == NULL || reply == NULL)
14140 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014141 if (check_restricted() || check_secure())
14142 return;
14143# ifdef FEAT_X11
14144 if (check_connection() == FAIL)
14145 return;
14146# endif
14147
14148 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014150 EMSG(_("E258: Unable to send to client"));
14151 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014153 rettv->vval.v_number = 0;
14154#else
14155 rettv->vval.v_number = -1;
14156#endif
14157}
14158
14159/*ARGSUSED*/
14160 static void
14161f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 typval_T *argvars;
14163 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014164{
14165 char_u *r = NULL;
14166
14167#ifdef FEAT_CLIENTSERVER
14168# ifdef WIN32
14169 r = serverGetVimNames();
14170# else
14171 make_connection();
14172 if (X_DISPLAY != NULL)
14173 r = serverGetVimNames(X_DISPLAY);
14174# endif
14175#endif
14176 rettv->v_type = VAR_STRING;
14177 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178}
14179
14180/*
14181 * "setbufvar()" function
14182 */
14183/*ARGSUSED*/
14184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014186 typval_T *argvars;
14187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188{
14189 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 char_u nbuf[NUMBUFLEN];
14194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 rettv->vval.v_number = 0;
14196
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 if (check_restricted() || check_secure())
14198 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14200 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014201 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 varp = &argvars[2];
14203
14204 if (buf != NULL && varname != NULL && varp != NULL)
14205 {
14206 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208
14209 if (*varname == '&')
14210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 long numval;
14212 char_u *strval;
14213 int error = FALSE;
14214
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 numval = get_tv_number_chk(varp, &error);
14217 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 if (!error && strval != NULL)
14219 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 }
14221 else
14222 {
14223 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14224 if (bufvarname != NULL)
14225 {
14226 STRCPY(bufvarname, "b:");
14227 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014228 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 vim_free(bufvarname);
14230 }
14231 }
14232
14233 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236}
14237
14238/*
14239 * "setcmdpos()" function
14240 */
14241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242f_setcmdpos(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{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 int pos = (int)get_tv_number(&argvars[0]) - 1;
14247
14248 if (pos >= 0)
14249 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250}
14251
14252/*
14253 * "setline()" function
14254 */
14255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014257 typval_T *argvars;
14258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259{
14260 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014261 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014262 list_T *l = NULL;
14263 listitem_T *li = NULL;
14264 long added = 0;
14265 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014267 lnum = get_tv_lnum(&argvars[0]);
14268 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014270 l = argvars[1].vval.v_list;
14271 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014273 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014275
14276 rettv->vval.v_number = 0; /* OK */
14277 for (;;)
14278 {
14279 if (l != NULL)
14280 {
14281 /* list argument, get next string */
14282 if (li == NULL)
14283 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014285 li = li->li_next;
14286 }
14287
14288 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014290 break;
14291 if (lnum <= curbuf->b_ml.ml_line_count)
14292 {
14293 /* existing line, replace it */
14294 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14295 {
14296 changed_bytes(lnum, 0);
14297 check_cursor_col();
14298 rettv->vval.v_number = 0; /* OK */
14299 }
14300 }
14301 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14302 {
14303 /* lnum is one past the last line, append the line */
14304 ++added;
14305 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14306 rettv->vval.v_number = 0; /* OK */
14307 }
14308
14309 if (l == NULL) /* only one string argument */
14310 break;
14311 ++lnum;
14312 }
14313
14314 if (added > 0)
14315 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316}
14317
14318/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014319 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014320 */
14321/*ARGSUSED*/
14322 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014323set_qf_ll_list(wp, list_arg, action_arg, rettv)
14324 win_T *wp;
14325 typval_T *list_arg;
14326 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014327 typval_T *rettv;
14328{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014329#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014330 char_u *act;
14331 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014332#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014333
Bram Moolenaar2641f772005-03-25 21:58:17 +000014334 rettv->vval.v_number = -1;
14335
14336#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014337 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014338 EMSG(_(e_listreq));
14339 else
14340 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014341 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014342
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014343 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014344 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014345 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 if (act == NULL)
14347 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014348 if (*act == 'a' || *act == 'r')
14349 action = *act;
14350 }
14351
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014352 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014353 rettv->vval.v_number = 0;
14354 }
14355#endif
14356}
14357
14358/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014359 * "setloclist()" function
14360 */
14361/*ARGSUSED*/
14362 static void
14363f_setloclist(argvars, rettv)
14364 typval_T *argvars;
14365 typval_T *rettv;
14366{
14367 win_T *win;
14368
14369 rettv->vval.v_number = -1;
14370
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014371 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014372 if (win != NULL)
14373 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14374}
14375
14376/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014377 * "setpos()" function
14378 */
14379/*ARGSUSED*/
14380 static void
14381f_setpos(argvars, rettv)
14382 typval_T *argvars;
14383 typval_T *rettv;
14384{
14385 pos_T pos;
14386 int fnum;
14387 char_u *name;
14388
14389 name = get_tv_string_chk(argvars);
14390 if (name != NULL)
14391 {
14392 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14393 {
14394 --pos.col;
14395 if (name[0] == '.') /* cursor */
14396 {
14397 if (fnum == curbuf->b_fnum)
14398 {
14399 curwin->w_cursor = pos;
14400 check_cursor();
14401 }
14402 else
14403 EMSG(_(e_invarg));
14404 }
14405 else if (name[0] == '\'') /* mark */
14406 (void)setmark_pos(name[1], &pos, fnum);
14407 else
14408 EMSG(_(e_invarg));
14409 }
14410 }
14411}
14412
14413/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014414 * "setqflist()" function
14415 */
14416/*ARGSUSED*/
14417 static void
14418f_setqflist(argvars, rettv)
14419 typval_T *argvars;
14420 typval_T *rettv;
14421{
14422 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14423}
14424
14425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 * "setreg()" function
14427 */
14428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 typval_T *argvars;
14431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432{
14433 int regname;
14434 char_u *strregname;
14435 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014436 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 int append;
14438 char_u yank_type;
14439 long block_len;
14440
14441 block_len = -1;
14442 yank_type = MAUTO;
14443 append = FALSE;
14444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014446 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 if (strregname == NULL)
14449 return; /* type error; errmsg already given */
14450 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 if (regname == 0 || regname == '@')
14452 regname = '"';
14453 else if (regname == '=')
14454 return;
14455
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014458 stropt = get_tv_string_chk(&argvars[2]);
14459 if (stropt == NULL)
14460 return; /* type error */
14461 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 switch (*stropt)
14463 {
14464 case 'a': case 'A': /* append */
14465 append = TRUE;
14466 break;
14467 case 'v': case 'c': /* character-wise selection */
14468 yank_type = MCHAR;
14469 break;
14470 case 'V': case 'l': /* line-wise selection */
14471 yank_type = MLINE;
14472 break;
14473#ifdef FEAT_VISUAL
14474 case 'b': case Ctrl_V: /* block-wise selection */
14475 yank_type = MBLOCK;
14476 if (VIM_ISDIGIT(stropt[1]))
14477 {
14478 ++stropt;
14479 block_len = getdigits(&stropt) - 1;
14480 --stropt;
14481 }
14482 break;
14483#endif
14484 }
14485 }
14486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014487 strval = get_tv_string_chk(&argvars[1]);
14488 if (strval != NULL)
14489 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492}
14493
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014494/*
14495 * "settabwinvar()" function
14496 */
14497 static void
14498f_settabwinvar(argvars, rettv)
14499 typval_T *argvars;
14500 typval_T *rettv;
14501{
14502 setwinvar(argvars, rettv, 1);
14503}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504
14505/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014506 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 typval_T *argvars;
14511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014513 setwinvar(argvars, rettv, 0);
14514}
14515
14516/*
14517 * "setwinvar()" and "settabwinvar()" functions
14518 */
14519 static void
14520setwinvar(argvars, rettv, off)
14521 typval_T *argvars;
14522 typval_T *rettv;
14523 int off;
14524{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 win_T *win;
14526#ifdef FEAT_WINDOWS
14527 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014528 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529#endif
14530 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014531 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014533 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 rettv->vval.v_number = 0;
14536
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 if (check_restricted() || check_secure())
14538 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014539
14540#ifdef FEAT_WINDOWS
14541 if (off == 1)
14542 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14543 else
14544 tp = curtab;
14545#endif
14546 win = find_win_by_nr(&argvars[off], tp);
14547 varname = get_tv_string_chk(&argvars[off + 1]);
14548 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
14550 if (win != NULL && varname != NULL && varp != NULL)
14551 {
14552#ifdef FEAT_WINDOWS
14553 /* set curwin to be our win, temporarily */
14554 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014555 save_curtab = curtab;
14556 goto_tabpage_tp(tp);
14557 if (!win_valid(win))
14558 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 curwin = win;
14560 curbuf = curwin->w_buffer;
14561#endif
14562
14563 if (*varname == '&')
14564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 long numval;
14566 char_u *strval;
14567 int error = FALSE;
14568
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 numval = get_tv_number_chk(varp, &error);
14571 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 if (!error && strval != NULL)
14573 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 }
14575 else
14576 {
14577 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14578 if (winvarname != NULL)
14579 {
14580 STRCPY(winvarname, "w:");
14581 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014582 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 vim_free(winvarname);
14584 }
14585 }
14586
14587#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014588 /* Restore current tabpage and window, if still valid (autocomands can
14589 * make them invalid). */
14590 if (valid_tabpage(save_curtab))
14591 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 if (win_valid(save_curwin))
14593 {
14594 curwin = save_curwin;
14595 curbuf = curwin->w_buffer;
14596 }
14597#endif
14598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599}
14600
14601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 */
14604 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *argvars;
14607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611 p = get_tv_string(&argvars[0]);
14612 rettv->vval.v_string = vim_strsave(p);
14613 simplify_filename(rettv->vval.v_string); /* simplify in place */
14614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615}
14616
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617static int
14618#ifdef __BORLANDC__
14619 _RTLENTRYF
14620#endif
14621 item_compare __ARGS((const void *s1, const void *s2));
14622static int
14623#ifdef __BORLANDC__
14624 _RTLENTRYF
14625#endif
14626 item_compare2 __ARGS((const void *s1, const void *s2));
14627
14628static int item_compare_ic;
14629static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631#define ITEM_COMPARE_FAIL 999
14632
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014636 static int
14637#ifdef __BORLANDC__
14638_RTLENTRYF
14639#endif
14640item_compare(s1, s2)
14641 const void *s1;
14642 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 char_u *p1, *p2;
14645 char_u *tofree1, *tofree2;
14646 int res;
14647 char_u numbuf1[NUMBUFLEN];
14648 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014650 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14651 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652 if (item_compare_ic)
14653 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 res = STRCMP(p1, p2);
14656 vim_free(tofree1);
14657 vim_free(tofree2);
14658 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659}
14660
14661 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662#ifdef __BORLANDC__
14663_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665item_compare2(s1, s2)
14666 const void *s1;
14667 const void *s2;
14668{
14669 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014671 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014674 /* shortcut after failure in previous call; compare all items equal */
14675 if (item_compare_func_err)
14676 return 0;
14677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014678 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14679 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14681 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682
14683 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014684 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014685 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686 clear_tv(&argv[0]);
14687 clear_tv(&argv[1]);
14688
14689 if (res == FAIL)
14690 res = ITEM_COMPARE_FAIL;
14691 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 /* return value has wrong type */
14693 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14694 if (item_compare_func_err)
14695 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 clear_tv(&rettv);
14697 return res;
14698}
14699
14700/*
14701 * "sort({list})" function
14702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014705 typval_T *argvars;
14706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707{
Bram Moolenaar33570922005-01-25 22:26:29 +000014708 list_T *l;
14709 listitem_T *li;
14710 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 long len;
14712 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714 rettv->vval.v_number = 0;
14715 if (argvars[0].v_type != VAR_LIST)
14716 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717 else
14718 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014720 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014721 return;
14722 rettv->vval.v_list = l;
14723 rettv->v_type = VAR_LIST;
14724 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 len = list_len(l);
14727 if (len <= 1)
14728 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730 item_compare_ic = FALSE;
14731 item_compare_func = NULL;
14732 if (argvars[1].v_type != VAR_UNKNOWN)
14733 {
14734 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 else
14737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 int error = FALSE;
14739
14740 i = get_tv_number_chk(&argvars[1], &error);
14741 if (error)
14742 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014743 if (i == 1)
14744 item_compare_ic = TRUE;
14745 else
14746 item_compare_func = get_tv_string(&argvars[1]);
14747 }
14748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014751 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014752 if (ptrs == NULL)
14753 return;
14754 i = 0;
14755 for (li = l->lv_first; li != NULL; li = li->li_next)
14756 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014759 /* test the compare function */
14760 if (item_compare_func != NULL
14761 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14762 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014763 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765 {
14766 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014767 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014768 item_compare_func == NULL ? item_compare : item_compare2);
14769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014770 if (!item_compare_func_err)
14771 {
14772 /* Clear the List and append the items in the sorted order. */
14773 l->lv_first = l->lv_last = NULL;
14774 l->lv_len = 0;
14775 for (i = 0; i < len; ++i)
14776 list_append(l, ptrs[i]);
14777 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 }
14779
14780 vim_free(ptrs);
14781 }
14782}
14783
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014784/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014785 * "soundfold({word})" function
14786 */
14787 static void
14788f_soundfold(argvars, rettv)
14789 typval_T *argvars;
14790 typval_T *rettv;
14791{
14792 char_u *s;
14793
14794 rettv->v_type = VAR_STRING;
14795 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014796#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014797 rettv->vval.v_string = eval_soundfold(s);
14798#else
14799 rettv->vval.v_string = vim_strsave(s);
14800#endif
14801}
14802
14803/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014804 * "spellbadword()" function
14805 */
14806/* ARGSUSED */
14807 static void
14808f_spellbadword(argvars, rettv)
14809 typval_T *argvars;
14810 typval_T *rettv;
14811{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014812 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014813 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014814 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014815
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014816 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014817 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014818
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014819#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014820 if (argvars[0].v_type == VAR_UNKNOWN)
14821 {
14822 /* Find the start and length of the badly spelled word. */
14823 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14824 if (len != 0)
14825 word = ml_get_cursor();
14826 }
14827 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14828 {
14829 char_u *str = get_tv_string_chk(&argvars[0]);
14830 int capcol = -1;
14831
14832 if (str != NULL)
14833 {
14834 /* Check the argument for spelling. */
14835 while (*str != NUL)
14836 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014837 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014838 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014839 {
14840 word = str;
14841 break;
14842 }
14843 str += len;
14844 }
14845 }
14846 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014847#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014848
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014849 list_append_string(rettv->vval.v_list, word, len);
14850 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014851 attr == HLF_SPB ? "bad" :
14852 attr == HLF_SPR ? "rare" :
14853 attr == HLF_SPL ? "local" :
14854 attr == HLF_SPC ? "caps" :
14855 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014856}
14857
14858/*
14859 * "spellsuggest()" function
14860 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014861/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014862 static void
14863f_spellsuggest(argvars, rettv)
14864 typval_T *argvars;
14865 typval_T *rettv;
14866{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014867#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014868 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014869 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014870 int maxcount;
14871 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014872 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014873 listitem_T *li;
14874 int need_capital = FALSE;
14875#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014876
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014877 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014878 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014879
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014880#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014881 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14882 {
14883 str = get_tv_string(&argvars[0]);
14884 if (argvars[1].v_type != VAR_UNKNOWN)
14885 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014886 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014887 if (maxcount <= 0)
14888 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014889 if (argvars[2].v_type != VAR_UNKNOWN)
14890 {
14891 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14892 if (typeerr)
14893 return;
14894 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014895 }
14896 else
14897 maxcount = 25;
14898
Bram Moolenaar4770d092006-01-12 23:22:24 +000014899 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014900
14901 for (i = 0; i < ga.ga_len; ++i)
14902 {
14903 str = ((char_u **)ga.ga_data)[i];
14904
14905 li = listitem_alloc();
14906 if (li == NULL)
14907 vim_free(str);
14908 else
14909 {
14910 li->li_tv.v_type = VAR_STRING;
14911 li->li_tv.v_lock = 0;
14912 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014913 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014914 }
14915 }
14916 ga_clear(&ga);
14917 }
14918#endif
14919}
14920
Bram Moolenaar0d660222005-01-07 21:51:51 +000014921 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014922f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 typval_T *argvars;
14924 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014925{
14926 char_u *str;
14927 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014928 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014929 regmatch_T regmatch;
14930 char_u patbuf[NUMBUFLEN];
14931 char_u *save_cpo;
14932 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014934 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014935 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014936
14937 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14938 save_cpo = p_cpo;
14939 p_cpo = (char_u *)"";
14940
14941 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014942 if (argvars[1].v_type != VAR_UNKNOWN)
14943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14945 if (pat == NULL)
14946 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014947 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014948 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014949 }
14950 if (pat == NULL || *pat == NUL)
14951 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014952
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014953 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 if (typeerr)
14956 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14959 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014962 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014964 if (*str == NUL)
14965 match = FALSE; /* empty item at the end */
14966 else
14967 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 if (match)
14969 end = regmatch.startp[0];
14970 else
14971 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014972 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14973 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014975 if (list_append_string(rettv->vval.v_list, str,
14976 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 }
14979 if (!match)
14980 break;
14981 /* Advance to just after the match. */
14982 if (regmatch.endp[0] > str)
14983 col = 0;
14984 else
14985 {
14986 /* Don't get stuck at the same match. */
14987#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014988 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989#else
14990 col = 1;
14991#endif
14992 }
14993 str = regmatch.endp[0];
14994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998
Bram Moolenaar0d660222005-01-07 21:51:51 +000014999 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000}
15001
Bram Moolenaar2c932302006-03-18 21:42:09 +000015002/*
15003 * "str2nr()" function
15004 */
15005 static void
15006f_str2nr(argvars, rettv)
15007 typval_T *argvars;
15008 typval_T *rettv;
15009{
15010 int base = 10;
15011 char_u *p;
15012 long n;
15013
15014 if (argvars[1].v_type != VAR_UNKNOWN)
15015 {
15016 base = get_tv_number(&argvars[1]);
15017 if (base != 8 && base != 10 && base != 16)
15018 {
15019 EMSG(_(e_invarg));
15020 return;
15021 }
15022 }
15023
15024 p = skipwhite(get_tv_string(&argvars[0]));
15025 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15026 rettv->vval.v_number = n;
15027}
15028
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029#ifdef HAVE_STRFTIME
15030/*
15031 * "strftime({format}[, {time}])" function
15032 */
15033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 typval_T *argvars;
15036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037{
15038 char_u result_buf[256];
15039 struct tm *curtime;
15040 time_t seconds;
15041 char_u *p;
15042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015046 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 seconds = time(NULL);
15048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 curtime = localtime(&seconds);
15051 /* MSVC returns NULL for an invalid value of seconds. */
15052 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 else
15055 {
15056# ifdef FEAT_MBYTE
15057 vimconv_T conv;
15058 char_u *enc;
15059
15060 conv.vc_type = CONV_NONE;
15061 enc = enc_locale();
15062 convert_setup(&conv, p_enc, enc);
15063 if (conv.vc_type != CONV_NONE)
15064 p = string_convert(&conv, p, NULL);
15065# endif
15066 if (p != NULL)
15067 (void)strftime((char *)result_buf, sizeof(result_buf),
15068 (char *)p, curtime);
15069 else
15070 result_buf[0] = NUL;
15071
15072# ifdef FEAT_MBYTE
15073 if (conv.vc_type != CONV_NONE)
15074 vim_free(p);
15075 convert_setup(&conv, enc, p_enc);
15076 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 else
15079# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015080 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081
15082# ifdef FEAT_MBYTE
15083 /* Release conversion descriptors */
15084 convert_setup(&conv, NULL, NULL);
15085 vim_free(enc);
15086# endif
15087 }
15088}
15089#endif
15090
15091/*
15092 * "stridx()" function
15093 */
15094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015095f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 typval_T *argvars;
15097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098{
15099 char_u buf[NUMBUFLEN];
15100 char_u *needle;
15101 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015106 needle = get_tv_string_chk(&argvars[1]);
15107 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015109 if (needle == NULL || haystack == NULL)
15110 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 if (argvars[2].v_type != VAR_UNKNOWN)
15113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015114 int error = FALSE;
15115
15116 start_idx = get_tv_number_chk(&argvars[2], &error);
15117 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015119 if (start_idx >= 0)
15120 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 }
15122
15123 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15124 if (pos != NULL)
15125 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126}
15127
15128/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015129 * "string()" function
15130 */
15131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015133 typval_T *argvars;
15134 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015135{
15136 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015137 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015140 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015141 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015142 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143}
15144
15145/*
15146 * "strlen()" function
15147 */
15148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015150 typval_T *argvars;
15151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015153 rettv->vval.v_number = (varnumber_T)(STRLEN(
15154 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155}
15156
15157/*
15158 * "strpart()" function
15159 */
15160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 typval_T *argvars;
15163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164{
15165 char_u *p;
15166 int n;
15167 int len;
15168 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015169 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015171 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 slen = (int)STRLEN(p);
15173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015174 n = get_tv_number_chk(&argvars[1], &error);
15175 if (error)
15176 len = 0;
15177 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 else
15180 len = slen - n; /* default len: all bytes that are available. */
15181
15182 /*
15183 * Only return the overlap between the specified part and the actual
15184 * string.
15185 */
15186 if (n < 0)
15187 {
15188 len += n;
15189 n = 0;
15190 }
15191 else if (n > slen)
15192 n = slen;
15193 if (len < 0)
15194 len = 0;
15195 else if (n + len > slen)
15196 len = slen - n;
15197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015198 rettv->v_type = VAR_STRING;
15199 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200}
15201
15202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203 * "strridx()" function
15204 */
15205 static void
15206f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *argvars;
15208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209{
15210 char_u buf[NUMBUFLEN];
15211 char_u *needle;
15212 char_u *haystack;
15213 char_u *rest;
15214 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015215 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 needle = get_tv_string_chk(&argvars[1]);
15218 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219
15220 rettv->vval.v_number = -1;
15221 if (needle == NULL || haystack == NULL)
15222 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015223
15224 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015225 if (argvars[2].v_type != VAR_UNKNOWN)
15226 {
15227 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015229 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015231 }
15232 else
15233 end_idx = haystack_len;
15234
Bram Moolenaar0d660222005-01-07 21:51:51 +000015235 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015236 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015238 lastmatch = haystack + end_idx;
15239 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015241 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242 for (rest = haystack; *rest != '\0'; ++rest)
15243 {
15244 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015245 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246 break;
15247 lastmatch = rest;
15248 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015249 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250
15251 if (lastmatch == NULL)
15252 rettv->vval.v_number = -1;
15253 else
15254 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15255}
15256
15257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 * "strtrans()" function
15259 */
15260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015261f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 typval_T *argvars;
15263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 rettv->v_type = VAR_STRING;
15266 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267}
15268
15269/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015270 * "submatch()" function
15271 */
15272 static void
15273f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 typval_T *argvars;
15275 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276{
15277 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 rettv->vval.v_string =
15279 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280}
15281
15282/*
15283 * "substitute()" function
15284 */
15285 static void
15286f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015287 typval_T *argvars;
15288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289{
15290 char_u patbuf[NUMBUFLEN];
15291 char_u subbuf[NUMBUFLEN];
15292 char_u flagsbuf[NUMBUFLEN];
15293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015294 char_u *str = get_tv_string_chk(&argvars[0]);
15295 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15296 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15297 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15298
Bram Moolenaar0d660222005-01-07 21:51:51 +000015299 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015300 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15301 rettv->vval.v_string = NULL;
15302 else
15303 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015304}
15305
15306/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015307 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 */
15309/*ARGSUSED*/
15310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015312 typval_T *argvars;
15313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314{
15315 int id = 0;
15316#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015317 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 long col;
15319 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015320 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015322 lnum = get_tv_lnum(argvars); /* -1 on type error */
15323 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15324 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015327 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015328 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329#endif
15330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332}
15333
15334/*
15335 * "synIDattr(id, what [, mode])" function
15336 */
15337/*ARGSUSED*/
15338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015340 typval_T *argvars;
15341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342{
15343 char_u *p = NULL;
15344#ifdef FEAT_SYN_HL
15345 int id;
15346 char_u *what;
15347 char_u *mode;
15348 char_u modebuf[NUMBUFLEN];
15349 int modec;
15350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 id = get_tv_number(&argvars[0]);
15352 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015353 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 modec = TOLOWER_ASC(mode[0]);
15357 if (modec != 't' && modec != 'c'
15358#ifdef FEAT_GUI
15359 && modec != 'g'
15360#endif
15361 )
15362 modec = 0; /* replace invalid with current */
15363 }
15364 else
15365 {
15366#ifdef FEAT_GUI
15367 if (gui.in_use)
15368 modec = 'g';
15369 else
15370#endif
15371 if (t_colors > 1)
15372 modec = 'c';
15373 else
15374 modec = 't';
15375 }
15376
15377
15378 switch (TOLOWER_ASC(what[0]))
15379 {
15380 case 'b':
15381 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15382 p = highlight_color(id, what, modec);
15383 else /* bold */
15384 p = highlight_has_attr(id, HL_BOLD, modec);
15385 break;
15386
15387 case 'f': /* fg[#] */
15388 p = highlight_color(id, what, modec);
15389 break;
15390
15391 case 'i':
15392 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15393 p = highlight_has_attr(id, HL_INVERSE, modec);
15394 else /* italic */
15395 p = highlight_has_attr(id, HL_ITALIC, modec);
15396 break;
15397
15398 case 'n': /* name */
15399 p = get_highlight_name(NULL, id - 1);
15400 break;
15401
15402 case 'r': /* reverse */
15403 p = highlight_has_attr(id, HL_INVERSE, modec);
15404 break;
15405
15406 case 's': /* standout */
15407 p = highlight_has_attr(id, HL_STANDOUT, modec);
15408 break;
15409
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015410 case 'u':
15411 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15412 /* underline */
15413 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15414 else
15415 /* undercurl */
15416 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 break;
15418 }
15419
15420 if (p != NULL)
15421 p = vim_strsave(p);
15422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 rettv->v_type = VAR_STRING;
15424 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425}
15426
15427/*
15428 * "synIDtrans(id)" function
15429 */
15430/*ARGSUSED*/
15431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015433 typval_T *argvars;
15434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435{
15436 int id;
15437
15438#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440
15441 if (id > 0)
15442 id = syn_get_final_id(id);
15443 else
15444#endif
15445 id = 0;
15446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448}
15449
15450/*
15451 * "system()" function
15452 */
15453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015454f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015455 typval_T *argvars;
15456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015458 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015460 char_u *infile = NULL;
15461 char_u buf[NUMBUFLEN];
15462 int err = FALSE;
15463 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015465 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015466 {
15467 /*
15468 * Write the string to a temp file, to be used for input of the shell
15469 * command.
15470 */
15471 if ((infile = vim_tempname('i')) == NULL)
15472 {
15473 EMSG(_(e_notmp));
15474 return;
15475 }
15476
15477 fd = mch_fopen((char *)infile, WRITEBIN);
15478 if (fd == NULL)
15479 {
15480 EMSG2(_(e_notopen), infile);
15481 goto done;
15482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 p = get_tv_string_buf_chk(&argvars[1], buf);
15484 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015485 {
15486 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015488 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015489 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15490 err = TRUE;
15491 if (fclose(fd) != 0)
15492 err = TRUE;
15493 if (err)
15494 {
15495 EMSG(_("E677: Error writing temp file"));
15496 goto done;
15497 }
15498 }
15499
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015500 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15501 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015502
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503#ifdef USE_CR
15504 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015505 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 {
15507 char_u *s;
15508
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015509 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 {
15511 if (*s == CAR)
15512 *s = NL;
15513 }
15514 }
15515#else
15516# ifdef USE_CRNL
15517 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015518 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 {
15520 char_u *s, *d;
15521
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015522 d = res;
15523 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 {
15525 if (s[0] == CAR && s[1] == NL)
15526 ++s;
15527 *d++ = *s;
15528 }
15529 *d = NUL;
15530 }
15531# endif
15532#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015533
15534done:
15535 if (infile != NULL)
15536 {
15537 mch_remove(infile);
15538 vim_free(infile);
15539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540 rettv->v_type = VAR_STRING;
15541 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542}
15543
15544/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015545 * "tabpagebuflist()" function
15546 */
15547/* ARGSUSED */
15548 static void
15549f_tabpagebuflist(argvars, rettv)
15550 typval_T *argvars;
15551 typval_T *rettv;
15552{
15553#ifndef FEAT_WINDOWS
15554 rettv->vval.v_number = 0;
15555#else
15556 tabpage_T *tp;
15557 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015558
15559 if (argvars[0].v_type == VAR_UNKNOWN)
15560 wp = firstwin;
15561 else
15562 {
15563 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15564 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015565 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015566 }
15567 if (wp == NULL)
15568 rettv->vval.v_number = 0;
15569 else
15570 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015571 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015572 rettv->vval.v_number = 0;
15573 else
15574 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015575 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015576 if (list_append_number(rettv->vval.v_list,
15577 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015578 break;
15579 }
15580 }
15581#endif
15582}
15583
15584
15585/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015586 * "tabpagenr()" function
15587 */
15588/* ARGSUSED */
15589 static void
15590f_tabpagenr(argvars, rettv)
15591 typval_T *argvars;
15592 typval_T *rettv;
15593{
15594 int nr = 1;
15595#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015596 char_u *arg;
15597
15598 if (argvars[0].v_type != VAR_UNKNOWN)
15599 {
15600 arg = get_tv_string_chk(&argvars[0]);
15601 nr = 0;
15602 if (arg != NULL)
15603 {
15604 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015605 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015606 else
15607 EMSG2(_(e_invexpr2), arg);
15608 }
15609 }
15610 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015611 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015612#endif
15613 rettv->vval.v_number = nr;
15614}
15615
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015616
15617#ifdef FEAT_WINDOWS
15618static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15619
15620/*
15621 * Common code for tabpagewinnr() and winnr().
15622 */
15623 static int
15624get_winnr(tp, argvar)
15625 tabpage_T *tp;
15626 typval_T *argvar;
15627{
15628 win_T *twin;
15629 int nr = 1;
15630 win_T *wp;
15631 char_u *arg;
15632
15633 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15634 if (argvar->v_type != VAR_UNKNOWN)
15635 {
15636 arg = get_tv_string_chk(argvar);
15637 if (arg == NULL)
15638 nr = 0; /* type error; errmsg already given */
15639 else if (STRCMP(arg, "$") == 0)
15640 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15641 else if (STRCMP(arg, "#") == 0)
15642 {
15643 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15644 if (twin == NULL)
15645 nr = 0;
15646 }
15647 else
15648 {
15649 EMSG2(_(e_invexpr2), arg);
15650 nr = 0;
15651 }
15652 }
15653
15654 if (nr > 0)
15655 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15656 wp != twin; wp = wp->w_next)
15657 ++nr;
15658 return nr;
15659}
15660#endif
15661
15662/*
15663 * "tabpagewinnr()" function
15664 */
15665/* ARGSUSED */
15666 static void
15667f_tabpagewinnr(argvars, rettv)
15668 typval_T *argvars;
15669 typval_T *rettv;
15670{
15671 int nr = 1;
15672#ifdef FEAT_WINDOWS
15673 tabpage_T *tp;
15674
15675 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15676 if (tp == NULL)
15677 nr = 0;
15678 else
15679 nr = get_winnr(tp, &argvars[1]);
15680#endif
15681 rettv->vval.v_number = nr;
15682}
15683
15684
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015685/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015686 * "tagfiles()" function
15687 */
15688/*ARGSUSED*/
15689 static void
15690f_tagfiles(argvars, rettv)
15691 typval_T *argvars;
15692 typval_T *rettv;
15693{
15694 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015695 tagname_T tn;
15696 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015697
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015698 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015699 {
15700 rettv->vval.v_number = 0;
15701 return;
15702 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 for (first = TRUE; ; first = FALSE)
15705 if (get_tagfname(&tn, first, fname) == FAIL
15706 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015707 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015709}
15710
15711/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015712 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015713 */
15714 static void
15715f_taglist(argvars, rettv)
15716 typval_T *argvars;
15717 typval_T *rettv;
15718{
15719 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015720
15721 tag_pattern = get_tv_string(&argvars[0]);
15722
15723 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 if (*tag_pattern == NUL)
15725 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015726
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015727 if (rettv_list_alloc(rettv) == OK)
15728 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015729}
15730
15731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 * "tempname()" function
15733 */
15734/*ARGSUSED*/
15735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015737 typval_T *argvars;
15738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739{
15740 static int x = 'A';
15741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742 rettv->v_type = VAR_STRING;
15743 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744
15745 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15746 * names. Skip 'I' and 'O', they are used for shell redirection. */
15747 do
15748 {
15749 if (x == 'Z')
15750 x = '0';
15751 else if (x == '9')
15752 x = 'A';
15753 else
15754 {
15755#ifdef EBCDIC
15756 if (x == 'I')
15757 x = 'J';
15758 else if (x == 'R')
15759 x = 'S';
15760 else
15761#endif
15762 ++x;
15763 }
15764 } while (x == 'I' || x == 'O');
15765}
15766
15767/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015768 * "test(list)" function: Just checking the walls...
15769 */
15770/*ARGSUSED*/
15771 static void
15772f_test(argvars, rettv)
15773 typval_T *argvars;
15774 typval_T *rettv;
15775{
15776 /* Used for unit testing. Change the code below to your liking. */
15777#if 0
15778 listitem_T *li;
15779 list_T *l;
15780 char_u *bad, *good;
15781
15782 if (argvars[0].v_type != VAR_LIST)
15783 return;
15784 l = argvars[0].vval.v_list;
15785 if (l == NULL)
15786 return;
15787 li = l->lv_first;
15788 if (li == NULL)
15789 return;
15790 bad = get_tv_string(&li->li_tv);
15791 li = li->li_next;
15792 if (li == NULL)
15793 return;
15794 good = get_tv_string(&li->li_tv);
15795 rettv->vval.v_number = test_edit_score(bad, good);
15796#endif
15797}
15798
15799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 * "tolower(string)" function
15801 */
15802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015803f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015804 typval_T *argvars;
15805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806{
15807 char_u *p;
15808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 p = vim_strsave(get_tv_string(&argvars[0]));
15810 rettv->v_type = VAR_STRING;
15811 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812
15813 if (p != NULL)
15814 while (*p != NUL)
15815 {
15816#ifdef FEAT_MBYTE
15817 int l;
15818
15819 if (enc_utf8)
15820 {
15821 int c, lc;
15822
15823 c = utf_ptr2char(p);
15824 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015825 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 /* TODO: reallocate string when byte count changes. */
15827 if (utf_char2len(lc) == l)
15828 utf_char2bytes(lc, p);
15829 p += l;
15830 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015831 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 p += l; /* skip multi-byte character */
15833 else
15834#endif
15835 {
15836 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15837 ++p;
15838 }
15839 }
15840}
15841
15842/*
15843 * "toupper(string)" function
15844 */
15845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 typval_T *argvars;
15848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015850 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015851 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852}
15853
15854/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015855 * "tr(string, fromstr, tostr)" function
15856 */
15857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015858f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015859 typval_T *argvars;
15860 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015861{
15862 char_u *instr;
15863 char_u *fromstr;
15864 char_u *tostr;
15865 char_u *p;
15866#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015867 int inlen;
15868 int fromlen;
15869 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015870 int idx;
15871 char_u *cpstr;
15872 int cplen;
15873 int first = TRUE;
15874#endif
15875 char_u buf[NUMBUFLEN];
15876 char_u buf2[NUMBUFLEN];
15877 garray_T ga;
15878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015879 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015880 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15881 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015882
15883 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015884 rettv->v_type = VAR_STRING;
15885 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015886 if (fromstr == NULL || tostr == NULL)
15887 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015888 ga_init2(&ga, (int)sizeof(char), 80);
15889
15890#ifdef FEAT_MBYTE
15891 if (!has_mbyte)
15892#endif
15893 /* not multi-byte: fromstr and tostr must be the same length */
15894 if (STRLEN(fromstr) != STRLEN(tostr))
15895 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015896#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015897error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015898#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015899 EMSG2(_(e_invarg2), fromstr);
15900 ga_clear(&ga);
15901 return;
15902 }
15903
15904 /* fromstr and tostr have to contain the same number of chars */
15905 while (*instr != NUL)
15906 {
15907#ifdef FEAT_MBYTE
15908 if (has_mbyte)
15909 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015910 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015911 cpstr = instr;
15912 cplen = inlen;
15913 idx = 0;
15914 for (p = fromstr; *p != NUL; p += fromlen)
15915 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015916 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015917 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15918 {
15919 for (p = tostr; *p != NUL; p += tolen)
15920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015921 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015922 if (idx-- == 0)
15923 {
15924 cplen = tolen;
15925 cpstr = p;
15926 break;
15927 }
15928 }
15929 if (*p == NUL) /* tostr is shorter than fromstr */
15930 goto error;
15931 break;
15932 }
15933 ++idx;
15934 }
15935
15936 if (first && cpstr == instr)
15937 {
15938 /* Check that fromstr and tostr have the same number of
15939 * (multi-byte) characters. Done only once when a character
15940 * of instr doesn't appear in fromstr. */
15941 first = FALSE;
15942 for (p = tostr; *p != NUL; p += tolen)
15943 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015944 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015945 --idx;
15946 }
15947 if (idx != 0)
15948 goto error;
15949 }
15950
15951 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015952 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015953 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015954
15955 instr += inlen;
15956 }
15957 else
15958#endif
15959 {
15960 /* When not using multi-byte chars we can do it faster. */
15961 p = vim_strchr(fromstr, *instr);
15962 if (p != NULL)
15963 ga_append(&ga, tostr[p - fromstr]);
15964 else
15965 ga_append(&ga, *instr);
15966 ++instr;
15967 }
15968 }
15969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015970 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015971}
15972
15973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 * "type(expr)" function
15975 */
15976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 typval_T *argvars;
15979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015981 int n;
15982
15983 switch (argvars[0].v_type)
15984 {
15985 case VAR_NUMBER: n = 0; break;
15986 case VAR_STRING: n = 1; break;
15987 case VAR_FUNC: n = 2; break;
15988 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015989 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015990 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15991 }
15992 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993}
15994
15995/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015996 * "values(dict)" function
15997 */
15998 static void
15999f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 typval_T *argvars;
16001 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016002{
16003 dict_list(argvars, rettv, 1);
16004}
16005
16006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 * "virtcol(string)" function
16008 */
16009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 typval_T *argvars;
16012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013{
16014 colnr_T vcol = 0;
16015 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016016 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016018 fp = var2fpos(&argvars[0], FALSE, &fnum);
16019 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16020 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 {
16022 getvvcol(curwin, fp, NULL, NULL, &vcol);
16023 ++vcol;
16024 }
16025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016026 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027}
16028
16029/*
16030 * "visualmode()" function
16031 */
16032/*ARGSUSED*/
16033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016034f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016035 typval_T *argvars;
16036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037{
16038#ifdef FEAT_VISUAL
16039 char_u str[2];
16040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 str[0] = curbuf->b_visual_mode_eval;
16043 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045
16046 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016047 if ((argvars[0].v_type == VAR_NUMBER
16048 && argvars[0].vval.v_number != 0)
16049 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 curbuf->b_visual_mode_eval = NUL;
16052#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016053 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054#endif
16055}
16056
16057/*
16058 * "winbufnr(nr)" function
16059 */
16060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016061f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016062 typval_T *argvars;
16063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064{
16065 win_T *wp;
16066
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016067 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016069 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016071 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072}
16073
16074/*
16075 * "wincol()" function
16076 */
16077/*ARGSUSED*/
16078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 typval_T *argvars;
16081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082{
16083 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085}
16086
16087/*
16088 * "winheight(nr)" function
16089 */
16090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016091f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016092 typval_T *argvars;
16093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094{
16095 win_T *wp;
16096
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016097 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016101 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102}
16103
16104/*
16105 * "winline()" function
16106 */
16107/*ARGSUSED*/
16108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 typval_T *argvars;
16111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112{
16113 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016114 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115}
16116
16117/*
16118 * "winnr()" function
16119 */
16120/* ARGSUSED */
16121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016122f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016123 typval_T *argvars;
16124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125{
16126 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016127
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016129 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132}
16133
16134/*
16135 * "winrestcmd()" function
16136 */
16137/* ARGSUSED */
16138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 typval_T *argvars;
16141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142{
16143#ifdef FEAT_WINDOWS
16144 win_T *wp;
16145 int winnr = 1;
16146 garray_T ga;
16147 char_u buf[50];
16148
16149 ga_init2(&ga, (int)sizeof(char), 70);
16150 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16151 {
16152 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16153 ga_concat(&ga, buf);
16154# ifdef FEAT_VERTSPLIT
16155 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16156 ga_concat(&ga, buf);
16157# endif
16158 ++winnr;
16159 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016160 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016162 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016164 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016166 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167}
16168
16169/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016170 * "winrestview()" function
16171 */
16172/* ARGSUSED */
16173 static void
16174f_winrestview(argvars, rettv)
16175 typval_T *argvars;
16176 typval_T *rettv;
16177{
16178 dict_T *dict;
16179
16180 if (argvars[0].v_type != VAR_DICT
16181 || (dict = argvars[0].vval.v_dict) == NULL)
16182 EMSG(_(e_invarg));
16183 else
16184 {
16185 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16186 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16187#ifdef FEAT_VIRTUALEDIT
16188 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16189#endif
16190 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016191 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016192
16193 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16194#ifdef FEAT_DIFF
16195 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16196#endif
16197 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16198 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16199
16200 check_cursor();
16201 changed_cline_bef_curs();
16202 invalidate_botline();
16203 redraw_later(VALID);
16204
16205 if (curwin->w_topline == 0)
16206 curwin->w_topline = 1;
16207 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16208 curwin->w_topline = curbuf->b_ml.ml_line_count;
16209#ifdef FEAT_DIFF
16210 check_topfill(curwin, TRUE);
16211#endif
16212 }
16213}
16214
16215/*
16216 * "winsaveview()" function
16217 */
16218/* ARGSUSED */
16219 static void
16220f_winsaveview(argvars, rettv)
16221 typval_T *argvars;
16222 typval_T *rettv;
16223{
16224 dict_T *dict;
16225
16226 dict = dict_alloc();
16227 if (dict == NULL)
16228 return;
16229 rettv->v_type = VAR_DICT;
16230 rettv->vval.v_dict = dict;
16231 ++dict->dv_refcount;
16232
16233 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16234 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16235#ifdef FEAT_VIRTUALEDIT
16236 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16237#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016238 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016239 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16240
16241 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16242#ifdef FEAT_DIFF
16243 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16244#endif
16245 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16246 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16247}
16248
16249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 * "winwidth(nr)" function
16251 */
16252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 typval_T *argvars;
16255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256{
16257 win_T *wp;
16258
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016259 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 else
16263#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016266 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267#endif
16268}
16269
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016271 * "writefile()" function
16272 */
16273 static void
16274f_writefile(argvars, rettv)
16275 typval_T *argvars;
16276 typval_T *rettv;
16277{
16278 int binary = FALSE;
16279 char_u *fname;
16280 FILE *fd;
16281 listitem_T *li;
16282 char_u *s;
16283 int ret = 0;
16284 int c;
16285
16286 if (argvars[0].v_type != VAR_LIST)
16287 {
16288 EMSG2(_(e_listarg), "writefile()");
16289 return;
16290 }
16291 if (argvars[0].vval.v_list == NULL)
16292 return;
16293
16294 if (argvars[2].v_type != VAR_UNKNOWN
16295 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16296 binary = TRUE;
16297
16298 /* Always open the file in binary mode, library functions have a mind of
16299 * their own about CR-LF conversion. */
16300 fname = get_tv_string(&argvars[1]);
16301 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16302 {
16303 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16304 ret = -1;
16305 }
16306 else
16307 {
16308 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16309 li = li->li_next)
16310 {
16311 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16312 {
16313 if (*s == '\n')
16314 c = putc(NUL, fd);
16315 else
16316 c = putc(*s, fd);
16317 if (c == EOF)
16318 {
16319 ret = -1;
16320 break;
16321 }
16322 }
16323 if (!binary || li->li_next != NULL)
16324 if (putc('\n', fd) == EOF)
16325 {
16326 ret = -1;
16327 break;
16328 }
16329 if (ret < 0)
16330 {
16331 EMSG(_(e_write));
16332 break;
16333 }
16334 }
16335 fclose(fd);
16336 }
16337
16338 rettv->vval.v_number = ret;
16339}
16340
16341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016343 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 */
16345 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016346var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016349 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016351 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016353 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
Bram Moolenaara5525202006-03-02 22:52:09 +000016355 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016356 if (varp->v_type == VAR_LIST)
16357 {
16358 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016359 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016360 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016361
16362 l = varp->vval.v_list;
16363 if (l == NULL)
16364 return NULL;
16365
16366 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016367 pos.lnum = list_find_nr(l, 0L, &error);
16368 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016369 return NULL; /* invalid line number */
16370
16371 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016372 pos.col = list_find_nr(l, 1L, &error);
16373 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016374 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016375 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016376 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016377 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016378 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016379 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016380
Bram Moolenaara5525202006-03-02 22:52:09 +000016381#ifdef FEAT_VIRTUALEDIT
16382 /* Get the virtual offset. Defaults to zero. */
16383 pos.coladd = list_find_nr(l, 2L, &error);
16384 if (error)
16385 pos.coladd = 0;
16386#endif
16387
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016388 return &pos;
16389 }
16390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 name = get_tv_string_chk(varp);
16392 if (name == NULL)
16393 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 if (name[0] == '.') /* cursor */
16395 return &curwin->w_cursor;
16396 if (name[0] == '\'') /* mark */
16397 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016398 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16400 return NULL;
16401 return pp;
16402 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016403
16404#ifdef FEAT_VIRTUALEDIT
16405 pos.coladd = 0;
16406#endif
16407
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016408 if (name[0] == 'w' && lnum)
16409 {
16410 pos.col = 0;
16411 if (name[1] == '0') /* "w0": first visible line */
16412 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016413 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016414 pos.lnum = curwin->w_topline;
16415 return &pos;
16416 }
16417 else if (name[1] == '$') /* "w$": last visible line */
16418 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016419 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016420 pos.lnum = curwin->w_botline - 1;
16421 return &pos;
16422 }
16423 }
16424 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 {
16426 if (lnum)
16427 {
16428 pos.lnum = curbuf->b_ml.ml_line_count;
16429 pos.col = 0;
16430 }
16431 else
16432 {
16433 pos.lnum = curwin->w_cursor.lnum;
16434 pos.col = (colnr_T)STRLEN(ml_get_curline());
16435 }
16436 return &pos;
16437 }
16438 return NULL;
16439}
16440
16441/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016442 * Convert list in "arg" into a position and optional file number.
16443 * When "fnump" is NULL there is no file number, only 3 items.
16444 * Note that the column is passed on as-is, the caller may want to decrement
16445 * it to use 1 for the first column.
16446 * Return FAIL when conversion is not possible, doesn't check the position for
16447 * validity.
16448 */
16449 static int
16450list2fpos(arg, posp, fnump)
16451 typval_T *arg;
16452 pos_T *posp;
16453 int *fnump;
16454{
16455 list_T *l = arg->vval.v_list;
16456 long i = 0;
16457 long n;
16458
Bram Moolenaarbde35262006-07-23 20:12:24 +000016459 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16460 * when "fnump" isn't NULL and "coladd" is optional. */
16461 if (arg->v_type != VAR_LIST
16462 || l == NULL
16463 || l->lv_len < (fnump == NULL ? 2 : 3)
16464 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016465 return FAIL;
16466
16467 if (fnump != NULL)
16468 {
16469 n = list_find_nr(l, i++, NULL); /* fnum */
16470 if (n < 0)
16471 return FAIL;
16472 if (n == 0)
16473 n = curbuf->b_fnum; /* current buffer */
16474 *fnump = n;
16475 }
16476
16477 n = list_find_nr(l, i++, NULL); /* lnum */
16478 if (n < 0)
16479 return FAIL;
16480 posp->lnum = n;
16481
16482 n = list_find_nr(l, i++, NULL); /* col */
16483 if (n < 0)
16484 return FAIL;
16485 posp->col = n;
16486
16487#ifdef FEAT_VIRTUALEDIT
16488 n = list_find_nr(l, i, NULL);
16489 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016490 posp->coladd = 0;
16491 else
16492 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016493#endif
16494
16495 return OK;
16496}
16497
16498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 * Get the length of an environment variable name.
16500 * Advance "arg" to the first character after the name.
16501 * Return 0 for error.
16502 */
16503 static int
16504get_env_len(arg)
16505 char_u **arg;
16506{
16507 char_u *p;
16508 int len;
16509
16510 for (p = *arg; vim_isIDc(*p); ++p)
16511 ;
16512 if (p == *arg) /* no name found */
16513 return 0;
16514
16515 len = (int)(p - *arg);
16516 *arg = p;
16517 return len;
16518}
16519
16520/*
16521 * Get the length of the name of a function or internal variable.
16522 * "arg" is advanced to the first non-white character after the name.
16523 * Return 0 if something is wrong.
16524 */
16525 static int
16526get_id_len(arg)
16527 char_u **arg;
16528{
16529 char_u *p;
16530 int len;
16531
16532 /* Find the end of the name. */
16533 for (p = *arg; eval_isnamec(*p); ++p)
16534 ;
16535 if (p == *arg) /* no name found */
16536 return 0;
16537
16538 len = (int)(p - *arg);
16539 *arg = skipwhite(p);
16540
16541 return len;
16542}
16543
16544/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016545 * Get the length of the name of a variable or function.
16546 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 * Return -1 if curly braces expansion failed.
16549 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 * If the name contains 'magic' {}'s, expand them and return the
16551 * expanded name in an allocated string via 'alias' - caller must free.
16552 */
16553 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 char_u **arg;
16556 char_u **alias;
16557 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016558 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559{
16560 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 char_u *p;
16562 char_u *expr_start;
16563 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564
16565 *alias = NULL; /* default to no alias */
16566
16567 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16568 && (*arg)[2] == (int)KE_SNR)
16569 {
16570 /* hard coded <SNR>, already translated */
16571 *arg += 3;
16572 return get_id_len(arg) + 3;
16573 }
16574 len = eval_fname_script(*arg);
16575 if (len > 0)
16576 {
16577 /* literal "<SID>", "s:" or "<SNR>" */
16578 *arg += len;
16579 }
16580
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016582 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016584 p = find_name_end(*arg, &expr_start, &expr_end,
16585 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 if (expr_start != NULL)
16587 {
16588 char_u *temp_string;
16589
16590 if (!evaluate)
16591 {
16592 len += (int)(p - *arg);
16593 *arg = skipwhite(p);
16594 return len;
16595 }
16596
16597 /*
16598 * Include any <SID> etc in the expanded string:
16599 * Thus the -len here.
16600 */
16601 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16602 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016603 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 *alias = temp_string;
16605 *arg = skipwhite(p);
16606 return (int)STRLEN(temp_string);
16607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608
16609 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016610 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 EMSG2(_(e_invexpr2), *arg);
16612
16613 return len;
16614}
16615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016616/*
16617 * Find the end of a variable or function name, taking care of magic braces.
16618 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16619 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016620 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 * Return a pointer to just after the name. Equal to "arg" if there is no
16622 * valid name.
16623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016625find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 char_u *arg;
16627 char_u **expr_start;
16628 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016629 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016631 int mb_nest = 0;
16632 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 char_u *p;
16634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 *expr_start = NULL;
16638 *expr_end = NULL;
16639 }
16640
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016641 /* Quick check for valid starting character. */
16642 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16643 return arg;
16644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 for (p = arg; *p != NUL
16646 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016647 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016648 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016649 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016650 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016652 if (*p == '\'')
16653 {
16654 /* skip over 'string' to avoid counting [ and ] inside it. */
16655 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16656 ;
16657 if (*p == NUL)
16658 break;
16659 }
16660 else if (*p == '"')
16661 {
16662 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16663 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16664 if (*p == '\\' && p[1] != NUL)
16665 ++p;
16666 if (*p == NUL)
16667 break;
16668 }
16669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016672 if (*p == '[')
16673 ++br_nest;
16674 else if (*p == ']')
16675 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 if (*p == '{')
16681 {
16682 mb_nest++;
16683 if (expr_start != NULL && *expr_start == NULL)
16684 *expr_start = p;
16685 }
16686 else if (*p == '}')
16687 {
16688 mb_nest--;
16689 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16690 *expr_end = p;
16691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 }
16694
16695 return p;
16696}
16697
16698/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016699 * Expands out the 'magic' {}'s in a variable/function name.
16700 * Note that this can call itself recursively, to deal with
16701 * constructs like foo{bar}{baz}{bam}
16702 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16703 * "in_start" ^
16704 * "expr_start" ^
16705 * "expr_end" ^
16706 * "in_end" ^
16707 *
16708 * Returns a new allocated string, which the caller must free.
16709 * Returns NULL for failure.
16710 */
16711 static char_u *
16712make_expanded_name(in_start, expr_start, expr_end, in_end)
16713 char_u *in_start;
16714 char_u *expr_start;
16715 char_u *expr_end;
16716 char_u *in_end;
16717{
16718 char_u c1;
16719 char_u *retval = NULL;
16720 char_u *temp_result;
16721 char_u *nextcmd = NULL;
16722
16723 if (expr_end == NULL || in_end == NULL)
16724 return NULL;
16725 *expr_start = NUL;
16726 *expr_end = NUL;
16727 c1 = *in_end;
16728 *in_end = NUL;
16729
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016730 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016731 if (temp_result != NULL && nextcmd == NULL)
16732 {
16733 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16734 + (in_end - expr_end) + 1));
16735 if (retval != NULL)
16736 {
16737 STRCPY(retval, in_start);
16738 STRCAT(retval, temp_result);
16739 STRCAT(retval, expr_end + 1);
16740 }
16741 }
16742 vim_free(temp_result);
16743
16744 *in_end = c1; /* put char back for error messages */
16745 *expr_start = '{';
16746 *expr_end = '}';
16747
16748 if (retval != NULL)
16749 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016750 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016751 if (expr_start != NULL)
16752 {
16753 /* Further expansion! */
16754 temp_result = make_expanded_name(retval, expr_start,
16755 expr_end, temp_result);
16756 vim_free(retval);
16757 retval = temp_result;
16758 }
16759 }
16760
16761 return retval;
16762}
16763
16764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016766 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 */
16768 static int
16769eval_isnamec(c)
16770 int c;
16771{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016772 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16773}
16774
16775/*
16776 * Return TRUE if character "c" can be used as the first character in a
16777 * variable or function name (excluding '{' and '}').
16778 */
16779 static int
16780eval_isnamec1(c)
16781 int c;
16782{
16783 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784}
16785
16786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 * Set number v: variable to "val".
16788 */
16789 void
16790set_vim_var_nr(idx, val)
16791 int idx;
16792 long val;
16793{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016794 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795}
16796
16797/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016798 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 */
16800 long
16801get_vim_var_nr(idx)
16802 int idx;
16803{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016804 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805}
16806
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016807#if defined(FEAT_AUTOCMD) || defined(PROTO)
16808/*
16809 * Get string v: variable value. Uses a static buffer, can only be used once.
16810 */
16811 char_u *
16812get_vim_var_str(idx)
16813 int idx;
16814{
16815 return get_tv_string(&vimvars[idx].vv_tv);
16816}
16817#endif
16818
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819/*
16820 * Set v:count, v:count1 and v:prevcount.
16821 */
16822 void
16823set_vcount(count, count1)
16824 long count;
16825 long count1;
16826{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016827 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16828 vimvars[VV_COUNT].vv_nr = count;
16829 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830}
16831
16832/*
16833 * Set string v: variable to a copy of "val".
16834 */
16835 void
16836set_vim_var_string(idx, val, len)
16837 int idx;
16838 char_u *val;
16839 int len; /* length of "val" to use or -1 (whole string) */
16840{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016841 /* Need to do this (at least) once, since we can't initialize a union.
16842 * Will always be invoked when "v:progname" is set. */
16843 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16844
Bram Moolenaare9a41262005-01-15 22:18:47 +000016845 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016847 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016849 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016851 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852}
16853
16854/*
16855 * Set v:register if needed.
16856 */
16857 void
16858set_reg_var(c)
16859 int c;
16860{
16861 char_u regname;
16862
16863 if (c == 0 || c == ' ')
16864 regname = '"';
16865 else
16866 regname = c;
16867 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016868 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 set_vim_var_string(VV_REG, &regname, 1);
16870}
16871
16872/*
16873 * Get or set v:exception. If "oldval" == NULL, return the current value.
16874 * Otherwise, restore the value to "oldval" and return NULL.
16875 * Must always be called in pairs to save and restore v:exception! Does not
16876 * take care of memory allocations.
16877 */
16878 char_u *
16879v_exception(oldval)
16880 char_u *oldval;
16881{
16882 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016883 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884
Bram Moolenaare9a41262005-01-15 22:18:47 +000016885 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 return NULL;
16887}
16888
16889/*
16890 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16891 * Otherwise, restore the value to "oldval" and return NULL.
16892 * Must always be called in pairs to save and restore v:throwpoint! Does not
16893 * take care of memory allocations.
16894 */
16895 char_u *
16896v_throwpoint(oldval)
16897 char_u *oldval;
16898{
16899 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016900 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901
Bram Moolenaare9a41262005-01-15 22:18:47 +000016902 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 return NULL;
16904}
16905
16906#if defined(FEAT_AUTOCMD) || defined(PROTO)
16907/*
16908 * Set v:cmdarg.
16909 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16910 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16911 * Must always be called in pairs!
16912 */
16913 char_u *
16914set_cmdarg(eap, oldarg)
16915 exarg_T *eap;
16916 char_u *oldarg;
16917{
16918 char_u *oldval;
16919 char_u *newval;
16920 unsigned len;
16921
Bram Moolenaare9a41262005-01-15 22:18:47 +000016922 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016923 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016925 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016926 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016927 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 }
16929
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016930 if (eap->force_bin == FORCE_BIN)
16931 len = 6;
16932 else if (eap->force_bin == FORCE_NOBIN)
16933 len = 8;
16934 else
16935 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016936
16937 if (eap->read_edit)
16938 len += 7;
16939
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016940 if (eap->force_ff != 0)
16941 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16942# ifdef FEAT_MBYTE
16943 if (eap->force_enc != 0)
16944 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016945 if (eap->bad_char != 0)
16946 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016947# endif
16948
16949 newval = alloc(len + 1);
16950 if (newval == NULL)
16951 return NULL;
16952
16953 if (eap->force_bin == FORCE_BIN)
16954 sprintf((char *)newval, " ++bin");
16955 else if (eap->force_bin == FORCE_NOBIN)
16956 sprintf((char *)newval, " ++nobin");
16957 else
16958 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016959
16960 if (eap->read_edit)
16961 STRCAT(newval, " ++edit");
16962
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016963 if (eap->force_ff != 0)
16964 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16965 eap->cmd + eap->force_ff);
16966# ifdef FEAT_MBYTE
16967 if (eap->force_enc != 0)
16968 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16969 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016970 if (eap->bad_char != 0)
16971 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16972 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016973# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016974 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016975 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976}
16977#endif
16978
16979/*
16980 * Get the value of internal variable "name".
16981 * Return OK or FAIL.
16982 */
16983 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016984get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 char_u *name;
16986 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016987 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016988 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989{
16990 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 typval_T *tv = NULL;
16992 typval_T atv;
16993 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995
16996 /* truncate the name, so that we can use strcmp() */
16997 cc = name[len];
16998 name[len] = NUL;
16999
17000 /*
17001 * Check for "b:changedtick".
17002 */
17003 if (STRCMP(name, "b:changedtick") == 0)
17004 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017005 atv.v_type = VAR_NUMBER;
17006 atv.vval.v_number = curbuf->b_changedtick;
17007 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 }
17009
17010 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 * Check for user-defined variables.
17012 */
17013 else
17014 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017015 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 }
17019
Bram Moolenaare9a41262005-01-15 22:18:47 +000017020 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017022 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017023 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 ret = FAIL;
17025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017027 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
17029 name[len] = cc;
17030
17031 return ret;
17032}
17033
17034/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017035 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17036 * Also handle function call with Funcref variable: func(expr)
17037 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17038 */
17039 static int
17040handle_subscript(arg, rettv, evaluate, verbose)
17041 char_u **arg;
17042 typval_T *rettv;
17043 int evaluate; /* do more than finding the end */
17044 int verbose; /* give error messages */
17045{
17046 int ret = OK;
17047 dict_T *selfdict = NULL;
17048 char_u *s;
17049 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017050 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017051
17052 while (ret == OK
17053 && (**arg == '['
17054 || (**arg == '.' && rettv->v_type == VAR_DICT)
17055 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17056 && !vim_iswhite(*(*arg - 1)))
17057 {
17058 if (**arg == '(')
17059 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017060 /* need to copy the funcref so that we can clear rettv */
17061 functv = *rettv;
17062 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017063
17064 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017065 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017066 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017067 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17068 &len, evaluate, selfdict);
17069
17070 /* Clear the funcref afterwards, so that deleting it while
17071 * evaluating the arguments is possible (see test55). */
17072 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017073
17074 /* Stop the expression evaluation when immediately aborting on
17075 * error, or when an interrupt occurred or an exception was thrown
17076 * but not caught. */
17077 if (aborting())
17078 {
17079 if (ret == OK)
17080 clear_tv(rettv);
17081 ret = FAIL;
17082 }
17083 dict_unref(selfdict);
17084 selfdict = NULL;
17085 }
17086 else /* **arg == '[' || **arg == '.' */
17087 {
17088 dict_unref(selfdict);
17089 if (rettv->v_type == VAR_DICT)
17090 {
17091 selfdict = rettv->vval.v_dict;
17092 if (selfdict != NULL)
17093 ++selfdict->dv_refcount;
17094 }
17095 else
17096 selfdict = NULL;
17097 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17098 {
17099 clear_tv(rettv);
17100 ret = FAIL;
17101 }
17102 }
17103 }
17104 dict_unref(selfdict);
17105 return ret;
17106}
17107
17108/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017109 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17110 * value).
17111 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017114{
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017116}
17117
17118/*
17119 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 * The string "s" must have been allocated, it is consumed.
17121 * Return NULL for out of memory, the variable otherwise.
17122 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 char_u *s;
17126{
Bram Moolenaar33570922005-01-25 22:26:29 +000017127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017129 rettv = alloc_tv();
17130 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 rettv->v_type = VAR_STRING;
17133 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 }
17135 else
17136 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138}
17139
17140/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017141 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017143 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017144free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017145 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146{
17147 if (varp != NULL)
17148 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017149 switch (varp->v_type)
17150 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017151 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017152 func_unref(varp->vval.v_string);
17153 /*FALLTHROUGH*/
17154 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017155 vim_free(varp->vval.v_string);
17156 break;
17157 case VAR_LIST:
17158 list_unref(varp->vval.v_list);
17159 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017160 case VAR_DICT:
17161 dict_unref(varp->vval.v_dict);
17162 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017163 case VAR_NUMBER:
17164 case VAR_UNKNOWN:
17165 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017166 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017167 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017168 break;
17169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 vim_free(varp);
17171 }
17172}
17173
17174/*
17175 * Free the memory for a variable value and set the value to NULL or 0.
17176 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017177 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017179 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180{
17181 if (varp != NULL)
17182 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017183 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017185 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017186 func_unref(varp->vval.v_string);
17187 /*FALLTHROUGH*/
17188 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017189 vim_free(varp->vval.v_string);
17190 varp->vval.v_string = NULL;
17191 break;
17192 case VAR_LIST:
17193 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017194 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017195 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017196 case VAR_DICT:
17197 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017198 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017199 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017200 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017201 varp->vval.v_number = 0;
17202 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017203 case VAR_UNKNOWN:
17204 break;
17205 default:
17206 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017208 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 }
17210}
17211
17212/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017213 * Set the value of a variable to NULL without freeing items.
17214 */
17215 static void
17216init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017217 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017218{
17219 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017220 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017221}
17222
17223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 * Get the number value of a variable.
17225 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017226 * For incompatible types, return 0.
17227 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17228 * caller of incompatible types: it sets *denote to TRUE if "denote"
17229 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 */
17231 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017232get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017233 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017235 int error = FALSE;
17236
17237 return get_tv_number_chk(varp, &error); /* return 0L on error */
17238}
17239
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017240 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241get_tv_number_chk(varp, denote)
17242 typval_T *varp;
17243 int *denote;
17244{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017247 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017249 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017250 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017252 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017253 break;
17254 case VAR_STRING:
17255 if (varp->vval.v_string != NULL)
17256 vim_str2nr(varp->vval.v_string, NULL, NULL,
17257 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017258 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017259 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017260 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017261 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017262 case VAR_DICT:
17263 EMSG(_("E728: Using a Dictionary as a number"));
17264 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017265 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017266 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017269 if (denote == NULL) /* useful for values that must be unsigned */
17270 n = -1;
17271 else
17272 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274}
17275
17276/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017277 * Get the lnum from the first argument.
17278 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 */
17281 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284{
Bram Moolenaar33570922005-01-25 22:26:29 +000017285 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 linenr_T lnum;
17287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017288 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 if (lnum == 0) /* no valid number, try using line() */
17290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291 rettv.v_type = VAR_NUMBER;
17292 f_line(argvars, &rettv);
17293 lnum = rettv.vval.v_number;
17294 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 }
17296 return lnum;
17297}
17298
17299/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017300 * Get the lnum from the first argument.
17301 * Also accepts "$", then "buf" is used.
17302 * Returns 0 on error.
17303 */
17304 static linenr_T
17305get_tv_lnum_buf(argvars, buf)
17306 typval_T *argvars;
17307 buf_T *buf;
17308{
17309 if (argvars[0].v_type == VAR_STRING
17310 && argvars[0].vval.v_string != NULL
17311 && argvars[0].vval.v_string[0] == '$'
17312 && buf != NULL)
17313 return buf->b_ml.ml_line_count;
17314 return get_tv_number_chk(&argvars[0], NULL);
17315}
17316
17317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 * Get the string value of a variable.
17319 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017320 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17321 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 * If the String variable has never been set, return an empty string.
17323 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017324 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17325 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 */
17327 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330{
17331 static char_u mybuf[NUMBUFLEN];
17332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
17336 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017337get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017338 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017339 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017341 char_u *res = get_tv_string_buf_chk(varp, buf);
17342
17343 return res != NULL ? res : (char_u *)"";
17344}
17345
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017346 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017347get_tv_string_chk(varp)
17348 typval_T *varp;
17349{
17350 static char_u mybuf[NUMBUFLEN];
17351
17352 return get_tv_string_buf_chk(varp, mybuf);
17353}
17354
17355 static char_u *
17356get_tv_string_buf_chk(varp, buf)
17357 typval_T *varp;
17358 char_u *buf;
17359{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017360 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017362 case VAR_NUMBER:
17363 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17364 return buf;
17365 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017366 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017367 break;
17368 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017369 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017370 break;
17371 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017372 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017373 break;
17374 case VAR_STRING:
17375 if (varp->vval.v_string != NULL)
17376 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017378 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017379 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017380 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017382 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383}
17384
17385/*
17386 * Find variable "name" in the list of variables.
17387 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017388 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017389 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017392 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017393find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017395 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
Bram Moolenaara7043832005-01-21 11:56:39 +000017400 ht = find_var_ht(name, &varname);
17401 if (htp != NULL)
17402 *htp = ht;
17403 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017405 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406}
17407
17408/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017410 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017412 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017413find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017415 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017416 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017417{
Bram Moolenaar33570922005-01-25 22:26:29 +000017418 hashitem_T *hi;
17419
17420 if (*varname == NUL)
17421 {
17422 /* Must be something like "s:", otherwise "ht" would be NULL. */
17423 switch (varname[-2])
17424 {
17425 case 's': return &SCRIPT_SV(current_SID).sv_var;
17426 case 'g': return &globvars_var;
17427 case 'v': return &vimvars_var;
17428 case 'b': return &curbuf->b_bufvar;
17429 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017430#ifdef FEAT_WINDOWS
17431 case 't': return &curtab->tp_winvar;
17432#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017433 case 'l': return current_funccal == NULL
17434 ? NULL : &current_funccal->l_vars_var;
17435 case 'a': return current_funccal == NULL
17436 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 }
17438 return NULL;
17439 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017440
17441 hi = hash_find(ht, varname);
17442 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017443 {
17444 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017445 * worked find the variable again. Don't auto-load a script if it was
17446 * loaded already, otherwise it would be loaded every time when
17447 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017448 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017449 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017450 hi = hash_find(ht, varname);
17451 if (HASHITEM_EMPTY(hi))
17452 return NULL;
17453 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017454 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017455}
17456
17457/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017459 * Set "varname" to the start of name without ':'.
17460 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017462find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 char_u *name;
17464 char_u **varname;
17465{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017466 hashitem_T *hi;
17467
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 if (name[1] != ':')
17469 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017470 /* The name must not start with a colon or #. */
17471 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 return NULL;
17473 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017474
17475 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017476 hi = hash_find(&compat_hashtab, name);
17477 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017478 return &compat_hashtab;
17479
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 return &globvarht; /* global variable */
17482 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 }
17484 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017485 if (*name == 'g') /* global variable */
17486 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017487 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17488 */
17489 if (vim_strchr(name + 2, ':') != NULL
17490 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017491 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017496#ifdef FEAT_WINDOWS
17497 if (*name == 't') /* tab page variable */
17498 return &curtab->tp_vars.dv_hashtab;
17499#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 if (*name == 'v') /* v: variable */
17501 return &vimvarht;
17502 if (*name == 'a' && current_funccal != NULL) /* function argument */
17503 return &current_funccal->l_avars.dv_hashtab;
17504 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17505 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 if (*name == 's' /* script variable */
17507 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17508 return &SCRIPT_VARS(current_SID);
17509 return NULL;
17510}
17511
17512/*
17513 * Get the string value of a (global/local) variable.
17514 * Returns NULL when it doesn't exist.
17515 */
17516 char_u *
17517get_var_value(name)
17518 char_u *name;
17519{
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521
Bram Moolenaara7043832005-01-21 11:56:39 +000017522 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 if (v == NULL)
17524 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526}
17527
17528/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017529 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 * sourcing this script and when executing functions defined in the script.
17531 */
17532 void
17533new_script_vars(id)
17534 scid_T id;
17535{
Bram Moolenaara7043832005-01-21 11:56:39 +000017536 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 hashtab_T *ht;
17538 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017539
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17541 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017542 /* Re-allocating ga_data means that an ht_array pointing to
17543 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017545 for (i = 1; i <= ga_scripts.ga_len; ++i)
17546 {
17547 ht = &SCRIPT_VARS(i);
17548 if (ht->ht_mask == HT_INIT_SIZE - 1)
17549 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017550 sv = &SCRIPT_SV(i);
17551 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017552 }
17553
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 while (ga_scripts.ga_len < id)
17555 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17557 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 }
17560 }
17561}
17562
17563/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17565 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 */
17567 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017568init_var_dict(dict, dict_var)
17569 dict_T *dict;
17570 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571{
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 hash_init(&dict->dv_hashtab);
17573 dict->dv_refcount = 99999;
17574 dict_var->di_tv.vval.v_dict = dict;
17575 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017576 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17578 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579}
17580
17581/*
17582 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 * Frees all allocated variables and the value they contain.
17584 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 */
17586 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017587vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 hashtab_T *ht;
17589{
17590 vars_clear_ext(ht, TRUE);
17591}
17592
17593/*
17594 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17595 */
17596 static void
17597vars_clear_ext(ht, free_val)
17598 hashtab_T *ht;
17599 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600{
Bram Moolenaara7043832005-01-21 11:56:39 +000017601 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017602 hashitem_T *hi;
17603 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604
Bram Moolenaar33570922005-01-25 22:26:29 +000017605 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017606 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017607 for (hi = ht->ht_array; todo > 0; ++hi)
17608 {
17609 if (!HASHITEM_EMPTY(hi))
17610 {
17611 --todo;
17612
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017614 * ht_array might change then. hash_clear() takes care of it
17615 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 v = HI2DI(hi);
17617 if (free_val)
17618 clear_tv(&v->di_tv);
17619 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17620 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017621 }
17622 }
17623 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017624 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625}
17626
Bram Moolenaara7043832005-01-21 11:56:39 +000017627/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 * Delete a variable from hashtab "ht" at item "hi".
17629 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017632delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017633 hashtab_T *ht;
17634 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635{
Bram Moolenaar33570922005-01-25 22:26:29 +000017636 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017637
17638 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 clear_tv(&di->di_tv);
17640 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641}
17642
17643/*
17644 * List the value of one internal variable.
17645 */
17646 static void
17647list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017648 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 char_u *prefix;
17650{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017651 char_u *tofree;
17652 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017653 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017654
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017655 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017657 s == NULL ? (char_u *)"" : s);
17658 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659}
17660
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 static void
17662list_one_var_a(prefix, name, type, string)
17663 char_u *prefix;
17664 char_u *name;
17665 int type;
17666 char_u *string;
17667{
17668 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17669 if (name != NULL) /* "a:" vars don't have a name stored */
17670 msg_puts(name);
17671 msg_putchar(' ');
17672 msg_advance(22);
17673 if (type == VAR_NUMBER)
17674 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017675 else if (type == VAR_FUNC)
17676 msg_putchar('*');
17677 else if (type == VAR_LIST)
17678 {
17679 msg_putchar('[');
17680 if (*string == '[')
17681 ++string;
17682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017683 else if (type == VAR_DICT)
17684 {
17685 msg_putchar('{');
17686 if (*string == '{')
17687 ++string;
17688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 else
17690 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017691
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017693
17694 if (type == VAR_FUNC)
17695 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696}
17697
17698/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017699 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 * If the variable already exists, the value is updated.
17701 * Otherwise the variable is created.
17702 */
17703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017706 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017707 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708{
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017712 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017715 {
17716 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17717 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17718 ? name[2] : name[0]))
17719 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017720 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017721 return;
17722 }
17723 if (function_exists(name))
17724 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017725 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017726 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017727 return;
17728 }
17729 }
17730
Bram Moolenaara7043832005-01-21 11:56:39 +000017731 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017732 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017733 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017734 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017735 return;
17736 }
17737
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017738 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017741 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017742 if (var_check_ro(v->di_flags, name)
17743 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 return;
17745 if (v->di_tv.v_type != tv->v_type
17746 && !((v->di_tv.v_type == VAR_STRING
17747 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748 && (tv->v_type == VAR_STRING
17749 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017750 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017751 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017752 return;
17753 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017754
17755 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017756 * Handle setting internal v: variables separately: we don't change
17757 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 */
17759 if (ht == &vimvarht)
17760 {
17761 if (v->di_tv.v_type == VAR_STRING)
17762 {
17763 vim_free(v->di_tv.vval.v_string);
17764 if (copy || tv->v_type != VAR_STRING)
17765 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17766 else
17767 {
17768 /* Take over the string to avoid an extra alloc/free. */
17769 v->di_tv.vval.v_string = tv->vval.v_string;
17770 tv->vval.v_string = NULL;
17771 }
17772 }
17773 else if (v->di_tv.v_type != VAR_NUMBER)
17774 EMSG2(_(e_intern2), "set_var()");
17775 else
17776 v->di_tv.vval.v_number = get_tv_number(tv);
17777 return;
17778 }
17779
17780 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 }
17782 else /* add a new variable */
17783 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017784 /* Can't add "v:" variable. */
17785 if (ht == &vimvarht)
17786 {
17787 EMSG2(_(e_illvar), name);
17788 return;
17789 }
17790
Bram Moolenaar92124a32005-06-17 22:03:40 +000017791 /* Make sure the variable name is valid. */
17792 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017793 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17794 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017795 {
17796 EMSG2(_(e_illvar), varname);
17797 return;
17798 }
17799
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017800 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17801 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017802 if (v == NULL)
17803 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017804 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017805 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017807 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 return;
17809 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017810 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017814 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017815 else
17816 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017817 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017818 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821}
17822
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017823/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017824 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017825 * Also give an error message.
17826 */
17827 static int
17828var_check_ro(flags, name)
17829 int flags;
17830 char_u *name;
17831{
17832 if (flags & DI_FLAGS_RO)
17833 {
17834 EMSG2(_(e_readonlyvar), name);
17835 return TRUE;
17836 }
17837 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17838 {
17839 EMSG2(_(e_readonlysbx), name);
17840 return TRUE;
17841 }
17842 return FALSE;
17843}
17844
17845/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017846 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17847 * Also give an error message.
17848 */
17849 static int
17850var_check_fixed(flags, name)
17851 int flags;
17852 char_u *name;
17853{
17854 if (flags & DI_FLAGS_FIX)
17855 {
17856 EMSG2(_("E795: Cannot delete variable %s"), name);
17857 return TRUE;
17858 }
17859 return FALSE;
17860}
17861
17862/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017863 * Return TRUE if typeval "tv" is set to be locked (immutable).
17864 * Also give an error message, using "name".
17865 */
17866 static int
17867tv_check_lock(lock, name)
17868 int lock;
17869 char_u *name;
17870{
17871 if (lock & VAR_LOCKED)
17872 {
17873 EMSG2(_("E741: Value is locked: %s"),
17874 name == NULL ? (char_u *)_("Unknown") : name);
17875 return TRUE;
17876 }
17877 if (lock & VAR_FIXED)
17878 {
17879 EMSG2(_("E742: Cannot change value of %s"),
17880 name == NULL ? (char_u *)_("Unknown") : name);
17881 return TRUE;
17882 }
17883 return FALSE;
17884}
17885
17886/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017887 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017888 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017889 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017892copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 typval_T *from;
17894 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017896 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017897 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017898 switch (from->v_type)
17899 {
17900 case VAR_NUMBER:
17901 to->vval.v_number = from->vval.v_number;
17902 break;
17903 case VAR_STRING:
17904 case VAR_FUNC:
17905 if (from->vval.v_string == NULL)
17906 to->vval.v_string = NULL;
17907 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017908 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017909 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017910 if (from->v_type == VAR_FUNC)
17911 func_ref(to->vval.v_string);
17912 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017913 break;
17914 case VAR_LIST:
17915 if (from->vval.v_list == NULL)
17916 to->vval.v_list = NULL;
17917 else
17918 {
17919 to->vval.v_list = from->vval.v_list;
17920 ++to->vval.v_list->lv_refcount;
17921 }
17922 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017923 case VAR_DICT:
17924 if (from->vval.v_dict == NULL)
17925 to->vval.v_dict = NULL;
17926 else
17927 {
17928 to->vval.v_dict = from->vval.v_dict;
17929 ++to->vval.v_dict->dv_refcount;
17930 }
17931 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017932 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017934 break;
17935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936}
17937
17938/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017939 * Make a copy of an item.
17940 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017941 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17942 * reference to an already copied list/dict can be used.
17943 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017944 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017945 static int
17946item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 typval_T *from;
17948 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017949 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017950 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017951{
17952 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017953 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017954
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017956 {
17957 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017958 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017959 }
17960 ++recurse;
17961
17962 switch (from->v_type)
17963 {
17964 case VAR_NUMBER:
17965 case VAR_STRING:
17966 case VAR_FUNC:
17967 copy_tv(from, to);
17968 break;
17969 case VAR_LIST:
17970 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017971 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017972 if (from->vval.v_list == NULL)
17973 to->vval.v_list = NULL;
17974 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17975 {
17976 /* use the copy made earlier */
17977 to->vval.v_list = from->vval.v_list->lv_copylist;
17978 ++to->vval.v_list->lv_refcount;
17979 }
17980 else
17981 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17982 if (to->vval.v_list == NULL)
17983 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017984 break;
17985 case VAR_DICT:
17986 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017987 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017988 if (from->vval.v_dict == NULL)
17989 to->vval.v_dict = NULL;
17990 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17991 {
17992 /* use the copy made earlier */
17993 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17994 ++to->vval.v_dict->dv_refcount;
17995 }
17996 else
17997 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17998 if (to->vval.v_dict == NULL)
17999 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018000 break;
18001 default:
18002 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018003 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018004 }
18005 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018006 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018007}
18008
18009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 * ":echo expr1 ..." print each argument separated with a space, add a
18011 * newline at the end.
18012 * ":echon expr1 ..." print each argument plain.
18013 */
18014 void
18015ex_echo(eap)
18016 exarg_T *eap;
18017{
18018 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018020 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 char_u *p;
18022 int needclr = TRUE;
18023 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018024 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025
18026 if (eap->skip)
18027 ++emsg_skip;
18028 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18029 {
18030 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018031 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 {
18033 /*
18034 * Report the invalid expression unless the expression evaluation
18035 * has been cancelled due to an aborting error, an interrupt, or an
18036 * exception.
18037 */
18038 if (!aborting())
18039 EMSG2(_(e_invexpr2), p);
18040 break;
18041 }
18042 if (!eap->skip)
18043 {
18044 if (atstart)
18045 {
18046 atstart = FALSE;
18047 /* Call msg_start() after eval1(), evaluating the expression
18048 * may cause a message to appear. */
18049 if (eap->cmdidx == CMD_echo)
18050 msg_start();
18051 }
18052 else if (eap->cmdidx == CMD_echo)
18053 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018054 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018055 if (p != NULL)
18056 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018058 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018060 if (*p != TAB && needclr)
18061 {
18062 /* remove any text still there from the command */
18063 msg_clr_eos();
18064 needclr = FALSE;
18065 }
18066 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 }
18068 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018069 {
18070#ifdef FEAT_MBYTE
18071 if (has_mbyte)
18072 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018073 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018074
18075 (void)msg_outtrans_len_attr(p, i, echo_attr);
18076 p += i - 1;
18077 }
18078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018080 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018083 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 arg = skipwhite(arg);
18087 }
18088 eap->nextcmd = check_nextcmd(arg);
18089
18090 if (eap->skip)
18091 --emsg_skip;
18092 else
18093 {
18094 /* remove text that may still be there from the command */
18095 if (needclr)
18096 msg_clr_eos();
18097 if (eap->cmdidx == CMD_echo)
18098 msg_end();
18099 }
18100}
18101
18102/*
18103 * ":echohl {name}".
18104 */
18105 void
18106ex_echohl(eap)
18107 exarg_T *eap;
18108{
18109 int id;
18110
18111 id = syn_name2id(eap->arg);
18112 if (id == 0)
18113 echo_attr = 0;
18114 else
18115 echo_attr = syn_id2attr(id);
18116}
18117
18118/*
18119 * ":execute expr1 ..." execute the result of an expression.
18120 * ":echomsg expr1 ..." Print a message
18121 * ":echoerr expr1 ..." Print an error
18122 * Each gets spaces around each argument and a newline at the end for
18123 * echo commands
18124 */
18125 void
18126ex_execute(eap)
18127 exarg_T *eap;
18128{
18129 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 int ret = OK;
18132 char_u *p;
18133 garray_T ga;
18134 int len;
18135 int save_did_emsg;
18136
18137 ga_init2(&ga, 1, 80);
18138
18139 if (eap->skip)
18140 ++emsg_skip;
18141 while (*arg != NUL && *arg != '|' && *arg != '\n')
18142 {
18143 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 {
18146 /*
18147 * Report the invalid expression unless the expression evaluation
18148 * has been cancelled due to an aborting error, an interrupt, or an
18149 * exception.
18150 */
18151 if (!aborting())
18152 EMSG2(_(e_invexpr2), p);
18153 ret = FAIL;
18154 break;
18155 }
18156
18157 if (!eap->skip)
18158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 len = (int)STRLEN(p);
18161 if (ga_grow(&ga, len + 2) == FAIL)
18162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018163 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 ret = FAIL;
18165 break;
18166 }
18167 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 ga.ga_len += len;
18171 }
18172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018173 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 arg = skipwhite(arg);
18175 }
18176
18177 if (ret != FAIL && ga.ga_data != NULL)
18178 {
18179 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018182 out_flush();
18183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 else if (eap->cmdidx == CMD_echoerr)
18185 {
18186 /* We don't want to abort following commands, restore did_emsg. */
18187 save_did_emsg = did_emsg;
18188 EMSG((char_u *)ga.ga_data);
18189 if (!force_abort)
18190 did_emsg = save_did_emsg;
18191 }
18192 else if (eap->cmdidx == CMD_execute)
18193 do_cmdline((char_u *)ga.ga_data,
18194 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18195 }
18196
18197 ga_clear(&ga);
18198
18199 if (eap->skip)
18200 --emsg_skip;
18201
18202 eap->nextcmd = check_nextcmd(arg);
18203}
18204
18205/*
18206 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18207 * "arg" points to the "&" or '+' when called, to "option" when returning.
18208 * Returns NULL when no option name found. Otherwise pointer to the char
18209 * after the option name.
18210 */
18211 static char_u *
18212find_option_end(arg, opt_flags)
18213 char_u **arg;
18214 int *opt_flags;
18215{
18216 char_u *p = *arg;
18217
18218 ++p;
18219 if (*p == 'g' && p[1] == ':')
18220 {
18221 *opt_flags = OPT_GLOBAL;
18222 p += 2;
18223 }
18224 else if (*p == 'l' && p[1] == ':')
18225 {
18226 *opt_flags = OPT_LOCAL;
18227 p += 2;
18228 }
18229 else
18230 *opt_flags = 0;
18231
18232 if (!ASCII_ISALPHA(*p))
18233 return NULL;
18234 *arg = p;
18235
18236 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18237 p += 4; /* termcap option */
18238 else
18239 while (ASCII_ISALPHA(*p))
18240 ++p;
18241 return p;
18242}
18243
18244/*
18245 * ":function"
18246 */
18247 void
18248ex_function(eap)
18249 exarg_T *eap;
18250{
18251 char_u *theline;
18252 int j;
18253 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 char_u *name = NULL;
18256 char_u *p;
18257 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018258 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 garray_T newargs;
18260 garray_T newlines;
18261 int varargs = FALSE;
18262 int mustend = FALSE;
18263 int flags = 0;
18264 ufunc_T *fp;
18265 int indent;
18266 int nesting;
18267 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 dictitem_T *v;
18269 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018270 static int func_nr = 0; /* number for nameless function */
18271 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018272 hashtab_T *ht;
18273 int todo;
18274 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018275 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276
18277 /*
18278 * ":function" without argument: list functions.
18279 */
18280 if (ends_excmd(*eap->arg))
18281 {
18282 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018284 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018285 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018286 {
18287 if (!HASHITEM_EMPTY(hi))
18288 {
18289 --todo;
18290 fp = HI2UF(hi);
18291 if (!isdigit(*fp->uf_name))
18292 list_func_head(fp, FALSE);
18293 }
18294 }
18295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 eap->nextcmd = check_nextcmd(eap->arg);
18297 return;
18298 }
18299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018300 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018301 * ":function /pat": list functions matching pattern.
18302 */
18303 if (*eap->arg == '/')
18304 {
18305 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18306 if (!eap->skip)
18307 {
18308 regmatch_T regmatch;
18309
18310 c = *p;
18311 *p = NUL;
18312 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18313 *p = c;
18314 if (regmatch.regprog != NULL)
18315 {
18316 regmatch.rm_ic = p_ic;
18317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018318 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018319 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18320 {
18321 if (!HASHITEM_EMPTY(hi))
18322 {
18323 --todo;
18324 fp = HI2UF(hi);
18325 if (!isdigit(*fp->uf_name)
18326 && vim_regexec(&regmatch, fp->uf_name, 0))
18327 list_func_head(fp, FALSE);
18328 }
18329 }
18330 }
18331 }
18332 if (*p == '/')
18333 ++p;
18334 eap->nextcmd = check_nextcmd(p);
18335 return;
18336 }
18337
18338 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018339 * Get the function name. There are these situations:
18340 * func normal function name
18341 * "name" == func, "fudi.fd_dict" == NULL
18342 * dict.func new dictionary entry
18343 * "name" == NULL, "fudi.fd_dict" set,
18344 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18345 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018346 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018347 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18348 * dict.func existing dict entry that's not a Funcref
18349 * "name" == NULL, "fudi.fd_dict" set,
18350 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018353 name = trans_function_name(&p, eap->skip, 0, &fudi);
18354 paren = (vim_strchr(p, '(') != NULL);
18355 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 {
18357 /*
18358 * Return on an invalid expression in braces, unless the expression
18359 * evaluation has been cancelled due to an aborting error, an
18360 * interrupt, or an exception.
18361 */
18362 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018364 if (!eap->skip && fudi.fd_newkey != NULL)
18365 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018366 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 else
18370 eap->skip = TRUE;
18371 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018372
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 /* An error in a function call during evaluation of an expression in magic
18374 * braces should not cause the function not to be defined. */
18375 saved_did_emsg = did_emsg;
18376 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377
18378 /*
18379 * ":function func" with only function name: list function.
18380 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018381 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 {
18383 if (!ends_excmd(*skipwhite(p)))
18384 {
18385 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018386 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 }
18388 eap->nextcmd = check_nextcmd(p);
18389 if (eap->nextcmd != NULL)
18390 *p = NUL;
18391 if (!eap->skip && !got_int)
18392 {
18393 fp = find_func(name);
18394 if (fp != NULL)
18395 {
18396 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018397 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018399 if (FUNCLINE(fp, j) == NULL)
18400 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 msg_putchar('\n');
18402 msg_outnum((long)(j + 1));
18403 if (j < 9)
18404 msg_putchar(' ');
18405 if (j < 99)
18406 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018407 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 out_flush(); /* show a line at a time */
18409 ui_breakcheck();
18410 }
18411 if (!got_int)
18412 {
18413 msg_putchar('\n');
18414 msg_puts((char_u *)" endfunction");
18415 }
18416 }
18417 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018418 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018420 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 }
18422
18423 /*
18424 * ":function name(arg1, arg2)" Define function.
18425 */
18426 p = skipwhite(p);
18427 if (*p != '(')
18428 {
18429 if (!eap->skip)
18430 {
18431 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018432 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 }
18434 /* attempt to continue by skipping some text */
18435 if (vim_strchr(p, '(') != NULL)
18436 p = vim_strchr(p, '(');
18437 }
18438 p = skipwhite(p + 1);
18439
18440 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18441 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18442
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018443 if (!eap->skip)
18444 {
18445 /* Check the name of the function. */
18446 if (name != NULL)
18447 arg = name;
18448 else
18449 arg = fudi.fd_newkey;
18450 if (arg != NULL)
18451 {
18452 if (*arg == K_SPECIAL)
18453 j = 3;
18454 else
18455 j = 0;
18456 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18457 : eval_isnamec(arg[j])))
18458 ++j;
18459 if (arg[j] != NUL)
18460 emsg_funcname(_(e_invarg2), arg);
18461 }
18462 }
18463
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 /*
18465 * Isolate the arguments: "arg1, arg2, ...)"
18466 */
18467 while (*p != ')')
18468 {
18469 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18470 {
18471 varargs = TRUE;
18472 p += 3;
18473 mustend = TRUE;
18474 }
18475 else
18476 {
18477 arg = p;
18478 while (ASCII_ISALNUM(*p) || *p == '_')
18479 ++p;
18480 if (arg == p || isdigit(*arg)
18481 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18482 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18483 {
18484 if (!eap->skip)
18485 EMSG2(_("E125: Illegal argument: %s"), arg);
18486 break;
18487 }
18488 if (ga_grow(&newargs, 1) == FAIL)
18489 goto erret;
18490 c = *p;
18491 *p = NUL;
18492 arg = vim_strsave(arg);
18493 if (arg == NULL)
18494 goto erret;
18495 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18496 *p = c;
18497 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 if (*p == ',')
18499 ++p;
18500 else
18501 mustend = TRUE;
18502 }
18503 p = skipwhite(p);
18504 if (mustend && *p != ')')
18505 {
18506 if (!eap->skip)
18507 EMSG2(_(e_invarg2), eap->arg);
18508 break;
18509 }
18510 }
18511 ++p; /* skip the ')' */
18512
Bram Moolenaare9a41262005-01-15 22:18:47 +000018513 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 for (;;)
18515 {
18516 p = skipwhite(p);
18517 if (STRNCMP(p, "range", 5) == 0)
18518 {
18519 flags |= FC_RANGE;
18520 p += 5;
18521 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018522 else if (STRNCMP(p, "dict", 4) == 0)
18523 {
18524 flags |= FC_DICT;
18525 p += 4;
18526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 else if (STRNCMP(p, "abort", 5) == 0)
18528 {
18529 flags |= FC_ABORT;
18530 p += 5;
18531 }
18532 else
18533 break;
18534 }
18535
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018536 /* When there is a line break use what follows for the function body.
18537 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18538 if (*p == '\n')
18539 line_arg = p + 1;
18540 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 EMSG(_(e_trailing));
18542
18543 /*
18544 * Read the body of the function, until ":endfunction" is found.
18545 */
18546 if (KeyTyped)
18547 {
18548 /* Check if the function already exists, don't let the user type the
18549 * whole function before telling him it doesn't work! For a script we
18550 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018551 if (!eap->skip && !eap->forceit)
18552 {
18553 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18554 EMSG(_(e_funcdict));
18555 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018556 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018559 if (!eap->skip && did_emsg)
18560 goto erret;
18561
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 msg_putchar('\n'); /* don't overwrite the function name */
18563 cmdline_row = msg_row;
18564 }
18565
18566 indent = 2;
18567 nesting = 0;
18568 for (;;)
18569 {
18570 msg_scroll = TRUE;
18571 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018572 sourcing_lnum_off = sourcing_lnum;
18573
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018574 if (line_arg != NULL)
18575 {
18576 /* Use eap->arg, split up in parts by line breaks. */
18577 theline = line_arg;
18578 p = vim_strchr(theline, '\n');
18579 if (p == NULL)
18580 line_arg += STRLEN(line_arg);
18581 else
18582 {
18583 *p = NUL;
18584 line_arg = p + 1;
18585 }
18586 }
18587 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 theline = getcmdline(':', 0L, indent);
18589 else
18590 theline = eap->getline(':', eap->cookie, indent);
18591 if (KeyTyped)
18592 lines_left = Rows - 1;
18593 if (theline == NULL)
18594 {
18595 EMSG(_("E126: Missing :endfunction"));
18596 goto erret;
18597 }
18598
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018599 /* Detect line continuation: sourcing_lnum increased more than one. */
18600 if (sourcing_lnum > sourcing_lnum_off + 1)
18601 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18602 else
18603 sourcing_lnum_off = 0;
18604
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 if (skip_until != NULL)
18606 {
18607 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18608 * don't check for ":endfunc". */
18609 if (STRCMP(theline, skip_until) == 0)
18610 {
18611 vim_free(skip_until);
18612 skip_until = NULL;
18613 }
18614 }
18615 else
18616 {
18617 /* skip ':' and blanks*/
18618 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18619 ;
18620
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018621 /* Check for "endfunction". */
18622 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018624 if (line_arg == NULL)
18625 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 break;
18627 }
18628
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018629 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 * at "end". */
18631 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18632 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018633 else if (STRNCMP(p, "if", 2) == 0
18634 || STRNCMP(p, "wh", 2) == 0
18635 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 || STRNCMP(p, "try", 3) == 0)
18637 indent += 2;
18638
18639 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018640 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018642 if (*p == '!')
18643 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 p += eval_fname_script(p);
18645 if (ASCII_ISALPHA(*p))
18646 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018647 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 if (*skipwhite(p) == '(')
18649 {
18650 ++nesting;
18651 indent += 2;
18652 }
18653 }
18654 }
18655
18656 /* Check for ":append" or ":insert". */
18657 p = skip_range(p, NULL);
18658 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18659 || (p[0] == 'i'
18660 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18661 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18662 skip_until = vim_strsave((char_u *)".");
18663
18664 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18665 arg = skipwhite(skiptowhite(p));
18666 if (arg[0] == '<' && arg[1] =='<'
18667 && ((p[0] == 'p' && p[1] == 'y'
18668 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18669 || (p[0] == 'p' && p[1] == 'e'
18670 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18671 || (p[0] == 't' && p[1] == 'c'
18672 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18673 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18674 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018675 || (p[0] == 'm' && p[1] == 'z'
18676 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 ))
18678 {
18679 /* ":python <<" continues until a dot, like ":append" */
18680 p = skipwhite(arg + 2);
18681 if (*p == NUL)
18682 skip_until = vim_strsave((char_u *)".");
18683 else
18684 skip_until = vim_strsave(p);
18685 }
18686 }
18687
18688 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018689 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018690 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018691 if (line_arg == NULL)
18692 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018694 }
18695
18696 /* Copy the line to newly allocated memory. get_one_sourceline()
18697 * allocates 250 bytes per line, this saves 80% on average. The cost
18698 * is an extra alloc/free. */
18699 p = vim_strsave(theline);
18700 if (p != NULL)
18701 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018702 if (line_arg == NULL)
18703 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018704 theline = p;
18705 }
18706
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018707 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18708
18709 /* Add NULL lines for continuation lines, so that the line count is
18710 * equal to the index in the growarray. */
18711 while (sourcing_lnum_off-- > 0)
18712 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018713
18714 /* Check for end of eap->arg. */
18715 if (line_arg != NULL && *line_arg == NUL)
18716 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 }
18718
18719 /* Don't define the function when skipping commands or when an error was
18720 * detected. */
18721 if (eap->skip || did_emsg)
18722 goto erret;
18723
18724 /*
18725 * If there are no errors, add the function
18726 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018727 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018728 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018729 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018730 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018731 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018732 emsg_funcname("E707: Function name conflicts with variable: %s",
18733 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018734 goto erret;
18735 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018736
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018737 fp = find_func(name);
18738 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018740 if (!eap->forceit)
18741 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018742 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018743 goto erret;
18744 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018745 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018746 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018747 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018748 name);
18749 goto erret;
18750 }
18751 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018752 ga_clear_strings(&(fp->uf_args));
18753 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018754 vim_free(name);
18755 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 }
18758 else
18759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 char numbuf[20];
18761
18762 fp = NULL;
18763 if (fudi.fd_newkey == NULL && !eap->forceit)
18764 {
18765 EMSG(_(e_funcdict));
18766 goto erret;
18767 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018768 if (fudi.fd_di == NULL)
18769 {
18770 /* Can't add a function to a locked dictionary */
18771 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18772 goto erret;
18773 }
18774 /* Can't change an existing function if it is locked */
18775 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18776 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018777
18778 /* Give the function a sequential number. Can only be used with a
18779 * Funcref! */
18780 vim_free(name);
18781 sprintf(numbuf, "%d", ++func_nr);
18782 name = vim_strsave((char_u *)numbuf);
18783 if (name == NULL)
18784 goto erret;
18785 }
18786
18787 if (fp == NULL)
18788 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018789 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018790 {
18791 int slen, plen;
18792 char_u *scriptname;
18793
18794 /* Check that the autoload name matches the script name. */
18795 j = FAIL;
18796 if (sourcing_name != NULL)
18797 {
18798 scriptname = autoload_name(name);
18799 if (scriptname != NULL)
18800 {
18801 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018802 plen = (int)STRLEN(p);
18803 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018804 if (slen > plen && fnamecmp(p,
18805 sourcing_name + slen - plen) == 0)
18806 j = OK;
18807 vim_free(scriptname);
18808 }
18809 }
18810 if (j == FAIL)
18811 {
18812 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18813 goto erret;
18814 }
18815 }
18816
18817 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 if (fp == NULL)
18819 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018820
18821 if (fudi.fd_dict != NULL)
18822 {
18823 if (fudi.fd_di == NULL)
18824 {
18825 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018826 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018827 if (fudi.fd_di == NULL)
18828 {
18829 vim_free(fp);
18830 goto erret;
18831 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018832 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18833 {
18834 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018835 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018836 goto erret;
18837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018838 }
18839 else
18840 /* overwrite existing dict entry */
18841 clear_tv(&fudi.fd_di->di_tv);
18842 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018843 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018844 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018845 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018846
18847 /* behave like "dict" was used */
18848 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018849 }
18850
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018852 STRCPY(fp->uf_name, name);
18853 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018855 fp->uf_args = newargs;
18856 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018857#ifdef FEAT_PROFILE
18858 fp->uf_tml_count = NULL;
18859 fp->uf_tml_total = NULL;
18860 fp->uf_tml_self = NULL;
18861 fp->uf_profiling = FALSE;
18862 if (prof_def_func())
18863 func_do_profile(fp);
18864#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018865 fp->uf_varargs = varargs;
18866 fp->uf_flags = flags;
18867 fp->uf_calls = 0;
18868 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018869 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
18871erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 ga_clear_strings(&newargs);
18873 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018874ret_free:
18875 vim_free(skip_until);
18876 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879}
18880
18881/*
18882 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018883 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018885 * flags:
18886 * TFN_INT: internal function name OK
18887 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 * Advances "pp" to just after the function name (if no error).
18889 */
18890 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018891trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 char_u **pp;
18893 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018894 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018895 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018897 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 char_u *start;
18899 char_u *end;
18900 int lead;
18901 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018904
18905 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018908
18909 /* Check for hard coded <SNR>: already translated function ID (from a user
18910 * command). */
18911 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18912 && (*pp)[2] == (int)KE_SNR)
18913 {
18914 *pp += 3;
18915 len = get_id_len(pp) + 3;
18916 return vim_strnsave(start, len);
18917 }
18918
18919 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18920 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018922 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018924
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018925 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18926 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018927 if (end == start)
18928 {
18929 if (!skip)
18930 EMSG(_("E129: Function name required"));
18931 goto theend;
18932 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018933 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018934 {
18935 /*
18936 * Report an invalid expression in braces, unless the expression
18937 * evaluation has been cancelled due to an aborting error, an
18938 * interrupt, or an exception.
18939 */
18940 if (!aborting())
18941 {
18942 if (end != NULL)
18943 EMSG2(_(e_invarg2), start);
18944 }
18945 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018946 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018947 goto theend;
18948 }
18949
18950 if (lv.ll_tv != NULL)
18951 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018952 if (fdp != NULL)
18953 {
18954 fdp->fd_dict = lv.ll_dict;
18955 fdp->fd_newkey = lv.ll_newkey;
18956 lv.ll_newkey = NULL;
18957 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018959 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18960 {
18961 name = vim_strsave(lv.ll_tv->vval.v_string);
18962 *pp = end;
18963 }
18964 else
18965 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018966 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18967 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018968 EMSG(_(e_funcref));
18969 else
18970 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018971 name = NULL;
18972 }
18973 goto theend;
18974 }
18975
18976 if (lv.ll_name == NULL)
18977 {
18978 /* Error found, but continue after the function name. */
18979 *pp = end;
18980 goto theend;
18981 }
18982
18983 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018984 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018985 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018986 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18987 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18988 {
18989 /* When there was "s:" already or the name expanded to get a
18990 * leading "s:" then remove it. */
18991 lv.ll_name += 2;
18992 len -= 2;
18993 lead = 2;
18994 }
18995 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018996 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018997 {
18998 if (lead == 2) /* skip over "s:" */
18999 lv.ll_name += 2;
19000 len = (int)(end - lv.ll_name);
19001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019002
19003 /*
19004 * Copy the function name to allocated memory.
19005 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19006 * Accept <SNR>123_name() outside a script.
19007 */
19008 if (skip)
19009 lead = 0; /* do nothing */
19010 else if (lead > 0)
19011 {
19012 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019013 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19014 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019015 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019016 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019017 if (current_SID <= 0)
19018 {
19019 EMSG(_(e_usingsid));
19020 goto theend;
19021 }
19022 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19023 lead += (int)STRLEN(sid_buf);
19024 }
19025 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019026 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019027 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019028 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019029 goto theend;
19030 }
19031 name = alloc((unsigned)(len + lead + 1));
19032 if (name != NULL)
19033 {
19034 if (lead > 0)
19035 {
19036 name[0] = K_SPECIAL;
19037 name[1] = KS_EXTRA;
19038 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019039 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019040 STRCPY(name + 3, sid_buf);
19041 }
19042 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19043 name[len + lead] = NUL;
19044 }
19045 *pp = end;
19046
19047theend:
19048 clear_lval(&lv);
19049 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050}
19051
19052/*
19053 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19054 * Return 2 if "p" starts with "s:".
19055 * Return 0 otherwise.
19056 */
19057 static int
19058eval_fname_script(p)
19059 char_u *p;
19060{
19061 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19062 || STRNICMP(p + 1, "SNR>", 4) == 0))
19063 return 5;
19064 if (p[0] == 's' && p[1] == ':')
19065 return 2;
19066 return 0;
19067}
19068
19069/*
19070 * Return TRUE if "p" starts with "<SID>" or "s:".
19071 * Only works if eval_fname_script() returned non-zero for "p"!
19072 */
19073 static int
19074eval_fname_sid(p)
19075 char_u *p;
19076{
19077 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19078}
19079
19080/*
19081 * List the head of the function: "name(arg1, arg2)".
19082 */
19083 static void
19084list_func_head(fp, indent)
19085 ufunc_T *fp;
19086 int indent;
19087{
19088 int j;
19089
19090 msg_start();
19091 if (indent)
19092 MSG_PUTS(" ");
19093 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019094 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
19096 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019097 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 }
19099 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019100 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019102 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 {
19104 if (j)
19105 MSG_PUTS(", ");
19106 msg_puts(FUNCARG(fp, j));
19107 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019108 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 {
19110 if (j)
19111 MSG_PUTS(", ");
19112 MSG_PUTS("...");
19113 }
19114 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019115 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019116 if (p_verbose > 0)
19117 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118}
19119
19120/*
19121 * Find a function by name, return pointer to it in ufuncs.
19122 * Return NULL for unknown function.
19123 */
19124 static ufunc_T *
19125find_func(name)
19126 char_u *name;
19127{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019128 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019130 hi = hash_find(&func_hashtab, name);
19131 if (!HASHITEM_EMPTY(hi))
19132 return HI2UF(hi);
19133 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134}
19135
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019136#if defined(EXITFREE) || defined(PROTO)
19137 void
19138free_all_functions()
19139{
19140 hashitem_T *hi;
19141
19142 /* Need to start all over every time, because func_free() may change the
19143 * hash table. */
19144 while (func_hashtab.ht_used > 0)
19145 for (hi = func_hashtab.ht_array; ; ++hi)
19146 if (!HASHITEM_EMPTY(hi))
19147 {
19148 func_free(HI2UF(hi));
19149 break;
19150 }
19151}
19152#endif
19153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019154/*
19155 * Return TRUE if a function "name" exists.
19156 */
19157 static int
19158function_exists(name)
19159 char_u *name;
19160{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019161 char_u *nm = name;
19162 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019163 int n = FALSE;
19164
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019165 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019166 nm = skipwhite(nm);
19167
19168 /* Only accept "funcname", "funcname ", "funcname (..." and
19169 * "funcname(...", not "funcname!...". */
19170 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019171 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019172 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019173 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019174 else
19175 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019177 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019178 return n;
19179}
19180
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019181/*
19182 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019183 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019184 */
19185 static int
19186builtin_function(name)
19187 char_u *name;
19188{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019189 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19190 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019191}
19192
Bram Moolenaar05159a02005-02-26 23:04:13 +000019193#if defined(FEAT_PROFILE) || defined(PROTO)
19194/*
19195 * Start profiling function "fp".
19196 */
19197 static void
19198func_do_profile(fp)
19199 ufunc_T *fp;
19200{
19201 fp->uf_tm_count = 0;
19202 profile_zero(&fp->uf_tm_self);
19203 profile_zero(&fp->uf_tm_total);
19204 if (fp->uf_tml_count == NULL)
19205 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19206 (sizeof(int) * fp->uf_lines.ga_len));
19207 if (fp->uf_tml_total == NULL)
19208 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19209 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19210 if (fp->uf_tml_self == NULL)
19211 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19212 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19213 fp->uf_tml_idx = -1;
19214 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19215 || fp->uf_tml_self == NULL)
19216 return; /* out of memory */
19217
19218 fp->uf_profiling = TRUE;
19219}
19220
19221/*
19222 * Dump the profiling results for all functions in file "fd".
19223 */
19224 void
19225func_dump_profile(fd)
19226 FILE *fd;
19227{
19228 hashitem_T *hi;
19229 int todo;
19230 ufunc_T *fp;
19231 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019232 ufunc_T **sorttab;
19233 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019235 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019236 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19237
Bram Moolenaar05159a02005-02-26 23:04:13 +000019238 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19239 {
19240 if (!HASHITEM_EMPTY(hi))
19241 {
19242 --todo;
19243 fp = HI2UF(hi);
19244 if (fp->uf_profiling)
19245 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019246 if (sorttab != NULL)
19247 sorttab[st_len++] = fp;
19248
Bram Moolenaar05159a02005-02-26 23:04:13 +000019249 if (fp->uf_name[0] == K_SPECIAL)
19250 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19251 else
19252 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19253 if (fp->uf_tm_count == 1)
19254 fprintf(fd, "Called 1 time\n");
19255 else
19256 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19257 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19258 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19259 fprintf(fd, "\n");
19260 fprintf(fd, "count total (s) self (s)\n");
19261
19262 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19263 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019264 if (FUNCLINE(fp, i) == NULL)
19265 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019266 prof_func_line(fd, fp->uf_tml_count[i],
19267 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019268 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19269 }
19270 fprintf(fd, "\n");
19271 }
19272 }
19273 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019274
19275 if (sorttab != NULL && st_len > 0)
19276 {
19277 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19278 prof_total_cmp);
19279 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19280 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19281 prof_self_cmp);
19282 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19283 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019284}
Bram Moolenaar73830342005-02-28 22:48:19 +000019285
19286 static void
19287prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19288 FILE *fd;
19289 ufunc_T **sorttab;
19290 int st_len;
19291 char *title;
19292 int prefer_self; /* when equal print only self time */
19293{
19294 int i;
19295 ufunc_T *fp;
19296
19297 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19298 fprintf(fd, "count total (s) self (s) function\n");
19299 for (i = 0; i < 20 && i < st_len; ++i)
19300 {
19301 fp = sorttab[i];
19302 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19303 prefer_self);
19304 if (fp->uf_name[0] == K_SPECIAL)
19305 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19306 else
19307 fprintf(fd, " %s()\n", fp->uf_name);
19308 }
19309 fprintf(fd, "\n");
19310}
19311
19312/*
19313 * Print the count and times for one function or function line.
19314 */
19315 static void
19316prof_func_line(fd, count, total, self, prefer_self)
19317 FILE *fd;
19318 int count;
19319 proftime_T *total;
19320 proftime_T *self;
19321 int prefer_self; /* when equal print only self time */
19322{
19323 if (count > 0)
19324 {
19325 fprintf(fd, "%5d ", count);
19326 if (prefer_self && profile_equal(total, self))
19327 fprintf(fd, " ");
19328 else
19329 fprintf(fd, "%s ", profile_msg(total));
19330 if (!prefer_self && profile_equal(total, self))
19331 fprintf(fd, " ");
19332 else
19333 fprintf(fd, "%s ", profile_msg(self));
19334 }
19335 else
19336 fprintf(fd, " ");
19337}
19338
19339/*
19340 * Compare function for total time sorting.
19341 */
19342 static int
19343#ifdef __BORLANDC__
19344_RTLENTRYF
19345#endif
19346prof_total_cmp(s1, s2)
19347 const void *s1;
19348 const void *s2;
19349{
19350 ufunc_T *p1, *p2;
19351
19352 p1 = *(ufunc_T **)s1;
19353 p2 = *(ufunc_T **)s2;
19354 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19355}
19356
19357/*
19358 * Compare function for self time sorting.
19359 */
19360 static int
19361#ifdef __BORLANDC__
19362_RTLENTRYF
19363#endif
19364prof_self_cmp(s1, s2)
19365 const void *s1;
19366 const void *s2;
19367{
19368 ufunc_T *p1, *p2;
19369
19370 p1 = *(ufunc_T **)s1;
19371 p2 = *(ufunc_T **)s2;
19372 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19373}
19374
Bram Moolenaar05159a02005-02-26 23:04:13 +000019375#endif
19376
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019377/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019378 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019379 * Return TRUE if a package was loaded.
19380 */
19381 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019382script_autoload(name, reload)
19383 char_u *name;
19384 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019385{
19386 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019387 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019388 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019389 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019390
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019391 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019392 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019393 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019394 return FALSE;
19395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019396 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019397
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019398 /* Find the name in the list of previously loaded package names. Skip
19399 * "autoload/", it's always the same. */
19400 for (i = 0; i < ga_loaded.ga_len; ++i)
19401 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19402 break;
19403 if (!reload && i < ga_loaded.ga_len)
19404 ret = FALSE; /* was loaded already */
19405 else
19406 {
19407 /* Remember the name if it wasn't loaded already. */
19408 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19409 {
19410 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19411 tofree = NULL;
19412 }
19413
19414 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019415 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019416 ret = TRUE;
19417 }
19418
19419 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019420 return ret;
19421}
19422
19423/*
19424 * Return the autoload script name for a function or variable name.
19425 * Returns NULL when out of memory.
19426 */
19427 static char_u *
19428autoload_name(name)
19429 char_u *name;
19430{
19431 char_u *p;
19432 char_u *scriptname;
19433
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019434 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019435 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19436 if (scriptname == NULL)
19437 return FALSE;
19438 STRCPY(scriptname, "autoload/");
19439 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019440 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019441 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019442 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019443 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019444 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019445}
19446
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19448
19449/*
19450 * Function given to ExpandGeneric() to obtain the list of user defined
19451 * function names.
19452 */
19453 char_u *
19454get_user_func_name(xp, idx)
19455 expand_T *xp;
19456 int idx;
19457{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019458 static long_u done;
19459 static hashitem_T *hi;
19460 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461
19462 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019464 done = 0;
19465 hi = func_hashtab.ht_array;
19466 }
19467 if (done < func_hashtab.ht_used)
19468 {
19469 if (done++ > 0)
19470 ++hi;
19471 while (HASHITEM_EMPTY(hi))
19472 ++hi;
19473 fp = HI2UF(hi);
19474
19475 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19476 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477
19478 cat_func_name(IObuff, fp);
19479 if (xp->xp_context != EXPAND_USER_FUNC)
19480 {
19481 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019482 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 STRCAT(IObuff, ")");
19484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 return IObuff;
19486 }
19487 return NULL;
19488}
19489
19490#endif /* FEAT_CMDL_COMPL */
19491
19492/*
19493 * Copy the function name of "fp" to buffer "buf".
19494 * "buf" must be able to hold the function name plus three bytes.
19495 * Takes care of script-local function names.
19496 */
19497 static void
19498cat_func_name(buf, fp)
19499 char_u *buf;
19500 ufunc_T *fp;
19501{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019502 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 {
19504 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019505 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 }
19507 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509}
19510
19511/*
19512 * ":delfunction {name}"
19513 */
19514 void
19515ex_delfunction(eap)
19516 exarg_T *eap;
19517{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019518 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 char_u *p;
19520 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522
19523 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019524 name = trans_function_name(&p, eap->skip, 0, &fudi);
19525 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019527 {
19528 if (fudi.fd_dict != NULL && !eap->skip)
19529 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 if (!ends_excmd(*skipwhite(p)))
19533 {
19534 vim_free(name);
19535 EMSG(_(e_trailing));
19536 return;
19537 }
19538 eap->nextcmd = check_nextcmd(p);
19539 if (eap->nextcmd != NULL)
19540 *p = NUL;
19541
19542 if (!eap->skip)
19543 fp = find_func(name);
19544 vim_free(name);
19545
19546 if (!eap->skip)
19547 {
19548 if (fp == NULL)
19549 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019550 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 return;
19552 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019553 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 {
19555 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19556 return;
19557 }
19558
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019559 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019561 /* Delete the dict item that refers to the function, it will
19562 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019563 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019565 else
19566 func_free(fp);
19567 }
19568}
19569
19570/*
19571 * Free a function and remove it from the list of functions.
19572 */
19573 static void
19574func_free(fp)
19575 ufunc_T *fp;
19576{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019577 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019578
19579 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019580 ga_clear_strings(&(fp->uf_args));
19581 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019582#ifdef FEAT_PROFILE
19583 vim_free(fp->uf_tml_count);
19584 vim_free(fp->uf_tml_total);
19585 vim_free(fp->uf_tml_self);
19586#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019587
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019588 /* remove the function from the function hashtable */
19589 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19590 if (HASHITEM_EMPTY(hi))
19591 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019592 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019593 hash_remove(&func_hashtab, hi);
19594
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019595 vim_free(fp);
19596}
19597
19598/*
19599 * Unreference a Function: decrement the reference count and free it when it
19600 * becomes zero. Only for numbered functions.
19601 */
19602 static void
19603func_unref(name)
19604 char_u *name;
19605{
19606 ufunc_T *fp;
19607
19608 if (name != NULL && isdigit(*name))
19609 {
19610 fp = find_func(name);
19611 if (fp == NULL)
19612 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019613 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019614 {
19615 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 * when "uf_calls" becomes zero. */
19617 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019618 func_free(fp);
19619 }
19620 }
19621}
19622
19623/*
19624 * Count a reference to a Function.
19625 */
19626 static void
19627func_ref(name)
19628 char_u *name;
19629{
19630 ufunc_T *fp;
19631
19632 if (name != NULL && isdigit(*name))
19633 {
19634 fp = find_func(name);
19635 if (fp == NULL)
19636 EMSG2(_(e_intern2), "func_ref()");
19637 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019638 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 }
19640}
19641
19642/*
19643 * Call a user function.
19644 */
19645 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019646call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 ufunc_T *fp; /* pointer to function */
19648 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 typval_T *argvars; /* arguments */
19650 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 linenr_T firstline; /* first line of range */
19652 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654{
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 char_u *save_sourcing_name;
19656 linenr_T save_sourcing_lnum;
19657 scid_T save_current_SID;
19658 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 int save_did_emsg;
19660 static int depth = 0;
19661 dictitem_T *v;
19662 int fixvar_idx = 0; /* index in fixvar[] */
19663 int i;
19664 int ai;
19665 char_u numbuf[NUMBUFLEN];
19666 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019667#ifdef FEAT_PROFILE
19668 proftime_T wait_start;
19669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670
19671 /* If depth of calling is getting too high, don't execute the function */
19672 if (depth >= p_mfd)
19673 {
19674 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019675 rettv->v_type = VAR_NUMBER;
19676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 return;
19678 }
19679 ++depth;
19680
19681 line_breakcheck(); /* check for CTRL-C hit */
19682
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019683 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686 fc.rettv = rettv;
19687 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 fc.linenr = 0;
19689 fc.returned = FALSE;
19690 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019692 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 fc.dbg_tick = debug_tick;
19694
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 /*
19696 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19697 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19698 * each argument variable and saves a lot of time.
19699 */
19700 /*
19701 * Init l: variables.
19702 */
19703 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019704 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019705 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019706 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19707 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019709 name = v->di_key;
19710 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19712 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19713 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019714 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 v->di_tv.vval.v_dict = selfdict;
19716 ++selfdict->dv_refcount;
19717 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019718
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 /*
19720 * Init a: variables.
19721 * Set a:0 to "argcount".
19722 * Set a:000 to a list with room for the "..." arguments.
19723 */
19724 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19725 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019726 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 v = &fc.fixvar[fixvar_idx++].var;
19728 STRCPY(v->di_key, "000");
19729 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19730 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19731 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019732 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019733 v->di_tv.vval.v_list = &fc.l_varlist;
19734 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19735 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019736 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019737
19738 /*
19739 * Set a:firstline to "firstline" and a:lastline to "lastline".
19740 * Set a:name to named arguments.
19741 * Set a:N to the "..." arguments.
19742 */
19743 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19744 (varnumber_T)firstline);
19745 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19746 (varnumber_T)lastline);
19747 for (i = 0; i < argcount; ++i)
19748 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019749 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 if (ai < 0)
19751 /* named argument a:name */
19752 name = FUNCARG(fp, i);
19753 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019754 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 /* "..." argument a:1, a:2, etc. */
19756 sprintf((char *)numbuf, "%d", ai + 1);
19757 name = numbuf;
19758 }
19759 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19760 {
19761 v = &fc.fixvar[fixvar_idx++].var;
19762 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19763 }
19764 else
19765 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019766 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19767 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 if (v == NULL)
19769 break;
19770 v->di_flags = DI_FLAGS_RO;
19771 }
19772 STRCPY(v->di_key, name);
19773 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019775 /* Note: the values are copied directly to avoid alloc/free.
19776 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019778 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019779
19780 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19781 {
19782 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19783 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019784 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019785 }
19786 }
19787
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 /* Don't redraw while executing the function. */
19789 ++RedrawingDisabled;
19790 save_sourcing_name = sourcing_name;
19791 save_sourcing_lnum = sourcing_lnum;
19792 sourcing_lnum = 1;
19793 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019794 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 if (sourcing_name != NULL)
19796 {
19797 if (save_sourcing_name != NULL
19798 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19799 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19800 else
19801 STRCPY(sourcing_name, "function ");
19802 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19803
19804 if (p_verbose >= 12)
19805 {
19806 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019807 verbose_enter_scroll();
19808
Bram Moolenaar555b2802005-05-19 21:08:39 +000019809 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 if (p_verbose >= 14)
19811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019813 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019814 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815
19816 msg_puts((char_u *)"(");
19817 for (i = 0; i < argcount; ++i)
19818 {
19819 if (i > 0)
19820 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 if (argvars[i].v_type == VAR_NUMBER)
19822 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 else
19824 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019825 trunc_string(tv2string(&argvars[i], &tofree,
19826 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019828 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 }
19830 }
19831 msg_puts((char_u *)")");
19832 }
19833 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019834
19835 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 --no_wait_return;
19837 }
19838 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019839#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019840 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019841 {
19842 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19843 func_do_profile(fp);
19844 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019845 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019846 {
19847 ++fp->uf_tm_count;
19848 profile_start(&fp->uf_tm_start);
19849 profile_zero(&fp->uf_tm_children);
19850 }
19851 script_prof_save(&wait_start);
19852 }
19853#endif
19854
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019856 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 save_did_emsg = did_emsg;
19858 did_emsg = FALSE;
19859
19860 /* call do_cmdline() to execute the lines */
19861 do_cmdline(NULL, get_func_line, (void *)&fc,
19862 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19863
19864 --RedrawingDisabled;
19865
19866 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019867 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869 clear_tv(rettv);
19870 rettv->v_type = VAR_NUMBER;
19871 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 }
19873
Bram Moolenaar05159a02005-02-26 23:04:13 +000019874#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019875 if (do_profiling == PROF_YES && (fp->uf_profiling
19876 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019877 {
19878 profile_end(&fp->uf_tm_start);
19879 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19880 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019881 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019882 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019883 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019884 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19885 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019886 }
19887 }
19888#endif
19889
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 /* when being verbose, mention the return value */
19891 if (p_verbose >= 12)
19892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019894 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019897 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019898 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019899 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19900 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019903 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019904 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019905 char_u *tofree;
19906
Bram Moolenaar555b2802005-05-19 21:08:39 +000019907 /* The value may be very long. Skip the middle part, so that we
19908 * have some idea how it starts and ends. smsg() would always
19909 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000019910 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019911 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019912 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019913 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 }
19915 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019916
19917 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 --no_wait_return;
19919 }
19920
19921 vim_free(sourcing_name);
19922 sourcing_name = save_sourcing_name;
19923 sourcing_lnum = save_sourcing_lnum;
19924 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019925#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019927 script_prof_restore(&wait_start);
19928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929
19930 if (p_verbose >= 12 && sourcing_name != NULL)
19931 {
19932 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019933 verbose_enter_scroll();
19934
Bram Moolenaar555b2802005-05-19 21:08:39 +000019935 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019937
19938 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 --no_wait_return;
19940 }
19941
19942 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019943 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 /* The a: variables typevals were not alloced, only free the allocated
19946 * variables. */
19947 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19948
19949 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 --depth;
19951}
19952
19953/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 * Add a number variable "name" to dict "dp" with value "nr".
19955 */
19956 static void
19957add_nr_var(dp, v, name, nr)
19958 dict_T *dp;
19959 dictitem_T *v;
19960 char *name;
19961 varnumber_T nr;
19962{
19963 STRCPY(v->di_key, name);
19964 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19965 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19966 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019967 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019968 v->di_tv.vval.v_number = nr;
19969}
19970
19971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 * ":return [expr]"
19973 */
19974 void
19975ex_return(eap)
19976 exarg_T *eap;
19977{
19978 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019979 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 int returning = FALSE;
19981
19982 if (current_funccal == NULL)
19983 {
19984 EMSG(_("E133: :return not inside a function"));
19985 return;
19986 }
19987
19988 if (eap->skip)
19989 ++emsg_skip;
19990
19991 eap->nextcmd = NULL;
19992 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 {
19995 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019998 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 }
20000 /* It's safer to return also on error. */
20001 else if (!eap->skip)
20002 {
20003 /*
20004 * Return unless the expression evaluation has been cancelled due to an
20005 * aborting error, an interrupt, or an exception.
20006 */
20007 if (!aborting())
20008 returning = do_return(eap, FALSE, TRUE, NULL);
20009 }
20010
20011 /* When skipping or the return gets pending, advance to the next command
20012 * in this line (!returning). Otherwise, ignore the rest of the line.
20013 * Following lines will be ignored by get_func_line(). */
20014 if (returning)
20015 eap->nextcmd = NULL;
20016 else if (eap->nextcmd == NULL) /* no argument */
20017 eap->nextcmd = check_nextcmd(arg);
20018
20019 if (eap->skip)
20020 --emsg_skip;
20021}
20022
20023/*
20024 * Return from a function. Possibly makes the return pending. Also called
20025 * for a pending return at the ":endtry" or after returning from an extra
20026 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020027 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020028 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 * FALSE when the return gets pending.
20030 */
20031 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 exarg_T *eap;
20034 int reanimate;
20035 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020036 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037{
20038 int idx;
20039 struct condstack *cstack = eap->cstack;
20040
20041 if (reanimate)
20042 /* Undo the return. */
20043 current_funccal->returned = FALSE;
20044
20045 /*
20046 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20047 * not in its finally clause (which then is to be executed next) is found.
20048 * In this case, make the ":return" pending for execution at the ":endtry".
20049 * Otherwise, return normally.
20050 */
20051 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20052 if (idx >= 0)
20053 {
20054 cstack->cs_pending[idx] = CSTP_RETURN;
20055
20056 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020057 /* A pending return again gets pending. "rettv" points to an
20058 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 else
20062 {
20063 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 {
20070 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 else
20074 EMSG(_(e_outofmem));
20075 }
20076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078
20079 if (reanimate)
20080 {
20081 /* The pending return value could be overwritten by a ":return"
20082 * without argument in a finally clause; reset the default
20083 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084 current_funccal->rettv->v_type = VAR_NUMBER;
20085 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 }
20087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 }
20090 else
20091 {
20092 current_funccal->returned = TRUE;
20093
20094 /* If the return is carried out now, store the return value. For
20095 * a return immediately after reanimation, the value is already
20096 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020099 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 }
20104 }
20105
20106 return idx < 0;
20107}
20108
20109/*
20110 * Free the variable with a pending return value.
20111 */
20112 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020113discard_pending_return(rettv)
20114 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115{
Bram Moolenaar33570922005-01-25 22:26:29 +000020116 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117}
20118
20119/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 * is an allocated string. Used by report_pending() for verbose messages.
20122 */
20123 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020124get_return_cmd(rettv)
20125 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020127 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020128 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020129 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020131 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020132 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020133 if (s == NULL)
20134 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020135
20136 STRCPY(IObuff, ":return ");
20137 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20138 if (STRLEN(s) + 8 >= IOSIZE)
20139 STRCPY(IObuff + IOSIZE - 4, "...");
20140 vim_free(tofree);
20141 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142}
20143
20144/*
20145 * Get next function line.
20146 * Called by do_cmdline() to get the next line.
20147 * Returns allocated string, or NULL for end of function.
20148 */
20149/* ARGSUSED */
20150 char_u *
20151get_func_line(c, cookie, indent)
20152 int c; /* not used */
20153 void *cookie;
20154 int indent; /* not used */
20155{
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020157 ufunc_T *fp = fcp->func;
20158 char_u *retval;
20159 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160
20161 /* If breakpoints have been added/deleted need to check for it. */
20162 if (fcp->dbg_tick != debug_tick)
20163 {
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 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020168#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020169 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020170 func_line_end(cookie);
20171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172
Bram Moolenaar05159a02005-02-26 23:04:13 +000020173 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020174 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20175 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 retval = NULL;
20177 else
20178 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020179 /* Skip NULL lines (continuation lines). */
20180 while (fcp->linenr < gap->ga_len
20181 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20182 ++fcp->linenr;
20183 if (fcp->linenr >= gap->ga_len)
20184 retval = NULL;
20185 else
20186 {
20187 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20188 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020189#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020190 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020191 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020192#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 }
20195
20196 /* Did we encounter a breakpoint? */
20197 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20198 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020199 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020201 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 sourcing_lnum);
20203 fcp->dbg_tick = debug_tick;
20204 }
20205
20206 return retval;
20207}
20208
Bram Moolenaar05159a02005-02-26 23:04:13 +000020209#if defined(FEAT_PROFILE) || defined(PROTO)
20210/*
20211 * Called when starting to read a function line.
20212 * "sourcing_lnum" must be correct!
20213 * When skipping lines it may not actually be executed, but we won't find out
20214 * until later and we need to store the time now.
20215 */
20216 void
20217func_line_start(cookie)
20218 void *cookie;
20219{
20220 funccall_T *fcp = (funccall_T *)cookie;
20221 ufunc_T *fp = fcp->func;
20222
20223 if (fp->uf_profiling && sourcing_lnum >= 1
20224 && sourcing_lnum <= fp->uf_lines.ga_len)
20225 {
20226 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020227 /* Skip continuation lines. */
20228 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20229 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020230 fp->uf_tml_execed = FALSE;
20231 profile_start(&fp->uf_tml_start);
20232 profile_zero(&fp->uf_tml_children);
20233 profile_get_wait(&fp->uf_tml_wait);
20234 }
20235}
20236
20237/*
20238 * Called when actually executing a function line.
20239 */
20240 void
20241func_line_exec(cookie)
20242 void *cookie;
20243{
20244 funccall_T *fcp = (funccall_T *)cookie;
20245 ufunc_T *fp = fcp->func;
20246
20247 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20248 fp->uf_tml_execed = TRUE;
20249}
20250
20251/*
20252 * Called when done with a function line.
20253 */
20254 void
20255func_line_end(cookie)
20256 void *cookie;
20257{
20258 funccall_T *fcp = (funccall_T *)cookie;
20259 ufunc_T *fp = fcp->func;
20260
20261 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20262 {
20263 if (fp->uf_tml_execed)
20264 {
20265 ++fp->uf_tml_count[fp->uf_tml_idx];
20266 profile_end(&fp->uf_tml_start);
20267 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020268 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020269 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20270 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020271 }
20272 fp->uf_tml_idx = -1;
20273 }
20274}
20275#endif
20276
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277/*
20278 * Return TRUE if the currently active function should be ended, because a
20279 * return was encountered or an error occured. Used inside a ":while".
20280 */
20281 int
20282func_has_ended(cookie)
20283 void *cookie;
20284{
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286
20287 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20288 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020289 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 || fcp->returned);
20291}
20292
20293/*
20294 * return TRUE if cookie indicates a function which "abort"s on errors.
20295 */
20296 int
20297func_has_abort(cookie)
20298 void *cookie;
20299{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020300 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301}
20302
20303#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20304typedef enum
20305{
20306 VAR_FLAVOUR_DEFAULT,
20307 VAR_FLAVOUR_SESSION,
20308 VAR_FLAVOUR_VIMINFO
20309} var_flavour_T;
20310
20311static var_flavour_T var_flavour __ARGS((char_u *varname));
20312
20313 static var_flavour_T
20314var_flavour(varname)
20315 char_u *varname;
20316{
20317 char_u *p = varname;
20318
20319 if (ASCII_ISUPPER(*p))
20320 {
20321 while (*(++p))
20322 if (ASCII_ISLOWER(*p))
20323 return VAR_FLAVOUR_SESSION;
20324 return VAR_FLAVOUR_VIMINFO;
20325 }
20326 else
20327 return VAR_FLAVOUR_DEFAULT;
20328}
20329#endif
20330
20331#if defined(FEAT_VIMINFO) || defined(PROTO)
20332/*
20333 * Restore global vars that start with a capital from the viminfo file
20334 */
20335 int
20336read_viminfo_varlist(virp, writing)
20337 vir_T *virp;
20338 int writing;
20339{
20340 char_u *tab;
20341 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343
20344 if (!writing && (find_viminfo_parameter('!') != NULL))
20345 {
20346 tab = vim_strchr(virp->vir_line + 1, '\t');
20347 if (tab != NULL)
20348 {
20349 *tab++ = '\0'; /* isolate the variable name */
20350 if (*tab == 'S') /* string var */
20351 is_string = TRUE;
20352
20353 tab = vim_strchr(tab, '\t');
20354 if (tab != NULL)
20355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 if (is_string)
20357 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020358 tv.v_type = VAR_STRING;
20359 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 }
20362 else
20363 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020364 tv.v_type = VAR_NUMBER;
20365 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020367 set_var(virp->vir_line + 1, &tv, FALSE);
20368 if (is_string)
20369 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 }
20371 }
20372 }
20373
20374 return viminfo_readline(virp);
20375}
20376
20377/*
20378 * Write global vars that start with a capital to the viminfo file
20379 */
20380 void
20381write_viminfo_varlist(fp)
20382 FILE *fp;
20383{
Bram Moolenaar33570922005-01-25 22:26:29 +000020384 hashitem_T *hi;
20385 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020386 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020387 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020388 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020389 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020390 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391
20392 if (find_viminfo_parameter('!') == NULL)
20393 return;
20394
20395 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020396
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020397 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020398 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020400 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020402 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 this_var = HI2DI(hi);
20404 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020405 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020406 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020407 {
20408 case VAR_STRING: s = "STR"; break;
20409 case VAR_NUMBER: s = "NUM"; break;
20410 default: continue;
20411 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020413 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020414 if (p != NULL)
20415 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020416 vim_free(tofree);
20417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 }
20419 }
20420}
20421#endif
20422
20423#if defined(FEAT_SESSION) || defined(PROTO)
20424 int
20425store_session_globals(fd)
20426 FILE *fd;
20427{
Bram Moolenaar33570922005-01-25 22:26:29 +000020428 hashitem_T *hi;
20429 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020430 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 char_u *p, *t;
20432
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020433 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020434 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020436 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020438 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 this_var = HI2DI(hi);
20440 if ((this_var->di_tv.v_type == VAR_NUMBER
20441 || this_var->di_tv.v_type == VAR_STRING)
20442 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020443 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020444 /* Escape special characters with a backslash. Turn a LF and
20445 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020446 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020447 (char_u *)"\\\"\n\r");
20448 if (p == NULL) /* out of memory */
20449 break;
20450 for (t = p; *t != NUL; ++t)
20451 if (*t == '\n')
20452 *t = 'n';
20453 else if (*t == '\r')
20454 *t = 'r';
20455 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020456 this_var->di_key,
20457 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20458 : ' ',
20459 p,
20460 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20461 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020462 || put_eol(fd) == FAIL)
20463 {
20464 vim_free(p);
20465 return FAIL;
20466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 vim_free(p);
20468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 }
20470 }
20471 return OK;
20472}
20473#endif
20474
Bram Moolenaar661b1822005-07-28 22:36:45 +000020475/*
20476 * Display script name where an item was last set.
20477 * Should only be invoked when 'verbose' is non-zero.
20478 */
20479 void
20480last_set_msg(scriptID)
20481 scid_T scriptID;
20482{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020483 char_u *p;
20484
Bram Moolenaar661b1822005-07-28 22:36:45 +000020485 if (scriptID != 0)
20486 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020487 p = home_replace_save(NULL, get_scriptname(scriptID));
20488 if (p != NULL)
20489 {
20490 verbose_enter();
20491 MSG_PUTS(_("\n\tLast set from "));
20492 MSG_PUTS(p);
20493 vim_free(p);
20494 verbose_leave();
20495 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020496 }
20497}
20498
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499#endif /* FEAT_EVAL */
20500
20501#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20502
20503
20504#ifdef WIN3264
20505/*
20506 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20507 */
20508static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20509static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20510static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20511
20512/*
20513 * Get the short pathname of a file.
20514 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20515 */
20516 static int
20517get_short_pathname(fnamep, bufp, fnamelen)
20518 char_u **fnamep;
20519 char_u **bufp;
20520 int *fnamelen;
20521{
20522 int l,len;
20523 char_u *newbuf;
20524
20525 len = *fnamelen;
20526
20527 l = GetShortPathName(*fnamep, *fnamep, len);
20528 if (l > len - 1)
20529 {
20530 /* If that doesn't work (not enough space), then save the string
20531 * and try again with a new buffer big enough
20532 */
20533 newbuf = vim_strnsave(*fnamep, l);
20534 if (newbuf == NULL)
20535 return 0;
20536
20537 vim_free(*bufp);
20538 *fnamep = *bufp = newbuf;
20539
20540 l = GetShortPathName(*fnamep,*fnamep,l+1);
20541
20542 /* Really should always succeed, as the buffer is big enough */
20543 }
20544
20545 *fnamelen = l;
20546 return 1;
20547}
20548
20549/*
20550 * Create a short path name. Returns the length of the buffer it needs.
20551 * Doesn't copy over the end of the buffer passed in.
20552 */
20553 static int
20554shortpath_for_invalid_fname(fname, bufp, fnamelen)
20555 char_u **fname;
20556 char_u **bufp;
20557 int *fnamelen;
20558{
20559 char_u *s, *p, *pbuf2, *pbuf3;
20560 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020561 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562
20563 /* Make a copy */
20564 len2 = *fnamelen;
20565 pbuf2 = vim_strnsave(*fname, len2);
20566 pbuf3 = NULL;
20567
20568 s = pbuf2 + len2 - 1; /* Find the end */
20569 slen = 1;
20570 plen = len2;
20571
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020572 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 {
20574 --s;
20575 ++slen;
20576 --plen;
20577 }
20578
20579 do
20580 {
20581 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020582 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 {
20584 --s;
20585 ++slen;
20586 --plen;
20587 }
20588 if (s <= pbuf2)
20589 break;
20590
20591 /* Remeber the character that is about to be blatted */
20592 ch = *s;
20593 *s = 0; /* get_short_pathname requires a null-terminated string */
20594
20595 /* Try it in situ */
20596 p = pbuf2;
20597 if (!get_short_pathname(&p, &pbuf3, &plen))
20598 {
20599 vim_free(pbuf2);
20600 return -1;
20601 }
20602 *s = ch; /* Preserve the string */
20603 } while (plen == 0);
20604
20605 if (plen > 0)
20606 {
20607 /* Remeber the length of the new string. */
20608 *fnamelen = len = plen + slen;
20609 vim_free(*bufp);
20610 if (len > len2)
20611 {
20612 /* If there's not enough space in the currently allocated string,
20613 * then copy it to a buffer big enough.
20614 */
20615 *fname= *bufp = vim_strnsave(p, len);
20616 if (*fname == NULL)
20617 return -1;
20618 }
20619 else
20620 {
20621 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20622 *fname = *bufp = pbuf2;
20623 if (p != pbuf2)
20624 strncpy(*fname, p, plen);
20625 pbuf2 = NULL;
20626 }
20627 /* Concat the next bit */
20628 strncpy(*fname + plen, s, slen);
20629 (*fname)[len] = '\0';
20630 }
20631 vim_free(pbuf3);
20632 vim_free(pbuf2);
20633 return 0;
20634}
20635
20636/*
20637 * Get a pathname for a partial path.
20638 */
20639 static int
20640shortpath_for_partial(fnamep, bufp, fnamelen)
20641 char_u **fnamep;
20642 char_u **bufp;
20643 int *fnamelen;
20644{
20645 int sepcount, len, tflen;
20646 char_u *p;
20647 char_u *pbuf, *tfname;
20648 int hasTilde;
20649
20650 /* Count up the path seperators from the RHS.. so we know which part
20651 * of the path to return.
20652 */
20653 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020654 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 if (vim_ispathsep(*p))
20656 ++sepcount;
20657
20658 /* Need full path first (use expand_env() to remove a "~/") */
20659 hasTilde = (**fnamep == '~');
20660 if (hasTilde)
20661 pbuf = tfname = expand_env_save(*fnamep);
20662 else
20663 pbuf = tfname = FullName_save(*fnamep, FALSE);
20664
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020665 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666
20667 if (!get_short_pathname(&tfname, &pbuf, &len))
20668 return -1;
20669
20670 if (len == 0)
20671 {
20672 /* Don't have a valid filename, so shorten the rest of the
20673 * path if we can. This CAN give us invalid 8.3 filenames, but
20674 * there's not a lot of point in guessing what it might be.
20675 */
20676 len = tflen;
20677 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20678 return -1;
20679 }
20680
20681 /* Count the paths backward to find the beginning of the desired string. */
20682 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020683 {
20684#ifdef FEAT_MBYTE
20685 if (has_mbyte)
20686 p -= mb_head_off(tfname, p);
20687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 if (vim_ispathsep(*p))
20689 {
20690 if (sepcount == 0 || (hasTilde && sepcount == 1))
20691 break;
20692 else
20693 sepcount --;
20694 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 if (hasTilde)
20697 {
20698 --p;
20699 if (p >= tfname)
20700 *p = '~';
20701 else
20702 return -1;
20703 }
20704 else
20705 ++p;
20706
20707 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20708 vim_free(*bufp);
20709 *fnamelen = (int)STRLEN(p);
20710 *bufp = pbuf;
20711 *fnamep = p;
20712
20713 return 0;
20714}
20715#endif /* WIN3264 */
20716
20717/*
20718 * Adjust a filename, according to a string of modifiers.
20719 * *fnamep must be NUL terminated when called. When returning, the length is
20720 * determined by *fnamelen.
20721 * Returns valid flags.
20722 * When there is an error, *fnamep is set to NULL.
20723 */
20724 int
20725modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20726 char_u *src; /* string with modifiers */
20727 int *usedlen; /* characters after src that are used */
20728 char_u **fnamep; /* file name so far */
20729 char_u **bufp; /* buffer for allocated file name or NULL */
20730 int *fnamelen; /* length of fnamep */
20731{
20732 int valid = 0;
20733 char_u *tail;
20734 char_u *s, *p, *pbuf;
20735 char_u dirname[MAXPATHL];
20736 int c;
20737 int has_fullname = 0;
20738#ifdef WIN3264
20739 int has_shortname = 0;
20740#endif
20741
20742repeat:
20743 /* ":p" - full path/file_name */
20744 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20745 {
20746 has_fullname = 1;
20747
20748 valid |= VALID_PATH;
20749 *usedlen += 2;
20750
20751 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20752 if ((*fnamep)[0] == '~'
20753#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20754 && ((*fnamep)[1] == '/'
20755# ifdef BACKSLASH_IN_FILENAME
20756 || (*fnamep)[1] == '\\'
20757# endif
20758 || (*fnamep)[1] == NUL)
20759
20760#endif
20761 )
20762 {
20763 *fnamep = expand_env_save(*fnamep);
20764 vim_free(*bufp); /* free any allocated file name */
20765 *bufp = *fnamep;
20766 if (*fnamep == NULL)
20767 return -1;
20768 }
20769
20770 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020771 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 {
20773 if (vim_ispathsep(*p)
20774 && p[1] == '.'
20775 && (p[2] == NUL
20776 || vim_ispathsep(p[2])
20777 || (p[2] == '.'
20778 && (p[3] == NUL || vim_ispathsep(p[3])))))
20779 break;
20780 }
20781
20782 /* FullName_save() is slow, don't use it when not needed. */
20783 if (*p != NUL || !vim_isAbsName(*fnamep))
20784 {
20785 *fnamep = FullName_save(*fnamep, *p != NUL);
20786 vim_free(*bufp); /* free any allocated file name */
20787 *bufp = *fnamep;
20788 if (*fnamep == NULL)
20789 return -1;
20790 }
20791
20792 /* Append a path separator to a directory. */
20793 if (mch_isdir(*fnamep))
20794 {
20795 /* Make room for one or two extra characters. */
20796 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20797 vim_free(*bufp); /* free any allocated file name */
20798 *bufp = *fnamep;
20799 if (*fnamep == NULL)
20800 return -1;
20801 add_pathsep(*fnamep);
20802 }
20803 }
20804
20805 /* ":." - path relative to the current directory */
20806 /* ":~" - path relative to the home directory */
20807 /* ":8" - shortname path - postponed till after */
20808 while (src[*usedlen] == ':'
20809 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20810 {
20811 *usedlen += 2;
20812 if (c == '8')
20813 {
20814#ifdef WIN3264
20815 has_shortname = 1; /* Postpone this. */
20816#endif
20817 continue;
20818 }
20819 pbuf = NULL;
20820 /* Need full path first (use expand_env() to remove a "~/") */
20821 if (!has_fullname)
20822 {
20823 if (c == '.' && **fnamep == '~')
20824 p = pbuf = expand_env_save(*fnamep);
20825 else
20826 p = pbuf = FullName_save(*fnamep, FALSE);
20827 }
20828 else
20829 p = *fnamep;
20830
20831 has_fullname = 0;
20832
20833 if (p != NULL)
20834 {
20835 if (c == '.')
20836 {
20837 mch_dirname(dirname, MAXPATHL);
20838 s = shorten_fname(p, dirname);
20839 if (s != NULL)
20840 {
20841 *fnamep = s;
20842 if (pbuf != NULL)
20843 {
20844 vim_free(*bufp); /* free any allocated file name */
20845 *bufp = pbuf;
20846 pbuf = NULL;
20847 }
20848 }
20849 }
20850 else
20851 {
20852 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20853 /* Only replace it when it starts with '~' */
20854 if (*dirname == '~')
20855 {
20856 s = vim_strsave(dirname);
20857 if (s != NULL)
20858 {
20859 *fnamep = s;
20860 vim_free(*bufp);
20861 *bufp = s;
20862 }
20863 }
20864 }
20865 vim_free(pbuf);
20866 }
20867 }
20868
20869 tail = gettail(*fnamep);
20870 *fnamelen = (int)STRLEN(*fnamep);
20871
20872 /* ":h" - head, remove "/file_name", can be repeated */
20873 /* Don't remove the first "/" or "c:\" */
20874 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20875 {
20876 valid |= VALID_HEAD;
20877 *usedlen += 2;
20878 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020879 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 --tail;
20881 *fnamelen = (int)(tail - *fnamep);
20882#ifdef VMS
20883 if (*fnamelen > 0)
20884 *fnamelen += 1; /* the path separator is part of the path */
20885#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020886 while (tail > s && !after_pathsep(s, tail))
20887 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 }
20889
20890 /* ":8" - shortname */
20891 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20892 {
20893 *usedlen += 2;
20894#ifdef WIN3264
20895 has_shortname = 1;
20896#endif
20897 }
20898
20899#ifdef WIN3264
20900 /* Check shortname after we have done 'heads' and before we do 'tails'
20901 */
20902 if (has_shortname)
20903 {
20904 pbuf = NULL;
20905 /* Copy the string if it is shortened by :h */
20906 if (*fnamelen < (int)STRLEN(*fnamep))
20907 {
20908 p = vim_strnsave(*fnamep, *fnamelen);
20909 if (p == 0)
20910 return -1;
20911 vim_free(*bufp);
20912 *bufp = *fnamep = p;
20913 }
20914
20915 /* Split into two implementations - makes it easier. First is where
20916 * there isn't a full name already, second is where there is.
20917 */
20918 if (!has_fullname && !vim_isAbsName(*fnamep))
20919 {
20920 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20921 return -1;
20922 }
20923 else
20924 {
20925 int l;
20926
20927 /* Simple case, already have the full-name
20928 * Nearly always shorter, so try first time. */
20929 l = *fnamelen;
20930 if (!get_short_pathname(fnamep, bufp, &l))
20931 return -1;
20932
20933 if (l == 0)
20934 {
20935 /* Couldn't find the filename.. search the paths.
20936 */
20937 l = *fnamelen;
20938 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20939 return -1;
20940 }
20941 *fnamelen = l;
20942 }
20943 }
20944#endif /* WIN3264 */
20945
20946 /* ":t" - tail, just the basename */
20947 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20948 {
20949 *usedlen += 2;
20950 *fnamelen -= (int)(tail - *fnamep);
20951 *fnamep = tail;
20952 }
20953
20954 /* ":e" - extension, can be repeated */
20955 /* ":r" - root, without extension, can be repeated */
20956 while (src[*usedlen] == ':'
20957 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20958 {
20959 /* find a '.' in the tail:
20960 * - for second :e: before the current fname
20961 * - otherwise: The last '.'
20962 */
20963 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20964 s = *fnamep - 2;
20965 else
20966 s = *fnamep + *fnamelen - 1;
20967 for ( ; s > tail; --s)
20968 if (s[0] == '.')
20969 break;
20970 if (src[*usedlen + 1] == 'e') /* :e */
20971 {
20972 if (s > tail)
20973 {
20974 *fnamelen += (int)(*fnamep - (s + 1));
20975 *fnamep = s + 1;
20976#ifdef VMS
20977 /* cut version from the extension */
20978 s = *fnamep + *fnamelen - 1;
20979 for ( ; s > *fnamep; --s)
20980 if (s[0] == ';')
20981 break;
20982 if (s > *fnamep)
20983 *fnamelen = s - *fnamep;
20984#endif
20985 }
20986 else if (*fnamep <= tail)
20987 *fnamelen = 0;
20988 }
20989 else /* :r */
20990 {
20991 if (s > tail) /* remove one extension */
20992 *fnamelen = (int)(s - *fnamep);
20993 }
20994 *usedlen += 2;
20995 }
20996
20997 /* ":s?pat?foo?" - substitute */
20998 /* ":gs?pat?foo?" - global substitute */
20999 if (src[*usedlen] == ':'
21000 && (src[*usedlen + 1] == 's'
21001 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21002 {
21003 char_u *str;
21004 char_u *pat;
21005 char_u *sub;
21006 int sep;
21007 char_u *flags;
21008 int didit = FALSE;
21009
21010 flags = (char_u *)"";
21011 s = src + *usedlen + 2;
21012 if (src[*usedlen + 1] == 'g')
21013 {
21014 flags = (char_u *)"g";
21015 ++s;
21016 }
21017
21018 sep = *s++;
21019 if (sep)
21020 {
21021 /* find end of pattern */
21022 p = vim_strchr(s, sep);
21023 if (p != NULL)
21024 {
21025 pat = vim_strnsave(s, (int)(p - s));
21026 if (pat != NULL)
21027 {
21028 s = p + 1;
21029 /* find end of substitution */
21030 p = vim_strchr(s, sep);
21031 if (p != NULL)
21032 {
21033 sub = vim_strnsave(s, (int)(p - s));
21034 str = vim_strnsave(*fnamep, *fnamelen);
21035 if (sub != NULL && str != NULL)
21036 {
21037 *usedlen = (int)(p + 1 - src);
21038 s = do_string_sub(str, pat, sub, flags);
21039 if (s != NULL)
21040 {
21041 *fnamep = s;
21042 *fnamelen = (int)STRLEN(s);
21043 vim_free(*bufp);
21044 *bufp = s;
21045 didit = TRUE;
21046 }
21047 }
21048 vim_free(sub);
21049 vim_free(str);
21050 }
21051 vim_free(pat);
21052 }
21053 }
21054 /* after using ":s", repeat all the modifiers */
21055 if (didit)
21056 goto repeat;
21057 }
21058 }
21059
21060 return valid;
21061}
21062
21063/*
21064 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21065 * "flags" can be "g" to do a global substitute.
21066 * Returns an allocated string, NULL for error.
21067 */
21068 char_u *
21069do_string_sub(str, pat, sub, flags)
21070 char_u *str;
21071 char_u *pat;
21072 char_u *sub;
21073 char_u *flags;
21074{
21075 int sublen;
21076 regmatch_T regmatch;
21077 int i;
21078 int do_all;
21079 char_u *tail;
21080 garray_T ga;
21081 char_u *ret;
21082 char_u *save_cpo;
21083
21084 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21085 save_cpo = p_cpo;
21086 p_cpo = (char_u *)"";
21087
21088 ga_init2(&ga, 1, 200);
21089
21090 do_all = (flags[0] == 'g');
21091
21092 regmatch.rm_ic = p_ic;
21093 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21094 if (regmatch.regprog != NULL)
21095 {
21096 tail = str;
21097 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21098 {
21099 /*
21100 * Get some space for a temporary buffer to do the substitution
21101 * into. It will contain:
21102 * - The text up to where the match is.
21103 * - The substituted text.
21104 * - The text after the match.
21105 */
21106 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21107 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21108 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21109 {
21110 ga_clear(&ga);
21111 break;
21112 }
21113
21114 /* copy the text up to where the match is */
21115 i = (int)(regmatch.startp[0] - tail);
21116 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21117 /* add the substituted text */
21118 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21119 + ga.ga_len + i, TRUE, TRUE, FALSE);
21120 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 /* avoid getting stuck on a match with an empty string */
21122 if (tail == regmatch.endp[0])
21123 {
21124 if (*tail == NUL)
21125 break;
21126 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21127 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 }
21129 else
21130 {
21131 tail = regmatch.endp[0];
21132 if (*tail == NUL)
21133 break;
21134 }
21135 if (!do_all)
21136 break;
21137 }
21138
21139 if (ga.ga_data != NULL)
21140 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21141
21142 vim_free(regmatch.regprog);
21143 }
21144
21145 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21146 ga_clear(&ga);
21147 p_cpo = save_cpo;
21148
21149 return ret;
21150}
21151
21152#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */