blob: 412d31d21405165410ed08d2b620968569eb457f [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 */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
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));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
373static void list_glob_vars __ARGS((int *first));
374static void list_buf_vars __ARGS((int *first));
375static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000377static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_vim_vars __ARGS((int *first));
380static void list_script_vars __ARGS((int *first));
381static void list_func_vars __ARGS((int *first));
382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static 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));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000438static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static 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));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000478static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000480#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000481static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000482static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
490static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000503static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000517static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000519static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000525static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000533static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000534static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000535static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000538static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000546static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000560static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000566static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000582static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000586static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000590#ifdef vim_mkdir
591static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
592#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000596static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000598static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000601static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000602static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000615static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000617static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000624static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000626static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000627static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000629static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000631static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000634static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000638static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639#ifdef HAVE_STRFTIME
640static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
642static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000654static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000655static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000657static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000658static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000660static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000674static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000680static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static int get_env_len __ARGS((char_u **arg));
682static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
685#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
686#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
687 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static 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 +0000689static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000690static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000691static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
692static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static typval_T *alloc_tv __ARGS((void));
694static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void init_tv __ARGS((typval_T *varp));
696static long get_tv_number __ARGS((typval_T *varp));
697static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000698static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static char_u *get_tv_string __ARGS((typval_T *varp));
700static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000701static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000703static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
705static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
706static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000707static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
708static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
710static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000711static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000712static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000714static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
716static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
717static int eval_fname_script __ARGS((char_u *p));
718static int eval_fname_sid __ARGS((char_u *p));
719static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static ufunc_T *find_func __ARGS((char_u *name));
721static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000722static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723#ifdef FEAT_PROFILE
724static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000725static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
726static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_total_cmp __ARGS((const void *s1, const void *s2));
732static int
733# ifdef __BORLANDC__
734 _RTLENTRYF
735# endif
736 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000737#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000738static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000739static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000740static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void func_free __ARGS((ufunc_T *fp));
742static void func_unref __ARGS((char_u *name));
743static void func_ref __ARGS((char_u *name));
744static 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));
745static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000746static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
747static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000748static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000749static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000750static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000752/* Character used as separated in autoload function/variable names. */
753#define AUTOLOAD_CHAR '#'
754
Bram Moolenaar33570922005-01-25 22:26:29 +0000755/*
756 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000757 */
758 void
759eval_init()
760{
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 int i;
762 struct vimvar *p;
763
764 init_var_dict(&globvardict, &globvars_var);
765 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000766 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000767 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768
769 for (i = 0; i < VV_LEN; ++i)
770 {
771 p = &vimvars[i];
772 STRCPY(p->vv_di.di_key, p->vv_name);
773 if (p->vv_flags & VV_RO)
774 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
775 else if (p->vv_flags & VV_RO_SBX)
776 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
777 else
778 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779
780 /* add to v: scope dict, unless the value is not always available */
781 if (p->vv_type != VAR_UNKNOWN)
782 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000784 /* add to compat scope dict */
785 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000787}
788
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789#if defined(EXITFREE) || defined(PROTO)
790 void
791eval_clear()
792{
793 int i;
794 struct vimvar *p;
795
796 for (i = 0; i < VV_LEN; ++i)
797 {
798 p = &vimvars[i];
799 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000800 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 p->vv_di.di_tv.vval.v_string = NULL;
803 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 }
805 hash_clear(&vimvarht);
806 hash_clear(&compat_hashtab);
807
808 /* script-local variables */
809 for (i = 1; i <= ga_scripts.ga_len; ++i)
810 vars_clear(&SCRIPT_VARS(i));
811 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000813
814 /* global variables */
815 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 hash_clear(&func_hashtab);
820
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000821 /* autoloaded script names */
822 ga_clear_strings(&ga_loaded);
823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000824 /* unreferenced lists and dicts */
825 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000826}
827#endif
828
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Return the name of the executed function.
831 */
832 char_u *
833func_name(cookie)
834 void *cookie;
835{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000836 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
839/*
840 * Return the address holding the next breakpoint line for a funccall cookie.
841 */
842 linenr_T *
843func_breakpoint(cookie)
844 void *cookie;
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
849/*
850 * Return the address holding the debug tick for a funccall cookie.
851 */
852 int *
853func_dbg_tick(cookie)
854 void *cookie;
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
859/*
860 * Return the nesting level for a funccall cookie.
861 */
862 int
863func_level(cookie)
864 void *cookie;
865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000870funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872/*
873 * Return TRUE when a function was ended by a ":return" command.
874 */
875 int
876current_func_returned()
877{
878 return current_funccal->returned;
879}
880
881
882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Set an internal variable to a string value. Creates the variable if it does
884 * not already exist.
885 */
886 void
887set_internal_string_var(name, value)
888 char_u *name;
889 char_u *value;
890{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 val = vim_strsave(value);
895 if (val != NULL)
896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000898 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 }
904}
905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000907static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static char_u *redir_endp = NULL;
909static char_u *redir_varname = NULL;
910
911/*
912 * Start recording command output to a variable
913 * Returns OK if successfully completed the setup. FAIL otherwise.
914 */
915 int
916var_redir_start(name, append)
917 char_u *name;
918 int append; /* append to an existing variable */
919{
920 int save_emsg;
921 int err;
922 typval_T tv;
923
924 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000925 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000926 {
927 EMSG(_(e_invarg));
928 return FAIL;
929 }
930
931 redir_varname = vim_strsave(name);
932 if (redir_varname == NULL)
933 return FAIL;
934
935 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
936 if (redir_lval == NULL)
937 {
938 var_redir_stop();
939 return FAIL;
940 }
941
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000942 /* The output is stored in growarray "redir_ga" until redirection ends. */
943 ga_init2(&redir_ga, (int)sizeof(char), 500);
944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000946 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
947 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
949 {
950 if (redir_endp != NULL && *redir_endp != NUL)
951 /* Trailing characters are present after the variable name */
952 EMSG(_(e_trailing));
953 else
954 EMSG(_(e_invarg));
955 var_redir_stop();
956 return FAIL;
957 }
958
959 /* check if we can write to the variable: set it to or append an empty
960 * string */
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 tv.v_type = VAR_STRING;
964 tv.vval.v_string = (char_u *)"";
965 if (append)
966 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
967 else
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
969 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000970 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971 if (err)
972 {
973 var_redir_stop();
974 return FAIL;
975 }
976 if (redir_lval->ll_newkey != NULL)
977 {
978 /* Dictionary item was created, don't do it again. */
979 vim_free(redir_lval->ll_newkey);
980 redir_lval->ll_newkey = NULL;
981 }
982
983 return OK;
984}
985
986/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000987 * Append "value[value_len]" to the variable set by var_redir_start().
988 * The actual appending is postponed until redirection ends, because the value
989 * appended may in fact be the string we write to, changing it may cause freed
990 * memory to be used:
991 * :redir => foo
992 * :let foo
993 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 */
995 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000996var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001000 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001
1002 if (redir_lval == NULL)
1003 return;
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001006 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011 {
1012 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 }
1015 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017}
1018
1019/*
1020 * Stop redirecting command output to a variable.
1021 */
1022 void
1023var_redir_stop()
1024{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001025 typval_T tv;
1026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 if (redir_lval != NULL)
1028 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 /* Append the trailing NUL. */
1030 ga_append(&redir_ga, NUL);
1031
1032 /* Assign the text to the variable. */
1033 tv.v_type = VAR_STRING;
1034 tv.vval.v_string = redir_ga.ga_data;
1035 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1036 vim_free(tv.vval.v_string);
1037
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 clear_lval(redir_lval);
1039 vim_free(redir_lval);
1040 redir_lval = NULL;
1041 }
1042 vim_free(redir_varname);
1043 redir_varname = NULL;
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# if defined(FEAT_MBYTE) || defined(PROTO)
1047 int
1048eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1049 char_u *enc_from;
1050 char_u *enc_to;
1051 char_u *fname_from;
1052 char_u *fname_to;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1057 set_vim_var_string(VV_CC_TO, enc_to, -1);
1058 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1059 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1060 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1061 err = TRUE;
1062 set_vim_var_string(VV_CC_FROM, NULL, -1);
1063 set_vim_var_string(VV_CC_TO, NULL, -1);
1064 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066
1067 if (err)
1068 return FAIL;
1069 return OK;
1070}
1071# endif
1072
1073# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1074 int
1075eval_printexpr(fname, args)
1076 char_u *fname;
1077 char_u *args;
1078{
1079 int err = FALSE;
1080
1081 set_vim_var_string(VV_FNAME_IN, fname, -1);
1082 set_vim_var_string(VV_CMDARG, args, -1);
1083 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1084 err = TRUE;
1085 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1086 set_vim_var_string(VV_CMDARG, NULL, -1);
1087
1088 if (err)
1089 {
1090 mch_remove(fname);
1091 return FAIL;
1092 }
1093 return OK;
1094}
1095# endif
1096
1097# if defined(FEAT_DIFF) || defined(PROTO)
1098 void
1099eval_diff(origfile, newfile, outfile)
1100 char_u *origfile;
1101 char_u *newfile;
1102 char_u *outfile;
1103{
1104 int err = FALSE;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114
1115 void
1116eval_patch(origfile, difffile, outfile)
1117 char_u *origfile;
1118 char_u *difffile;
1119 char_u *outfile;
1120{
1121 int err;
1122
1123 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1124 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1125 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1126 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1129 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1130}
1131# endif
1132
1133/*
1134 * Top level evaluation function, returning a boolean.
1135 * Sets "error" to TRUE if there was an error.
1136 * Return TRUE or FALSE.
1137 */
1138 int
1139eval_to_bool(arg, error, nextcmd, skip)
1140 char_u *arg;
1141 int *error;
1142 char_u **nextcmd;
1143 int skip; /* only parse, don't execute */
1144{
Bram Moolenaar33570922005-01-25 22:26:29 +00001145 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 int retval = FALSE;
1147
1148 if (skip)
1149 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 {
1154 *error = FALSE;
1155 if (!skip)
1156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 }
1161 if (skip)
1162 --emsg_skip;
1163
1164 return retval;
1165}
1166
1167/*
1168 * Top level evaluation function, returning a string. If "skip" is TRUE,
1169 * only parsing to "nextcmd" is done, without reporting errors. Return
1170 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1171 */
1172 char_u *
1173eval_to_string_skip(arg, nextcmd, skip)
1174 char_u *arg;
1175 char_u **nextcmd;
1176 int skip; /* only parse, don't execute */
1177{
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *retval;
1180
1181 if (skip)
1182 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 retval = NULL;
1185 else
1186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 retval = vim_strsave(get_tv_string(&tv));
1188 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 if (skip)
1191 --emsg_skip;
1192
1193 return retval;
1194}
1195
1196/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001197 * Skip over an expression at "*pp".
1198 * Return FAIL for an error, OK otherwise.
1199 */
1200 int
1201skip_expr(pp)
1202 char_u **pp;
1203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001205
1206 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208}
1209
1210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * Top level evaluation function, returning a string.
1212 * Return pointer to allocated memory, or NULL for failure.
1213 */
1214 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001215eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *arg;
1217 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 retval = NULL;
1226 else
1227 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001228 if (dolist && tv.v_type == VAR_LIST)
1229 {
1230 ga_init2(&ga, (int)sizeof(char), 80);
1231 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1232 ga_append(&ga, NUL);
1233 retval = (char_u *)ga.ga_data;
1234 }
1235 else
1236 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239
1240 return retval;
1241}
1242
1243/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 * Call eval_to_string() without using current local variables and using
1245 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
1247 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 char_u *arg;
1250 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 char_u *retval;
1254 void *save_funccalp;
1255
1256 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001257 if (use_sandbox)
1258 ++sandbox;
1259 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001260 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 if (use_sandbox)
1262 --sandbox;
1263 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 restore_funccal(save_funccalp);
1265 return retval;
1266}
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Top level evaluation function, returning a number.
1270 * Evaluates "expr" silently.
1271 * Returns -1 for an error.
1272 */
1273 int
1274eval_to_number(expr)
1275 char_u *expr;
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001279 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 ++emsg_off;
1282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 retval = -1;
1285 else
1286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001287 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 --emsg_off;
1291
1292 return retval;
1293}
1294
Bram Moolenaara40058a2005-07-11 22:42:07 +00001295/*
1296 * Prepare v: variable "idx" to be used.
1297 * Save the current typeval in "save_tv".
1298 * When not used yet add the variable to the v: hashtable.
1299 */
1300 static void
1301prepare_vimvar(idx, save_tv)
1302 int idx;
1303 typval_T *save_tv;
1304{
1305 *save_tv = vimvars[idx].vv_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1308}
1309
1310/*
1311 * Restore v: variable "idx" to typeval "save_tv".
1312 * When no longer defined, remove the variable from the v: hashtable.
1313 */
1314 static void
1315restore_vimvar(idx, save_tv)
1316 int idx;
1317 typval_T *save_tv;
1318{
1319 hashitem_T *hi;
1320
Bram Moolenaara40058a2005-07-11 22:42:07 +00001321 vimvars[idx].vv_tv = *save_tv;
1322 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1323 {
1324 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1325 if (HASHITEM_EMPTY(hi))
1326 EMSG2(_(e_intern2), "restore_vimvar()");
1327 else
1328 hash_remove(&vimvarht, hi);
1329 }
1330}
1331
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001332#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001333/*
1334 * Evaluate an expression to a list with suggestions.
1335 * For the "expr:" part of 'spellsuggest'.
1336 */
1337 list_T *
1338eval_spell_expr(badword, expr)
1339 char_u *badword;
1340 char_u *expr;
1341{
1342 typval_T save_val;
1343 typval_T rettv;
1344 list_T *list = NULL;
1345 char_u *p = skipwhite(expr);
1346
1347 /* Set "v:val" to the bad word. */
1348 prepare_vimvar(VV_VAL, &save_val);
1349 vimvars[VV_VAL].vv_type = VAR_STRING;
1350 vimvars[VV_VAL].vv_str = badword;
1351 if (p_verbose == 0)
1352 ++emsg_off;
1353
1354 if (eval1(&p, &rettv, TRUE) == OK)
1355 {
1356 if (rettv.v_type != VAR_LIST)
1357 clear_tv(&rettv);
1358 else
1359 list = rettv.vval.v_list;
1360 }
1361
1362 if (p_verbose == 0)
1363 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001364 restore_vimvar(VV_VAL, &save_val);
1365
1366 return list;
1367}
1368
1369/*
1370 * "list" is supposed to contain two items: a word and a number. Return the
1371 * word in "pp" and the number as the return value.
1372 * Return -1 if anything isn't right.
1373 * Used to get the good word and score from the eval_spell_expr() result.
1374 */
1375 int
1376get_spellword(list, pp)
1377 list_T *list;
1378 char_u **pp;
1379{
1380 listitem_T *li;
1381
1382 li = list->lv_first;
1383 if (li == NULL)
1384 return -1;
1385 *pp = get_tv_string(&li->li_tv);
1386
1387 li = li->li_next;
1388 if (li == NULL)
1389 return -1;
1390 return get_tv_number(&li->li_tv);
1391}
1392#endif
1393
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001394/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 * Top level evaluation function.
1396 * Returns an allocated typval_T with the result.
1397 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398 */
1399 typval_T *
1400eval_expr(arg, nextcmd)
1401 char_u *arg;
1402 char_u **nextcmd;
1403{
1404 typval_T *tv;
1405
1406 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001407 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001408 {
1409 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001410 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001411 }
1412
1413 return tv;
1414}
1415
1416
Bram Moolenaar4f688582007-07-24 12:34:30 +00001417#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1418 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 static int
1425call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 char_u *func;
1427 int argc;
1428 char_u **argv;
1429 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 long n;
1434 int len;
1435 int i;
1436 int doesrange;
1437 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001440 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 for (i = 0; i < argc; i++)
1445 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001446 /* Pass a NULL or empty argument as an empty string */
1447 if (argv[i] == NULL || *argv[i] == NUL)
1448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001449 argvars[i].v_type = VAR_STRING;
1450 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001451 continue;
1452 }
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /* Recognize a number argument, the others must be strings. */
1455 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1456 if (len != 0 && len == (int)STRLEN(argv[i]))
1457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001458 argvars[i].v_type = VAR_NUMBER;
1459 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461 else
1462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001463 argvars[i].v_type = VAR_STRING;
1464 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466 }
1467
1468 if (safe)
1469 {
1470 save_funccalp = save_funccal();
1471 ++sandbox;
1472 }
1473
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001474 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1475 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (safe)
1479 {
1480 --sandbox;
1481 restore_funccal(save_funccalp);
1482 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001483 vim_free(argvars);
1484
1485 if (ret == FAIL)
1486 clear_tv(rettv);
1487
1488 return ret;
1489}
1490
Bram Moolenaar4f688582007-07-24 12:34:30 +00001491# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001492/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001493 * Call vimL function "func" and return the result as a string.
1494 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495 * Uses argv[argc] for the function arguments.
1496 */
1497 void *
1498call_func_retstr(func, argc, argv, safe)
1499 char_u *func;
1500 int argc;
1501 char_u **argv;
1502 int safe; /* use the sandbox */
1503{
1504 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001505 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506
1507 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1508 return NULL;
1509
1510 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 return retval;
1513}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001514# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001515
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001518 * Call vimL function "func" and return the result as a number.
1519 * Returns -1 when calling the function fails.
1520 * Uses argv[argc] for the function arguments.
1521 */
1522 long
1523call_func_retnr(func, argc, argv, safe)
1524 char_u *func;
1525 int argc;
1526 char_u **argv;
1527 int safe; /* use the sandbox */
1528{
1529 typval_T rettv;
1530 long retval;
1531
1532 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1533 return -1;
1534
1535 retval = get_tv_number_chk(&rettv, NULL);
1536 clear_tv(&rettv);
1537 return retval;
1538}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001539# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001540
1541/*
1542 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001543 * Uses argv[argc] for the function arguments.
1544 */
1545 void *
1546call_func_retlist(func, argc, argv, safe)
1547 char_u *func;
1548 int argc;
1549 char_u **argv;
1550 int safe; /* use the sandbox */
1551{
1552 typval_T rettv;
1553
1554 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1555 return NULL;
1556
1557 if (rettv.v_type != VAR_LIST)
1558 {
1559 clear_tv(&rettv);
1560 return NULL;
1561 }
1562
1563 return rettv.vval.v_list;
1564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
1566
Bram Moolenaar4f688582007-07-24 12:34:30 +00001567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568/*
1569 * Save the current function call pointer, and set it to NULL.
1570 * Used when executing autocommands and for ":source".
1571 */
1572 void *
1573save_funccal()
1574{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001575 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 current_funccal = NULL;
1578 return (void *)fc;
1579}
1580
1581 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001582restore_funccal(vfc)
1583 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001585 funccall_T *fc = (funccall_T *)vfc;
1586
1587 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588}
1589
Bram Moolenaar05159a02005-02-26 23:04:13 +00001590#if defined(FEAT_PROFILE) || defined(PROTO)
1591/*
1592 * Prepare profiling for entering a child or something else that is not
1593 * counted for the script/function itself.
1594 * Should always be called in pair with prof_child_exit().
1595 */
1596 void
1597prof_child_enter(tm)
1598 proftime_T *tm; /* place to store waittime */
1599{
1600 funccall_T *fc = current_funccal;
1601
1602 if (fc != NULL && fc->func->uf_profiling)
1603 profile_start(&fc->prof_child);
1604 script_prof_save(tm);
1605}
1606
1607/*
1608 * Take care of time spent in a child.
1609 * Should always be called after prof_child_enter().
1610 */
1611 void
1612prof_child_exit(tm)
1613 proftime_T *tm; /* where waittime was stored */
1614{
1615 funccall_T *fc = current_funccal;
1616
1617 if (fc != NULL && fc->func->uf_profiling)
1618 {
1619 profile_end(&fc->prof_child);
1620 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1621 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1622 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1623 }
1624 script_prof_restore(tm);
1625}
1626#endif
1627
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#ifdef FEAT_FOLDING
1630/*
1631 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1632 * it in "*cp". Doesn't give error messages.
1633 */
1634 int
1635eval_foldexpr(arg, cp)
1636 char_u *arg;
1637 int *cp;
1638{
Bram Moolenaar33570922005-01-25 22:26:29 +00001639 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 int retval;
1641 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001642 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1643 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001646 if (use_sandbox)
1647 ++sandbox;
1648 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 retval = 0;
1652 else
1653 {
1654 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 if (tv.v_type == VAR_NUMBER)
1656 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001657 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 retval = 0;
1659 else
1660 {
1661 /* If the result is a string, check if there is a non-digit before
1662 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (!VIM_ISDIGIT(*s) && *s != '-')
1665 *cp = *s++;
1666 retval = atol((char *)s);
1667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001671 if (use_sandbox)
1672 --sandbox;
1673 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 return retval;
1676}
1677#endif
1678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 * ":let" list all variable values
1681 * ":let var1 var2" list variable values
1682 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 * ":let var += expr" assignment command.
1684 * ":let var -= expr" assignment command.
1685 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 void
1689ex_let(eap)
1690 exarg_T *eap;
1691{
1692 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 int var_count = 0;
1697 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001699 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001700 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaardb552d602006-03-23 22:59:57 +00001702 argend = skip_var_list(arg, &var_count, &semicolon);
1703 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001705 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1706 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001707 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001710 /*
1711 * ":let" without "=": list variables
1712 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 if (*arg == '[')
1714 EMSG(_(e_invarg));
1715 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001717 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001721 list_glob_vars(&first);
1722 list_buf_vars(&first);
1723 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001724#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001725 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001727 list_script_vars(&first);
1728 list_func_vars(&first);
1729 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 eap->nextcmd = check_nextcmd(arg);
1732 }
1733 else
1734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 op[0] = '=';
1736 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001737 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 {
1739 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1740 op[0] = expr[-1]; /* +=, -= or .= */
1741 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (eap->skip)
1745 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (eap->skip)
1748 {
1749 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --emsg_skip;
1752 }
1753 else if (i != FAIL)
1754 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759 }
1760}
1761
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762/*
1763 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1764 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 * When "nextchars" is not NULL it points to a string with characters that
1766 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1767 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 * Returns OK or FAIL;
1769 */
1770 static int
1771ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1772 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 int copy; /* copy values from "tv", don't move */
1775 int semicolon; /* from skip_var_list() */
1776 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778{
1779 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 listitem_T *item;
1783 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784
1785 if (*arg != '[')
1786 {
1787 /*
1788 * ":let var = expr" or ":for var in list"
1789 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 return FAIL;
1792 return OK;
1793 }
1794
1795 /*
1796 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1797 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001798 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 {
1800 EMSG(_(e_listreq));
1801 return FAIL;
1802 }
1803
1804 i = list_len(l);
1805 if (semicolon == 0 && var_count < i)
1806 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001807 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 return FAIL;
1809 }
1810 if (var_count - semicolon > i)
1811 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001812 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 return FAIL;
1814 }
1815
1816 item = l->lv_first;
1817 while (*arg != ']')
1818 {
1819 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 item = item->li_next;
1822 if (arg == NULL)
1823 return FAIL;
1824
1825 arg = skipwhite(arg);
1826 if (*arg == ';')
1827 {
1828 /* Put the rest of the list (may be empty) in the var after ';'.
1829 * Create a new list for this. */
1830 l = list_alloc();
1831 if (l == NULL)
1832 return FAIL;
1833 while (item != NULL)
1834 {
1835 list_append_tv(l, &item->li_tv);
1836 item = item->li_next;
1837 }
1838
1839 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001840 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 ltv.vval.v_list = l;
1842 l->lv_refcount = 1;
1843
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1845 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 clear_tv(&ltv);
1847 if (arg == NULL)
1848 return FAIL;
1849 break;
1850 }
1851 else if (*arg != ',' && *arg != ']')
1852 {
1853 EMSG2(_(e_intern2), "ex_let_vars()");
1854 return FAIL;
1855 }
1856 }
1857
1858 return OK;
1859}
1860
1861/*
1862 * Skip over assignable variable "var" or list of variables "[var, var]".
1863 * Used for ":let varvar = expr" and ":for varvar in expr".
1864 * For "[var, var]" increment "*var_count" for each variable.
1865 * for "[var, var; var]" set "semicolon".
1866 * Return NULL for an error.
1867 */
1868 static char_u *
1869skip_var_list(arg, var_count, semicolon)
1870 char_u *arg;
1871 int *var_count;
1872 int *semicolon;
1873{
1874 char_u *p, *s;
1875
1876 if (*arg == '[')
1877 {
1878 /* "[var, var]": find the matching ']'. */
1879 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001880 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 {
1882 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1883 s = skip_var_one(p);
1884 if (s == p)
1885 {
1886 EMSG2(_(e_invarg2), p);
1887 return NULL;
1888 }
1889 ++*var_count;
1890
1891 p = skipwhite(s);
1892 if (*p == ']')
1893 break;
1894 else if (*p == ';')
1895 {
1896 if (*semicolon == 1)
1897 {
1898 EMSG(_("Double ; in list of variables"));
1899 return NULL;
1900 }
1901 *semicolon = 1;
1902 }
1903 else if (*p != ',')
1904 {
1905 EMSG2(_(e_invarg2), p);
1906 return NULL;
1907 }
1908 }
1909 return p + 1;
1910 }
1911 else
1912 return skip_var_one(arg);
1913}
1914
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001915/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001916 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001917 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 static char_u *
1920skip_var_one(arg)
1921 char_u *arg;
1922{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001923 if (*arg == '@' && arg[1] != NUL)
1924 return arg + 2;
1925 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1926 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927}
1928
Bram Moolenaara7043832005-01-21 11:56:39 +00001929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 * List variables for hashtab "ht" with prefix "prefix".
1931 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001939{
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 hashitem_T *hi;
1941 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001942 int todo;
1943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001944 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1946 {
1947 if (!HASHITEM_EMPTY(hi))
1948 {
1949 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 di = HI2DI(hi);
1951 if (empty || di->di_tv.v_type != VAR_STRING
1952 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001953 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001954 }
1955 }
1956}
1957
1958/*
1959 * List global variables.
1960 */
1961 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001962list_glob_vars(first)
1963 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001964{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001966}
1967
1968/*
1969 * List buffer variables.
1970 */
1971 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972list_buf_vars(first)
1973 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001974{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 char_u numbuf[NUMBUFLEN];
1976
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001977 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1978 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979
1980 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1982 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001983}
1984
1985/*
1986 * List window variables.
1987 */
1988 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001989list_win_vars(first)
1990 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001991{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001992 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1993 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001994}
1995
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001996#ifdef FEAT_WINDOWS
1997/*
1998 * List tab page variables.
1999 */
2000 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002001list_tab_vars(first)
2002 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002004 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2005 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006}
2007#endif
2008
Bram Moolenaara7043832005-01-21 11:56:39 +00002009/*
2010 * List Vim variables.
2011 */
2012 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013list_vim_vars(first)
2014 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002016 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017}
2018
2019/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002020 * List script-local variables, if there is a script.
2021 */
2022 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002023list_script_vars(first)
2024 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002025{
2026 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002027 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2028 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002029}
2030
2031/*
2032 * List function variables, if there is a function.
2033 */
2034 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002035list_func_vars(first)
2036 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002037{
2038 if (current_funccal != NULL)
2039 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002040 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002041}
2042
2043/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 * List variables in "arg".
2045 */
2046 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002047list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 exarg_T *eap;
2049 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002050 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051{
2052 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 char_u *name_start;
2056 char_u *arg_subsc;
2057 char_u *tofree;
2058 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059
2060 while (!ends_excmd(*arg) && !got_int)
2061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002064 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2066 {
2067 emsg_severe = TRUE;
2068 EMSG(_(e_trailing));
2069 break;
2070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 /* get_name_len() takes care of expanding curly braces */
2075 name_start = name = arg;
2076 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2077 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002079 /* This is mainly to keep test 49 working: when expanding
2080 * curly braces fails overrule the exception error message. */
2081 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 emsg_severe = TRUE;
2084 EMSG2(_(e_invarg2), arg);
2085 break;
2086 }
2087 error = TRUE;
2088 }
2089 else
2090 {
2091 if (tofree != NULL)
2092 name = tofree;
2093 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 else
2096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 /* handle d.key, l[idx], f(expr) */
2098 arg_subsc = arg;
2099 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002100 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002105 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 case 'g': list_glob_vars(first); break;
2108 case 'b': list_buf_vars(first); break;
2109 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002110#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 case 'v': list_vim_vars(first); break;
2114 case 's': list_script_vars(first); break;
2115 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 default:
2117 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 }
2120 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 {
2122 char_u numbuf[NUMBUFLEN];
2123 char_u *tf;
2124 int c;
2125 char_u *s;
2126
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002127 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128 c = *arg;
2129 *arg = NUL;
2130 list_one_var_a((char_u *)"",
2131 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 tv.v_type,
2133 s == NULL ? (char_u *)"" : s,
2134 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 *arg = c;
2136 vim_free(tf);
2137 }
2138 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 }
2141 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142
2143 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 }
2148
2149 return arg;
2150}
2151
2152/*
2153 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2154 * Returns a pointer to the char just after the var name.
2155 * Returns NULL if there is an error.
2156 */
2157 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002158ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164{
2165 int c1;
2166 char_u *name;
2167 char_u *p;
2168 char_u *arg_end = NULL;
2169 int len;
2170 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172
2173 /*
2174 * ":let $VAR = expr": Set environment variable.
2175 */
2176 if (*arg == '$')
2177 {
2178 /* Find the end of the name. */
2179 ++arg;
2180 name = arg;
2181 len = get_env_len(&arg);
2182 if (len == 0)
2183 EMSG2(_(e_invarg2), name - 1);
2184 else
2185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 if (op != NULL && (*op == '+' || *op == '-'))
2187 EMSG2(_(e_letwrong), op);
2188 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002189 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 EMSG(_(e_letunexp));
2191 else
2192 {
2193 c1 = name[len];
2194 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002195 p = get_tv_string_chk(tv);
2196 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 {
2198 int mustfree = FALSE;
2199 char_u *s = vim_getenv(name, &mustfree);
2200
2201 if (s != NULL)
2202 {
2203 p = tofree = concat_str(s, p);
2204 if (mustfree)
2205 vim_free(s);
2206 }
2207 }
2208 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 if (STRICMP(name, "HOME") == 0)
2212 init_homedir();
2213 else if (didset_vim && STRICMP(name, "VIM") == 0)
2214 didset_vim = FALSE;
2215 else if (didset_vimruntime
2216 && STRICMP(name, "VIMRUNTIME") == 0)
2217 didset_vimruntime = FALSE;
2218 arg_end = arg;
2219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002221 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
2223 }
2224 }
2225
2226 /*
2227 * ":let &option = expr": Set option value.
2228 * ":let &l:option = expr": Set local option value.
2229 * ":let &g:option = expr": Set global option value.
2230 */
2231 else if (*arg == '&')
2232 {
2233 /* Find the end of the name. */
2234 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002235 if (p == NULL || (endchars != NULL
2236 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 EMSG(_(e_letunexp));
2238 else
2239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 long n;
2241 int opt_type;
2242 long numval;
2243 char_u *stringval = NULL;
2244 char_u *s;
2245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 c1 = *p;
2247 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248
2249 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002250 s = get_tv_string_chk(tv); /* != NULL if number or string */
2251 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002252 {
2253 opt_type = get_option_value(arg, &numval,
2254 &stringval, opt_flags);
2255 if ((opt_type == 1 && *op == '.')
2256 || (opt_type == 0 && *op != '.'))
2257 EMSG2(_(e_letwrong), op);
2258 else
2259 {
2260 if (opt_type == 1) /* number */
2261 {
2262 if (*op == '+')
2263 n = numval + n;
2264 else
2265 n = numval - n;
2266 }
2267 else if (opt_type == 0 && stringval != NULL) /* string */
2268 {
2269 s = concat_str(stringval, s);
2270 vim_free(stringval);
2271 stringval = s;
2272 }
2273 }
2274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 if (s != NULL)
2276 {
2277 set_option_value(arg, n, s, opt_flags);
2278 arg_end = p;
2279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
2284
2285 /*
2286 * ":let @r = expr": Set register contents.
2287 */
2288 else if (*arg == '@')
2289 {
2290 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291 if (op != NULL && (*op == '+' || *op == '-'))
2292 EMSG2(_(e_letwrong), op);
2293 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002294 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 EMSG(_(e_letunexp));
2296 else
2297 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002298 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 char_u *s;
2300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002301 p = get_tv_string_chk(tv);
2302 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002304 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 if (s != NULL)
2306 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002307 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 vim_free(s);
2309 }
2310 }
2311 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002312 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002313 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 arg_end = arg + 1;
2315 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002316 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318 }
2319
2320 /*
2321 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002324 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002328 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2332 EMSG(_(e_letunexp));
2333 else
2334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 arg_end = p;
2337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341
2342 else
2343 EMSG2(_(e_invarg2), arg);
2344
2345 return arg_end;
2346}
2347
2348/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2350 */
2351 static int
2352check_changedtick(arg)
2353 char_u *arg;
2354{
2355 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2356 {
2357 EMSG2(_(e_readonlyvar), arg);
2358 return TRUE;
2359 }
2360 return FALSE;
2361}
2362
2363/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 * Get an lval: variable, Dict item or List item that can be assigned a value
2365 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2366 * "name.key", "name.key[expr]" etc.
2367 * Indexing only works if "name" is an existing List or Dictionary.
2368 * "name" points to the start of the name.
2369 * If "rettv" is not NULL it points to the value to be assigned.
2370 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2371 * wrong; must end in space or cmd separator.
2372 *
2373 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002374 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 */
2377 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002378get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002380 typval_T *rettv;
2381 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 int unlet;
2383 int skip;
2384 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002385 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 char_u *expr_start, *expr_end;
2389 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002390 dictitem_T *v;
2391 typval_T var1;
2392 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002394 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002397 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002400 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401
2402 if (skip)
2403 {
2404 /* When skipping just find the end of the name. */
2405 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002406 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 }
2408
2409 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002410 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (expr_start != NULL)
2412 {
2413 /* Don't expand the name when we already know there is an error. */
2414 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2415 && *p != '[' && *p != '.')
2416 {
2417 EMSG(_(e_trailing));
2418 return NULL;
2419 }
2420
2421 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2422 if (lp->ll_exp_name == NULL)
2423 {
2424 /* Report an invalid expression in braces, unless the
2425 * expression evaluation has been cancelled due to an
2426 * aborting error, an interrupt, or an exception. */
2427 if (!aborting() && !quiet)
2428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002429 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 EMSG2(_(e_invarg2), name);
2431 return NULL;
2432 }
2433 }
2434 lp->ll_name = lp->ll_exp_name;
2435 }
2436 else
2437 lp->ll_name = name;
2438
2439 /* Without [idx] or .key we are done. */
2440 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2441 return p;
2442
2443 cc = *p;
2444 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002445 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (v == NULL && !quiet)
2447 EMSG2(_(e_undefvar), lp->ll_name);
2448 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 if (v == NULL)
2450 return NULL;
2451
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 /*
2453 * Loop until no more [idx] or .key is following.
2454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002455 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2459 && !(lp->ll_tv->v_type == VAR_DICT
2460 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (!quiet)
2463 EMSG(_("E689: Can only index a List or Dictionary"));
2464 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (!quiet)
2469 EMSG(_("E708: [:] must come last"));
2470 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002472
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 len = -1;
2474 if (*p == '.')
2475 {
2476 key = p + 1;
2477 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2478 ;
2479 if (len == 0)
2480 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (!quiet)
2482 EMSG(_(e_emptykey));
2483 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 }
2485 p = key + len;
2486 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002487 else
2488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 if (*p == ':')
2492 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 else
2494 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 empty1 = FALSE;
2496 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 if (get_tv_string_chk(&var1) == NULL)
2499 {
2500 /* not a number or string */
2501 clear_tv(&var1);
2502 return NULL;
2503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505
2506 /* Optionally get the second index [ :expr]. */
2507 if (*p == ':')
2508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 if (!empty1)
2514 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (rettv != NULL && (rettv->v_type != VAR_LIST
2518 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
2521 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (!empty1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 }
2526 p = skipwhite(p + 1);
2527 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 else
2530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 if (!empty1)
2535 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 if (get_tv_string_chk(&var2) == NULL)
2539 {
2540 /* not a number or string */
2541 if (!empty1)
2542 clear_tv(&var1);
2543 clear_tv(&var2);
2544 return NULL;
2545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!quiet)
2555 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (!empty1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562
2563 /* Skip to past ']'. */
2564 ++p;
2565 }
2566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 {
2569 if (len == -1)
2570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002572 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 if (*key == NUL)
2574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (!quiet)
2576 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 }
2580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 lp->ll_list = NULL;
2582 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002583 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 if (len == -1)
2592 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 }
2595 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (len == -1)
2600 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 p = NULL;
2603 break;
2604 }
2605 if (len == -1)
2606 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609 else
2610 {
2611 /*
2612 * Get the number and item for the only or first index of the List.
2613 */
2614 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 else
2617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002618 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 clear_tv(&var1);
2620 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lp->ll_list = lp->ll_tv->vval.v_list;
2623 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2624 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002626 if (lp->ll_n1 < 0)
2627 {
2628 lp->ll_n1 = 0;
2629 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2630 }
2631 }
2632 if (lp->ll_li == NULL)
2633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638
2639 /*
2640 * May need to find the item or absolute index for the second
2641 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 * When no index given: "lp->ll_empty2" is TRUE.
2643 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002647 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2658 if (lp->ll_n1 < 0)
2659 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2660 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 }
2663
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
2667
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return p;
2669}
2670
2671/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 */
2674 static void
2675clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677{
2678 vim_free(lp->ll_exp_name);
2679 vim_free(lp->ll_newkey);
2680}
2681
2682/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002683 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 */
2687 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694{
2695 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002696 listitem_T *ri;
2697 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698
2699 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 cc = *endp;
2704 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 if (op != NULL && *op != '=')
2706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002707 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002709 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002710 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002711 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 {
2713 if (tv_op(&tv, rettv, op) == OK)
2714 set_var(lp->ll_name, &tv, FALSE);
2715 clear_tv(&tv);
2716 }
2717 }
2718 else
2719 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002723 else if (tv_check_lock(lp->ll_newkey == NULL
2724 ? lp->ll_tv->v_lock
2725 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2726 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 else if (lp->ll_range)
2728 {
2729 /*
2730 * Assign the List values to the list items.
2731 */
2732 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 if (op != NULL && *op != '=')
2735 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2736 else
2737 {
2738 clear_tv(&lp->ll_li->li_tv);
2739 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 ri = ri->li_next;
2742 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2743 break;
2744 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002747 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 ri = NULL;
2750 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_li = lp->ll_li->li_next;
2754 ++lp->ll_n1;
2755 }
2756 if (ri != NULL)
2757 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 else if (lp->ll_empty2
2759 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 : lp->ll_n1 != lp->ll_n2)
2761 EMSG(_("E711: List value has not enough items"));
2762 }
2763 else
2764 {
2765 /*
2766 * Assign to a List or Dictionary item.
2767 */
2768 if (lp->ll_newkey != NULL)
2769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 if (op != NULL && *op != '=')
2771 {
2772 EMSG2(_(e_letwrong), op);
2773 return;
2774 }
2775
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002777 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (di == NULL)
2779 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2781 {
2782 vim_free(di);
2783 return;
2784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 else if (op != NULL && *op != '=')
2788 {
2789 tv_op(lp->ll_tv, rettv, op);
2790 return;
2791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002792 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /*
2796 * Assign the value to the variable or list item.
2797 */
2798 if (copy)
2799 copy_tv(rettv, lp->ll_tv);
2800 else
2801 {
2802 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002803 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002805 }
2806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807}
2808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002809/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2811 * Returns OK or FAIL.
2812 */
2813 static int
2814tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 typval_T *tv1;
2816 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 char_u *op;
2818{
2819 long n;
2820 char_u numbuf[NUMBUFLEN];
2821 char_u *s;
2822
2823 /* Can't do anything with a Funcref or a Dict on the right. */
2824 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2825 {
2826 switch (tv1->v_type)
2827 {
2828 case VAR_DICT:
2829 case VAR_FUNC:
2830 break;
2831
2832 case VAR_LIST:
2833 if (*op != '+' || tv2->v_type != VAR_LIST)
2834 break;
2835 /* List += List */
2836 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2837 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2838 return OK;
2839
2840 case VAR_NUMBER:
2841 case VAR_STRING:
2842 if (tv2->v_type == VAR_LIST)
2843 break;
2844 if (*op == '+' || *op == '-')
2845 {
2846 /* nr += nr or nr -= nr*/
2847 n = get_tv_number(tv1);
2848 if (*op == '+')
2849 n += get_tv_number(tv2);
2850 else
2851 n -= get_tv_number(tv2);
2852 clear_tv(tv1);
2853 tv1->v_type = VAR_NUMBER;
2854 tv1->vval.v_number = n;
2855 }
2856 else
2857 {
2858 /* str .= str */
2859 s = get_tv_string(tv1);
2860 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2861 clear_tv(tv1);
2862 tv1->v_type = VAR_STRING;
2863 tv1->vval.v_string = s;
2864 }
2865 return OK;
2866 }
2867 }
2868
2869 EMSG2(_(e_letwrong), op);
2870 return FAIL;
2871}
2872
2873/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874 * Add a watcher to a list.
2875 */
2876 static void
2877list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 list_T *l;
2879 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880{
2881 lw->lw_next = l->lv_watch;
2882 l->lv_watch = lw;
2883}
2884
2885/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002886 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887 * No warning when it isn't found...
2888 */
2889 static void
2890list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 list_T *l;
2892 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
2896 lwp = &l->lv_watch;
2897 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2898 {
2899 if (lw == lwrem)
2900 {
2901 *lwp = lw->lw_next;
2902 break;
2903 }
2904 lwp = &lw->lw_next;
2905 }
2906}
2907
2908/*
2909 * Just before removing an item from a list: advance watchers to the next
2910 * item.
2911 */
2912 static void
2913list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 list_T *l;
2915 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916{
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918
2919 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2920 if (lw->lw_item == item)
2921 lw->lw_item = item->li_next;
2922}
2923
2924/*
2925 * Evaluate the expression used in a ":for var in expr" command.
2926 * "arg" points to "var".
2927 * Set "*errp" to TRUE for an error, FALSE otherwise;
2928 * Return a pointer that holds the info. Null when there is an error.
2929 */
2930 void *
2931eval_for_line(arg, errp, nextcmdp, skip)
2932 char_u *arg;
2933 int *errp;
2934 char_u **nextcmdp;
2935 int skip;
2936{
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002938 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T tv;
2940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941
2942 *errp = TRUE; /* default: there is an error */
2943
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945 if (fi == NULL)
2946 return NULL;
2947
2948 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2949 if (expr == NULL)
2950 return fi;
2951
2952 expr = skipwhite(expr);
2953 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002955 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956 return fi;
2957 }
2958
2959 if (skip)
2960 ++emsg_skip;
2961 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2962 {
2963 *errp = FALSE;
2964 if (!skip)
2965 {
2966 l = tv.vval.v_list;
2967 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002968 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002970 clear_tv(&tv);
2971 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 else
2973 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002974 /* No need to increment the refcount, it's already set for the
2975 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 fi->fi_list = l;
2977 list_add_watch(l, &fi->fi_lw);
2978 fi->fi_lw.lw_item = l->lv_first;
2979 }
2980 }
2981 }
2982 if (skip)
2983 --emsg_skip;
2984
2985 return fi;
2986}
2987
2988/*
2989 * Use the first item in a ":for" list. Advance to the next.
2990 * Assign the values to the variable (list). "arg" points to the first one.
2991 * Return TRUE when a valid item was found, FALSE when at end of list or
2992 * something wrong.
2993 */
2994 int
2995next_for_item(fi_void, arg)
2996 void *fi_void;
2997 char_u *arg;
2998{
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002
3003 item = fi->fi_lw.lw_item;
3004 if (item == NULL)
3005 result = FALSE;
3006 else
3007 {
3008 fi->fi_lw.lw_item = item->li_next;
3009 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3010 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3011 }
3012 return result;
3013}
3014
3015/*
3016 * Free the structure used to store info used by ":for".
3017 */
3018 void
3019free_for_info(fi_void)
3020 void *fi_void;
3021{
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003023
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003024 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003025 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003027 list_unref(fi->fi_list);
3028 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 vim_free(fi);
3030}
3031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3033
3034 void
3035set_context_for_expression(xp, arg, cmdidx)
3036 expand_T *xp;
3037 char_u *arg;
3038 cmdidx_T cmdidx;
3039{
3040 int got_eq = FALSE;
3041 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003042 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 if (cmdidx == CMD_let)
3045 {
3046 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003047 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 {
3049 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051 {
3052 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003053 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054 if (vim_iswhite(*p))
3055 break;
3056 }
3057 return;
3058 }
3059 }
3060 else
3061 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3062 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 while ((xp->xp_pattern = vim_strpbrk(arg,
3064 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3065 {
3066 c = *xp->xp_pattern;
3067 if (c == '&')
3068 {
3069 c = xp->xp_pattern[1];
3070 if (c == '&')
3071 {
3072 ++xp->xp_pattern;
3073 xp->xp_context = cmdidx != CMD_let || got_eq
3074 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3075 }
3076 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003079 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3080 xp->xp_pattern += 2;
3081
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
3084 else if (c == '$')
3085 {
3086 /* environment variable */
3087 xp->xp_context = EXPAND_ENV_VARS;
3088 }
3089 else if (c == '=')
3090 {
3091 got_eq = TRUE;
3092 xp->xp_context = EXPAND_EXPRESSION;
3093 }
3094 else if (c == '<'
3095 && xp->xp_context == EXPAND_FUNCTIONS
3096 && vim_strchr(xp->xp_pattern, '(') == NULL)
3097 {
3098 /* Function name can start with "<SNR>" */
3099 break;
3100 }
3101 else if (cmdidx != CMD_let || got_eq)
3102 {
3103 if (c == '"') /* string */
3104 {
3105 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3106 if (c == '\\' && xp->xp_pattern[1] != NUL)
3107 ++xp->xp_pattern;
3108 xp->xp_context = EXPAND_NOTHING;
3109 }
3110 else if (c == '\'') /* literal string */
3111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3114 /* skip */ ;
3115 xp->xp_context = EXPAND_NOTHING;
3116 }
3117 else if (c == '|')
3118 {
3119 if (xp->xp_pattern[1] == '|')
3120 {
3121 ++xp->xp_pattern;
3122 xp->xp_context = EXPAND_EXPRESSION;
3123 }
3124 else
3125 xp->xp_context = EXPAND_COMMANDS;
3126 }
3127 else
3128 xp->xp_context = EXPAND_EXPRESSION;
3129 }
3130 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 /* Doesn't look like something valid, expand as an expression
3132 * anyway. */
3133 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 arg = xp->xp_pattern;
3135 if (*arg != NUL)
3136 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3137 /* skip */ ;
3138 }
3139 xp->xp_pattern = arg;
3140}
3141
3142#endif /* FEAT_CMDL_COMPL */
3143
3144/*
3145 * ":1,25call func(arg1, arg2)" function call.
3146 */
3147 void
3148ex_call(eap)
3149 exarg_T *eap;
3150{
3151 char_u *arg = eap->arg;
3152 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 linenr_T lnum;
3158 int doesrange;
3159 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003162 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003163 if (fudi.fd_newkey != NULL)
3164 {
3165 /* Still need to give an error message for missing key. */
3166 EMSG2(_(e_dictkey), fudi.fd_newkey);
3167 vim_free(fudi.fd_newkey);
3168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003169 if (tofree == NULL)
3170 return;
3171
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003172 /* Increase refcount on dictionary, it could get deleted when evaluating
3173 * the arguments. */
3174 if (fudi.fd_dict != NULL)
3175 ++fudi.fd_dict->dv_refcount;
3176
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003177 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003178 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar532c7802005-01-27 14:44:31 +00003181 /* Skip white space to allow ":call func ()". Not good, but required for
3182 * backward compatibility. */
3183 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003184 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 if (*startarg != '(')
3187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003188 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 goto end;
3190 }
3191
3192 /*
3193 * When skipping, evaluate the function once, to find the end of the
3194 * arguments.
3195 * When the function takes a range, this is discovered after the first
3196 * call, and the loop is broken.
3197 */
3198 if (eap->skip)
3199 {
3200 ++emsg_skip;
3201 lnum = eap->line2; /* do it once, also with an invalid range */
3202 }
3203 else
3204 lnum = eap->line1;
3205 for ( ; lnum <= eap->line2; ++lnum)
3206 {
3207 if (!eap->skip && eap->addr_count > 0)
3208 {
3209 curwin->w_cursor.lnum = lnum;
3210 curwin->w_cursor.col = 0;
3211 }
3212 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003213 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003214 eap->line1, eap->line2, &doesrange,
3215 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 failed = TRUE;
3218 break;
3219 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003220
3221 /* Handle a function returning a Funcref, Dictionary or List. */
3222 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3223 {
3224 failed = TRUE;
3225 break;
3226 }
3227
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (doesrange || eap->skip)
3230 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003233 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003234 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (aborting())
3237 break;
3238 }
3239 if (eap->skip)
3240 --emsg_skip;
3241
3242 if (!failed)
3243 {
3244 /* Check for trailing illegal characters and a following command. */
3245 if (!ends_excmd(*arg))
3246 {
3247 emsg_severe = TRUE;
3248 EMSG(_(e_trailing));
3249 }
3250 else
3251 eap->nextcmd = check_nextcmd(arg);
3252 }
3253
3254end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003255 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003256 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259/*
3260 * ":unlet[!] var1 ... " command.
3261 */
3262 void
3263ex_unlet(eap)
3264 exarg_T *eap;
3265{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 ex_unletlock(eap, eap->arg, 0);
3267}
3268
3269/*
3270 * ":lockvar" and ":unlockvar" commands
3271 */
3272 void
3273ex_lockvar(eap)
3274 exarg_T *eap;
3275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 int deep = 2;
3278
3279 if (eap->forceit)
3280 deep = -1;
3281 else if (vim_isdigit(*arg))
3282 {
3283 deep = getdigits(&arg);
3284 arg = skipwhite(arg);
3285 }
3286
3287 ex_unletlock(eap, arg, deep);
3288}
3289
3290/*
3291 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3292 */
3293 static void
3294ex_unletlock(eap, argstart, deep)
3295 exarg_T *eap;
3296 char_u *argstart;
3297 int deep;
3298{
3299 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 do
3305 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003307 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3308 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 if (lv.ll_name == NULL)
3310 error = TRUE; /* error but continue parsing */
3311 if (name_end == NULL || (!vim_iswhite(*name_end)
3312 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003314 if (name_end != NULL)
3315 {
3316 emsg_severe = TRUE;
3317 EMSG(_(e_trailing));
3318 }
3319 if (!(eap->skip || error))
3320 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 break;
3322 }
3323
3324 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325 {
3326 if (eap->cmdidx == CMD_unlet)
3327 {
3328 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3329 error = TRUE;
3330 }
3331 else
3332 {
3333 if (do_lock_var(&lv, name_end, deep,
3334 eap->cmdidx == CMD_lockvar) == FAIL)
3335 error = TRUE;
3336 }
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 if (!eap->skip)
3340 clear_lval(&lv);
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 arg = skipwhite(name_end);
3343 } while (!ends_excmd(*arg));
3344
3345 eap->nextcmd = check_nextcmd(arg);
3346}
3347
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003349do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352 int forceit;
3353{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003354 int ret = OK;
3355 int cc;
3356
3357 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003359 cc = *name_end;
3360 *name_end = NUL;
3361
3362 /* Normal name or expanded name. */
3363 if (check_changedtick(lp->ll_name))
3364 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003366 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003368 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3370 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003371 else if (lp->ll_range)
3372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003374
3375 /* Delete a range of List items. */
3376 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3377 {
3378 li = lp->ll_li->li_next;
3379 listitem_remove(lp->ll_list, lp->ll_li);
3380 lp->ll_li = li;
3381 ++lp->ll_n1;
3382 }
3383 }
3384 else
3385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003386 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003387 /* unlet a List item. */
3388 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003391 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 }
3393
3394 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003395}
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
3398 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003399 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 */
3401 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003404 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 hashtab_T *ht;
3407 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003408 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 ht = find_var_ht(name, &varname);
3411 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 hi = hash_find(ht, varname);
3414 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003415 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003416 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3417 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003418 if (var_check_ro(HI2DI(hi)->di_flags, name))
3419 return FAIL;
3420 delete_var(ht, hi);
3421 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003424 if (forceit)
3425 return OK;
3426 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return FAIL;
3428}
3429
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003430/*
3431 * Lock or unlock variable indicated by "lp".
3432 * "deep" is the levels to go (-1 for unlimited);
3433 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3434 */
3435 static int
3436do_lock_var(lp, name_end, deep, lock)
3437 lval_T *lp;
3438 char_u *name_end;
3439 int deep;
3440 int lock;
3441{
3442 int ret = OK;
3443 int cc;
3444 dictitem_T *di;
3445
3446 if (deep == 0) /* nothing to do */
3447 return OK;
3448
3449 if (lp->ll_tv == NULL)
3450 {
3451 cc = *name_end;
3452 *name_end = NUL;
3453
3454 /* Normal name or expanded name. */
3455 if (check_changedtick(lp->ll_name))
3456 ret = FAIL;
3457 else
3458 {
3459 di = find_var(lp->ll_name, NULL);
3460 if (di == NULL)
3461 ret = FAIL;
3462 else
3463 {
3464 if (lock)
3465 di->di_flags |= DI_FLAGS_LOCK;
3466 else
3467 di->di_flags &= ~DI_FLAGS_LOCK;
3468 item_lock(&di->di_tv, deep, lock);
3469 }
3470 }
3471 *name_end = cc;
3472 }
3473 else if (lp->ll_range)
3474 {
3475 listitem_T *li = lp->ll_li;
3476
3477 /* (un)lock a range of List items. */
3478 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3479 {
3480 item_lock(&li->li_tv, deep, lock);
3481 li = li->li_next;
3482 ++lp->ll_n1;
3483 }
3484 }
3485 else if (lp->ll_list != NULL)
3486 /* (un)lock a List item. */
3487 item_lock(&lp->ll_li->li_tv, deep, lock);
3488 else
3489 /* un(lock) a Dictionary item. */
3490 item_lock(&lp->ll_di->di_tv, deep, lock);
3491
3492 return ret;
3493}
3494
3495/*
3496 * Lock or unlock an item. "deep" is nr of levels to go.
3497 */
3498 static void
3499item_lock(tv, deep, lock)
3500 typval_T *tv;
3501 int deep;
3502 int lock;
3503{
3504 static int recurse = 0;
3505 list_T *l;
3506 listitem_T *li;
3507 dict_T *d;
3508 hashitem_T *hi;
3509 int todo;
3510
3511 if (recurse >= DICT_MAXNEST)
3512 {
3513 EMSG(_("E743: variable nested too deep for (un)lock"));
3514 return;
3515 }
3516 if (deep == 0)
3517 return;
3518 ++recurse;
3519
3520 /* lock/unlock the item itself */
3521 if (lock)
3522 tv->v_lock |= VAR_LOCKED;
3523 else
3524 tv->v_lock &= ~VAR_LOCKED;
3525
3526 switch (tv->v_type)
3527 {
3528 case VAR_LIST:
3529 if ((l = tv->vval.v_list) != NULL)
3530 {
3531 if (lock)
3532 l->lv_lock |= VAR_LOCKED;
3533 else
3534 l->lv_lock &= ~VAR_LOCKED;
3535 if (deep < 0 || deep > 1)
3536 /* recursive: lock/unlock the items the List contains */
3537 for (li = l->lv_first; li != NULL; li = li->li_next)
3538 item_lock(&li->li_tv, deep - 1, lock);
3539 }
3540 break;
3541 case VAR_DICT:
3542 if ((d = tv->vval.v_dict) != NULL)
3543 {
3544 if (lock)
3545 d->dv_lock |= VAR_LOCKED;
3546 else
3547 d->dv_lock &= ~VAR_LOCKED;
3548 if (deep < 0 || deep > 1)
3549 {
3550 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3553 {
3554 if (!HASHITEM_EMPTY(hi))
3555 {
3556 --todo;
3557 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3558 }
3559 }
3560 }
3561 }
3562 }
3563 --recurse;
3564}
3565
Bram Moolenaara40058a2005-07-11 22:42:07 +00003566/*
3567 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3568 * it refers to a List or Dictionary that is locked.
3569 */
3570 static int
3571tv_islocked(tv)
3572 typval_T *tv;
3573{
3574 return (tv->v_lock & VAR_LOCKED)
3575 || (tv->v_type == VAR_LIST
3576 && tv->vval.v_list != NULL
3577 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3578 || (tv->v_type == VAR_DICT
3579 && tv->vval.v_dict != NULL
3580 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3581}
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3584/*
3585 * Delete all "menutrans_" variables.
3586 */
3587 void
3588del_menutrans_vars()
3589{
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003591 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003596 {
3597 if (!HASHITEM_EMPTY(hi))
3598 {
3599 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3601 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003602 }
3603 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605}
3606#endif
3607
3608#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3609
3610/*
3611 * Local string buffer for the next two functions to store a variable name
3612 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3613 * get_user_var_name().
3614 */
3615
3616static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3617
3618static char_u *varnamebuf = NULL;
3619static int varnamebuflen = 0;
3620
3621/*
3622 * Function to concatenate a prefix and a variable name.
3623 */
3624 static char_u *
3625cat_prefix_varname(prefix, name)
3626 int prefix;
3627 char_u *name;
3628{
3629 int len;
3630
3631 len = (int)STRLEN(name) + 3;
3632 if (len > varnamebuflen)
3633 {
3634 vim_free(varnamebuf);
3635 len += 10; /* some additional space */
3636 varnamebuf = alloc(len);
3637 if (varnamebuf == NULL)
3638 {
3639 varnamebuflen = 0;
3640 return NULL;
3641 }
3642 varnamebuflen = len;
3643 }
3644 *varnamebuf = prefix;
3645 varnamebuf[1] = ':';
3646 STRCPY(varnamebuf + 2, name);
3647 return varnamebuf;
3648}
3649
3650/*
3651 * Function given to ExpandGeneric() to obtain the list of user defined
3652 * (global/buffer/window/built-in) variable names.
3653 */
3654/*ARGSUSED*/
3655 char_u *
3656get_user_var_name(xp, idx)
3657 expand_T *xp;
3658 int idx;
3659{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003660 static long_u gdone;
3661 static long_u bdone;
3662 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003663#ifdef FEAT_WINDOWS
3664 static long_u tdone;
3665#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003666 static int vidx;
3667 static hashitem_T *hi;
3668 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003671 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003672 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003673#ifdef FEAT_WINDOWS
3674 tdone = 0;
3675#endif
3676 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003677
3678 /* Global variables */
3679 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003683 else
3684 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 while (HASHITEM_EMPTY(hi))
3686 ++hi;
3687 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3688 return cat_prefix_varname('g', hi->hi_key);
3689 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003691
3692 /* b: variables */
3693 ht = &curbuf->b_vars.dv_hashtab;
3694 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003696 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003698 else
3699 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003700 while (HASHITEM_EMPTY(hi))
3701 ++hi;
3702 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003704 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003706 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return (char_u *)"b:changedtick";
3708 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003709
3710 /* w: variables */
3711 ht = &curwin->w_vars.dv_hashtab;
3712 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003714 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003716 else
3717 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003718 while (HASHITEM_EMPTY(hi))
3719 ++hi;
3720 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003722
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003723#ifdef FEAT_WINDOWS
3724 /* t: variables */
3725 ht = &curtab->tp_vars.dv_hashtab;
3726 if (tdone < ht->ht_used)
3727 {
3728 if (tdone++ == 0)
3729 hi = ht->ht_array;
3730 else
3731 ++hi;
3732 while (HASHITEM_EMPTY(hi))
3733 ++hi;
3734 return cat_prefix_varname('t', hi->hi_key);
3735 }
3736#endif
3737
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 /* v: variables */
3739 if (vidx < VV_LEN)
3740 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 vim_free(varnamebuf);
3743 varnamebuf = NULL;
3744 varnamebuflen = 0;
3745 return NULL;
3746}
3747
3748#endif /* FEAT_CMDL_COMPL */
3749
3750/*
3751 * types for expressions.
3752 */
3753typedef enum
3754{
3755 TYPE_UNKNOWN = 0
3756 , TYPE_EQUAL /* == */
3757 , TYPE_NEQUAL /* != */
3758 , TYPE_GREATER /* > */
3759 , TYPE_GEQUAL /* >= */
3760 , TYPE_SMALLER /* < */
3761 , TYPE_SEQUAL /* <= */
3762 , TYPE_MATCH /* =~ */
3763 , TYPE_NOMATCH /* !~ */
3764} exptype_T;
3765
3766/*
3767 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3770 */
3771
3772/*
3773 * Handle zero level expression.
3774 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003776 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * Return OK or FAIL.
3778 */
3779 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 char_u **nextcmd;
3784 int evaluate;
3785{
3786 int ret;
3787 char_u *p;
3788
3789 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (ret == FAIL || !ends_excmd(*p))
3792 {
3793 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 /*
3796 * Report the invalid expression unless the expression evaluation has
3797 * been cancelled due to an aborting error, an interrupt, or an
3798 * exception.
3799 */
3800 if (!aborting())
3801 EMSG2(_(e_invexpr2), arg);
3802 ret = FAIL;
3803 }
3804 if (nextcmd != NULL)
3805 *nextcmd = check_nextcmd(p);
3806
3807 return ret;
3808}
3809
3810/*
3811 * Handle top level expression:
3812 * expr1 ? expr0 : expr0
3813 *
3814 * "arg" must point to the first non-white of the expression.
3815 * "arg" is advanced to the next non-white after the recognized expression.
3816 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003817 * Note: "rettv.v_lock" is not set.
3818 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 * Return OK or FAIL.
3820 */
3821 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 int evaluate;
3826{
3827 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 /*
3831 * Get the first variable.
3832 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return FAIL;
3835
3836 if ((*arg)[0] == '?')
3837 {
3838 result = FALSE;
3839 if (evaluate)
3840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003841 int error = FALSE;
3842
3843 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 if (error)
3847 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849
3850 /*
3851 * Get the second variable.
3852 */
3853 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return FAIL;
3856
3857 /*
3858 * Check for the ":".
3859 */
3860 if ((*arg)[0] != ':')
3861 {
3862 EMSG(_("E109: Missing ':' after '?'"));
3863 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return FAIL;
3866 }
3867
3868 /*
3869 * Get the third variable.
3870 */
3871 *arg = skipwhite(*arg + 1);
3872 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3873 {
3874 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return FAIL;
3877 }
3878 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882 return OK;
3883}
3884
3885/*
3886 * Handle first level expression:
3887 * expr2 || expr2 || expr2 logical OR
3888 *
3889 * "arg" must point to the first non-white of the expression.
3890 * "arg" is advanced to the next non-white after the recognized expression.
3891 *
3892 * Return OK or FAIL.
3893 */
3894 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int evaluate;
3899{
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 long result;
3902 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 /*
3906 * Get the first variable.
3907 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return FAIL;
3910
3911 /*
3912 * Repeat until there is no following "||".
3913 */
3914 first = TRUE;
3915 result = FALSE;
3916 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3917 {
3918 if (evaluate && first)
3919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003920 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 if (error)
3924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 first = FALSE;
3926 }
3927
3928 /*
3929 * Get the second variable.
3930 */
3931 *arg = skipwhite(*arg + 2);
3932 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3933 return FAIL;
3934
3935 /*
3936 * Compute the result.
3937 */
3938 if (evaluate && !result)
3939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (error)
3944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 if (evaluate)
3947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 rettv->v_type = VAR_NUMBER;
3949 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 }
3952
3953 return OK;
3954}
3955
3956/*
3957 * Handle second level expression:
3958 * expr3 && expr3 && expr3 logical AND
3959 *
3960 * "arg" must point to the first non-white of the expression.
3961 * "arg" is advanced to the next non-white after the recognized expression.
3962 *
3963 * Return OK or FAIL.
3964 */
3965 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int evaluate;
3970{
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 long result;
3973 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003974 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 /*
3977 * Get the first variable.
3978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return FAIL;
3981
3982 /*
3983 * Repeat until there is no following "&&".
3984 */
3985 first = TRUE;
3986 result = TRUE;
3987 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3988 {
3989 if (evaluate && first)
3990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (error)
3995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 first = FALSE;
3997 }
3998
3999 /*
4000 * Get the second variable.
4001 */
4002 *arg = skipwhite(*arg + 2);
4003 if (eval4(arg, &var2, evaluate && result) == FAIL)
4004 return FAIL;
4005
4006 /*
4007 * Compute the result.
4008 */
4009 if (evaluate && result)
4010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (error)
4015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 if (evaluate)
4018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 rettv->v_type = VAR_NUMBER;
4020 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 }
4023
4024 return OK;
4025}
4026
4027/*
4028 * Handle third level expression:
4029 * var1 == var2
4030 * var1 =~ var2
4031 * var1 != var2
4032 * var1 !~ var2
4033 * var1 > var2
4034 * var1 >= var2
4035 * var1 < var2
4036 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 * var1 is var2
4038 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 *
4040 * "arg" must point to the first non-white of the expression.
4041 * "arg" is advanced to the next non-white after the recognized expression.
4042 *
4043 * Return OK or FAIL.
4044 */
4045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int evaluate;
4050{
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 char_u *p;
4053 int i;
4054 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 int len = 2;
4057 long n1, n2;
4058 char_u *s1, *s2;
4059 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4060 regmatch_T regmatch;
4061 int ic;
4062 char_u *save_cpo;
4063
4064 /*
4065 * Get the first variable.
4066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return FAIL;
4069
4070 p = *arg;
4071 switch (p[0])
4072 {
4073 case '=': if (p[1] == '=')
4074 type = TYPE_EQUAL;
4075 else if (p[1] == '~')
4076 type = TYPE_MATCH;
4077 break;
4078 case '!': if (p[1] == '=')
4079 type = TYPE_NEQUAL;
4080 else if (p[1] == '~')
4081 type = TYPE_NOMATCH;
4082 break;
4083 case '>': if (p[1] != '=')
4084 {
4085 type = TYPE_GREATER;
4086 len = 1;
4087 }
4088 else
4089 type = TYPE_GEQUAL;
4090 break;
4091 case '<': if (p[1] != '=')
4092 {
4093 type = TYPE_SMALLER;
4094 len = 1;
4095 }
4096 else
4097 type = TYPE_SEQUAL;
4098 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004099 case 'i': if (p[1] == 's')
4100 {
4101 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4102 len = 5;
4103 if (!vim_isIDc(p[len]))
4104 {
4105 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4106 type_is = TRUE;
4107 }
4108 }
4109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111
4112 /*
4113 * If there is a comparitive operator, use it.
4114 */
4115 if (type != TYPE_UNKNOWN)
4116 {
4117 /* extra question mark appended: ignore case */
4118 if (p[len] == '?')
4119 {
4120 ic = TRUE;
4121 ++len;
4122 }
4123 /* extra '#' appended: match case */
4124 else if (p[len] == '#')
4125 {
4126 ic = FALSE;
4127 ++len;
4128 }
4129 /* nothing appened: use 'ignorecase' */
4130 else
4131 ic = p_ic;
4132
4133 /*
4134 * Get the second variable.
4135 */
4136 *arg = skipwhite(p + len);
4137 if (eval5(arg, &var2, evaluate) == FAIL)
4138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141 }
4142
4143 if (evaluate)
4144 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004145 if (type_is && rettv->v_type != var2.v_type)
4146 {
4147 /* For "is" a different type always means FALSE, for "notis"
4148 * it means TRUE. */
4149 n1 = (type == TYPE_NEQUAL);
4150 }
4151 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4152 {
4153 if (type_is)
4154 {
4155 n1 = (rettv->v_type == var2.v_type
4156 && rettv->vval.v_list == var2.vval.v_list);
4157 if (type == TYPE_NEQUAL)
4158 n1 = !n1;
4159 }
4160 else if (rettv->v_type != var2.v_type
4161 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4162 {
4163 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004164 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004165 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 clear_tv(rettv);
4168 clear_tv(&var2);
4169 return FAIL;
4170 }
4171 else
4172 {
4173 /* Compare two Lists for being equal or unequal. */
4174 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4175 if (type == TYPE_NEQUAL)
4176 n1 = !n1;
4177 }
4178 }
4179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004180 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4181 {
4182 if (type_is)
4183 {
4184 n1 = (rettv->v_type == var2.v_type
4185 && rettv->vval.v_dict == var2.vval.v_dict);
4186 if (type == TYPE_NEQUAL)
4187 n1 = !n1;
4188 }
4189 else if (rettv->v_type != var2.v_type
4190 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4191 {
4192 if (rettv->v_type != var2.v_type)
4193 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4194 else
4195 EMSG(_("E736: Invalid operation for Dictionary"));
4196 clear_tv(rettv);
4197 clear_tv(&var2);
4198 return FAIL;
4199 }
4200 else
4201 {
4202 /* Compare two Dictionaries for being equal or unequal. */
4203 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4204 if (type == TYPE_NEQUAL)
4205 n1 = !n1;
4206 }
4207 }
4208
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004209 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4210 {
4211 if (rettv->v_type != var2.v_type
4212 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4213 {
4214 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004215 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004217 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 clear_tv(rettv);
4219 clear_tv(&var2);
4220 return FAIL;
4221 }
4222 else
4223 {
4224 /* Compare two Funcrefs for being equal or unequal. */
4225 if (rettv->vval.v_string == NULL
4226 || var2.vval.v_string == NULL)
4227 n1 = FALSE;
4228 else
4229 n1 = STRCMP(rettv->vval.v_string,
4230 var2.vval.v_string) == 0;
4231 if (type == TYPE_NEQUAL)
4232 n1 = !n1;
4233 }
4234 }
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 /*
4237 * If one of the two variables is a number, compare as a number.
4238 * When using "=~" or "!~", always compare as string.
4239 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 n1 = get_tv_number(rettv);
4244 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 switch (type)
4246 {
4247 case TYPE_EQUAL: n1 = (n1 == n2); break;
4248 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4249 case TYPE_GREATER: n1 = (n1 > n2); break;
4250 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4251 case TYPE_SMALLER: n1 = (n1 < n2); break;
4252 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4253 case TYPE_UNKNOWN:
4254 case TYPE_MATCH:
4255 case TYPE_NOMATCH: break; /* avoid gcc warning */
4256 }
4257 }
4258 else
4259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 s1 = get_tv_string_buf(rettv, buf1);
4261 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4263 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4264 else
4265 i = 0;
4266 n1 = FALSE;
4267 switch (type)
4268 {
4269 case TYPE_EQUAL: n1 = (i == 0); break;
4270 case TYPE_NEQUAL: n1 = (i != 0); break;
4271 case TYPE_GREATER: n1 = (i > 0); break;
4272 case TYPE_GEQUAL: n1 = (i >= 0); break;
4273 case TYPE_SMALLER: n1 = (i < 0); break;
4274 case TYPE_SEQUAL: n1 = (i <= 0); break;
4275
4276 case TYPE_MATCH:
4277 case TYPE_NOMATCH:
4278 /* avoid 'l' flag in 'cpoptions' */
4279 save_cpo = p_cpo;
4280 p_cpo = (char_u *)"";
4281 regmatch.regprog = vim_regcomp(s2,
4282 RE_MAGIC + RE_STRING);
4283 regmatch.rm_ic = ic;
4284 if (regmatch.regprog != NULL)
4285 {
4286 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4287 vim_free(regmatch.regprog);
4288 if (type == TYPE_NOMATCH)
4289 n1 = !n1;
4290 }
4291 p_cpo = save_cpo;
4292 break;
4293
4294 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4295 }
4296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
4298 clear_tv(&var2);
4299 rettv->v_type = VAR_NUMBER;
4300 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 return OK;
4305}
4306
4307/*
4308 * Handle fourth level expression:
4309 * + number addition
4310 * - number subtraction
4311 * . string concatenation
4312 *
4313 * "arg" must point to the first non-white of the expression.
4314 * "arg" is advanced to the next non-white after the recognized expression.
4315 *
4316 * Return OK or FAIL.
4317 */
4318 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 int evaluate;
4323{
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T var2;
4325 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int op;
4327 long n1, n2;
4328 char_u *s1, *s2;
4329 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4330 char_u *p;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 /*
4339 * Repeat computing, until no '+', '-' or '.' is following.
4340 */
4341 for (;;)
4342 {
4343 op = **arg;
4344 if (op != '+' && op != '-' && op != '.')
4345 break;
4346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (op != '+' || rettv->v_type != VAR_LIST)
4348 {
4349 /* For "list + ...", an illegal use of the first operand as
4350 * a number cannot be determined before evaluating the 2nd
4351 * operand: if this is also a list, all is ok.
4352 * For "something . ...", "something - ..." or "non-list + ...",
4353 * we know that the first operand needs to be a string or number
4354 * without evaluating the 2nd operand. So check before to avoid
4355 * side effects after an error. */
4356 if (evaluate && get_tv_string_chk(rettv) == NULL)
4357 {
4358 clear_tv(rettv);
4359 return FAIL;
4360 }
4361 }
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 /*
4364 * Get the second variable.
4365 */
4366 *arg = skipwhite(*arg + 1);
4367 if (eval6(arg, &var2, evaluate) == FAIL)
4368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
4371 }
4372
4373 if (evaluate)
4374 {
4375 /*
4376 * Compute the result.
4377 */
4378 if (op == '.')
4379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4381 s2 = get_tv_string_buf_chk(&var2, buf2);
4382 if (s2 == NULL) /* type error ? */
4383 {
4384 clear_tv(rettv);
4385 clear_tv(&var2);
4386 return FAIL;
4387 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004388 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 clear_tv(rettv);
4390 rettv->v_type = VAR_STRING;
4391 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004393 else if (op == '+' && rettv->v_type == VAR_LIST
4394 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 {
4396 /* concatenate Lists */
4397 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4398 &var3) == FAIL)
4399 {
4400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
4404 clear_tv(rettv);
4405 *rettv = var3;
4406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else
4408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 int error = FALSE;
4410
4411 n1 = get_tv_number_chk(rettv, &error);
4412 if (error)
4413 {
4414 /* This can only happen for "list + non-list".
4415 * For "non-list + ..." or "something - ...", we returned
4416 * before evaluating the 2nd operand. */
4417 clear_tv(rettv);
4418 return FAIL;
4419 }
4420 n2 = get_tv_number_chk(&var2, &error);
4421 if (error)
4422 {
4423 clear_tv(rettv);
4424 clear_tv(&var2);
4425 return FAIL;
4426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (op == '+')
4429 n1 = n1 + n2;
4430 else
4431 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 }
4438 return OK;
4439}
4440
4441/*
4442 * Handle fifth level expression:
4443 * * number multiplication
4444 * / number division
4445 * % number modulo
4446 *
4447 * "arg" must point to the first non-white of the expression.
4448 * "arg" is advanced to the next non-white after the recognized expression.
4449 *
4450 * Return OK or FAIL.
4451 */
4452 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 int evaluate;
4457{
Bram Moolenaar33570922005-01-25 22:26:29 +00004458 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int op;
4460 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004461 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 /*
4464 * Get the first variable.
4465 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004466 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 return FAIL;
4468
4469 /*
4470 * Repeat computing, until no '*', '/' or '%' is following.
4471 */
4472 for (;;)
4473 {
4474 op = **arg;
4475 if (op != '*' && op != '/' && op != '%')
4476 break;
4477
4478 if (evaluate)
4479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 if (error)
4483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else
4486 n1 = 0;
4487
4488 /*
4489 * Get the second variable.
4490 */
4491 *arg = skipwhite(*arg + 1);
4492 if (eval7(arg, &var2, evaluate) == FAIL)
4493 return FAIL;
4494
4495 if (evaluate)
4496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 if (error)
4500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /*
4503 * Compute the result.
4504 */
4505 if (op == '*')
4506 n1 = n1 * n2;
4507 else if (op == '/')
4508 {
4509 if (n2 == 0) /* give an error message? */
4510 n1 = 0x7fffffffL;
4511 else
4512 n1 = n1 / n2;
4513 }
4514 else
4515 {
4516 if (n2 == 0) /* give an error message? */
4517 n1 = 0;
4518 else
4519 n1 = n1 % n2;
4520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 rettv->v_type = VAR_NUMBER;
4522 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 }
4525
4526 return OK;
4527}
4528
4529/*
4530 * Handle sixth level expression:
4531 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004532 * "string" string constant
4533 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 * &option-name option value
4535 * @r register contents
4536 * identifier variable value
4537 * function() function call
4538 * $VAR environment variable
4539 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004540 * [expr, expr] List
4541 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 *
4543 * Also handle:
4544 * ! in front logical NOT
4545 * - in front unary minus
4546 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 * trailing [] subscript in String or List
4548 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 *
4550 * "arg" must point to the first non-white of the expression.
4551 * "arg" is advanced to the next non-white after the recognized expression.
4552 *
4553 * Return OK or FAIL.
4554 */
4555 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004556eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 int evaluate;
4560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 long n;
4562 int len;
4563 char_u *s;
4564 int val;
4565 char_u *start_leader, *end_leader;
4566 int ret = OK;
4567 char_u *alias;
4568
4569 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 /*
4576 * Skip '!' and '-' characters. They are handled later.
4577 */
4578 start_leader = *arg;
4579 while (**arg == '!' || **arg == '-' || **arg == '+')
4580 *arg = skipwhite(*arg + 1);
4581 end_leader = *arg;
4582
4583 switch (**arg)
4584 {
4585 /*
4586 * Number constant.
4587 */
4588 case '0':
4589 case '1':
4590 case '2':
4591 case '3':
4592 case '4':
4593 case '5':
4594 case '6':
4595 case '7':
4596 case '8':
4597 case '9':
4598 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4599 *arg += len;
4600 if (evaluate)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 rettv->v_type = VAR_NUMBER;
4603 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 break;
4606
4607 /*
4608 * String constant: "string".
4609 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 break;
4618
4619 /*
4620 * List: [expr, expr]
4621 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624
4625 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 * Dictionary: {key: val, key: val}
4627 */
4628 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4629 break;
4630
4631 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004632 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004634 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 break;
4636
4637 /*
4638 * Environment variable: $VAR.
4639 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
4642
4643 /*
4644 * Register contents: @r.
4645 */
4646 case '@': ++*arg;
4647 if (evaluate)
4648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004650 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 if (**arg != NUL)
4653 ++*arg;
4654 break;
4655
4656 /*
4657 * nested expression: (expression).
4658 */
4659 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (**arg == ')')
4662 ++*arg;
4663 else if (ret == OK)
4664 {
4665 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 ret = FAIL;
4668 }
4669 break;
4670
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 break;
4673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674
4675 if (ret == NOTDONE)
4676 {
4677 /*
4678 * Must be a variable or function name.
4679 * Can also be a curly-braces kind of name: {expr}.
4680 */
4681 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004682 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 if (alias != NULL)
4684 s = alias;
4685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 ret = FAIL;
4688 else
4689 {
4690 if (**arg == '(') /* recursive! */
4691 {
4692 /* If "s" is the name of a variable of type VAR_FUNC
4693 * use its contents. */
4694 s = deref_func_name(s, &len);
4695
4696 /* Invoke the function. */
4697 ret = get_func_tv(s, len, rettv, arg,
4698 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004699 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 /* Stop the expression evaluation when immediately
4701 * aborting on error, or when an interrupt occurred or
4702 * an exception was thrown but not caught. */
4703 if (aborting())
4704 {
4705 if (ret == OK)
4706 clear_tv(rettv);
4707 ret = FAIL;
4708 }
4709 }
4710 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004712 else
4713 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 }
4715
4716 if (alias != NULL)
4717 vim_free(alias);
4718 }
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 *arg = skipwhite(*arg);
4721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004722 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4723 * expr(expr). */
4724 if (ret == OK)
4725 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4729 */
4730 if (ret == OK && evaluate && end_leader > start_leader)
4731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 int error = FALSE;
4733
4734 val = get_tv_number_chk(rettv, &error);
4735 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 clear_tv(rettv);
4738 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 else
4741 {
4742 while (end_leader > start_leader)
4743 {
4744 --end_leader;
4745 if (*end_leader == '!')
4746 val = !val;
4747 else if (*end_leader == '-')
4748 val = -val;
4749 }
4750 clear_tv(rettv);
4751 rettv->v_type = VAR_NUMBER;
4752 rettv->vval.v_number = val;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755
4756 return ret;
4757}
4758
4759/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004760 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4761 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4763 */
4764 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004765eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770{
4771 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004772 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 long len = -1;
4775 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004781 if (verbose)
4782 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 return FAIL;
4784 }
4785
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 /*
4789 * dict.name
4790 */
4791 key = *arg + 1;
4792 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4793 ;
4794 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 /*
4801 * something[idx]
4802 *
4803 * Get the (first) variable from inside the [].
4804 */
4805 *arg = skipwhite(*arg + 1);
4806 if (**arg == ':')
4807 empty1 = TRUE;
4808 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4809 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4811 {
4812 /* not a number or string */
4813 clear_tv(&var1);
4814 return FAIL;
4815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816
4817 /*
4818 * Get the second variable from inside the [:].
4819 */
4820 if (**arg == ':')
4821 {
4822 range = TRUE;
4823 *arg = skipwhite(*arg + 1);
4824 if (**arg == ']')
4825 empty2 = TRUE;
4826 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 if (!empty1)
4829 clear_tv(&var1);
4830 return FAIL;
4831 }
4832 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4833 {
4834 /* not a number or string */
4835 if (!empty1)
4836 clear_tv(&var1);
4837 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 return FAIL;
4839 }
4840 }
4841
4842 /* Check for the ']'. */
4843 if (**arg != ']')
4844 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004845 if (verbose)
4846 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 clear_tv(&var1);
4848 if (range)
4849 clear_tv(&var2);
4850 return FAIL;
4851 }
4852 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004853 }
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857 n1 = 0;
4858 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 n1 = get_tv_number(&var1);
4861 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 }
4863 if (range)
4864 {
4865 if (empty2)
4866 n2 = -1;
4867 else
4868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 n2 = get_tv_number(&var2);
4870 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 }
4872 }
4873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 {
4876 case VAR_NUMBER:
4877 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 len = (long)STRLEN(s);
4880 if (range)
4881 {
4882 /* The resulting variable is a substring. If the indexes
4883 * are out of range the result is empty. */
4884 if (n1 < 0)
4885 {
4886 n1 = len + n1;
4887 if (n1 < 0)
4888 n1 = 0;
4889 }
4890 if (n2 < 0)
4891 n2 = len + n2;
4892 else if (n2 >= len)
4893 n2 = len;
4894 if (n1 >= len || n2 < 0 || n1 > n2)
4895 s = NULL;
4896 else
4897 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4898 }
4899 else
4900 {
4901 /* The resulting variable is a string of a single
4902 * character. If the index is too big or negative the
4903 * result is empty. */
4904 if (n1 >= len || n1 < 0)
4905 s = NULL;
4906 else
4907 s = vim_strnsave(s + n1, 1);
4908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(rettv);
4910 rettv->v_type = VAR_STRING;
4911 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 break;
4913
4914 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 if (n1 < 0)
4917 n1 = len + n1;
4918 if (!empty1 && (n1 < 0 || n1 >= len))
4919 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004920 /* For a range we allow invalid values and return an empty
4921 * list. A list index out of range is an error. */
4922 if (!range)
4923 {
4924 if (verbose)
4925 EMSGN(_(e_listidx), n1);
4926 return FAIL;
4927 }
4928 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929 }
4930 if (range)
4931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004932 list_T *l;
4933 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934
4935 if (n2 < 0)
4936 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004937 else if (n2 >= len)
4938 n2 = len - 1;
4939 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004940 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 l = list_alloc();
4942 if (l == NULL)
4943 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 n1 <= n2; ++n1)
4946 {
4947 if (list_append_tv(l, &item->li_tv) == FAIL)
4948 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004949 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950 return FAIL;
4951 }
4952 item = item->li_next;
4953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004954 clear_tv(rettv);
4955 rettv->v_type = VAR_LIST;
4956 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004957 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 }
4959 else
4960 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004961 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004962 clear_tv(rettv);
4963 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 }
4965 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966
4967 case VAR_DICT:
4968 if (range)
4969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004970 if (verbose)
4971 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (len == -1)
4973 clear_tv(&var1);
4974 return FAIL;
4975 }
4976 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004977 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978
4979 if (len == -1)
4980 {
4981 key = get_tv_string(&var1);
4982 if (*key == NUL)
4983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004984 if (verbose)
4985 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 clear_tv(&var1);
4987 return FAIL;
4988 }
4989 }
4990
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004991 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004994 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (len == -1)
4996 clear_tv(&var1);
4997 if (item == NULL)
4998 return FAIL;
4999
5000 copy_tv(&item->di_tv, &var1);
5001 clear_tv(rettv);
5002 *rettv = var1;
5003 }
5004 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 }
5006 }
5007
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 return OK;
5009}
5010
5011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 * Get an option value.
5013 * "arg" points to the '&' or '+' before the option name.
5014 * "arg" is advanced to character after the option name.
5015 * Return OK or FAIL.
5016 */
5017 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005020 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 int evaluate;
5022{
5023 char_u *option_end;
5024 long numval;
5025 char_u *stringval;
5026 int opt_type;
5027 int c;
5028 int working = (**arg == '+'); /* has("+option") */
5029 int ret = OK;
5030 int opt_flags;
5031
5032 /*
5033 * Isolate the option name and find its value.
5034 */
5035 option_end = find_option_end(arg, &opt_flags);
5036 if (option_end == NULL)
5037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 EMSG2(_("E112: Option name missing: %s"), *arg);
5040 return FAIL;
5041 }
5042
5043 if (!evaluate)
5044 {
5045 *arg = option_end;
5046 return OK;
5047 }
5048
5049 c = *option_end;
5050 *option_end = NUL;
5051 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 if (opt_type == -3) /* invalid name */
5055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 EMSG2(_("E113: Unknown option: %s"), *arg);
5058 ret = FAIL;
5059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 if (opt_type == -2) /* hidden string option */
5063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_STRING;
5065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 else if (opt_type == -1) /* hidden number option */
5068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_NUMBER;
5070 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 else if (opt_type == 1) /* number option */
5073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else /* string option */
5078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_STRING;
5080 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 }
5083 else if (working && (opt_type == -2 || opt_type == -1))
5084 ret = FAIL;
5085
5086 *option_end = c; /* put back for error messages */
5087 *arg = option_end;
5088
5089 return ret;
5090}
5091
5092/*
5093 * Allocate a variable for a string constant.
5094 * Return OK or FAIL.
5095 */
5096 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 int evaluate;
5101{
5102 char_u *p;
5103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 int extra = 0;
5105
5106 /*
5107 * Find the end of the string, skipping backslashed characters.
5108 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 if (*p == '\\' && p[1] != NUL)
5112 {
5113 ++p;
5114 /* A "\<x>" form occupies at least 4 characters, and produces up
5115 * to 6 characters: reserve space for 2 extra */
5116 if (*p == '<')
5117 extra += 2;
5118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
5121 if (*p != '"')
5122 {
5123 EMSG2(_("E114: Missing quote: %s"), *arg);
5124 return FAIL;
5125 }
5126
5127 /* If only parsing, set *arg and return here */
5128 if (!evaluate)
5129 {
5130 *arg = p + 1;
5131 return OK;
5132 }
5133
5134 /*
5135 * Copy the string into allocated memory, handling backslashed
5136 * characters.
5137 */
5138 name = alloc((unsigned)(p - *arg + extra));
5139 if (name == NULL)
5140 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 rettv->v_type = VAR_STRING;
5142 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 if (*p == '\\')
5147 {
5148 switch (*++p)
5149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 case 'b': *name++ = BS; ++p; break;
5151 case 'e': *name++ = ESC; ++p; break;
5152 case 'f': *name++ = FF; ++p; break;
5153 case 'n': *name++ = NL; ++p; break;
5154 case 'r': *name++ = CAR; ++p; break;
5155 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 case 'X': /* hex: "\x1", "\x12" */
5158 case 'x':
5159 case 'u': /* Unicode: "\u0023" */
5160 case 'U':
5161 if (vim_isxdigit(p[1]))
5162 {
5163 int n, nr;
5164 int c = toupper(*p);
5165
5166 if (c == 'X')
5167 n = 2;
5168 else
5169 n = 4;
5170 nr = 0;
5171 while (--n >= 0 && vim_isxdigit(p[1]))
5172 {
5173 ++p;
5174 nr = (nr << 4) + hex2nr(*p);
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_MBYTE
5178 /* For "\u" store the number according to
5179 * 'encoding'. */
5180 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 else
5183#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /* octal: "\1", "\12", "\123" */
5189 case '0':
5190 case '1':
5191 case '2':
5192 case '3':
5193 case '4':
5194 case '5':
5195 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 case '7': *name = *p++ - '0';
5197 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 *name = (*name << 3) + *p++ - '0';
5200 if (*p >= '0' && *p <= '7')
5201 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205
5206 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (extra != 0)
5209 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
5213 /* FALLTHROUGH */
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 break;
5217 }
5218 }
5219 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 *arg = p + 1;
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return OK;
5227}
5228
5229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 * Return OK or FAIL.
5232 */
5233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 int evaluate;
5238{
5239 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 int reduce = 0;
5242
5243 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 */
5246 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5247 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 break;
5252 ++reduce;
5253 ++p;
5254 }
5255 }
5256
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 return FAIL;
5261 }
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 if (!evaluate)
5265 {
5266 *arg = p + 1;
5267 return OK;
5268 }
5269
5270 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005272 */
5273 str = alloc((unsigned)((p - *arg) - reduce));
5274 if (str == NULL)
5275 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 rettv->v_type = VAR_STRING;
5277 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 break;
5285 ++p;
5286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 *arg = p + 1;
5291
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 return OK;
5293}
5294
5295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 * Allocate a variable for a List and fill it from "*arg".
5297 * Return OK or FAIL.
5298 */
5299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005300get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 int evaluate;
5304{
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 list_T *l = NULL;
5306 typval_T tv;
5307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308
5309 if (evaluate)
5310 {
5311 l = list_alloc();
5312 if (l == NULL)
5313 return FAIL;
5314 }
5315
5316 *arg = skipwhite(*arg + 1);
5317 while (**arg != ']' && **arg != NUL)
5318 {
5319 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5320 goto failret;
5321 if (evaluate)
5322 {
5323 item = listitem_alloc();
5324 if (item != NULL)
5325 {
5326 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005327 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 list_append(l, item);
5329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005330 else
5331 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 }
5333
5334 if (**arg == ']')
5335 break;
5336 if (**arg != ',')
5337 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 goto failret;
5340 }
5341 *arg = skipwhite(*arg + 1);
5342 }
5343
5344 if (**arg != ']')
5345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347failret:
5348 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005349 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
5352
5353 *arg = skipwhite(*arg + 1);
5354 if (evaluate)
5355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 rettv->v_type = VAR_LIST;
5357 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 ++l->lv_refcount;
5359 }
5360
5361 return OK;
5362}
5363
5364/*
5365 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005366 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005368 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369list_alloc()
5370{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371 list_T *l;
5372
5373 l = (list_T *)alloc_clear(sizeof(list_T));
5374 if (l != NULL)
5375 {
5376 /* Prepend the list to the list of lists for garbage collection. */
5377 if (first_list != NULL)
5378 first_list->lv_used_prev = l;
5379 l->lv_used_prev = NULL;
5380 l->lv_used_next = first_list;
5381 first_list = l;
5382 }
5383 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384}
5385
5386/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005387 * Allocate an empty list for a return value.
5388 * Returns OK or FAIL.
5389 */
5390 static int
5391rettv_list_alloc(rettv)
5392 typval_T *rettv;
5393{
5394 list_T *l = list_alloc();
5395
5396 if (l == NULL)
5397 return FAIL;
5398
5399 rettv->vval.v_list = l;
5400 rettv->v_type = VAR_LIST;
5401 ++l->lv_refcount;
5402 return OK;
5403}
5404
5405/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 * Unreference a list: decrement the reference count and free it when it
5407 * becomes zero.
5408 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005409 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005413 if (l != NULL && --l->lv_refcount <= 0)
5414 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415}
5416
5417/*
5418 * Free a list, including all items it points to.
5419 * Ignores the reference count.
5420 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005421 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005422list_free(l, recurse)
5423 list_T *l;
5424 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425{
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428 /* Remove the list from the list of lists for garbage collection. */
5429 if (l->lv_used_prev == NULL)
5430 first_list = l->lv_used_next;
5431 else
5432 l->lv_used_prev->lv_used_next = l->lv_used_next;
5433 if (l->lv_used_next != NULL)
5434 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5435
Bram Moolenaard9fba312005-06-26 22:34:35 +00005436 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 /* Remove the item before deleting it. */
5439 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005440 if (recurse || (item->li_tv.v_type != VAR_LIST
5441 && item->li_tv.v_type != VAR_DICT))
5442 clear_tv(&item->li_tv);
5443 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 vim_free(l);
5446}
5447
5448/*
5449 * Allocate a list item.
5450 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452listitem_alloc()
5453{
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455}
5456
5457/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005458 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 */
5460 static void
5461listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 vim_free(item);
5466}
5467
5468/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005469 * Remove a list item from a List and free it. Also clears the value.
5470 */
5471 static void
5472listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 list_T *l;
5474 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005475{
5476 list_remove(l, item, item);
5477 listitem_free(item);
5478}
5479
5480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 * Get the number of items in a list.
5482 */
5483 static long
5484list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 if (l == NULL)
5488 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005489 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490}
5491
5492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493 * Return TRUE when two lists have exactly the same values.
5494 */
5495 static int
5496list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 list_T *l1;
5498 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005499 int ic; /* ignore case for strings */
5500{
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005502
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005503 if (l1 == l2)
5504 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505 if (list_len(l1) != list_len(l2))
5506 return FALSE;
5507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508 for (item1 = l1->lv_first, item2 = l2->lv_first;
5509 item1 != NULL && item2 != NULL;
5510 item1 = item1->li_next, item2 = item2->li_next)
5511 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5512 return FALSE;
5513 return item1 == NULL && item2 == NULL;
5514}
5515
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005516#if defined(FEAT_PYTHON) || defined(PROTO)
5517/*
5518 * Return the dictitem that an entry in a hashtable points to.
5519 */
5520 dictitem_T *
5521dict_lookup(hi)
5522 hashitem_T *hi;
5523{
5524 return HI2DI(hi);
5525}
5526#endif
5527
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005529 * Return TRUE when two dictionaries have exactly the same key/values.
5530 */
5531 static int
5532dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 dict_T *d1;
5534 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005535 int ic; /* ignore case for strings */
5536{
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 hashitem_T *hi;
5538 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005539 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005540
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005541 if (d1 == d2)
5542 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543 if (dict_len(d1) != dict_len(d2))
5544 return FALSE;
5545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005546 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 if (!HASHITEM_EMPTY(hi))
5550 {
5551 item2 = dict_find(d2, hi->hi_key, -1);
5552 if (item2 == NULL)
5553 return FALSE;
5554 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5555 return FALSE;
5556 --todo;
5557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005558 }
5559 return TRUE;
5560}
5561
5562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005563 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005564 * Compares the items just like "==" would compare them, but strings and
5565 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005566 */
5567 static int
5568tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005569 typval_T *tv1;
5570 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 int ic; /* ignore case */
5572{
5573 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005574 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005575 static int recursive = 0; /* cach recursive loops */
5576 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005577
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005578 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005579 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005580 /* Catch lists and dicts that have an endless loop by limiting
5581 * recursiveness to 1000. We guess they are equal then. */
5582 if (recursive >= 1000)
5583 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005584
5585 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005586 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005587 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005588 ++recursive;
5589 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5590 --recursive;
5591 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005592
5593 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005594 ++recursive;
5595 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5596 --recursive;
5597 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005598
5599 case VAR_FUNC:
5600 return (tv1->vval.v_string != NULL
5601 && tv2->vval.v_string != NULL
5602 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5603
5604 case VAR_NUMBER:
5605 return tv1->vval.v_number == tv2->vval.v_number;
5606
5607 case VAR_STRING:
5608 s1 = get_tv_string_buf(tv1, buf1);
5609 s2 = get_tv_string_buf(tv2, buf2);
5610 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005611 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005612
5613 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 return TRUE;
5615}
5616
5617/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 * Locate item with index "n" in list "l" and return it.
5619 * A negative index is counted from the end; -1 is the last item.
5620 * Returns NULL when "n" is out of range.
5621 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 long n;
5626{
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 long idx;
5629
5630 if (l == NULL)
5631 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632
5633 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635 n = l->lv_len + n;
5636
5637 /* Check for index out of range. */
5638 if (n < 0 || n >= l->lv_len)
5639 return NULL;
5640
5641 /* When there is a cached index may start search from there. */
5642 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005644 if (n < l->lv_idx / 2)
5645 {
5646 /* closest to the start of the list */
5647 item = l->lv_first;
5648 idx = 0;
5649 }
5650 else if (n > (l->lv_idx + l->lv_len) / 2)
5651 {
5652 /* closest to the end of the list */
5653 item = l->lv_last;
5654 idx = l->lv_len - 1;
5655 }
5656 else
5657 {
5658 /* closest to the cached index */
5659 item = l->lv_idx_item;
5660 idx = l->lv_idx;
5661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 }
5663 else
5664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005665 if (n < l->lv_len / 2)
5666 {
5667 /* closest to the start of the list */
5668 item = l->lv_first;
5669 idx = 0;
5670 }
5671 else
5672 {
5673 /* closest to the end of the list */
5674 item = l->lv_last;
5675 idx = l->lv_len - 1;
5676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005678
5679 while (n > idx)
5680 {
5681 /* search forward */
5682 item = item->li_next;
5683 ++idx;
5684 }
5685 while (n < idx)
5686 {
5687 /* search backward */
5688 item = item->li_prev;
5689 --idx;
5690 }
5691
5692 /* cache the used index */
5693 l->lv_idx = idx;
5694 l->lv_idx_item = item;
5695
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 return item;
5697}
5698
5699/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005700 * Get list item "l[idx]" as a number.
5701 */
5702 static long
5703list_find_nr(l, idx, errorp)
5704 list_T *l;
5705 long idx;
5706 int *errorp; /* set to TRUE when something wrong */
5707{
5708 listitem_T *li;
5709
5710 li = list_find(l, idx);
5711 if (li == NULL)
5712 {
5713 if (errorp != NULL)
5714 *errorp = TRUE;
5715 return -1L;
5716 }
5717 return get_tv_number_chk(&li->li_tv, errorp);
5718}
5719
5720/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005721 * Locate "item" list "l" and return its index.
5722 * Returns -1 when "item" is not in the list.
5723 */
5724 static long
5725list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 list_T *l;
5727 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005728{
5729 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005731
5732 if (l == NULL)
5733 return -1;
5734 idx = 0;
5735 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5736 ++idx;
5737 if (li == NULL)
5738 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005739 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005740}
5741
5742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 * Append item "item" to the end of list "l".
5744 */
5745 static void
5746list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l;
5748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749{
5750 if (l->lv_last == NULL)
5751 {
5752 /* empty list */
5753 l->lv_first = item;
5754 l->lv_last = item;
5755 item->li_prev = NULL;
5756 }
5757 else
5758 {
5759 l->lv_last->li_next = item;
5760 item->li_prev = l->lv_last;
5761 l->lv_last = item;
5762 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005763 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 item->li_next = NULL;
5765}
5766
5767/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005769 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 */
5771 static int
5772list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 list_T *l;
5774 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005776 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 copy_tv(tv, &li->li_tv);
5781 list_append(l, li);
5782 return OK;
5783}
5784
5785/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005786 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005787 * Return FAIL when out of memory.
5788 */
5789 int
5790list_append_dict(list, dict)
5791 list_T *list;
5792 dict_T *dict;
5793{
5794 listitem_T *li = listitem_alloc();
5795
5796 if (li == NULL)
5797 return FAIL;
5798 li->li_tv.v_type = VAR_DICT;
5799 li->li_tv.v_lock = 0;
5800 li->li_tv.vval.v_dict = dict;
5801 list_append(list, li);
5802 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 return OK;
5804}
5805
5806/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005807 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005808 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005809 * Returns FAIL when out of memory.
5810 */
5811 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005812list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005813 list_T *l;
5814 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005815 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005816{
5817 listitem_T *li = listitem_alloc();
5818
5819 if (li == NULL)
5820 return FAIL;
5821 list_append(l, li);
5822 li->li_tv.v_type = VAR_STRING;
5823 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005824 if (str == NULL)
5825 li->li_tv.vval.v_string = NULL;
5826 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005827 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005828 return FAIL;
5829 return OK;
5830}
5831
5832/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005833 * Append "n" to list "l".
5834 * Returns FAIL when out of memory.
5835 */
5836 static int
5837list_append_number(l, n)
5838 list_T *l;
5839 varnumber_T n;
5840{
5841 listitem_T *li;
5842
5843 li = listitem_alloc();
5844 if (li == NULL)
5845 return FAIL;
5846 li->li_tv.v_type = VAR_NUMBER;
5847 li->li_tv.v_lock = 0;
5848 li->li_tv.vval.v_number = n;
5849 list_append(l, li);
5850 return OK;
5851}
5852
5853/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005855 * If "item" is NULL append at the end.
5856 * Return FAIL when out of memory.
5857 */
5858 static int
5859list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
5861 typval_T *tv;
5862 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863{
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865
5866 if (ni == NULL)
5867 return FAIL;
5868 copy_tv(tv, &ni->li_tv);
5869 if (item == NULL)
5870 /* Append new item at end of list. */
5871 list_append(l, ni);
5872 else
5873 {
5874 /* Insert new item before existing item. */
5875 ni->li_prev = item->li_prev;
5876 ni->li_next = item;
5877 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005878 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 ++l->lv_idx;
5881 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005883 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 l->lv_idx_item = NULL;
5886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 }
5890 return OK;
5891}
5892
5893/*
5894 * Extend "l1" with "l2".
5895 * If "bef" is NULL append at the end, otherwise insert before this item.
5896 * Returns FAIL when out of memory.
5897 */
5898 static int
5899list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 list_T *l1;
5901 list_T *l2;
5902 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903{
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005905
5906 for (item = l2->lv_first; item != NULL; item = item->li_next)
5907 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5908 return FAIL;
5909 return OK;
5910}
5911
5912/*
5913 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5914 * Return FAIL when out of memory.
5915 */
5916 static int
5917list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 list_T *l1;
5919 list_T *l2;
5920 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923
5924 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005925 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 if (l == NULL)
5927 return FAIL;
5928 tv->v_type = VAR_LIST;
5929 tv->vval.v_list = l;
5930
5931 /* append all items from the second list */
5932 return list_extend(l, l2, NULL);
5933}
5934
5935/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * Returns NULL when out of memory.
5940 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946{
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *copy;
5948 listitem_T *item;
5949 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950
5951 if (orig == NULL)
5952 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953
5954 copy = list_alloc();
5955 if (copy != NULL)
5956 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (copyID != 0)
5958 {
5959 /* Do this before adding the items, because one of the items may
5960 * refer back to this list. */
5961 orig->lv_copyID = copyID;
5962 orig->lv_copylist = copy;
5963 }
5964 for (item = orig->lv_first; item != NULL && !got_int;
5965 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 {
5967 ni = listitem_alloc();
5968 if (ni == NULL)
5969 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005971 {
5972 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5973 {
5974 vim_free(ni);
5975 break;
5976 }
5977 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 list_append(copy, ni);
5981 }
5982 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 if (item != NULL)
5984 {
5985 list_unref(copy);
5986 copy = NULL;
5987 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 }
5989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 return copy;
5991}
5992
5993/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005995 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 list_T *l;
6000 listitem_T *item;
6001 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 /* notify watchers */
6006 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006008 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 list_fix_watch(l, ip);
6010 if (ip == item2)
6011 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013
6014 if (item2->li_next == NULL)
6015 l->lv_last = item->li_prev;
6016 else
6017 item2->li_next->li_prev = item->li_prev;
6018 if (item->li_prev == NULL)
6019 l->lv_first = item2->li_next;
6020 else
6021 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006022 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023}
6024
6025/*
6026 * Return an allocated string with the string representation of a list.
6027 * May return NULL.
6028 */
6029 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
6034 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
6036 if (tv->vval.v_list == NULL)
6037 return NULL;
6038 ga_init2(&ga, (int)sizeof(char), 80);
6039 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006040 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006041 {
6042 vim_free(ga.ga_data);
6043 return NULL;
6044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 ga_append(&ga, ']');
6046 ga_append(&ga, NUL);
6047 return (char_u *)ga.ga_data;
6048}
6049
6050/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006051 * Join list "l" into a string in "*gap", using separator "sep".
6052 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006056list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 char_u *sep;
6060 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062{
6063 int first = TRUE;
6064 char_u *tofree;
6065 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067 char_u *s;
6068
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006069 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 {
6071 if (first)
6072 first = FALSE;
6073 else
6074 ga_concat(gap, sep);
6075
6076 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006077 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006078 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 if (s != NULL)
6081 ga_concat(gap, s);
6082 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006083 if (s == NULL)
6084 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006085 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006086 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087}
6088
6089/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 * Garbage collection for lists and dictionaries.
6091 *
6092 * We use reference counts to be able to free most items right away when they
6093 * are no longer used. But for composite items it's possible that it becomes
6094 * unused while the reference count is > 0: When there is a recursive
6095 * reference. Example:
6096 * :let l = [1, 2, 3]
6097 * :let d = {9: l}
6098 * :let l[1] = d
6099 *
6100 * Since this is quite unusual we handle this with garbage collection: every
6101 * once in a while find out which lists and dicts are not referenced from any
6102 * variable.
6103 *
6104 * Here is a good reference text about garbage collection (refers to Python
6105 * but it applies to all reference-counting mechanisms):
6106 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006107 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108
6109/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 * Do garbage collection for lists and dicts.
6111 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 int
6114garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115{
6116 dict_T *dd;
6117 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006118 int copyID = ++current_copyID;
6119 buf_T *buf;
6120 win_T *wp;
6121 int i;
6122 funccall_T *fc;
6123 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006124#ifdef FEAT_WINDOWS
6125 tabpage_T *tp;
6126#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006127
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006128 /* Only do this once. */
6129 want_garbage_collect = FALSE;
6130 may_garbage_collect = FALSE;
6131
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006132 /*
6133 * 1. Go through all accessible variables and mark all lists and dicts
6134 * with copyID.
6135 */
6136 /* script-local variables */
6137 for (i = 1; i <= ga_scripts.ga_len; ++i)
6138 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6139
6140 /* buffer-local variables */
6141 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6142 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6143
6144 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006145 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006146 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6147
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006148#ifdef FEAT_WINDOWS
6149 /* tabpage-local variables */
6150 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6151 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6152#endif
6153
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006154 /* global variables */
6155 set_ref_in_ht(&globvarht, copyID);
6156
6157 /* function-local variables */
6158 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6159 {
6160 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6161 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6162 }
6163
6164 /*
6165 * 2. Go through the list of dicts and free items without the copyID.
6166 */
6167 for (dd = first_dict; dd != NULL; )
6168 if (dd->dv_copyID != copyID)
6169 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006170 /* Free the Dictionary and ordinary items it contains, but don't
6171 * recurse into Lists and Dictionaries, they will be in the list
6172 * of dicts or list of lists. */
6173 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006174 did_free = TRUE;
6175
6176 /* restart, next dict may also have been freed */
6177 dd = first_dict;
6178 }
6179 else
6180 dd = dd->dv_used_next;
6181
6182 /*
6183 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006184 * But don't free a list that has a watcher (used in a for loop), these
6185 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006186 */
6187 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006188 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006189 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006190 /* Free the List and ordinary items it contains, but don't recurse
6191 * into Lists and Dictionaries, they will be in the list of dicts
6192 * or list of lists. */
6193 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 did_free = TRUE;
6195
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006196 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006197 ll = first_list;
6198 }
6199 else
6200 ll = ll->lv_used_next;
6201
6202 return did_free;
6203}
6204
6205/*
6206 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6207 */
6208 static void
6209set_ref_in_ht(ht, copyID)
6210 hashtab_T *ht;
6211 int copyID;
6212{
6213 int todo;
6214 hashitem_T *hi;
6215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006216 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006217 for (hi = ht->ht_array; todo > 0; ++hi)
6218 if (!HASHITEM_EMPTY(hi))
6219 {
6220 --todo;
6221 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6222 }
6223}
6224
6225/*
6226 * Mark all lists and dicts referenced through list "l" with "copyID".
6227 */
6228 static void
6229set_ref_in_list(l, copyID)
6230 list_T *l;
6231 int copyID;
6232{
6233 listitem_T *li;
6234
6235 for (li = l->lv_first; li != NULL; li = li->li_next)
6236 set_ref_in_item(&li->li_tv, copyID);
6237}
6238
6239/*
6240 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6241 */
6242 static void
6243set_ref_in_item(tv, copyID)
6244 typval_T *tv;
6245 int copyID;
6246{
6247 dict_T *dd;
6248 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006249
6250 switch (tv->v_type)
6251 {
6252 case VAR_DICT:
6253 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006254 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006255 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006256 /* Didn't see this dict yet. */
6257 dd->dv_copyID = copyID;
6258 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006259 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006260 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006261
6262 case VAR_LIST:
6263 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006264 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006265 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006266 /* Didn't see this list yet. */
6267 ll->lv_copyID = copyID;
6268 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006269 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006270 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006271 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273}
6274
6275/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276 * Allocate an empty header for a dictionary.
6277 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006278 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279dict_alloc()
6280{
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006282
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006285 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006286 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006287 if (first_dict != NULL)
6288 first_dict->dv_used_prev = d;
6289 d->dv_used_next = first_dict;
6290 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006291 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006292
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006294 d->dv_lock = 0;
6295 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006296 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006299}
6300
6301/*
6302 * Unreference a Dictionary: decrement the reference count and free it when it
6303 * becomes zero.
6304 */
6305 static void
6306dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006308{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006309 if (d != NULL && --d->dv_refcount <= 0)
6310 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311}
6312
6313/*
6314 * Free a Dictionary, including all items it contains.
6315 * Ignores the reference count.
6316 */
6317 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006318dict_free(d, recurse)
6319 dict_T *d;
6320 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006324 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006325
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006326 /* Remove the dict from the list of dicts for garbage collection. */
6327 if (d->dv_used_prev == NULL)
6328 first_dict = d->dv_used_next;
6329 else
6330 d->dv_used_prev->dv_used_next = d->dv_used_next;
6331 if (d->dv_used_next != NULL)
6332 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6333
6334 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006335 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006336 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006339 if (!HASHITEM_EMPTY(hi))
6340 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006341 /* Remove the item before deleting it, just in case there is
6342 * something recursive causing trouble. */
6343 di = HI2DI(hi);
6344 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006345 if (recurse || (di->di_tv.v_type != VAR_LIST
6346 && di->di_tv.v_type != VAR_DICT))
6347 clear_tv(&di->di_tv);
6348 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006349 --todo;
6350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 vim_free(d);
6354}
6355
6356/*
6357 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006358 * The "key" is copied to the new item.
6359 * Note that the value of the item "di_tv" still needs to be initialized!
6360 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363dictitem_alloc(key)
6364 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365{
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006368 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006372 di->di_flags = 0;
6373 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375}
6376
6377/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 * Make a copy of a Dictionary item.
6379 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383{
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006386 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6387 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388 if (di != NULL)
6389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006390 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006391 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 copy_tv(&org->di_tv, &di->di_tv);
6393 }
6394 return di;
6395}
6396
6397/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398 * Remove item "item" from Dictionary "dict" and free it.
6399 */
6400 static void
6401dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 dict_T *dict;
6403 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404{
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 if (HASHITEM_EMPTY(hi))
6409 EMSG2(_(e_intern2), "dictitem_remove()");
6410 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 dictitem_free(item);
6413}
6414
6415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416 * Free a dict item. Also clears the value.
6417 */
6418 static void
6419dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422 clear_tv(&item->di_tv);
6423 vim_free(item);
6424}
6425
6426/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006427 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6428 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006429 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006430 * Returns NULL when out of memory.
6431 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006433dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437{
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 dict_T *copy;
6439 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006440 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442
6443 if (orig == NULL)
6444 return NULL;
6445
6446 copy = dict_alloc();
6447 if (copy != NULL)
6448 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 if (copyID != 0)
6450 {
6451 orig->dv_copyID = copyID;
6452 orig->dv_copydict = copy;
6453 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006454 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006457 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459 --todo;
6460
6461 di = dictitem_alloc(hi->hi_key);
6462 if (di == NULL)
6463 break;
6464 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 {
6466 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6467 copyID) == FAIL)
6468 {
6469 vim_free(di);
6470 break;
6471 }
6472 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006473 else
6474 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6475 if (dict_add(copy, di) == FAIL)
6476 {
6477 dictitem_free(di);
6478 break;
6479 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006480 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006482
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 if (todo > 0)
6485 {
6486 dict_unref(copy);
6487 copy = NULL;
6488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 }
6490
6491 return copy;
6492}
6493
6494/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 dict_T *d;
6501 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504}
6505
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006507 * Add a number or string entry to dictionary "d".
6508 * When "str" is NULL use number "nr", otherwise use "str".
6509 * Returns FAIL when out of memory and when key already exists.
6510 */
6511 int
6512dict_add_nr_str(d, key, nr, str)
6513 dict_T *d;
6514 char *key;
6515 long nr;
6516 char_u *str;
6517{
6518 dictitem_T *item;
6519
6520 item = dictitem_alloc((char_u *)key);
6521 if (item == NULL)
6522 return FAIL;
6523 item->di_tv.v_lock = 0;
6524 if (str == NULL)
6525 {
6526 item->di_tv.v_type = VAR_NUMBER;
6527 item->di_tv.vval.v_number = nr;
6528 }
6529 else
6530 {
6531 item->di_tv.v_type = VAR_STRING;
6532 item->di_tv.vval.v_string = vim_strsave(str);
6533 }
6534 if (dict_add(d, item) == FAIL)
6535 {
6536 dictitem_free(item);
6537 return FAIL;
6538 }
6539 return OK;
6540}
6541
6542/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 * Get the number of items in a Dictionary.
6544 */
6545 static long
6546dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 if (d == NULL)
6550 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006551 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552}
6553
6554/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006555 * Find item "key[len]" in Dictionary "d".
6556 * If "len" is negative use strlen(key).
6557 * Returns NULL when not found.
6558 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 char_u *key;
6563 int len;
6564{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006565#define AKEYLEN 200
6566 char_u buf[AKEYLEN];
6567 char_u *akey;
6568 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571 if (len < 0)
6572 akey = key;
6573 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006575 tofree = akey = vim_strnsave(key, len);
6576 if (akey == NULL)
6577 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006578 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006579 else
6580 {
6581 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006582 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 akey = buf;
6584 }
6585
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006587 vim_free(tofree);
6588 if (HASHITEM_EMPTY(hi))
6589 return NULL;
6590 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591}
6592
6593/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006594 * Get a string item from a dictionary.
6595 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006596 * Returns NULL if the entry doesn't exist or out of memory.
6597 */
6598 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006599get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006600 dict_T *d;
6601 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006602 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006603{
6604 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006605 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006606
6607 di = dict_find(d, key, -1);
6608 if (di == NULL)
6609 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006610 s = get_tv_string(&di->di_tv);
6611 if (save && s != NULL)
6612 s = vim_strsave(s);
6613 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006614}
6615
6616/*
6617 * Get a number item from a dictionary.
6618 * Returns 0 if the entry doesn't exist or out of memory.
6619 */
6620 long
6621get_dict_number(d, key)
6622 dict_T *d;
6623 char_u *key;
6624{
6625 dictitem_T *di;
6626
6627 di = dict_find(d, key, -1);
6628 if (di == NULL)
6629 return 0;
6630 return get_tv_number(&di->di_tv);
6631}
6632
6633/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634 * Return an allocated string with the string representation of a Dictionary.
6635 * May return NULL.
6636 */
6637 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006638dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641{
6642 garray_T ga;
6643 int first = TRUE;
6644 char_u *tofree;
6645 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006648 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006649 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006650
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006651 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652 return NULL;
6653 ga_init2(&ga, (int)sizeof(char), 80);
6654 ga_append(&ga, '{');
6655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006656 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 --todo;
6662
6663 if (first)
6664 first = FALSE;
6665 else
6666 ga_concat(&ga, (char_u *)", ");
6667
6668 tofree = string_quote(hi->hi_key, FALSE);
6669 if (tofree != NULL)
6670 {
6671 ga_concat(&ga, tofree);
6672 vim_free(tofree);
6673 }
6674 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006675 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006676 if (s != NULL)
6677 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 if (s == NULL)
6680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 if (todo > 0)
6684 {
6685 vim_free(ga.ga_data);
6686 return NULL;
6687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688
6689 ga_append(&ga, '}');
6690 ga_append(&ga, NUL);
6691 return (char_u *)ga.ga_data;
6692}
6693
6694/*
6695 * Allocate a variable for a Dictionary and fill it from "*arg".
6696 * Return OK or FAIL. Returns NOTDONE for {expr}.
6697 */
6698 static int
6699get_dict_tv(arg, rettv, evaluate)
6700 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006701 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006702 int evaluate;
6703{
Bram Moolenaar33570922005-01-25 22:26:29 +00006704 dict_T *d = NULL;
6705 typval_T tvkey;
6706 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006710 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711
6712 /*
6713 * First check if it's not a curly-braces thing: {expr}.
6714 * Must do this without evaluating, otherwise a function may be called
6715 * twice. Unfortunately this means we need to call eval1() twice for the
6716 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006717 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 if (*start != '}')
6720 {
6721 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6722 return FAIL;
6723 if (*start == '}')
6724 return NOTDONE;
6725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726
6727 if (evaluate)
6728 {
6729 d = dict_alloc();
6730 if (d == NULL)
6731 return FAIL;
6732 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006733 tvkey.v_type = VAR_UNKNOWN;
6734 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006735
6736 *arg = skipwhite(*arg + 1);
6737 while (**arg != '}' && **arg != NUL)
6738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006739 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 goto failret;
6741 if (**arg != ':')
6742 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006743 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006744 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 goto failret;
6746 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006747 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006749 key = get_tv_string_buf_chk(&tvkey, buf);
6750 if (key == NULL || *key == NUL)
6751 {
6752 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6753 if (key != NULL)
6754 EMSG(_(e_emptykey));
6755 clear_tv(&tvkey);
6756 goto failret;
6757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006759
6760 *arg = skipwhite(*arg + 1);
6761 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6762 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006763 if (evaluate)
6764 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765 goto failret;
6766 }
6767 if (evaluate)
6768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006769 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 if (item != NULL)
6771 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006772 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 clear_tv(&tv);
6775 goto failret;
6776 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777 item = dictitem_alloc(key);
6778 clear_tv(&tvkey);
6779 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006782 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006783 if (dict_add(d, item) == FAIL)
6784 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 }
6786 }
6787
6788 if (**arg == '}')
6789 break;
6790 if (**arg != ',')
6791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006792 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006793 goto failret;
6794 }
6795 *arg = skipwhite(*arg + 1);
6796 }
6797
6798 if (**arg != '}')
6799 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006800 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006801failret:
6802 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006803 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804 return FAIL;
6805 }
6806
6807 *arg = skipwhite(*arg + 1);
6808 if (evaluate)
6809 {
6810 rettv->v_type = VAR_DICT;
6811 rettv->vval.v_dict = d;
6812 ++d->dv_refcount;
6813 }
6814
6815 return OK;
6816}
6817
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 * Return a string with the string representation of a variable.
6820 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006821 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006824 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 */
6826 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006827echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006828 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006830 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006831 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006833 static int recurse = 0;
6834 char_u *r = NULL;
6835
Bram Moolenaar33570922005-01-25 22:26:29 +00006836 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006838 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 *tofree = NULL;
6840 return NULL;
6841 }
6842 ++recurse;
6843
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844 switch (tv->v_type)
6845 {
6846 case VAR_FUNC:
6847 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006848 r = tv->vval.v_string;
6849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006850
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006852 if (tv->vval.v_list == NULL)
6853 {
6854 *tofree = NULL;
6855 r = NULL;
6856 }
6857 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6858 {
6859 *tofree = NULL;
6860 r = (char_u *)"[...]";
6861 }
6862 else
6863 {
6864 tv->vval.v_list->lv_copyID = copyID;
6865 *tofree = list2string(tv, copyID);
6866 r = *tofree;
6867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006869
Bram Moolenaar8c711452005-01-14 21:53:12 +00006870 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006871 if (tv->vval.v_dict == NULL)
6872 {
6873 *tofree = NULL;
6874 r = NULL;
6875 }
6876 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6877 {
6878 *tofree = NULL;
6879 r = (char_u *)"{...}";
6880 }
6881 else
6882 {
6883 tv->vval.v_dict->dv_copyID = copyID;
6884 *tofree = dict2string(tv, copyID);
6885 r = *tofree;
6886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006889 case VAR_STRING:
6890 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006891 *tofree = NULL;
6892 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006893 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899
6900 --recurse;
6901 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902}
6903
6904/*
6905 * Return a string with the string representation of a variable.
6906 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6907 * "numbuf" is used for a number.
6908 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006909 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006910 */
6911 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006912tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006914 char_u **tofree;
6915 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006916 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006917{
6918 switch (tv->v_type)
6919 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920 case VAR_FUNC:
6921 *tofree = string_quote(tv->vval.v_string, TRUE);
6922 return *tofree;
6923 case VAR_STRING:
6924 *tofree = string_quote(tv->vval.v_string, FALSE);
6925 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006927 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006928 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006930 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006931 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006932 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006933 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006934}
6935
6936/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 * Return string "str" in ' quotes, doubling ' characters.
6938 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006940 */
6941 static char_u *
6942string_quote(str, function)
6943 char_u *str;
6944 int function;
6945{
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006947 char_u *p, *r, *s;
6948
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 len = (function ? 13 : 3);
6950 if (str != NULL)
6951 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006952 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 for (p = str; *p != NUL; mb_ptr_adv(p))
6954 if (*p == '\'')
6955 ++len;
6956 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006957 s = r = alloc(len);
6958 if (r != NULL)
6959 {
6960 if (function)
6961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006963 r += 10;
6964 }
6965 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 if (str != NULL)
6968 for (p = str; *p != NUL; )
6969 {
6970 if (*p == '\'')
6971 *r++ = '\'';
6972 MB_COPY_CHAR(p, r);
6973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006975 if (function)
6976 *r++ = ')';
6977 *r++ = NUL;
6978 }
6979 return s;
6980}
6981
6982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 * Get the value of an environment variable.
6984 * "arg" is pointing to the '$'. It is advanced to after the name.
6985 * If the environment variable was not set, silently assume it is empty.
6986 * Always return OK.
6987 */
6988 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 int evaluate;
6993{
6994 char_u *string = NULL;
6995 int len;
6996 int cc;
6997 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006998 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 ++*arg;
7001 name = *arg;
7002 len = get_env_len(arg);
7003 if (evaluate)
7004 {
7005 if (len != 0)
7006 {
7007 cc = name[len];
7008 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007009 /* first try vim_getenv(), fast for normal environment vars */
7010 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007012 {
7013 if (!mustfree)
7014 string = vim_strsave(string);
7015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 else
7017 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007018 if (mustfree)
7019 vim_free(string);
7020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 /* next try expanding things like $VIM and ${HOME} */
7022 string = expand_env_save(name - 1);
7023 if (string != NULL && *string == '$')
7024 {
7025 vim_free(string);
7026 string = NULL;
7027 }
7028 }
7029 name[len] = cc;
7030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007031 rettv->v_type = VAR_STRING;
7032 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034
7035 return OK;
7036}
7037
7038/*
7039 * Array with names and number of arguments of all internal functions
7040 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7041 */
7042static struct fst
7043{
7044 char *f_name; /* function name */
7045 char f_min_argc; /* minimal number of arguments */
7046 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007048 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049} functions[] =
7050{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007051 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"append", 2, 2, f_append},
7053 {"argc", 0, 0, f_argc},
7054 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007055 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007057 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"bufexists", 1, 1, f_bufexists},
7059 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7060 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7061 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7062 {"buflisted", 1, 1, f_buflisted},
7063 {"bufloaded", 1, 1, f_bufloaded},
7064 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007065 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {"bufwinnr", 1, 1, f_bufwinnr},
7067 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007068 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007070 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"char2nr", 1, 1, f_char2nr},
7072 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007073 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007075#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007076 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007077 {"complete_add", 1, 1, f_complete_add},
7078 {"complete_check", 0, 0, f_complete_check},
7079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007081 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007082 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007084 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007085 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"delete", 1, 1, f_delete},
7087 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007088 {"diff_filler", 1, 1, f_diff_filler},
7089 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007090 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007092 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {"eventhandler", 0, 0, f_eventhandler},
7094 {"executable", 1, 1, f_executable},
7095 {"exists", 1, 1, f_exists},
7096 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007097 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007098 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7100 {"filereadable", 1, 1, f_filereadable},
7101 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007102 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007103 {"finddir", 1, 3, f_finddir},
7104 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"fnamemodify", 2, 2, f_fnamemodify},
7106 {"foldclosed", 1, 1, f_foldclosed},
7107 {"foldclosedend", 1, 1, f_foldclosedend},
7108 {"foldlevel", 1, 1, f_foldlevel},
7109 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007110 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007114 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007115 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"getbufvar", 2, 2, f_getbufvar},
7117 {"getchar", 0, 1, f_getchar},
7118 {"getcharmod", 0, 0, f_getcharmod},
7119 {"getcmdline", 0, 0, f_getcmdline},
7120 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007123 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007124 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"getfsize", 1, 1, f_getfsize},
7126 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007127 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007128 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007129 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007130 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007131 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007132 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007133 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007135 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {"getwinposx", 0, 0, f_getwinposx},
7137 {"getwinposy", 0, 0, f_getwinposy},
7138 {"getwinvar", 2, 2, f_getwinvar},
7139 {"glob", 1, 1, f_glob},
7140 {"globpath", 2, 2, f_globpath},
7141 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007143 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007144 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7146 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7147 {"histadd", 2, 2, f_histadd},
7148 {"histdel", 1, 2, f_histdel},
7149 {"histget", 1, 2, f_histget},
7150 {"histnr", 1, 1, f_histnr},
7151 {"hlID", 1, 1, f_hlID},
7152 {"hlexists", 1, 1, f_hlexists},
7153 {"hostname", 0, 0, f_hostname},
7154 {"iconv", 3, 3, f_iconv},
7155 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007156 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007157 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007159 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"inputrestore", 0, 0, f_inputrestore},
7161 {"inputsave", 0, 0, f_inputsave},
7162 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007165 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007167 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"libcall", 3, 3, f_libcall},
7172 {"libcallnr", 3, 3, f_libcallnr},
7173 {"line", 1, 1, f_line},
7174 {"line2byte", 1, 1, f_line2byte},
7175 {"lispindent", 1, 1, f_lispindent},
7176 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007177 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007178 {"maparg", 1, 3, f_maparg},
7179 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007180 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007181 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007182 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007183 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007184 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007185 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007186 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007187 {"max", 1, 1, f_max},
7188 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007189#ifdef vim_mkdir
7190 {"mkdir", 1, 3, f_mkdir},
7191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {"mode", 0, 0, f_mode},
7193 {"nextnonblank", 1, 1, f_nextnonblank},
7194 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007195 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007197 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007200 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007201 {"reltime", 0, 2, f_reltime},
7202 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {"remote_expr", 2, 3, f_remote_expr},
7204 {"remote_foreground", 1, 1, f_remote_foreground},
7205 {"remote_peek", 1, 2, f_remote_peek},
7206 {"remote_read", 1, 1, f_remote_read},
7207 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007208 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007210 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007212 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007213 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007214 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007215 {"searchpair", 3, 6, f_searchpair},
7216 {"searchpairpos", 3, 6, f_searchpairpos},
7217 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {"server2client", 2, 2, f_server2client},
7219 {"serverlist", 0, 0, f_serverlist},
7220 {"setbufvar", 3, 3, f_setbufvar},
7221 {"setcmdpos", 1, 1, f_setcmdpos},
7222 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007223 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007224 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007225 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007226 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007228 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007230 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007232 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007233 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007234 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007235 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007236 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007237 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#ifdef HAVE_STRFTIME
7239 {"strftime", 1, 2, f_strftime},
7240#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007242 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {"strlen", 1, 1, f_strlen},
7244 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007245 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {"strtrans", 1, 1, f_strtrans},
7247 {"submatch", 1, 1, f_submatch},
7248 {"substitute", 4, 4, f_substitute},
7249 {"synID", 3, 3, f_synID},
7250 {"synIDattr", 2, 3, f_synIDattr},
7251 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007252 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007253 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007254 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007256 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007257 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007259 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {"tolower", 1, 1, f_tolower},
7261 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007262 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"virtcol", 1, 1, f_virtcol},
7266 {"visualmode", 0, 1, f_visualmode},
7267 {"winbufnr", 1, 1, f_winbufnr},
7268 {"wincol", 0, 0, f_wincol},
7269 {"winheight", 1, 1, f_winheight},
7270 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007271 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007273 {"winrestview", 1, 1, f_winrestview},
7274 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007276 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277};
7278
7279#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7280
7281/*
7282 * Function given to ExpandGeneric() to obtain the list of internal
7283 * or user defined function names.
7284 */
7285 char_u *
7286get_function_name(xp, idx)
7287 expand_T *xp;
7288 int idx;
7289{
7290 static int intidx = -1;
7291 char_u *name;
7292
7293 if (idx == 0)
7294 intidx = -1;
7295 if (intidx < 0)
7296 {
7297 name = get_user_func_name(xp, idx);
7298 if (name != NULL)
7299 return name;
7300 }
7301 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7302 {
7303 STRCPY(IObuff, functions[intidx].f_name);
7304 STRCAT(IObuff, "(");
7305 if (functions[intidx].f_max_argc == 0)
7306 STRCAT(IObuff, ")");
7307 return IObuff;
7308 }
7309
7310 return NULL;
7311}
7312
7313/*
7314 * Function given to ExpandGeneric() to obtain the list of internal or
7315 * user defined variable or function names.
7316 */
7317/*ARGSUSED*/
7318 char_u *
7319get_expr_name(xp, idx)
7320 expand_T *xp;
7321 int idx;
7322{
7323 static int intidx = -1;
7324 char_u *name;
7325
7326 if (idx == 0)
7327 intidx = -1;
7328 if (intidx < 0)
7329 {
7330 name = get_function_name(xp, idx);
7331 if (name != NULL)
7332 return name;
7333 }
7334 return get_user_var_name(xp, ++intidx);
7335}
7336
7337#endif /* FEAT_CMDL_COMPL */
7338
7339/*
7340 * Find internal function in table above.
7341 * Return index, or -1 if not found
7342 */
7343 static int
7344find_internal_func(name)
7345 char_u *name; /* name of the function */
7346{
7347 int first = 0;
7348 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7349 int cmp;
7350 int x;
7351
7352 /*
7353 * Find the function name in the table. Binary search.
7354 */
7355 while (first <= last)
7356 {
7357 x = first + ((unsigned)(last - first) >> 1);
7358 cmp = STRCMP(name, functions[x].f_name);
7359 if (cmp < 0)
7360 last = x - 1;
7361 else if (cmp > 0)
7362 first = x + 1;
7363 else
7364 return x;
7365 }
7366 return -1;
7367}
7368
7369/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7371 * name it contains, otherwise return "name".
7372 */
7373 static char_u *
7374deref_func_name(name, lenp)
7375 char_u *name;
7376 int *lenp;
7377{
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007379 int cc;
7380
7381 cc = name[*lenp];
7382 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007383 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 {
7389 *lenp = 0;
7390 return (char_u *)""; /* just in case */
7391 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007392 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394 }
7395
7396 return name;
7397}
7398
7399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 * Allocate a variable for the result of a function.
7401 * Return OK or FAIL.
7402 */
7403 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7405 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 char_u *name; /* name of the function */
7407 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 char_u **arg; /* argument, pointing to the '(' */
7410 linenr_T firstline; /* first line of range */
7411 linenr_T lastline; /* last line of range */
7412 int *doesrange; /* return: function handled range */
7413 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 char_u *argp;
7417 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007418 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int argcount = 0; /* number of arguments found */
7420
7421 /*
7422 * Get the arguments.
7423 */
7424 argp = *arg;
7425 while (argcount < MAX_FUNC_ARGS)
7426 {
7427 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7428 if (*argp == ')' || *argp == ',' || *argp == NUL)
7429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7431 {
7432 ret = FAIL;
7433 break;
7434 }
7435 ++argcount;
7436 if (*argp != ',')
7437 break;
7438 }
7439 if (*argp == ')')
7440 ++argp;
7441 else
7442 ret = FAIL;
7443
7444 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007446 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 {
7449 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454
7455 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007456 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 *arg = skipwhite(argp);
7459 return ret;
7460}
7461
7462
7463/*
7464 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007465 * Return OK when the function can't be called, FAIL otherwise.
7466 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 */
7468 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 char_u *name; /* name of the function */
7472 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007475 typval_T *argvars; /* vars for arguments, must have "argcount"
7476 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 linenr_T firstline; /* first line of range */
7478 linenr_T lastline; /* last line of range */
7479 int *doesrange; /* return: function handled range */
7480 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#define ERROR_UNKNOWN 0
7485#define ERROR_TOOMANY 1
7486#define ERROR_TOOFEW 2
7487#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488#define ERROR_DICT 4
7489#define ERROR_NONE 5
7490#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 int error = ERROR_NONE;
7492 int i;
7493 int llen;
7494 ufunc_T *fp;
7495 int cc;
7496#define FLEN_FIXED 40
7497 char_u fname_buf[FLEN_FIXED + 1];
7498 char_u *fname;
7499
7500 /*
7501 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7502 * Change <SNR>123_name() to K_SNR 123_name().
7503 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7504 */
7505 cc = name[len];
7506 name[len] = NUL;
7507 llen = eval_fname_script(name);
7508 if (llen > 0)
7509 {
7510 fname_buf[0] = K_SPECIAL;
7511 fname_buf[1] = KS_EXTRA;
7512 fname_buf[2] = (int)KE_SNR;
7513 i = 3;
7514 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7515 {
7516 if (current_SID <= 0)
7517 error = ERROR_SCRIPT;
7518 else
7519 {
7520 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7521 i = (int)STRLEN(fname_buf);
7522 }
7523 }
7524 if (i + STRLEN(name + llen) < FLEN_FIXED)
7525 {
7526 STRCPY(fname_buf + i, name + llen);
7527 fname = fname_buf;
7528 }
7529 else
7530 {
7531 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7532 if (fname == NULL)
7533 error = ERROR_OTHER;
7534 else
7535 {
7536 mch_memmove(fname, fname_buf, (size_t)i);
7537 STRCPY(fname + i, name + llen);
7538 }
7539 }
7540 }
7541 else
7542 fname = name;
7543
7544 *doesrange = FALSE;
7545
7546
7547 /* execute the function if no errors detected and executing */
7548 if (evaluate && error == ERROR_NONE)
7549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 error = ERROR_UNKNOWN;
7552
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007553 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
7555 /*
7556 * User defined function.
7557 */
7558 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007559
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007561 /* Trigger FuncUndefined event, may load the function. */
7562 if (fp == NULL
7563 && apply_autocmds(EVENT_FUNCUNDEFINED,
7564 fname, fname, TRUE, NULL)
7565 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007567 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 fp = find_func(fname);
7569 }
7570#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007571 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007572 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007573 {
7574 /* loaded a package, search for the function again */
7575 fp = find_func(fname);
7576 }
7577
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (fp != NULL)
7579 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007580 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 {
7590 /*
7591 * Call the user function.
7592 * Save and restore search patterns, script variables and
7593 * redo buffer.
7594 */
7595 save_search_patterns();
7596 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007597 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007600 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7601 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7602 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007603 /* Function was unreferenced while being used, free it
7604 * now. */
7605 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 restoreRedobuff();
7607 restore_search_patterns();
7608 error = ERROR_NONE;
7609 }
7610 }
7611 }
7612 else
7613 {
7614 /*
7615 * Find the function name in the table, call its implementation.
7616 */
7617 i = find_internal_func(fname);
7618 if (i >= 0)
7619 {
7620 if (argcount < functions[i].f_min_argc)
7621 error = ERROR_TOOFEW;
7622 else if (argcount > functions[i].f_max_argc)
7623 error = ERROR_TOOMANY;
7624 else
7625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 error = ERROR_NONE;
7629 }
7630 }
7631 }
7632 /*
7633 * The function call (or "FuncUndefined" autocommand sequence) might
7634 * have been aborted by an error, an interrupt, or an explicitly thrown
7635 * exception that has not been caught so far. This situation can be
7636 * tested for by calling aborting(). For an error in an internal
7637 * function or for the "E132" error in call_user_func(), however, the
7638 * throw point at which the "force_abort" flag (temporarily reset by
7639 * emsg()) is normally updated has not been reached yet. We need to
7640 * update that flag first to make aborting() reliable.
7641 */
7642 update_force_abort();
7643 }
7644 if (error == ERROR_NONE)
7645 ret = OK;
7646
7647 /*
7648 * Report an error unless the argument evaluation or function call has been
7649 * cancelled due to an aborting error, an interrupt, or an exception.
7650 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 if (!aborting())
7652 {
7653 switch (error)
7654 {
7655 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007656 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 break;
7658 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 break;
7661 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007662 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 name);
7664 break;
7665 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007666 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 name);
7668 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007670 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 name);
7672 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 }
7674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
7676 name[len] = cc;
7677 if (fname != name && fname != fname_buf)
7678 vim_free(fname);
7679
7680 return ret;
7681}
7682
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007683/*
7684 * Give an error message with a function name. Handle <SNR> things.
7685 */
7686 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007687emsg_funcname(ermsg, name)
7688 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007689 char_u *name;
7690{
7691 char_u *p;
7692
7693 if (*name == K_SPECIAL)
7694 p = concat_str((char_u *)"<SNR>", name + 3);
7695 else
7696 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007697 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007698 if (p != name)
7699 vim_free(p);
7700}
7701
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702/*********************************************
7703 * Implementation of the built-in functions
7704 */
7705
7706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007707 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 */
7709 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007710f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 typval_T *argvars;
7712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007719 if ((l = argvars[0].vval.v_list) != NULL
7720 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7721 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723 }
7724 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007725 EMSG(_(e_listreq));
7726}
7727
7728/*
7729 * "append(lnum, string/list)" function
7730 */
7731 static void
7732f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 typval_T *argvars;
7734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007735{
7736 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007737 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 list_T *l = NULL;
7739 listitem_T *li = NULL;
7740 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007741 long added = 0;
7742
Bram Moolenaar0d660222005-01-07 21:51:51 +00007743 lnum = get_tv_lnum(argvars);
7744 if (lnum >= 0
7745 && lnum <= curbuf->b_ml.ml_line_count
7746 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 l = argvars[1].vval.v_list;
7751 if (l == NULL)
7752 return;
7753 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007756 for (;;)
7757 {
7758 if (l == NULL)
7759 tv = &argvars[1]; /* append a string */
7760 else if (li == NULL)
7761 break; /* end of list */
7762 else
7763 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007764 line = get_tv_string_chk(tv);
7765 if (line == NULL) /* type error */
7766 {
7767 rettv->vval.v_number = 1; /* Failed */
7768 break;
7769 }
7770 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007771 ++added;
7772 if (l == NULL)
7773 break;
7774 li = li->li_next;
7775 }
7776
7777 appended_lines_mark(lnum, added);
7778 if (curwin->w_cursor.lnum > lnum)
7779 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007781 else
7782 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785/*
7786 * "argc()" function
7787 */
7788/* ARGSUSED */
7789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007790f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 typval_T *argvars;
7792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796
7797/*
7798 * "argidx()" function
7799 */
7800/* ARGSUSED */
7801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807}
7808
7809/*
7810 * "argv(nr)" function
7811 */
7812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 typval_T *argvars;
7815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 int idx;
7818
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007819 if (argvars[0].v_type != VAR_UNKNOWN)
7820 {
7821 idx = get_tv_number_chk(&argvars[0], NULL);
7822 if (idx >= 0 && idx < ARGCOUNT)
7823 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7824 else
7825 rettv->vval.v_string = NULL;
7826 rettv->v_type = VAR_STRING;
7827 }
7828 else if (rettv_list_alloc(rettv) == OK)
7829 for (idx = 0; idx < ARGCOUNT; ++idx)
7830 list_append_string(rettv->vval.v_list,
7831 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832}
7833
7834/*
7835 * "browse(save, title, initdir, default)" function
7836 */
7837/* ARGSUSED */
7838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 typval_T *argvars;
7841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843#ifdef FEAT_BROWSE
7844 int save;
7845 char_u *title;
7846 char_u *initdir;
7847 char_u *defname;
7848 char_u buf[NUMBUFLEN];
7849 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 save = get_tv_number_chk(&argvars[0], &error);
7853 title = get_tv_string_chk(&argvars[1]);
7854 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7855 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 if (error || title == NULL || initdir == NULL || defname == NULL)
7858 rettv->vval.v_string = NULL;
7859 else
7860 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007861 do_browse(save ? BROWSE_SAVE : 0,
7862 title, defname, NULL, initdir, NULL, curbuf);
7863#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007867}
7868
7869/*
7870 * "browsedir(title, initdir)" function
7871 */
7872/* ARGSUSED */
7873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 typval_T *argvars;
7876 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007877{
7878#ifdef FEAT_BROWSE
7879 char_u *title;
7880 char_u *initdir;
7881 char_u buf[NUMBUFLEN];
7882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007883 title = get_tv_string_chk(&argvars[0]);
7884 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007886 if (title == NULL || initdir == NULL)
7887 rettv->vval.v_string = NULL;
7888 else
7889 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007890 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895}
7896
Bram Moolenaar33570922005-01-25 22:26:29 +00007897static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007898
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899/*
7900 * Find a buffer by number or exact name.
7901 */
7902 static buf_T *
7903find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 if (avar->v_type == VAR_NUMBER)
7909 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007910 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007913 if (buf == NULL)
7914 {
7915 /* No full path name match, try a match with a URL or a "nofile"
7916 * buffer, these don't use the full path. */
7917 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7918 if (buf->b_fname != NULL
7919 && (path_with_url(buf->b_fname)
7920#ifdef FEAT_QUICKFIX
7921 || bt_nofile(buf)
7922#endif
7923 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007925 break;
7926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928 return buf;
7929}
7930
7931/*
7932 * "bufexists(expr)" function
7933 */
7934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 typval_T *argvars;
7937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940}
7941
7942/*
7943 * "buflisted(expr)" function
7944 */
7945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 typval_T *argvars;
7948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 buf_T *buf;
7951
7952 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954}
7955
7956/*
7957 * "bufloaded(expr)" function
7958 */
7959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 typval_T *argvars;
7962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 buf_T *buf;
7965
7966 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
Bram Moolenaar33570922005-01-25 22:26:29 +00007970static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007971
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972/*
7973 * Get buffer by number or pattern.
7974 */
7975 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 int save_magic;
7981 char_u *save_cpo;
7982 buf_T *buf;
7983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 if (tv->v_type == VAR_NUMBER)
7985 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007986 if (tv->v_type != VAR_STRING)
7987 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (name == NULL || *name == NUL)
7989 return curbuf;
7990 if (name[0] == '$' && name[1] == NUL)
7991 return lastbuf;
7992
7993 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7994 save_magic = p_magic;
7995 p_magic = TRUE;
7996 save_cpo = p_cpo;
7997 p_cpo = (char_u *)"";
7998
7999 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8000 TRUE, FALSE));
8001
8002 p_magic = save_magic;
8003 p_cpo = save_cpo;
8004
8005 /* If not found, try expanding the name, like done for bufexists(). */
8006 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008
8009 return buf;
8010}
8011
8012/*
8013 * "bufname(expr)" function
8014 */
8015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016f_bufname(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 buf_T *buf;
8021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 buf = get_buf_tv(&argvars[0]);
8025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 --emsg_off;
8031}
8032
8033/*
8034 * "bufnr(expr)" function
8035 */
8036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 typval_T *argvars;
8039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008042 int error = FALSE;
8043 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008045 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008048 --emsg_off;
8049
8050 /* If the buffer isn't found and the second argument is not zero create a
8051 * new buffer. */
8052 if (buf == NULL
8053 && argvars[1].v_type != VAR_UNKNOWN
8054 && get_tv_number_chk(&argvars[1], &error) != 0
8055 && !error
8056 && (name = get_tv_string_chk(&argvars[0])) != NULL
8057 && !error)
8058 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8059
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064}
8065
8066/*
8067 * "bufwinnr(nr)" function
8068 */
8069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 typval_T *argvars;
8072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074#ifdef FEAT_WINDOWS
8075 win_T *wp;
8076 int winnr = 0;
8077#endif
8078 buf_T *buf;
8079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083#ifdef FEAT_WINDOWS
8084 for (wp = firstwin; wp; wp = wp->w_next)
8085 {
8086 ++winnr;
8087 if (wp->w_buffer == buf)
8088 break;
8089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#endif
8094 --emsg_off;
8095}
8096
8097/*
8098 * "byte2line(byte)" function
8099 */
8100/*ARGSUSED*/
8101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008102f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008103 typval_T *argvars;
8104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108#else
8109 long boff = 0;
8110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008111 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 (linenr_T)0, &boff);
8117#endif
8118}
8119
8120/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121 * "byteidx()" function
8122 */
8123/*ARGSUSED*/
8124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008125f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 typval_T *argvars;
8127 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008128{
8129#ifdef FEAT_MBYTE
8130 char_u *t;
8131#endif
8132 char_u *str;
8133 long idx;
8134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008135 str = get_tv_string_chk(&argvars[0]);
8136 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008138 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008139 return;
8140
8141#ifdef FEAT_MBYTE
8142 t = str;
8143 for ( ; idx > 0; idx--)
8144 {
8145 if (*t == NUL) /* EOL reached */
8146 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008147 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008148 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008149 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008150#else
8151 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008153#endif
8154}
8155
8156/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 * "call(func, arglist)" function
8158 */
8159 static void
8160f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 typval_T *argvars;
8162 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008163{
8164 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008165 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008166 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170
8171 rettv->vval.v_number = 0;
8172 if (argvars[1].v_type != VAR_LIST)
8173 {
8174 EMSG(_(e_listreq));
8175 return;
8176 }
8177 if (argvars[1].vval.v_list == NULL)
8178 return;
8179
8180 if (argvars[0].v_type == VAR_FUNC)
8181 func = argvars[0].vval.v_string;
8182 else
8183 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 if (*func == NUL)
8185 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008186
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 if (argvars[2].v_type != VAR_UNKNOWN)
8188 {
8189 if (argvars[2].v_type != VAR_DICT)
8190 {
8191 EMSG(_(e_dictreq));
8192 return;
8193 }
8194 selfdict = argvars[2].vval.v_dict;
8195 }
8196
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008197 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8198 item = item->li_next)
8199 {
8200 if (argc == MAX_FUNC_ARGS)
8201 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008202 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008203 break;
8204 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008205 /* Make a copy of each argument. This is needed to be able to set
8206 * v_lock to VAR_FIXED in the copy without changing the original list.
8207 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008208 copy_tv(&item->li_tv, &argv[argc++]);
8209 }
8210
8211 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008212 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8214 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008215
8216 /* Free the arguments. */
8217 while (argc > 0)
8218 clear_tv(&argv[--argc]);
8219}
8220
8221/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008222 * "changenr()" function
8223 */
8224/*ARGSUSED*/
8225 static void
8226f_changenr(argvars, rettv)
8227 typval_T *argvars;
8228 typval_T *rettv;
8229{
8230 rettv->vval.v_number = curbuf->b_u_seq_cur;
8231}
8232
8233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 * "char2nr(string)" function
8235 */
8236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 typval_T *argvars;
8239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
8241#ifdef FEAT_MBYTE
8242 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008243 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 else
8245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
8248
8249/*
8250 * "cindent(lnum)" function
8251 */
8252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257#ifdef FEAT_CINDENT
8258 pos_T pos;
8259 linenr_T lnum;
8260
8261 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8264 {
8265 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 curwin->w_cursor = pos;
8268 }
8269 else
8270#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272}
8273
8274/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008275 * "clearmatches()" function
8276 */
8277/*ARGSUSED*/
8278 static void
8279f_clearmatches(argvars, rettv)
8280 typval_T *argvars;
8281 typval_T *rettv;
8282{
8283#ifdef FEAT_SEARCH_EXTRA
8284 clear_matches(curwin);
8285#endif
8286}
8287
8288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 * "col(string)" function
8290 */
8291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 typval_T *argvars;
8294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 colnr_T col = 0;
8297 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008298 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008300 fp = var2fpos(&argvars[0], FALSE, &fnum);
8301 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 if (fp->col == MAXCOL)
8304 {
8305 /* '> can be MAXCOL, get the length of the line then */
8306 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008307 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 else
8309 col = MAXCOL;
8310 }
8311 else
8312 {
8313 col = fp->col + 1;
8314#ifdef FEAT_VIRTUALEDIT
8315 /* col(".") when the cursor is on the NUL at the end of the line
8316 * because of "coladd" can be seen as an extra column. */
8317 if (virtual_active() && fp == &curwin->w_cursor)
8318 {
8319 char_u *p = ml_get_cursor();
8320
8321 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8322 curwin->w_virtcol - curwin->w_cursor.coladd))
8323 {
8324# ifdef FEAT_MBYTE
8325 int l;
8326
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008327 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 col += l;
8329# else
8330 if (*p != NUL && p[1] == NUL)
8331 ++col;
8332# endif
8333 }
8334 }
8335#endif
8336 }
8337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008338 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339}
8340
Bram Moolenaar572cb562005-08-05 21:35:02 +00008341#if defined(FEAT_INS_EXPAND)
8342/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008343 * "complete()" function
8344 */
8345/*ARGSUSED*/
8346 static void
8347f_complete(argvars, rettv)
8348 typval_T *argvars;
8349 typval_T *rettv;
8350{
8351 int startcol;
8352
8353 if ((State & INSERT) == 0)
8354 {
8355 EMSG(_("E785: complete() can only be used in Insert mode"));
8356 return;
8357 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008358
8359 /* Check for undo allowed here, because if something was already inserted
8360 * the line was already saved for undo and this check isn't done. */
8361 if (!undo_allowed())
8362 return;
8363
Bram Moolenaarade00832006-03-10 21:46:58 +00008364 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8365 {
8366 EMSG(_(e_invarg));
8367 return;
8368 }
8369
8370 startcol = get_tv_number_chk(&argvars[0], NULL);
8371 if (startcol <= 0)
8372 return;
8373
8374 set_completion(startcol - 1, argvars[1].vval.v_list);
8375}
8376
8377/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008378 * "complete_add()" function
8379 */
8380/*ARGSUSED*/
8381 static void
8382f_complete_add(argvars, rettv)
8383 typval_T *argvars;
8384 typval_T *rettv;
8385{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008386 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008387}
8388
8389/*
8390 * "complete_check()" function
8391 */
8392/*ARGSUSED*/
8393 static void
8394f_complete_check(argvars, rettv)
8395 typval_T *argvars;
8396 typval_T *rettv;
8397{
8398 int saved = RedrawingDisabled;
8399
8400 RedrawingDisabled = 0;
8401 ins_compl_check_keys(0);
8402 rettv->vval.v_number = compl_interrupted;
8403 RedrawingDisabled = saved;
8404}
8405#endif
8406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407/*
8408 * "confirm(message, buttons[, default [, type]])" function
8409 */
8410/*ARGSUSED*/
8411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 typval_T *argvars;
8414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
8416#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8417 char_u *message;
8418 char_u *buttons = NULL;
8419 char_u buf[NUMBUFLEN];
8420 char_u buf2[NUMBUFLEN];
8421 int def = 1;
8422 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008423 char_u *typestr;
8424 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008426 message = get_tv_string_chk(&argvars[0]);
8427 if (message == NULL)
8428 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008429 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8432 if (buttons == NULL)
8433 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008436 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8440 if (typestr == NULL)
8441 error = TRUE;
8442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008444 switch (TOUPPER_ASC(*typestr))
8445 {
8446 case 'E': type = VIM_ERROR; break;
8447 case 'Q': type = VIM_QUESTION; break;
8448 case 'I': type = VIM_INFO; break;
8449 case 'W': type = VIM_WARNING; break;
8450 case 'G': type = VIM_GENERIC; break;
8451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
8453 }
8454 }
8455 }
8456
8457 if (buttons == NULL || *buttons == NUL)
8458 buttons = (char_u *)_("&Ok");
8459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460 if (error)
8461 rettv->vval.v_number = 0;
8462 else
8463 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 def, NULL);
8465#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#endif
8468}
8469
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008470/*
8471 * "copy()" function
8472 */
8473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 typval_T *argvars;
8476 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008478 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
8481/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008482 * "count()" function
8483 */
8484 static void
8485f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008486 typval_T *argvars;
8487 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008488{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008489 long n = 0;
8490 int ic = FALSE;
8491
Bram Moolenaare9a41262005-01-15 22:18:47 +00008492 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008493 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 listitem_T *li;
8495 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008497
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 if ((l = argvars[0].vval.v_list) != NULL)
8499 {
8500 li = l->lv_first;
8501 if (argvars[2].v_type != VAR_UNKNOWN)
8502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008503 int error = FALSE;
8504
8505 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 if (argvars[3].v_type != VAR_UNKNOWN)
8507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 idx = get_tv_number_chk(&argvars[3], &error);
8509 if (!error)
8510 {
8511 li = list_find(l, idx);
8512 if (li == NULL)
8513 EMSGN(_(e_listidx), idx);
8514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008516 if (error)
8517 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008518 }
8519
8520 for ( ; li != NULL; li = li->li_next)
8521 if (tv_equal(&li->li_tv, &argvars[1], ic))
8522 ++n;
8523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008524 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 else if (argvars[0].v_type == VAR_DICT)
8526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008527 int todo;
8528 dict_T *d;
8529 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008531 if ((d = argvars[0].vval.v_dict) != NULL)
8532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008533 int error = FALSE;
8534
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 if (argvars[2].v_type != VAR_UNKNOWN)
8536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008537 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 if (argvars[3].v_type != VAR_UNKNOWN)
8539 EMSG(_(e_invarg));
8540 }
8541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008542 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008544 {
8545 if (!HASHITEM_EMPTY(hi))
8546 {
8547 --todo;
8548 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8549 ++n;
8550 }
8551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008552 }
8553 }
8554 else
8555 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008556 rettv->vval.v_number = n;
8557}
8558
8559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8561 *
8562 * Checks the existence of a cscope connection.
8563 */
8564/*ARGSUSED*/
8565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 typval_T *argvars;
8568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569{
8570#ifdef FEAT_CSCOPE
8571 int num = 0;
8572 char_u *dbpath = NULL;
8573 char_u *prepend = NULL;
8574 char_u buf[NUMBUFLEN];
8575
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 if (argvars[0].v_type != VAR_UNKNOWN
8577 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 num = (int)get_tv_number(&argvars[0]);
8580 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 }
8584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#endif
8589}
8590
8591/*
8592 * "cursor(lnum, col)" function
8593 *
8594 * Moves the cursor to the specified line and column
8595 */
8596/*ARGSUSED*/
8597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008599 typval_T *argvars;
8600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008603#ifdef FEAT_VIRTUALEDIT
8604 long coladd = 0;
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaara5525202006-03-02 22:52:09 +00008607 if (argvars[1].v_type == VAR_UNKNOWN)
8608 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008609 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008610
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008611 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008612 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008613 line = pos.lnum;
8614 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008615#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008617#endif
8618 }
8619 else
8620 {
8621 line = get_tv_lnum(argvars);
8622 col = get_tv_number_chk(&argvars[1], NULL);
8623#ifdef FEAT_VIRTUALEDIT
8624 if (argvars[2].v_type != VAR_UNKNOWN)
8625 coladd = get_tv_number_chk(&argvars[2], NULL);
8626#endif
8627 }
8628 if (line < 0 || col < 0
8629#ifdef FEAT_VIRTUALEDIT
8630 || coladd < 0
8631#endif
8632 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008633 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (line > 0)
8635 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (col > 0)
8637 curwin->w_cursor.col = col - 1;
8638#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008639 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#endif
8641
8642 /* Make sure the cursor is in a valid position. */
8643 check_cursor();
8644#ifdef FEAT_MBYTE
8645 /* Correct cursor for multi-byte character. */
8646 if (has_mbyte)
8647 mb_adjust_cursor();
8648#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008649
8650 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 */
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008661 int noref = 0;
8662
8663 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008664 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008665 if (noref < 0 || noref > 1)
8666 EMSG(_(e_invarg));
8667 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008668 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
8671/*
8672 * "delete()" function
8673 */
8674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 typval_T *argvars;
8677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683}
8684
8685/*
8686 * "did_filetype()" function
8687 */
8688/*ARGSUSED*/
8689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008691 typval_T *argvars;
8692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
8694#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#endif
8699}
8700
8701/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008702 * "diff_filler()" function
8703 */
8704/*ARGSUSED*/
8705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 typval_T *argvars;
8708 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008709{
8710#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008712#endif
8713}
8714
8715/*
8716 * "diff_hlID()" function
8717 */
8718/*ARGSUSED*/
8719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008723{
8724#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008726 static linenr_T prev_lnum = 0;
8727 static int changedtick = 0;
8728 static int fnum = 0;
8729 static int change_start = 0;
8730 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008731 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008732 int filler_lines;
8733 int col;
8734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 if (lnum < 0) /* ignore type error in {lnum} arg */
8736 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008737 if (lnum != prev_lnum
8738 || changedtick != curbuf->b_changedtick
8739 || fnum != curbuf->b_fnum)
8740 {
8741 /* New line, buffer, change: need to get the values. */
8742 filler_lines = diff_check(curwin, lnum);
8743 if (filler_lines < 0)
8744 {
8745 if (filler_lines == -1)
8746 {
8747 change_start = MAXCOL;
8748 change_end = -1;
8749 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8750 hlID = HLF_ADD; /* added line */
8751 else
8752 hlID = HLF_CHD; /* changed line */
8753 }
8754 else
8755 hlID = HLF_ADD; /* added line */
8756 }
8757 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008758 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008759 prev_lnum = lnum;
8760 changedtick = curbuf->b_changedtick;
8761 fnum = curbuf->b_fnum;
8762 }
8763
8764 if (hlID == HLF_CHD || hlID == HLF_TXD)
8765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008767 if (col >= change_start && col <= change_end)
8768 hlID = HLF_TXD; /* changed text */
8769 else
8770 hlID = HLF_CHD; /* changed line */
8771 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008772 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008773#endif
8774}
8775
8776/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008777 * "empty({expr})" function
8778 */
8779 static void
8780f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *argvars;
8782 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008783{
8784 int n;
8785
8786 switch (argvars[0].v_type)
8787 {
8788 case VAR_STRING:
8789 case VAR_FUNC:
8790 n = argvars[0].vval.v_string == NULL
8791 || *argvars[0].vval.v_string == NUL;
8792 break;
8793 case VAR_NUMBER:
8794 n = argvars[0].vval.v_number == 0;
8795 break;
8796 case VAR_LIST:
8797 n = argvars[0].vval.v_list == NULL
8798 || argvars[0].vval.v_list->lv_first == NULL;
8799 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008800 case VAR_DICT:
8801 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008804 default:
8805 EMSG2(_(e_intern2), "f_empty()");
8806 n = 0;
8807 }
8808
8809 rettv->vval.v_number = n;
8810}
8811
8812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 * "escape({string}, {chars})" function
8814 */
8815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008817 typval_T *argvars;
8818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819{
8820 char_u buf[NUMBUFLEN];
8821
Bram Moolenaar758711c2005-02-02 23:11:38 +00008822 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8823 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
8827/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008828 * "eval()" function
8829 */
8830/*ARGSUSED*/
8831 static void
8832f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *argvars;
8834 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835{
8836 char_u *s;
8837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008838 s = get_tv_string_chk(&argvars[0]);
8839 if (s != NULL)
8840 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008842 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8843 {
8844 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008845 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008847 else if (*s != NUL)
8848 EMSG(_(e_trailing));
8849}
8850
8851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 * "eventhandler()" function
8853 */
8854/*ARGSUSED*/
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
8863/*
8864 * "executable()" function
8865 */
8866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 typval_T *argvars;
8869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872}
8873
8874/*
8875 * "exists()" function
8876 */
8877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *argvars;
8880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 char_u *p;
8883 char_u *name;
8884 int n = FALSE;
8885 int len = 0;
8886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (*p == '$') /* environment variable */
8889 {
8890 /* first try "normal" environment variables (fast) */
8891 if (mch_getenv(p + 1) != NULL)
8892 n = TRUE;
8893 else
8894 {
8895 /* try expanding things like $VIM and ${HOME} */
8896 p = expand_env_save(p);
8897 if (p != NULL && *p != '$')
8898 n = TRUE;
8899 vim_free(p);
8900 }
8901 }
8902 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008905 if (*skipwhite(p) != NUL)
8906 n = FALSE; /* trailing garbage */
8907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 else if (*p == '*') /* internal or user defined function */
8909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912 else if (*p == ':')
8913 {
8914 n = cmd_exists(p + 1);
8915 }
8916 else if (*p == '#')
8917 {
8918#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008919 if (p[1] == '#')
8920 n = autocmd_supported(p + 2);
8921 else
8922 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#endif
8924 }
8925 else /* internal variable */
8926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008927 char_u *tofree;
8928 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008930 /* get_name_len() takes care of expanding curly braces */
8931 name = p;
8932 len = get_name_len(&p, &tofree, TRUE, FALSE);
8933 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008935 if (tofree != NULL)
8936 name = tofree;
8937 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8938 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008940 /* handle d.key, l[idx], f(expr) */
8941 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8942 if (n)
8943 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 }
8945 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008946 if (*p != NUL)
8947 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008949 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953}
8954
8955/*
8956 * "expand()" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 char_u *s;
8964 int len;
8965 char_u *errormsg;
8966 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8967 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->v_type = VAR_STRING;
8971 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 if (*s == '%' || *s == '#' || *s == '<')
8973 {
8974 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008975 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 --emsg_off;
8977 }
8978 else
8979 {
8980 /* When the optional second argument is non-zero, don't remove matches
8981 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 if (argvars[1].v_type != VAR_UNKNOWN
8983 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 if (!error)
8986 {
8987 ExpandInit(&xpc);
8988 xpc.xp_context = EXPAND_FILES;
8989 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 }
8991 else
8992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 }
8994}
8995
8996/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008997 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008998 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008999 */
9000 static void
9001f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009005 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009006 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 list_T *l1, *l2;
9009 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 l1 = argvars[0].vval.v_list;
9014 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009015 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9016 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 {
9018 if (argvars[2].v_type != VAR_UNKNOWN)
9019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 before = get_tv_number_chk(&argvars[2], &error);
9021 if (error)
9022 return; /* type error; errmsg already given */
9023
Bram Moolenaar758711c2005-02-02 23:11:38 +00009024 if (before == l1->lv_len)
9025 item = NULL;
9026 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009028 item = list_find(l1, before);
9029 if (item == NULL)
9030 {
9031 EMSGN(_(e_listidx), before);
9032 return;
9033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034 }
9035 }
9036 else
9037 item = NULL;
9038 list_extend(l1, l2, item);
9039
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 copy_tv(&argvars[0], rettv);
9041 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9044 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 dict_T *d1, *d2;
9046 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 char_u *action;
9048 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009050 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009051
9052 d1 = argvars[0].vval.v_dict;
9053 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009054 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9055 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 {
9057 /* Check the third argument. */
9058 if (argvars[2].v_type != VAR_UNKNOWN)
9059 {
9060 static char *(av[]) = {"keep", "force", "error"};
9061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 action = get_tv_string_chk(&argvars[2]);
9063 if (action == NULL)
9064 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 for (i = 0; i < 3; ++i)
9066 if (STRCMP(action, av[i]) == 0)
9067 break;
9068 if (i == 3)
9069 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009070 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009071 return;
9072 }
9073 }
9074 else
9075 action = (char_u *)"force";
9076
9077 /* Go over all entries in the second dict and add them to the
9078 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009079 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009082 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009083 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009084 --todo;
9085 di1 = dict_find(d1, hi2->hi_key, -1);
9086 if (di1 == NULL)
9087 {
9088 di1 = dictitem_copy(HI2DI(hi2));
9089 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9090 dictitem_free(di1);
9091 }
9092 else if (*action == 'e')
9093 {
9094 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9095 break;
9096 }
9097 else if (*action == 'f')
9098 {
9099 clear_tv(&di1->di_tv);
9100 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009102 }
9103 }
9104
Bram Moolenaare9a41262005-01-15 22:18:47 +00009105 copy_tv(&argvars[0], rettv);
9106 }
9107 }
9108 else
9109 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009110}
9111
9112/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009113 * "feedkeys()" function
9114 */
9115/*ARGSUSED*/
9116 static void
9117f_feedkeys(argvars, rettv)
9118 typval_T *argvars;
9119 typval_T *rettv;
9120{
9121 int remap = TRUE;
9122 char_u *keys, *flags;
9123 char_u nbuf[NUMBUFLEN];
9124 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009125 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009126
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009127 /* This is not allowed in the sandbox. If the commands would still be
9128 * executed in the sandbox it would be OK, but it probably happens later,
9129 * when "sandbox" is no longer set. */
9130 if (check_secure())
9131 return;
9132
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009133 rettv->vval.v_number = 0;
9134 keys = get_tv_string(&argvars[0]);
9135 if (*keys != NUL)
9136 {
9137 if (argvars[1].v_type != VAR_UNKNOWN)
9138 {
9139 flags = get_tv_string_buf(&argvars[1], nbuf);
9140 for ( ; *flags != NUL; ++flags)
9141 {
9142 switch (*flags)
9143 {
9144 case 'n': remap = FALSE; break;
9145 case 'm': remap = TRUE; break;
9146 case 't': typed = TRUE; break;
9147 }
9148 }
9149 }
9150
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009151 /* Need to escape K_SPECIAL and CSI before putting the string in the
9152 * typeahead buffer. */
9153 keys_esc = vim_strsave_escape_csi(keys);
9154 if (keys_esc != NULL)
9155 {
9156 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009157 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009158 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009159 if (vgetc_busy)
9160 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009161 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009162 }
9163}
9164
9165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 * "filereadable()" function
9167 */
9168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009170 typval_T *argvars;
9171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
9173 FILE *fd;
9174 char_u *p;
9175 int n;
9176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9179 {
9180 n = TRUE;
9181 fclose(fd);
9182 }
9183 else
9184 n = FALSE;
9185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009190 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 * rights to write into.
9192 */
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009198 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
Bram Moolenaar33570922005-01-25 22:26:29 +00009201static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009202
9203 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009204findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *argvars;
9206 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009207 int dir;
9208{
9209#ifdef FEAT_SEARCHPATH
9210 char_u *fname;
9211 char_u *fresult = NULL;
9212 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9213 char_u *p;
9214 char_u pathbuf[NUMBUFLEN];
9215 int count = 1;
9216 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009217 int error = FALSE;
9218#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009219
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009220 rettv->vval.v_string = NULL;
9221 rettv->v_type = VAR_STRING;
9222
9223#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009226 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9229 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009230 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 else
9232 {
9233 if (*p != NUL)
9234 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009237 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009239 }
9240
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009241 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9242 error = TRUE;
9243
9244 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 do
9247 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009248 if (rettv->v_type == VAR_STRING)
9249 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 fresult = find_file_in_path_option(first ? fname : NULL,
9251 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009252 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009253 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009254 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009255
9256 if (fresult != NULL && rettv->v_type == VAR_LIST)
9257 list_append_string(rettv->vval.v_list, fresult, -1);
9258
9259 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009261
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009262 if (rettv->v_type == VAR_STRING)
9263 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009264#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009265}
9266
Bram Moolenaar33570922005-01-25 22:26:29 +00009267static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9268static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009269
9270/*
9271 * Implementation of map() and filter().
9272 */
9273 static void
9274filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 typval_T *argvars;
9276 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009277 int map;
9278{
9279 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 listitem_T *li, *nli;
9282 list_T *l = NULL;
9283 dictitem_T *di;
9284 hashtab_T *ht;
9285 hashitem_T *hi;
9286 dict_T *d = NULL;
9287 typval_T save_val;
9288 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009289 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009290 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009291 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009292 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009293
9294 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009295 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009296 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009297 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009298 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009299 return;
9300 }
9301 else if (argvars[0].v_type == VAR_DICT)
9302 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009303 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009304 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 return;
9306 }
9307 else
9308 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009309 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 return;
9311 }
9312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009313 expr = get_tv_string_buf_chk(&argvars[1], buf);
9314 /* On type errors, the preceding call has already displayed an error
9315 * message. Avoid a misleading error message for an empty string that
9316 * was not passed as argument. */
9317 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 prepare_vimvar(VV_VAL, &save_val);
9320 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009321
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009322 /* We reset "did_emsg" to be able to detect whether an error
9323 * occurred during evaluation of the expression. */
9324 save_did_emsg = did_emsg;
9325 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 prepare_vimvar(VV_KEY, &save_key);
9330 vimvars[VV_KEY].vv_type = VAR_STRING;
9331
9332 ht = &d->dv_hashtab;
9333 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009334 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 if (!HASHITEM_EMPTY(hi))
9338 {
9339 --todo;
9340 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009341 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 break;
9343 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009344 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009345 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 break;
9347 if (!map && rem)
9348 dictitem_remove(d, di);
9349 clear_tv(&vimvars[VV_KEY].vv_tv);
9350 }
9351 }
9352 hash_unlock(ht);
9353
9354 restore_vimvar(VV_KEY, &save_key);
9355 }
9356 else
9357 {
9358 for (li = l->lv_first; li != NULL; li = nli)
9359 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009360 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009361 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009362 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009363 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009364 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009365 break;
9366 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009368 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009369 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009372
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009373 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009375
9376 copy_tv(&argvars[0], rettv);
9377}
9378
9379 static int
9380filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009382 char_u *expr;
9383 int map;
9384 int *remp;
9385{
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009388 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009391 s = expr;
9392 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009393 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 if (*s != NUL) /* check for trailing chars after expr */
9395 {
9396 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009397 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009398 }
9399 if (map)
9400 {
9401 /* map(): replace the list item value */
9402 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009403 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009404 *tv = rettv;
9405 }
9406 else
9407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 int error = FALSE;
9409
Bram Moolenaare9a41262005-01-15 22:18:47 +00009410 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009413 /* On type error, nothing has been removed; return FAIL to stop the
9414 * loop. The error message was given by get_tv_number_chk(). */
9415 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009416 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009418 retval = OK;
9419theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009421 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009422}
9423
9424/*
9425 * "filter()" function
9426 */
9427 static void
9428f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 typval_T *argvars;
9430 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009431{
9432 filter_map(argvars, rettv, FALSE);
9433}
9434
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 * "finddir({fname}[, {path}[, {count}]])" function
9437 */
9438 static void
9439f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009442{
9443 findfilendir(argvars, rettv, TRUE);
9444}
9445
9446/*
9447 * "findfile({fname}[, {path}[, {count}]])" function
9448 */
9449 static void
9450f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009453{
9454 findfilendir(argvars, rettv, FALSE);
9455}
9456
9457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 * "fnamemodify({fname}, {mods})" function
9459 */
9460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 typval_T *argvars;
9463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 char_u *fname;
9466 char_u *mods;
9467 int usedlen = 0;
9468 int len;
9469 char_u *fbuf = NULL;
9470 char_u buf[NUMBUFLEN];
9471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009472 fname = get_tv_string_chk(&argvars[0]);
9473 mods = get_tv_string_buf_chk(&argvars[1], buf);
9474 if (fname == NULL || mods == NULL)
9475 fname = NULL;
9476 else
9477 {
9478 len = (int)STRLEN(fname);
9479 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 vim_free(fbuf);
9488}
9489
Bram Moolenaar33570922005-01-25 22:26:29 +00009490static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
9492/*
9493 * "foldclosed()" function
9494 */
9495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009497 typval_T *argvars;
9498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 int end;
9500{
9501#ifdef FEAT_FOLDING
9502 linenr_T lnum;
9503 linenr_T first, last;
9504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9507 {
9508 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9509 {
9510 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 return;
9515 }
9516 }
9517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519}
9520
9521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009522 * "foldclosed()" function
9523 */
9524 static void
9525f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009528{
9529 foldclosed_both(argvars, rettv, FALSE);
9530}
9531
9532/*
9533 * "foldclosedend()" function
9534 */
9535 static void
9536f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009537 typval_T *argvars;
9538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539{
9540 foldclosed_both(argvars, rettv, TRUE);
9541}
9542
9543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 * "foldlevel()" function
9545 */
9546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551#ifdef FEAT_FOLDING
9552 linenr_T lnum;
9553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 else
9558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560}
9561
9562/*
9563 * "foldtext()" function
9564 */
9565/*ARGSUSED*/
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571#ifdef FEAT_FOLDING
9572 linenr_T lnum;
9573 char_u *s;
9574 char_u *r;
9575 int len;
9576 char *txt;
9577#endif
9578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->v_type = VAR_STRING;
9580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9583 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9584 <= curbuf->b_ml.ml_line_count
9585 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
9587 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009588 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9589 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 {
9591 if (!linewhite(lnum))
9592 break;
9593 ++lnum;
9594 }
9595
9596 /* Find interesting text in this line. */
9597 s = skipwhite(ml_get(lnum));
9598 /* skip C comment-start */
9599 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009602 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009604 {
9605 s = skipwhite(ml_get(lnum + 1));
9606 if (*s == '*')
9607 s = skipwhite(s + 1);
9608 }
9609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 txt = _("+-%s%3ld lines: ");
9611 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 + 20 /* for %3ld */
9614 + STRLEN(s))); /* concatenated */
9615 if (r != NULL)
9616 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9618 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9619 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 len = (int)STRLEN(r);
9621 STRCAT(r, s);
9622 /* remove 'foldmarker' and 'commentstring' */
9623 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 }
9626 }
9627#endif
9628}
9629
9630/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009631 * "foldtextresult(lnum)" function
9632 */
9633/*ARGSUSED*/
9634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009638{
9639#ifdef FEAT_FOLDING
9640 linenr_T lnum;
9641 char_u *text;
9642 char_u buf[51];
9643 foldinfo_T foldinfo;
9644 int fold_count;
9645#endif
9646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->v_type = VAR_STRING;
9648 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009649#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 /* treat illegal types and illegal string values for {lnum} the same */
9652 if (lnum < 0)
9653 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009654 fold_count = foldedCount(curwin, lnum, &foldinfo);
9655 if (fold_count > 0)
9656 {
9657 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9658 &foldinfo, buf);
9659 if (text == buf)
9660 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009662 }
9663#endif
9664}
9665
9666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 * "foreground()" function
9668 */
9669/*ARGSUSED*/
9670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009672 typval_T *argvars;
9673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676#ifdef FEAT_GUI
9677 if (gui.in_use)
9678 gui_mch_set_foreground();
9679#else
9680# ifdef WIN32
9681 win32_set_foreground();
9682# endif
9683#endif
9684}
9685
9686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009687 * "function()" function
9688 */
9689/*ARGSUSED*/
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009694{
9695 char_u *s;
9696
Bram Moolenaara7043832005-01-21 11:56:39 +00009697 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009699 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009700 EMSG2(_(e_invarg2), s);
9701 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009702 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009703 else
9704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_string = vim_strsave(s);
9706 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009707 }
9708}
9709
9710/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009711 * "garbagecollect()" function
9712 */
9713/*ARGSUSED*/
9714 static void
9715f_garbagecollect(argvars, rettv)
9716 typval_T *argvars;
9717 typval_T *rettv;
9718{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009719 /* This is postponed until we are back at the toplevel, because we may be
9720 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9721 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009722}
9723
9724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725 * "get()" function
9726 */
9727 static void
9728f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *argvars;
9730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009731{
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 listitem_T *li;
9733 list_T *l;
9734 dictitem_T *di;
9735 dict_T *d;
9736 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009737
Bram Moolenaare9a41262005-01-15 22:18:47 +00009738 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009739 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009740 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 int error = FALSE;
9743
9744 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9745 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009749 else if (argvars[0].v_type == VAR_DICT)
9750 {
9751 if ((d = argvars[0].vval.v_dict) != NULL)
9752 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009753 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009754 if (di != NULL)
9755 tv = &di->di_tv;
9756 }
9757 }
9758 else
9759 EMSG2(_(e_listdictarg), "get()");
9760
9761 if (tv == NULL)
9762 {
9763 if (argvars[2].v_type == VAR_UNKNOWN)
9764 rettv->vval.v_number = 0;
9765 else
9766 copy_tv(&argvars[2], rettv);
9767 }
9768 else
9769 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009770}
9771
Bram Moolenaar342337a2005-07-21 21:11:17 +00009772static 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 +00009773
9774/*
9775 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009776 * Return a range (from start to end) of lines in rettv from the specified
9777 * buffer.
9778 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009779 */
9780 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009781get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009782 buf_T *buf;
9783 linenr_T start;
9784 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009786 typval_T *rettv;
9787{
9788 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009789
Bram Moolenaar342337a2005-07-21 21:11:17 +00009790 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009791 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009792 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009793 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009794 }
9795 else
9796 rettv->vval.v_number = 0;
9797
9798 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9799 return;
9800
9801 if (!retlist)
9802 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009803 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9804 p = ml_get_buf(buf, start, FALSE);
9805 else
9806 p = (char_u *)"";
9807
9808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = vim_strsave(p);
9810 }
9811 else
9812 {
9813 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009814 return;
9815
9816 if (start < 1)
9817 start = 1;
9818 if (end > buf->b_ml.ml_line_count)
9819 end = buf->b_ml.ml_line_count;
9820 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009821 if (list_append_string(rettv->vval.v_list,
9822 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009823 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009824 }
9825}
9826
9827/*
9828 * "getbufline()" function
9829 */
9830 static void
9831f_getbufline(argvars, rettv)
9832 typval_T *argvars;
9833 typval_T *rettv;
9834{
9835 linenr_T lnum;
9836 linenr_T end;
9837 buf_T *buf;
9838
9839 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9840 ++emsg_off;
9841 buf = get_buf_tv(&argvars[0]);
9842 --emsg_off;
9843
Bram Moolenaar661b1822005-07-28 22:36:45 +00009844 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009845 if (argvars[2].v_type == VAR_UNKNOWN)
9846 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009847 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009848 end = get_tv_lnum_buf(&argvars[2], buf);
9849
Bram Moolenaar342337a2005-07-21 21:11:17 +00009850 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009851}
9852
Bram Moolenaar0d660222005-01-07 21:51:51 +00009853/*
9854 * "getbufvar()" function
9855 */
9856 static void
9857f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009860{
9861 buf_T *buf;
9862 buf_T *save_curbuf;
9863 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9867 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009868 ++emsg_off;
9869 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009870
9871 rettv->v_type = VAR_STRING;
9872 rettv->vval.v_string = NULL;
9873
9874 if (buf != NULL && varname != NULL)
9875 {
9876 if (*varname == '&') /* buffer-local-option */
9877 {
9878 /* set curbuf to be our buf, temporarily */
9879 save_curbuf = curbuf;
9880 curbuf = buf;
9881
9882 get_option_tv(&varname, rettv, TRUE);
9883
9884 /* restore previous notion of curbuf */
9885 curbuf = save_curbuf;
9886 }
9887 else
9888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 if (*varname == NUL)
9890 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9891 * scope prefix before the NUL byte is required by
9892 * find_var_in_ht(). */
9893 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009894 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009895 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009896 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009898 }
9899 }
9900
9901 --emsg_off;
9902}
9903
9904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 * "getchar()" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009915 /* Position the cursor. Needed after a message that ends in a space. */
9916 windgoto(msg_row, msg_col);
9917
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 ++no_mapping;
9919 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009920 for (;;)
9921 {
9922 if (argvars[0].v_type == VAR_UNKNOWN)
9923 /* getchar(): blocking wait. */
9924 n = safe_vgetc();
9925 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9926 /* getchar(1): only check if char avail */
9927 n = vpeekc();
9928 else if (error || vpeekc() == NUL)
9929 /* illegal argument or getchar(0) and no char avail: return zero */
9930 n = 0;
9931 else
9932 /* getchar(0) and char avail: return char */
9933 n = safe_vgetc();
9934 if (n == K_IGNORE)
9935 continue;
9936 break;
9937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 --no_mapping;
9939 --allow_keys;
9940
Bram Moolenaar219b8702006-11-01 14:32:36 +00009941 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9942 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9943 vimvars[VV_MOUSE_COL].vv_nr = 0;
9944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (IS_SPECIAL(n) || mod_mask != 0)
9947 {
9948 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9949 int i = 0;
9950
9951 /* Turn a special key into three bytes, plus modifier. */
9952 if (mod_mask != 0)
9953 {
9954 temp[i++] = K_SPECIAL;
9955 temp[i++] = KS_MODIFIER;
9956 temp[i++] = mod_mask;
9957 }
9958 if (IS_SPECIAL(n))
9959 {
9960 temp[i++] = K_SPECIAL;
9961 temp[i++] = K_SECOND(n);
9962 temp[i++] = K_THIRD(n);
9963 }
9964#ifdef FEAT_MBYTE
9965 else if (has_mbyte)
9966 i += (*mb_char2bytes)(n, temp + i);
9967#endif
9968 else
9969 temp[i++] = n;
9970 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->v_type = VAR_STRING;
9972 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009973
9974#ifdef FEAT_MOUSE
9975 if (n == K_LEFTMOUSE
9976 || n == K_LEFTMOUSE_NM
9977 || n == K_LEFTDRAG
9978 || n == K_LEFTRELEASE
9979 || n == K_LEFTRELEASE_NM
9980 || n == K_MIDDLEMOUSE
9981 || n == K_MIDDLEDRAG
9982 || n == K_MIDDLERELEASE
9983 || n == K_RIGHTMOUSE
9984 || n == K_RIGHTDRAG
9985 || n == K_RIGHTRELEASE
9986 || n == K_X1MOUSE
9987 || n == K_X1DRAG
9988 || n == K_X1RELEASE
9989 || n == K_X2MOUSE
9990 || n == K_X2DRAG
9991 || n == K_X2RELEASE
9992 || n == K_MOUSEDOWN
9993 || n == K_MOUSEUP)
9994 {
9995 int row = mouse_row;
9996 int col = mouse_col;
9997 win_T *win;
9998 linenr_T lnum;
9999# ifdef FEAT_WINDOWS
10000 win_T *wp;
10001# endif
10002 int n = 1;
10003
10004 if (row >= 0 && col >= 0)
10005 {
10006 /* Find the window at the mouse coordinates and compute the
10007 * text position. */
10008 win = mouse_find_win(&row, &col);
10009 (void)mouse_comp_pos(win, &row, &col, &lnum);
10010# ifdef FEAT_WINDOWS
10011 for (wp = firstwin; wp != win; wp = wp->w_next)
10012 ++n;
10013# endif
10014 vimvars[VV_MOUSE_WIN].vv_nr = n;
10015 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10016 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10017 }
10018 }
10019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021}
10022
10023/*
10024 * "getcharmod()" function
10025 */
10026/*ARGSUSED*/
10027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010029 typval_T *argvars;
10030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "getcmdline()" function
10037 */
10038/*ARGSUSED*/
10039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010041 typval_T *argvars;
10042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 rettv->v_type = VAR_STRING;
10045 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "getcmdpos()" function
10050 */
10051/*ARGSUSED*/
10052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010054 typval_T *argvars;
10055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058}
10059
10060/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010061 * "getcmdtype()" function
10062 */
10063/*ARGSUSED*/
10064 static void
10065f_getcmdtype(argvars, rettv)
10066 typval_T *argvars;
10067 typval_T *rettv;
10068{
10069 rettv->v_type = VAR_STRING;
10070 rettv->vval.v_string = alloc(2);
10071 if (rettv->vval.v_string != NULL)
10072 {
10073 rettv->vval.v_string[0] = get_cmdline_type();
10074 rettv->vval.v_string[1] = NUL;
10075 }
10076}
10077
10078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 * "getcwd()" function
10080 */
10081/*ARGSUSED*/
10082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010084 typval_T *argvars;
10085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086{
10087 char_u cwd[MAXPATHL];
10088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 else
10093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010096 if (rettv->vval.v_string != NULL)
10097 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098#endif
10099 }
10100}
10101
10102/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010103 * "getfontname()" function
10104 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010105/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010108 typval_T *argvars;
10109 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->v_type = VAR_STRING;
10112 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010113#ifdef FEAT_GUI
10114 if (gui.in_use)
10115 {
10116 GuiFont font;
10117 char_u *name = NULL;
10118
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010120 {
10121 /* Get the "Normal" font. Either the name saved by
10122 * hl_set_font_name() or from the font ID. */
10123 font = gui.norm_font;
10124 name = hl_get_font_name();
10125 }
10126 else
10127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010129 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10130 return;
10131 font = gui_mch_get_font(name, FALSE);
10132 if (font == NOFONT)
10133 return; /* Invalid font name, return empty string. */
10134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010136 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010137 gui_mch_free_font(font);
10138 }
10139#endif
10140}
10141
10142/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010143 * "getfperm({fname})" function
10144 */
10145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010147 typval_T *argvars;
10148 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010149{
10150 char_u *fname;
10151 struct stat st;
10152 char_u *perm = NULL;
10153 char_u flags[] = "rwx";
10154 int i;
10155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010159 if (mch_stat((char *)fname, &st) >= 0)
10160 {
10161 perm = vim_strsave((char_u *)"---------");
10162 if (perm != NULL)
10163 {
10164 for (i = 0; i < 9; i++)
10165 {
10166 if (st.st_mode & (1 << (8 - i)))
10167 perm[i] = flags[i % 3];
10168 }
10169 }
10170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010172}
10173
10174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 * "getfsize({fname})" function
10176 */
10177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 char_u *fname;
10183 struct stat st;
10184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188
10189 if (mch_stat((char *)fname, &st) >= 0)
10190 {
10191 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010196
10197 /* non-perfect check for overflow */
10198 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10199 rettv->vval.v_number = -2;
10200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 }
10202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204}
10205
10206/*
10207 * "getftime({fname})" function
10208 */
10209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010211 typval_T *argvars;
10212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 char_u *fname;
10215 struct stat st;
10216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
10219 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223}
10224
10225/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010226 * "getftype({fname})" function
10227 */
10228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010230 typval_T *argvars;
10231 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010232{
10233 char_u *fname;
10234 struct stat st;
10235 char_u *type = NULL;
10236 char *t;
10237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010241 if (mch_lstat((char *)fname, &st) >= 0)
10242 {
10243#ifdef S_ISREG
10244 if (S_ISREG(st.st_mode))
10245 t = "file";
10246 else if (S_ISDIR(st.st_mode))
10247 t = "dir";
10248# ifdef S_ISLNK
10249 else if (S_ISLNK(st.st_mode))
10250 t = "link";
10251# endif
10252# ifdef S_ISBLK
10253 else if (S_ISBLK(st.st_mode))
10254 t = "bdev";
10255# endif
10256# ifdef S_ISCHR
10257 else if (S_ISCHR(st.st_mode))
10258 t = "cdev";
10259# endif
10260# ifdef S_ISFIFO
10261 else if (S_ISFIFO(st.st_mode))
10262 t = "fifo";
10263# endif
10264# ifdef S_ISSOCK
10265 else if (S_ISSOCK(st.st_mode))
10266 t = "fifo";
10267# endif
10268 else
10269 t = "other";
10270#else
10271# ifdef S_IFMT
10272 switch (st.st_mode & S_IFMT)
10273 {
10274 case S_IFREG: t = "file"; break;
10275 case S_IFDIR: t = "dir"; break;
10276# ifdef S_IFLNK
10277 case S_IFLNK: t = "link"; break;
10278# endif
10279# ifdef S_IFBLK
10280 case S_IFBLK: t = "bdev"; break;
10281# endif
10282# ifdef S_IFCHR
10283 case S_IFCHR: t = "cdev"; break;
10284# endif
10285# ifdef S_IFIFO
10286 case S_IFIFO: t = "fifo"; break;
10287# endif
10288# ifdef S_IFSOCK
10289 case S_IFSOCK: t = "socket"; break;
10290# endif
10291 default: t = "other";
10292 }
10293# else
10294 if (mch_isdir(fname))
10295 t = "dir";
10296 else
10297 t = "file";
10298# endif
10299#endif
10300 type = vim_strsave((char_u *)t);
10301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010303}
10304
10305/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010306 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010307 */
10308 static void
10309f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 typval_T *argvars;
10311 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010312{
10313 linenr_T lnum;
10314 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010315 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010316
10317 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010318 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010319 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010320 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010321 retlist = FALSE;
10322 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010324 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010326 retlist = TRUE;
10327 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010328
Bram Moolenaar342337a2005-07-21 21:11:17 +000010329 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010330}
10331
10332/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010333 * "getmatches()" function
10334 */
10335/*ARGSUSED*/
10336 static void
10337f_getmatches(argvars, rettv)
10338 typval_T *argvars;
10339 typval_T *rettv;
10340{
10341#ifdef FEAT_SEARCH_EXTRA
10342 dict_T *dict;
10343 matchitem_T *cur = curwin->w_match_head;
10344
10345 rettv->vval.v_number = 0;
10346
10347 if (rettv_list_alloc(rettv) == OK)
10348 {
10349 while (cur != NULL)
10350 {
10351 dict = dict_alloc();
10352 if (dict == NULL)
10353 return;
10354 ++dict->dv_refcount;
10355 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10356 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10357 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10358 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10359 list_append_dict(rettv->vval.v_list, dict);
10360 cur = cur->next;
10361 }
10362 }
10363#endif
10364}
10365
10366/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010367 * "getpos(string)" function
10368 */
10369 static void
10370f_getpos(argvars, rettv)
10371 typval_T *argvars;
10372 typval_T *rettv;
10373{
10374 pos_T *fp;
10375 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010376 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010377
10378 if (rettv_list_alloc(rettv) == OK)
10379 {
10380 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010381 fp = var2fpos(&argvars[0], TRUE, &fnum);
10382 if (fnum != -1)
10383 list_append_number(l, (varnumber_T)fnum);
10384 else
10385 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010386 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10387 : (varnumber_T)0);
10388 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10389 : (varnumber_T)0);
10390 list_append_number(l,
10391#ifdef FEAT_VIRTUALEDIT
10392 (fp != NULL) ? (varnumber_T)fp->coladd :
10393#endif
10394 (varnumber_T)0);
10395 }
10396 else
10397 rettv->vval.v_number = FALSE;
10398}
10399
10400/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010401 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010402 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010403/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010404 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010405f_getqflist(argvars, rettv)
10406 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010407 typval_T *rettv;
10408{
10409#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010410 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010411#endif
10412
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010413 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010414#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010415 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010416 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010417 wp = NULL;
10418 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10419 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010420 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010421 if (wp == NULL)
10422 return;
10423 }
10424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010425 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010426 }
10427#endif
10428}
10429
10430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 * "getreg()" function
10432 */
10433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 char_u *strregname;
10439 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010440 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010443 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 strregname = get_tv_string_chk(&argvars[0]);
10446 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010447 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010451 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 regname = (strregname == NULL ? '"' : *strregname);
10453 if (regname == 0)
10454 regname = '"';
10455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 rettv->vval.v_string = error ? NULL :
10458 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459}
10460
10461/*
10462 * "getregtype()" function
10463 */
10464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010466 typval_T *argvars;
10467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468{
10469 char_u *strregname;
10470 int regname;
10471 char_u buf[NUMBUFLEN + 2];
10472 long reglen = 0;
10473
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010474 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 {
10476 strregname = get_tv_string_chk(&argvars[0]);
10477 if (strregname == NULL) /* type error; errmsg already given */
10478 {
10479 rettv->v_type = VAR_STRING;
10480 rettv->vval.v_string = NULL;
10481 return;
10482 }
10483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 else
10485 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487
10488 regname = (strregname == NULL ? '"' : *strregname);
10489 if (regname == 0)
10490 regname = '"';
10491
10492 buf[0] = NUL;
10493 buf[1] = NUL;
10494 switch (get_reg_type(regname, &reglen))
10495 {
10496 case MLINE: buf[0] = 'V'; break;
10497 case MCHAR: buf[0] = 'v'; break;
10498#ifdef FEAT_VISUAL
10499 case MBLOCK:
10500 buf[0] = Ctrl_V;
10501 sprintf((char *)buf + 1, "%ld", reglen + 1);
10502 break;
10503#endif
10504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505 rettv->v_type = VAR_STRING;
10506 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507}
10508
10509/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010510 * "gettabwinvar()" function
10511 */
10512 static void
10513f_gettabwinvar(argvars, rettv)
10514 typval_T *argvars;
10515 typval_T *rettv;
10516{
10517 getwinvar(argvars, rettv, 1);
10518}
10519
10520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 * "getwinposx()" function
10522 */
10523/*ARGSUSED*/
10524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010526 typval_T *argvars;
10527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530#ifdef FEAT_GUI
10531 if (gui.in_use)
10532 {
10533 int x, y;
10534
10535 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538#endif
10539}
10540
10541/*
10542 * "getwinposy()" function
10543 */
10544/*ARGSUSED*/
10545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551#ifdef FEAT_GUI
10552 if (gui.in_use)
10553 {
10554 int x, y;
10555
10556 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 }
10559#endif
10560}
10561
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010562/*
10563 * Find window specifed by "vp" in tabpage "tp".
10564 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010565 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010566find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010567 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010568 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010569{
10570#ifdef FEAT_WINDOWS
10571 win_T *wp;
10572#endif
10573 int nr;
10574
10575 nr = get_tv_number_chk(vp, NULL);
10576
10577#ifdef FEAT_WINDOWS
10578 if (nr < 0)
10579 return NULL;
10580 if (nr == 0)
10581 return curwin;
10582
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010583 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10584 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010585 if (--nr <= 0)
10586 break;
10587 return wp;
10588#else
10589 if (nr == 0 || nr == 1)
10590 return curwin;
10591 return NULL;
10592#endif
10593}
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595/*
10596 * "getwinvar()" function
10597 */
10598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 typval_T *argvars;
10601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010603 getwinvar(argvars, rettv, 0);
10604}
10605
10606/*
10607 * getwinvar() and gettabwinvar()
10608 */
10609 static void
10610getwinvar(argvars, rettv, off)
10611 typval_T *argvars;
10612 typval_T *rettv;
10613 int off; /* 1 for gettabwinvar() */
10614{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 win_T *win, *oldcurwin;
10616 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010617 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010618 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010620#ifdef FEAT_WINDOWS
10621 if (off == 1)
10622 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10623 else
10624 tp = curtab;
10625#endif
10626 win = find_win_by_nr(&argvars[off], tp);
10627 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630 rettv->v_type = VAR_STRING;
10631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632
10633 if (win != NULL && varname != NULL)
10634 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010635 /* Set curwin to be our win, temporarily. Also set curbuf, so
10636 * that we can get buffer-local options. */
10637 oldcurwin = curwin;
10638 curwin = win;
10639 curbuf = win->w_buffer;
10640
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 else
10644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 if (*varname == NUL)
10646 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10647 * scope prefix before the NUL byte is required by
10648 * find_var_in_ht(). */
10649 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010651 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010655
10656 /* restore previous notion of curwin */
10657 curwin = oldcurwin;
10658 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 }
10660
10661 --emsg_off;
10662}
10663
10664/*
10665 * "glob()" function
10666 */
10667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010669 typval_T *argvars;
10670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672 expand_T xpc;
10673
10674 ExpandInit(&xpc);
10675 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 rettv->v_type = VAR_STRING;
10677 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
10682 * "globpath()" function
10683 */
10684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010686 typval_T *argvars;
10687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 if (file == NULL)
10694 rettv->vval.v_string = NULL;
10695 else
10696 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697}
10698
10699/*
10700 * "has()" function
10701 */
10702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010704 typval_T *argvars;
10705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706{
10707 int i;
10708 char_u *name;
10709 int n = FALSE;
10710 static char *(has_list[]) =
10711 {
10712#ifdef AMIGA
10713 "amiga",
10714# ifdef FEAT_ARP
10715 "arp",
10716# endif
10717#endif
10718#ifdef __BEOS__
10719 "beos",
10720#endif
10721#ifdef MSDOS
10722# ifdef DJGPP
10723 "dos32",
10724# else
10725 "dos16",
10726# endif
10727#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010728#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 "mac",
10730#endif
10731#if defined(MACOS_X_UNIX)
10732 "macunix",
10733#endif
10734#ifdef OS2
10735 "os2",
10736#endif
10737#ifdef __QNX__
10738 "qnx",
10739#endif
10740#ifdef RISCOS
10741 "riscos",
10742#endif
10743#ifdef UNIX
10744 "unix",
10745#endif
10746#ifdef VMS
10747 "vms",
10748#endif
10749#ifdef WIN16
10750 "win16",
10751#endif
10752#ifdef WIN32
10753 "win32",
10754#endif
10755#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10756 "win32unix",
10757#endif
10758#ifdef WIN64
10759 "win64",
10760#endif
10761#ifdef EBCDIC
10762 "ebcdic",
10763#endif
10764#ifndef CASE_INSENSITIVE_FILENAME
10765 "fname_case",
10766#endif
10767#ifdef FEAT_ARABIC
10768 "arabic",
10769#endif
10770#ifdef FEAT_AUTOCMD
10771 "autocmd",
10772#endif
10773#ifdef FEAT_BEVAL
10774 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010775# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10776 "balloon_multiline",
10777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
10779#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10780 "builtin_terms",
10781# ifdef ALL_BUILTIN_TCAPS
10782 "all_builtin_terms",
10783# endif
10784#endif
10785#ifdef FEAT_BYTEOFF
10786 "byte_offset",
10787#endif
10788#ifdef FEAT_CINDENT
10789 "cindent",
10790#endif
10791#ifdef FEAT_CLIENTSERVER
10792 "clientserver",
10793#endif
10794#ifdef FEAT_CLIPBOARD
10795 "clipboard",
10796#endif
10797#ifdef FEAT_CMDL_COMPL
10798 "cmdline_compl",
10799#endif
10800#ifdef FEAT_CMDHIST
10801 "cmdline_hist",
10802#endif
10803#ifdef FEAT_COMMENTS
10804 "comments",
10805#endif
10806#ifdef FEAT_CRYPT
10807 "cryptv",
10808#endif
10809#ifdef FEAT_CSCOPE
10810 "cscope",
10811#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010812#ifdef CURSOR_SHAPE
10813 "cursorshape",
10814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815#ifdef DEBUG
10816 "debug",
10817#endif
10818#ifdef FEAT_CON_DIALOG
10819 "dialog_con",
10820#endif
10821#ifdef FEAT_GUI_DIALOG
10822 "dialog_gui",
10823#endif
10824#ifdef FEAT_DIFF
10825 "diff",
10826#endif
10827#ifdef FEAT_DIGRAPHS
10828 "digraphs",
10829#endif
10830#ifdef FEAT_DND
10831 "dnd",
10832#endif
10833#ifdef FEAT_EMACS_TAGS
10834 "emacs_tags",
10835#endif
10836 "eval", /* always present, of course! */
10837#ifdef FEAT_EX_EXTRA
10838 "ex_extra",
10839#endif
10840#ifdef FEAT_SEARCH_EXTRA
10841 "extra_search",
10842#endif
10843#ifdef FEAT_FKMAP
10844 "farsi",
10845#endif
10846#ifdef FEAT_SEARCHPATH
10847 "file_in_path",
10848#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010849#if defined(UNIX) && !defined(USE_SYSTEM)
10850 "filterpipe",
10851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852#ifdef FEAT_FIND_ID
10853 "find_in_path",
10854#endif
10855#ifdef FEAT_FOLDING
10856 "folding",
10857#endif
10858#ifdef FEAT_FOOTER
10859 "footer",
10860#endif
10861#if !defined(USE_SYSTEM) && defined(UNIX)
10862 "fork",
10863#endif
10864#ifdef FEAT_GETTEXT
10865 "gettext",
10866#endif
10867#ifdef FEAT_GUI
10868 "gui",
10869#endif
10870#ifdef FEAT_GUI_ATHENA
10871# ifdef FEAT_GUI_NEXTAW
10872 "gui_neXtaw",
10873# else
10874 "gui_athena",
10875# endif
10876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#ifdef FEAT_GUI_GTK
10878 "gui_gtk",
10879# ifdef HAVE_GTK2
10880 "gui_gtk2",
10881# endif
10882#endif
10883#ifdef FEAT_GUI_MAC
10884 "gui_mac",
10885#endif
10886#ifdef FEAT_GUI_MOTIF
10887 "gui_motif",
10888#endif
10889#ifdef FEAT_GUI_PHOTON
10890 "gui_photon",
10891#endif
10892#ifdef FEAT_GUI_W16
10893 "gui_win16",
10894#endif
10895#ifdef FEAT_GUI_W32
10896 "gui_win32",
10897#endif
10898#ifdef FEAT_HANGULIN
10899 "hangul_input",
10900#endif
10901#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10902 "iconv",
10903#endif
10904#ifdef FEAT_INS_EXPAND
10905 "insert_expand",
10906#endif
10907#ifdef FEAT_JUMPLIST
10908 "jumplist",
10909#endif
10910#ifdef FEAT_KEYMAP
10911 "keymap",
10912#endif
10913#ifdef FEAT_LANGMAP
10914 "langmap",
10915#endif
10916#ifdef FEAT_LIBCALL
10917 "libcall",
10918#endif
10919#ifdef FEAT_LINEBREAK
10920 "linebreak",
10921#endif
10922#ifdef FEAT_LISP
10923 "lispindent",
10924#endif
10925#ifdef FEAT_LISTCMDS
10926 "listcmds",
10927#endif
10928#ifdef FEAT_LOCALMAP
10929 "localmap",
10930#endif
10931#ifdef FEAT_MENU
10932 "menu",
10933#endif
10934#ifdef FEAT_SESSION
10935 "mksession",
10936#endif
10937#ifdef FEAT_MODIFY_FNAME
10938 "modify_fname",
10939#endif
10940#ifdef FEAT_MOUSE
10941 "mouse",
10942#endif
10943#ifdef FEAT_MOUSESHAPE
10944 "mouseshape",
10945#endif
10946#if defined(UNIX) || defined(VMS)
10947# ifdef FEAT_MOUSE_DEC
10948 "mouse_dec",
10949# endif
10950# ifdef FEAT_MOUSE_GPM
10951 "mouse_gpm",
10952# endif
10953# ifdef FEAT_MOUSE_JSB
10954 "mouse_jsbterm",
10955# endif
10956# ifdef FEAT_MOUSE_NET
10957 "mouse_netterm",
10958# endif
10959# ifdef FEAT_MOUSE_PTERM
10960 "mouse_pterm",
10961# endif
10962# ifdef FEAT_MOUSE_XTERM
10963 "mouse_xterm",
10964# endif
10965#endif
10966#ifdef FEAT_MBYTE
10967 "multi_byte",
10968#endif
10969#ifdef FEAT_MBYTE_IME
10970 "multi_byte_ime",
10971#endif
10972#ifdef FEAT_MULTI_LANG
10973 "multi_lang",
10974#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010975#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010976#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010977 "mzscheme",
10978#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980#ifdef FEAT_OLE
10981 "ole",
10982#endif
10983#ifdef FEAT_OSFILETYPE
10984 "osfiletype",
10985#endif
10986#ifdef FEAT_PATH_EXTRA
10987 "path_extra",
10988#endif
10989#ifdef FEAT_PERL
10990#ifndef DYNAMIC_PERL
10991 "perl",
10992#endif
10993#endif
10994#ifdef FEAT_PYTHON
10995#ifndef DYNAMIC_PYTHON
10996 "python",
10997#endif
10998#endif
10999#ifdef FEAT_POSTSCRIPT
11000 "postscript",
11001#endif
11002#ifdef FEAT_PRINTER
11003 "printer",
11004#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011005#ifdef FEAT_PROFILE
11006 "profile",
11007#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011008#ifdef FEAT_RELTIME
11009 "reltime",
11010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011#ifdef FEAT_QUICKFIX
11012 "quickfix",
11013#endif
11014#ifdef FEAT_RIGHTLEFT
11015 "rightleft",
11016#endif
11017#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11018 "ruby",
11019#endif
11020#ifdef FEAT_SCROLLBIND
11021 "scrollbind",
11022#endif
11023#ifdef FEAT_CMDL_INFO
11024 "showcmd",
11025 "cmdline_info",
11026#endif
11027#ifdef FEAT_SIGNS
11028 "signs",
11029#endif
11030#ifdef FEAT_SMARTINDENT
11031 "smartindent",
11032#endif
11033#ifdef FEAT_SNIFF
11034 "sniff",
11035#endif
11036#ifdef FEAT_STL_OPT
11037 "statusline",
11038#endif
11039#ifdef FEAT_SUN_WORKSHOP
11040 "sun_workshop",
11041#endif
11042#ifdef FEAT_NETBEANS_INTG
11043 "netbeans_intg",
11044#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011045#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011046 "spell",
11047#endif
11048#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 "syntax",
11050#endif
11051#if defined(USE_SYSTEM) || !defined(UNIX)
11052 "system",
11053#endif
11054#ifdef FEAT_TAG_BINS
11055 "tag_binary",
11056#endif
11057#ifdef FEAT_TAG_OLDSTATIC
11058 "tag_old_static",
11059#endif
11060#ifdef FEAT_TAG_ANYWHITE
11061 "tag_any_white",
11062#endif
11063#ifdef FEAT_TCL
11064# ifndef DYNAMIC_TCL
11065 "tcl",
11066# endif
11067#endif
11068#ifdef TERMINFO
11069 "terminfo",
11070#endif
11071#ifdef FEAT_TERMRESPONSE
11072 "termresponse",
11073#endif
11074#ifdef FEAT_TEXTOBJ
11075 "textobjects",
11076#endif
11077#ifdef HAVE_TGETENT
11078 "tgetent",
11079#endif
11080#ifdef FEAT_TITLE
11081 "title",
11082#endif
11083#ifdef FEAT_TOOLBAR
11084 "toolbar",
11085#endif
11086#ifdef FEAT_USR_CMDS
11087 "user-commands", /* was accidentally included in 5.4 */
11088 "user_commands",
11089#endif
11090#ifdef FEAT_VIMINFO
11091 "viminfo",
11092#endif
11093#ifdef FEAT_VERTSPLIT
11094 "vertsplit",
11095#endif
11096#ifdef FEAT_VIRTUALEDIT
11097 "virtualedit",
11098#endif
11099#ifdef FEAT_VISUAL
11100 "visual",
11101#endif
11102#ifdef FEAT_VISUALEXTRA
11103 "visualextra",
11104#endif
11105#ifdef FEAT_VREPLACE
11106 "vreplace",
11107#endif
11108#ifdef FEAT_WILDIGN
11109 "wildignore",
11110#endif
11111#ifdef FEAT_WILDMENU
11112 "wildmenu",
11113#endif
11114#ifdef FEAT_WINDOWS
11115 "windows",
11116#endif
11117#ifdef FEAT_WAK
11118 "winaltkeys",
11119#endif
11120#ifdef FEAT_WRITEBACKUP
11121 "writebackup",
11122#endif
11123#ifdef FEAT_XIM
11124 "xim",
11125#endif
11126#ifdef FEAT_XFONTSET
11127 "xfontset",
11128#endif
11129#ifdef USE_XSMP
11130 "xsmp",
11131#endif
11132#ifdef USE_XSMP_INTERACT
11133 "xsmp_interact",
11134#endif
11135#ifdef FEAT_XCLIPBOARD
11136 "xterm_clipboard",
11137#endif
11138#ifdef FEAT_XTERM_SAVE
11139 "xterm_save",
11140#endif
11141#if defined(UNIX) && defined(FEAT_X11)
11142 "X11",
11143#endif
11144 NULL
11145 };
11146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 for (i = 0; has_list[i] != NULL; ++i)
11149 if (STRICMP(name, has_list[i]) == 0)
11150 {
11151 n = TRUE;
11152 break;
11153 }
11154
11155 if (n == FALSE)
11156 {
11157 if (STRNICMP(name, "patch", 5) == 0)
11158 n = has_patch(atoi((char *)name + 5));
11159 else if (STRICMP(name, "vim_starting") == 0)
11160 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011161#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11162 else if (STRICMP(name, "balloon_multiline") == 0)
11163 n = multiline_balloon_available();
11164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165#ifdef DYNAMIC_TCL
11166 else if (STRICMP(name, "tcl") == 0)
11167 n = tcl_enabled(FALSE);
11168#endif
11169#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11170 else if (STRICMP(name, "iconv") == 0)
11171 n = iconv_enabled(FALSE);
11172#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011173#ifdef DYNAMIC_MZSCHEME
11174 else if (STRICMP(name, "mzscheme") == 0)
11175 n = mzscheme_enabled(FALSE);
11176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177#ifdef DYNAMIC_RUBY
11178 else if (STRICMP(name, "ruby") == 0)
11179 n = ruby_enabled(FALSE);
11180#endif
11181#ifdef DYNAMIC_PYTHON
11182 else if (STRICMP(name, "python") == 0)
11183 n = python_enabled(FALSE);
11184#endif
11185#ifdef DYNAMIC_PERL
11186 else if (STRICMP(name, "perl") == 0)
11187 n = perl_enabled(FALSE);
11188#endif
11189#ifdef FEAT_GUI
11190 else if (STRICMP(name, "gui_running") == 0)
11191 n = (gui.in_use || gui.starting);
11192# ifdef FEAT_GUI_W32
11193 else if (STRICMP(name, "gui_win32s") == 0)
11194 n = gui_is_win32s();
11195# endif
11196# ifdef FEAT_BROWSE
11197 else if (STRICMP(name, "browse") == 0)
11198 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11199# endif
11200#endif
11201#ifdef FEAT_SYN_HL
11202 else if (STRICMP(name, "syntax_items") == 0)
11203 n = syntax_present(curbuf);
11204#endif
11205#if defined(WIN3264)
11206 else if (STRICMP(name, "win95") == 0)
11207 n = mch_windows95();
11208#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011209#ifdef FEAT_NETBEANS_INTG
11210 else if (STRICMP(name, "netbeans_enabled") == 0)
11211 n = usingNetbeans;
11212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 }
11214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216}
11217
11218/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219 * "has_key()" function
11220 */
11221 static void
11222f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 typval_T *argvars;
11224 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225{
11226 rettv->vval.v_number = 0;
11227 if (argvars[0].v_type != VAR_DICT)
11228 {
11229 EMSG(_(e_dictreq));
11230 return;
11231 }
11232 if (argvars[0].vval.v_dict == NULL)
11233 return;
11234
11235 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011236 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237}
11238
11239/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011240 * "haslocaldir()" function
11241 */
11242/*ARGSUSED*/
11243 static void
11244f_haslocaldir(argvars, rettv)
11245 typval_T *argvars;
11246 typval_T *rettv;
11247{
11248 rettv->vval.v_number = (curwin->w_localdir != NULL);
11249}
11250
11251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 * "hasmapto()" function
11253 */
11254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011256 typval_T *argvars;
11257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
11259 char_u *name;
11260 char_u *mode;
11261 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011262 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011265 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 mode = (char_u *)"nvo";
11267 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011270 if (argvars[2].v_type != VAR_UNKNOWN)
11271 abbr = get_tv_number(&argvars[2]);
11272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273
Bram Moolenaar2c932302006-03-18 21:42:09 +000011274 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
11281 * "histadd()" function
11282 */
11283/*ARGSUSED*/
11284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
11289#ifdef FEAT_CMDHIST
11290 int histype;
11291 char_u *str;
11292 char_u buf[NUMBUFLEN];
11293#endif
11294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 if (check_restricted() || check_secure())
11297 return;
11298#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11300 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 if (histype >= 0)
11302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 if (*str != NUL)
11305 {
11306 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 return;
11309 }
11310 }
11311#endif
11312}
11313
11314/*
11315 * "histdel()" function
11316 */
11317/*ARGSUSED*/
11318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323#ifdef FEAT_CMDHIST
11324 int n;
11325 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011326 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11329 if (str == NULL)
11330 n = 0;
11331 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011334 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 else
11339 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 get_tv_string_buf(&argvars[1], buf));
11342 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345#endif
11346}
11347
11348/*
11349 * "histget()" function
11350 */
11351/*ARGSUSED*/
11352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011354 typval_T *argvars;
11355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356{
11357#ifdef FEAT_CMDHIST
11358 int type;
11359 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11363 if (str == NULL)
11364 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 {
11367 type = get_histtype(str);
11368 if (argvars[1].v_type == VAR_UNKNOWN)
11369 idx = get_history_idx(type);
11370 else
11371 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11372 /* -1 on type error */
11373 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379}
11380
11381/*
11382 * "histnr()" function
11383 */
11384/*ARGSUSED*/
11385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389{
11390 int i;
11391
11392#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 char_u *history = get_tv_string_chk(&argvars[0]);
11394
11395 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 if (i >= HIST_CMD && i < HIST_COUNT)
11397 i = get_history_idx(i);
11398 else
11399#endif
11400 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403
11404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 * "highlightID(name)" function
11406 */
11407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 typval_T *argvars;
11410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413}
11414
11415/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011416 * "highlight_exists()" function
11417 */
11418 static void
11419f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *argvars;
11421 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011422{
11423 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11424}
11425
11426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 * "hostname()" function
11428 */
11429/*ARGSUSED*/
11430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011432 typval_T *argvars;
11433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434{
11435 char_u hostname[256];
11436
11437 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 rettv->v_type = VAR_STRING;
11439 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440}
11441
11442/*
11443 * iconv() function
11444 */
11445/*ARGSUSED*/
11446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
11451#ifdef FEAT_MBYTE
11452 char_u buf1[NUMBUFLEN];
11453 char_u buf2[NUMBUFLEN];
11454 char_u *from, *to, *str;
11455 vimconv_T vimconv;
11456#endif
11457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 rettv->v_type = VAR_STRING;
11459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
11461#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 str = get_tv_string(&argvars[0]);
11463 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11464 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 vimconv.vc_type = CONV_NONE;
11466 convert_setup(&vimconv, from, to);
11467
11468 /* If the encodings are equal, no conversion needed. */
11469 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473
11474 convert_setup(&vimconv, NULL, NULL);
11475 vim_free(from);
11476 vim_free(to);
11477#endif
11478}
11479
11480/*
11481 * "indent()" function
11482 */
11483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *argvars;
11486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487{
11488 linenr_T lnum;
11489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495}
11496
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011497/*
11498 * "index()" function
11499 */
11500 static void
11501f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011504{
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 list_T *l;
11506 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011507 long idx = 0;
11508 int ic = FALSE;
11509
11510 rettv->vval.v_number = -1;
11511 if (argvars[0].v_type != VAR_LIST)
11512 {
11513 EMSG(_(e_listreq));
11514 return;
11515 }
11516 l = argvars[0].vval.v_list;
11517 if (l != NULL)
11518 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011519 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011520 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 int error = FALSE;
11523
Bram Moolenaar758711c2005-02-02 23:11:38 +000011524 /* Start at specified item. Use the cached index that list_find()
11525 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011526 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011527 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011528 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 ic = get_tv_number_chk(&argvars[3], &error);
11530 if (error)
11531 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011533
Bram Moolenaar758711c2005-02-02 23:11:38 +000011534 for ( ; item != NULL; item = item->li_next, ++idx)
11535 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011536 {
11537 rettv->vval.v_number = idx;
11538 break;
11539 }
11540 }
11541}
11542
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543static int inputsecret_flag = 0;
11544
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011545static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11546
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011548 * This function is used by f_input() and f_inputdialog() functions. The third
11549 * argument to f_input() specifies the type of completion to use at the
11550 * prompt. The third argument to f_inputdialog() specifies the value to return
11551 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 */
11553 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011554get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011555 typval_T *argvars;
11556 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011557 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 char_u *p = NULL;
11561 int c;
11562 char_u buf[NUMBUFLEN];
11563 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011565 int xp_type = EXPAND_NOTHING;
11566 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011568 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570
11571#ifdef NO_CONSOLE_INPUT
11572 /* While starting up, there is no place to enter text. */
11573 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575#endif
11576
11577 cmd_silent = FALSE; /* Want to see the prompt. */
11578 if (prompt != NULL)
11579 {
11580 /* Only the part of the message after the last NL is considered as
11581 * prompt for the command line */
11582 p = vim_strrchr(prompt, '\n');
11583 if (p == NULL)
11584 p = prompt;
11585 else
11586 {
11587 ++p;
11588 c = *p;
11589 *p = NUL;
11590 msg_start();
11591 msg_clr_eos();
11592 msg_puts_attr(prompt, echo_attr);
11593 msg_didout = FALSE;
11594 msg_starthere();
11595 *p = c;
11596 }
11597 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 if (argvars[1].v_type != VAR_UNKNOWN)
11600 {
11601 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11602 if (defstr != NULL)
11603 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011605 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011606 {
11607 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011608 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011609 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011610
Bram Moolenaar4463f292005-09-25 22:20:24 +000011611 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011612
Bram Moolenaar4463f292005-09-25 22:20:24 +000011613 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11614 if (xp_name == NULL)
11615 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011616
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011617 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011618
Bram Moolenaar4463f292005-09-25 22:20:24 +000011619 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11620 &xp_arg) == FAIL)
11621 return;
11622 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011623 }
11624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 if (defstr != NULL)
11626 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011627 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11628 xp_type, xp_arg);
11629
11630 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011632 /* since the user typed this, no need to wait for return */
11633 need_wait_return = FALSE;
11634 msg_didout = FALSE;
11635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 cmd_silent = cmd_silent_save;
11637}
11638
11639/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011640 * "input()" function
11641 * Also handles inputsecret() when inputsecret is set.
11642 */
11643 static void
11644f_input(argvars, rettv)
11645 typval_T *argvars;
11646 typval_T *rettv;
11647{
11648 get_user_input(argvars, rettv, FALSE);
11649}
11650
11651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 * "inputdialog()" function
11653 */
11654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 typval_T *argvars;
11657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658{
11659#if defined(FEAT_GUI_TEXTDIALOG)
11660 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11661 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11662 {
11663 char_u *message;
11664 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 message = get_tv_string_chk(&argvars[0]);
11668 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011669 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011670 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 else
11672 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 if (message != NULL && defstr != NULL
11674 && do_dialog(VIM_QUESTION, NULL, message,
11675 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 else
11678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 if (message != NULL && defstr != NULL
11680 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011681 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 rettv->vval.v_string = vim_strsave(
11683 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 }
11689 else
11690#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011691 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692}
11693
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011694/*
11695 * "inputlist()" function
11696 */
11697 static void
11698f_inputlist(argvars, rettv)
11699 typval_T *argvars;
11700 typval_T *rettv;
11701{
11702 listitem_T *li;
11703 int selected;
11704 int mouse_used;
11705
11706 rettv->vval.v_number = 0;
11707#ifdef NO_CONSOLE_INPUT
11708 /* While starting up, there is no place to enter text. */
11709 if (no_console_input())
11710 return;
11711#endif
11712 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11713 {
11714 EMSG2(_(e_listarg), "inputlist()");
11715 return;
11716 }
11717
11718 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011719 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011720 lines_left = Rows; /* avoid more prompt */
11721 msg_scroll = TRUE;
11722 msg_clr_eos();
11723
11724 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11725 {
11726 msg_puts(get_tv_string(&li->li_tv));
11727 msg_putchar('\n');
11728 }
11729
11730 /* Ask for choice. */
11731 selected = prompt_for_number(&mouse_used);
11732 if (mouse_used)
11733 selected -= lines_left;
11734
11735 rettv->vval.v_number = selected;
11736}
11737
11738
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11740
11741/*
11742 * "inputrestore()" function
11743 */
11744/*ARGSUSED*/
11745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011746f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011747 typval_T *argvars;
11748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749{
11750 if (ga_userinput.ga_len > 0)
11751 {
11752 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11754 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 }
11757 else if (p_verbose > 1)
11758 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011759 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 }
11762}
11763
11764/*
11765 * "inputsave()" function
11766 */
11767/*ARGSUSED*/
11768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011770 typval_T *argvars;
11771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772{
11773 /* Add an entry to the stack of typehead storage. */
11774 if (ga_grow(&ga_userinput, 1) == OK)
11775 {
11776 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11777 + ga_userinput.ga_len);
11778 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 }
11781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783}
11784
11785/*
11786 * "inputsecret()" function
11787 */
11788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011789f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011790 typval_T *argvars;
11791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792{
11793 ++cmdline_star;
11794 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 --cmdline_star;
11797 --inputsecret_flag;
11798}
11799
11800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011801 * "insert()" function
11802 */
11803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011805 typval_T *argvars;
11806 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011807{
11808 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011809 listitem_T *item;
11810 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011813 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011814 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011815 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011816 else if ((l = argvars[0].vval.v_list) != NULL
11817 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011818 {
11819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 before = get_tv_number_chk(&argvars[2], &error);
11821 if (error)
11822 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011823
Bram Moolenaar758711c2005-02-02 23:11:38 +000011824 if (before == l->lv_len)
11825 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826 else
11827 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011828 item = list_find(l, before);
11829 if (item == NULL)
11830 {
11831 EMSGN(_(e_listidx), before);
11832 l = NULL;
11833 }
11834 }
11835 if (l != NULL)
11836 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011837 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011838 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011839 }
11840 }
11841}
11842
11843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 * "isdirectory()" function
11845 */
11846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852}
11853
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011854/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011855 * "islocked()" function
11856 */
11857 static void
11858f_islocked(argvars, rettv)
11859 typval_T *argvars;
11860 typval_T *rettv;
11861{
11862 lval_T lv;
11863 char_u *end;
11864 dictitem_T *di;
11865
11866 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011867 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11868 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011869 if (end != NULL && lv.ll_name != NULL)
11870 {
11871 if (*end != NUL)
11872 EMSG(_(e_trailing));
11873 else
11874 {
11875 if (lv.ll_tv == NULL)
11876 {
11877 if (check_changedtick(lv.ll_name))
11878 rettv->vval.v_number = 1; /* always locked */
11879 else
11880 {
11881 di = find_var(lv.ll_name, NULL);
11882 if (di != NULL)
11883 {
11884 /* Consider a variable locked when:
11885 * 1. the variable itself is locked
11886 * 2. the value of the variable is locked.
11887 * 3. the List or Dict value is locked.
11888 */
11889 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11890 || tv_islocked(&di->di_tv));
11891 }
11892 }
11893 }
11894 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011895 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011896 else if (lv.ll_newkey != NULL)
11897 EMSG2(_(e_dictkey), lv.ll_newkey);
11898 else if (lv.ll_list != NULL)
11899 /* List item. */
11900 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11901 else
11902 /* Dictionary item. */
11903 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11904 }
11905 }
11906
11907 clear_lval(&lv);
11908}
11909
Bram Moolenaar33570922005-01-25 22:26:29 +000011910static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011911
11912/*
11913 * Turn a dict into a list:
11914 * "what" == 0: list of keys
11915 * "what" == 1: list of values
11916 * "what" == 2: list of items
11917 */
11918 static void
11919dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011920 typval_T *argvars;
11921 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011922 int what;
11923{
Bram Moolenaar33570922005-01-25 22:26:29 +000011924 list_T *l2;
11925 dictitem_T *di;
11926 hashitem_T *hi;
11927 listitem_T *li;
11928 listitem_T *li2;
11929 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011930 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011931
11932 rettv->vval.v_number = 0;
11933 if (argvars[0].v_type != VAR_DICT)
11934 {
11935 EMSG(_(e_dictreq));
11936 return;
11937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011938 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939 return;
11940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011941 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011942 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011944 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011946 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011949 --todo;
11950 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011951
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011952 li = listitem_alloc();
11953 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011954 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011955 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011956
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011957 if (what == 0)
11958 {
11959 /* keys() */
11960 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011961 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011962 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11963 }
11964 else if (what == 1)
11965 {
11966 /* values() */
11967 copy_tv(&di->di_tv, &li->li_tv);
11968 }
11969 else
11970 {
11971 /* items() */
11972 l2 = list_alloc();
11973 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011974 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 li->li_tv.vval.v_list = l2;
11976 if (l2 == NULL)
11977 break;
11978 ++l2->lv_refcount;
11979
11980 li2 = listitem_alloc();
11981 if (li2 == NULL)
11982 break;
11983 list_append(l2, li2);
11984 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011985 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011986 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11987
11988 li2 = listitem_alloc();
11989 if (li2 == NULL)
11990 break;
11991 list_append(l2, li2);
11992 copy_tv(&di->di_tv, &li2->li_tv);
11993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011994 }
11995 }
11996}
11997
11998/*
11999 * "items(dict)" function
12000 */
12001 static void
12002f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012003 typval_T *argvars;
12004 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012005{
12006 dict_list(argvars, rettv, 2);
12007}
12008
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012010 * "join()" function
12011 */
12012 static void
12013f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012014 typval_T *argvars;
12015 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012016{
12017 garray_T ga;
12018 char_u *sep;
12019
12020 rettv->vval.v_number = 0;
12021 if (argvars[0].v_type != VAR_LIST)
12022 {
12023 EMSG(_(e_listreq));
12024 return;
12025 }
12026 if (argvars[0].vval.v_list == NULL)
12027 return;
12028 if (argvars[1].v_type == VAR_UNKNOWN)
12029 sep = (char_u *)" ";
12030 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012032
12033 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034
12035 if (sep != NULL)
12036 {
12037 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012038 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 ga_append(&ga, NUL);
12040 rettv->vval.v_string = (char_u *)ga.ga_data;
12041 }
12042 else
12043 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044}
12045
12046/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012047 * "keys()" function
12048 */
12049 static void
12050f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012051 typval_T *argvars;
12052 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012053{
12054 dict_list(argvars, rettv, 0);
12055}
12056
12057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 * "last_buffer_nr()" function.
12059 */
12060/*ARGSUSED*/
12061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012063 typval_T *argvars;
12064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065{
12066 int n = 0;
12067 buf_T *buf;
12068
12069 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12070 if (n < buf->b_fnum)
12071 n = buf->b_fnum;
12072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012074}
12075
12076/*
12077 * "len()" function
12078 */
12079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 typval_T *argvars;
12082 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012083{
12084 switch (argvars[0].v_type)
12085 {
12086 case VAR_STRING:
12087 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_number = (varnumber_T)STRLEN(
12089 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012090 break;
12091 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012093 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012094 case VAR_DICT:
12095 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12096 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012097 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012098 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 break;
12100 }
12101}
12102
Bram Moolenaar33570922005-01-25 22:26:29 +000012103static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012104
12105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012107 typval_T *argvars;
12108 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012109 int type;
12110{
12111#ifdef FEAT_LIBCALL
12112 char_u *string_in;
12113 char_u **string_result;
12114 int nr_result;
12115#endif
12116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012118 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012122
12123 if (check_restricted() || check_secure())
12124 return;
12125
12126#ifdef FEAT_LIBCALL
12127 /* The first two args must be strings, otherwise its meaningless */
12128 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12129 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012130 string_in = NULL;
12131 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012132 string_in = argvars[2].vval.v_string;
12133 if (type == VAR_NUMBER)
12134 string_result = NULL;
12135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012137 if (mch_libcall(argvars[0].vval.v_string,
12138 argvars[1].vval.v_string,
12139 string_in,
12140 argvars[2].vval.v_number,
12141 string_result,
12142 &nr_result) == OK
12143 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012145 }
12146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147}
12148
12149/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012150 * "libcall()" function
12151 */
12152 static void
12153f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012154 typval_T *argvars;
12155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156{
12157 libcall_common(argvars, rettv, VAR_STRING);
12158}
12159
12160/*
12161 * "libcallnr()" function
12162 */
12163 static void
12164f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012165 typval_T *argvars;
12166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012167{
12168 libcall_common(argvars, rettv, VAR_NUMBER);
12169}
12170
12171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 * "line(string)" function
12173 */
12174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012176 typval_T *argvars;
12177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178{
12179 linenr_T lnum = 0;
12180 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012181 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012183 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 if (fp != NULL)
12185 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187}
12188
12189/*
12190 * "line2byte(lnum)" function
12191 */
12192/*ARGSUSED*/
12193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012195 typval_T *argvars;
12196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197{
12198#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#else
12201 linenr_T lnum;
12202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12208 if (rettv->vval.v_number >= 0)
12209 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210#endif
12211}
12212
12213/*
12214 * "lispindent(lnum)" function
12215 */
12216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012218 typval_T *argvars;
12219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220{
12221#ifdef FEAT_LISP
12222 pos_T pos;
12223 linenr_T lnum;
12224
12225 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12228 {
12229 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 curwin->w_cursor = pos;
12232 }
12233 else
12234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236}
12237
12238/*
12239 * "localtime()" function
12240 */
12241/*ARGSUSED*/
12242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 typval_T *argvars;
12245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012247 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248}
12249
Bram Moolenaar33570922005-01-25 22:26:29 +000012250static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
12252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012254 typval_T *argvars;
12255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 int exact;
12257{
12258 char_u *keys;
12259 char_u *which;
12260 char_u buf[NUMBUFLEN];
12261 char_u *keys_buf = NULL;
12262 char_u *rhs;
12263 int mode;
12264 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012265 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266
12267 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->v_type = VAR_STRING;
12269 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012271 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 if (*keys == NUL)
12273 return;
12274
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012277 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012278 if (argvars[2].v_type != VAR_UNKNOWN)
12279 abbr = get_tv_number(&argvars[2]);
12280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 else
12282 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012283 if (which == NULL)
12284 return;
12285
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 mode = get_map_mode(&which, 0);
12287
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012288 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012289 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 vim_free(keys_buf);
12291 if (rhs != NULL)
12292 {
12293 ga_init(&ga);
12294 ga.ga_itemsize = 1;
12295 ga.ga_growsize = 40;
12296
12297 while (*rhs != NUL)
12298 ga_concat(&ga, str2special(&rhs, FALSE));
12299
12300 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 }
12303}
12304
12305/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012306 * "map()" function
12307 */
12308 static void
12309f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012310 typval_T *argvars;
12311 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012312{
12313 filter_map(argvars, rettv, TRUE);
12314}
12315
12316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012317 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 */
12319 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012320f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012321 typval_T *argvars;
12322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012324 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325}
12326
12327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 */
12330 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *argvars;
12333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336}
12337
Bram Moolenaar33570922005-01-25 22:26:29 +000012338static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339
12340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012342 typval_T *argvars;
12343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 int type;
12345{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012346 char_u *str = NULL;
12347 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 char_u *pat;
12349 regmatch_T regmatch;
12350 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012351 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 char_u *save_cpo;
12353 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012354 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012355 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012356 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012357 list_T *l = NULL;
12358 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012359 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012360 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361
12362 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12363 save_cpo = p_cpo;
12364 p_cpo = (char_u *)"";
12365
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012366 rettv->vval.v_number = -1;
12367 if (type == 3)
12368 {
12369 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012370 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012371 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012372 }
12373 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012375 rettv->v_type = VAR_STRING;
12376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012379 if (argvars[0].v_type == VAR_LIST)
12380 {
12381 if ((l = argvars[0].vval.v_list) == NULL)
12382 goto theend;
12383 li = l->lv_first;
12384 }
12385 else
12386 expr = str = get_tv_string(&argvars[0]);
12387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12389 if (pat == NULL)
12390 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012391
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012392 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012394 int error = FALSE;
12395
12396 start = get_tv_number_chk(&argvars[2], &error);
12397 if (error)
12398 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012399 if (l != NULL)
12400 {
12401 li = list_find(l, start);
12402 if (li == NULL)
12403 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012404 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012405 }
12406 else
12407 {
12408 if (start < 0)
12409 start = 0;
12410 if (start > (long)STRLEN(str))
12411 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012412 /* When "count" argument is there ignore matches before "start",
12413 * otherwise skip part of the string. Differs when pattern is "^"
12414 * or "\<". */
12415 if (argvars[3].v_type != VAR_UNKNOWN)
12416 startcol = start;
12417 else
12418 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012419 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012420
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012421 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012422 nth = get_tv_number_chk(&argvars[3], &error);
12423 if (error)
12424 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 }
12426
12427 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12428 if (regmatch.regprog != NULL)
12429 {
12430 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012431
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012432 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012433 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012434 if (l != NULL)
12435 {
12436 if (li == NULL)
12437 {
12438 match = FALSE;
12439 break;
12440 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012441 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012442 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012443 if (str == NULL)
12444 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012445 }
12446
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012447 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012448
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012449 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012450 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012451 if (l == NULL && !match)
12452 break;
12453
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012454 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012455 if (l != NULL)
12456 {
12457 li = li->li_next;
12458 ++idx;
12459 }
12460 else
12461 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012462#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012463 startcol = (colnr_T)(regmatch.startp[0]
12464 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012465#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012466 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012467#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012468 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012469 }
12470
12471 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012473 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012474 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012475 int i;
12476
12477 /* return list with matched string and submatches */
12478 for (i = 0; i < NSUBEXP; ++i)
12479 {
12480 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012481 {
12482 if (list_append_string(rettv->vval.v_list,
12483 (char_u *)"", 0) == FAIL)
12484 break;
12485 }
12486 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012487 regmatch.startp[i],
12488 (int)(regmatch.endp[i] - regmatch.startp[i]))
12489 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012490 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012491 }
12492 }
12493 else if (type == 2)
12494 {
12495 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012496 if (l != NULL)
12497 copy_tv(&li->li_tv, rettv);
12498 else
12499 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012501 }
12502 else if (l != NULL)
12503 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 else
12505 {
12506 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 (varnumber_T)(regmatch.startp[0] - str);
12509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012512 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 }
12514 }
12515 vim_free(regmatch.regprog);
12516 }
12517
12518theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012519 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520 p_cpo = save_cpo;
12521}
12522
12523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 * "match()" function
12525 */
12526 static void
12527f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012528 typval_T *argvars;
12529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530{
12531 find_some_match(argvars, rettv, 1);
12532}
12533
12534/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012535 * "matchadd()" function
12536 */
12537 static void
12538f_matchadd(argvars, rettv)
12539 typval_T *argvars;
12540 typval_T *rettv;
12541{
12542#ifdef FEAT_SEARCH_EXTRA
12543 char_u buf[NUMBUFLEN];
12544 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12545 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12546 int prio = 10; /* default priority */
12547 int id = -1;
12548 int error = FALSE;
12549
12550 rettv->vval.v_number = -1;
12551
12552 if (grp == NULL || pat == NULL)
12553 return;
12554 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012555 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012556 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012557 if (argvars[3].v_type != VAR_UNKNOWN)
12558 id = get_tv_number_chk(&argvars[3], &error);
12559 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012560 if (error == TRUE)
12561 return;
12562 if (id >= 1 && id <= 3)
12563 {
12564 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12565 return;
12566 }
12567
12568 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12569#endif
12570}
12571
12572/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012573 * "matcharg()" function
12574 */
12575 static void
12576f_matcharg(argvars, rettv)
12577 typval_T *argvars;
12578 typval_T *rettv;
12579{
12580 if (rettv_list_alloc(rettv) == OK)
12581 {
12582#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012583 int id = get_tv_number(&argvars[0]);
12584 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012585
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012586 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012587 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012588 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12589 {
12590 list_append_string(rettv->vval.v_list,
12591 syn_id2name(m->hlg_id), -1);
12592 list_append_string(rettv->vval.v_list, m->pattern, -1);
12593 }
12594 else
12595 {
12596 list_append_string(rettv->vval.v_list, NUL, -1);
12597 list_append_string(rettv->vval.v_list, NUL, -1);
12598 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012599 }
12600#endif
12601 }
12602}
12603
12604/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012605 * "matchdelete()" function
12606 */
12607 static void
12608f_matchdelete(argvars, rettv)
12609 typval_T *argvars;
12610 typval_T *rettv;
12611{
12612#ifdef FEAT_SEARCH_EXTRA
12613 rettv->vval.v_number = match_delete(curwin,
12614 (int)get_tv_number(&argvars[0]), TRUE);
12615#endif
12616}
12617
12618/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 * "matchend()" function
12620 */
12621 static void
12622f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 typval_T *argvars;
12624 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012625{
12626 find_some_match(argvars, rettv, 0);
12627}
12628
12629/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012630 * "matchlist()" function
12631 */
12632 static void
12633f_matchlist(argvars, rettv)
12634 typval_T *argvars;
12635 typval_T *rettv;
12636{
12637 find_some_match(argvars, rettv, 3);
12638}
12639
12640/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641 * "matchstr()" function
12642 */
12643 static void
12644f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012645 typval_T *argvars;
12646 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647{
12648 find_some_match(argvars, rettv, 2);
12649}
12650
Bram Moolenaar33570922005-01-25 22:26:29 +000012651static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012652
12653 static void
12654max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012655 typval_T *argvars;
12656 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012657 int domax;
12658{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012659 long n = 0;
12660 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012662
12663 if (argvars[0].v_type == VAR_LIST)
12664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012665 list_T *l;
12666 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012667
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012668 l = argvars[0].vval.v_list;
12669 if (l != NULL)
12670 {
12671 li = l->lv_first;
12672 if (li != NULL)
12673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012675 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012676 {
12677 li = li->li_next;
12678 if (li == NULL)
12679 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012680 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012681 if (domax ? i > n : i < n)
12682 n = i;
12683 }
12684 }
12685 }
12686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012687 else if (argvars[0].v_type == VAR_DICT)
12688 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012689 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012690 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012692 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012693
12694 d = argvars[0].vval.v_dict;
12695 if (d != NULL)
12696 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012697 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012700 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012702 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012704 if (first)
12705 {
12706 n = i;
12707 first = FALSE;
12708 }
12709 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012710 n = i;
12711 }
12712 }
12713 }
12714 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012715 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012716 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012718}
12719
12720/*
12721 * "max()" function
12722 */
12723 static void
12724f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 typval_T *argvars;
12726 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012727{
12728 max_min(argvars, rettv, TRUE);
12729}
12730
12731/*
12732 * "min()" function
12733 */
12734 static void
12735f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 typval_T *argvars;
12737 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012738{
12739 max_min(argvars, rettv, FALSE);
12740}
12741
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012742static int mkdir_recurse __ARGS((char_u *dir, int prot));
12743
12744/*
12745 * Create the directory in which "dir" is located, and higher levels when
12746 * needed.
12747 */
12748 static int
12749mkdir_recurse(dir, prot)
12750 char_u *dir;
12751 int prot;
12752{
12753 char_u *p;
12754 char_u *updir;
12755 int r = FAIL;
12756
12757 /* Get end of directory name in "dir".
12758 * We're done when it's "/" or "c:/". */
12759 p = gettail_sep(dir);
12760 if (p <= get_past_head(dir))
12761 return OK;
12762
12763 /* If the directory exists we're done. Otherwise: create it.*/
12764 updir = vim_strnsave(dir, (int)(p - dir));
12765 if (updir == NULL)
12766 return FAIL;
12767 if (mch_isdir(updir))
12768 r = OK;
12769 else if (mkdir_recurse(updir, prot) == OK)
12770 r = vim_mkdir_emsg(updir, prot);
12771 vim_free(updir);
12772 return r;
12773}
12774
12775#ifdef vim_mkdir
12776/*
12777 * "mkdir()" function
12778 */
12779 static void
12780f_mkdir(argvars, rettv)
12781 typval_T *argvars;
12782 typval_T *rettv;
12783{
12784 char_u *dir;
12785 char_u buf[NUMBUFLEN];
12786 int prot = 0755;
12787
12788 rettv->vval.v_number = FAIL;
12789 if (check_restricted() || check_secure())
12790 return;
12791
12792 dir = get_tv_string_buf(&argvars[0], buf);
12793 if (argvars[1].v_type != VAR_UNKNOWN)
12794 {
12795 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012796 prot = get_tv_number_chk(&argvars[2], NULL);
12797 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012798 mkdir_recurse(dir, prot);
12799 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012801}
12802#endif
12803
Bram Moolenaar0d660222005-01-07 21:51:51 +000012804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 * "mode()" function
12806 */
12807/*ARGSUSED*/
12808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012810 typval_T *argvars;
12811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812{
12813 char_u buf[2];
12814
12815#ifdef FEAT_VISUAL
12816 if (VIsual_active)
12817 {
12818 if (VIsual_select)
12819 buf[0] = VIsual_mode + 's' - 'v';
12820 else
12821 buf[0] = VIsual_mode;
12822 }
12823 else
12824#endif
12825 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12826 buf[0] = 'r';
12827 else if (State & INSERT)
12828 {
12829 if (State & REPLACE_FLAG)
12830 buf[0] = 'R';
12831 else
12832 buf[0] = 'i';
12833 }
12834 else if (State & CMDLINE)
12835 buf[0] = 'c';
12836 else
12837 buf[0] = 'n';
12838
12839 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 rettv->vval.v_string = vim_strsave(buf);
12841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842}
12843
12844/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012845 * "nextnonblank()" function
12846 */
12847 static void
12848f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012851{
12852 linenr_T lnum;
12853
12854 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012857 {
12858 lnum = 0;
12859 break;
12860 }
12861 if (*skipwhite(ml_get(lnum)) != NUL)
12862 break;
12863 }
12864 rettv->vval.v_number = lnum;
12865}
12866
12867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 * "nr2char()" function
12869 */
12870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012872 typval_T *argvars;
12873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874{
12875 char_u buf[NUMBUFLEN];
12876
12877#ifdef FEAT_MBYTE
12878 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 else
12881#endif
12882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 buf[1] = NUL;
12885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->v_type = VAR_STRING;
12887 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012888}
12889
12890/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012891 * "pathshorten()" function
12892 */
12893 static void
12894f_pathshorten(argvars, rettv)
12895 typval_T *argvars;
12896 typval_T *rettv;
12897{
12898 char_u *p;
12899
12900 rettv->v_type = VAR_STRING;
12901 p = get_tv_string_chk(&argvars[0]);
12902 if (p == NULL)
12903 rettv->vval.v_string = NULL;
12904 else
12905 {
12906 p = vim_strsave(p);
12907 rettv->vval.v_string = p;
12908 if (p != NULL)
12909 shorten_dir(p);
12910 }
12911}
12912
12913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914 * "prevnonblank()" function
12915 */
12916 static void
12917f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 typval_T *argvars;
12919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012920{
12921 linenr_T lnum;
12922
12923 lnum = get_tv_lnum(argvars);
12924 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12925 lnum = 0;
12926 else
12927 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12928 --lnum;
12929 rettv->vval.v_number = lnum;
12930}
12931
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012932#ifdef HAVE_STDARG_H
12933/* This dummy va_list is here because:
12934 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12935 * - locally in the function results in a "used before set" warning
12936 * - using va_start() to initialize it gives "function with fixed args" error */
12937static va_list ap;
12938#endif
12939
Bram Moolenaar8c711452005-01-14 21:53:12 +000012940/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012941 * "printf()" function
12942 */
12943 static void
12944f_printf(argvars, rettv)
12945 typval_T *argvars;
12946 typval_T *rettv;
12947{
12948 rettv->v_type = VAR_STRING;
12949 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012950#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012951 {
12952 char_u buf[NUMBUFLEN];
12953 int len;
12954 char_u *s;
12955 int saved_did_emsg = did_emsg;
12956 char *fmt;
12957
12958 /* Get the required length, allocate the buffer and do it for real. */
12959 did_emsg = FALSE;
12960 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012961 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012962 if (!did_emsg)
12963 {
12964 s = alloc(len + 1);
12965 if (s != NULL)
12966 {
12967 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012968 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012969 }
12970 }
12971 did_emsg |= saved_did_emsg;
12972 }
12973#endif
12974}
12975
12976/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012977 * "pumvisible()" function
12978 */
12979/*ARGSUSED*/
12980 static void
12981f_pumvisible(argvars, rettv)
12982 typval_T *argvars;
12983 typval_T *rettv;
12984{
12985 rettv->vval.v_number = 0;
12986#ifdef FEAT_INS_EXPAND
12987 if (pum_visible())
12988 rettv->vval.v_number = 1;
12989#endif
12990}
12991
12992/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012993 * "range()" function
12994 */
12995 static void
12996f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012997 typval_T *argvars;
12998 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012999{
13000 long start;
13001 long end;
13002 long stride = 1;
13003 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013004 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007 if (argvars[1].v_type == VAR_UNKNOWN)
13008 {
13009 end = start - 1;
13010 start = 0;
13011 }
13012 else
13013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013016 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013017 }
13018
13019 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 if (error)
13021 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013022 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013023 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013024 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013025 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013026 else
13027 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013028 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013029 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013030 if (list_append_number(rettv->vval.v_list,
13031 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013032 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013033 }
13034}
13035
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013036/*
13037 * "readfile()" function
13038 */
13039 static void
13040f_readfile(argvars, rettv)
13041 typval_T *argvars;
13042 typval_T *rettv;
13043{
13044 int binary = FALSE;
13045 char_u *fname;
13046 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013047 listitem_T *li;
13048#define FREAD_SIZE 200 /* optimized for text lines */
13049 char_u buf[FREAD_SIZE];
13050 int readlen; /* size of last fread() */
13051 int buflen; /* nr of valid chars in buf[] */
13052 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13053 int tolist; /* first byte in buf[] still to be put in list */
13054 int chop; /* how many CR to chop off */
13055 char_u *prev = NULL; /* previously read bytes, if any */
13056 int prevlen = 0; /* length of "prev" if not NULL */
13057 char_u *s;
13058 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013059 long maxline = MAXLNUM;
13060 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013061
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013062 if (argvars[1].v_type != VAR_UNKNOWN)
13063 {
13064 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13065 binary = TRUE;
13066 if (argvars[2].v_type != VAR_UNKNOWN)
13067 maxline = get_tv_number(&argvars[2]);
13068 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013069
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013070 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013071 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013072
13073 /* Always open the file in binary mode, library functions have a mind of
13074 * their own about CR-LF conversion. */
13075 fname = get_tv_string(&argvars[0]);
13076 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13077 {
13078 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13079 return;
13080 }
13081
13082 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013083 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013084 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013085 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013086 buflen = filtd + readlen;
13087 tolist = 0;
13088 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13089 {
13090 if (buf[filtd] == '\n' || readlen <= 0)
13091 {
13092 /* Only when in binary mode add an empty list item when the
13093 * last line ends in a '\n'. */
13094 if (!binary && readlen == 0 && filtd == 0)
13095 break;
13096
13097 /* Found end-of-line or end-of-file: add a text line to the
13098 * list. */
13099 chop = 0;
13100 if (!binary)
13101 while (filtd - chop - 1 >= tolist
13102 && buf[filtd - chop - 1] == '\r')
13103 ++chop;
13104 len = filtd - tolist - chop;
13105 if (prev == NULL)
13106 s = vim_strnsave(buf + tolist, len);
13107 else
13108 {
13109 s = alloc((unsigned)(prevlen + len + 1));
13110 if (s != NULL)
13111 {
13112 mch_memmove(s, prev, prevlen);
13113 vim_free(prev);
13114 prev = NULL;
13115 mch_memmove(s + prevlen, buf + tolist, len);
13116 s[prevlen + len] = NUL;
13117 }
13118 }
13119 tolist = filtd + 1;
13120
13121 li = listitem_alloc();
13122 if (li == NULL)
13123 {
13124 vim_free(s);
13125 break;
13126 }
13127 li->li_tv.v_type = VAR_STRING;
13128 li->li_tv.v_lock = 0;
13129 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013130 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013131
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013132 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013133 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013134 if (readlen <= 0)
13135 break;
13136 }
13137 else if (buf[filtd] == NUL)
13138 buf[filtd] = '\n';
13139 }
13140 if (readlen <= 0)
13141 break;
13142
13143 if (tolist == 0)
13144 {
13145 /* "buf" is full, need to move text to an allocated buffer */
13146 if (prev == NULL)
13147 {
13148 prev = vim_strnsave(buf, buflen);
13149 prevlen = buflen;
13150 }
13151 else
13152 {
13153 s = alloc((unsigned)(prevlen + buflen));
13154 if (s != NULL)
13155 {
13156 mch_memmove(s, prev, prevlen);
13157 mch_memmove(s + prevlen, buf, buflen);
13158 vim_free(prev);
13159 prev = s;
13160 prevlen += buflen;
13161 }
13162 }
13163 filtd = 0;
13164 }
13165 else
13166 {
13167 mch_memmove(buf, buf + tolist, buflen - tolist);
13168 filtd -= tolist;
13169 }
13170 }
13171
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013172 /*
13173 * For a negative line count use only the lines at the end of the file,
13174 * free the rest.
13175 */
13176 if (maxline < 0)
13177 while (cnt > -maxline)
13178 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013179 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013180 --cnt;
13181 }
13182
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013183 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013184 fclose(fd);
13185}
13186
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013187#if defined(FEAT_RELTIME)
13188static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13189
13190/*
13191 * Convert a List to proftime_T.
13192 * Return FAIL when there is something wrong.
13193 */
13194 static int
13195list2proftime(arg, tm)
13196 typval_T *arg;
13197 proftime_T *tm;
13198{
13199 long n1, n2;
13200 int error = FALSE;
13201
13202 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13203 || arg->vval.v_list->lv_len != 2)
13204 return FAIL;
13205 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13206 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13207# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013208 tm->HighPart = n1;
13209 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013210# else
13211 tm->tv_sec = n1;
13212 tm->tv_usec = n2;
13213# endif
13214 return error ? FAIL : OK;
13215}
13216#endif /* FEAT_RELTIME */
13217
13218/*
13219 * "reltime()" function
13220 */
13221 static void
13222f_reltime(argvars, rettv)
13223 typval_T *argvars;
13224 typval_T *rettv;
13225{
13226#ifdef FEAT_RELTIME
13227 proftime_T res;
13228 proftime_T start;
13229
13230 if (argvars[0].v_type == VAR_UNKNOWN)
13231 {
13232 /* No arguments: get current time. */
13233 profile_start(&res);
13234 }
13235 else if (argvars[1].v_type == VAR_UNKNOWN)
13236 {
13237 if (list2proftime(&argvars[0], &res) == FAIL)
13238 return;
13239 profile_end(&res);
13240 }
13241 else
13242 {
13243 /* Two arguments: compute the difference. */
13244 if (list2proftime(&argvars[0], &start) == FAIL
13245 || list2proftime(&argvars[1], &res) == FAIL)
13246 return;
13247 profile_sub(&res, &start);
13248 }
13249
13250 if (rettv_list_alloc(rettv) == OK)
13251 {
13252 long n1, n2;
13253
13254# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013255 n1 = res.HighPart;
13256 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013257# else
13258 n1 = res.tv_sec;
13259 n2 = res.tv_usec;
13260# endif
13261 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13262 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13263 }
13264#endif
13265}
13266
13267/*
13268 * "reltimestr()" function
13269 */
13270 static void
13271f_reltimestr(argvars, rettv)
13272 typval_T *argvars;
13273 typval_T *rettv;
13274{
13275#ifdef FEAT_RELTIME
13276 proftime_T tm;
13277#endif
13278
13279 rettv->v_type = VAR_STRING;
13280 rettv->vval.v_string = NULL;
13281#ifdef FEAT_RELTIME
13282 if (list2proftime(&argvars[0], &tm) == OK)
13283 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13284#endif
13285}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013286
Bram Moolenaar0d660222005-01-07 21:51:51 +000013287#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13288static void make_connection __ARGS((void));
13289static int check_connection __ARGS((void));
13290
13291 static void
13292make_connection()
13293{
13294 if (X_DISPLAY == NULL
13295# ifdef FEAT_GUI
13296 && !gui.in_use
13297# endif
13298 )
13299 {
13300 x_force_connect = TRUE;
13301 setup_term_clip();
13302 x_force_connect = FALSE;
13303 }
13304}
13305
13306 static int
13307check_connection()
13308{
13309 make_connection();
13310 if (X_DISPLAY == NULL)
13311 {
13312 EMSG(_("E240: No connection to Vim server"));
13313 return FAIL;
13314 }
13315 return OK;
13316}
13317#endif
13318
13319#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013320static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013321
13322 static void
13323remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 typval_T *argvars;
13325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013326 int expr;
13327{
13328 char_u *server_name;
13329 char_u *keys;
13330 char_u *r = NULL;
13331 char_u buf[NUMBUFLEN];
13332# ifdef WIN32
13333 HWND w;
13334# else
13335 Window w;
13336# endif
13337
13338 if (check_restricted() || check_secure())
13339 return;
13340
13341# ifdef FEAT_X11
13342 if (check_connection() == FAIL)
13343 return;
13344# endif
13345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013346 server_name = get_tv_string_chk(&argvars[0]);
13347 if (server_name == NULL)
13348 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349 keys = get_tv_string_buf(&argvars[1], buf);
13350# ifdef WIN32
13351 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13352# else
13353 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13354 < 0)
13355# endif
13356 {
13357 if (r != NULL)
13358 EMSG(r); /* sending worked but evaluation failed */
13359 else
13360 EMSG2(_("E241: Unable to send to %s"), server_name);
13361 return;
13362 }
13363
13364 rettv->vval.v_string = r;
13365
13366 if (argvars[2].v_type != VAR_UNKNOWN)
13367 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013369 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013371
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013372 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 v.di_tv.v_type = VAR_STRING;
13374 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375 idvar = get_tv_string_chk(&argvars[2]);
13376 if (idvar != NULL)
13377 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013379 }
13380}
13381#endif
13382
13383/*
13384 * "remote_expr()" function
13385 */
13386/*ARGSUSED*/
13387 static void
13388f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013389 typval_T *argvars;
13390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013391{
13392 rettv->v_type = VAR_STRING;
13393 rettv->vval.v_string = NULL;
13394#ifdef FEAT_CLIENTSERVER
13395 remote_common(argvars, rettv, TRUE);
13396#endif
13397}
13398
13399/*
13400 * "remote_foreground()" function
13401 */
13402/*ARGSUSED*/
13403 static void
13404f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013405 typval_T *argvars;
13406 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013407{
13408 rettv->vval.v_number = 0;
13409#ifdef FEAT_CLIENTSERVER
13410# ifdef WIN32
13411 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 {
13413 char_u *server_name = get_tv_string_chk(&argvars[0]);
13414
13415 if (server_name != NULL)
13416 serverForeground(server_name);
13417 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013418# else
13419 /* Send a foreground() expression to the server. */
13420 argvars[1].v_type = VAR_STRING;
13421 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13422 argvars[2].v_type = VAR_UNKNOWN;
13423 remote_common(argvars, rettv, TRUE);
13424 vim_free(argvars[1].vval.v_string);
13425# endif
13426#endif
13427}
13428
13429/*ARGSUSED*/
13430 static void
13431f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 typval_T *argvars;
13433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434{
13435#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013437 char_u *s = NULL;
13438# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013439 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013440# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013441 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013442
13443 if (check_restricted() || check_secure())
13444 {
13445 rettv->vval.v_number = -1;
13446 return;
13447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 serverid = get_tv_string_chk(&argvars[0]);
13449 if (serverid == NULL)
13450 {
13451 rettv->vval.v_number = -1;
13452 return; /* type error; errmsg already given */
13453 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013454# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013455 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456 if (n == 0)
13457 rettv->vval.v_number = -1;
13458 else
13459 {
13460 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13461 rettv->vval.v_number = (s != NULL);
13462 }
13463# else
13464 rettv->vval.v_number = 0;
13465 if (check_connection() == FAIL)
13466 return;
13467
13468 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013470# endif
13471
13472 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474 char_u *retvar;
13475
Bram Moolenaar33570922005-01-25 22:26:29 +000013476 v.di_tv.v_type = VAR_STRING;
13477 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 retvar = get_tv_string_chk(&argvars[1]);
13479 if (retvar != NULL)
13480 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013481 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013482 }
13483#else
13484 rettv->vval.v_number = -1;
13485#endif
13486}
13487
13488/*ARGSUSED*/
13489 static void
13490f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013491 typval_T *argvars;
13492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013493{
13494 char_u *r = NULL;
13495
13496#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 char_u *serverid = get_tv_string_chk(&argvars[0]);
13498
13499 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013500 {
13501# ifdef WIN32
13502 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013503 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013504
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013505 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013506 if (n != 0)
13507 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13508 if (r == NULL)
13509# else
13510 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013512# endif
13513 EMSG(_("E277: Unable to read a server reply"));
13514 }
13515#endif
13516 rettv->v_type = VAR_STRING;
13517 rettv->vval.v_string = r;
13518}
13519
13520/*
13521 * "remote_send()" function
13522 */
13523/*ARGSUSED*/
13524 static void
13525f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 typval_T *argvars;
13527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013528{
13529 rettv->v_type = VAR_STRING;
13530 rettv->vval.v_string = NULL;
13531#ifdef FEAT_CLIENTSERVER
13532 remote_common(argvars, rettv, FALSE);
13533#endif
13534}
13535
13536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013537 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013538 */
13539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *argvars;
13542 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013543{
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 list_T *l;
13545 listitem_T *item, *item2;
13546 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013547 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013548 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013549 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013550 dict_T *d;
13551 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013552
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013553 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013554 if (argvars[0].v_type == VAR_DICT)
13555 {
13556 if (argvars[2].v_type != VAR_UNKNOWN)
13557 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013558 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013559 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 key = get_tv_string_chk(&argvars[1]);
13562 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 di = dict_find(d, key, -1);
13565 if (di == NULL)
13566 EMSG2(_(e_dictkey), key);
13567 else
13568 {
13569 *rettv = di->di_tv;
13570 init_tv(&di->di_tv);
13571 dictitem_remove(d, di);
13572 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013573 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013574 }
13575 }
13576 else if (argvars[0].v_type != VAR_LIST)
13577 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013578 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013579 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 int error = FALSE;
13582
13583 idx = get_tv_number_chk(&argvars[1], &error);
13584 if (error)
13585 ; /* type error: do nothing, errmsg already given */
13586 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013587 EMSGN(_(e_listidx), idx);
13588 else
13589 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013590 if (argvars[2].v_type == VAR_UNKNOWN)
13591 {
13592 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013593 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013594 *rettv = item->li_tv;
13595 vim_free(item);
13596 }
13597 else
13598 {
13599 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 end = get_tv_number_chk(&argvars[2], &error);
13601 if (error)
13602 ; /* type error: do nothing */
13603 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013604 EMSGN(_(e_listidx), end);
13605 else
13606 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013607 int cnt = 0;
13608
13609 for (li = item; li != NULL; li = li->li_next)
13610 {
13611 ++cnt;
13612 if (li == item2)
13613 break;
13614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013615 if (li == NULL) /* didn't find "item2" after "item" */
13616 EMSG(_(e_invrange));
13617 else
13618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013619 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013620 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013621 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013622 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013623 l->lv_first = item;
13624 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013625 item->li_prev = NULL;
13626 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013627 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013628 }
13629 }
13630 }
13631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013632 }
13633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
13637 * "rename({from}, {to})" function
13638 */
13639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013641 typval_T *argvars;
13642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643{
13644 char_u buf[NUMBUFLEN];
13645
13646 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13650 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
13653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013654 * "repeat()" function
13655 */
13656/*ARGSUSED*/
13657 static void
13658f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 typval_T *argvars;
13660 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013661{
13662 char_u *p;
13663 int n;
13664 int slen;
13665 int len;
13666 char_u *r;
13667 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013668
13669 n = get_tv_number(&argvars[1]);
13670 if (argvars[0].v_type == VAR_LIST)
13671 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013672 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013673 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013674 if (list_extend(rettv->vval.v_list,
13675 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013676 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013677 }
13678 else
13679 {
13680 p = get_tv_string(&argvars[0]);
13681 rettv->v_type = VAR_STRING;
13682 rettv->vval.v_string = NULL;
13683
13684 slen = (int)STRLEN(p);
13685 len = slen * n;
13686 if (len <= 0)
13687 return;
13688
13689 r = alloc(len + 1);
13690 if (r != NULL)
13691 {
13692 for (i = 0; i < n; i++)
13693 mch_memmove(r + i * slen, p, (size_t)slen);
13694 r[len] = NUL;
13695 }
13696
13697 rettv->vval.v_string = r;
13698 }
13699}
13700
13701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 * "resolve()" function
13703 */
13704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 typval_T *argvars;
13707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
13709 char_u *p;
13710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712#ifdef FEAT_SHORTCUT
13713 {
13714 char_u *v = NULL;
13715
13716 v = mch_resolve_shortcut(p);
13717 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 }
13722#else
13723# ifdef HAVE_READLINK
13724 {
13725 char_u buf[MAXPATHL + 1];
13726 char_u *cpy;
13727 int len;
13728 char_u *remain = NULL;
13729 char_u *q;
13730 int is_relative_to_current = FALSE;
13731 int has_trailing_pathsep = FALSE;
13732 int limit = 100;
13733
13734 p = vim_strsave(p);
13735
13736 if (p[0] == '.' && (vim_ispathsep(p[1])
13737 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13738 is_relative_to_current = TRUE;
13739
13740 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013741 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 has_trailing_pathsep = TRUE;
13743
13744 q = getnextcomp(p);
13745 if (*q != NUL)
13746 {
13747 /* Separate the first path component in "p", and keep the
13748 * remainder (beginning with the path separator). */
13749 remain = vim_strsave(q - 1);
13750 q[-1] = NUL;
13751 }
13752
13753 for (;;)
13754 {
13755 for (;;)
13756 {
13757 len = readlink((char *)p, (char *)buf, MAXPATHL);
13758 if (len <= 0)
13759 break;
13760 buf[len] = NUL;
13761
13762 if (limit-- == 0)
13763 {
13764 vim_free(p);
13765 vim_free(remain);
13766 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 goto fail;
13769 }
13770
13771 /* Ensure that the result will have a trailing path separator
13772 * if the argument has one. */
13773 if (remain == NULL && has_trailing_pathsep)
13774 add_pathsep(buf);
13775
13776 /* Separate the first path component in the link value and
13777 * concatenate the remainders. */
13778 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13779 if (*q != NUL)
13780 {
13781 if (remain == NULL)
13782 remain = vim_strsave(q - 1);
13783 else
13784 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013785 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 if (cpy != NULL)
13787 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 vim_free(remain);
13789 remain = cpy;
13790 }
13791 }
13792 q[-1] = NUL;
13793 }
13794
13795 q = gettail(p);
13796 if (q > p && *q == NUL)
13797 {
13798 /* Ignore trailing path separator. */
13799 q[-1] = NUL;
13800 q = gettail(p);
13801 }
13802 if (q > p && !mch_isFullName(buf))
13803 {
13804 /* symlink is relative to directory of argument */
13805 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13806 if (cpy != NULL)
13807 {
13808 STRCPY(cpy, p);
13809 STRCPY(gettail(cpy), buf);
13810 vim_free(p);
13811 p = cpy;
13812 }
13813 }
13814 else
13815 {
13816 vim_free(p);
13817 p = vim_strsave(buf);
13818 }
13819 }
13820
13821 if (remain == NULL)
13822 break;
13823
13824 /* Append the first path component of "remain" to "p". */
13825 q = getnextcomp(remain + 1);
13826 len = q - remain - (*q != NUL);
13827 cpy = vim_strnsave(p, STRLEN(p) + len);
13828 if (cpy != NULL)
13829 {
13830 STRNCAT(cpy, remain, len);
13831 vim_free(p);
13832 p = cpy;
13833 }
13834 /* Shorten "remain". */
13835 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013836 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 else
13838 {
13839 vim_free(remain);
13840 remain = NULL;
13841 }
13842 }
13843
13844 /* If the result is a relative path name, make it explicitly relative to
13845 * the current directory if and only if the argument had this form. */
13846 if (!vim_ispathsep(*p))
13847 {
13848 if (is_relative_to_current
13849 && *p != NUL
13850 && !(p[0] == '.'
13851 && (p[1] == NUL
13852 || vim_ispathsep(p[1])
13853 || (p[1] == '.'
13854 && (p[2] == NUL
13855 || vim_ispathsep(p[2]))))))
13856 {
13857 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013858 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 if (cpy != NULL)
13860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 vim_free(p);
13862 p = cpy;
13863 }
13864 }
13865 else if (!is_relative_to_current)
13866 {
13867 /* Strip leading "./". */
13868 q = p;
13869 while (q[0] == '.' && vim_ispathsep(q[1]))
13870 q += 2;
13871 if (q > p)
13872 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13873 }
13874 }
13875
13876 /* Ensure that the result will have no trailing path separator
13877 * if the argument had none. But keep "/" or "//". */
13878 if (!has_trailing_pathsep)
13879 {
13880 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013881 if (after_pathsep(p, q))
13882 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 }
13884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013885 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 }
13887# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889# endif
13890#endif
13891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893
13894#ifdef HAVE_READLINK
13895fail:
13896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013897 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898}
13899
13900/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013901 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 */
13903 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013904f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 typval_T *argvars;
13906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907{
Bram Moolenaar33570922005-01-25 22:26:29 +000013908 list_T *l;
13909 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910
Bram Moolenaar0d660222005-01-07 21:51:51 +000013911 rettv->vval.v_number = 0;
13912 if (argvars[0].v_type != VAR_LIST)
13913 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013914 else if ((l = argvars[0].vval.v_list) != NULL
13915 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013916 {
13917 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013918 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013919 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013920 while (li != NULL)
13921 {
13922 ni = li->li_prev;
13923 list_append(l, li);
13924 li = ni;
13925 }
13926 rettv->vval.v_list = l;
13927 rettv->v_type = VAR_LIST;
13928 ++l->lv_refcount;
13929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930}
13931
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013932#define SP_NOMOVE 0x01 /* don't move cursor */
13933#define SP_REPEAT 0x02 /* repeat to find outer pair */
13934#define SP_RETCOUNT 0x04 /* return matchcount */
13935#define SP_SETPCMARK 0x08 /* set previous context mark */
13936#define SP_START 0x10 /* accept match at start position */
13937#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13938#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013939
Bram Moolenaar33570922005-01-25 22:26:29 +000013940static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013941
13942/*
13943 * Get flags for a search function.
13944 * Possibly sets "p_ws".
13945 * Returns BACKWARD, FORWARD or zero (for an error).
13946 */
13947 static int
13948get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013949 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950 int *flagsp;
13951{
13952 int dir = FORWARD;
13953 char_u *flags;
13954 char_u nbuf[NUMBUFLEN];
13955 int mask;
13956
13957 if (varp->v_type != VAR_UNKNOWN)
13958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013959 flags = get_tv_string_buf_chk(varp, nbuf);
13960 if (flags == NULL)
13961 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013962 while (*flags != NUL)
13963 {
13964 switch (*flags)
13965 {
13966 case 'b': dir = BACKWARD; break;
13967 case 'w': p_ws = TRUE; break;
13968 case 'W': p_ws = FALSE; break;
13969 default: mask = 0;
13970 if (flagsp != NULL)
13971 switch (*flags)
13972 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013973 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013974 case 'e': mask = SP_END; break;
13975 case 'm': mask = SP_RETCOUNT; break;
13976 case 'n': mask = SP_NOMOVE; break;
13977 case 'p': mask = SP_SUBPAT; break;
13978 case 'r': mask = SP_REPEAT; break;
13979 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013980 }
13981 if (mask == 0)
13982 {
13983 EMSG2(_(e_invarg2), flags);
13984 dir = 0;
13985 }
13986 else
13987 *flagsp |= mask;
13988 }
13989 if (dir == 0)
13990 break;
13991 ++flags;
13992 }
13993 }
13994 return dir;
13995}
13996
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013998 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014000 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014001search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014002 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014003 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014004 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014006 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 char_u *pat;
14008 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014009 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 int save_p_ws = p_ws;
14011 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014012 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014013 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014014 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014015 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014018 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014019 if (dir == 0)
14020 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014021 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014022 if (flags & SP_START)
14023 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014024 if (flags & SP_END)
14025 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014026
14027 /* Optional extra argument: line number to stop searching. */
14028 if (argvars[1].v_type != VAR_UNKNOWN
14029 && argvars[2].v_type != VAR_UNKNOWN)
14030 {
14031 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14032 if (lnum_stop < 0)
14033 goto theend;
14034 }
14035
Bram Moolenaar231334e2005-07-25 20:46:57 +000014036 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014037 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014038 * Check to make sure only those flags are set.
14039 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14040 * flags cannot be set. Check for that condition also.
14041 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014042 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014043 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014046 goto theend;
14047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014049 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014050 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14051 options, RE_SEARCH, (linenr_T)lnum_stop);
14052 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014054 if (flags & SP_SUBPAT)
14055 retval = subpatnum;
14056 else
14057 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014058 if (flags & SP_SETPCMARK)
14059 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014061 if (match_pos != NULL)
14062 {
14063 /* Store the match cursor position */
14064 match_pos->lnum = pos.lnum;
14065 match_pos->col = pos.col + 1;
14066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 /* "/$" will put the cursor after the end of the line, may need to
14068 * correct that here */
14069 check_cursor();
14070 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014071
14072 /* If 'n' flag is used: restore cursor position. */
14073 if (flags & SP_NOMOVE)
14074 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014075 else
14076 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014077theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014079
14080 return retval;
14081}
14082
14083/*
14084 * "search()" function
14085 */
14086 static void
14087f_search(argvars, rettv)
14088 typval_T *argvars;
14089 typval_T *rettv;
14090{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014091 int flags = 0;
14092
14093 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094}
14095
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014097 * "searchdecl()" function
14098 */
14099 static void
14100f_searchdecl(argvars, rettv)
14101 typval_T *argvars;
14102 typval_T *rettv;
14103{
14104 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014105 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014106 int error = FALSE;
14107 char_u *name;
14108
14109 rettv->vval.v_number = 1; /* default: FAIL */
14110
14111 name = get_tv_string_chk(&argvars[0]);
14112 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014113 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014114 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014115 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14116 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14117 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014118 if (!error && name != NULL)
14119 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014120 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014121}
14122
14123/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014124 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014126 static int
14127searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014129 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130{
14131 char_u *spat, *mpat, *epat;
14132 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 int dir;
14135 int flags = 0;
14136 char_u nbuf1[NUMBUFLEN];
14137 char_u nbuf2[NUMBUFLEN];
14138 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014139 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014140 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 spat = get_tv_string_chk(&argvars[0]);
14144 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14145 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14146 if (spat == NULL || mpat == NULL || epat == NULL)
14147 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 /* Handle the optional fourth argument: flags */
14150 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014151 if (dir == 0)
14152 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014153
14154 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014155 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14156 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014157 if ((flags & (SP_END | SP_SUBPAT)) != 0
14158 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014159 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014160 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014161 goto theend;
14162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014164 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014165 if (argvars[3].v_type == VAR_UNKNOWN
14166 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 skip = (char_u *)"";
14168 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014171 if (argvars[5].v_type != VAR_UNKNOWN)
14172 {
14173 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14174 if (lnum_stop < 0)
14175 goto theend;
14176 }
14177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 if (skip == NULL)
14179 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014181 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14182 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014183
14184theend:
14185 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014186
14187 return retval;
14188}
14189
14190/*
14191 * "searchpair()" function
14192 */
14193 static void
14194f_searchpair(argvars, rettv)
14195 typval_T *argvars;
14196 typval_T *rettv;
14197{
14198 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14199}
14200
14201/*
14202 * "searchpairpos()" function
14203 */
14204 static void
14205f_searchpairpos(argvars, rettv)
14206 typval_T *argvars;
14207 typval_T *rettv;
14208{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014209 pos_T match_pos;
14210 int lnum = 0;
14211 int col = 0;
14212
14213 rettv->vval.v_number = 0;
14214
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014215 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014216 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014217
14218 if (searchpair_cmn(argvars, &match_pos) > 0)
14219 {
14220 lnum = match_pos.lnum;
14221 col = match_pos.col;
14222 }
14223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014224 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14225 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014226}
14227
14228/*
14229 * Search for a start/middle/end thing.
14230 * Used by searchpair(), see its documentation for the details.
14231 * Returns 0 or -1 for no match,
14232 */
14233 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014234do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014235 char_u *spat; /* start pattern */
14236 char_u *mpat; /* middle pattern */
14237 char_u *epat; /* end pattern */
14238 int dir; /* BACKWARD or FORWARD */
14239 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014240 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014241 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014242 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014243{
14244 char_u *save_cpo;
14245 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14246 long retval = 0;
14247 pos_T pos;
14248 pos_T firstpos;
14249 pos_T foundpos;
14250 pos_T save_cursor;
14251 pos_T save_pos;
14252 int n;
14253 int r;
14254 int nest = 1;
14255 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014256 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014257
14258 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14259 save_cpo = p_cpo;
14260 p_cpo = (char_u *)"";
14261
14262 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14263 * start/middle/end (pat3, for the top pair). */
14264 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14265 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14266 if (pat2 == NULL || pat3 == NULL)
14267 goto theend;
14268 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14269 if (*mpat == NUL)
14270 STRCPY(pat3, pat2);
14271 else
14272 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14273 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014274 if (flags & SP_START)
14275 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014276
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 save_cursor = curwin->w_cursor;
14278 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014279 clearpos(&firstpos);
14280 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281 pat = pat3;
14282 for (;;)
14283 {
14284 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014285 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14287 /* didn't find it or found the first match again: FAIL */
14288 break;
14289
14290 if (firstpos.lnum == 0)
14291 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014292 if (equalpos(pos, foundpos))
14293 {
14294 /* Found the same position again. Can happen with a pattern that
14295 * has "\zs" at the end and searching backwards. Advance one
14296 * character and try again. */
14297 if (dir == BACKWARD)
14298 decl(&pos);
14299 else
14300 incl(&pos);
14301 }
14302 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
14304 /* If the skip pattern matches, ignore this match. */
14305 if (*skip != NUL)
14306 {
14307 save_pos = curwin->w_cursor;
14308 curwin->w_cursor = pos;
14309 r = eval_to_bool(skip, &err, NULL, FALSE);
14310 curwin->w_cursor = save_pos;
14311 if (err)
14312 {
14313 /* Evaluating {skip} caused an error, break here. */
14314 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014315 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 break;
14317 }
14318 if (r)
14319 continue;
14320 }
14321
14322 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14323 {
14324 /* Found end when searching backwards or start when searching
14325 * forward: nested pair. */
14326 ++nest;
14327 pat = pat2; /* nested, don't search for middle */
14328 }
14329 else
14330 {
14331 /* Found end when searching forward or start when searching
14332 * backward: end of (nested) pair; or found middle in outer pair. */
14333 if (--nest == 1)
14334 pat = pat3; /* outer level, search for middle */
14335 }
14336
14337 if (nest == 0)
14338 {
14339 /* Found the match: return matchcount or line number. */
14340 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014341 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014343 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014344 if (flags & SP_SETPCMARK)
14345 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 curwin->w_cursor = pos;
14347 if (!(flags & SP_REPEAT))
14348 break;
14349 nest = 1; /* search for next unmatched */
14350 }
14351 }
14352
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014353 if (match_pos != NULL)
14354 {
14355 /* Store the match cursor position */
14356 match_pos->lnum = curwin->w_cursor.lnum;
14357 match_pos->col = curwin->w_cursor.col + 1;
14358 }
14359
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014361 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 curwin->w_cursor = save_cursor;
14363
14364theend:
14365 vim_free(pat2);
14366 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014368
14369 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370}
14371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014372/*
14373 * "searchpos()" function
14374 */
14375 static void
14376f_searchpos(argvars, rettv)
14377 typval_T *argvars;
14378 typval_T *rettv;
14379{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014380 pos_T match_pos;
14381 int lnum = 0;
14382 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014383 int n;
14384 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014385
14386 rettv->vval.v_number = 0;
14387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014388 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014389 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014390
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014391 n = search_cmn(argvars, &match_pos, &flags);
14392 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014393 {
14394 lnum = match_pos.lnum;
14395 col = match_pos.col;
14396 }
14397
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014398 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14399 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014400 if (flags & SP_SUBPAT)
14401 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014402}
14403
14404
Bram Moolenaar0d660222005-01-07 21:51:51 +000014405/*ARGSUSED*/
14406 static void
14407f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014408 typval_T *argvars;
14409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014411#ifdef FEAT_CLIENTSERVER
14412 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 char_u *server = get_tv_string_chk(&argvars[0]);
14414 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 if (server == NULL || reply == NULL)
14418 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014419 if (check_restricted() || check_secure())
14420 return;
14421# ifdef FEAT_X11
14422 if (check_connection() == FAIL)
14423 return;
14424# endif
14425
14426 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 EMSG(_("E258: Unable to send to client"));
14429 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 rettv->vval.v_number = 0;
14432#else
14433 rettv->vval.v_number = -1;
14434#endif
14435}
14436
14437/*ARGSUSED*/
14438 static void
14439f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 typval_T *argvars;
14441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014442{
14443 char_u *r = NULL;
14444
14445#ifdef FEAT_CLIENTSERVER
14446# ifdef WIN32
14447 r = serverGetVimNames();
14448# else
14449 make_connection();
14450 if (X_DISPLAY != NULL)
14451 r = serverGetVimNames(X_DISPLAY);
14452# endif
14453#endif
14454 rettv->v_type = VAR_STRING;
14455 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456}
14457
14458/*
14459 * "setbufvar()" function
14460 */
14461/*ARGSUSED*/
14462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014464 typval_T *argvars;
14465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466{
14467 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 char_u nbuf[NUMBUFLEN];
14472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 rettv->vval.v_number = 0;
14474
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 if (check_restricted() || check_secure())
14476 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014477 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14478 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 varp = &argvars[2];
14481
14482 if (buf != NULL && varname != NULL && varp != NULL)
14483 {
14484 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486
14487 if (*varname == '&')
14488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014489 long numval;
14490 char_u *strval;
14491 int error = FALSE;
14492
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 numval = get_tv_number_chk(varp, &error);
14495 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 if (!error && strval != NULL)
14497 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 }
14499 else
14500 {
14501 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14502 if (bufvarname != NULL)
14503 {
14504 STRCPY(bufvarname, "b:");
14505 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014506 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 vim_free(bufvarname);
14508 }
14509 }
14510
14511 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514}
14515
14516/*
14517 * "setcmdpos()" function
14518 */
14519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 typval_T *argvars;
14522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 int pos = (int)get_tv_number(&argvars[0]) - 1;
14525
14526 if (pos >= 0)
14527 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528}
14529
14530/*
14531 * "setline()" function
14532 */
14533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 typval_T *argvars;
14536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
14538 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014539 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014540 list_T *l = NULL;
14541 listitem_T *li = NULL;
14542 long added = 0;
14543 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014545 lnum = get_tv_lnum(&argvars[0]);
14546 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014548 l = argvars[1].vval.v_list;
14549 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014551 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014553
14554 rettv->vval.v_number = 0; /* OK */
14555 for (;;)
14556 {
14557 if (l != NULL)
14558 {
14559 /* list argument, get next string */
14560 if (li == NULL)
14561 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014563 li = li->li_next;
14564 }
14565
14566 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014568 break;
14569 if (lnum <= curbuf->b_ml.ml_line_count)
14570 {
14571 /* existing line, replace it */
14572 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14573 {
14574 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014575 if (lnum == curwin->w_cursor.lnum)
14576 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014577 rettv->vval.v_number = 0; /* OK */
14578 }
14579 }
14580 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14581 {
14582 /* lnum is one past the last line, append the line */
14583 ++added;
14584 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14585 rettv->vval.v_number = 0; /* OK */
14586 }
14587
14588 if (l == NULL) /* only one string argument */
14589 break;
14590 ++lnum;
14591 }
14592
14593 if (added > 0)
14594 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595}
14596
14597/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014598 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014599 */
14600/*ARGSUSED*/
14601 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014602set_qf_ll_list(wp, list_arg, action_arg, rettv)
14603 win_T *wp;
14604 typval_T *list_arg;
14605 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014606 typval_T *rettv;
14607{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014608#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014609 char_u *act;
14610 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014611#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014612
Bram Moolenaar2641f772005-03-25 21:58:17 +000014613 rettv->vval.v_number = -1;
14614
14615#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014616 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014617 EMSG(_(e_listreq));
14618 else
14619 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014620 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014621
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014622 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014623 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014624 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 if (act == NULL)
14626 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014627 if (*act == 'a' || *act == 'r')
14628 action = *act;
14629 }
14630
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014631 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014632 rettv->vval.v_number = 0;
14633 }
14634#endif
14635}
14636
14637/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014638 * "setloclist()" function
14639 */
14640/*ARGSUSED*/
14641 static void
14642f_setloclist(argvars, rettv)
14643 typval_T *argvars;
14644 typval_T *rettv;
14645{
14646 win_T *win;
14647
14648 rettv->vval.v_number = -1;
14649
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014650 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014651 if (win != NULL)
14652 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14653}
14654
14655/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014656 * "setmatches()" function
14657 */
14658 static void
14659f_setmatches(argvars, rettv)
14660 typval_T *argvars;
14661 typval_T *rettv;
14662{
14663#ifdef FEAT_SEARCH_EXTRA
14664 list_T *l;
14665 listitem_T *li;
14666 dict_T *d;
14667
14668 rettv->vval.v_number = -1;
14669 if (argvars[0].v_type != VAR_LIST)
14670 {
14671 EMSG(_(e_listreq));
14672 return;
14673 }
14674 if ((l = argvars[0].vval.v_list) != NULL)
14675 {
14676
14677 /* To some extent make sure that we are dealing with a list from
14678 * "getmatches()". */
14679 li = l->lv_first;
14680 while (li != NULL)
14681 {
14682 if (li->li_tv.v_type != VAR_DICT
14683 || (d = li->li_tv.vval.v_dict) == NULL)
14684 {
14685 EMSG(_(e_invarg));
14686 return;
14687 }
14688 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14689 && dict_find(d, (char_u *)"pattern", -1) != NULL
14690 && dict_find(d, (char_u *)"priority", -1) != NULL
14691 && dict_find(d, (char_u *)"id", -1) != NULL))
14692 {
14693 EMSG(_(e_invarg));
14694 return;
14695 }
14696 li = li->li_next;
14697 }
14698
14699 clear_matches(curwin);
14700 li = l->lv_first;
14701 while (li != NULL)
14702 {
14703 d = li->li_tv.vval.v_dict;
14704 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14705 get_dict_string(d, (char_u *)"pattern", FALSE),
14706 (int)get_dict_number(d, (char_u *)"priority"),
14707 (int)get_dict_number(d, (char_u *)"id"));
14708 li = li->li_next;
14709 }
14710 rettv->vval.v_number = 0;
14711 }
14712#endif
14713}
14714
14715/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014716 * "setpos()" function
14717 */
14718/*ARGSUSED*/
14719 static void
14720f_setpos(argvars, rettv)
14721 typval_T *argvars;
14722 typval_T *rettv;
14723{
14724 pos_T pos;
14725 int fnum;
14726 char_u *name;
14727
14728 name = get_tv_string_chk(argvars);
14729 if (name != NULL)
14730 {
14731 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14732 {
14733 --pos.col;
14734 if (name[0] == '.') /* cursor */
14735 {
14736 if (fnum == curbuf->b_fnum)
14737 {
14738 curwin->w_cursor = pos;
14739 check_cursor();
14740 }
14741 else
14742 EMSG(_(e_invarg));
14743 }
14744 else if (name[0] == '\'') /* mark */
14745 (void)setmark_pos(name[1], &pos, fnum);
14746 else
14747 EMSG(_(e_invarg));
14748 }
14749 }
14750}
14751
14752/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014753 * "setqflist()" function
14754 */
14755/*ARGSUSED*/
14756 static void
14757f_setqflist(argvars, rettv)
14758 typval_T *argvars;
14759 typval_T *rettv;
14760{
14761 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14762}
14763
14764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 * "setreg()" function
14766 */
14767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014769 typval_T *argvars;
14770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771{
14772 int regname;
14773 char_u *strregname;
14774 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014775 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 int append;
14777 char_u yank_type;
14778 long block_len;
14779
14780 block_len = -1;
14781 yank_type = MAUTO;
14782 append = FALSE;
14783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014785 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 if (strregname == NULL)
14788 return; /* type error; errmsg already given */
14789 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 if (regname == 0 || regname == '@')
14791 regname = '"';
14792 else if (regname == '=')
14793 return;
14794
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 stropt = get_tv_string_chk(&argvars[2]);
14798 if (stropt == NULL)
14799 return; /* type error */
14800 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 switch (*stropt)
14802 {
14803 case 'a': case 'A': /* append */
14804 append = TRUE;
14805 break;
14806 case 'v': case 'c': /* character-wise selection */
14807 yank_type = MCHAR;
14808 break;
14809 case 'V': case 'l': /* line-wise selection */
14810 yank_type = MLINE;
14811 break;
14812#ifdef FEAT_VISUAL
14813 case 'b': case Ctrl_V: /* block-wise selection */
14814 yank_type = MBLOCK;
14815 if (VIM_ISDIGIT(stropt[1]))
14816 {
14817 ++stropt;
14818 block_len = getdigits(&stropt) - 1;
14819 --stropt;
14820 }
14821 break;
14822#endif
14823 }
14824 }
14825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014826 strval = get_tv_string_chk(&argvars[1]);
14827 if (strval != NULL)
14828 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014830 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831}
14832
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014833/*
14834 * "settabwinvar()" function
14835 */
14836 static void
14837f_settabwinvar(argvars, rettv)
14838 typval_T *argvars;
14839 typval_T *rettv;
14840{
14841 setwinvar(argvars, rettv, 1);
14842}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843
14844/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014845 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014848f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 typval_T *argvars;
14850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014852 setwinvar(argvars, rettv, 0);
14853}
14854
14855/*
14856 * "setwinvar()" and "settabwinvar()" functions
14857 */
14858 static void
14859setwinvar(argvars, rettv, off)
14860 typval_T *argvars;
14861 typval_T *rettv;
14862 int off;
14863{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 win_T *win;
14865#ifdef FEAT_WINDOWS
14866 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014867 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868#endif
14869 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014872 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 rettv->vval.v_number = 0;
14875
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 if (check_restricted() || check_secure())
14877 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014878
14879#ifdef FEAT_WINDOWS
14880 if (off == 1)
14881 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14882 else
14883 tp = curtab;
14884#endif
14885 win = find_win_by_nr(&argvars[off], tp);
14886 varname = get_tv_string_chk(&argvars[off + 1]);
14887 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888
14889 if (win != NULL && varname != NULL && varp != NULL)
14890 {
14891#ifdef FEAT_WINDOWS
14892 /* set curwin to be our win, temporarily */
14893 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014894 save_curtab = curtab;
14895 goto_tabpage_tp(tp);
14896 if (!win_valid(win))
14897 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 curwin = win;
14899 curbuf = curwin->w_buffer;
14900#endif
14901
14902 if (*varname == '&')
14903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904 long numval;
14905 char_u *strval;
14906 int error = FALSE;
14907
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014909 numval = get_tv_number_chk(varp, &error);
14910 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014911 if (!error && strval != NULL)
14912 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 }
14914 else
14915 {
14916 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14917 if (winvarname != NULL)
14918 {
14919 STRCPY(winvarname, "w:");
14920 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014921 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 vim_free(winvarname);
14923 }
14924 }
14925
14926#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014927 /* Restore current tabpage and window, if still valid (autocomands can
14928 * make them invalid). */
14929 if (valid_tabpage(save_curtab))
14930 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 if (win_valid(save_curwin))
14932 {
14933 curwin = save_curwin;
14934 curbuf = curwin->w_buffer;
14935 }
14936#endif
14937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938}
14939
14940/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014941 * "shellescape({string})" function
14942 */
14943 static void
14944f_shellescape(argvars, rettv)
14945 typval_T *argvars;
14946 typval_T *rettv;
14947{
14948 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14949 rettv->v_type = VAR_STRING;
14950}
14951
14952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014953 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 */
14955 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014957 typval_T *argvars;
14958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962 p = get_tv_string(&argvars[0]);
14963 rettv->vval.v_string = vim_strsave(p);
14964 simplify_filename(rettv->vval.v_string); /* simplify in place */
14965 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966}
14967
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968static int
14969#ifdef __BORLANDC__
14970 _RTLENTRYF
14971#endif
14972 item_compare __ARGS((const void *s1, const void *s2));
14973static int
14974#ifdef __BORLANDC__
14975 _RTLENTRYF
14976#endif
14977 item_compare2 __ARGS((const void *s1, const void *s2));
14978
14979static int item_compare_ic;
14980static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014981static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982#define ITEM_COMPARE_FAIL 999
14983
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987 static int
14988#ifdef __BORLANDC__
14989_RTLENTRYF
14990#endif
14991item_compare(s1, s2)
14992 const void *s1;
14993 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995 char_u *p1, *p2;
14996 char_u *tofree1, *tofree2;
14997 int res;
14998 char_u numbuf1[NUMBUFLEN];
14999 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015001 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15002 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015003 if (p1 == NULL)
15004 p1 = (char_u *)"";
15005 if (p2 == NULL)
15006 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 if (item_compare_ic)
15008 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015010 res = STRCMP(p1, p2);
15011 vim_free(tofree1);
15012 vim_free(tofree2);
15013 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014}
15015
15016 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017#ifdef __BORLANDC__
15018_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020item_compare2(s1, s2)
15021 const void *s1;
15022 const void *s2;
15023{
15024 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015025 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015026 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015029 /* shortcut after failure in previous call; compare all items equal */
15030 if (item_compare_func_err)
15031 return 0;
15032
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015033 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15034 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15036 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037
15038 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015039 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015040 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041 clear_tv(&argv[0]);
15042 clear_tv(&argv[1]);
15043
15044 if (res == FAIL)
15045 res = ITEM_COMPARE_FAIL;
15046 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047 /* return value has wrong type */
15048 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15049 if (item_compare_func_err)
15050 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 clear_tv(&rettv);
15052 return res;
15053}
15054
15055/*
15056 * "sort({list})" function
15057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015060 typval_T *argvars;
15061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062{
Bram Moolenaar33570922005-01-25 22:26:29 +000015063 list_T *l;
15064 listitem_T *li;
15065 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 long len;
15067 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069 rettv->vval.v_number = 0;
15070 if (argvars[0].v_type != VAR_LIST)
15071 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072 else
15073 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015075 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 return;
15077 rettv->vval.v_list = l;
15078 rettv->v_type = VAR_LIST;
15079 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081 len = list_len(l);
15082 if (len <= 1)
15083 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 item_compare_ic = FALSE;
15086 item_compare_func = NULL;
15087 if (argvars[1].v_type != VAR_UNKNOWN)
15088 {
15089 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015090 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 else
15092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 int error = FALSE;
15094
15095 i = get_tv_number_chk(&argvars[1], &error);
15096 if (error)
15097 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098 if (i == 1)
15099 item_compare_ic = TRUE;
15100 else
15101 item_compare_func = get_tv_string(&argvars[1]);
15102 }
15103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 if (ptrs == NULL)
15108 return;
15109 i = 0;
15110 for (li = l->lv_first; li != NULL; li = li->li_next)
15111 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015113 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015114 /* test the compare function */
15115 if (item_compare_func != NULL
15116 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15117 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015118 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120 {
15121 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 item_compare_func == NULL ? item_compare : item_compare2);
15124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 if (!item_compare_func_err)
15126 {
15127 /* Clear the List and append the items in the sorted order. */
15128 l->lv_first = l->lv_last = NULL;
15129 l->lv_len = 0;
15130 for (i = 0; i < len; ++i)
15131 list_append(l, ptrs[i]);
15132 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133 }
15134
15135 vim_free(ptrs);
15136 }
15137}
15138
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015139/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015140 * "soundfold({word})" function
15141 */
15142 static void
15143f_soundfold(argvars, rettv)
15144 typval_T *argvars;
15145 typval_T *rettv;
15146{
15147 char_u *s;
15148
15149 rettv->v_type = VAR_STRING;
15150 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015151#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015152 rettv->vval.v_string = eval_soundfold(s);
15153#else
15154 rettv->vval.v_string = vim_strsave(s);
15155#endif
15156}
15157
15158/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015159 * "spellbadword()" function
15160 */
15161/* ARGSUSED */
15162 static void
15163f_spellbadword(argvars, rettv)
15164 typval_T *argvars;
15165 typval_T *rettv;
15166{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015167 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015168 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015169 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015170
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015171 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015172 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015173
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015174#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015175 if (argvars[0].v_type == VAR_UNKNOWN)
15176 {
15177 /* Find the start and length of the badly spelled word. */
15178 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15179 if (len != 0)
15180 word = ml_get_cursor();
15181 }
15182 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15183 {
15184 char_u *str = get_tv_string_chk(&argvars[0]);
15185 int capcol = -1;
15186
15187 if (str != NULL)
15188 {
15189 /* Check the argument for spelling. */
15190 while (*str != NUL)
15191 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015192 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015193 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015194 {
15195 word = str;
15196 break;
15197 }
15198 str += len;
15199 }
15200 }
15201 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015202#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015203
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015204 list_append_string(rettv->vval.v_list, word, len);
15205 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015206 attr == HLF_SPB ? "bad" :
15207 attr == HLF_SPR ? "rare" :
15208 attr == HLF_SPL ? "local" :
15209 attr == HLF_SPC ? "caps" :
15210 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015211}
15212
15213/*
15214 * "spellsuggest()" function
15215 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015216/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015217 static void
15218f_spellsuggest(argvars, rettv)
15219 typval_T *argvars;
15220 typval_T *rettv;
15221{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015222#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015223 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015224 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015225 int maxcount;
15226 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015227 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015228 listitem_T *li;
15229 int need_capital = FALSE;
15230#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015231
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015232 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015233 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015234
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015235#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015236 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15237 {
15238 str = get_tv_string(&argvars[0]);
15239 if (argvars[1].v_type != VAR_UNKNOWN)
15240 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015241 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015242 if (maxcount <= 0)
15243 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015244 if (argvars[2].v_type != VAR_UNKNOWN)
15245 {
15246 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15247 if (typeerr)
15248 return;
15249 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015250 }
15251 else
15252 maxcount = 25;
15253
Bram Moolenaar4770d092006-01-12 23:22:24 +000015254 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015255
15256 for (i = 0; i < ga.ga_len; ++i)
15257 {
15258 str = ((char_u **)ga.ga_data)[i];
15259
15260 li = listitem_alloc();
15261 if (li == NULL)
15262 vim_free(str);
15263 else
15264 {
15265 li->li_tv.v_type = VAR_STRING;
15266 li->li_tv.v_lock = 0;
15267 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015268 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015269 }
15270 }
15271 ga_clear(&ga);
15272 }
15273#endif
15274}
15275
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015277f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015278 typval_T *argvars;
15279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280{
15281 char_u *str;
15282 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015283 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 regmatch_T regmatch;
15285 char_u patbuf[NUMBUFLEN];
15286 char_u *save_cpo;
15287 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015289 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015291
15292 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15293 save_cpo = p_cpo;
15294 p_cpo = (char_u *)"";
15295
15296 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015297 if (argvars[1].v_type != VAR_UNKNOWN)
15298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15300 if (pat == NULL)
15301 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015302 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015303 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015304 }
15305 if (pat == NULL || *pat == NUL)
15306 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015308 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 if (typeerr)
15311 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15314 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015316 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015317 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015318 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015319 if (*str == NUL)
15320 match = FALSE; /* empty item at the end */
15321 else
15322 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015323 if (match)
15324 end = regmatch.startp[0];
15325 else
15326 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015327 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15328 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015330 if (list_append_string(rettv->vval.v_list, str,
15331 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015333 }
15334 if (!match)
15335 break;
15336 /* Advance to just after the match. */
15337 if (regmatch.endp[0] > str)
15338 col = 0;
15339 else
15340 {
15341 /* Don't get stuck at the same match. */
15342#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015343 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015344#else
15345 col = 1;
15346#endif
15347 }
15348 str = regmatch.endp[0];
15349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353
Bram Moolenaar0d660222005-01-07 21:51:51 +000015354 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355}
15356
Bram Moolenaar2c932302006-03-18 21:42:09 +000015357/*
15358 * "str2nr()" function
15359 */
15360 static void
15361f_str2nr(argvars, rettv)
15362 typval_T *argvars;
15363 typval_T *rettv;
15364{
15365 int base = 10;
15366 char_u *p;
15367 long n;
15368
15369 if (argvars[1].v_type != VAR_UNKNOWN)
15370 {
15371 base = get_tv_number(&argvars[1]);
15372 if (base != 8 && base != 10 && base != 16)
15373 {
15374 EMSG(_(e_invarg));
15375 return;
15376 }
15377 }
15378
15379 p = skipwhite(get_tv_string(&argvars[0]));
15380 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15381 rettv->vval.v_number = n;
15382}
15383
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384#ifdef HAVE_STRFTIME
15385/*
15386 * "strftime({format}[, {time}])" function
15387 */
15388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015390 typval_T *argvars;
15391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392{
15393 char_u result_buf[256];
15394 struct tm *curtime;
15395 time_t seconds;
15396 char_u *p;
15397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015401 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 seconds = time(NULL);
15403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 curtime = localtime(&seconds);
15406 /* MSVC returns NULL for an invalid value of seconds. */
15407 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 else
15410 {
15411# ifdef FEAT_MBYTE
15412 vimconv_T conv;
15413 char_u *enc;
15414
15415 conv.vc_type = CONV_NONE;
15416 enc = enc_locale();
15417 convert_setup(&conv, p_enc, enc);
15418 if (conv.vc_type != CONV_NONE)
15419 p = string_convert(&conv, p, NULL);
15420# endif
15421 if (p != NULL)
15422 (void)strftime((char *)result_buf, sizeof(result_buf),
15423 (char *)p, curtime);
15424 else
15425 result_buf[0] = NUL;
15426
15427# ifdef FEAT_MBYTE
15428 if (conv.vc_type != CONV_NONE)
15429 vim_free(p);
15430 convert_setup(&conv, enc, p_enc);
15431 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 else
15434# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015435 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436
15437# ifdef FEAT_MBYTE
15438 /* Release conversion descriptors */
15439 convert_setup(&conv, NULL, NULL);
15440 vim_free(enc);
15441# endif
15442 }
15443}
15444#endif
15445
15446/*
15447 * "stridx()" function
15448 */
15449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015451 typval_T *argvars;
15452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453{
15454 char_u buf[NUMBUFLEN];
15455 char_u *needle;
15456 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015457 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015461 needle = get_tv_string_chk(&argvars[1]);
15462 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015463 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015464 if (needle == NULL || haystack == NULL)
15465 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
Bram Moolenaar33570922005-01-25 22:26:29 +000015467 if (argvars[2].v_type != VAR_UNKNOWN)
15468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015469 int error = FALSE;
15470
15471 start_idx = get_tv_number_chk(&argvars[2], &error);
15472 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015473 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015474 if (start_idx >= 0)
15475 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 }
15477
15478 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15479 if (pos != NULL)
15480 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481}
15482
15483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015484 * "string()" function
15485 */
15486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015488 typval_T *argvars;
15489 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015490{
15491 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015492 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015495 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015496 /* Make a copy if we have a value but it's not in allocate memory. */
15497 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015498 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499}
15500
15501/*
15502 * "strlen()" function
15503 */
15504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015505f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015506 typval_T *argvars;
15507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015509 rettv->vval.v_number = (varnumber_T)(STRLEN(
15510 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511}
15512
15513/*
15514 * "strpart()" function
15515 */
15516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015517f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015518 typval_T *argvars;
15519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520{
15521 char_u *p;
15522 int n;
15523 int len;
15524 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015525 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015527 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 slen = (int)STRLEN(p);
15529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015530 n = get_tv_number_chk(&argvars[1], &error);
15531 if (error)
15532 len = 0;
15533 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015534 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 else
15536 len = slen - n; /* default len: all bytes that are available. */
15537
15538 /*
15539 * Only return the overlap between the specified part and the actual
15540 * string.
15541 */
15542 if (n < 0)
15543 {
15544 len += n;
15545 n = 0;
15546 }
15547 else if (n > slen)
15548 n = slen;
15549 if (len < 0)
15550 len = 0;
15551 else if (n + len > slen)
15552 len = slen - n;
15553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 rettv->v_type = VAR_STRING;
15555 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556}
15557
15558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559 * "strridx()" function
15560 */
15561 static void
15562f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 typval_T *argvars;
15564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565{
15566 char_u buf[NUMBUFLEN];
15567 char_u *needle;
15568 char_u *haystack;
15569 char_u *rest;
15570 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015571 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015573 needle = get_tv_string_chk(&argvars[1]);
15574 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015575
15576 rettv->vval.v_number = -1;
15577 if (needle == NULL || haystack == NULL)
15578 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015579
15580 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015581 if (argvars[2].v_type != VAR_UNKNOWN)
15582 {
15583 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015584 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015585 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015587 }
15588 else
15589 end_idx = haystack_len;
15590
Bram Moolenaar0d660222005-01-07 21:51:51 +000015591 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015592 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015594 lastmatch = haystack + end_idx;
15595 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015597 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015598 for (rest = haystack; *rest != '\0'; ++rest)
15599 {
15600 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015601 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602 break;
15603 lastmatch = rest;
15604 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015605 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606
15607 if (lastmatch == NULL)
15608 rettv->vval.v_number = -1;
15609 else
15610 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15611}
15612
15613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 * "strtrans()" function
15615 */
15616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015617f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 typval_T *argvars;
15619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015621 rettv->v_type = VAR_STRING;
15622 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623}
15624
15625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015626 * "submatch()" function
15627 */
15628 static void
15629f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 typval_T *argvars;
15631 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632{
15633 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 rettv->vval.v_string =
15635 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636}
15637
15638/*
15639 * "substitute()" function
15640 */
15641 static void
15642f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 typval_T *argvars;
15644 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645{
15646 char_u patbuf[NUMBUFLEN];
15647 char_u subbuf[NUMBUFLEN];
15648 char_u flagsbuf[NUMBUFLEN];
15649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015650 char_u *str = get_tv_string_chk(&argvars[0]);
15651 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15652 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15653 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15654
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15657 rettv->vval.v_string = NULL;
15658 else
15659 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015660}
15661
15662/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015663 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 */
15665/*ARGSUSED*/
15666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015667f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015668 typval_T *argvars;
15669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670{
15671 int id = 0;
15672#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015673 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 long col;
15675 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015676 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015678 lnum = get_tv_lnum(argvars); /* -1 on type error */
15679 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15680 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015682 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015683 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015684 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685#endif
15686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015687 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688}
15689
15690/*
15691 * "synIDattr(id, what [, mode])" function
15692 */
15693/*ARGSUSED*/
15694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *argvars;
15697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
15699 char_u *p = NULL;
15700#ifdef FEAT_SYN_HL
15701 int id;
15702 char_u *what;
15703 char_u *mode;
15704 char_u modebuf[NUMBUFLEN];
15705 int modec;
15706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015707 id = get_tv_number(&argvars[0]);
15708 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015711 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 modec = TOLOWER_ASC(mode[0]);
15713 if (modec != 't' && modec != 'c'
15714#ifdef FEAT_GUI
15715 && modec != 'g'
15716#endif
15717 )
15718 modec = 0; /* replace invalid with current */
15719 }
15720 else
15721 {
15722#ifdef FEAT_GUI
15723 if (gui.in_use)
15724 modec = 'g';
15725 else
15726#endif
15727 if (t_colors > 1)
15728 modec = 'c';
15729 else
15730 modec = 't';
15731 }
15732
15733
15734 switch (TOLOWER_ASC(what[0]))
15735 {
15736 case 'b':
15737 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15738 p = highlight_color(id, what, modec);
15739 else /* bold */
15740 p = highlight_has_attr(id, HL_BOLD, modec);
15741 break;
15742
15743 case 'f': /* fg[#] */
15744 p = highlight_color(id, what, modec);
15745 break;
15746
15747 case 'i':
15748 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15749 p = highlight_has_attr(id, HL_INVERSE, modec);
15750 else /* italic */
15751 p = highlight_has_attr(id, HL_ITALIC, modec);
15752 break;
15753
15754 case 'n': /* name */
15755 p = get_highlight_name(NULL, id - 1);
15756 break;
15757
15758 case 'r': /* reverse */
15759 p = highlight_has_attr(id, HL_INVERSE, modec);
15760 break;
15761
15762 case 's': /* standout */
15763 p = highlight_has_attr(id, HL_STANDOUT, modec);
15764 break;
15765
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015766 case 'u':
15767 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15768 /* underline */
15769 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15770 else
15771 /* undercurl */
15772 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 break;
15774 }
15775
15776 if (p != NULL)
15777 p = vim_strsave(p);
15778#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015779 rettv->v_type = VAR_STRING;
15780 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781}
15782
15783/*
15784 * "synIDtrans(id)" function
15785 */
15786/*ARGSUSED*/
15787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015789 typval_T *argvars;
15790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791{
15792 int id;
15793
15794#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015795 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796
15797 if (id > 0)
15798 id = syn_get_final_id(id);
15799 else
15800#endif
15801 id = 0;
15802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015803 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804}
15805
15806/*
15807 * "system()" function
15808 */
15809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015810f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015811 typval_T *argvars;
15812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015814 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015816 char_u *infile = NULL;
15817 char_u buf[NUMBUFLEN];
15818 int err = FALSE;
15819 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015821 if (check_restricted() || check_secure())
15822 return;
15823
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015825 {
15826 /*
15827 * Write the string to a temp file, to be used for input of the shell
15828 * command.
15829 */
15830 if ((infile = vim_tempname('i')) == NULL)
15831 {
15832 EMSG(_(e_notmp));
15833 return;
15834 }
15835
15836 fd = mch_fopen((char *)infile, WRITEBIN);
15837 if (fd == NULL)
15838 {
15839 EMSG2(_(e_notopen), infile);
15840 goto done;
15841 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015842 p = get_tv_string_buf_chk(&argvars[1], buf);
15843 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015844 {
15845 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015846 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015847 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015848 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15849 err = TRUE;
15850 if (fclose(fd) != 0)
15851 err = TRUE;
15852 if (err)
15853 {
15854 EMSG(_("E677: Error writing temp file"));
15855 goto done;
15856 }
15857 }
15858
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015859 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15860 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015861
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862#ifdef USE_CR
15863 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015864 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 {
15866 char_u *s;
15867
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015868 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 {
15870 if (*s == CAR)
15871 *s = NL;
15872 }
15873 }
15874#else
15875# ifdef USE_CRNL
15876 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015877 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 {
15879 char_u *s, *d;
15880
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015881 d = res;
15882 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 {
15884 if (s[0] == CAR && s[1] == NL)
15885 ++s;
15886 *d++ = *s;
15887 }
15888 *d = NUL;
15889 }
15890# endif
15891#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015892
15893done:
15894 if (infile != NULL)
15895 {
15896 mch_remove(infile);
15897 vim_free(infile);
15898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015899 rettv->v_type = VAR_STRING;
15900 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901}
15902
15903/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015904 * "tabpagebuflist()" function
15905 */
15906/* ARGSUSED */
15907 static void
15908f_tabpagebuflist(argvars, rettv)
15909 typval_T *argvars;
15910 typval_T *rettv;
15911{
15912#ifndef FEAT_WINDOWS
15913 rettv->vval.v_number = 0;
15914#else
15915 tabpage_T *tp;
15916 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015917
15918 if (argvars[0].v_type == VAR_UNKNOWN)
15919 wp = firstwin;
15920 else
15921 {
15922 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15923 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015924 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015925 }
15926 if (wp == NULL)
15927 rettv->vval.v_number = 0;
15928 else
15929 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015930 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015931 rettv->vval.v_number = 0;
15932 else
15933 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015934 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015935 if (list_append_number(rettv->vval.v_list,
15936 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015937 break;
15938 }
15939 }
15940#endif
15941}
15942
15943
15944/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015945 * "tabpagenr()" function
15946 */
15947/* ARGSUSED */
15948 static void
15949f_tabpagenr(argvars, rettv)
15950 typval_T *argvars;
15951 typval_T *rettv;
15952{
15953 int nr = 1;
15954#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015955 char_u *arg;
15956
15957 if (argvars[0].v_type != VAR_UNKNOWN)
15958 {
15959 arg = get_tv_string_chk(&argvars[0]);
15960 nr = 0;
15961 if (arg != NULL)
15962 {
15963 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015964 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015965 else
15966 EMSG2(_(e_invexpr2), arg);
15967 }
15968 }
15969 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015970 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015971#endif
15972 rettv->vval.v_number = nr;
15973}
15974
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015975
15976#ifdef FEAT_WINDOWS
15977static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15978
15979/*
15980 * Common code for tabpagewinnr() and winnr().
15981 */
15982 static int
15983get_winnr(tp, argvar)
15984 tabpage_T *tp;
15985 typval_T *argvar;
15986{
15987 win_T *twin;
15988 int nr = 1;
15989 win_T *wp;
15990 char_u *arg;
15991
15992 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15993 if (argvar->v_type != VAR_UNKNOWN)
15994 {
15995 arg = get_tv_string_chk(argvar);
15996 if (arg == NULL)
15997 nr = 0; /* type error; errmsg already given */
15998 else if (STRCMP(arg, "$") == 0)
15999 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16000 else if (STRCMP(arg, "#") == 0)
16001 {
16002 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16003 if (twin == NULL)
16004 nr = 0;
16005 }
16006 else
16007 {
16008 EMSG2(_(e_invexpr2), arg);
16009 nr = 0;
16010 }
16011 }
16012
16013 if (nr > 0)
16014 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16015 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016016 {
16017 if (wp == NULL)
16018 {
16019 /* didn't find it in this tabpage */
16020 nr = 0;
16021 break;
16022 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016023 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016024 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016025 return nr;
16026}
16027#endif
16028
16029/*
16030 * "tabpagewinnr()" function
16031 */
16032/* ARGSUSED */
16033 static void
16034f_tabpagewinnr(argvars, rettv)
16035 typval_T *argvars;
16036 typval_T *rettv;
16037{
16038 int nr = 1;
16039#ifdef FEAT_WINDOWS
16040 tabpage_T *tp;
16041
16042 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16043 if (tp == NULL)
16044 nr = 0;
16045 else
16046 nr = get_winnr(tp, &argvars[1]);
16047#endif
16048 rettv->vval.v_number = nr;
16049}
16050
16051
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016052/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016053 * "tagfiles()" function
16054 */
16055/*ARGSUSED*/
16056 static void
16057f_tagfiles(argvars, rettv)
16058 typval_T *argvars;
16059 typval_T *rettv;
16060{
16061 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016062 tagname_T tn;
16063 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016064
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016065 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016066 {
16067 rettv->vval.v_number = 0;
16068 return;
16069 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016070
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016071 for (first = TRUE; ; first = FALSE)
16072 if (get_tagfname(&tn, first, fname) == FAIL
16073 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016074 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016075 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016076}
16077
16078/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016079 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016080 */
16081 static void
16082f_taglist(argvars, rettv)
16083 typval_T *argvars;
16084 typval_T *rettv;
16085{
16086 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016087
16088 tag_pattern = get_tv_string(&argvars[0]);
16089
16090 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 if (*tag_pattern == NUL)
16092 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016094 if (rettv_list_alloc(rettv) == OK)
16095 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016096}
16097
16098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 * "tempname()" function
16100 */
16101/*ARGSUSED*/
16102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016103f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016104 typval_T *argvars;
16105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106{
16107 static int x = 'A';
16108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109 rettv->v_type = VAR_STRING;
16110 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111
16112 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16113 * names. Skip 'I' and 'O', they are used for shell redirection. */
16114 do
16115 {
16116 if (x == 'Z')
16117 x = '0';
16118 else if (x == '9')
16119 x = 'A';
16120 else
16121 {
16122#ifdef EBCDIC
16123 if (x == 'I')
16124 x = 'J';
16125 else if (x == 'R')
16126 x = 'S';
16127 else
16128#endif
16129 ++x;
16130 }
16131 } while (x == 'I' || x == 'O');
16132}
16133
16134/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016135 * "test(list)" function: Just checking the walls...
16136 */
16137/*ARGSUSED*/
16138 static void
16139f_test(argvars, rettv)
16140 typval_T *argvars;
16141 typval_T *rettv;
16142{
16143 /* Used for unit testing. Change the code below to your liking. */
16144#if 0
16145 listitem_T *li;
16146 list_T *l;
16147 char_u *bad, *good;
16148
16149 if (argvars[0].v_type != VAR_LIST)
16150 return;
16151 l = argvars[0].vval.v_list;
16152 if (l == NULL)
16153 return;
16154 li = l->lv_first;
16155 if (li == NULL)
16156 return;
16157 bad = get_tv_string(&li->li_tv);
16158 li = li->li_next;
16159 if (li == NULL)
16160 return;
16161 good = get_tv_string(&li->li_tv);
16162 rettv->vval.v_number = test_edit_score(bad, good);
16163#endif
16164}
16165
16166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 * "tolower(string)" function
16168 */
16169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 typval_T *argvars;
16172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173{
16174 char_u *p;
16175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176 p = vim_strsave(get_tv_string(&argvars[0]));
16177 rettv->v_type = VAR_STRING;
16178 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179
16180 if (p != NULL)
16181 while (*p != NUL)
16182 {
16183#ifdef FEAT_MBYTE
16184 int l;
16185
16186 if (enc_utf8)
16187 {
16188 int c, lc;
16189
16190 c = utf_ptr2char(p);
16191 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016192 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 /* TODO: reallocate string when byte count changes. */
16194 if (utf_char2len(lc) == l)
16195 utf_char2bytes(lc, p);
16196 p += l;
16197 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016198 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 p += l; /* skip multi-byte character */
16200 else
16201#endif
16202 {
16203 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16204 ++p;
16205 }
16206 }
16207}
16208
16209/*
16210 * "toupper(string)" function
16211 */
16212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 typval_T *argvars;
16215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016217 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016218 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219}
16220
16221/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016222 * "tr(string, fromstr, tostr)" function
16223 */
16224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 typval_T *argvars;
16227 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016228{
16229 char_u *instr;
16230 char_u *fromstr;
16231 char_u *tostr;
16232 char_u *p;
16233#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016234 int inlen;
16235 int fromlen;
16236 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016237 int idx;
16238 char_u *cpstr;
16239 int cplen;
16240 int first = TRUE;
16241#endif
16242 char_u buf[NUMBUFLEN];
16243 char_u buf2[NUMBUFLEN];
16244 garray_T ga;
16245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016247 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16248 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016249
16250 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016251 rettv->v_type = VAR_STRING;
16252 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 if (fromstr == NULL || tostr == NULL)
16254 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016255 ga_init2(&ga, (int)sizeof(char), 80);
16256
16257#ifdef FEAT_MBYTE
16258 if (!has_mbyte)
16259#endif
16260 /* not multi-byte: fromstr and tostr must be the same length */
16261 if (STRLEN(fromstr) != STRLEN(tostr))
16262 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016263#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016264error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016265#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016266 EMSG2(_(e_invarg2), fromstr);
16267 ga_clear(&ga);
16268 return;
16269 }
16270
16271 /* fromstr and tostr have to contain the same number of chars */
16272 while (*instr != NUL)
16273 {
16274#ifdef FEAT_MBYTE
16275 if (has_mbyte)
16276 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016277 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016278 cpstr = instr;
16279 cplen = inlen;
16280 idx = 0;
16281 for (p = fromstr; *p != NUL; p += fromlen)
16282 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016283 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016284 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16285 {
16286 for (p = tostr; *p != NUL; p += tolen)
16287 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016288 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016289 if (idx-- == 0)
16290 {
16291 cplen = tolen;
16292 cpstr = p;
16293 break;
16294 }
16295 }
16296 if (*p == NUL) /* tostr is shorter than fromstr */
16297 goto error;
16298 break;
16299 }
16300 ++idx;
16301 }
16302
16303 if (first && cpstr == instr)
16304 {
16305 /* Check that fromstr and tostr have the same number of
16306 * (multi-byte) characters. Done only once when a character
16307 * of instr doesn't appear in fromstr. */
16308 first = FALSE;
16309 for (p = tostr; *p != NUL; p += tolen)
16310 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016311 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016312 --idx;
16313 }
16314 if (idx != 0)
16315 goto error;
16316 }
16317
16318 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016319 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016320 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016321
16322 instr += inlen;
16323 }
16324 else
16325#endif
16326 {
16327 /* When not using multi-byte chars we can do it faster. */
16328 p = vim_strchr(fromstr, *instr);
16329 if (p != NULL)
16330 ga_append(&ga, tostr[p - fromstr]);
16331 else
16332 ga_append(&ga, *instr);
16333 ++instr;
16334 }
16335 }
16336
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016337 /* add a terminating NUL */
16338 ga_grow(&ga, 1);
16339 ga_append(&ga, NUL);
16340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016341 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016342}
16343
16344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 * "type(expr)" function
16346 */
16347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016348f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 typval_T *argvars;
16350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016352 int n;
16353
16354 switch (argvars[0].v_type)
16355 {
16356 case VAR_NUMBER: n = 0; break;
16357 case VAR_STRING: n = 1; break;
16358 case VAR_FUNC: n = 2; break;
16359 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016360 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016361 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16362 }
16363 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364}
16365
16366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016367 * "values(dict)" function
16368 */
16369 static void
16370f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 typval_T *argvars;
16372 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016373{
16374 dict_list(argvars, rettv, 1);
16375}
16376
16377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 * "virtcol(string)" function
16379 */
16380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016381f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016382 typval_T *argvars;
16383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384{
16385 colnr_T vcol = 0;
16386 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016387 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016389 fp = var2fpos(&argvars[0], FALSE, &fnum);
16390 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16391 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 {
16393 getvvcol(curwin, fp, NULL, NULL, &vcol);
16394 ++vcol;
16395 }
16396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016397 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398}
16399
16400/*
16401 * "visualmode()" function
16402 */
16403/*ARGSUSED*/
16404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016405f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016406 typval_T *argvars;
16407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408{
16409#ifdef FEAT_VISUAL
16410 char_u str[2];
16411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 str[0] = curbuf->b_visual_mode_eval;
16414 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016415 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416
16417 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016418 if ((argvars[0].v_type == VAR_NUMBER
16419 && argvars[0].vval.v_number != 0)
16420 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 curbuf->b_visual_mode_eval = NUL;
16423#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016424 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425#endif
16426}
16427
16428/*
16429 * "winbufnr(nr)" function
16430 */
16431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016432f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 typval_T *argvars;
16434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435{
16436 win_T *wp;
16437
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016438 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016440 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016442 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443}
16444
16445/*
16446 * "wincol()" function
16447 */
16448/*ARGSUSED*/
16449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
16452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453{
16454 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016455 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456}
16457
16458/*
16459 * "winheight(nr)" function
16460 */
16461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 typval_T *argvars;
16464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465{
16466 win_T *wp;
16467
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016468 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016470 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016472 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473}
16474
16475/*
16476 * "winline()" function
16477 */
16478/*ARGSUSED*/
16479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016480f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016481 typval_T *argvars;
16482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483{
16484 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486}
16487
16488/*
16489 * "winnr()" function
16490 */
16491/* ARGSUSED */
16492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016494 typval_T *argvars;
16495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496{
16497 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016498
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016500 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503}
16504
16505/*
16506 * "winrestcmd()" function
16507 */
16508/* ARGSUSED */
16509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 typval_T *argvars;
16512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513{
16514#ifdef FEAT_WINDOWS
16515 win_T *wp;
16516 int winnr = 1;
16517 garray_T ga;
16518 char_u buf[50];
16519
16520 ga_init2(&ga, (int)sizeof(char), 70);
16521 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16522 {
16523 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16524 ga_concat(&ga, buf);
16525# ifdef FEAT_VERTSPLIT
16526 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16527 ga_concat(&ga, buf);
16528# endif
16529 ++winnr;
16530 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016531 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016533 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538}
16539
16540/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016541 * "winrestview()" function
16542 */
16543/* ARGSUSED */
16544 static void
16545f_winrestview(argvars, rettv)
16546 typval_T *argvars;
16547 typval_T *rettv;
16548{
16549 dict_T *dict;
16550
16551 if (argvars[0].v_type != VAR_DICT
16552 || (dict = argvars[0].vval.v_dict) == NULL)
16553 EMSG(_(e_invarg));
16554 else
16555 {
16556 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16557 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16558#ifdef FEAT_VIRTUALEDIT
16559 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16560#endif
16561 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016562 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016563
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016564 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016565#ifdef FEAT_DIFF
16566 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16567#endif
16568 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16569 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16570
16571 check_cursor();
16572 changed_cline_bef_curs();
16573 invalidate_botline();
16574 redraw_later(VALID);
16575
16576 if (curwin->w_topline == 0)
16577 curwin->w_topline = 1;
16578 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16579 curwin->w_topline = curbuf->b_ml.ml_line_count;
16580#ifdef FEAT_DIFF
16581 check_topfill(curwin, TRUE);
16582#endif
16583 }
16584}
16585
16586/*
16587 * "winsaveview()" function
16588 */
16589/* ARGSUSED */
16590 static void
16591f_winsaveview(argvars, rettv)
16592 typval_T *argvars;
16593 typval_T *rettv;
16594{
16595 dict_T *dict;
16596
16597 dict = dict_alloc();
16598 if (dict == NULL)
16599 return;
16600 rettv->v_type = VAR_DICT;
16601 rettv->vval.v_dict = dict;
16602 ++dict->dv_refcount;
16603
16604 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16605 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16606#ifdef FEAT_VIRTUALEDIT
16607 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16608#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016609 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016610 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16611
16612 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16613#ifdef FEAT_DIFF
16614 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16615#endif
16616 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16617 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16618}
16619
16620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 * "winwidth(nr)" function
16622 */
16623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016624f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016625 typval_T *argvars;
16626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627{
16628 win_T *wp;
16629
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016630 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 else
16634#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638#endif
16639}
16640
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642 * "writefile()" function
16643 */
16644 static void
16645f_writefile(argvars, rettv)
16646 typval_T *argvars;
16647 typval_T *rettv;
16648{
16649 int binary = FALSE;
16650 char_u *fname;
16651 FILE *fd;
16652 listitem_T *li;
16653 char_u *s;
16654 int ret = 0;
16655 int c;
16656
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016657 if (check_restricted() || check_secure())
16658 return;
16659
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660 if (argvars[0].v_type != VAR_LIST)
16661 {
16662 EMSG2(_(e_listarg), "writefile()");
16663 return;
16664 }
16665 if (argvars[0].vval.v_list == NULL)
16666 return;
16667
16668 if (argvars[2].v_type != VAR_UNKNOWN
16669 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16670 binary = TRUE;
16671
16672 /* Always open the file in binary mode, library functions have a mind of
16673 * their own about CR-LF conversion. */
16674 fname = get_tv_string(&argvars[1]);
16675 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16676 {
16677 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16678 ret = -1;
16679 }
16680 else
16681 {
16682 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16683 li = li->li_next)
16684 {
16685 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16686 {
16687 if (*s == '\n')
16688 c = putc(NUL, fd);
16689 else
16690 c = putc(*s, fd);
16691 if (c == EOF)
16692 {
16693 ret = -1;
16694 break;
16695 }
16696 }
16697 if (!binary || li->li_next != NULL)
16698 if (putc('\n', fd) == EOF)
16699 {
16700 ret = -1;
16701 break;
16702 }
16703 if (ret < 0)
16704 {
16705 EMSG(_(e_write));
16706 break;
16707 }
16708 }
16709 fclose(fd);
16710 }
16711
16712 rettv->vval.v_number = ret;
16713}
16714
16715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016717 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 */
16719 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016720var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016721 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016722 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016723 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016725 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016727 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728
Bram Moolenaara5525202006-03-02 22:52:09 +000016729 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016730 if (varp->v_type == VAR_LIST)
16731 {
16732 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016733 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016734 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016735 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016736
16737 l = varp->vval.v_list;
16738 if (l == NULL)
16739 return NULL;
16740
16741 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016742 pos.lnum = list_find_nr(l, 0L, &error);
16743 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016744 return NULL; /* invalid line number */
16745
16746 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016747 pos.col = list_find_nr(l, 1L, &error);
16748 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016749 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016750 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016751
16752 /* We accept "$" for the column number: last column. */
16753 li = list_find(l, 1L);
16754 if (li != NULL && li->li_tv.v_type == VAR_STRING
16755 && li->li_tv.vval.v_string != NULL
16756 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16757 pos.col = len + 1;
16758
Bram Moolenaara5525202006-03-02 22:52:09 +000016759 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016760 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016761 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016762 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016763
Bram Moolenaara5525202006-03-02 22:52:09 +000016764#ifdef FEAT_VIRTUALEDIT
16765 /* Get the virtual offset. Defaults to zero. */
16766 pos.coladd = list_find_nr(l, 2L, &error);
16767 if (error)
16768 pos.coladd = 0;
16769#endif
16770
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016771 return &pos;
16772 }
16773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016774 name = get_tv_string_chk(varp);
16775 if (name == NULL)
16776 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 if (name[0] == '.') /* cursor */
16778 return &curwin->w_cursor;
16779 if (name[0] == '\'') /* mark */
16780 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016781 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16783 return NULL;
16784 return pp;
16785 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016786
16787#ifdef FEAT_VIRTUALEDIT
16788 pos.coladd = 0;
16789#endif
16790
Bram Moolenaar477933c2007-07-17 14:32:23 +000016791 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016792 {
16793 pos.col = 0;
16794 if (name[1] == '0') /* "w0": first visible line */
16795 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016796 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016797 pos.lnum = curwin->w_topline;
16798 return &pos;
16799 }
16800 else if (name[1] == '$') /* "w$": last visible line */
16801 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016802 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016803 pos.lnum = curwin->w_botline - 1;
16804 return &pos;
16805 }
16806 }
16807 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016809 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 {
16811 pos.lnum = curbuf->b_ml.ml_line_count;
16812 pos.col = 0;
16813 }
16814 else
16815 {
16816 pos.lnum = curwin->w_cursor.lnum;
16817 pos.col = (colnr_T)STRLEN(ml_get_curline());
16818 }
16819 return &pos;
16820 }
16821 return NULL;
16822}
16823
16824/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016825 * Convert list in "arg" into a position and optional file number.
16826 * When "fnump" is NULL there is no file number, only 3 items.
16827 * Note that the column is passed on as-is, the caller may want to decrement
16828 * it to use 1 for the first column.
16829 * Return FAIL when conversion is not possible, doesn't check the position for
16830 * validity.
16831 */
16832 static int
16833list2fpos(arg, posp, fnump)
16834 typval_T *arg;
16835 pos_T *posp;
16836 int *fnump;
16837{
16838 list_T *l = arg->vval.v_list;
16839 long i = 0;
16840 long n;
16841
Bram Moolenaarbde35262006-07-23 20:12:24 +000016842 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16843 * when "fnump" isn't NULL and "coladd" is optional. */
16844 if (arg->v_type != VAR_LIST
16845 || l == NULL
16846 || l->lv_len < (fnump == NULL ? 2 : 3)
16847 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016848 return FAIL;
16849
16850 if (fnump != NULL)
16851 {
16852 n = list_find_nr(l, i++, NULL); /* fnum */
16853 if (n < 0)
16854 return FAIL;
16855 if (n == 0)
16856 n = curbuf->b_fnum; /* current buffer */
16857 *fnump = n;
16858 }
16859
16860 n = list_find_nr(l, i++, NULL); /* lnum */
16861 if (n < 0)
16862 return FAIL;
16863 posp->lnum = n;
16864
16865 n = list_find_nr(l, i++, NULL); /* col */
16866 if (n < 0)
16867 return FAIL;
16868 posp->col = n;
16869
16870#ifdef FEAT_VIRTUALEDIT
16871 n = list_find_nr(l, i, NULL);
16872 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016873 posp->coladd = 0;
16874 else
16875 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016876#endif
16877
16878 return OK;
16879}
16880
16881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 * Get the length of an environment variable name.
16883 * Advance "arg" to the first character after the name.
16884 * Return 0 for error.
16885 */
16886 static int
16887get_env_len(arg)
16888 char_u **arg;
16889{
16890 char_u *p;
16891 int len;
16892
16893 for (p = *arg; vim_isIDc(*p); ++p)
16894 ;
16895 if (p == *arg) /* no name found */
16896 return 0;
16897
16898 len = (int)(p - *arg);
16899 *arg = p;
16900 return len;
16901}
16902
16903/*
16904 * Get the length of the name of a function or internal variable.
16905 * "arg" is advanced to the first non-white character after the name.
16906 * Return 0 if something is wrong.
16907 */
16908 static int
16909get_id_len(arg)
16910 char_u **arg;
16911{
16912 char_u *p;
16913 int len;
16914
16915 /* Find the end of the name. */
16916 for (p = *arg; eval_isnamec(*p); ++p)
16917 ;
16918 if (p == *arg) /* no name found */
16919 return 0;
16920
16921 len = (int)(p - *arg);
16922 *arg = skipwhite(p);
16923
16924 return len;
16925}
16926
16927/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016928 * Get the length of the name of a variable or function.
16929 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016931 * Return -1 if curly braces expansion failed.
16932 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 * If the name contains 'magic' {}'s, expand them and return the
16934 * expanded name in an allocated string via 'alias' - caller must free.
16935 */
16936 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016937get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 char_u **arg;
16939 char_u **alias;
16940 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016941 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942{
16943 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 char_u *p;
16945 char_u *expr_start;
16946 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
16948 *alias = NULL; /* default to no alias */
16949
16950 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16951 && (*arg)[2] == (int)KE_SNR)
16952 {
16953 /* hard coded <SNR>, already translated */
16954 *arg += 3;
16955 return get_id_len(arg) + 3;
16956 }
16957 len = eval_fname_script(*arg);
16958 if (len > 0)
16959 {
16960 /* literal "<SID>", "s:" or "<SNR>" */
16961 *arg += len;
16962 }
16963
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016965 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016967 p = find_name_end(*arg, &expr_start, &expr_end,
16968 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 if (expr_start != NULL)
16970 {
16971 char_u *temp_string;
16972
16973 if (!evaluate)
16974 {
16975 len += (int)(p - *arg);
16976 *arg = skipwhite(p);
16977 return len;
16978 }
16979
16980 /*
16981 * Include any <SID> etc in the expanded string:
16982 * Thus the -len here.
16983 */
16984 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16985 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016986 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 *alias = temp_string;
16988 *arg = skipwhite(p);
16989 return (int)STRLEN(temp_string);
16990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
16992 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016993 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 EMSG2(_(e_invexpr2), *arg);
16995
16996 return len;
16997}
16998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016999/*
17000 * Find the end of a variable or function name, taking care of magic braces.
17001 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17002 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017003 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 * Return a pointer to just after the name. Equal to "arg" if there is no
17005 * valid name.
17006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017008find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 char_u *arg;
17010 char_u **expr_start;
17011 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017012 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017014 int mb_nest = 0;
17015 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 char_u *p;
17017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017018 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 *expr_start = NULL;
17021 *expr_end = NULL;
17022 }
17023
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017024 /* Quick check for valid starting character. */
17025 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17026 return arg;
17027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017028 for (p = arg; *p != NUL
17029 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017030 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017031 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017032 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017033 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017035 if (*p == '\'')
17036 {
17037 /* skip over 'string' to avoid counting [ and ] inside it. */
17038 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17039 ;
17040 if (*p == NUL)
17041 break;
17042 }
17043 else if (*p == '"')
17044 {
17045 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17046 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17047 if (*p == '\\' && p[1] != NUL)
17048 ++p;
17049 if (*p == NUL)
17050 break;
17051 }
17052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017053 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017055 if (*p == '[')
17056 ++br_nest;
17057 else if (*p == ']')
17058 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063 if (*p == '{')
17064 {
17065 mb_nest++;
17066 if (expr_start != NULL && *expr_start == NULL)
17067 *expr_start = p;
17068 }
17069 else if (*p == '}')
17070 {
17071 mb_nest--;
17072 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17073 *expr_end = p;
17074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 }
17077
17078 return p;
17079}
17080
17081/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017082 * Expands out the 'magic' {}'s in a variable/function name.
17083 * Note that this can call itself recursively, to deal with
17084 * constructs like foo{bar}{baz}{bam}
17085 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17086 * "in_start" ^
17087 * "expr_start" ^
17088 * "expr_end" ^
17089 * "in_end" ^
17090 *
17091 * Returns a new allocated string, which the caller must free.
17092 * Returns NULL for failure.
17093 */
17094 static char_u *
17095make_expanded_name(in_start, expr_start, expr_end, in_end)
17096 char_u *in_start;
17097 char_u *expr_start;
17098 char_u *expr_end;
17099 char_u *in_end;
17100{
17101 char_u c1;
17102 char_u *retval = NULL;
17103 char_u *temp_result;
17104 char_u *nextcmd = NULL;
17105
17106 if (expr_end == NULL || in_end == NULL)
17107 return NULL;
17108 *expr_start = NUL;
17109 *expr_end = NUL;
17110 c1 = *in_end;
17111 *in_end = NUL;
17112
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017113 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017114 if (temp_result != NULL && nextcmd == NULL)
17115 {
17116 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17117 + (in_end - expr_end) + 1));
17118 if (retval != NULL)
17119 {
17120 STRCPY(retval, in_start);
17121 STRCAT(retval, temp_result);
17122 STRCAT(retval, expr_end + 1);
17123 }
17124 }
17125 vim_free(temp_result);
17126
17127 *in_end = c1; /* put char back for error messages */
17128 *expr_start = '{';
17129 *expr_end = '}';
17130
17131 if (retval != NULL)
17132 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017133 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017134 if (expr_start != NULL)
17135 {
17136 /* Further expansion! */
17137 temp_result = make_expanded_name(retval, expr_start,
17138 expr_end, temp_result);
17139 vim_free(retval);
17140 retval = temp_result;
17141 }
17142 }
17143
17144 return retval;
17145}
17146
17147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017149 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 */
17151 static int
17152eval_isnamec(c)
17153 int c;
17154{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017155 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17156}
17157
17158/*
17159 * Return TRUE if character "c" can be used as the first character in a
17160 * variable or function name (excluding '{' and '}').
17161 */
17162 static int
17163eval_isnamec1(c)
17164 int c;
17165{
17166 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167}
17168
17169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 * Set number v: variable to "val".
17171 */
17172 void
17173set_vim_var_nr(idx, val)
17174 int idx;
17175 long val;
17176{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017177 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178}
17179
17180/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017181 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 */
17183 long
17184get_vim_var_nr(idx)
17185 int idx;
17186{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017187 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188}
17189
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017190#if defined(FEAT_AUTOCMD) || defined(PROTO)
17191/*
17192 * Get string v: variable value. Uses a static buffer, can only be used once.
17193 */
17194 char_u *
17195get_vim_var_str(idx)
17196 int idx;
17197{
17198 return get_tv_string(&vimvars[idx].vv_tv);
17199}
17200#endif
17201
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202/*
17203 * Set v:count, v:count1 and v:prevcount.
17204 */
17205 void
17206set_vcount(count, count1)
17207 long count;
17208 long count1;
17209{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017210 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17211 vimvars[VV_COUNT].vv_nr = count;
17212 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213}
17214
17215/*
17216 * Set string v: variable to a copy of "val".
17217 */
17218 void
17219set_vim_var_string(idx, val, len)
17220 int idx;
17221 char_u *val;
17222 int len; /* length of "val" to use or -1 (whole string) */
17223{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017224 /* Need to do this (at least) once, since we can't initialize a union.
17225 * Will always be invoked when "v:progname" is set. */
17226 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17227
Bram Moolenaare9a41262005-01-15 22:18:47 +000017228 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017230 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017232 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017234 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235}
17236
17237/*
17238 * Set v:register if needed.
17239 */
17240 void
17241set_reg_var(c)
17242 int c;
17243{
17244 char_u regname;
17245
17246 if (c == 0 || c == ' ')
17247 regname = '"';
17248 else
17249 regname = c;
17250 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017251 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 set_vim_var_string(VV_REG, &regname, 1);
17253}
17254
17255/*
17256 * Get or set v:exception. If "oldval" == NULL, return the current value.
17257 * Otherwise, restore the value to "oldval" and return NULL.
17258 * Must always be called in pairs to save and restore v:exception! Does not
17259 * take care of memory allocations.
17260 */
17261 char_u *
17262v_exception(oldval)
17263 char_u *oldval;
17264{
17265 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017266 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267
Bram Moolenaare9a41262005-01-15 22:18:47 +000017268 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 return NULL;
17270}
17271
17272/*
17273 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17274 * Otherwise, restore the value to "oldval" and return NULL.
17275 * Must always be called in pairs to save and restore v:throwpoint! Does not
17276 * take care of memory allocations.
17277 */
17278 char_u *
17279v_throwpoint(oldval)
17280 char_u *oldval;
17281{
17282 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017283 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284
Bram Moolenaare9a41262005-01-15 22:18:47 +000017285 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 return NULL;
17287}
17288
17289#if defined(FEAT_AUTOCMD) || defined(PROTO)
17290/*
17291 * Set v:cmdarg.
17292 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17293 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17294 * Must always be called in pairs!
17295 */
17296 char_u *
17297set_cmdarg(eap, oldarg)
17298 exarg_T *eap;
17299 char_u *oldarg;
17300{
17301 char_u *oldval;
17302 char_u *newval;
17303 unsigned len;
17304
Bram Moolenaare9a41262005-01-15 22:18:47 +000017305 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017306 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017308 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017309 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017310 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 }
17312
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017313 if (eap->force_bin == FORCE_BIN)
17314 len = 6;
17315 else if (eap->force_bin == FORCE_NOBIN)
17316 len = 8;
17317 else
17318 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017319
17320 if (eap->read_edit)
17321 len += 7;
17322
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017323 if (eap->force_ff != 0)
17324 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17325# ifdef FEAT_MBYTE
17326 if (eap->force_enc != 0)
17327 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017328 if (eap->bad_char != 0)
17329 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017330# endif
17331
17332 newval = alloc(len + 1);
17333 if (newval == NULL)
17334 return NULL;
17335
17336 if (eap->force_bin == FORCE_BIN)
17337 sprintf((char *)newval, " ++bin");
17338 else if (eap->force_bin == FORCE_NOBIN)
17339 sprintf((char *)newval, " ++nobin");
17340 else
17341 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017342
17343 if (eap->read_edit)
17344 STRCAT(newval, " ++edit");
17345
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017346 if (eap->force_ff != 0)
17347 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17348 eap->cmd + eap->force_ff);
17349# ifdef FEAT_MBYTE
17350 if (eap->force_enc != 0)
17351 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17352 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017353 if (eap->bad_char != 0)
17354 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17355 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017356# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017357 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017358 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359}
17360#endif
17361
17362/*
17363 * Get the value of internal variable "name".
17364 * Return OK or FAIL.
17365 */
17366 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017367get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 char_u *name;
17369 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017371 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372{
17373 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017374 typval_T *tv = NULL;
17375 typval_T atv;
17376 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
17379 /* truncate the name, so that we can use strcmp() */
17380 cc = name[len];
17381 name[len] = NUL;
17382
17383 /*
17384 * Check for "b:changedtick".
17385 */
17386 if (STRCMP(name, "b:changedtick") == 0)
17387 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017388 atv.v_type = VAR_NUMBER;
17389 atv.vval.v_number = curbuf->b_changedtick;
17390 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 }
17392
17393 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 * Check for user-defined variables.
17395 */
17396 else
17397 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017398 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 }
17402
Bram Moolenaare9a41262005-01-15 22:18:47 +000017403 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017405 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 ret = FAIL;
17408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017410 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411
17412 name[len] = cc;
17413
17414 return ret;
17415}
17416
17417/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017418 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17419 * Also handle function call with Funcref variable: func(expr)
17420 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17421 */
17422 static int
17423handle_subscript(arg, rettv, evaluate, verbose)
17424 char_u **arg;
17425 typval_T *rettv;
17426 int evaluate; /* do more than finding the end */
17427 int verbose; /* give error messages */
17428{
17429 int ret = OK;
17430 dict_T *selfdict = NULL;
17431 char_u *s;
17432 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017433 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017434
17435 while (ret == OK
17436 && (**arg == '['
17437 || (**arg == '.' && rettv->v_type == VAR_DICT)
17438 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17439 && !vim_iswhite(*(*arg - 1)))
17440 {
17441 if (**arg == '(')
17442 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017443 /* need to copy the funcref so that we can clear rettv */
17444 functv = *rettv;
17445 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017446
17447 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017448 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017449 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017450 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17451 &len, evaluate, selfdict);
17452
17453 /* Clear the funcref afterwards, so that deleting it while
17454 * evaluating the arguments is possible (see test55). */
17455 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017456
17457 /* Stop the expression evaluation when immediately aborting on
17458 * error, or when an interrupt occurred or an exception was thrown
17459 * but not caught. */
17460 if (aborting())
17461 {
17462 if (ret == OK)
17463 clear_tv(rettv);
17464 ret = FAIL;
17465 }
17466 dict_unref(selfdict);
17467 selfdict = NULL;
17468 }
17469 else /* **arg == '[' || **arg == '.' */
17470 {
17471 dict_unref(selfdict);
17472 if (rettv->v_type == VAR_DICT)
17473 {
17474 selfdict = rettv->vval.v_dict;
17475 if (selfdict != NULL)
17476 ++selfdict->dv_refcount;
17477 }
17478 else
17479 selfdict = NULL;
17480 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17481 {
17482 clear_tv(rettv);
17483 ret = FAIL;
17484 }
17485 }
17486 }
17487 dict_unref(selfdict);
17488 return ret;
17489}
17490
17491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017492 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17493 * value).
17494 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017497{
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017499}
17500
17501/*
17502 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 * The string "s" must have been allocated, it is consumed.
17504 * Return NULL for out of memory, the variable otherwise.
17505 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 char_u *s;
17509{
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv = alloc_tv();
17513 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 rettv->v_type = VAR_STRING;
17516 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 }
17518 else
17519 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521}
17522
17523/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017524 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017526 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529{
17530 if (varp != NULL)
17531 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017532 switch (varp->v_type)
17533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017534 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017535 func_unref(varp->vval.v_string);
17536 /*FALLTHROUGH*/
17537 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017538 vim_free(varp->vval.v_string);
17539 break;
17540 case VAR_LIST:
17541 list_unref(varp->vval.v_list);
17542 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017543 case VAR_DICT:
17544 dict_unref(varp->vval.v_dict);
17545 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017546 case VAR_NUMBER:
17547 case VAR_UNKNOWN:
17548 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017549 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017550 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017551 break;
17552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 vim_free(varp);
17554 }
17555}
17556
17557/*
17558 * Free the memory for a variable value and set the value to NULL or 0.
17559 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017560 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017561clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017562 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563{
17564 if (varp != NULL)
17565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017566 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017568 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017569 func_unref(varp->vval.v_string);
17570 /*FALLTHROUGH*/
17571 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017572 vim_free(varp->vval.v_string);
17573 varp->vval.v_string = NULL;
17574 break;
17575 case VAR_LIST:
17576 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017577 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017578 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017579 case VAR_DICT:
17580 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017581 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017582 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017583 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017584 varp->vval.v_number = 0;
17585 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 case VAR_UNKNOWN:
17587 break;
17588 default:
17589 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017591 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 }
17593}
17594
17595/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596 * Set the value of a variable to NULL without freeing items.
17597 */
17598 static void
17599init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017600 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017601{
17602 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017603 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017604}
17605
17606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 * Get the number value of a variable.
17608 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017609 * For incompatible types, return 0.
17610 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17611 * caller of incompatible types: it sets *denote to TRUE if "denote"
17612 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 */
17614 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017618 int error = FALSE;
17619
17620 return get_tv_number_chk(varp, &error); /* return 0L on error */
17621}
17622
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017623 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624get_tv_number_chk(varp, denote)
17625 typval_T *varp;
17626 int *denote;
17627{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017628 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017630 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017632 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017633 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017634 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017635 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 break;
17637 case VAR_STRING:
17638 if (varp->vval.v_string != NULL)
17639 vim_str2nr(varp->vval.v_string, NULL, NULL,
17640 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017641 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017642 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017643 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017644 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017645 case VAR_DICT:
17646 EMSG(_("E728: Using a Dictionary as a number"));
17647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017648 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017649 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017650 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017652 if (denote == NULL) /* useful for values that must be unsigned */
17653 n = -1;
17654 else
17655 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657}
17658
17659/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017660 * Get the lnum from the first argument.
17661 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 */
17664 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017665get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017666 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667{
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 linenr_T lnum;
17670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017671 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 if (lnum == 0) /* no valid number, try using line() */
17673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 rettv.v_type = VAR_NUMBER;
17675 f_line(argvars, &rettv);
17676 lnum = rettv.vval.v_number;
17677 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 }
17679 return lnum;
17680}
17681
17682/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017683 * Get the lnum from the first argument.
17684 * Also accepts "$", then "buf" is used.
17685 * Returns 0 on error.
17686 */
17687 static linenr_T
17688get_tv_lnum_buf(argvars, buf)
17689 typval_T *argvars;
17690 buf_T *buf;
17691{
17692 if (argvars[0].v_type == VAR_STRING
17693 && argvars[0].vval.v_string != NULL
17694 && argvars[0].vval.v_string[0] == '$'
17695 && buf != NULL)
17696 return buf->b_ml.ml_line_count;
17697 return get_tv_number_chk(&argvars[0], NULL);
17698}
17699
17700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 * Get the string value of a variable.
17702 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017703 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17704 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 * If the String variable has never been set, return an empty string.
17706 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17708 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 */
17710 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713{
17714 static char_u mybuf[NUMBUFLEN];
17715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717}
17718
17719 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017721 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017722 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017724 char_u *res = get_tv_string_buf_chk(varp, buf);
17725
17726 return res != NULL ? res : (char_u *)"";
17727}
17728
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017729 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730get_tv_string_chk(varp)
17731 typval_T *varp;
17732{
17733 static char_u mybuf[NUMBUFLEN];
17734
17735 return get_tv_string_buf_chk(varp, mybuf);
17736}
17737
17738 static char_u *
17739get_tv_string_buf_chk(varp, buf)
17740 typval_T *varp;
17741 char_u *buf;
17742{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017743 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017745 case VAR_NUMBER:
17746 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17747 return buf;
17748 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017749 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017750 break;
17751 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017752 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017753 break;
17754 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017755 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017756 break;
17757 case VAR_STRING:
17758 if (varp->vval.v_string != NULL)
17759 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017761 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017763 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017765 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766}
17767
17768/*
17769 * Find variable "name" in the list of variables.
17770 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017772 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017773 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017776find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782
Bram Moolenaara7043832005-01-21 11:56:39 +000017783 ht = find_var_ht(name, &varname);
17784 if (htp != NULL)
17785 *htp = ht;
17786 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017788 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789}
17790
17791/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017792 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017793 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017796find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017797 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017798 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017799 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017800{
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 hashitem_T *hi;
17802
17803 if (*varname == NUL)
17804 {
17805 /* Must be something like "s:", otherwise "ht" would be NULL. */
17806 switch (varname[-2])
17807 {
17808 case 's': return &SCRIPT_SV(current_SID).sv_var;
17809 case 'g': return &globvars_var;
17810 case 'v': return &vimvars_var;
17811 case 'b': return &curbuf->b_bufvar;
17812 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017813#ifdef FEAT_WINDOWS
17814 case 't': return &curtab->tp_winvar;
17815#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017816 case 'l': return current_funccal == NULL
17817 ? NULL : &current_funccal->l_vars_var;
17818 case 'a': return current_funccal == NULL
17819 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017820 }
17821 return NULL;
17822 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017823
17824 hi = hash_find(ht, varname);
17825 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017826 {
17827 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017828 * worked find the variable again. Don't auto-load a script if it was
17829 * loaded already, otherwise it would be loaded every time when
17830 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017831 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017832 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017833 hi = hash_find(ht, varname);
17834 if (HASHITEM_EMPTY(hi))
17835 return NULL;
17836 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017837 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017838}
17839
17840/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017842 * Set "varname" to the start of name without ':'.
17843 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017845find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 char_u *name;
17847 char_u **varname;
17848{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017849 hashitem_T *hi;
17850
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 if (name[1] != ':')
17852 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017853 /* The name must not start with a colon or #. */
17854 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 return NULL;
17856 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017857
17858 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017859 hi = hash_find(&compat_hashtab, name);
17860 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017861 return &compat_hashtab;
17862
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 return &globvarht; /* global variable */
17865 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 }
17867 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017868 if (*name == 'g') /* global variable */
17869 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017870 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17871 */
17872 if (vim_strchr(name + 2, ':') != NULL
17873 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017874 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017876 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017878 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017879#ifdef FEAT_WINDOWS
17880 if (*name == 't') /* tab page variable */
17881 return &curtab->tp_vars.dv_hashtab;
17882#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017883 if (*name == 'v') /* v: variable */
17884 return &vimvarht;
17885 if (*name == 'a' && current_funccal != NULL) /* function argument */
17886 return &current_funccal->l_avars.dv_hashtab;
17887 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17888 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 if (*name == 's' /* script variable */
17890 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17891 return &SCRIPT_VARS(current_SID);
17892 return NULL;
17893}
17894
17895/*
17896 * Get the string value of a (global/local) variable.
17897 * Returns NULL when it doesn't exist.
17898 */
17899 char_u *
17900get_var_value(name)
17901 char_u *name;
17902{
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904
Bram Moolenaara7043832005-01-21 11:56:39 +000017905 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 if (v == NULL)
17907 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909}
17910
17911/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 * sourcing this script and when executing functions defined in the script.
17914 */
17915 void
17916new_script_vars(id)
17917 scid_T id;
17918{
Bram Moolenaara7043832005-01-21 11:56:39 +000017919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017920 hashtab_T *ht;
17921 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017922
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17924 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017925 /* Re-allocating ga_data means that an ht_array pointing to
17926 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017927 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017928 for (i = 1; i <= ga_scripts.ga_len; ++i)
17929 {
17930 ht = &SCRIPT_VARS(i);
17931 if (ht->ht_mask == HT_INIT_SIZE - 1)
17932 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 sv = &SCRIPT_SV(i);
17934 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017935 }
17936
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 while (ga_scripts.ga_len < id)
17938 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017939 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17940 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 }
17943 }
17944}
17945
17946/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17948 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 */
17950 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017951init_var_dict(dict, dict_var)
17952 dict_T *dict;
17953 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954{
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 hash_init(&dict->dv_hashtab);
17956 dict->dv_refcount = 99999;
17957 dict_var->di_tv.vval.v_dict = dict;
17958 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017959 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017960 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17961 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962}
17963
17964/*
17965 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017966 * Frees all allocated variables and the value they contain.
17967 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 */
17969 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017970vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017971 hashtab_T *ht;
17972{
17973 vars_clear_ext(ht, TRUE);
17974}
17975
17976/*
17977 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17978 */
17979 static void
17980vars_clear_ext(ht, free_val)
17981 hashtab_T *ht;
17982 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983{
Bram Moolenaara7043832005-01-21 11:56:39 +000017984 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 hashitem_T *hi;
17986 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987
Bram Moolenaar33570922005-01-25 22:26:29 +000017988 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017989 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017990 for (hi = ht->ht_array; todo > 0; ++hi)
17991 {
17992 if (!HASHITEM_EMPTY(hi))
17993 {
17994 --todo;
17995
Bram Moolenaar33570922005-01-25 22:26:29 +000017996 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017997 * ht_array might change then. hash_clear() takes care of it
17998 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017999 v = HI2DI(hi);
18000 if (free_val)
18001 clear_tv(&v->di_tv);
18002 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18003 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018004 }
18005 }
18006 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018007 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008}
18009
Bram Moolenaara7043832005-01-21 11:56:39 +000018010/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018011 * Delete a variable from hashtab "ht" at item "hi".
18012 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018015delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 hashtab_T *ht;
18017 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018{
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018020
18021 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 clear_tv(&di->di_tv);
18023 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024}
18025
18026/*
18027 * List the value of one internal variable.
18028 */
18029 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018030list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018031 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018033 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018035 char_u *tofree;
18036 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018037 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018038
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018039 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018041 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018042 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043}
18044
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018046list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 char_u *prefix;
18048 char_u *name;
18049 int type;
18050 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018051 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052{
Bram Moolenaar31859182007-08-14 20:41:13 +000018053 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18054 msg_start();
18055 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 if (name != NULL) /* "a:" vars don't have a name stored */
18057 msg_puts(name);
18058 msg_putchar(' ');
18059 msg_advance(22);
18060 if (type == VAR_NUMBER)
18061 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018062 else if (type == VAR_FUNC)
18063 msg_putchar('*');
18064 else if (type == VAR_LIST)
18065 {
18066 msg_putchar('[');
18067 if (*string == '[')
18068 ++string;
18069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018070 else if (type == VAR_DICT)
18071 {
18072 msg_putchar('{');
18073 if (*string == '{')
18074 ++string;
18075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 else
18077 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018078
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018080
18081 if (type == VAR_FUNC)
18082 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018083 if (*first)
18084 {
18085 msg_clr_eos();
18086 *first = FALSE;
18087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088}
18089
18090/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018091 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 * If the variable already exists, the value is updated.
18093 * Otherwise the variable is created.
18094 */
18095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018096set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018098 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018099 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100{
Bram Moolenaar33570922005-01-25 22:26:29 +000018101 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018103 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018104 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018106 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018107 {
18108 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18109 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18110 ? name[2] : name[0]))
18111 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018112 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018113 return;
18114 }
18115 if (function_exists(name))
18116 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018117 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018118 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018119 return;
18120 }
18121 }
18122
Bram Moolenaara7043832005-01-21 11:56:39 +000018123 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018124 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018125 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018126 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018127 return;
18128 }
18129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018130 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018131 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018134 if (var_check_ro(v->di_flags, name)
18135 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 return;
18137 if (v->di_tv.v_type != tv->v_type
18138 && !((v->di_tv.v_type == VAR_STRING
18139 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140 && (tv->v_type == VAR_STRING
18141 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018142 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018143 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018144 return;
18145 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018146
18147 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018148 * Handle setting internal v: variables separately: we don't change
18149 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018150 */
18151 if (ht == &vimvarht)
18152 {
18153 if (v->di_tv.v_type == VAR_STRING)
18154 {
18155 vim_free(v->di_tv.vval.v_string);
18156 if (copy || tv->v_type != VAR_STRING)
18157 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18158 else
18159 {
18160 /* Take over the string to avoid an extra alloc/free. */
18161 v->di_tv.vval.v_string = tv->vval.v_string;
18162 tv->vval.v_string = NULL;
18163 }
18164 }
18165 else if (v->di_tv.v_type != VAR_NUMBER)
18166 EMSG2(_(e_intern2), "set_var()");
18167 else
18168 v->di_tv.vval.v_number = get_tv_number(tv);
18169 return;
18170 }
18171
18172 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 }
18174 else /* add a new variable */
18175 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018176 /* Can't add "v:" variable. */
18177 if (ht == &vimvarht)
18178 {
18179 EMSG2(_(e_illvar), name);
18180 return;
18181 }
18182
Bram Moolenaar92124a32005-06-17 22:03:40 +000018183 /* Make sure the variable name is valid. */
18184 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018185 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18186 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018187 {
18188 EMSG2(_(e_illvar), varname);
18189 return;
18190 }
18191
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018192 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18193 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018194 if (v == NULL)
18195 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018197 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018199 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 return;
18201 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018202 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018207 else
18208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018209 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018210 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213}
18214
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018215/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018216 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018217 * Also give an error message.
18218 */
18219 static int
18220var_check_ro(flags, name)
18221 int flags;
18222 char_u *name;
18223{
18224 if (flags & DI_FLAGS_RO)
18225 {
18226 EMSG2(_(e_readonlyvar), name);
18227 return TRUE;
18228 }
18229 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18230 {
18231 EMSG2(_(e_readonlysbx), name);
18232 return TRUE;
18233 }
18234 return FALSE;
18235}
18236
18237/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018238 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18239 * Also give an error message.
18240 */
18241 static int
18242var_check_fixed(flags, name)
18243 int flags;
18244 char_u *name;
18245{
18246 if (flags & DI_FLAGS_FIX)
18247 {
18248 EMSG2(_("E795: Cannot delete variable %s"), name);
18249 return TRUE;
18250 }
18251 return FALSE;
18252}
18253
18254/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018255 * Return TRUE if typeval "tv" is set to be locked (immutable).
18256 * Also give an error message, using "name".
18257 */
18258 static int
18259tv_check_lock(lock, name)
18260 int lock;
18261 char_u *name;
18262{
18263 if (lock & VAR_LOCKED)
18264 {
18265 EMSG2(_("E741: Value is locked: %s"),
18266 name == NULL ? (char_u *)_("Unknown") : name);
18267 return TRUE;
18268 }
18269 if (lock & VAR_FIXED)
18270 {
18271 EMSG2(_("E742: Cannot change value of %s"),
18272 name == NULL ? (char_u *)_("Unknown") : name);
18273 return TRUE;
18274 }
18275 return FALSE;
18276}
18277
18278/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018279 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018280 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018281 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018284copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018285 typval_T *from;
18286 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018288 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018289 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018290 switch (from->v_type)
18291 {
18292 case VAR_NUMBER:
18293 to->vval.v_number = from->vval.v_number;
18294 break;
18295 case VAR_STRING:
18296 case VAR_FUNC:
18297 if (from->vval.v_string == NULL)
18298 to->vval.v_string = NULL;
18299 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018300 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018301 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018302 if (from->v_type == VAR_FUNC)
18303 func_ref(to->vval.v_string);
18304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018305 break;
18306 case VAR_LIST:
18307 if (from->vval.v_list == NULL)
18308 to->vval.v_list = NULL;
18309 else
18310 {
18311 to->vval.v_list = from->vval.v_list;
18312 ++to->vval.v_list->lv_refcount;
18313 }
18314 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018315 case VAR_DICT:
18316 if (from->vval.v_dict == NULL)
18317 to->vval.v_dict = NULL;
18318 else
18319 {
18320 to->vval.v_dict = from->vval.v_dict;
18321 ++to->vval.v_dict->dv_refcount;
18322 }
18323 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018324 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018325 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018326 break;
18327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328}
18329
18330/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018331 * Make a copy of an item.
18332 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018333 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18334 * reference to an already copied list/dict can be used.
18335 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018336 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018337 static int
18338item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018339 typval_T *from;
18340 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018341 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018342 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018343{
18344 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018345 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018346
Bram Moolenaar33570922005-01-25 22:26:29 +000018347 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018348 {
18349 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018350 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018351 }
18352 ++recurse;
18353
18354 switch (from->v_type)
18355 {
18356 case VAR_NUMBER:
18357 case VAR_STRING:
18358 case VAR_FUNC:
18359 copy_tv(from, to);
18360 break;
18361 case VAR_LIST:
18362 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018363 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018364 if (from->vval.v_list == NULL)
18365 to->vval.v_list = NULL;
18366 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18367 {
18368 /* use the copy made earlier */
18369 to->vval.v_list = from->vval.v_list->lv_copylist;
18370 ++to->vval.v_list->lv_refcount;
18371 }
18372 else
18373 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18374 if (to->vval.v_list == NULL)
18375 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018376 break;
18377 case VAR_DICT:
18378 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018379 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018380 if (from->vval.v_dict == NULL)
18381 to->vval.v_dict = NULL;
18382 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18383 {
18384 /* use the copy made earlier */
18385 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18386 ++to->vval.v_dict->dv_refcount;
18387 }
18388 else
18389 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18390 if (to->vval.v_dict == NULL)
18391 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018392 break;
18393 default:
18394 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018395 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018396 }
18397 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018398 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018399}
18400
18401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 * ":echo expr1 ..." print each argument separated with a space, add a
18403 * newline at the end.
18404 * ":echon expr1 ..." print each argument plain.
18405 */
18406 void
18407ex_echo(eap)
18408 exarg_T *eap;
18409{
18410 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018411 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018412 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 char_u *p;
18414 int needclr = TRUE;
18415 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018416 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417
18418 if (eap->skip)
18419 ++emsg_skip;
18420 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18421 {
18422 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 {
18425 /*
18426 * Report the invalid expression unless the expression evaluation
18427 * has been cancelled due to an aborting error, an interrupt, or an
18428 * exception.
18429 */
18430 if (!aborting())
18431 EMSG2(_(e_invexpr2), p);
18432 break;
18433 }
18434 if (!eap->skip)
18435 {
18436 if (atstart)
18437 {
18438 atstart = FALSE;
18439 /* Call msg_start() after eval1(), evaluating the expression
18440 * may cause a message to appear. */
18441 if (eap->cmdidx == CMD_echo)
18442 msg_start();
18443 }
18444 else if (eap->cmdidx == CMD_echo)
18445 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018446 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018447 if (p != NULL)
18448 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018450 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018452 if (*p != TAB && needclr)
18453 {
18454 /* remove any text still there from the command */
18455 msg_clr_eos();
18456 needclr = FALSE;
18457 }
18458 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 }
18460 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018461 {
18462#ifdef FEAT_MBYTE
18463 if (has_mbyte)
18464 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018465 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018466
18467 (void)msg_outtrans_len_attr(p, i, echo_attr);
18468 p += i - 1;
18469 }
18470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018472 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018475 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 arg = skipwhite(arg);
18479 }
18480 eap->nextcmd = check_nextcmd(arg);
18481
18482 if (eap->skip)
18483 --emsg_skip;
18484 else
18485 {
18486 /* remove text that may still be there from the command */
18487 if (needclr)
18488 msg_clr_eos();
18489 if (eap->cmdidx == CMD_echo)
18490 msg_end();
18491 }
18492}
18493
18494/*
18495 * ":echohl {name}".
18496 */
18497 void
18498ex_echohl(eap)
18499 exarg_T *eap;
18500{
18501 int id;
18502
18503 id = syn_name2id(eap->arg);
18504 if (id == 0)
18505 echo_attr = 0;
18506 else
18507 echo_attr = syn_id2attr(id);
18508}
18509
18510/*
18511 * ":execute expr1 ..." execute the result of an expression.
18512 * ":echomsg expr1 ..." Print a message
18513 * ":echoerr expr1 ..." Print an error
18514 * Each gets spaces around each argument and a newline at the end for
18515 * echo commands
18516 */
18517 void
18518ex_execute(eap)
18519 exarg_T *eap;
18520{
18521 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018522 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 int ret = OK;
18524 char_u *p;
18525 garray_T ga;
18526 int len;
18527 int save_did_emsg;
18528
18529 ga_init2(&ga, 1, 80);
18530
18531 if (eap->skip)
18532 ++emsg_skip;
18533 while (*arg != NUL && *arg != '|' && *arg != '\n')
18534 {
18535 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018536 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 {
18538 /*
18539 * Report the invalid expression unless the expression evaluation
18540 * has been cancelled due to an aborting error, an interrupt, or an
18541 * exception.
18542 */
18543 if (!aborting())
18544 EMSG2(_(e_invexpr2), p);
18545 ret = FAIL;
18546 break;
18547 }
18548
18549 if (!eap->skip)
18550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 len = (int)STRLEN(p);
18553 if (ga_grow(&ga, len + 2) == FAIL)
18554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018555 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 ret = FAIL;
18557 break;
18558 }
18559 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 ga.ga_len += len;
18563 }
18564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 arg = skipwhite(arg);
18567 }
18568
18569 if (ret != FAIL && ga.ga_data != NULL)
18570 {
18571 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018574 out_flush();
18575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 else if (eap->cmdidx == CMD_echoerr)
18577 {
18578 /* We don't want to abort following commands, restore did_emsg. */
18579 save_did_emsg = did_emsg;
18580 EMSG((char_u *)ga.ga_data);
18581 if (!force_abort)
18582 did_emsg = save_did_emsg;
18583 }
18584 else if (eap->cmdidx == CMD_execute)
18585 do_cmdline((char_u *)ga.ga_data,
18586 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18587 }
18588
18589 ga_clear(&ga);
18590
18591 if (eap->skip)
18592 --emsg_skip;
18593
18594 eap->nextcmd = check_nextcmd(arg);
18595}
18596
18597/*
18598 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18599 * "arg" points to the "&" or '+' when called, to "option" when returning.
18600 * Returns NULL when no option name found. Otherwise pointer to the char
18601 * after the option name.
18602 */
18603 static char_u *
18604find_option_end(arg, opt_flags)
18605 char_u **arg;
18606 int *opt_flags;
18607{
18608 char_u *p = *arg;
18609
18610 ++p;
18611 if (*p == 'g' && p[1] == ':')
18612 {
18613 *opt_flags = OPT_GLOBAL;
18614 p += 2;
18615 }
18616 else if (*p == 'l' && p[1] == ':')
18617 {
18618 *opt_flags = OPT_LOCAL;
18619 p += 2;
18620 }
18621 else
18622 *opt_flags = 0;
18623
18624 if (!ASCII_ISALPHA(*p))
18625 return NULL;
18626 *arg = p;
18627
18628 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18629 p += 4; /* termcap option */
18630 else
18631 while (ASCII_ISALPHA(*p))
18632 ++p;
18633 return p;
18634}
18635
18636/*
18637 * ":function"
18638 */
18639 void
18640ex_function(eap)
18641 exarg_T *eap;
18642{
18643 char_u *theline;
18644 int j;
18645 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 char_u *name = NULL;
18648 char_u *p;
18649 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018650 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 garray_T newargs;
18652 garray_T newlines;
18653 int varargs = FALSE;
18654 int mustend = FALSE;
18655 int flags = 0;
18656 ufunc_T *fp;
18657 int indent;
18658 int nesting;
18659 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018660 dictitem_T *v;
18661 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018662 static int func_nr = 0; /* number for nameless function */
18663 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018664 hashtab_T *ht;
18665 int todo;
18666 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018667 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668
18669 /*
18670 * ":function" without argument: list functions.
18671 */
18672 if (ends_excmd(*eap->arg))
18673 {
18674 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018675 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018676 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018677 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018678 {
18679 if (!HASHITEM_EMPTY(hi))
18680 {
18681 --todo;
18682 fp = HI2UF(hi);
18683 if (!isdigit(*fp->uf_name))
18684 list_func_head(fp, FALSE);
18685 }
18686 }
18687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 eap->nextcmd = check_nextcmd(eap->arg);
18689 return;
18690 }
18691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018692 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018693 * ":function /pat": list functions matching pattern.
18694 */
18695 if (*eap->arg == '/')
18696 {
18697 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18698 if (!eap->skip)
18699 {
18700 regmatch_T regmatch;
18701
18702 c = *p;
18703 *p = NUL;
18704 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18705 *p = c;
18706 if (regmatch.regprog != NULL)
18707 {
18708 regmatch.rm_ic = p_ic;
18709
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018710 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018711 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18712 {
18713 if (!HASHITEM_EMPTY(hi))
18714 {
18715 --todo;
18716 fp = HI2UF(hi);
18717 if (!isdigit(*fp->uf_name)
18718 && vim_regexec(&regmatch, fp->uf_name, 0))
18719 list_func_head(fp, FALSE);
18720 }
18721 }
18722 }
18723 }
18724 if (*p == '/')
18725 ++p;
18726 eap->nextcmd = check_nextcmd(p);
18727 return;
18728 }
18729
18730 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018731 * Get the function name. There are these situations:
18732 * func normal function name
18733 * "name" == func, "fudi.fd_dict" == NULL
18734 * dict.func new dictionary entry
18735 * "name" == NULL, "fudi.fd_dict" set,
18736 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18737 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018738 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018739 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18740 * dict.func existing dict entry that's not a Funcref
18741 * "name" == NULL, "fudi.fd_dict" set,
18742 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018745 name = trans_function_name(&p, eap->skip, 0, &fudi);
18746 paren = (vim_strchr(p, '(') != NULL);
18747 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 {
18749 /*
18750 * Return on an invalid expression in braces, unless the expression
18751 * evaluation has been cancelled due to an aborting error, an
18752 * interrupt, or an exception.
18753 */
18754 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018755 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018756 if (!eap->skip && fudi.fd_newkey != NULL)
18757 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018758 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 else
18762 eap->skip = TRUE;
18763 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018764
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 /* An error in a function call during evaluation of an expression in magic
18766 * braces should not cause the function not to be defined. */
18767 saved_did_emsg = did_emsg;
18768 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769
18770 /*
18771 * ":function func" with only function name: list function.
18772 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018773 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 {
18775 if (!ends_excmd(*skipwhite(p)))
18776 {
18777 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018778 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 }
18780 eap->nextcmd = check_nextcmd(p);
18781 if (eap->nextcmd != NULL)
18782 *p = NUL;
18783 if (!eap->skip && !got_int)
18784 {
18785 fp = find_func(name);
18786 if (fp != NULL)
18787 {
18788 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018789 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018791 if (FUNCLINE(fp, j) == NULL)
18792 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 msg_putchar('\n');
18794 msg_outnum((long)(j + 1));
18795 if (j < 9)
18796 msg_putchar(' ');
18797 if (j < 99)
18798 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018799 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 out_flush(); /* show a line at a time */
18801 ui_breakcheck();
18802 }
18803 if (!got_int)
18804 {
18805 msg_putchar('\n');
18806 msg_puts((char_u *)" endfunction");
18807 }
18808 }
18809 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018810 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018812 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 }
18814
18815 /*
18816 * ":function name(arg1, arg2)" Define function.
18817 */
18818 p = skipwhite(p);
18819 if (*p != '(')
18820 {
18821 if (!eap->skip)
18822 {
18823 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018824 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 }
18826 /* attempt to continue by skipping some text */
18827 if (vim_strchr(p, '(') != NULL)
18828 p = vim_strchr(p, '(');
18829 }
18830 p = skipwhite(p + 1);
18831
18832 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18833 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18834
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018835 if (!eap->skip)
18836 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018837 /* Check the name of the function. Unless it's a dictionary function
18838 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018839 if (name != NULL)
18840 arg = name;
18841 else
18842 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018843 if (arg != NULL && (fudi.fd_di == NULL
18844 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845 {
18846 if (*arg == K_SPECIAL)
18847 j = 3;
18848 else
18849 j = 0;
18850 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18851 : eval_isnamec(arg[j])))
18852 ++j;
18853 if (arg[j] != NUL)
18854 emsg_funcname(_(e_invarg2), arg);
18855 }
18856 }
18857
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 /*
18859 * Isolate the arguments: "arg1, arg2, ...)"
18860 */
18861 while (*p != ')')
18862 {
18863 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18864 {
18865 varargs = TRUE;
18866 p += 3;
18867 mustend = TRUE;
18868 }
18869 else
18870 {
18871 arg = p;
18872 while (ASCII_ISALNUM(*p) || *p == '_')
18873 ++p;
18874 if (arg == p || isdigit(*arg)
18875 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18876 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18877 {
18878 if (!eap->skip)
18879 EMSG2(_("E125: Illegal argument: %s"), arg);
18880 break;
18881 }
18882 if (ga_grow(&newargs, 1) == FAIL)
18883 goto erret;
18884 c = *p;
18885 *p = NUL;
18886 arg = vim_strsave(arg);
18887 if (arg == NULL)
18888 goto erret;
18889 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18890 *p = c;
18891 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 if (*p == ',')
18893 ++p;
18894 else
18895 mustend = TRUE;
18896 }
18897 p = skipwhite(p);
18898 if (mustend && *p != ')')
18899 {
18900 if (!eap->skip)
18901 EMSG2(_(e_invarg2), eap->arg);
18902 break;
18903 }
18904 }
18905 ++p; /* skip the ')' */
18906
Bram Moolenaare9a41262005-01-15 22:18:47 +000018907 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 for (;;)
18909 {
18910 p = skipwhite(p);
18911 if (STRNCMP(p, "range", 5) == 0)
18912 {
18913 flags |= FC_RANGE;
18914 p += 5;
18915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018916 else if (STRNCMP(p, "dict", 4) == 0)
18917 {
18918 flags |= FC_DICT;
18919 p += 4;
18920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 else if (STRNCMP(p, "abort", 5) == 0)
18922 {
18923 flags |= FC_ABORT;
18924 p += 5;
18925 }
18926 else
18927 break;
18928 }
18929
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018930 /* When there is a line break use what follows for the function body.
18931 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18932 if (*p == '\n')
18933 line_arg = p + 1;
18934 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 EMSG(_(e_trailing));
18936
18937 /*
18938 * Read the body of the function, until ":endfunction" is found.
18939 */
18940 if (KeyTyped)
18941 {
18942 /* Check if the function already exists, don't let the user type the
18943 * whole function before telling him it doesn't work! For a script we
18944 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018945 if (!eap->skip && !eap->forceit)
18946 {
18947 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18948 EMSG(_(e_funcdict));
18949 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018950 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018953 if (!eap->skip && did_emsg)
18954 goto erret;
18955
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 msg_putchar('\n'); /* don't overwrite the function name */
18957 cmdline_row = msg_row;
18958 }
18959
18960 indent = 2;
18961 nesting = 0;
18962 for (;;)
18963 {
18964 msg_scroll = TRUE;
18965 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018966 sourcing_lnum_off = sourcing_lnum;
18967
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018968 if (line_arg != NULL)
18969 {
18970 /* Use eap->arg, split up in parts by line breaks. */
18971 theline = line_arg;
18972 p = vim_strchr(theline, '\n');
18973 if (p == NULL)
18974 line_arg += STRLEN(line_arg);
18975 else
18976 {
18977 *p = NUL;
18978 line_arg = p + 1;
18979 }
18980 }
18981 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 theline = getcmdline(':', 0L, indent);
18983 else
18984 theline = eap->getline(':', eap->cookie, indent);
18985 if (KeyTyped)
18986 lines_left = Rows - 1;
18987 if (theline == NULL)
18988 {
18989 EMSG(_("E126: Missing :endfunction"));
18990 goto erret;
18991 }
18992
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018993 /* Detect line continuation: sourcing_lnum increased more than one. */
18994 if (sourcing_lnum > sourcing_lnum_off + 1)
18995 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18996 else
18997 sourcing_lnum_off = 0;
18998
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 if (skip_until != NULL)
19000 {
19001 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19002 * don't check for ":endfunc". */
19003 if (STRCMP(theline, skip_until) == 0)
19004 {
19005 vim_free(skip_until);
19006 skip_until = NULL;
19007 }
19008 }
19009 else
19010 {
19011 /* skip ':' and blanks*/
19012 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19013 ;
19014
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019015 /* Check for "endfunction". */
19016 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019018 if (line_arg == NULL)
19019 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 break;
19021 }
19022
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019023 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 * at "end". */
19025 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19026 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019027 else if (STRNCMP(p, "if", 2) == 0
19028 || STRNCMP(p, "wh", 2) == 0
19029 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 || STRNCMP(p, "try", 3) == 0)
19031 indent += 2;
19032
19033 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019034 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019036 if (*p == '!')
19037 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 p += eval_fname_script(p);
19039 if (ASCII_ISALPHA(*p))
19040 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019041 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 if (*skipwhite(p) == '(')
19043 {
19044 ++nesting;
19045 indent += 2;
19046 }
19047 }
19048 }
19049
19050 /* Check for ":append" or ":insert". */
19051 p = skip_range(p, NULL);
19052 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19053 || (p[0] == 'i'
19054 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19055 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19056 skip_until = vim_strsave((char_u *)".");
19057
19058 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19059 arg = skipwhite(skiptowhite(p));
19060 if (arg[0] == '<' && arg[1] =='<'
19061 && ((p[0] == 'p' && p[1] == 'y'
19062 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19063 || (p[0] == 'p' && p[1] == 'e'
19064 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19065 || (p[0] == 't' && p[1] == 'c'
19066 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19067 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19068 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019069 || (p[0] == 'm' && p[1] == 'z'
19070 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 ))
19072 {
19073 /* ":python <<" continues until a dot, like ":append" */
19074 p = skipwhite(arg + 2);
19075 if (*p == NUL)
19076 skip_until = vim_strsave((char_u *)".");
19077 else
19078 skip_until = vim_strsave(p);
19079 }
19080 }
19081
19082 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019083 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019084 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019085 if (line_arg == NULL)
19086 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019088 }
19089
19090 /* Copy the line to newly allocated memory. get_one_sourceline()
19091 * allocates 250 bytes per line, this saves 80% on average. The cost
19092 * is an extra alloc/free. */
19093 p = vim_strsave(theline);
19094 if (p != NULL)
19095 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019096 if (line_arg == NULL)
19097 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019098 theline = p;
19099 }
19100
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019101 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19102
19103 /* Add NULL lines for continuation lines, so that the line count is
19104 * equal to the index in the growarray. */
19105 while (sourcing_lnum_off-- > 0)
19106 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019107
19108 /* Check for end of eap->arg. */
19109 if (line_arg != NULL && *line_arg == NUL)
19110 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 }
19112
19113 /* Don't define the function when skipping commands or when an error was
19114 * detected. */
19115 if (eap->skip || did_emsg)
19116 goto erret;
19117
19118 /*
19119 * If there are no errors, add the function
19120 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019121 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019122 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019123 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019124 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019125 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019126 emsg_funcname("E707: Function name conflicts with variable: %s",
19127 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019128 goto erret;
19129 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019131 fp = find_func(name);
19132 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019134 if (!eap->forceit)
19135 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019136 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019137 goto erret;
19138 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019139 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019140 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019141 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019142 name);
19143 goto erret;
19144 }
19145 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019146 ga_clear_strings(&(fp->uf_args));
19147 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019148 vim_free(name);
19149 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 }
19152 else
19153 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019154 char numbuf[20];
19155
19156 fp = NULL;
19157 if (fudi.fd_newkey == NULL && !eap->forceit)
19158 {
19159 EMSG(_(e_funcdict));
19160 goto erret;
19161 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019162 if (fudi.fd_di == NULL)
19163 {
19164 /* Can't add a function to a locked dictionary */
19165 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19166 goto erret;
19167 }
19168 /* Can't change an existing function if it is locked */
19169 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19170 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019171
19172 /* Give the function a sequential number. Can only be used with a
19173 * Funcref! */
19174 vim_free(name);
19175 sprintf(numbuf, "%d", ++func_nr);
19176 name = vim_strsave((char_u *)numbuf);
19177 if (name == NULL)
19178 goto erret;
19179 }
19180
19181 if (fp == NULL)
19182 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019183 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019184 {
19185 int slen, plen;
19186 char_u *scriptname;
19187
19188 /* Check that the autoload name matches the script name. */
19189 j = FAIL;
19190 if (sourcing_name != NULL)
19191 {
19192 scriptname = autoload_name(name);
19193 if (scriptname != NULL)
19194 {
19195 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019196 plen = (int)STRLEN(p);
19197 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019198 if (slen > plen && fnamecmp(p,
19199 sourcing_name + slen - plen) == 0)
19200 j = OK;
19201 vim_free(scriptname);
19202 }
19203 }
19204 if (j == FAIL)
19205 {
19206 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19207 goto erret;
19208 }
19209 }
19210
19211 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 if (fp == NULL)
19213 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019214
19215 if (fudi.fd_dict != NULL)
19216 {
19217 if (fudi.fd_di == NULL)
19218 {
19219 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019220 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019221 if (fudi.fd_di == NULL)
19222 {
19223 vim_free(fp);
19224 goto erret;
19225 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019226 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19227 {
19228 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019229 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019230 goto erret;
19231 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019232 }
19233 else
19234 /* overwrite existing dict entry */
19235 clear_tv(&fudi.fd_di->di_tv);
19236 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019237 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019238 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019239 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019240
19241 /* behave like "dict" was used */
19242 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019243 }
19244
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019246 STRCPY(fp->uf_name, name);
19247 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019249 fp->uf_args = newargs;
19250 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019251#ifdef FEAT_PROFILE
19252 fp->uf_tml_count = NULL;
19253 fp->uf_tml_total = NULL;
19254 fp->uf_tml_self = NULL;
19255 fp->uf_profiling = FALSE;
19256 if (prof_def_func())
19257 func_do_profile(fp);
19258#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019259 fp->uf_varargs = varargs;
19260 fp->uf_flags = flags;
19261 fp->uf_calls = 0;
19262 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019263 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264
19265erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 ga_clear_strings(&newargs);
19267 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019268ret_free:
19269 vim_free(skip_until);
19270 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273}
19274
19275/*
19276 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019277 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019279 * flags:
19280 * TFN_INT: internal function name OK
19281 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 * Advances "pp" to just after the function name (if no error).
19283 */
19284 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019285trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 char_u **pp;
19287 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019288 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019291 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 char_u *start;
19293 char_u *end;
19294 int lead;
19295 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019298
19299 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019300 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019302
19303 /* Check for hard coded <SNR>: already translated function ID (from a user
19304 * command). */
19305 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19306 && (*pp)[2] == (int)KE_SNR)
19307 {
19308 *pp += 3;
19309 len = get_id_len(pp) + 3;
19310 return vim_strnsave(start, len);
19311 }
19312
19313 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19314 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019316 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019318
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019319 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19320 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019321 if (end == start)
19322 {
19323 if (!skip)
19324 EMSG(_("E129: Function name required"));
19325 goto theend;
19326 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019327 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019328 {
19329 /*
19330 * Report an invalid expression in braces, unless the expression
19331 * evaluation has been cancelled due to an aborting error, an
19332 * interrupt, or an exception.
19333 */
19334 if (!aborting())
19335 {
19336 if (end != NULL)
19337 EMSG2(_(e_invarg2), start);
19338 }
19339 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019340 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019341 goto theend;
19342 }
19343
19344 if (lv.ll_tv != NULL)
19345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019346 if (fdp != NULL)
19347 {
19348 fdp->fd_dict = lv.ll_dict;
19349 fdp->fd_newkey = lv.ll_newkey;
19350 lv.ll_newkey = NULL;
19351 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019352 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019353 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19354 {
19355 name = vim_strsave(lv.ll_tv->vval.v_string);
19356 *pp = end;
19357 }
19358 else
19359 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019360 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19361 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019362 EMSG(_(e_funcref));
19363 else
19364 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019365 name = NULL;
19366 }
19367 goto theend;
19368 }
19369
19370 if (lv.ll_name == NULL)
19371 {
19372 /* Error found, but continue after the function name. */
19373 *pp = end;
19374 goto theend;
19375 }
19376
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019377 /* Check if the name is a Funcref. If so, use the value. */
19378 if (lv.ll_exp_name != NULL)
19379 {
19380 len = (int)STRLEN(lv.ll_exp_name);
19381 name = deref_func_name(lv.ll_exp_name, &len);
19382 if (name == lv.ll_exp_name)
19383 name = NULL;
19384 }
19385 else
19386 {
19387 len = (int)(end - *pp);
19388 name = deref_func_name(*pp, &len);
19389 if (name == *pp)
19390 name = NULL;
19391 }
19392 if (name != NULL)
19393 {
19394 name = vim_strsave(name);
19395 *pp = end;
19396 goto theend;
19397 }
19398
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019399 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019400 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019401 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019402 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19403 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19404 {
19405 /* When there was "s:" already or the name expanded to get a
19406 * leading "s:" then remove it. */
19407 lv.ll_name += 2;
19408 len -= 2;
19409 lead = 2;
19410 }
19411 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019412 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019413 {
19414 if (lead == 2) /* skip over "s:" */
19415 lv.ll_name += 2;
19416 len = (int)(end - lv.ll_name);
19417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019418
19419 /*
19420 * Copy the function name to allocated memory.
19421 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19422 * Accept <SNR>123_name() outside a script.
19423 */
19424 if (skip)
19425 lead = 0; /* do nothing */
19426 else if (lead > 0)
19427 {
19428 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019429 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19430 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019431 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019432 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019433 if (current_SID <= 0)
19434 {
19435 EMSG(_(e_usingsid));
19436 goto theend;
19437 }
19438 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19439 lead += (int)STRLEN(sid_buf);
19440 }
19441 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019442 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019443 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019444 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019445 goto theend;
19446 }
19447 name = alloc((unsigned)(len + lead + 1));
19448 if (name != NULL)
19449 {
19450 if (lead > 0)
19451 {
19452 name[0] = K_SPECIAL;
19453 name[1] = KS_EXTRA;
19454 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019455 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019456 STRCPY(name + 3, sid_buf);
19457 }
19458 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19459 name[len + lead] = NUL;
19460 }
19461 *pp = end;
19462
19463theend:
19464 clear_lval(&lv);
19465 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466}
19467
19468/*
19469 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19470 * Return 2 if "p" starts with "s:".
19471 * Return 0 otherwise.
19472 */
19473 static int
19474eval_fname_script(p)
19475 char_u *p;
19476{
19477 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19478 || STRNICMP(p + 1, "SNR>", 4) == 0))
19479 return 5;
19480 if (p[0] == 's' && p[1] == ':')
19481 return 2;
19482 return 0;
19483}
19484
19485/*
19486 * Return TRUE if "p" starts with "<SID>" or "s:".
19487 * Only works if eval_fname_script() returned non-zero for "p"!
19488 */
19489 static int
19490eval_fname_sid(p)
19491 char_u *p;
19492{
19493 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19494}
19495
19496/*
19497 * List the head of the function: "name(arg1, arg2)".
19498 */
19499 static void
19500list_func_head(fp, indent)
19501 ufunc_T *fp;
19502 int indent;
19503{
19504 int j;
19505
19506 msg_start();
19507 if (indent)
19508 MSG_PUTS(" ");
19509 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019510 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 {
19512 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019513 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 }
19515 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019516 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019518 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
19520 if (j)
19521 MSG_PUTS(", ");
19522 msg_puts(FUNCARG(fp, j));
19523 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019524 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 {
19526 if (j)
19527 MSG_PUTS(", ");
19528 MSG_PUTS("...");
19529 }
19530 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019531 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019532 if (p_verbose > 0)
19533 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534}
19535
19536/*
19537 * Find a function by name, return pointer to it in ufuncs.
19538 * Return NULL for unknown function.
19539 */
19540 static ufunc_T *
19541find_func(name)
19542 char_u *name;
19543{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019544 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019546 hi = hash_find(&func_hashtab, name);
19547 if (!HASHITEM_EMPTY(hi))
19548 return HI2UF(hi);
19549 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019552#if defined(EXITFREE) || defined(PROTO)
19553 void
19554free_all_functions()
19555{
19556 hashitem_T *hi;
19557
19558 /* Need to start all over every time, because func_free() may change the
19559 * hash table. */
19560 while (func_hashtab.ht_used > 0)
19561 for (hi = func_hashtab.ht_array; ; ++hi)
19562 if (!HASHITEM_EMPTY(hi))
19563 {
19564 func_free(HI2UF(hi));
19565 break;
19566 }
19567}
19568#endif
19569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019570/*
19571 * Return TRUE if a function "name" exists.
19572 */
19573 static int
19574function_exists(name)
19575 char_u *name;
19576{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019577 char_u *nm = name;
19578 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019579 int n = FALSE;
19580
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019581 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019582 nm = skipwhite(nm);
19583
19584 /* Only accept "funcname", "funcname ", "funcname (..." and
19585 * "funcname(...", not "funcname!...". */
19586 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019587 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019588 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019589 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019590 else
19591 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019592 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019593 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019594 return n;
19595}
19596
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019597/*
19598 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019599 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019600 */
19601 static int
19602builtin_function(name)
19603 char_u *name;
19604{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019605 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19606 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019607}
19608
Bram Moolenaar05159a02005-02-26 23:04:13 +000019609#if defined(FEAT_PROFILE) || defined(PROTO)
19610/*
19611 * Start profiling function "fp".
19612 */
19613 static void
19614func_do_profile(fp)
19615 ufunc_T *fp;
19616{
19617 fp->uf_tm_count = 0;
19618 profile_zero(&fp->uf_tm_self);
19619 profile_zero(&fp->uf_tm_total);
19620 if (fp->uf_tml_count == NULL)
19621 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19622 (sizeof(int) * fp->uf_lines.ga_len));
19623 if (fp->uf_tml_total == NULL)
19624 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19625 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19626 if (fp->uf_tml_self == NULL)
19627 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19628 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19629 fp->uf_tml_idx = -1;
19630 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19631 || fp->uf_tml_self == NULL)
19632 return; /* out of memory */
19633
19634 fp->uf_profiling = TRUE;
19635}
19636
19637/*
19638 * Dump the profiling results for all functions in file "fd".
19639 */
19640 void
19641func_dump_profile(fd)
19642 FILE *fd;
19643{
19644 hashitem_T *hi;
19645 int todo;
19646 ufunc_T *fp;
19647 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019648 ufunc_T **sorttab;
19649 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019651 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019652 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19653
Bram Moolenaar05159a02005-02-26 23:04:13 +000019654 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19655 {
19656 if (!HASHITEM_EMPTY(hi))
19657 {
19658 --todo;
19659 fp = HI2UF(hi);
19660 if (fp->uf_profiling)
19661 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019662 if (sorttab != NULL)
19663 sorttab[st_len++] = fp;
19664
Bram Moolenaar05159a02005-02-26 23:04:13 +000019665 if (fp->uf_name[0] == K_SPECIAL)
19666 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19667 else
19668 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19669 if (fp->uf_tm_count == 1)
19670 fprintf(fd, "Called 1 time\n");
19671 else
19672 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19673 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19674 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19675 fprintf(fd, "\n");
19676 fprintf(fd, "count total (s) self (s)\n");
19677
19678 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19679 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019680 if (FUNCLINE(fp, i) == NULL)
19681 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019682 prof_func_line(fd, fp->uf_tml_count[i],
19683 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019684 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19685 }
19686 fprintf(fd, "\n");
19687 }
19688 }
19689 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019690
19691 if (sorttab != NULL && st_len > 0)
19692 {
19693 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19694 prof_total_cmp);
19695 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19696 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19697 prof_self_cmp);
19698 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19699 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019700}
Bram Moolenaar73830342005-02-28 22:48:19 +000019701
19702 static void
19703prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19704 FILE *fd;
19705 ufunc_T **sorttab;
19706 int st_len;
19707 char *title;
19708 int prefer_self; /* when equal print only self time */
19709{
19710 int i;
19711 ufunc_T *fp;
19712
19713 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19714 fprintf(fd, "count total (s) self (s) function\n");
19715 for (i = 0; i < 20 && i < st_len; ++i)
19716 {
19717 fp = sorttab[i];
19718 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19719 prefer_self);
19720 if (fp->uf_name[0] == K_SPECIAL)
19721 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19722 else
19723 fprintf(fd, " %s()\n", fp->uf_name);
19724 }
19725 fprintf(fd, "\n");
19726}
19727
19728/*
19729 * Print the count and times for one function or function line.
19730 */
19731 static void
19732prof_func_line(fd, count, total, self, prefer_self)
19733 FILE *fd;
19734 int count;
19735 proftime_T *total;
19736 proftime_T *self;
19737 int prefer_self; /* when equal print only self time */
19738{
19739 if (count > 0)
19740 {
19741 fprintf(fd, "%5d ", count);
19742 if (prefer_self && profile_equal(total, self))
19743 fprintf(fd, " ");
19744 else
19745 fprintf(fd, "%s ", profile_msg(total));
19746 if (!prefer_self && profile_equal(total, self))
19747 fprintf(fd, " ");
19748 else
19749 fprintf(fd, "%s ", profile_msg(self));
19750 }
19751 else
19752 fprintf(fd, " ");
19753}
19754
19755/*
19756 * Compare function for total time sorting.
19757 */
19758 static int
19759#ifdef __BORLANDC__
19760_RTLENTRYF
19761#endif
19762prof_total_cmp(s1, s2)
19763 const void *s1;
19764 const void *s2;
19765{
19766 ufunc_T *p1, *p2;
19767
19768 p1 = *(ufunc_T **)s1;
19769 p2 = *(ufunc_T **)s2;
19770 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19771}
19772
19773/*
19774 * Compare function for self time sorting.
19775 */
19776 static int
19777#ifdef __BORLANDC__
19778_RTLENTRYF
19779#endif
19780prof_self_cmp(s1, s2)
19781 const void *s1;
19782 const void *s2;
19783{
19784 ufunc_T *p1, *p2;
19785
19786 p1 = *(ufunc_T **)s1;
19787 p2 = *(ufunc_T **)s2;
19788 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19789}
19790
Bram Moolenaar05159a02005-02-26 23:04:13 +000019791#endif
19792
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019793/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019794 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019795 * Return TRUE if a package was loaded.
19796 */
19797 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019798script_autoload(name, reload)
19799 char_u *name;
19800 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019801{
19802 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019803 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019804 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019805 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019806
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019807 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019808 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019809 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019810 return FALSE;
19811
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019812 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019813
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019814 /* Find the name in the list of previously loaded package names. Skip
19815 * "autoload/", it's always the same. */
19816 for (i = 0; i < ga_loaded.ga_len; ++i)
19817 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19818 break;
19819 if (!reload && i < ga_loaded.ga_len)
19820 ret = FALSE; /* was loaded already */
19821 else
19822 {
19823 /* Remember the name if it wasn't loaded already. */
19824 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19825 {
19826 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19827 tofree = NULL;
19828 }
19829
19830 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019831 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019832 ret = TRUE;
19833 }
19834
19835 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019836 return ret;
19837}
19838
19839/*
19840 * Return the autoload script name for a function or variable name.
19841 * Returns NULL when out of memory.
19842 */
19843 static char_u *
19844autoload_name(name)
19845 char_u *name;
19846{
19847 char_u *p;
19848 char_u *scriptname;
19849
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019850 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019851 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19852 if (scriptname == NULL)
19853 return FALSE;
19854 STRCPY(scriptname, "autoload/");
19855 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019856 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019857 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019858 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019859 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019860 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019861}
19862
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19864
19865/*
19866 * Function given to ExpandGeneric() to obtain the list of user defined
19867 * function names.
19868 */
19869 char_u *
19870get_user_func_name(xp, idx)
19871 expand_T *xp;
19872 int idx;
19873{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019874 static long_u done;
19875 static hashitem_T *hi;
19876 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877
19878 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019880 done = 0;
19881 hi = func_hashtab.ht_array;
19882 }
19883 if (done < func_hashtab.ht_used)
19884 {
19885 if (done++ > 0)
19886 ++hi;
19887 while (HASHITEM_EMPTY(hi))
19888 ++hi;
19889 fp = HI2UF(hi);
19890
19891 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19892 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893
19894 cat_func_name(IObuff, fp);
19895 if (xp->xp_context != EXPAND_USER_FUNC)
19896 {
19897 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019898 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 STRCAT(IObuff, ")");
19900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 return IObuff;
19902 }
19903 return NULL;
19904}
19905
19906#endif /* FEAT_CMDL_COMPL */
19907
19908/*
19909 * Copy the function name of "fp" to buffer "buf".
19910 * "buf" must be able to hold the function name plus three bytes.
19911 * Takes care of script-local function names.
19912 */
19913 static void
19914cat_func_name(buf, fp)
19915 char_u *buf;
19916 ufunc_T *fp;
19917{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019918 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 {
19920 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019921 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 }
19923 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019924 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925}
19926
19927/*
19928 * ":delfunction {name}"
19929 */
19930 void
19931ex_delfunction(eap)
19932 exarg_T *eap;
19933{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019934 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 char_u *p;
19936 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019937 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938
19939 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019940 name = trans_function_name(&p, eap->skip, 0, &fudi);
19941 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019943 {
19944 if (fudi.fd_dict != NULL && !eap->skip)
19945 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 if (!ends_excmd(*skipwhite(p)))
19949 {
19950 vim_free(name);
19951 EMSG(_(e_trailing));
19952 return;
19953 }
19954 eap->nextcmd = check_nextcmd(p);
19955 if (eap->nextcmd != NULL)
19956 *p = NUL;
19957
19958 if (!eap->skip)
19959 fp = find_func(name);
19960 vim_free(name);
19961
19962 if (!eap->skip)
19963 {
19964 if (fp == NULL)
19965 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019966 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 return;
19968 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019969 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 {
19971 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19972 return;
19973 }
19974
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019975 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019977 /* Delete the dict item that refers to the function, it will
19978 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019979 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019981 else
19982 func_free(fp);
19983 }
19984}
19985
19986/*
19987 * Free a function and remove it from the list of functions.
19988 */
19989 static void
19990func_free(fp)
19991 ufunc_T *fp;
19992{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019993 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019994
19995 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019996 ga_clear_strings(&(fp->uf_args));
19997 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019998#ifdef FEAT_PROFILE
19999 vim_free(fp->uf_tml_count);
20000 vim_free(fp->uf_tml_total);
20001 vim_free(fp->uf_tml_self);
20002#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020003
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020004 /* remove the function from the function hashtable */
20005 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20006 if (HASHITEM_EMPTY(hi))
20007 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020008 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020009 hash_remove(&func_hashtab, hi);
20010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020011 vim_free(fp);
20012}
20013
20014/*
20015 * Unreference a Function: decrement the reference count and free it when it
20016 * becomes zero. Only for numbered functions.
20017 */
20018 static void
20019func_unref(name)
20020 char_u *name;
20021{
20022 ufunc_T *fp;
20023
20024 if (name != NULL && isdigit(*name))
20025 {
20026 fp = find_func(name);
20027 if (fp == NULL)
20028 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020029 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020030 {
20031 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020032 * when "uf_calls" becomes zero. */
20033 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020034 func_free(fp);
20035 }
20036 }
20037}
20038
20039/*
20040 * Count a reference to a Function.
20041 */
20042 static void
20043func_ref(name)
20044 char_u *name;
20045{
20046 ufunc_T *fp;
20047
20048 if (name != NULL && isdigit(*name))
20049 {
20050 fp = find_func(name);
20051 if (fp == NULL)
20052 EMSG2(_(e_intern2), "func_ref()");
20053 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020054 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 }
20056}
20057
20058/*
20059 * Call a user function.
20060 */
20061 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020062call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 ufunc_T *fp; /* pointer to function */
20064 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 typval_T *argvars; /* arguments */
20066 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 linenr_T firstline; /* first line of range */
20068 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020069 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070{
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 char_u *save_sourcing_name;
20072 linenr_T save_sourcing_lnum;
20073 scid_T save_current_SID;
20074 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 int save_did_emsg;
20076 static int depth = 0;
20077 dictitem_T *v;
20078 int fixvar_idx = 0; /* index in fixvar[] */
20079 int i;
20080 int ai;
20081 char_u numbuf[NUMBUFLEN];
20082 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020083#ifdef FEAT_PROFILE
20084 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020085 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087
20088 /* If depth of calling is getting too high, don't execute the function */
20089 if (depth >= p_mfd)
20090 {
20091 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 rettv->v_type = VAR_NUMBER;
20093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 return;
20095 }
20096 ++depth;
20097
20098 line_breakcheck(); /* check for CTRL-C hit */
20099
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020100 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020101 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020103 fc.rettv = rettv;
20104 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 fc.linenr = 0;
20106 fc.returned = FALSE;
20107 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020109 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 fc.dbg_tick = debug_tick;
20111
Bram Moolenaar33570922005-01-25 22:26:29 +000020112 /*
20113 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20114 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20115 * each argument variable and saves a lot of time.
20116 */
20117 /*
20118 * Init l: variables.
20119 */
20120 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020121 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020122 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020123 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20124 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020126 name = v->di_key;
20127 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20129 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20130 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020131 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020132 v->di_tv.vval.v_dict = selfdict;
20133 ++selfdict->dv_refcount;
20134 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020135
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 /*
20137 * Init a: variables.
20138 * Set a:0 to "argcount".
20139 * Set a:000 to a list with room for the "..." arguments.
20140 */
20141 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20142 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020143 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 v = &fc.fixvar[fixvar_idx++].var;
20145 STRCPY(v->di_key, "000");
20146 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20147 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20148 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020149 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 v->di_tv.vval.v_list = &fc.l_varlist;
20151 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20152 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020153 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020154
20155 /*
20156 * Set a:firstline to "firstline" and a:lastline to "lastline".
20157 * Set a:name to named arguments.
20158 * Set a:N to the "..." arguments.
20159 */
20160 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20161 (varnumber_T)firstline);
20162 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20163 (varnumber_T)lastline);
20164 for (i = 0; i < argcount; ++i)
20165 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020166 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020167 if (ai < 0)
20168 /* named argument a:name */
20169 name = FUNCARG(fp, i);
20170 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 /* "..." argument a:1, a:2, etc. */
20173 sprintf((char *)numbuf, "%d", ai + 1);
20174 name = numbuf;
20175 }
20176 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20177 {
20178 v = &fc.fixvar[fixvar_idx++].var;
20179 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20180 }
20181 else
20182 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020183 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20184 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 if (v == NULL)
20186 break;
20187 v->di_flags = DI_FLAGS_RO;
20188 }
20189 STRCPY(v->di_key, name);
20190 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20191
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020192 /* Note: the values are copied directly to avoid alloc/free.
20193 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020195 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020196
20197 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20198 {
20199 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20200 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020201 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020202 }
20203 }
20204
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 /* Don't redraw while executing the function. */
20206 ++RedrawingDisabled;
20207 save_sourcing_name = sourcing_name;
20208 save_sourcing_lnum = sourcing_lnum;
20209 sourcing_lnum = 1;
20210 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020211 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 if (sourcing_name != NULL)
20213 {
20214 if (save_sourcing_name != NULL
20215 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20216 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20217 else
20218 STRCPY(sourcing_name, "function ");
20219 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20220
20221 if (p_verbose >= 12)
20222 {
20223 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020224 verbose_enter_scroll();
20225
Bram Moolenaar555b2802005-05-19 21:08:39 +000020226 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 if (p_verbose >= 14)
20228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020230 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020231 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020232 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233
20234 msg_puts((char_u *)"(");
20235 for (i = 0; i < argcount; ++i)
20236 {
20237 if (i > 0)
20238 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020239 if (argvars[i].v_type == VAR_NUMBER)
20240 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 else
20242 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020243 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20244 if (s != NULL)
20245 {
20246 trunc_string(s, buf, MSG_BUF_CLEN);
20247 msg_puts(buf);
20248 vim_free(tofree);
20249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 }
20251 }
20252 msg_puts((char_u *)")");
20253 }
20254 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020255
20256 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 --no_wait_return;
20258 }
20259 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020260#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020261 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020262 {
20263 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20264 func_do_profile(fp);
20265 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020266 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020267 {
20268 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020269 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020270 profile_zero(&fp->uf_tm_children);
20271 }
20272 script_prof_save(&wait_start);
20273 }
20274#endif
20275
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020277 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 save_did_emsg = did_emsg;
20279 did_emsg = FALSE;
20280
20281 /* call do_cmdline() to execute the lines */
20282 do_cmdline(NULL, get_func_line, (void *)&fc,
20283 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20284
20285 --RedrawingDisabled;
20286
20287 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020288 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 clear_tv(rettv);
20291 rettv->v_type = VAR_NUMBER;
20292 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 }
20294
Bram Moolenaar05159a02005-02-26 23:04:13 +000020295#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020296 if (do_profiling == PROF_YES && (fp->uf_profiling
20297 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020298 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020299 profile_end(&call_start);
20300 profile_sub_wait(&wait_start, &call_start);
20301 profile_add(&fp->uf_tm_total, &call_start);
20302 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020303 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020304 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020305 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20306 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020307 }
20308 }
20309#endif
20310
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 /* when being verbose, mention the return value */
20312 if (p_verbose >= 12)
20313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020315 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020318 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020319 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020320 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20321 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020324 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020325 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020326 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020327 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020328
Bram Moolenaar555b2802005-05-19 21:08:39 +000020329 /* The value may be very long. Skip the middle part, so that we
20330 * have some idea how it starts and ends. smsg() would always
20331 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020332 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20333 if (s != NULL)
20334 {
20335 trunc_string(s, buf, MSG_BUF_CLEN);
20336 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20337 vim_free(tofree);
20338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 }
20340 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020341
20342 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 --no_wait_return;
20344 }
20345
20346 vim_free(sourcing_name);
20347 sourcing_name = save_sourcing_name;
20348 sourcing_lnum = save_sourcing_lnum;
20349 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020350#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020351 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020352 script_prof_restore(&wait_start);
20353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354
20355 if (p_verbose >= 12 && sourcing_name != NULL)
20356 {
20357 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020358 verbose_enter_scroll();
20359
Bram Moolenaar555b2802005-05-19 21:08:39 +000020360 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020362
20363 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 --no_wait_return;
20365 }
20366
20367 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020368 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
Bram Moolenaar33570922005-01-25 22:26:29 +000020370 /* The a: variables typevals were not alloced, only free the allocated
20371 * variables. */
20372 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20373
20374 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 --depth;
20376}
20377
20378/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020379 * Add a number variable "name" to dict "dp" with value "nr".
20380 */
20381 static void
20382add_nr_var(dp, v, name, nr)
20383 dict_T *dp;
20384 dictitem_T *v;
20385 char *name;
20386 varnumber_T nr;
20387{
20388 STRCPY(v->di_key, name);
20389 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20390 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20391 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020392 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 v->di_tv.vval.v_number = nr;
20394}
20395
20396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 * ":return [expr]"
20398 */
20399 void
20400ex_return(eap)
20401 exarg_T *eap;
20402{
20403 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 int returning = FALSE;
20406
20407 if (current_funccal == NULL)
20408 {
20409 EMSG(_("E133: :return not inside a function"));
20410 return;
20411 }
20412
20413 if (eap->skip)
20414 ++emsg_skip;
20415
20416 eap->nextcmd = NULL;
20417 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020418 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 {
20420 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020421 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 }
20425 /* It's safer to return also on error. */
20426 else if (!eap->skip)
20427 {
20428 /*
20429 * Return unless the expression evaluation has been cancelled due to an
20430 * aborting error, an interrupt, or an exception.
20431 */
20432 if (!aborting())
20433 returning = do_return(eap, FALSE, TRUE, NULL);
20434 }
20435
20436 /* When skipping or the return gets pending, advance to the next command
20437 * in this line (!returning). Otherwise, ignore the rest of the line.
20438 * Following lines will be ignored by get_func_line(). */
20439 if (returning)
20440 eap->nextcmd = NULL;
20441 else if (eap->nextcmd == NULL) /* no argument */
20442 eap->nextcmd = check_nextcmd(arg);
20443
20444 if (eap->skip)
20445 --emsg_skip;
20446}
20447
20448/*
20449 * Return from a function. Possibly makes the return pending. Also called
20450 * for a pending return at the ":endtry" or after returning from an extra
20451 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020452 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020453 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 * FALSE when the return gets pending.
20455 */
20456 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 exarg_T *eap;
20459 int reanimate;
20460 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020461 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462{
20463 int idx;
20464 struct condstack *cstack = eap->cstack;
20465
20466 if (reanimate)
20467 /* Undo the return. */
20468 current_funccal->returned = FALSE;
20469
20470 /*
20471 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20472 * not in its finally clause (which then is to be executed next) is found.
20473 * In this case, make the ":return" pending for execution at the ":endtry".
20474 * Otherwise, return normally.
20475 */
20476 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20477 if (idx >= 0)
20478 {
20479 cstack->cs_pending[idx] = CSTP_RETURN;
20480
20481 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020482 /* A pending return again gets pending. "rettv" points to an
20483 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020485 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486 else
20487 {
20488 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020489 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020493 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 {
20495 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020496 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020497 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 else
20499 EMSG(_(e_outofmem));
20500 }
20501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020502 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503
20504 if (reanimate)
20505 {
20506 /* The pending return value could be overwritten by a ":return"
20507 * without argument in a finally clause; reset the default
20508 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020509 current_funccal->rettv->v_type = VAR_NUMBER;
20510 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 }
20512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020513 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 }
20515 else
20516 {
20517 current_funccal->returned = TRUE;
20518
20519 /* If the return is carried out now, store the return value. For
20520 * a return immediately after reanimation, the value is already
20521 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020522 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020524 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020525 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020527 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 }
20529 }
20530
20531 return idx < 0;
20532}
20533
20534/*
20535 * Free the variable with a pending return value.
20536 */
20537 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020538discard_pending_return(rettv)
20539 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540{
Bram Moolenaar33570922005-01-25 22:26:29 +000020541 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542}
20543
20544/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020545 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 * is an allocated string. Used by report_pending() for verbose messages.
20547 */
20548 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020549get_return_cmd(rettv)
20550 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020552 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020553 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020554 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020556 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020557 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020558 if (s == NULL)
20559 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020560
20561 STRCPY(IObuff, ":return ");
20562 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20563 if (STRLEN(s) + 8 >= IOSIZE)
20564 STRCPY(IObuff + IOSIZE - 4, "...");
20565 vim_free(tofree);
20566 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567}
20568
20569/*
20570 * Get next function line.
20571 * Called by do_cmdline() to get the next line.
20572 * Returns allocated string, or NULL for end of function.
20573 */
20574/* ARGSUSED */
20575 char_u *
20576get_func_line(c, cookie, indent)
20577 int c; /* not used */
20578 void *cookie;
20579 int indent; /* not used */
20580{
Bram Moolenaar33570922005-01-25 22:26:29 +000020581 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020582 ufunc_T *fp = fcp->func;
20583 char_u *retval;
20584 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585
20586 /* If breakpoints have been added/deleted need to check for it. */
20587 if (fcp->dbg_tick != debug_tick)
20588 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020589 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 sourcing_lnum);
20591 fcp->dbg_tick = debug_tick;
20592 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020593#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020594 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020595 func_line_end(cookie);
20596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597
Bram Moolenaar05159a02005-02-26 23:04:13 +000020598 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020599 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20600 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 retval = NULL;
20602 else
20603 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020604 /* Skip NULL lines (continuation lines). */
20605 while (fcp->linenr < gap->ga_len
20606 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20607 ++fcp->linenr;
20608 if (fcp->linenr >= gap->ga_len)
20609 retval = NULL;
20610 else
20611 {
20612 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20613 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020614#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020615 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020616 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020617#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 }
20620
20621 /* Did we encounter a breakpoint? */
20622 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20623 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020624 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020626 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 sourcing_lnum);
20628 fcp->dbg_tick = debug_tick;
20629 }
20630
20631 return retval;
20632}
20633
Bram Moolenaar05159a02005-02-26 23:04:13 +000020634#if defined(FEAT_PROFILE) || defined(PROTO)
20635/*
20636 * Called when starting to read a function line.
20637 * "sourcing_lnum" must be correct!
20638 * When skipping lines it may not actually be executed, but we won't find out
20639 * until later and we need to store the time now.
20640 */
20641 void
20642func_line_start(cookie)
20643 void *cookie;
20644{
20645 funccall_T *fcp = (funccall_T *)cookie;
20646 ufunc_T *fp = fcp->func;
20647
20648 if (fp->uf_profiling && sourcing_lnum >= 1
20649 && sourcing_lnum <= fp->uf_lines.ga_len)
20650 {
20651 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020652 /* Skip continuation lines. */
20653 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20654 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020655 fp->uf_tml_execed = FALSE;
20656 profile_start(&fp->uf_tml_start);
20657 profile_zero(&fp->uf_tml_children);
20658 profile_get_wait(&fp->uf_tml_wait);
20659 }
20660}
20661
20662/*
20663 * Called when actually executing a function line.
20664 */
20665 void
20666func_line_exec(cookie)
20667 void *cookie;
20668{
20669 funccall_T *fcp = (funccall_T *)cookie;
20670 ufunc_T *fp = fcp->func;
20671
20672 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20673 fp->uf_tml_execed = TRUE;
20674}
20675
20676/*
20677 * Called when done with a function line.
20678 */
20679 void
20680func_line_end(cookie)
20681 void *cookie;
20682{
20683 funccall_T *fcp = (funccall_T *)cookie;
20684 ufunc_T *fp = fcp->func;
20685
20686 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20687 {
20688 if (fp->uf_tml_execed)
20689 {
20690 ++fp->uf_tml_count[fp->uf_tml_idx];
20691 profile_end(&fp->uf_tml_start);
20692 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020693 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020694 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20695 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020696 }
20697 fp->uf_tml_idx = -1;
20698 }
20699}
20700#endif
20701
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702/*
20703 * Return TRUE if the currently active function should be ended, because a
20704 * return was encountered or an error occured. Used inside a ":while".
20705 */
20706 int
20707func_has_ended(cookie)
20708 void *cookie;
20709{
Bram Moolenaar33570922005-01-25 22:26:29 +000020710 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711
20712 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20713 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020714 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 || fcp->returned);
20716}
20717
20718/*
20719 * return TRUE if cookie indicates a function which "abort"s on errors.
20720 */
20721 int
20722func_has_abort(cookie)
20723 void *cookie;
20724{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020725 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726}
20727
20728#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20729typedef enum
20730{
20731 VAR_FLAVOUR_DEFAULT,
20732 VAR_FLAVOUR_SESSION,
20733 VAR_FLAVOUR_VIMINFO
20734} var_flavour_T;
20735
20736static var_flavour_T var_flavour __ARGS((char_u *varname));
20737
20738 static var_flavour_T
20739var_flavour(varname)
20740 char_u *varname;
20741{
20742 char_u *p = varname;
20743
20744 if (ASCII_ISUPPER(*p))
20745 {
20746 while (*(++p))
20747 if (ASCII_ISLOWER(*p))
20748 return VAR_FLAVOUR_SESSION;
20749 return VAR_FLAVOUR_VIMINFO;
20750 }
20751 else
20752 return VAR_FLAVOUR_DEFAULT;
20753}
20754#endif
20755
20756#if defined(FEAT_VIMINFO) || defined(PROTO)
20757/*
20758 * Restore global vars that start with a capital from the viminfo file
20759 */
20760 int
20761read_viminfo_varlist(virp, writing)
20762 vir_T *virp;
20763 int writing;
20764{
20765 char_u *tab;
20766 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020767 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768
20769 if (!writing && (find_viminfo_parameter('!') != NULL))
20770 {
20771 tab = vim_strchr(virp->vir_line + 1, '\t');
20772 if (tab != NULL)
20773 {
20774 *tab++ = '\0'; /* isolate the variable name */
20775 if (*tab == 'S') /* string var */
20776 is_string = TRUE;
20777
20778 tab = vim_strchr(tab, '\t');
20779 if (tab != NULL)
20780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 if (is_string)
20782 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020783 tv.v_type = VAR_STRING;
20784 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 }
20787 else
20788 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020789 tv.v_type = VAR_NUMBER;
20790 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020792 set_var(virp->vir_line + 1, &tv, FALSE);
20793 if (is_string)
20794 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 }
20796 }
20797 }
20798
20799 return viminfo_readline(virp);
20800}
20801
20802/*
20803 * Write global vars that start with a capital to the viminfo file
20804 */
20805 void
20806write_viminfo_varlist(fp)
20807 FILE *fp;
20808{
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 hashitem_T *hi;
20810 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020811 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020813 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816
20817 if (find_viminfo_parameter('!') == NULL)
20818 return;
20819
20820 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020821
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020822 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020823 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020827 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020828 this_var = HI2DI(hi);
20829 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020830 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020832 {
20833 case VAR_STRING: s = "STR"; break;
20834 case VAR_NUMBER: s = "NUM"; break;
20835 default: continue;
20836 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020837 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020838 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020839 if (p != NULL)
20840 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020841 vim_free(tofree);
20842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 }
20844 }
20845}
20846#endif
20847
20848#if defined(FEAT_SESSION) || defined(PROTO)
20849 int
20850store_session_globals(fd)
20851 FILE *fd;
20852{
Bram Moolenaar33570922005-01-25 22:26:29 +000020853 hashitem_T *hi;
20854 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020855 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 char_u *p, *t;
20857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020858 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020859 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020861 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020863 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020864 this_var = HI2DI(hi);
20865 if ((this_var->di_tv.v_type == VAR_NUMBER
20866 || this_var->di_tv.v_type == VAR_STRING)
20867 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020868 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020869 /* Escape special characters with a backslash. Turn a LF and
20870 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020871 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020872 (char_u *)"\\\"\n\r");
20873 if (p == NULL) /* out of memory */
20874 break;
20875 for (t = p; *t != NUL; ++t)
20876 if (*t == '\n')
20877 *t = 'n';
20878 else if (*t == '\r')
20879 *t = 'r';
20880 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020881 this_var->di_key,
20882 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20883 : ' ',
20884 p,
20885 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20886 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020887 || put_eol(fd) == FAIL)
20888 {
20889 vim_free(p);
20890 return FAIL;
20891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 vim_free(p);
20893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 }
20895 }
20896 return OK;
20897}
20898#endif
20899
Bram Moolenaar661b1822005-07-28 22:36:45 +000020900/*
20901 * Display script name where an item was last set.
20902 * Should only be invoked when 'verbose' is non-zero.
20903 */
20904 void
20905last_set_msg(scriptID)
20906 scid_T scriptID;
20907{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020908 char_u *p;
20909
Bram Moolenaar661b1822005-07-28 22:36:45 +000020910 if (scriptID != 0)
20911 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020912 p = home_replace_save(NULL, get_scriptname(scriptID));
20913 if (p != NULL)
20914 {
20915 verbose_enter();
20916 MSG_PUTS(_("\n\tLast set from "));
20917 MSG_PUTS(p);
20918 vim_free(p);
20919 verbose_leave();
20920 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020921 }
20922}
20923
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924#endif /* FEAT_EVAL */
20925
20926#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20927
20928
20929#ifdef WIN3264
20930/*
20931 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20932 */
20933static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20934static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20935static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20936
20937/*
20938 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020939 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 */
20941 static int
20942get_short_pathname(fnamep, bufp, fnamelen)
20943 char_u **fnamep;
20944 char_u **bufp;
20945 int *fnamelen;
20946{
20947 int l,len;
20948 char_u *newbuf;
20949
20950 len = *fnamelen;
20951
20952 l = GetShortPathName(*fnamep, *fnamep, len);
20953 if (l > len - 1)
20954 {
20955 /* If that doesn't work (not enough space), then save the string
20956 * and try again with a new buffer big enough
20957 */
20958 newbuf = vim_strnsave(*fnamep, l);
20959 if (newbuf == NULL)
20960 return 0;
20961
20962 vim_free(*bufp);
20963 *fnamep = *bufp = newbuf;
20964
20965 l = GetShortPathName(*fnamep,*fnamep,l+1);
20966
20967 /* Really should always succeed, as the buffer is big enough */
20968 }
20969
20970 *fnamelen = l;
20971 return 1;
20972}
20973
20974/*
20975 * Create a short path name. Returns the length of the buffer it needs.
20976 * Doesn't copy over the end of the buffer passed in.
20977 */
20978 static int
20979shortpath_for_invalid_fname(fname, bufp, fnamelen)
20980 char_u **fname;
20981 char_u **bufp;
20982 int *fnamelen;
20983{
20984 char_u *s, *p, *pbuf2, *pbuf3;
20985 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020986 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987
20988 /* Make a copy */
20989 len2 = *fnamelen;
20990 pbuf2 = vim_strnsave(*fname, len2);
20991 pbuf3 = NULL;
20992
20993 s = pbuf2 + len2 - 1; /* Find the end */
20994 slen = 1;
20995 plen = len2;
20996
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020997 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 {
20999 --s;
21000 ++slen;
21001 --plen;
21002 }
21003
21004 do
21005 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021006 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021007 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 {
21009 --s;
21010 ++slen;
21011 --plen;
21012 }
21013 if (s <= pbuf2)
21014 break;
21015
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021016 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 ch = *s;
21018 *s = 0; /* get_short_pathname requires a null-terminated string */
21019
21020 /* Try it in situ */
21021 p = pbuf2;
21022 if (!get_short_pathname(&p, &pbuf3, &plen))
21023 {
21024 vim_free(pbuf2);
21025 return -1;
21026 }
21027 *s = ch; /* Preserve the string */
21028 } while (plen == 0);
21029
21030 if (plen > 0)
21031 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021032 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 *fnamelen = len = plen + slen;
21034 vim_free(*bufp);
21035 if (len > len2)
21036 {
21037 /* If there's not enough space in the currently allocated string,
21038 * then copy it to a buffer big enough.
21039 */
21040 *fname= *bufp = vim_strnsave(p, len);
21041 if (*fname == NULL)
21042 return -1;
21043 }
21044 else
21045 {
21046 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21047 *fname = *bufp = pbuf2;
21048 if (p != pbuf2)
21049 strncpy(*fname, p, plen);
21050 pbuf2 = NULL;
21051 }
21052 /* Concat the next bit */
21053 strncpy(*fname + plen, s, slen);
21054 (*fname)[len] = '\0';
21055 }
21056 vim_free(pbuf3);
21057 vim_free(pbuf2);
21058 return 0;
21059}
21060
21061/*
21062 * Get a pathname for a partial path.
21063 */
21064 static int
21065shortpath_for_partial(fnamep, bufp, fnamelen)
21066 char_u **fnamep;
21067 char_u **bufp;
21068 int *fnamelen;
21069{
21070 int sepcount, len, tflen;
21071 char_u *p;
21072 char_u *pbuf, *tfname;
21073 int hasTilde;
21074
21075 /* Count up the path seperators from the RHS.. so we know which part
21076 * of the path to return.
21077 */
21078 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021079 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 if (vim_ispathsep(*p))
21081 ++sepcount;
21082
21083 /* Need full path first (use expand_env() to remove a "~/") */
21084 hasTilde = (**fnamep == '~');
21085 if (hasTilde)
21086 pbuf = tfname = expand_env_save(*fnamep);
21087 else
21088 pbuf = tfname = FullName_save(*fnamep, FALSE);
21089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021090 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091
21092 if (!get_short_pathname(&tfname, &pbuf, &len))
21093 return -1;
21094
21095 if (len == 0)
21096 {
21097 /* Don't have a valid filename, so shorten the rest of the
21098 * path if we can. This CAN give us invalid 8.3 filenames, but
21099 * there's not a lot of point in guessing what it might be.
21100 */
21101 len = tflen;
21102 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21103 return -1;
21104 }
21105
21106 /* Count the paths backward to find the beginning of the desired string. */
21107 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021108 {
21109#ifdef FEAT_MBYTE
21110 if (has_mbyte)
21111 p -= mb_head_off(tfname, p);
21112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 if (vim_ispathsep(*p))
21114 {
21115 if (sepcount == 0 || (hasTilde && sepcount == 1))
21116 break;
21117 else
21118 sepcount --;
21119 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 if (hasTilde)
21122 {
21123 --p;
21124 if (p >= tfname)
21125 *p = '~';
21126 else
21127 return -1;
21128 }
21129 else
21130 ++p;
21131
21132 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21133 vim_free(*bufp);
21134 *fnamelen = (int)STRLEN(p);
21135 *bufp = pbuf;
21136 *fnamep = p;
21137
21138 return 0;
21139}
21140#endif /* WIN3264 */
21141
21142/*
21143 * Adjust a filename, according to a string of modifiers.
21144 * *fnamep must be NUL terminated when called. When returning, the length is
21145 * determined by *fnamelen.
21146 * Returns valid flags.
21147 * When there is an error, *fnamep is set to NULL.
21148 */
21149 int
21150modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21151 char_u *src; /* string with modifiers */
21152 int *usedlen; /* characters after src that are used */
21153 char_u **fnamep; /* file name so far */
21154 char_u **bufp; /* buffer for allocated file name or NULL */
21155 int *fnamelen; /* length of fnamep */
21156{
21157 int valid = 0;
21158 char_u *tail;
21159 char_u *s, *p, *pbuf;
21160 char_u dirname[MAXPATHL];
21161 int c;
21162 int has_fullname = 0;
21163#ifdef WIN3264
21164 int has_shortname = 0;
21165#endif
21166
21167repeat:
21168 /* ":p" - full path/file_name */
21169 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21170 {
21171 has_fullname = 1;
21172
21173 valid |= VALID_PATH;
21174 *usedlen += 2;
21175
21176 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21177 if ((*fnamep)[0] == '~'
21178#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21179 && ((*fnamep)[1] == '/'
21180# ifdef BACKSLASH_IN_FILENAME
21181 || (*fnamep)[1] == '\\'
21182# endif
21183 || (*fnamep)[1] == NUL)
21184
21185#endif
21186 )
21187 {
21188 *fnamep = expand_env_save(*fnamep);
21189 vim_free(*bufp); /* free any allocated file name */
21190 *bufp = *fnamep;
21191 if (*fnamep == NULL)
21192 return -1;
21193 }
21194
21195 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021196 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 {
21198 if (vim_ispathsep(*p)
21199 && p[1] == '.'
21200 && (p[2] == NUL
21201 || vim_ispathsep(p[2])
21202 || (p[2] == '.'
21203 && (p[3] == NUL || vim_ispathsep(p[3])))))
21204 break;
21205 }
21206
21207 /* FullName_save() is slow, don't use it when not needed. */
21208 if (*p != NUL || !vim_isAbsName(*fnamep))
21209 {
21210 *fnamep = FullName_save(*fnamep, *p != NUL);
21211 vim_free(*bufp); /* free any allocated file name */
21212 *bufp = *fnamep;
21213 if (*fnamep == NULL)
21214 return -1;
21215 }
21216
21217 /* Append a path separator to a directory. */
21218 if (mch_isdir(*fnamep))
21219 {
21220 /* Make room for one or two extra characters. */
21221 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21222 vim_free(*bufp); /* free any allocated file name */
21223 *bufp = *fnamep;
21224 if (*fnamep == NULL)
21225 return -1;
21226 add_pathsep(*fnamep);
21227 }
21228 }
21229
21230 /* ":." - path relative to the current directory */
21231 /* ":~" - path relative to the home directory */
21232 /* ":8" - shortname path - postponed till after */
21233 while (src[*usedlen] == ':'
21234 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21235 {
21236 *usedlen += 2;
21237 if (c == '8')
21238 {
21239#ifdef WIN3264
21240 has_shortname = 1; /* Postpone this. */
21241#endif
21242 continue;
21243 }
21244 pbuf = NULL;
21245 /* Need full path first (use expand_env() to remove a "~/") */
21246 if (!has_fullname)
21247 {
21248 if (c == '.' && **fnamep == '~')
21249 p = pbuf = expand_env_save(*fnamep);
21250 else
21251 p = pbuf = FullName_save(*fnamep, FALSE);
21252 }
21253 else
21254 p = *fnamep;
21255
21256 has_fullname = 0;
21257
21258 if (p != NULL)
21259 {
21260 if (c == '.')
21261 {
21262 mch_dirname(dirname, MAXPATHL);
21263 s = shorten_fname(p, dirname);
21264 if (s != NULL)
21265 {
21266 *fnamep = s;
21267 if (pbuf != NULL)
21268 {
21269 vim_free(*bufp); /* free any allocated file name */
21270 *bufp = pbuf;
21271 pbuf = NULL;
21272 }
21273 }
21274 }
21275 else
21276 {
21277 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21278 /* Only replace it when it starts with '~' */
21279 if (*dirname == '~')
21280 {
21281 s = vim_strsave(dirname);
21282 if (s != NULL)
21283 {
21284 *fnamep = s;
21285 vim_free(*bufp);
21286 *bufp = s;
21287 }
21288 }
21289 }
21290 vim_free(pbuf);
21291 }
21292 }
21293
21294 tail = gettail(*fnamep);
21295 *fnamelen = (int)STRLEN(*fnamep);
21296
21297 /* ":h" - head, remove "/file_name", can be repeated */
21298 /* Don't remove the first "/" or "c:\" */
21299 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21300 {
21301 valid |= VALID_HEAD;
21302 *usedlen += 2;
21303 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021304 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 --tail;
21306 *fnamelen = (int)(tail - *fnamep);
21307#ifdef VMS
21308 if (*fnamelen > 0)
21309 *fnamelen += 1; /* the path separator is part of the path */
21310#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021311 while (tail > s && !after_pathsep(s, tail))
21312 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 }
21314
21315 /* ":8" - shortname */
21316 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21317 {
21318 *usedlen += 2;
21319#ifdef WIN3264
21320 has_shortname = 1;
21321#endif
21322 }
21323
21324#ifdef WIN3264
21325 /* Check shortname after we have done 'heads' and before we do 'tails'
21326 */
21327 if (has_shortname)
21328 {
21329 pbuf = NULL;
21330 /* Copy the string if it is shortened by :h */
21331 if (*fnamelen < (int)STRLEN(*fnamep))
21332 {
21333 p = vim_strnsave(*fnamep, *fnamelen);
21334 if (p == 0)
21335 return -1;
21336 vim_free(*bufp);
21337 *bufp = *fnamep = p;
21338 }
21339
21340 /* Split into two implementations - makes it easier. First is where
21341 * there isn't a full name already, second is where there is.
21342 */
21343 if (!has_fullname && !vim_isAbsName(*fnamep))
21344 {
21345 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21346 return -1;
21347 }
21348 else
21349 {
21350 int l;
21351
21352 /* Simple case, already have the full-name
21353 * Nearly always shorter, so try first time. */
21354 l = *fnamelen;
21355 if (!get_short_pathname(fnamep, bufp, &l))
21356 return -1;
21357
21358 if (l == 0)
21359 {
21360 /* Couldn't find the filename.. search the paths.
21361 */
21362 l = *fnamelen;
21363 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21364 return -1;
21365 }
21366 *fnamelen = l;
21367 }
21368 }
21369#endif /* WIN3264 */
21370
21371 /* ":t" - tail, just the basename */
21372 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21373 {
21374 *usedlen += 2;
21375 *fnamelen -= (int)(tail - *fnamep);
21376 *fnamep = tail;
21377 }
21378
21379 /* ":e" - extension, can be repeated */
21380 /* ":r" - root, without extension, can be repeated */
21381 while (src[*usedlen] == ':'
21382 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21383 {
21384 /* find a '.' in the tail:
21385 * - for second :e: before the current fname
21386 * - otherwise: The last '.'
21387 */
21388 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21389 s = *fnamep - 2;
21390 else
21391 s = *fnamep + *fnamelen - 1;
21392 for ( ; s > tail; --s)
21393 if (s[0] == '.')
21394 break;
21395 if (src[*usedlen + 1] == 'e') /* :e */
21396 {
21397 if (s > tail)
21398 {
21399 *fnamelen += (int)(*fnamep - (s + 1));
21400 *fnamep = s + 1;
21401#ifdef VMS
21402 /* cut version from the extension */
21403 s = *fnamep + *fnamelen - 1;
21404 for ( ; s > *fnamep; --s)
21405 if (s[0] == ';')
21406 break;
21407 if (s > *fnamep)
21408 *fnamelen = s - *fnamep;
21409#endif
21410 }
21411 else if (*fnamep <= tail)
21412 *fnamelen = 0;
21413 }
21414 else /* :r */
21415 {
21416 if (s > tail) /* remove one extension */
21417 *fnamelen = (int)(s - *fnamep);
21418 }
21419 *usedlen += 2;
21420 }
21421
21422 /* ":s?pat?foo?" - substitute */
21423 /* ":gs?pat?foo?" - global substitute */
21424 if (src[*usedlen] == ':'
21425 && (src[*usedlen + 1] == 's'
21426 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21427 {
21428 char_u *str;
21429 char_u *pat;
21430 char_u *sub;
21431 int sep;
21432 char_u *flags;
21433 int didit = FALSE;
21434
21435 flags = (char_u *)"";
21436 s = src + *usedlen + 2;
21437 if (src[*usedlen + 1] == 'g')
21438 {
21439 flags = (char_u *)"g";
21440 ++s;
21441 }
21442
21443 sep = *s++;
21444 if (sep)
21445 {
21446 /* find end of pattern */
21447 p = vim_strchr(s, sep);
21448 if (p != NULL)
21449 {
21450 pat = vim_strnsave(s, (int)(p - s));
21451 if (pat != NULL)
21452 {
21453 s = p + 1;
21454 /* find end of substitution */
21455 p = vim_strchr(s, sep);
21456 if (p != NULL)
21457 {
21458 sub = vim_strnsave(s, (int)(p - s));
21459 str = vim_strnsave(*fnamep, *fnamelen);
21460 if (sub != NULL && str != NULL)
21461 {
21462 *usedlen = (int)(p + 1 - src);
21463 s = do_string_sub(str, pat, sub, flags);
21464 if (s != NULL)
21465 {
21466 *fnamep = s;
21467 *fnamelen = (int)STRLEN(s);
21468 vim_free(*bufp);
21469 *bufp = s;
21470 didit = TRUE;
21471 }
21472 }
21473 vim_free(sub);
21474 vim_free(str);
21475 }
21476 vim_free(pat);
21477 }
21478 }
21479 /* after using ":s", repeat all the modifiers */
21480 if (didit)
21481 goto repeat;
21482 }
21483 }
21484
21485 return valid;
21486}
21487
21488/*
21489 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21490 * "flags" can be "g" to do a global substitute.
21491 * Returns an allocated string, NULL for error.
21492 */
21493 char_u *
21494do_string_sub(str, pat, sub, flags)
21495 char_u *str;
21496 char_u *pat;
21497 char_u *sub;
21498 char_u *flags;
21499{
21500 int sublen;
21501 regmatch_T regmatch;
21502 int i;
21503 int do_all;
21504 char_u *tail;
21505 garray_T ga;
21506 char_u *ret;
21507 char_u *save_cpo;
21508
21509 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21510 save_cpo = p_cpo;
21511 p_cpo = (char_u *)"";
21512
21513 ga_init2(&ga, 1, 200);
21514
21515 do_all = (flags[0] == 'g');
21516
21517 regmatch.rm_ic = p_ic;
21518 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21519 if (regmatch.regprog != NULL)
21520 {
21521 tail = str;
21522 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21523 {
21524 /*
21525 * Get some space for a temporary buffer to do the substitution
21526 * into. It will contain:
21527 * - The text up to where the match is.
21528 * - The substituted text.
21529 * - The text after the match.
21530 */
21531 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21532 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21533 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21534 {
21535 ga_clear(&ga);
21536 break;
21537 }
21538
21539 /* copy the text up to where the match is */
21540 i = (int)(regmatch.startp[0] - tail);
21541 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21542 /* add the substituted text */
21543 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21544 + ga.ga_len + i, TRUE, TRUE, FALSE);
21545 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 /* avoid getting stuck on a match with an empty string */
21547 if (tail == regmatch.endp[0])
21548 {
21549 if (*tail == NUL)
21550 break;
21551 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21552 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 }
21554 else
21555 {
21556 tail = regmatch.endp[0];
21557 if (*tail == NUL)
21558 break;
21559 }
21560 if (!do_all)
21561 break;
21562 }
21563
21564 if (ga.ga_data != NULL)
21565 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21566
21567 vim_free(regmatch.regprog);
21568 }
21569
21570 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21571 ga_clear(&ga);
21572 p_cpo = save_cpo;
21573
21574 return ret;
21575}
21576
21577#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */