blob: d55ba10ae9b3470a2da4a4adb4dc248cacaf4c63 [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));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
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));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000544static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000558static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000564static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000580static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000586#ifdef vim_mkdir
587static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
588#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000592static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000594static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000595static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000597static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000598static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000611static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000613static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000620static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000621static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000622static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000624static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000626static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000629static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000633static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634#ifdef HAVE_STRFTIME
635static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000649static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000650static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000651static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000652static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000653static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000655static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000669static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000674static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
675static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static int get_env_len __ARGS((char_u **arg));
677static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000678static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000679static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
680#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
681#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
682 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000683static 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 +0000684static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000685static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000686static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
687static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static typval_T *alloc_tv __ARGS((void));
689static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void init_tv __ARGS((typval_T *varp));
691static long get_tv_number __ARGS((typval_T *varp));
692static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000693static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static char_u *get_tv_string __ARGS((typval_T *varp));
695static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000696static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000698static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
700static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
701static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
702static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
703static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
704static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
705static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000706static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000707static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000709static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
711static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
712static int eval_fname_script __ARGS((char_u *p));
713static int eval_fname_sid __ARGS((char_u *p));
714static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static ufunc_T *find_func __ARGS((char_u *name));
716static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000717static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000718#ifdef FEAT_PROFILE
719static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000720static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
721static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
722static int
723# ifdef __BORLANDC__
724 _RTLENTRYF
725# endif
726 prof_total_cmp __ARGS((const void *s1, const void *s2));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000732#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000733static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000734static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000735static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void func_free __ARGS((ufunc_T *fp));
737static void func_unref __ARGS((char_u *name));
738static void func_ref __ARGS((char_u *name));
739static 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));
740static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000741static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
742static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000743static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000744static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000745static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000747/* Character used as separated in autoload function/variable names. */
748#define AUTOLOAD_CHAR '#'
749
Bram Moolenaar33570922005-01-25 22:26:29 +0000750/*
751 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000752 */
753 void
754eval_init()
755{
Bram Moolenaar33570922005-01-25 22:26:29 +0000756 int i;
757 struct vimvar *p;
758
759 init_var_dict(&globvardict, &globvars_var);
760 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000761 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000762 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763
764 for (i = 0; i < VV_LEN; ++i)
765 {
766 p = &vimvars[i];
767 STRCPY(p->vv_di.di_key, p->vv_name);
768 if (p->vv_flags & VV_RO)
769 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
770 else if (p->vv_flags & VV_RO_SBX)
771 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
772 else
773 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000774
775 /* add to v: scope dict, unless the value is not always available */
776 if (p->vv_type != VAR_UNKNOWN)
777 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000779 /* add to compat scope dict */
780 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000782}
783
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784#if defined(EXITFREE) || defined(PROTO)
785 void
786eval_clear()
787{
788 int i;
789 struct vimvar *p;
790
791 for (i = 0; i < VV_LEN; ++i)
792 {
793 p = &vimvars[i];
794 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000795 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000797 p->vv_di.di_tv.vval.v_string = NULL;
798 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000799 }
800 hash_clear(&vimvarht);
801 hash_clear(&compat_hashtab);
802
803 /* script-local variables */
804 for (i = 1; i <= ga_scripts.ga_len; ++i)
805 vars_clear(&SCRIPT_VARS(i));
806 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000807 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000808
809 /* global variables */
810 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000811
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000812 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000813 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000814 hash_clear(&func_hashtab);
815
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000816 /* autoloaded script names */
817 ga_clear_strings(&ga_loaded);
818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 /* unreferenced lists and dicts */
820 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000821}
822#endif
823
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * Return the name of the executed function.
826 */
827 char_u *
828func_name(cookie)
829 void *cookie;
830{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000831 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the address holding the next breakpoint line for a funccall cookie.
836 */
837 linenr_T *
838func_breakpoint(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Return the address holding the debug tick for a funccall cookie.
846 */
847 int *
848func_dbg_tick(cookie)
849 void *cookie;
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
854/*
855 * Return the nesting level for a funccall cookie.
856 */
857 int
858func_level(cookie)
859 void *cookie;
860{
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863
864/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000865funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867/*
868 * Return TRUE when a function was ended by a ":return" command.
869 */
870 int
871current_func_returned()
872{
873 return current_funccal->returned;
874}
875
876
877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Set an internal variable to a string value. Creates the variable if it does
879 * not already exist.
880 */
881 void
882set_internal_string_var(name, value)
883 char_u *name;
884 char_u *value;
885{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000886 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 val = vim_strsave(value);
890 if (val != NULL)
891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000895 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 }
899}
900
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000901static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000902static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000903static char_u *redir_endp = NULL;
904static char_u *redir_varname = NULL;
905
906/*
907 * Start recording command output to a variable
908 * Returns OK if successfully completed the setup. FAIL otherwise.
909 */
910 int
911var_redir_start(name, append)
912 char_u *name;
913 int append; /* append to an existing variable */
914{
915 int save_emsg;
916 int err;
917 typval_T tv;
918
919 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000920 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000921 {
922 EMSG(_(e_invarg));
923 return FAIL;
924 }
925
926 redir_varname = vim_strsave(name);
927 if (redir_varname == NULL)
928 return FAIL;
929
930 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
931 if (redir_lval == NULL)
932 {
933 var_redir_stop();
934 return FAIL;
935 }
936
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000937 /* The output is stored in growarray "redir_ga" until redirection ends. */
938 ga_init2(&redir_ga, (int)sizeof(char), 500);
939
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000941 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
942 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000943 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
944 {
945 if (redir_endp != NULL && *redir_endp != NUL)
946 /* Trailing characters are present after the variable name */
947 EMSG(_(e_trailing));
948 else
949 EMSG(_(e_invarg));
950 var_redir_stop();
951 return FAIL;
952 }
953
954 /* check if we can write to the variable: set it to or append an empty
955 * string */
956 save_emsg = did_emsg;
957 did_emsg = FALSE;
958 tv.v_type = VAR_STRING;
959 tv.vval.v_string = (char_u *)"";
960 if (append)
961 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
962 else
963 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
964 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000965 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000966 if (err)
967 {
968 var_redir_stop();
969 return FAIL;
970 }
971 if (redir_lval->ll_newkey != NULL)
972 {
973 /* Dictionary item was created, don't do it again. */
974 vim_free(redir_lval->ll_newkey);
975 redir_lval->ll_newkey = NULL;
976 }
977
978 return OK;
979}
980
981/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000982 * Append "value[value_len]" to the variable set by var_redir_start().
983 * The actual appending is postponed until redirection ends, because the value
984 * appended may in fact be the string we write to, changing it may cause freed
985 * memory to be used:
986 * :redir => foo
987 * :let foo
988 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000989 */
990 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000991var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000992 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000993 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000995 size_t len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000996
997 if (redir_lval == NULL)
998 return;
999
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001000 if (value_len == -1)
1001 len = STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002 else
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001003 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (ga_grow(&redir_ga, (int)len) == OK)
1006 {
1007 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001008 redir_ga.ga_len += (int)len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001009 }
1010 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012}
1013
1014/*
1015 * Stop redirecting command output to a variable.
1016 */
1017 void
1018var_redir_stop()
1019{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020 typval_T tv;
1021
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022 if (redir_lval != NULL)
1023 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024 /* Append the trailing NUL. */
1025 ga_append(&redir_ga, NUL);
1026
1027 /* Assign the text to the variable. */
1028 tv.v_type = VAR_STRING;
1029 tv.vval.v_string = redir_ga.ga_data;
1030 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1031 vim_free(tv.vval.v_string);
1032
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 clear_lval(redir_lval);
1034 vim_free(redir_lval);
1035 redir_lval = NULL;
1036 }
1037 vim_free(redir_varname);
1038 redir_varname = NULL;
1039}
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041# if defined(FEAT_MBYTE) || defined(PROTO)
1042 int
1043eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1044 char_u *enc_from;
1045 char_u *enc_to;
1046 char_u *fname_from;
1047 char_u *fname_to;
1048{
1049 int err = FALSE;
1050
1051 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1052 set_vim_var_string(VV_CC_TO, enc_to, -1);
1053 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1054 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1055 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1056 err = TRUE;
1057 set_vim_var_string(VV_CC_FROM, NULL, -1);
1058 set_vim_var_string(VV_CC_TO, NULL, -1);
1059 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1060 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1061
1062 if (err)
1063 return FAIL;
1064 return OK;
1065}
1066# endif
1067
1068# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1069 int
1070eval_printexpr(fname, args)
1071 char_u *fname;
1072 char_u *args;
1073{
1074 int err = FALSE;
1075
1076 set_vim_var_string(VV_FNAME_IN, fname, -1);
1077 set_vim_var_string(VV_CMDARG, args, -1);
1078 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1079 err = TRUE;
1080 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1081 set_vim_var_string(VV_CMDARG, NULL, -1);
1082
1083 if (err)
1084 {
1085 mch_remove(fname);
1086 return FAIL;
1087 }
1088 return OK;
1089}
1090# endif
1091
1092# if defined(FEAT_DIFF) || defined(PROTO)
1093 void
1094eval_diff(origfile, newfile, outfile)
1095 char_u *origfile;
1096 char_u *newfile;
1097 char_u *outfile;
1098{
1099 int err = FALSE;
1100
1101 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1102 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1103 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1104 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1105 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1106 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1107 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1108}
1109
1110 void
1111eval_patch(origfile, difffile, outfile)
1112 char_u *origfile;
1113 char_u *difffile;
1114 char_u *outfile;
1115{
1116 int err;
1117
1118 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1119 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1120 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1121 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1122 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1123 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1124 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1125}
1126# endif
1127
1128/*
1129 * Top level evaluation function, returning a boolean.
1130 * Sets "error" to TRUE if there was an error.
1131 * Return TRUE or FALSE.
1132 */
1133 int
1134eval_to_bool(arg, error, nextcmd, skip)
1135 char_u *arg;
1136 int *error;
1137 char_u **nextcmd;
1138 int skip; /* only parse, don't execute */
1139{
Bram Moolenaar33570922005-01-25 22:26:29 +00001140 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int retval = FALSE;
1142
1143 if (skip)
1144 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 else
1148 {
1149 *error = FALSE;
1150 if (!skip)
1151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001152 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 }
1156 if (skip)
1157 --emsg_skip;
1158
1159 return retval;
1160}
1161
1162/*
1163 * Top level evaluation function, returning a string. If "skip" is TRUE,
1164 * only parsing to "nextcmd" is done, without reporting errors. Return
1165 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1166 */
1167 char_u *
1168eval_to_string_skip(arg, nextcmd, skip)
1169 char_u *arg;
1170 char_u **nextcmd;
1171 int skip; /* only parse, don't execute */
1172{
Bram Moolenaar33570922005-01-25 22:26:29 +00001173 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *retval;
1175
1176 if (skip)
1177 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 retval = NULL;
1180 else
1181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182 retval = vim_strsave(get_tv_string(&tv));
1183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 if (skip)
1186 --emsg_skip;
1187
1188 return retval;
1189}
1190
1191/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001192 * Skip over an expression at "*pp".
1193 * Return FAIL for an error, OK otherwise.
1194 */
1195 int
1196skip_expr(pp)
1197 char_u **pp;
1198{
Bram Moolenaar33570922005-01-25 22:26:29 +00001199 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001200
1201 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001203}
1204
1205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 * Top level evaluation function, returning a string.
1207 * Return pointer to allocated memory, or NULL for failure.
1208 */
1209 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001210eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 char_u *arg;
1212 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001213 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
Bram Moolenaar33570922005-01-25 22:26:29 +00001215 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001217 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 retval = NULL;
1221 else
1222 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001223 if (dolist && tv.v_type == VAR_LIST)
1224 {
1225 ga_init2(&ga, (int)sizeof(char), 80);
1226 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1227 ga_append(&ga, NUL);
1228 retval = (char_u *)ga.ga_data;
1229 }
1230 else
1231 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234
1235 return retval;
1236}
1237
1238/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001239 * Call eval_to_string() without using current local variables and using
1240 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
1242 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001243eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 char_u *arg;
1245 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 char_u *retval;
1249 void *save_funccalp;
1250
1251 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001252 if (use_sandbox)
1253 ++sandbox;
1254 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001255 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001256 if (use_sandbox)
1257 --sandbox;
1258 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 restore_funccal(save_funccalp);
1260 return retval;
1261}
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263/*
1264 * Top level evaluation function, returning a number.
1265 * Evaluates "expr" silently.
1266 * Returns -1 for an error.
1267 */
1268 int
1269eval_to_number(expr)
1270 char_u *expr;
1271{
Bram Moolenaar33570922005-01-25 22:26:29 +00001272 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001274 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276 ++emsg_off;
1277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 retval = -1;
1280 else
1281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001282 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285 --emsg_off;
1286
1287 return retval;
1288}
1289
Bram Moolenaara40058a2005-07-11 22:42:07 +00001290/*
1291 * Prepare v: variable "idx" to be used.
1292 * Save the current typeval in "save_tv".
1293 * When not used yet add the variable to the v: hashtable.
1294 */
1295 static void
1296prepare_vimvar(idx, save_tv)
1297 int idx;
1298 typval_T *save_tv;
1299{
1300 *save_tv = vimvars[idx].vv_tv;
1301 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1302 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1303}
1304
1305/*
1306 * Restore v: variable "idx" to typeval "save_tv".
1307 * When no longer defined, remove the variable from the v: hashtable.
1308 */
1309 static void
1310restore_vimvar(idx, save_tv)
1311 int idx;
1312 typval_T *save_tv;
1313{
1314 hashitem_T *hi;
1315
1316 clear_tv(&vimvars[idx].vv_tv);
1317 vimvars[idx].vv_tv = *save_tv;
1318 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1319 {
1320 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1321 if (HASHITEM_EMPTY(hi))
1322 EMSG2(_(e_intern2), "restore_vimvar()");
1323 else
1324 hash_remove(&vimvarht, hi);
1325 }
1326}
1327
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001328#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001329/*
1330 * Evaluate an expression to a list with suggestions.
1331 * For the "expr:" part of 'spellsuggest'.
1332 */
1333 list_T *
1334eval_spell_expr(badword, expr)
1335 char_u *badword;
1336 char_u *expr;
1337{
1338 typval_T save_val;
1339 typval_T rettv;
1340 list_T *list = NULL;
1341 char_u *p = skipwhite(expr);
1342
1343 /* Set "v:val" to the bad word. */
1344 prepare_vimvar(VV_VAL, &save_val);
1345 vimvars[VV_VAL].vv_type = VAR_STRING;
1346 vimvars[VV_VAL].vv_str = badword;
1347 if (p_verbose == 0)
1348 ++emsg_off;
1349
1350 if (eval1(&p, &rettv, TRUE) == OK)
1351 {
1352 if (rettv.v_type != VAR_LIST)
1353 clear_tv(&rettv);
1354 else
1355 list = rettv.vval.v_list;
1356 }
1357
1358 if (p_verbose == 0)
1359 --emsg_off;
1360 vimvars[VV_VAL].vv_str = NULL;
1361 restore_vimvar(VV_VAL, &save_val);
1362
1363 return list;
1364}
1365
1366/*
1367 * "list" is supposed to contain two items: a word and a number. Return the
1368 * word in "pp" and the number as the return value.
1369 * Return -1 if anything isn't right.
1370 * Used to get the good word and score from the eval_spell_expr() result.
1371 */
1372 int
1373get_spellword(list, pp)
1374 list_T *list;
1375 char_u **pp;
1376{
1377 listitem_T *li;
1378
1379 li = list->lv_first;
1380 if (li == NULL)
1381 return -1;
1382 *pp = get_tv_string(&li->li_tv);
1383
1384 li = li->li_next;
1385 if (li == NULL)
1386 return -1;
1387 return get_tv_number(&li->li_tv);
1388}
1389#endif
1390
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001391/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001392 * Top level evaluation function.
1393 * Returns an allocated typval_T with the result.
1394 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395 */
1396 typval_T *
1397eval_expr(arg, nextcmd)
1398 char_u *arg;
1399 char_u **nextcmd;
1400{
1401 typval_T *tv;
1402
1403 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001404 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001405 {
1406 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001407 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001408 }
1409
1410 return tv;
1411}
1412
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1415/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 static int
1421call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 char_u *func;
1423 int argc;
1424 char_u **argv;
1425 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
Bram Moolenaar33570922005-01-25 22:26:29 +00001428 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 long n;
1430 int len;
1431 int i;
1432 int doesrange;
1433 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001436 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 for (i = 0; i < argc; i++)
1441 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001442 /* Pass a NULL or empty argument as an empty string */
1443 if (argv[i] == NULL || *argv[i] == NUL)
1444 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001445 argvars[i].v_type = VAR_STRING;
1446 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001447 continue;
1448 }
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /* Recognize a number argument, the others must be strings. */
1451 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1452 if (len != 0 && len == (int)STRLEN(argv[i]))
1453 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001454 argvars[i].v_type = VAR_NUMBER;
1455 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 else
1458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001459 argvars[i].v_type = VAR_STRING;
1460 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 }
1463
1464 if (safe)
1465 {
1466 save_funccalp = save_funccal();
1467 ++sandbox;
1468 }
1469
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1471 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001473 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (safe)
1475 {
1476 --sandbox;
1477 restore_funccal(save_funccalp);
1478 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 vim_free(argvars);
1480
1481 if (ret == FAIL)
1482 clear_tv(rettv);
1483
1484 return ret;
1485}
1486
1487/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001488 * Call vimL function "func" and return the result as a string.
1489 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490 * Uses argv[argc] for the function arguments.
1491 */
1492 void *
1493call_func_retstr(func, argc, argv, safe)
1494 char_u *func;
1495 int argc;
1496 char_u **argv;
1497 int safe; /* use the sandbox */
1498{
1499 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501
1502 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1503 return NULL;
1504
1505 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 return retval;
1508}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001509
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001510#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001511/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001512 * Call vimL function "func" and return the result as a number.
1513 * Returns -1 when calling the function fails.
1514 * Uses argv[argc] for the function arguments.
1515 */
1516 long
1517call_func_retnr(func, argc, argv, safe)
1518 char_u *func;
1519 int argc;
1520 char_u **argv;
1521 int safe; /* use the sandbox */
1522{
1523 typval_T rettv;
1524 long retval;
1525
1526 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1527 return -1;
1528
1529 retval = get_tv_number_chk(&rettv, NULL);
1530 clear_tv(&rettv);
1531 return retval;
1532}
1533#endif
1534
1535/*
1536 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001537 * Uses argv[argc] for the function arguments.
1538 */
1539 void *
1540call_func_retlist(func, argc, argv, safe)
1541 char_u *func;
1542 int argc;
1543 char_u **argv;
1544 int safe; /* use the sandbox */
1545{
1546 typval_T rettv;
1547
1548 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1549 return NULL;
1550
1551 if (rettv.v_type != VAR_LIST)
1552 {
1553 clear_tv(&rettv);
1554 return NULL;
1555 }
1556
1557 return rettv.vval.v_list;
1558}
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560#endif
1561
1562/*
1563 * Save the current function call pointer, and set it to NULL.
1564 * Used when executing autocommands and for ":source".
1565 */
1566 void *
1567save_funccal()
1568{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001569 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 current_funccal = NULL;
1572 return (void *)fc;
1573}
1574
1575 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001576restore_funccal(vfc)
1577 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001579 funccall_T *fc = (funccall_T *)vfc;
1580
1581 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582}
1583
Bram Moolenaar05159a02005-02-26 23:04:13 +00001584#if defined(FEAT_PROFILE) || defined(PROTO)
1585/*
1586 * Prepare profiling for entering a child or something else that is not
1587 * counted for the script/function itself.
1588 * Should always be called in pair with prof_child_exit().
1589 */
1590 void
1591prof_child_enter(tm)
1592 proftime_T *tm; /* place to store waittime */
1593{
1594 funccall_T *fc = current_funccal;
1595
1596 if (fc != NULL && fc->func->uf_profiling)
1597 profile_start(&fc->prof_child);
1598 script_prof_save(tm);
1599}
1600
1601/*
1602 * Take care of time spent in a child.
1603 * Should always be called after prof_child_enter().
1604 */
1605 void
1606prof_child_exit(tm)
1607 proftime_T *tm; /* where waittime was stored */
1608{
1609 funccall_T *fc = current_funccal;
1610
1611 if (fc != NULL && fc->func->uf_profiling)
1612 {
1613 profile_end(&fc->prof_child);
1614 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1615 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1616 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1617 }
1618 script_prof_restore(tm);
1619}
1620#endif
1621
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#ifdef FEAT_FOLDING
1624/*
1625 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1626 * it in "*cp". Doesn't give error messages.
1627 */
1628 int
1629eval_foldexpr(arg, cp)
1630 char_u *arg;
1631 int *cp;
1632{
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 int retval;
1635 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001636 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1637 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001640 if (use_sandbox)
1641 ++sandbox;
1642 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 retval = 0;
1646 else
1647 {
1648 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 if (tv.v_type == VAR_NUMBER)
1650 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001651 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 retval = 0;
1653 else
1654 {
1655 /* If the result is a string, check if there is a non-digit before
1656 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (!VIM_ISDIGIT(*s) && *s != '-')
1659 *cp = *s++;
1660 retval = atol((char *)s);
1661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001665 if (use_sandbox)
1666 --sandbox;
1667 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 return retval;
1670}
1671#endif
1672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 * ":let" list all variable values
1675 * ":let var1 var2" list variable values
1676 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 * ":let var += expr" assignment command.
1678 * ":let var -= expr" assignment command.
1679 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
1682 void
1683ex_let(eap)
1684 exarg_T *eap;
1685{
1686 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001688 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 int var_count = 0;
1691 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001693 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaardb552d602006-03-23 22:59:57 +00001695 argend = skip_var_list(arg, &var_count, &semicolon);
1696 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001698 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1699 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001700 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001703 /*
1704 * ":let" without "=": list variables
1705 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 if (*arg == '[')
1707 EMSG(_(e_invarg));
1708 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 /* ":let var1 var2" */
1710 arg = list_arg_vars(eap, arg);
1711 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001714 list_glob_vars();
1715 list_buf_vars();
1716 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001717#ifdef FEAT_WINDOWS
1718 list_tab_vars();
1719#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001720 list_script_vars();
1721 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 list_vim_vars();
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 eap->nextcmd = check_nextcmd(arg);
1725 }
1726 else
1727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 op[0] = '=';
1729 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001730 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 {
1732 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1733 op[0] = expr[-1]; /* +=, -= or .= */
1734 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (eap->skip)
1738 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (eap->skip)
1741 {
1742 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 --emsg_skip;
1745 }
1746 else if (i != FAIL)
1747 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 }
1753}
1754
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755/*
1756 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1757 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 * When "nextchars" is not NULL it points to a string with characters that
1759 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1760 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761 * Returns OK or FAIL;
1762 */
1763 static int
1764ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1765 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001766 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767 int copy; /* copy values from "tv", don't move */
1768 int semicolon; /* from skip_var_list() */
1769 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771{
1772 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 listitem_T *item;
1776 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777
1778 if (*arg != '[')
1779 {
1780 /*
1781 * ":let var = expr" or ":for var in list"
1782 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 return FAIL;
1785 return OK;
1786 }
1787
1788 /*
1789 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1790 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001791 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 {
1793 EMSG(_(e_listreq));
1794 return FAIL;
1795 }
1796
1797 i = list_len(l);
1798 if (semicolon == 0 && var_count < i)
1799 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001800 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 return FAIL;
1802 }
1803 if (var_count - semicolon > i)
1804 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001805 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 return FAIL;
1807 }
1808
1809 item = l->lv_first;
1810 while (*arg != ']')
1811 {
1812 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001813 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 item = item->li_next;
1815 if (arg == NULL)
1816 return FAIL;
1817
1818 arg = skipwhite(arg);
1819 if (*arg == ';')
1820 {
1821 /* Put the rest of the list (may be empty) in the var after ';'.
1822 * Create a new list for this. */
1823 l = list_alloc();
1824 if (l == NULL)
1825 return FAIL;
1826 while (item != NULL)
1827 {
1828 list_append_tv(l, &item->li_tv);
1829 item = item->li_next;
1830 }
1831
1832 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001833 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 ltv.vval.v_list = l;
1835 l->lv_refcount = 1;
1836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1838 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 clear_tv(&ltv);
1840 if (arg == NULL)
1841 return FAIL;
1842 break;
1843 }
1844 else if (*arg != ',' && *arg != ']')
1845 {
1846 EMSG2(_(e_intern2), "ex_let_vars()");
1847 return FAIL;
1848 }
1849 }
1850
1851 return OK;
1852}
1853
1854/*
1855 * Skip over assignable variable "var" or list of variables "[var, var]".
1856 * Used for ":let varvar = expr" and ":for varvar in expr".
1857 * For "[var, var]" increment "*var_count" for each variable.
1858 * for "[var, var; var]" set "semicolon".
1859 * Return NULL for an error.
1860 */
1861 static char_u *
1862skip_var_list(arg, var_count, semicolon)
1863 char_u *arg;
1864 int *var_count;
1865 int *semicolon;
1866{
1867 char_u *p, *s;
1868
1869 if (*arg == '[')
1870 {
1871 /* "[var, var]": find the matching ']'. */
1872 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001873 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 {
1875 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1876 s = skip_var_one(p);
1877 if (s == p)
1878 {
1879 EMSG2(_(e_invarg2), p);
1880 return NULL;
1881 }
1882 ++*var_count;
1883
1884 p = skipwhite(s);
1885 if (*p == ']')
1886 break;
1887 else if (*p == ';')
1888 {
1889 if (*semicolon == 1)
1890 {
1891 EMSG(_("Double ; in list of variables"));
1892 return NULL;
1893 }
1894 *semicolon = 1;
1895 }
1896 else if (*p != ',')
1897 {
1898 EMSG2(_(e_invarg2), p);
1899 return NULL;
1900 }
1901 }
1902 return p + 1;
1903 }
1904 else
1905 return skip_var_one(arg);
1906}
1907
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001909 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001910 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 static char_u *
1913skip_var_one(arg)
1914 char_u *arg;
1915{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001916 if (*arg == '@' && arg[1] != NUL)
1917 return arg + 2;
1918 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1919 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920}
1921
Bram Moolenaara7043832005-01-21 11:56:39 +00001922/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 * List variables for hashtab "ht" with prefix "prefix".
1924 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001927list_hashtable_vars(ht, prefix, empty)
1928 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001929 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001931{
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 hashitem_T *hi;
1933 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 int todo;
1935
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001936 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1938 {
1939 if (!HASHITEM_EMPTY(hi))
1940 {
1941 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 di = HI2DI(hi);
1943 if (empty || di->di_tv.v_type != VAR_STRING
1944 || di->di_tv.vval.v_string != NULL)
1945 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 }
1947 }
1948}
1949
1950/*
1951 * List global variables.
1952 */
1953 static void
1954list_glob_vars()
1955{
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001957}
1958
1959/*
1960 * List buffer variables.
1961 */
1962 static void
1963list_buf_vars()
1964{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 char_u numbuf[NUMBUFLEN];
1966
Bram Moolenaar33570922005-01-25 22:26:29 +00001967 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968
1969 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1970 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001971}
1972
1973/*
1974 * List window variables.
1975 */
1976 static void
1977list_win_vars()
1978{
Bram Moolenaar33570922005-01-25 22:26:29 +00001979 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001980}
1981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001982#ifdef FEAT_WINDOWS
1983/*
1984 * List tab page variables.
1985 */
1986 static void
1987list_tab_vars()
1988{
1989 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1990}
1991#endif
1992
Bram Moolenaara7043832005-01-21 11:56:39 +00001993/*
1994 * List Vim variables.
1995 */
1996 static void
1997list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998{
Bram Moolenaar33570922005-01-25 22:26:29 +00001999 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000}
2001
2002/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002003 * List script-local variables, if there is a script.
2004 */
2005 static void
2006list_script_vars()
2007{
2008 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
2009 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2010}
2011
2012/*
2013 * List function variables, if there is a function.
2014 */
2015 static void
2016list_func_vars()
2017{
2018 if (current_funccal != NULL)
2019 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2020 (char_u *)"l:", FALSE);
2021}
2022
2023/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 * List variables in "arg".
2025 */
2026 static char_u *
2027list_arg_vars(eap, arg)
2028 exarg_T *eap;
2029 char_u *arg;
2030{
2031 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 char_u *name_start;
2035 char_u *arg_subsc;
2036 char_u *tofree;
2037 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038
2039 while (!ends_excmd(*arg) && !got_int)
2040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002043 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002044 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2045 {
2046 emsg_severe = TRUE;
2047 EMSG(_(e_trailing));
2048 break;
2049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 /* get_name_len() takes care of expanding curly braces */
2054 name_start = name = arg;
2055 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2056 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 /* This is mainly to keep test 49 working: when expanding
2059 * curly braces fails overrule the exception error message. */
2060 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 emsg_severe = TRUE;
2063 EMSG2(_(e_invarg2), arg);
2064 break;
2065 }
2066 error = TRUE;
2067 }
2068 else
2069 {
2070 if (tofree != NULL)
2071 name = tofree;
2072 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 else
2075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 /* handle d.key, l[idx], f(expr) */
2077 arg_subsc = arg;
2078 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086 case 'g': list_glob_vars(); break;
2087 case 'b': list_buf_vars(); break;
2088 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002089#ifdef FEAT_WINDOWS
2090 case 't': list_tab_vars(); break;
2091#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002092 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002093 case 's': list_script_vars(); break;
2094 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095 default:
2096 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 {
2101 char_u numbuf[NUMBUFLEN];
2102 char_u *tf;
2103 int c;
2104 char_u *s;
2105
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002106 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 c = *arg;
2108 *arg = NUL;
2109 list_one_var_a((char_u *)"",
2110 arg == arg_subsc ? name : name_start,
2111 tv.v_type, s == NULL ? (char_u *)"" : s);
2112 *arg = c;
2113 vim_free(tf);
2114 }
2115 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 }
2118 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119
2120 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 }
2125
2126 return arg;
2127}
2128
2129/*
2130 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2131 * Returns a pointer to the char just after the var name.
2132 * Returns NULL if there is an error.
2133 */
2134 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002140 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141{
2142 int c1;
2143 char_u *name;
2144 char_u *p;
2145 char_u *arg_end = NULL;
2146 int len;
2147 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149
2150 /*
2151 * ":let $VAR = expr": Set environment variable.
2152 */
2153 if (*arg == '$')
2154 {
2155 /* Find the end of the name. */
2156 ++arg;
2157 name = arg;
2158 len = get_env_len(&arg);
2159 if (len == 0)
2160 EMSG2(_(e_invarg2), name - 1);
2161 else
2162 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 if (op != NULL && (*op == '+' || *op == '-'))
2164 EMSG2(_(e_letwrong), op);
2165 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 EMSG(_(e_letunexp));
2168 else
2169 {
2170 c1 = name[len];
2171 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 p = get_tv_string_chk(tv);
2173 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 {
2175 int mustfree = FALSE;
2176 char_u *s = vim_getenv(name, &mustfree);
2177
2178 if (s != NULL)
2179 {
2180 p = tofree = concat_str(s, p);
2181 if (mustfree)
2182 vim_free(s);
2183 }
2184 }
2185 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002186 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002188 if (STRICMP(name, "HOME") == 0)
2189 init_homedir();
2190 else if (didset_vim && STRICMP(name, "VIM") == 0)
2191 didset_vim = FALSE;
2192 else if (didset_vimruntime
2193 && STRICMP(name, "VIMRUNTIME") == 0)
2194 didset_vimruntime = FALSE;
2195 arg_end = arg;
2196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
2200 }
2201 }
2202
2203 /*
2204 * ":let &option = expr": Set option value.
2205 * ":let &l:option = expr": Set local option value.
2206 * ":let &g:option = expr": Set global option value.
2207 */
2208 else if (*arg == '&')
2209 {
2210 /* Find the end of the name. */
2211 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002212 if (p == NULL || (endchars != NULL
2213 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 EMSG(_(e_letunexp));
2215 else
2216 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 long n;
2218 int opt_type;
2219 long numval;
2220 char_u *stringval = NULL;
2221 char_u *s;
2222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 c1 = *p;
2224 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225
2226 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002227 s = get_tv_string_chk(tv); /* != NULL if number or string */
2228 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 {
2230 opt_type = get_option_value(arg, &numval,
2231 &stringval, opt_flags);
2232 if ((opt_type == 1 && *op == '.')
2233 || (opt_type == 0 && *op != '.'))
2234 EMSG2(_(e_letwrong), op);
2235 else
2236 {
2237 if (opt_type == 1) /* number */
2238 {
2239 if (*op == '+')
2240 n = numval + n;
2241 else
2242 n = numval - n;
2243 }
2244 else if (opt_type == 0 && stringval != NULL) /* string */
2245 {
2246 s = concat_str(stringval, s);
2247 vim_free(stringval);
2248 stringval = s;
2249 }
2250 }
2251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 if (s != NULL)
2253 {
2254 set_option_value(arg, n, s, opt_flags);
2255 arg_end = p;
2256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 }
2260 }
2261
2262 /*
2263 * ":let @r = expr": Set register contents.
2264 */
2265 else if (*arg == '@')
2266 {
2267 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 if (op != NULL && (*op == '+' || *op == '-'))
2269 EMSG2(_(e_letwrong), op);
2270 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 EMSG(_(e_letunexp));
2273 else
2274 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002275 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 char_u *s;
2277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 p = get_tv_string_chk(tv);
2279 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002281 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282 if (s != NULL)
2283 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002284 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002285 vim_free(s);
2286 }
2287 }
2288 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002289 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002290 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002291 arg_end = arg + 1;
2292 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002293 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
2295 }
2296
2297 /*
2298 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002301 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002305 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2309 EMSG(_(e_letunexp));
2310 else
2311 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 arg_end = p;
2314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318
2319 else
2320 EMSG2(_(e_invarg2), arg);
2321
2322 return arg_end;
2323}
2324
2325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2327 */
2328 static int
2329check_changedtick(arg)
2330 char_u *arg;
2331{
2332 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2333 {
2334 EMSG2(_(e_readonlyvar), arg);
2335 return TRUE;
2336 }
2337 return FALSE;
2338}
2339
2340/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 * Get an lval: variable, Dict item or List item that can be assigned a value
2342 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2343 * "name.key", "name.key[expr]" etc.
2344 * Indexing only works if "name" is an existing List or Dictionary.
2345 * "name" points to the start of the name.
2346 * If "rettv" is not NULL it points to the value to be assigned.
2347 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2348 * wrong; must end in space or cmd separator.
2349 *
2350 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002351 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 */
2354 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002355get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002357 typval_T *rettv;
2358 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 int unlet;
2360 int skip;
2361 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002362 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 char_u *expr_start, *expr_end;
2366 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002367 dictitem_T *v;
2368 typval_T var1;
2369 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002371 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002374 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002377 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378
2379 if (skip)
2380 {
2381 /* When skipping just find the end of the name. */
2382 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002383 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 }
2385
2386 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002387 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 if (expr_start != NULL)
2389 {
2390 /* Don't expand the name when we already know there is an error. */
2391 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2392 && *p != '[' && *p != '.')
2393 {
2394 EMSG(_(e_trailing));
2395 return NULL;
2396 }
2397
2398 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2399 if (lp->ll_exp_name == NULL)
2400 {
2401 /* Report an invalid expression in braces, unless the
2402 * expression evaluation has been cancelled due to an
2403 * aborting error, an interrupt, or an exception. */
2404 if (!aborting() && !quiet)
2405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002406 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 EMSG2(_(e_invarg2), name);
2408 return NULL;
2409 }
2410 }
2411 lp->ll_name = lp->ll_exp_name;
2412 }
2413 else
2414 lp->ll_name = name;
2415
2416 /* Without [idx] or .key we are done. */
2417 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2418 return p;
2419
2420 cc = *p;
2421 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002422 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (v == NULL && !quiet)
2424 EMSG2(_(e_undefvar), lp->ll_name);
2425 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 if (v == NULL)
2427 return NULL;
2428
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 /*
2430 * Loop until no more [idx] or .key is following.
2431 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002432 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2436 && !(lp->ll_tv->v_type == VAR_DICT
2437 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (!quiet)
2440 EMSG(_("E689: Can only index a List or Dictionary"));
2441 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002444 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (!quiet)
2446 EMSG(_("E708: [:] must come last"));
2447 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002449
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 len = -1;
2451 if (*p == '.')
2452 {
2453 key = p + 1;
2454 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2455 ;
2456 if (len == 0)
2457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!quiet)
2459 EMSG(_(e_emptykey));
2460 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 }
2462 p = key + len;
2463 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002464 else
2465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002467 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 if (*p == ':')
2469 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002470 else
2471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 empty1 = FALSE;
2473 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 if (get_tv_string_chk(&var1) == NULL)
2476 {
2477 /* not a number or string */
2478 clear_tv(&var1);
2479 return NULL;
2480 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 }
2482
2483 /* Optionally get the second index [ :expr]. */
2484 if (*p == ':')
2485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 if (!empty1)
2491 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (rettv != NULL && (rettv->v_type != VAR_LIST
2495 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (!quiet)
2498 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (!empty1)
2500 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 }
2503 p = skipwhite(p + 1);
2504 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 else
2507 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2510 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 if (!empty1)
2512 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 if (get_tv_string_chk(&var2) == NULL)
2516 {
2517 /* not a number or string */
2518 if (!empty1)
2519 clear_tv(&var1);
2520 clear_tv(&var2);
2521 return NULL;
2522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002525 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002528
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (!quiet)
2532 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 if (!empty1)
2534 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 }
2539
2540 /* Skip to past ']'. */
2541 ++p;
2542 }
2543
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 {
2546 if (len == -1)
2547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 if (*key == NUL)
2551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (!quiet)
2553 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 }
2557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 lp->ll_list = NULL;
2559 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002560 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002563 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 if (len == -1)
2569 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 }
2572 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 if (len == -1)
2577 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 p = NULL;
2580 break;
2581 }
2582 if (len == -1)
2583 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 }
2586 else
2587 {
2588 /*
2589 * Get the number and item for the only or first index of the List.
2590 */
2591 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 else
2594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 clear_tv(&var1);
2597 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002598 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 lp->ll_list = lp->ll_tv->vval.v_list;
2600 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2601 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002603 if (lp->ll_n1 < 0)
2604 {
2605 lp->ll_n1 = 0;
2606 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2607 }
2608 }
2609 if (lp->ll_li == NULL)
2610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 }
2615
2616 /*
2617 * May need to find the item or absolute index for the second
2618 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 * When no index given: "lp->ll_empty2" is TRUE.
2620 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002624 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 }
2633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2635 if (lp->ll_n1 < 0)
2636 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2637 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002639 }
2640
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
2644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 return p;
2646}
2647
2648/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 */
2651 static void
2652clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654{
2655 vim_free(lp->ll_exp_name);
2656 vim_free(lp->ll_newkey);
2657}
2658
2659/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002660 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 */
2664 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002665set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002666 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002668 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671{
2672 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 listitem_T *ri;
2674 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675
2676 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 cc = *endp;
2681 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 if (op != NULL && *op != '=')
2683 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002684 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002686 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002687 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002688 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 {
2690 if (tv_op(&tv, rettv, op) == OK)
2691 set_var(lp->ll_name, &tv, FALSE);
2692 clear_tv(&tv);
2693 }
2694 }
2695 else
2696 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002700 else if (tv_check_lock(lp->ll_newkey == NULL
2701 ? lp->ll_tv->v_lock
2702 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2703 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 else if (lp->ll_range)
2705 {
2706 /*
2707 * Assign the List values to the list items.
2708 */
2709 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711 if (op != NULL && *op != '=')
2712 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2713 else
2714 {
2715 clear_tv(&lp->ll_li->li_tv);
2716 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 ri = ri->li_next;
2719 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2720 break;
2721 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002724 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 ri = NULL;
2727 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_li = lp->ll_li->li_next;
2731 ++lp->ll_n1;
2732 }
2733 if (ri != NULL)
2734 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 else if (lp->ll_empty2
2736 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 : lp->ll_n1 != lp->ll_n2)
2738 EMSG(_("E711: List value has not enough items"));
2739 }
2740 else
2741 {
2742 /*
2743 * Assign to a List or Dictionary item.
2744 */
2745 if (lp->ll_newkey != NULL)
2746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 if (op != NULL && *op != '=')
2748 {
2749 EMSG2(_(e_letwrong), op);
2750 return;
2751 }
2752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (di == NULL)
2756 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002757 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2758 {
2759 vim_free(di);
2760 return;
2761 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002763 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 else if (op != NULL && *op != '=')
2765 {
2766 tv_op(lp->ll_tv, rettv, op);
2767 return;
2768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 /*
2773 * Assign the value to the variable or list item.
2774 */
2775 if (copy)
2776 copy_tv(rettv, lp->ll_tv);
2777 else
2778 {
2779 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002780 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002782 }
2783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002784}
2785
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2788 * Returns OK or FAIL.
2789 */
2790 static int
2791tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 typval_T *tv1;
2793 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 char_u *op;
2795{
2796 long n;
2797 char_u numbuf[NUMBUFLEN];
2798 char_u *s;
2799
2800 /* Can't do anything with a Funcref or a Dict on the right. */
2801 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2802 {
2803 switch (tv1->v_type)
2804 {
2805 case VAR_DICT:
2806 case VAR_FUNC:
2807 break;
2808
2809 case VAR_LIST:
2810 if (*op != '+' || tv2->v_type != VAR_LIST)
2811 break;
2812 /* List += List */
2813 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2814 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2815 return OK;
2816
2817 case VAR_NUMBER:
2818 case VAR_STRING:
2819 if (tv2->v_type == VAR_LIST)
2820 break;
2821 if (*op == '+' || *op == '-')
2822 {
2823 /* nr += nr or nr -= nr*/
2824 n = get_tv_number(tv1);
2825 if (*op == '+')
2826 n += get_tv_number(tv2);
2827 else
2828 n -= get_tv_number(tv2);
2829 clear_tv(tv1);
2830 tv1->v_type = VAR_NUMBER;
2831 tv1->vval.v_number = n;
2832 }
2833 else
2834 {
2835 /* str .= str */
2836 s = get_tv_string(tv1);
2837 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2838 clear_tv(tv1);
2839 tv1->v_type = VAR_STRING;
2840 tv1->vval.v_string = s;
2841 }
2842 return OK;
2843 }
2844 }
2845
2846 EMSG2(_(e_letwrong), op);
2847 return FAIL;
2848}
2849
2850/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851 * Add a watcher to a list.
2852 */
2853 static void
2854list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002855 list_T *l;
2856 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857{
2858 lw->lw_next = l->lv_watch;
2859 l->lv_watch = lw;
2860}
2861
2862/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002863 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864 * No warning when it isn't found...
2865 */
2866 static void
2867list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 list_T *l;
2869 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002870{
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872
2873 lwp = &l->lv_watch;
2874 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2875 {
2876 if (lw == lwrem)
2877 {
2878 *lwp = lw->lw_next;
2879 break;
2880 }
2881 lwp = &lw->lw_next;
2882 }
2883}
2884
2885/*
2886 * Just before removing an item from a list: advance watchers to the next
2887 * item.
2888 */
2889 static void
2890list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 list_T *l;
2892 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
2896 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2897 if (lw->lw_item == item)
2898 lw->lw_item = item->li_next;
2899}
2900
2901/*
2902 * Evaluate the expression used in a ":for var in expr" command.
2903 * "arg" points to "var".
2904 * Set "*errp" to TRUE for an error, FALSE otherwise;
2905 * Return a pointer that holds the info. Null when there is an error.
2906 */
2907 void *
2908eval_for_line(arg, errp, nextcmdp, skip)
2909 char_u *arg;
2910 int *errp;
2911 char_u **nextcmdp;
2912 int skip;
2913{
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T tv;
2917 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918
2919 *errp = TRUE; /* default: there is an error */
2920
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 if (fi == NULL)
2923 return NULL;
2924
2925 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2926 if (expr == NULL)
2927 return fi;
2928
2929 expr = skipwhite(expr);
2930 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2931 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002932 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933 return fi;
2934 }
2935
2936 if (skip)
2937 ++emsg_skip;
2938 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2939 {
2940 *errp = FALSE;
2941 if (!skip)
2942 {
2943 l = tv.vval.v_list;
2944 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002945 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002947 clear_tv(&tv);
2948 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002949 else
2950 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002951 /* No need to increment the refcount, it's already set for the
2952 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002953 fi->fi_list = l;
2954 list_add_watch(l, &fi->fi_lw);
2955 fi->fi_lw.lw_item = l->lv_first;
2956 }
2957 }
2958 }
2959 if (skip)
2960 --emsg_skip;
2961
2962 return fi;
2963}
2964
2965/*
2966 * Use the first item in a ":for" list. Advance to the next.
2967 * Assign the values to the variable (list). "arg" points to the first one.
2968 * Return TRUE when a valid item was found, FALSE when at end of list or
2969 * something wrong.
2970 */
2971 int
2972next_for_item(fi_void, arg)
2973 void *fi_void;
2974 char_u *arg;
2975{
Bram Moolenaar33570922005-01-25 22:26:29 +00002976 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002978 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979
2980 item = fi->fi_lw.lw_item;
2981 if (item == NULL)
2982 result = FALSE;
2983 else
2984 {
2985 fi->fi_lw.lw_item = item->li_next;
2986 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2987 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2988 }
2989 return result;
2990}
2991
2992/*
2993 * Free the structure used to store info used by ":for".
2994 */
2995 void
2996free_for_info(fi_void)
2997 void *fi_void;
2998{
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003001 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003002 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003004 list_unref(fi->fi_list);
3005 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 vim_free(fi);
3007}
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3010
3011 void
3012set_context_for_expression(xp, arg, cmdidx)
3013 expand_T *xp;
3014 char_u *arg;
3015 cmdidx_T cmdidx;
3016{
3017 int got_eq = FALSE;
3018 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021 if (cmdidx == CMD_let)
3022 {
3023 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003024 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025 {
3026 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003027 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028 {
3029 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003030 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003031 if (vim_iswhite(*p))
3032 break;
3033 }
3034 return;
3035 }
3036 }
3037 else
3038 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3039 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 while ((xp->xp_pattern = vim_strpbrk(arg,
3041 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3042 {
3043 c = *xp->xp_pattern;
3044 if (c == '&')
3045 {
3046 c = xp->xp_pattern[1];
3047 if (c == '&')
3048 {
3049 ++xp->xp_pattern;
3050 xp->xp_context = cmdidx != CMD_let || got_eq
3051 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3052 }
3053 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003056 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3057 xp->xp_pattern += 2;
3058
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
3061 else if (c == '$')
3062 {
3063 /* environment variable */
3064 xp->xp_context = EXPAND_ENV_VARS;
3065 }
3066 else if (c == '=')
3067 {
3068 got_eq = TRUE;
3069 xp->xp_context = EXPAND_EXPRESSION;
3070 }
3071 else if (c == '<'
3072 && xp->xp_context == EXPAND_FUNCTIONS
3073 && vim_strchr(xp->xp_pattern, '(') == NULL)
3074 {
3075 /* Function name can start with "<SNR>" */
3076 break;
3077 }
3078 else if (cmdidx != CMD_let || got_eq)
3079 {
3080 if (c == '"') /* string */
3081 {
3082 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3083 if (c == '\\' && xp->xp_pattern[1] != NUL)
3084 ++xp->xp_pattern;
3085 xp->xp_context = EXPAND_NOTHING;
3086 }
3087 else if (c == '\'') /* literal string */
3088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003089 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3091 /* skip */ ;
3092 xp->xp_context = EXPAND_NOTHING;
3093 }
3094 else if (c == '|')
3095 {
3096 if (xp->xp_pattern[1] == '|')
3097 {
3098 ++xp->xp_pattern;
3099 xp->xp_context = EXPAND_EXPRESSION;
3100 }
3101 else
3102 xp->xp_context = EXPAND_COMMANDS;
3103 }
3104 else
3105 xp->xp_context = EXPAND_EXPRESSION;
3106 }
3107 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108 /* Doesn't look like something valid, expand as an expression
3109 * anyway. */
3110 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 arg = xp->xp_pattern;
3112 if (*arg != NUL)
3113 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3114 /* skip */ ;
3115 }
3116 xp->xp_pattern = arg;
3117}
3118
3119#endif /* FEAT_CMDL_COMPL */
3120
3121/*
3122 * ":1,25call func(arg1, arg2)" function call.
3123 */
3124 void
3125ex_call(eap)
3126 exarg_T *eap;
3127{
3128 char_u *arg = eap->arg;
3129 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003133 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 linenr_T lnum;
3135 int doesrange;
3136 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003139 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003140 if (fudi.fd_newkey != NULL)
3141 {
3142 /* Still need to give an error message for missing key. */
3143 EMSG2(_(e_dictkey), fudi.fd_newkey);
3144 vim_free(fudi.fd_newkey);
3145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 if (tofree == NULL)
3147 return;
3148
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003149 /* Increase refcount on dictionary, it could get deleted when evaluating
3150 * the arguments. */
3151 if (fudi.fd_dict != NULL)
3152 ++fudi.fd_dict->dv_refcount;
3153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003155 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
Bram Moolenaar532c7802005-01-27 14:44:31 +00003158 /* Skip white space to allow ":call func ()". Not good, but required for
3159 * backward compatibility. */
3160 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003161 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 if (*startarg != '(')
3164 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003165 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 goto end;
3167 }
3168
3169 /*
3170 * When skipping, evaluate the function once, to find the end of the
3171 * arguments.
3172 * When the function takes a range, this is discovered after the first
3173 * call, and the loop is broken.
3174 */
3175 if (eap->skip)
3176 {
3177 ++emsg_skip;
3178 lnum = eap->line2; /* do it once, also with an invalid range */
3179 }
3180 else
3181 lnum = eap->line1;
3182 for ( ; lnum <= eap->line2; ++lnum)
3183 {
3184 if (!eap->skip && eap->addr_count > 0)
3185 {
3186 curwin->w_cursor.lnum = lnum;
3187 curwin->w_cursor.col = 0;
3188 }
3189 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003190 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003191 eap->line1, eap->line2, &doesrange,
3192 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 failed = TRUE;
3195 break;
3196 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003197
3198 /* Handle a function returning a Funcref, Dictionary or List. */
3199 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3200 {
3201 failed = TRUE;
3202 break;
3203 }
3204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (doesrange || eap->skip)
3207 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003210 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003211 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003212 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (aborting())
3214 break;
3215 }
3216 if (eap->skip)
3217 --emsg_skip;
3218
3219 if (!failed)
3220 {
3221 /* Check for trailing illegal characters and a following command. */
3222 if (!ends_excmd(*arg))
3223 {
3224 emsg_severe = TRUE;
3225 EMSG(_(e_trailing));
3226 }
3227 else
3228 eap->nextcmd = check_nextcmd(arg);
3229 }
3230
3231end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003232 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003233 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234}
3235
3236/*
3237 * ":unlet[!] var1 ... " command.
3238 */
3239 void
3240ex_unlet(eap)
3241 exarg_T *eap;
3242{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003243 ex_unletlock(eap, eap->arg, 0);
3244}
3245
3246/*
3247 * ":lockvar" and ":unlockvar" commands
3248 */
3249 void
3250ex_lockvar(eap)
3251 exarg_T *eap;
3252{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 int deep = 2;
3255
3256 if (eap->forceit)
3257 deep = -1;
3258 else if (vim_isdigit(*arg))
3259 {
3260 deep = getdigits(&arg);
3261 arg = skipwhite(arg);
3262 }
3263
3264 ex_unletlock(eap, arg, deep);
3265}
3266
3267/*
3268 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3269 */
3270 static void
3271ex_unletlock(eap, argstart, deep)
3272 exarg_T *eap;
3273 char_u *argstart;
3274 int deep;
3275{
3276 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 do
3282 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003283 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003284 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3285 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003286 if (lv.ll_name == NULL)
3287 error = TRUE; /* error but continue parsing */
3288 if (name_end == NULL || (!vim_iswhite(*name_end)
3289 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003291 if (name_end != NULL)
3292 {
3293 emsg_severe = TRUE;
3294 EMSG(_(e_trailing));
3295 }
3296 if (!(eap->skip || error))
3297 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 break;
3299 }
3300
3301 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302 {
3303 if (eap->cmdidx == CMD_unlet)
3304 {
3305 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3306 error = TRUE;
3307 }
3308 else
3309 {
3310 if (do_lock_var(&lv, name_end, deep,
3311 eap->cmdidx == CMD_lockvar) == FAIL)
3312 error = TRUE;
3313 }
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 if (!eap->skip)
3317 clear_lval(&lv);
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 arg = skipwhite(name_end);
3320 } while (!ends_excmd(*arg));
3321
3322 eap->nextcmd = check_nextcmd(arg);
3323}
3324
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003326do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003328 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 int forceit;
3330{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003331 int ret = OK;
3332 int cc;
3333
3334 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003336 cc = *name_end;
3337 *name_end = NUL;
3338
3339 /* Normal name or expanded name. */
3340 if (check_changedtick(lp->ll_name))
3341 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003342 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003344 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003346 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3347 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003348 else if (lp->ll_range)
3349 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351
3352 /* Delete a range of List items. */
3353 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3354 {
3355 li = lp->ll_li->li_next;
3356 listitem_remove(lp->ll_list, lp->ll_li);
3357 lp->ll_li = li;
3358 ++lp->ll_n1;
3359 }
3360 }
3361 else
3362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003363 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003364 /* unlet a List item. */
3365 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003366 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003368 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 }
3370
3371 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
3375 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003376 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 */
3378 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003379do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003381 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 hashtab_T *ht;
3384 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003385 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 ht = find_var_ht(name, &varname);
3388 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003390 hi = hash_find(ht, varname);
3391 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003392 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003393 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3394 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003395 if (var_check_ro(HI2DI(hi)->di_flags, name))
3396 return FAIL;
3397 delete_var(ht, hi);
3398 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 if (forceit)
3402 return OK;
3403 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 return FAIL;
3405}
3406
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407/*
3408 * Lock or unlock variable indicated by "lp".
3409 * "deep" is the levels to go (-1 for unlimited);
3410 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3411 */
3412 static int
3413do_lock_var(lp, name_end, deep, lock)
3414 lval_T *lp;
3415 char_u *name_end;
3416 int deep;
3417 int lock;
3418{
3419 int ret = OK;
3420 int cc;
3421 dictitem_T *di;
3422
3423 if (deep == 0) /* nothing to do */
3424 return OK;
3425
3426 if (lp->ll_tv == NULL)
3427 {
3428 cc = *name_end;
3429 *name_end = NUL;
3430
3431 /* Normal name or expanded name. */
3432 if (check_changedtick(lp->ll_name))
3433 ret = FAIL;
3434 else
3435 {
3436 di = find_var(lp->ll_name, NULL);
3437 if (di == NULL)
3438 ret = FAIL;
3439 else
3440 {
3441 if (lock)
3442 di->di_flags |= DI_FLAGS_LOCK;
3443 else
3444 di->di_flags &= ~DI_FLAGS_LOCK;
3445 item_lock(&di->di_tv, deep, lock);
3446 }
3447 }
3448 *name_end = cc;
3449 }
3450 else if (lp->ll_range)
3451 {
3452 listitem_T *li = lp->ll_li;
3453
3454 /* (un)lock a range of List items. */
3455 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3456 {
3457 item_lock(&li->li_tv, deep, lock);
3458 li = li->li_next;
3459 ++lp->ll_n1;
3460 }
3461 }
3462 else if (lp->ll_list != NULL)
3463 /* (un)lock a List item. */
3464 item_lock(&lp->ll_li->li_tv, deep, lock);
3465 else
3466 /* un(lock) a Dictionary item. */
3467 item_lock(&lp->ll_di->di_tv, deep, lock);
3468
3469 return ret;
3470}
3471
3472/*
3473 * Lock or unlock an item. "deep" is nr of levels to go.
3474 */
3475 static void
3476item_lock(tv, deep, lock)
3477 typval_T *tv;
3478 int deep;
3479 int lock;
3480{
3481 static int recurse = 0;
3482 list_T *l;
3483 listitem_T *li;
3484 dict_T *d;
3485 hashitem_T *hi;
3486 int todo;
3487
3488 if (recurse >= DICT_MAXNEST)
3489 {
3490 EMSG(_("E743: variable nested too deep for (un)lock"));
3491 return;
3492 }
3493 if (deep == 0)
3494 return;
3495 ++recurse;
3496
3497 /* lock/unlock the item itself */
3498 if (lock)
3499 tv->v_lock |= VAR_LOCKED;
3500 else
3501 tv->v_lock &= ~VAR_LOCKED;
3502
3503 switch (tv->v_type)
3504 {
3505 case VAR_LIST:
3506 if ((l = tv->vval.v_list) != NULL)
3507 {
3508 if (lock)
3509 l->lv_lock |= VAR_LOCKED;
3510 else
3511 l->lv_lock &= ~VAR_LOCKED;
3512 if (deep < 0 || deep > 1)
3513 /* recursive: lock/unlock the items the List contains */
3514 for (li = l->lv_first; li != NULL; li = li->li_next)
3515 item_lock(&li->li_tv, deep - 1, lock);
3516 }
3517 break;
3518 case VAR_DICT:
3519 if ((d = tv->vval.v_dict) != NULL)
3520 {
3521 if (lock)
3522 d->dv_lock |= VAR_LOCKED;
3523 else
3524 d->dv_lock &= ~VAR_LOCKED;
3525 if (deep < 0 || deep > 1)
3526 {
3527 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003529 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3530 {
3531 if (!HASHITEM_EMPTY(hi))
3532 {
3533 --todo;
3534 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3535 }
3536 }
3537 }
3538 }
3539 }
3540 --recurse;
3541}
3542
Bram Moolenaara40058a2005-07-11 22:42:07 +00003543/*
3544 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3545 * it refers to a List or Dictionary that is locked.
3546 */
3547 static int
3548tv_islocked(tv)
3549 typval_T *tv;
3550{
3551 return (tv->v_lock & VAR_LOCKED)
3552 || (tv->v_type == VAR_LIST
3553 && tv->vval.v_list != NULL
3554 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3555 || (tv->v_type == VAR_DICT
3556 && tv->vval.v_dict != NULL
3557 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3558}
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3561/*
3562 * Delete all "menutrans_" variables.
3563 */
3564 void
3565del_menutrans_vars()
3566{
Bram Moolenaar33570922005-01-25 22:26:29 +00003567 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003568 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar33570922005-01-25 22:26:29 +00003570 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003571 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003573 {
3574 if (!HASHITEM_EMPTY(hi))
3575 {
3576 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003577 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3578 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003579 }
3580 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003581 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582}
3583#endif
3584
3585#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3586
3587/*
3588 * Local string buffer for the next two functions to store a variable name
3589 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3590 * get_user_var_name().
3591 */
3592
3593static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3594
3595static char_u *varnamebuf = NULL;
3596static int varnamebuflen = 0;
3597
3598/*
3599 * Function to concatenate a prefix and a variable name.
3600 */
3601 static char_u *
3602cat_prefix_varname(prefix, name)
3603 int prefix;
3604 char_u *name;
3605{
3606 int len;
3607
3608 len = (int)STRLEN(name) + 3;
3609 if (len > varnamebuflen)
3610 {
3611 vim_free(varnamebuf);
3612 len += 10; /* some additional space */
3613 varnamebuf = alloc(len);
3614 if (varnamebuf == NULL)
3615 {
3616 varnamebuflen = 0;
3617 return NULL;
3618 }
3619 varnamebuflen = len;
3620 }
3621 *varnamebuf = prefix;
3622 varnamebuf[1] = ':';
3623 STRCPY(varnamebuf + 2, name);
3624 return varnamebuf;
3625}
3626
3627/*
3628 * Function given to ExpandGeneric() to obtain the list of user defined
3629 * (global/buffer/window/built-in) variable names.
3630 */
3631/*ARGSUSED*/
3632 char_u *
3633get_user_var_name(xp, idx)
3634 expand_T *xp;
3635 int idx;
3636{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003637 static long_u gdone;
3638 static long_u bdone;
3639 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003640#ifdef FEAT_WINDOWS
3641 static long_u tdone;
3642#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003643 static int vidx;
3644 static hashitem_T *hi;
3645 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003648 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003650#ifdef FEAT_WINDOWS
3651 tdone = 0;
3652#endif
3653 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003654
3655 /* Global variables */
3656 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003660 else
3661 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003662 while (HASHITEM_EMPTY(hi))
3663 ++hi;
3664 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3665 return cat_prefix_varname('g', hi->hi_key);
3666 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003668
3669 /* b: variables */
3670 ht = &curbuf->b_vars.dv_hashtab;
3671 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003675 else
3676 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 while (HASHITEM_EMPTY(hi))
3678 ++hi;
3679 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 return (char_u *)"b:changedtick";
3685 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003686
3687 /* w: variables */
3688 ht = &curwin->w_vars.dv_hashtab;
3689 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003691 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003692 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003693 else
3694 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003695 while (HASHITEM_EMPTY(hi))
3696 ++hi;
3697 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003699
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003700#ifdef FEAT_WINDOWS
3701 /* t: variables */
3702 ht = &curtab->tp_vars.dv_hashtab;
3703 if (tdone < ht->ht_used)
3704 {
3705 if (tdone++ == 0)
3706 hi = ht->ht_array;
3707 else
3708 ++hi;
3709 while (HASHITEM_EMPTY(hi))
3710 ++hi;
3711 return cat_prefix_varname('t', hi->hi_key);
3712 }
3713#endif
3714
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 /* v: variables */
3716 if (vidx < VV_LEN)
3717 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 vim_free(varnamebuf);
3720 varnamebuf = NULL;
3721 varnamebuflen = 0;
3722 return NULL;
3723}
3724
3725#endif /* FEAT_CMDL_COMPL */
3726
3727/*
3728 * types for expressions.
3729 */
3730typedef enum
3731{
3732 TYPE_UNKNOWN = 0
3733 , TYPE_EQUAL /* == */
3734 , TYPE_NEQUAL /* != */
3735 , TYPE_GREATER /* > */
3736 , TYPE_GEQUAL /* >= */
3737 , TYPE_SMALLER /* < */
3738 , TYPE_SEQUAL /* <= */
3739 , TYPE_MATCH /* =~ */
3740 , TYPE_NOMATCH /* !~ */
3741} exptype_T;
3742
3743/*
3744 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003745 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3747 */
3748
3749/*
3750 * Handle zero level expression.
3751 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003753 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 * Return OK or FAIL.
3755 */
3756 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 char_u **nextcmd;
3761 int evaluate;
3762{
3763 int ret;
3764 char_u *p;
3765
3766 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (ret == FAIL || !ends_excmd(*p))
3769 {
3770 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /*
3773 * Report the invalid expression unless the expression evaluation has
3774 * been cancelled due to an aborting error, an interrupt, or an
3775 * exception.
3776 */
3777 if (!aborting())
3778 EMSG2(_(e_invexpr2), arg);
3779 ret = FAIL;
3780 }
3781 if (nextcmd != NULL)
3782 *nextcmd = check_nextcmd(p);
3783
3784 return ret;
3785}
3786
3787/*
3788 * Handle top level expression:
3789 * expr1 ? expr0 : expr0
3790 *
3791 * "arg" must point to the first non-white of the expression.
3792 * "arg" is advanced to the next non-white after the recognized expression.
3793 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003794 * Note: "rettv.v_lock" is not set.
3795 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 * Return OK or FAIL.
3797 */
3798 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int evaluate;
3803{
3804 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
3807 /*
3808 * Get the first variable.
3809 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 return FAIL;
3812
3813 if ((*arg)[0] == '?')
3814 {
3815 result = FALSE;
3816 if (evaluate)
3817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003818 int error = FALSE;
3819
3820 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003823 if (error)
3824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
3826
3827 /*
3828 * Get the second variable.
3829 */
3830 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833
3834 /*
3835 * Check for the ":".
3836 */
3837 if ((*arg)[0] != ':')
3838 {
3839 EMSG(_("E109: Missing ':' after '?'"));
3840 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 return FAIL;
3843 }
3844
3845 /*
3846 * Get the third variable.
3847 */
3848 *arg = skipwhite(*arg + 1);
3849 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3850 {
3851 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return FAIL;
3854 }
3855 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858
3859 return OK;
3860}
3861
3862/*
3863 * Handle first level expression:
3864 * expr2 || expr2 || expr2 logical OR
3865 *
3866 * "arg" must point to the first non-white of the expression.
3867 * "arg" is advanced to the next non-white after the recognized expression.
3868 *
3869 * Return OK or FAIL.
3870 */
3871 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003872eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 int evaluate;
3876{
Bram Moolenaar33570922005-01-25 22:26:29 +00003877 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 long result;
3879 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
3882 /*
3883 * Get the first variable.
3884 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return FAIL;
3887
3888 /*
3889 * Repeat until there is no following "||".
3890 */
3891 first = TRUE;
3892 result = FALSE;
3893 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3894 {
3895 if (evaluate && first)
3896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003897 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (error)
3901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 first = FALSE;
3903 }
3904
3905 /*
3906 * Get the second variable.
3907 */
3908 *arg = skipwhite(*arg + 2);
3909 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3910 return FAIL;
3911
3912 /*
3913 * Compute the result.
3914 */
3915 if (evaluate && !result)
3916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003917 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003920 if (error)
3921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 if (evaluate)
3924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 rettv->v_type = VAR_NUMBER;
3926 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 }
3929
3930 return OK;
3931}
3932
3933/*
3934 * Handle second level expression:
3935 * expr3 && expr3 && expr3 logical AND
3936 *
3937 * "arg" must point to the first non-white of the expression.
3938 * "arg" is advanced to the next non-white after the recognized expression.
3939 *
3940 * Return OK or FAIL.
3941 */
3942 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 int evaluate;
3947{
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 long result;
3950 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003951 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 /*
3954 * Get the first variable.
3955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return FAIL;
3958
3959 /*
3960 * Repeat until there is no following "&&".
3961 */
3962 first = TRUE;
3963 result = TRUE;
3964 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3965 {
3966 if (evaluate && first)
3967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003968 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003971 if (error)
3972 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 first = FALSE;
3974 }
3975
3976 /*
3977 * Get the second variable.
3978 */
3979 *arg = skipwhite(*arg + 2);
3980 if (eval4(arg, &var2, evaluate && result) == FAIL)
3981 return FAIL;
3982
3983 /*
3984 * Compute the result.
3985 */
3986 if (evaluate && result)
3987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003988 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 if (error)
3992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 if (evaluate)
3995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 rettv->v_type = VAR_NUMBER;
3997 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
4000
4001 return OK;
4002}
4003
4004/*
4005 * Handle third level expression:
4006 * var1 == var2
4007 * var1 =~ var2
4008 * var1 != var2
4009 * var1 !~ var2
4010 * var1 > var2
4011 * var1 >= var2
4012 * var1 < var2
4013 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004014 * var1 is var2
4015 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *
4017 * "arg" must point to the first non-white of the expression.
4018 * "arg" is advanced to the next non-white after the recognized expression.
4019 *
4020 * Return OK or FAIL.
4021 */
4022 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 int evaluate;
4027{
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *p;
4030 int i;
4031 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004032 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 int len = 2;
4034 long n1, n2;
4035 char_u *s1, *s2;
4036 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4037 regmatch_T regmatch;
4038 int ic;
4039 char_u *save_cpo;
4040
4041 /*
4042 * Get the first variable.
4043 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return FAIL;
4046
4047 p = *arg;
4048 switch (p[0])
4049 {
4050 case '=': if (p[1] == '=')
4051 type = TYPE_EQUAL;
4052 else if (p[1] == '~')
4053 type = TYPE_MATCH;
4054 break;
4055 case '!': if (p[1] == '=')
4056 type = TYPE_NEQUAL;
4057 else if (p[1] == '~')
4058 type = TYPE_NOMATCH;
4059 break;
4060 case '>': if (p[1] != '=')
4061 {
4062 type = TYPE_GREATER;
4063 len = 1;
4064 }
4065 else
4066 type = TYPE_GEQUAL;
4067 break;
4068 case '<': if (p[1] != '=')
4069 {
4070 type = TYPE_SMALLER;
4071 len = 1;
4072 }
4073 else
4074 type = TYPE_SEQUAL;
4075 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004076 case 'i': if (p[1] == 's')
4077 {
4078 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4079 len = 5;
4080 if (!vim_isIDc(p[len]))
4081 {
4082 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4083 type_is = TRUE;
4084 }
4085 }
4086 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088
4089 /*
4090 * If there is a comparitive operator, use it.
4091 */
4092 if (type != TYPE_UNKNOWN)
4093 {
4094 /* extra question mark appended: ignore case */
4095 if (p[len] == '?')
4096 {
4097 ic = TRUE;
4098 ++len;
4099 }
4100 /* extra '#' appended: match case */
4101 else if (p[len] == '#')
4102 {
4103 ic = FALSE;
4104 ++len;
4105 }
4106 /* nothing appened: use 'ignorecase' */
4107 else
4108 ic = p_ic;
4109
4110 /*
4111 * Get the second variable.
4112 */
4113 *arg = skipwhite(p + len);
4114 if (eval5(arg, &var2, evaluate) == FAIL)
4115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return FAIL;
4118 }
4119
4120 if (evaluate)
4121 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004122 if (type_is && rettv->v_type != var2.v_type)
4123 {
4124 /* For "is" a different type always means FALSE, for "notis"
4125 * it means TRUE. */
4126 n1 = (type == TYPE_NEQUAL);
4127 }
4128 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4129 {
4130 if (type_is)
4131 {
4132 n1 = (rettv->v_type == var2.v_type
4133 && rettv->vval.v_list == var2.vval.v_list);
4134 if (type == TYPE_NEQUAL)
4135 n1 = !n1;
4136 }
4137 else if (rettv->v_type != var2.v_type
4138 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4139 {
4140 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004141 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004142 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004143 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004144 clear_tv(rettv);
4145 clear_tv(&var2);
4146 return FAIL;
4147 }
4148 else
4149 {
4150 /* Compare two Lists for being equal or unequal. */
4151 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4152 if (type == TYPE_NEQUAL)
4153 n1 = !n1;
4154 }
4155 }
4156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004157 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4158 {
4159 if (type_is)
4160 {
4161 n1 = (rettv->v_type == var2.v_type
4162 && rettv->vval.v_dict == var2.vval.v_dict);
4163 if (type == TYPE_NEQUAL)
4164 n1 = !n1;
4165 }
4166 else if (rettv->v_type != var2.v_type
4167 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4168 {
4169 if (rettv->v_type != var2.v_type)
4170 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4171 else
4172 EMSG(_("E736: Invalid operation for Dictionary"));
4173 clear_tv(rettv);
4174 clear_tv(&var2);
4175 return FAIL;
4176 }
4177 else
4178 {
4179 /* Compare two Dictionaries for being equal or unequal. */
4180 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4181 if (type == TYPE_NEQUAL)
4182 n1 = !n1;
4183 }
4184 }
4185
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004186 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4187 {
4188 if (rettv->v_type != var2.v_type
4189 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4190 {
4191 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004192 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004193 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004194 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004195 clear_tv(rettv);
4196 clear_tv(&var2);
4197 return FAIL;
4198 }
4199 else
4200 {
4201 /* Compare two Funcrefs for being equal or unequal. */
4202 if (rettv->vval.v_string == NULL
4203 || var2.vval.v_string == NULL)
4204 n1 = FALSE;
4205 else
4206 n1 = STRCMP(rettv->vval.v_string,
4207 var2.vval.v_string) == 0;
4208 if (type == TYPE_NEQUAL)
4209 n1 = !n1;
4210 }
4211 }
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 /*
4214 * If one of the two variables is a number, compare as a number.
4215 * When using "=~" or "!~", always compare as string.
4216 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004217 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 n1 = get_tv_number(rettv);
4221 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 switch (type)
4223 {
4224 case TYPE_EQUAL: n1 = (n1 == n2); break;
4225 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4226 case TYPE_GREATER: n1 = (n1 > n2); break;
4227 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4228 case TYPE_SMALLER: n1 = (n1 < n2); break;
4229 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4230 case TYPE_UNKNOWN:
4231 case TYPE_MATCH:
4232 case TYPE_NOMATCH: break; /* avoid gcc warning */
4233 }
4234 }
4235 else
4236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 s1 = get_tv_string_buf(rettv, buf1);
4238 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4240 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4241 else
4242 i = 0;
4243 n1 = FALSE;
4244 switch (type)
4245 {
4246 case TYPE_EQUAL: n1 = (i == 0); break;
4247 case TYPE_NEQUAL: n1 = (i != 0); break;
4248 case TYPE_GREATER: n1 = (i > 0); break;
4249 case TYPE_GEQUAL: n1 = (i >= 0); break;
4250 case TYPE_SMALLER: n1 = (i < 0); break;
4251 case TYPE_SEQUAL: n1 = (i <= 0); break;
4252
4253 case TYPE_MATCH:
4254 case TYPE_NOMATCH:
4255 /* avoid 'l' flag in 'cpoptions' */
4256 save_cpo = p_cpo;
4257 p_cpo = (char_u *)"";
4258 regmatch.regprog = vim_regcomp(s2,
4259 RE_MAGIC + RE_STRING);
4260 regmatch.rm_ic = ic;
4261 if (regmatch.regprog != NULL)
4262 {
4263 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4264 vim_free(regmatch.regprog);
4265 if (type == TYPE_NOMATCH)
4266 n1 = !n1;
4267 }
4268 p_cpo = save_cpo;
4269 break;
4270
4271 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4272 }
4273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 clear_tv(rettv);
4275 clear_tv(&var2);
4276 rettv->v_type = VAR_NUMBER;
4277 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280
4281 return OK;
4282}
4283
4284/*
4285 * Handle fourth level expression:
4286 * + number addition
4287 * - number subtraction
4288 * . string concatenation
4289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int evaluate;
4300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
4302 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int op;
4304 long n1, n2;
4305 char_u *s1, *s2;
4306 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4307 char_u *p;
4308
4309 /*
4310 * Get the first variable.
4311 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314
4315 /*
4316 * Repeat computing, until no '+', '-' or '.' is following.
4317 */
4318 for (;;)
4319 {
4320 op = **arg;
4321 if (op != '+' && op != '-' && op != '.')
4322 break;
4323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (op != '+' || rettv->v_type != VAR_LIST)
4325 {
4326 /* For "list + ...", an illegal use of the first operand as
4327 * a number cannot be determined before evaluating the 2nd
4328 * operand: if this is also a list, all is ok.
4329 * For "something . ...", "something - ..." or "non-list + ...",
4330 * we know that the first operand needs to be a string or number
4331 * without evaluating the 2nd operand. So check before to avoid
4332 * side effects after an error. */
4333 if (evaluate && get_tv_string_chk(rettv) == NULL)
4334 {
4335 clear_tv(rettv);
4336 return FAIL;
4337 }
4338 }
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /*
4341 * Get the second variable.
4342 */
4343 *arg = skipwhite(*arg + 1);
4344 if (eval6(arg, &var2, evaluate) == FAIL)
4345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004346 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 return FAIL;
4348 }
4349
4350 if (evaluate)
4351 {
4352 /*
4353 * Compute the result.
4354 */
4355 if (op == '.')
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4358 s2 = get_tv_string_buf_chk(&var2, buf2);
4359 if (s2 == NULL) /* type error ? */
4360 {
4361 clear_tv(rettv);
4362 clear_tv(&var2);
4363 return FAIL;
4364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004365 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 clear_tv(rettv);
4367 rettv->v_type = VAR_STRING;
4368 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004370 else if (op == '+' && rettv->v_type == VAR_LIST
4371 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004372 {
4373 /* concatenate Lists */
4374 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4375 &var3) == FAIL)
4376 {
4377 clear_tv(rettv);
4378 clear_tv(&var2);
4379 return FAIL;
4380 }
4381 clear_tv(rettv);
4382 *rettv = var3;
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 else
4385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
4387
4388 n1 = get_tv_number_chk(rettv, &error);
4389 if (error)
4390 {
4391 /* This can only happen for "list + non-list".
4392 * For "non-list + ..." or "something - ...", we returned
4393 * before evaluating the 2nd operand. */
4394 clear_tv(rettv);
4395 return FAIL;
4396 }
4397 n2 = get_tv_number_chk(&var2, &error);
4398 if (error)
4399 {
4400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (op == '+')
4406 n1 = n1 + n2;
4407 else
4408 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 rettv->v_type = VAR_NUMBER;
4410 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 }
4415 return OK;
4416}
4417
4418/*
4419 * Handle fifth level expression:
4420 * * number multiplication
4421 * / number division
4422 * % number modulo
4423 *
4424 * "arg" must point to the first non-white of the expression.
4425 * "arg" is advanced to the next non-white after the recognized expression.
4426 *
4427 * Return OK or FAIL.
4428 */
4429 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 int evaluate;
4434{
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 int op;
4437 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 /*
4441 * Get the first variable.
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return FAIL;
4445
4446 /*
4447 * Repeat computing, until no '*', '/' or '%' is following.
4448 */
4449 for (;;)
4450 {
4451 op = **arg;
4452 if (op != '*' && op != '/' && op != '%')
4453 break;
4454
4455 if (evaluate)
4456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 if (error)
4460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
4463 n1 = 0;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(*arg + 1);
4469 if (eval7(arg, &var2, evaluate) == FAIL)
4470 return FAIL;
4471
4472 if (evaluate)
4473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004474 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004476 if (error)
4477 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 /*
4480 * Compute the result.
4481 */
4482 if (op == '*')
4483 n1 = n1 * n2;
4484 else if (op == '/')
4485 {
4486 if (n2 == 0) /* give an error message? */
4487 n1 = 0x7fffffffL;
4488 else
4489 n1 = n1 / n2;
4490 }
4491 else
4492 {
4493 if (n2 == 0) /* give an error message? */
4494 n1 = 0;
4495 else
4496 n1 = n1 % n2;
4497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 rettv->v_type = VAR_NUMBER;
4499 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 }
4502
4503 return OK;
4504}
4505
4506/*
4507 * Handle sixth level expression:
4508 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004509 * "string" string constant
4510 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 * &option-name option value
4512 * @r register contents
4513 * identifier variable value
4514 * function() function call
4515 * $VAR environment variable
4516 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004517 * [expr, expr] List
4518 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 *
4520 * Also handle:
4521 * ! in front logical NOT
4522 * - in front unary minus
4523 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 * trailing [] subscript in String or List
4525 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 *
4527 * "arg" must point to the first non-white of the expression.
4528 * "arg" is advanced to the next non-white after the recognized expression.
4529 *
4530 * Return OK or FAIL.
4531 */
4532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 int evaluate;
4537{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 long n;
4539 int len;
4540 char_u *s;
4541 int val;
4542 char_u *start_leader, *end_leader;
4543 int ret = OK;
4544 char_u *alias;
4545
4546 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004550 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 /*
4553 * Skip '!' and '-' characters. They are handled later.
4554 */
4555 start_leader = *arg;
4556 while (**arg == '!' || **arg == '-' || **arg == '+')
4557 *arg = skipwhite(*arg + 1);
4558 end_leader = *arg;
4559
4560 switch (**arg)
4561 {
4562 /*
4563 * Number constant.
4564 */
4565 case '0':
4566 case '1':
4567 case '2':
4568 case '3':
4569 case '4':
4570 case '5':
4571 case '6':
4572 case '7':
4573 case '8':
4574 case '9':
4575 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4576 *arg += len;
4577 if (evaluate)
4578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 rettv->v_type = VAR_NUMBER;
4580 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 break;
4583
4584 /*
4585 * String constant: "string".
4586 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 break;
4589
4590 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 break;
4595
4596 /*
4597 * List: [expr, expr]
4598 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 break;
4601
4602 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 * Dictionary: {key: val, key: val}
4604 */
4605 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4606 break;
4607
4608 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004609 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004611 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613
4614 /*
4615 * Environment variable: $VAR.
4616 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619
4620 /*
4621 * Register contents: @r.
4622 */
4623 case '@': ++*arg;
4624 if (evaluate)
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004627 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 if (**arg != NUL)
4630 ++*arg;
4631 break;
4632
4633 /*
4634 * nested expression: (expression).
4635 */
4636 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (**arg == ')')
4639 ++*arg;
4640 else if (ret == OK)
4641 {
4642 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 ret = FAIL;
4645 }
4646 break;
4647
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651
4652 if (ret == NOTDONE)
4653 {
4654 /*
4655 * Must be a variable or function name.
4656 * Can also be a curly-braces kind of name: {expr}.
4657 */
4658 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004659 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 if (alias != NULL)
4661 s = alias;
4662
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 ret = FAIL;
4665 else
4666 {
4667 if (**arg == '(') /* recursive! */
4668 {
4669 /* If "s" is the name of a variable of type VAR_FUNC
4670 * use its contents. */
4671 s = deref_func_name(s, &len);
4672
4673 /* Invoke the function. */
4674 ret = get_func_tv(s, len, rettv, arg,
4675 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004676 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 /* Stop the expression evaluation when immediately
4678 * aborting on error, or when an interrupt occurred or
4679 * an exception was thrown but not caught. */
4680 if (aborting())
4681 {
4682 if (ret == OK)
4683 clear_tv(rettv);
4684 ret = FAIL;
4685 }
4686 }
4687 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004689 else
4690 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 }
4692
4693 if (alias != NULL)
4694 vim_free(alias);
4695 }
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 *arg = skipwhite(*arg);
4698
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004699 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4700 * expr(expr). */
4701 if (ret == OK)
4702 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
4704 /*
4705 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4706 */
4707 if (ret == OK && evaluate && end_leader > start_leader)
4708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 int error = FALSE;
4710
4711 val = get_tv_number_chk(rettv, &error);
4712 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 clear_tv(rettv);
4715 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 else
4718 {
4719 while (end_leader > start_leader)
4720 {
4721 --end_leader;
4722 if (*end_leader == '!')
4723 val = !val;
4724 else if (*end_leader == '-')
4725 val = -val;
4726 }
4727 clear_tv(rettv);
4728 rettv->v_type = VAR_NUMBER;
4729 rettv->vval.v_number = val;
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732
4733 return ret;
4734}
4735
4736/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004737 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4738 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4740 */
4741 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004742eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004744 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004746 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747{
4748 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004749 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 long len = -1;
4752 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 if (verbose)
4759 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 return FAIL;
4761 }
4762
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004765 /*
4766 * dict.name
4767 */
4768 key = *arg + 1;
4769 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4770 ;
4771 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 }
4775 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 /*
4778 * something[idx]
4779 *
4780 * Get the (first) variable from inside the [].
4781 */
4782 *arg = skipwhite(*arg + 1);
4783 if (**arg == ':')
4784 empty1 = TRUE;
4785 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4786 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4788 {
4789 /* not a number or string */
4790 clear_tv(&var1);
4791 return FAIL;
4792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793
4794 /*
4795 * Get the second variable from inside the [:].
4796 */
4797 if (**arg == ':')
4798 {
4799 range = TRUE;
4800 *arg = skipwhite(*arg + 1);
4801 if (**arg == ']')
4802 empty2 = TRUE;
4803 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 if (!empty1)
4806 clear_tv(&var1);
4807 return FAIL;
4808 }
4809 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4810 {
4811 /* not a number or string */
4812 if (!empty1)
4813 clear_tv(&var1);
4814 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004815 return FAIL;
4816 }
4817 }
4818
4819 /* Check for the ']'. */
4820 if (**arg != ']')
4821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 if (verbose)
4823 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824 clear_tv(&var1);
4825 if (range)
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 }
4831
4832 if (evaluate)
4833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004834 n1 = 0;
4835 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 n1 = get_tv_number(&var1);
4838 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 }
4840 if (range)
4841 {
4842 if (empty2)
4843 n2 = -1;
4844 else
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 n2 = get_tv_number(&var2);
4847 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 }
4849 }
4850
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 {
4853 case VAR_NUMBER:
4854 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 len = (long)STRLEN(s);
4857 if (range)
4858 {
4859 /* The resulting variable is a substring. If the indexes
4860 * are out of range the result is empty. */
4861 if (n1 < 0)
4862 {
4863 n1 = len + n1;
4864 if (n1 < 0)
4865 n1 = 0;
4866 }
4867 if (n2 < 0)
4868 n2 = len + n2;
4869 else if (n2 >= len)
4870 n2 = len;
4871 if (n1 >= len || n2 < 0 || n1 > n2)
4872 s = NULL;
4873 else
4874 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4875 }
4876 else
4877 {
4878 /* The resulting variable is a string of a single
4879 * character. If the index is too big or negative the
4880 * result is empty. */
4881 if (n1 >= len || n1 < 0)
4882 s = NULL;
4883 else
4884 s = vim_strnsave(s + n1, 1);
4885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 clear_tv(rettv);
4887 rettv->v_type = VAR_STRING;
4888 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 break;
4890
4891 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 if (n1 < 0)
4894 n1 = len + n1;
4895 if (!empty1 && (n1 < 0 || n1 >= len))
4896 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004897 /* For a range we allow invalid values and return an empty
4898 * list. A list index out of range is an error. */
4899 if (!range)
4900 {
4901 if (verbose)
4902 EMSGN(_(e_listidx), n1);
4903 return FAIL;
4904 }
4905 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 }
4907 if (range)
4908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 list_T *l;
4910 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911
4912 if (n2 < 0)
4913 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004914 else if (n2 >= len)
4915 n2 = len - 1;
4916 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004917 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 l = list_alloc();
4919 if (l == NULL)
4920 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 n1 <= n2; ++n1)
4923 {
4924 if (list_append_tv(l, &item->li_tv) == FAIL)
4925 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004926 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004927 return FAIL;
4928 }
4929 item = item->li_next;
4930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 clear_tv(rettv);
4932 rettv->v_type = VAR_LIST;
4933 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004934 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 }
4936 else
4937 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004938 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 clear_tv(rettv);
4940 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 }
4942 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943
4944 case VAR_DICT:
4945 if (range)
4946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004947 if (verbose)
4948 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 if (len == -1)
4950 clear_tv(&var1);
4951 return FAIL;
4952 }
4953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004954 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955
4956 if (len == -1)
4957 {
4958 key = get_tv_string(&var1);
4959 if (*key == NUL)
4960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004961 if (verbose)
4962 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 clear_tv(&var1);
4964 return FAIL;
4965 }
4966 }
4967
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004968 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004970 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004971 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (len == -1)
4973 clear_tv(&var1);
4974 if (item == NULL)
4975 return FAIL;
4976
4977 copy_tv(&item->di_tv, &var1);
4978 clear_tv(rettv);
4979 *rettv = var1;
4980 }
4981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 }
4983 }
4984
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 return OK;
4986}
4987
4988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 * Get an option value.
4990 * "arg" points to the '&' or '+' before the option name.
4991 * "arg" is advanced to character after the option name.
4992 * Return OK or FAIL.
4993 */
4994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004997 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 int evaluate;
4999{
5000 char_u *option_end;
5001 long numval;
5002 char_u *stringval;
5003 int opt_type;
5004 int c;
5005 int working = (**arg == '+'); /* has("+option") */
5006 int ret = OK;
5007 int opt_flags;
5008
5009 /*
5010 * Isolate the option name and find its value.
5011 */
5012 option_end = find_option_end(arg, &opt_flags);
5013 if (option_end == NULL)
5014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 EMSG2(_("E112: Option name missing: %s"), *arg);
5017 return FAIL;
5018 }
5019
5020 if (!evaluate)
5021 {
5022 *arg = option_end;
5023 return OK;
5024 }
5025
5026 c = *option_end;
5027 *option_end = NUL;
5028 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005029 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
5031 if (opt_type == -3) /* invalid name */
5032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005033 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 EMSG2(_("E113: Unknown option: %s"), *arg);
5035 ret = FAIL;
5036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 if (opt_type == -2) /* hidden string option */
5040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 rettv->v_type = VAR_STRING;
5042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 else if (opt_type == -1) /* hidden number option */
5045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 rettv->v_type = VAR_NUMBER;
5047 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049 else if (opt_type == 1) /* number option */
5050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 rettv->v_type = VAR_NUMBER;
5052 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 else /* string option */
5055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 rettv->v_type = VAR_STRING;
5057 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 }
5060 else if (working && (opt_type == -2 || opt_type == -1))
5061 ret = FAIL;
5062
5063 *option_end = c; /* put back for error messages */
5064 *arg = option_end;
5065
5066 return ret;
5067}
5068
5069/*
5070 * Allocate a variable for a string constant.
5071 * Return OK or FAIL.
5072 */
5073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 int evaluate;
5078{
5079 char_u *p;
5080 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 int extra = 0;
5082
5083 /*
5084 * Find the end of the string, skipping backslashed characters.
5085 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 if (*p == '\\' && p[1] != NUL)
5089 {
5090 ++p;
5091 /* A "\<x>" form occupies at least 4 characters, and produces up
5092 * to 6 characters: reserve space for 2 extra */
5093 if (*p == '<')
5094 extra += 2;
5095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
5098 if (*p != '"')
5099 {
5100 EMSG2(_("E114: Missing quote: %s"), *arg);
5101 return FAIL;
5102 }
5103
5104 /* If only parsing, set *arg and return here */
5105 if (!evaluate)
5106 {
5107 *arg = p + 1;
5108 return OK;
5109 }
5110
5111 /*
5112 * Copy the string into allocated memory, handling backslashed
5113 * characters.
5114 */
5115 name = alloc((unsigned)(p - *arg + extra));
5116 if (name == NULL)
5117 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 rettv->v_type = VAR_STRING;
5119 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 if (*p == '\\')
5124 {
5125 switch (*++p)
5126 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 case 'b': *name++ = BS; ++p; break;
5128 case 'e': *name++ = ESC; ++p; break;
5129 case 'f': *name++ = FF; ++p; break;
5130 case 'n': *name++ = NL; ++p; break;
5131 case 'r': *name++ = CAR; ++p; break;
5132 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 case 'X': /* hex: "\x1", "\x12" */
5135 case 'x':
5136 case 'u': /* Unicode: "\u0023" */
5137 case 'U':
5138 if (vim_isxdigit(p[1]))
5139 {
5140 int n, nr;
5141 int c = toupper(*p);
5142
5143 if (c == 'X')
5144 n = 2;
5145 else
5146 n = 4;
5147 nr = 0;
5148 while (--n >= 0 && vim_isxdigit(p[1]))
5149 {
5150 ++p;
5151 nr = (nr << 4) + hex2nr(*p);
5152 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef FEAT_MBYTE
5155 /* For "\u" store the number according to
5156 * 'encoding'. */
5157 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
5160#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /* octal: "\1", "\12", "\123" */
5166 case '0':
5167 case '1':
5168 case '2':
5169 case '3':
5170 case '4':
5171 case '5':
5172 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 case '7': *name = *p++ - '0';
5174 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 *name = (*name << 3) + *p++ - '0';
5177 if (*p >= '0' && *p <= '7')
5178 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (extra != 0)
5186 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189 }
5190 /* FALLTHROUGH */
5191
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 break;
5194 }
5195 }
5196 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 *arg = p + 1;
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 return OK;
5204}
5205
5206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 * Return OK or FAIL.
5209 */
5210 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 int evaluate;
5215{
5216 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005217 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005218 int reduce = 0;
5219
5220 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005222 */
5223 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005228 break;
5229 ++reduce;
5230 ++p;
5231 }
5232 }
5233
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005235 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005237 return FAIL;
5238 }
5239
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 if (!evaluate)
5242 {
5243 *arg = p + 1;
5244 return OK;
5245 }
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 */
5250 str = alloc((unsigned)((p - *arg) - reduce));
5251 if (str == NULL)
5252 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 rettv->v_type = VAR_STRING;
5254 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005255
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005259 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005261 break;
5262 ++p;
5263 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005267 *arg = p + 1;
5268
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005269 return OK;
5270}
5271
5272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 * Allocate a variable for a List and fill it from "*arg".
5274 * Return OK or FAIL.
5275 */
5276 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 int evaluate;
5281{
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 list_T *l = NULL;
5283 typval_T tv;
5284 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
5286 if (evaluate)
5287 {
5288 l = list_alloc();
5289 if (l == NULL)
5290 return FAIL;
5291 }
5292
5293 *arg = skipwhite(*arg + 1);
5294 while (**arg != ']' && **arg != NUL)
5295 {
5296 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5297 goto failret;
5298 if (evaluate)
5299 {
5300 item = listitem_alloc();
5301 if (item != NULL)
5302 {
5303 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005304 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 list_append(l, item);
5306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005307 else
5308 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 }
5310
5311 if (**arg == ']')
5312 break;
5313 if (**arg != ',')
5314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 goto failret;
5317 }
5318 *arg = skipwhite(*arg + 1);
5319 }
5320
5321 if (**arg != ']')
5322 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324failret:
5325 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005326 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
5328 }
5329
5330 *arg = skipwhite(*arg + 1);
5331 if (evaluate)
5332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333 rettv->v_type = VAR_LIST;
5334 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 ++l->lv_refcount;
5336 }
5337
5338 return OK;
5339}
5340
5341/*
5342 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005343 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005345 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346list_alloc()
5347{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005348 list_T *l;
5349
5350 l = (list_T *)alloc_clear(sizeof(list_T));
5351 if (l != NULL)
5352 {
5353 /* Prepend the list to the list of lists for garbage collection. */
5354 if (first_list != NULL)
5355 first_list->lv_used_prev = l;
5356 l->lv_used_prev = NULL;
5357 l->lv_used_next = first_list;
5358 first_list = l;
5359 }
5360 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361}
5362
5363/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005364 * Allocate an empty list for a return value.
5365 * Returns OK or FAIL.
5366 */
5367 static int
5368rettv_list_alloc(rettv)
5369 typval_T *rettv;
5370{
5371 list_T *l = list_alloc();
5372
5373 if (l == NULL)
5374 return FAIL;
5375
5376 rettv->vval.v_list = l;
5377 rettv->v_type = VAR_LIST;
5378 ++l->lv_refcount;
5379 return OK;
5380}
5381
5382/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 * Unreference a list: decrement the reference count and free it when it
5384 * becomes zero.
5385 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005386 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005388 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005390 if (l != NULL && --l->lv_refcount <= 0)
5391 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392}
5393
5394/*
5395 * Free a list, including all items it points to.
5396 * Ignores the reference count.
5397 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005398 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005399list_free(l, recurse)
5400 list_T *l;
5401 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402{
Bram Moolenaar33570922005-01-25 22:26:29 +00005403 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405 /* Remove the list from the list of lists for garbage collection. */
5406 if (l->lv_used_prev == NULL)
5407 first_list = l->lv_used_next;
5408 else
5409 l->lv_used_prev->lv_used_next = l->lv_used_next;
5410 if (l->lv_used_next != NULL)
5411 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5412
Bram Moolenaard9fba312005-06-26 22:34:35 +00005413 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005415 /* Remove the item before deleting it. */
5416 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005417 if (recurse || (item->li_tv.v_type != VAR_LIST
5418 && item->li_tv.v_type != VAR_DICT))
5419 clear_tv(&item->li_tv);
5420 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 vim_free(l);
5423}
5424
5425/*
5426 * Allocate a list item.
5427 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429listitem_alloc()
5430{
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432}
5433
5434/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005435 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 */
5437 static void
5438listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005439 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 vim_free(item);
5443}
5444
5445/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005446 * Remove a list item from a List and free it. Also clears the value.
5447 */
5448 static void
5449listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 list_T *l;
5451 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005452{
5453 list_remove(l, item, item);
5454 listitem_free(item);
5455}
5456
5457/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 * Get the number of items in a list.
5459 */
5460 static long
5461list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 if (l == NULL)
5465 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005466 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467}
5468
5469/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005470 * Return TRUE when two lists have exactly the same values.
5471 */
5472 static int
5473list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005474 list_T *l1;
5475 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005476 int ic; /* ignore case for strings */
5477{
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005479
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005480 if (l1 == l2)
5481 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005482 if (list_len(l1) != list_len(l2))
5483 return FALSE;
5484
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485 for (item1 = l1->lv_first, item2 = l2->lv_first;
5486 item1 != NULL && item2 != NULL;
5487 item1 = item1->li_next, item2 = item2->li_next)
5488 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5489 return FALSE;
5490 return item1 == NULL && item2 == NULL;
5491}
5492
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005493#if defined(FEAT_PYTHON) || defined(PROTO)
5494/*
5495 * Return the dictitem that an entry in a hashtable points to.
5496 */
5497 dictitem_T *
5498dict_lookup(hi)
5499 hashitem_T *hi;
5500{
5501 return HI2DI(hi);
5502}
5503#endif
5504
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005505/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005506 * Return TRUE when two dictionaries have exactly the same key/values.
5507 */
5508 static int
5509dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 dict_T *d1;
5511 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005512 int ic; /* ignore case for strings */
5513{
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 hashitem_T *hi;
5515 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005517
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005518 if (d1 == d2)
5519 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005520 if (dict_len(d1) != dict_len(d2))
5521 return FALSE;
5522
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005523 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005525 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005526 if (!HASHITEM_EMPTY(hi))
5527 {
5528 item2 = dict_find(d2, hi->hi_key, -1);
5529 if (item2 == NULL)
5530 return FALSE;
5531 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5532 return FALSE;
5533 --todo;
5534 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005535 }
5536 return TRUE;
5537}
5538
5539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005540 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005541 * Compares the items just like "==" would compare them, but strings and
5542 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005543 */
5544 static int
5545tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 typval_T *tv1;
5547 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 int ic; /* ignore case */
5549{
5550 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005551 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005552 static int recursive = 0; /* cach recursive loops */
5553 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005554
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005555 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005556 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005557 /* Catch lists and dicts that have an endless loop by limiting
5558 * recursiveness to 1000. We guess they are equal then. */
5559 if (recursive >= 1000)
5560 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005561
5562 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005563 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005564 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005565 ++recursive;
5566 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5567 --recursive;
5568 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005569
5570 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005571 ++recursive;
5572 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5573 --recursive;
5574 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005575
5576 case VAR_FUNC:
5577 return (tv1->vval.v_string != NULL
5578 && tv2->vval.v_string != NULL
5579 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5580
5581 case VAR_NUMBER:
5582 return tv1->vval.v_number == tv2->vval.v_number;
5583
5584 case VAR_STRING:
5585 s1 = get_tv_string_buf(tv1, buf1);
5586 s2 = get_tv_string_buf(tv2, buf2);
5587 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005589
5590 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005591 return TRUE;
5592}
5593
5594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 * Locate item with index "n" in list "l" and return it.
5596 * A negative index is counted from the end; -1 is the last item.
5597 * Returns NULL when "n" is out of range.
5598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005599 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 long n;
5603{
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 long idx;
5606
5607 if (l == NULL)
5608 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005609
5610 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005612 n = l->lv_len + n;
5613
5614 /* Check for index out of range. */
5615 if (n < 0 || n >= l->lv_len)
5616 return NULL;
5617
5618 /* When there is a cached index may start search from there. */
5619 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005621 if (n < l->lv_idx / 2)
5622 {
5623 /* closest to the start of the list */
5624 item = l->lv_first;
5625 idx = 0;
5626 }
5627 else if (n > (l->lv_idx + l->lv_len) / 2)
5628 {
5629 /* closest to the end of the list */
5630 item = l->lv_last;
5631 idx = l->lv_len - 1;
5632 }
5633 else
5634 {
5635 /* closest to the cached index */
5636 item = l->lv_idx_item;
5637 idx = l->lv_idx;
5638 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 }
5640 else
5641 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005642 if (n < l->lv_len / 2)
5643 {
5644 /* closest to the start of the list */
5645 item = l->lv_first;
5646 idx = 0;
5647 }
5648 else
5649 {
5650 /* closest to the end of the list */
5651 item = l->lv_last;
5652 idx = l->lv_len - 1;
5653 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005655
5656 while (n > idx)
5657 {
5658 /* search forward */
5659 item = item->li_next;
5660 ++idx;
5661 }
5662 while (n < idx)
5663 {
5664 /* search backward */
5665 item = item->li_prev;
5666 --idx;
5667 }
5668
5669 /* cache the used index */
5670 l->lv_idx = idx;
5671 l->lv_idx_item = item;
5672
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 return item;
5674}
5675
5676/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005677 * Get list item "l[idx]" as a number.
5678 */
5679 static long
5680list_find_nr(l, idx, errorp)
5681 list_T *l;
5682 long idx;
5683 int *errorp; /* set to TRUE when something wrong */
5684{
5685 listitem_T *li;
5686
5687 li = list_find(l, idx);
5688 if (li == NULL)
5689 {
5690 if (errorp != NULL)
5691 *errorp = TRUE;
5692 return -1L;
5693 }
5694 return get_tv_number_chk(&li->li_tv, errorp);
5695}
5696
5697/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005698 * Locate "item" list "l" and return its index.
5699 * Returns -1 when "item" is not in the list.
5700 */
5701 static long
5702list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 list_T *l;
5704 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005705{
5706 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005708
5709 if (l == NULL)
5710 return -1;
5711 idx = 0;
5712 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5713 ++idx;
5714 if (li == NULL)
5715 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005716 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005717}
5718
5719/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 * Append item "item" to the end of list "l".
5721 */
5722 static void
5723list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *l;
5725 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726{
5727 if (l->lv_last == NULL)
5728 {
5729 /* empty list */
5730 l->lv_first = item;
5731 l->lv_last = item;
5732 item->li_prev = NULL;
5733 }
5734 else
5735 {
5736 l->lv_last->li_next = item;
5737 item->li_prev = l->lv_last;
5738 l->lv_last = item;
5739 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005740 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 item->li_next = NULL;
5742}
5743
5744/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 */
5748 static int
5749list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005750 list_T *l;
5751 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005753 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754
Bram Moolenaar05159a02005-02-26 23:04:13 +00005755 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005757 copy_tv(tv, &li->li_tv);
5758 list_append(l, li);
5759 return OK;
5760}
5761
5762/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005763 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005764 * Return FAIL when out of memory.
5765 */
5766 int
5767list_append_dict(list, dict)
5768 list_T *list;
5769 dict_T *dict;
5770{
5771 listitem_T *li = listitem_alloc();
5772
5773 if (li == NULL)
5774 return FAIL;
5775 li->li_tv.v_type = VAR_DICT;
5776 li->li_tv.v_lock = 0;
5777 li->li_tv.vval.v_dict = dict;
5778 list_append(list, li);
5779 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 return OK;
5781}
5782
5783/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005784 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005785 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005786 * Returns FAIL when out of memory.
5787 */
5788 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005789list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005790 list_T *l;
5791 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005792 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005793{
5794 listitem_T *li = listitem_alloc();
5795
5796 if (li == NULL)
5797 return FAIL;
5798 list_append(l, li);
5799 li->li_tv.v_type = VAR_STRING;
5800 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005801 if (str == NULL)
5802 li->li_tv.vval.v_string = NULL;
5803 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005804 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005805 return FAIL;
5806 return OK;
5807}
5808
5809/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005810 * Append "n" to list "l".
5811 * Returns FAIL when out of memory.
5812 */
5813 static int
5814list_append_number(l, n)
5815 list_T *l;
5816 varnumber_T n;
5817{
5818 listitem_T *li;
5819
5820 li = listitem_alloc();
5821 if (li == NULL)
5822 return FAIL;
5823 li->li_tv.v_type = VAR_NUMBER;
5824 li->li_tv.v_lock = 0;
5825 li->li_tv.vval.v_number = n;
5826 list_append(l, li);
5827 return OK;
5828}
5829
5830/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005832 * If "item" is NULL append at the end.
5833 * Return FAIL when out of memory.
5834 */
5835 static int
5836list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l;
5838 typval_T *tv;
5839 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840{
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842
5843 if (ni == NULL)
5844 return FAIL;
5845 copy_tv(tv, &ni->li_tv);
5846 if (item == NULL)
5847 /* Append new item at end of list. */
5848 list_append(l, ni);
5849 else
5850 {
5851 /* Insert new item before existing item. */
5852 ni->li_prev = item->li_prev;
5853 ni->li_next = item;
5854 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005855 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005857 ++l->lv_idx;
5858 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005859 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005860 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005862 l->lv_idx_item = NULL;
5863 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005864 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005865 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866 }
5867 return OK;
5868}
5869
5870/*
5871 * Extend "l1" with "l2".
5872 * If "bef" is NULL append at the end, otherwise insert before this item.
5873 * Returns FAIL when out of memory.
5874 */
5875 static int
5876list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *l1;
5878 list_T *l2;
5879 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880{
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882
5883 for (item = l2->lv_first; item != NULL; item = item->li_next)
5884 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5885 return FAIL;
5886 return OK;
5887}
5888
5889/*
5890 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5891 * Return FAIL when out of memory.
5892 */
5893 static int
5894list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 list_T *l1;
5896 list_T *l2;
5897 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898{
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005900
5901 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005902 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903 if (l == NULL)
5904 return FAIL;
5905 tv->v_type = VAR_LIST;
5906 tv->vval.v_list = l;
5907
5908 /* append all items from the second list */
5909 return list_extend(l, l2, NULL);
5910}
5911
5912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 * Returns NULL when out of memory.
5917 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005919list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 list_T *copy;
5925 listitem_T *item;
5926 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927
5928 if (orig == NULL)
5929 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930
5931 copy = list_alloc();
5932 if (copy != NULL)
5933 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005934 if (copyID != 0)
5935 {
5936 /* Do this before adding the items, because one of the items may
5937 * refer back to this list. */
5938 orig->lv_copyID = copyID;
5939 orig->lv_copylist = copy;
5940 }
5941 for (item = orig->lv_first; item != NULL && !got_int;
5942 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 {
5944 ni = listitem_alloc();
5945 if (ni == NULL)
5946 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005947 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005948 {
5949 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5950 {
5951 vim_free(ni);
5952 break;
5953 }
5954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 list_append(copy, ni);
5958 }
5959 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 if (item != NULL)
5961 {
5962 list_unref(copy);
5963 copy = NULL;
5964 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 }
5966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 return copy;
5968}
5969
5970/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l;
5977 listitem_T *item;
5978 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979{
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 /* notify watchers */
5983 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005985 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 list_fix_watch(l, ip);
5987 if (ip == item2)
5988 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990
5991 if (item2->li_next == NULL)
5992 l->lv_last = item->li_prev;
5993 else
5994 item2->li_next->li_prev = item->li_prev;
5995 if (item->li_prev == NULL)
5996 l->lv_first = item2->li_next;
5997 else
5998 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000}
6001
6002/*
6003 * Return an allocated string with the string representation of a list.
6004 * May return NULL.
6005 */
6006 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006009 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
6011 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012
6013 if (tv->vval.v_list == NULL)
6014 return NULL;
6015 ga_init2(&ga, (int)sizeof(char), 80);
6016 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006018 {
6019 vim_free(ga.ga_data);
6020 return NULL;
6021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 ga_append(&ga, ']');
6023 ga_append(&ga, NUL);
6024 return (char_u *)ga.ga_data;
6025}
6026
6027/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006028 * Join list "l" into a string in "*gap", using separator "sep".
6029 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006030 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006032 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006034 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 char_u *sep;
6037 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006038 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039{
6040 int first = TRUE;
6041 char_u *tofree;
6042 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006044 char_u *s;
6045
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006046 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047 {
6048 if (first)
6049 first = FALSE;
6050 else
6051 ga_concat(gap, sep);
6052
6053 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006054 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006056 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 if (s != NULL)
6058 ga_concat(gap, s);
6059 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006060 if (s == NULL)
6061 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006063 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064}
6065
6066/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006067 * Garbage collection for lists and dictionaries.
6068 *
6069 * We use reference counts to be able to free most items right away when they
6070 * are no longer used. But for composite items it's possible that it becomes
6071 * unused while the reference count is > 0: When there is a recursive
6072 * reference. Example:
6073 * :let l = [1, 2, 3]
6074 * :let d = {9: l}
6075 * :let l[1] = d
6076 *
6077 * Since this is quite unusual we handle this with garbage collection: every
6078 * once in a while find out which lists and dicts are not referenced from any
6079 * variable.
6080 *
6081 * Here is a good reference text about garbage collection (refers to Python
6082 * but it applies to all reference-counting mechanisms):
6083 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006084 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006085
6086/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006087 * Do garbage collection for lists and dicts.
6088 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006089 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 int
6091garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006092{
6093 dict_T *dd;
6094 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006095 int copyID = ++current_copyID;
6096 buf_T *buf;
6097 win_T *wp;
6098 int i;
6099 funccall_T *fc;
6100 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006101#ifdef FEAT_WINDOWS
6102 tabpage_T *tp;
6103#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006105 /* Only do this once. */
6106 want_garbage_collect = FALSE;
6107 may_garbage_collect = FALSE;
6108
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 /*
6110 * 1. Go through all accessible variables and mark all lists and dicts
6111 * with copyID.
6112 */
6113 /* script-local variables */
6114 for (i = 1; i <= ga_scripts.ga_len; ++i)
6115 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6116
6117 /* buffer-local variables */
6118 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6119 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6120
6121 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006122 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006123 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6124
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006125#ifdef FEAT_WINDOWS
6126 /* tabpage-local variables */
6127 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6128 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6129#endif
6130
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006131 /* global variables */
6132 set_ref_in_ht(&globvarht, copyID);
6133
6134 /* function-local variables */
6135 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6136 {
6137 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6138 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6139 }
6140
6141 /*
6142 * 2. Go through the list of dicts and free items without the copyID.
6143 */
6144 for (dd = first_dict; dd != NULL; )
6145 if (dd->dv_copyID != copyID)
6146 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006147 /* Free the Dictionary and ordinary items it contains, but don't
6148 * recurse into Lists and Dictionaries, they will be in the list
6149 * of dicts or list of lists. */
6150 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006151 did_free = TRUE;
6152
6153 /* restart, next dict may also have been freed */
6154 dd = first_dict;
6155 }
6156 else
6157 dd = dd->dv_used_next;
6158
6159 /*
6160 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006161 * But don't free a list that has a watcher (used in a for loop), these
6162 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006163 */
6164 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006165 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006166 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006167 /* Free the List and ordinary items it contains, but don't recurse
6168 * into Lists and Dictionaries, they will be in the list of dicts
6169 * or list of lists. */
6170 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006171 did_free = TRUE;
6172
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006173 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006174 ll = first_list;
6175 }
6176 else
6177 ll = ll->lv_used_next;
6178
6179 return did_free;
6180}
6181
6182/*
6183 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6184 */
6185 static void
6186set_ref_in_ht(ht, copyID)
6187 hashtab_T *ht;
6188 int copyID;
6189{
6190 int todo;
6191 hashitem_T *hi;
6192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006193 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 for (hi = ht->ht_array; todo > 0; ++hi)
6195 if (!HASHITEM_EMPTY(hi))
6196 {
6197 --todo;
6198 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6199 }
6200}
6201
6202/*
6203 * Mark all lists and dicts referenced through list "l" with "copyID".
6204 */
6205 static void
6206set_ref_in_list(l, copyID)
6207 list_T *l;
6208 int copyID;
6209{
6210 listitem_T *li;
6211
6212 for (li = l->lv_first; li != NULL; li = li->li_next)
6213 set_ref_in_item(&li->li_tv, copyID);
6214}
6215
6216/*
6217 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6218 */
6219 static void
6220set_ref_in_item(tv, copyID)
6221 typval_T *tv;
6222 int copyID;
6223{
6224 dict_T *dd;
6225 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006226
6227 switch (tv->v_type)
6228 {
6229 case VAR_DICT:
6230 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006231 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006232 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006233 /* Didn't see this dict yet. */
6234 dd->dv_copyID = copyID;
6235 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006236 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006237 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006238
6239 case VAR_LIST:
6240 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006241 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006242 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006243 /* Didn't see this list yet. */
6244 ll->lv_copyID = copyID;
6245 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006246 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006247 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006248 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006249 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006250}
6251
6252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253 * Allocate an empty header for a dictionary.
6254 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006255 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256dict_alloc()
6257{
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006259
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006261 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006262 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006263 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006264 if (first_dict != NULL)
6265 first_dict->dv_used_prev = d;
6266 d->dv_used_next = first_dict;
6267 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006268 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006269
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006271 d->dv_lock = 0;
6272 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006273 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006274 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006275 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276}
6277
6278/*
6279 * Unreference a Dictionary: decrement the reference count and free it when it
6280 * becomes zero.
6281 */
6282 static void
6283dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006285{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006286 if (d != NULL && --d->dv_refcount <= 0)
6287 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006288}
6289
6290/*
6291 * Free a Dictionary, including all items it contains.
6292 * Ignores the reference count.
6293 */
6294 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006295dict_free(d, recurse)
6296 dict_T *d;
6297 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006301 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006303 /* Remove the dict from the list of dicts for garbage collection. */
6304 if (d->dv_used_prev == NULL)
6305 first_dict = d->dv_used_next;
6306 else
6307 d->dv_used_prev->dv_used_next = d->dv_used_next;
6308 if (d->dv_used_next != NULL)
6309 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6310
6311 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006312 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006313 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316 if (!HASHITEM_EMPTY(hi))
6317 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006318 /* Remove the item before deleting it, just in case there is
6319 * something recursive causing trouble. */
6320 di = HI2DI(hi);
6321 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006322 if (recurse || (di->di_tv.v_type != VAR_LIST
6323 && di->di_tv.v_type != VAR_DICT))
6324 clear_tv(&di->di_tv);
6325 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326 --todo;
6327 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330 vim_free(d);
6331}
6332
6333/*
6334 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006335 * The "key" is copied to the new item.
6336 * Note that the value of the item "di_tv" still needs to be initialized!
6337 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340dictitem_alloc(key)
6341 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342{
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006345 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006349 di->di_flags = 0;
6350 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352}
6353
6354/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 * Make a copy of a Dictionary item.
6356 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006358dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006362
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006363 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6364 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365 if (di != NULL)
6366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006368 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006369 copy_tv(&org->di_tv, &di->di_tv);
6370 }
6371 return di;
6372}
6373
6374/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 * Remove item "item" from Dictionary "dict" and free it.
6376 */
6377 static void
6378dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 dict_T *dict;
6380 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006381{
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006383
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006385 if (HASHITEM_EMPTY(hi))
6386 EMSG2(_(e_intern2), "dictitem_remove()");
6387 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389 dictitem_free(item);
6390}
6391
6392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006393 * Free a dict item. Also clears the value.
6394 */
6395 static void
6396dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 clear_tv(&item->di_tv);
6400 vim_free(item);
6401}
6402
6403/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6405 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006407 * Returns NULL when out of memory.
6408 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006410dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006413 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414{
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 dict_T *copy;
6416 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006419
6420 if (orig == NULL)
6421 return NULL;
6422
6423 copy = dict_alloc();
6424 if (copy != NULL)
6425 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006426 if (copyID != 0)
6427 {
6428 orig->dv_copyID = copyID;
6429 orig->dv_copydict = copy;
6430 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006431 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006432 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006436 --todo;
6437
6438 di = dictitem_alloc(hi->hi_key);
6439 if (di == NULL)
6440 break;
6441 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006442 {
6443 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6444 copyID) == FAIL)
6445 {
6446 vim_free(di);
6447 break;
6448 }
6449 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006450 else
6451 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6452 if (dict_add(copy, di) == FAIL)
6453 {
6454 dictitem_free(di);
6455 break;
6456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006461 if (todo > 0)
6462 {
6463 dict_unref(copy);
6464 copy = NULL;
6465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 }
6467
6468 return copy;
6469}
6470
6471/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006472 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006473 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006476dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 dict_T *d;
6478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006479{
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006481}
6482
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006484 * Add a number or string entry to dictionary "d".
6485 * When "str" is NULL use number "nr", otherwise use "str".
6486 * Returns FAIL when out of memory and when key already exists.
6487 */
6488 int
6489dict_add_nr_str(d, key, nr, str)
6490 dict_T *d;
6491 char *key;
6492 long nr;
6493 char_u *str;
6494{
6495 dictitem_T *item;
6496
6497 item = dictitem_alloc((char_u *)key);
6498 if (item == NULL)
6499 return FAIL;
6500 item->di_tv.v_lock = 0;
6501 if (str == NULL)
6502 {
6503 item->di_tv.v_type = VAR_NUMBER;
6504 item->di_tv.vval.v_number = nr;
6505 }
6506 else
6507 {
6508 item->di_tv.v_type = VAR_STRING;
6509 item->di_tv.vval.v_string = vim_strsave(str);
6510 }
6511 if (dict_add(d, item) == FAIL)
6512 {
6513 dictitem_free(item);
6514 return FAIL;
6515 }
6516 return OK;
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Get the number of items in a Dictionary.
6521 */
6522 static long
6523dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 if (d == NULL)
6527 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006528 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006529}
6530
6531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532 * Find item "key[len]" in Dictionary "d".
6533 * If "len" is negative use strlen(key).
6534 * Returns NULL when not found.
6535 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006537dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006539 char_u *key;
6540 int len;
6541{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006542#define AKEYLEN 200
6543 char_u buf[AKEYLEN];
6544 char_u *akey;
6545 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006548 if (len < 0)
6549 akey = key;
6550 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006552 tofree = akey = vim_strnsave(key, len);
6553 if (akey == NULL)
6554 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006555 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006556 else
6557 {
6558 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006559 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560 akey = buf;
6561 }
6562
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006564 vim_free(tofree);
6565 if (HASHITEM_EMPTY(hi))
6566 return NULL;
6567 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568}
6569
6570/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006571 * Get a string item from a dictionary.
6572 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006573 * Returns NULL if the entry doesn't exist or out of memory.
6574 */
6575 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006576get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006577 dict_T *d;
6578 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006579 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006580{
6581 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006582 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006583
6584 di = dict_find(d, key, -1);
6585 if (di == NULL)
6586 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006587 s = get_tv_string(&di->di_tv);
6588 if (save && s != NULL)
6589 s = vim_strsave(s);
6590 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006591}
6592
6593/*
6594 * Get a number item from a dictionary.
6595 * Returns 0 if the entry doesn't exist or out of memory.
6596 */
6597 long
6598get_dict_number(d, key)
6599 dict_T *d;
6600 char_u *key;
6601{
6602 dictitem_T *di;
6603
6604 di = dict_find(d, key, -1);
6605 if (di == NULL)
6606 return 0;
6607 return get_tv_number(&di->di_tv);
6608}
6609
6610/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 * Return an allocated string with the string representation of a Dictionary.
6612 * May return NULL.
6613 */
6614 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006615dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006617 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618{
6619 garray_T ga;
6620 int first = TRUE;
6621 char_u *tofree;
6622 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006626 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006628 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 return NULL;
6630 ga_init2(&ga, (int)sizeof(char), 80);
6631 ga_append(&ga, '{');
6632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006633 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006638 --todo;
6639
6640 if (first)
6641 first = FALSE;
6642 else
6643 ga_concat(&ga, (char_u *)", ");
6644
6645 tofree = string_quote(hi->hi_key, FALSE);
6646 if (tofree != NULL)
6647 {
6648 ga_concat(&ga, tofree);
6649 vim_free(tofree);
6650 }
6651 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006652 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 if (s != NULL)
6654 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 if (s == NULL)
6657 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006659 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006660 if (todo > 0)
6661 {
6662 vim_free(ga.ga_data);
6663 return NULL;
6664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665
6666 ga_append(&ga, '}');
6667 ga_append(&ga, NUL);
6668 return (char_u *)ga.ga_data;
6669}
6670
6671/*
6672 * Allocate a variable for a Dictionary and fill it from "*arg".
6673 * Return OK or FAIL. Returns NOTDONE for {expr}.
6674 */
6675 static int
6676get_dict_tv(arg, rettv, evaluate)
6677 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 int evaluate;
6680{
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 dict_T *d = NULL;
6682 typval_T tvkey;
6683 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006687 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688
6689 /*
6690 * First check if it's not a curly-braces thing: {expr}.
6691 * Must do this without evaluating, otherwise a function may be called
6692 * twice. Unfortunately this means we need to call eval1() twice for the
6693 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006695 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 if (*start != '}')
6697 {
6698 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6699 return FAIL;
6700 if (*start == '}')
6701 return NOTDONE;
6702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703
6704 if (evaluate)
6705 {
6706 d = dict_alloc();
6707 if (d == NULL)
6708 return FAIL;
6709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006710 tvkey.v_type = VAR_UNKNOWN;
6711 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712
6713 *arg = skipwhite(*arg + 1);
6714 while (**arg != '}' && **arg != NUL)
6715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717 goto failret;
6718 if (**arg != ':')
6719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006720 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006721 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 goto failret;
6723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006724 key = get_tv_string_buf_chk(&tvkey, buf);
6725 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006727 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6728 if (key != NULL)
6729 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006730 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006731 goto failret;
6732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006733
6734 *arg = skipwhite(*arg + 1);
6735 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 goto failret;
6739 }
6740 if (evaluate)
6741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006742 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743 if (item != NULL)
6744 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006745 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006746 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 clear_tv(&tv);
6748 goto failret;
6749 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006750 item = dictitem_alloc(key);
6751 clear_tv(&tvkey);
6752 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006754 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006755 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006756 if (dict_add(d, item) == FAIL)
6757 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 }
6759 }
6760
6761 if (**arg == '}')
6762 break;
6763 if (**arg != ',')
6764 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006765 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006766 goto failret;
6767 }
6768 *arg = skipwhite(*arg + 1);
6769 }
6770
6771 if (**arg != '}')
6772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006773 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774failret:
6775 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006776 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006777 return FAIL;
6778 }
6779
6780 *arg = skipwhite(*arg + 1);
6781 if (evaluate)
6782 {
6783 rettv->v_type = VAR_DICT;
6784 rettv->vval.v_dict = d;
6785 ++d->dv_refcount;
6786 }
6787
6788 return OK;
6789}
6790
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792 * Return a string with the string representation of a variable.
6793 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006794 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006796 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006797 * May return NULL;
6798 */
6799 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006800echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006803 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006804 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006806 static int recurse = 0;
6807 char_u *r = NULL;
6808
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006811 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 *tofree = NULL;
6813 return NULL;
6814 }
6815 ++recurse;
6816
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 switch (tv->v_type)
6818 {
6819 case VAR_FUNC:
6820 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006821 r = tv->vval.v_string;
6822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006825 if (tv->vval.v_list == NULL)
6826 {
6827 *tofree = NULL;
6828 r = NULL;
6829 }
6830 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6831 {
6832 *tofree = NULL;
6833 r = (char_u *)"[...]";
6834 }
6835 else
6836 {
6837 tv->vval.v_list->lv_copyID = copyID;
6838 *tofree = list2string(tv, copyID);
6839 r = *tofree;
6840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006842
Bram Moolenaar8c711452005-01-14 21:53:12 +00006843 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006844 if (tv->vval.v_dict == NULL)
6845 {
6846 *tofree = NULL;
6847 r = NULL;
6848 }
6849 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6850 {
6851 *tofree = NULL;
6852 r = (char_u *)"{...}";
6853 }
6854 else
6855 {
6856 tv->vval.v_dict->dv_copyID = copyID;
6857 *tofree = dict2string(tv, copyID);
6858 r = *tofree;
6859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862 case VAR_STRING:
6863 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006864 *tofree = NULL;
6865 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006868 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872
6873 --recurse;
6874 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875}
6876
6877/*
6878 * Return a string with the string representation of a variable.
6879 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6880 * "numbuf" is used for a number.
6881 * Puts quotes around strings, so that they can be parsed back by eval().
6882 * May return NULL;
6883 */
6884 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006885tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006886 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 char_u **tofree;
6888 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006889 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890{
6891 switch (tv->v_type)
6892 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 case VAR_FUNC:
6894 *tofree = string_quote(tv->vval.v_string, TRUE);
6895 return *tofree;
6896 case VAR_STRING:
6897 *tofree = string_quote(tv->vval.v_string, FALSE);
6898 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006904 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006905 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006906 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006907}
6908
6909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 * Return string "str" in ' quotes, doubling ' characters.
6911 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006913 */
6914 static char_u *
6915string_quote(str, function)
6916 char_u *str;
6917 int function;
6918{
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920 char_u *p, *r, *s;
6921
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 len = (function ? 13 : 3);
6923 if (str != NULL)
6924 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006925 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006926 for (p = str; *p != NUL; mb_ptr_adv(p))
6927 if (*p == '\'')
6928 ++len;
6929 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006930 s = r = alloc(len);
6931 if (r != NULL)
6932 {
6933 if (function)
6934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006936 r += 10;
6937 }
6938 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 if (str != NULL)
6941 for (p = str; *p != NUL; )
6942 {
6943 if (*p == '\'')
6944 *r++ = '\'';
6945 MB_COPY_CHAR(p, r);
6946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006948 if (function)
6949 *r++ = ')';
6950 *r++ = NUL;
6951 }
6952 return s;
6953}
6954
6955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 * Get the value of an environment variable.
6957 * "arg" is pointing to the '$'. It is advanced to after the name.
6958 * If the environment variable was not set, silently assume it is empty.
6959 * Always return OK.
6960 */
6961 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006962get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 int evaluate;
6966{
6967 char_u *string = NULL;
6968 int len;
6969 int cc;
6970 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006971 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 ++*arg;
6974 name = *arg;
6975 len = get_env_len(arg);
6976 if (evaluate)
6977 {
6978 if (len != 0)
6979 {
6980 cc = name[len];
6981 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006982 /* first try vim_getenv(), fast for normal environment vars */
6983 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006985 {
6986 if (!mustfree)
6987 string = vim_strsave(string);
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else
6990 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006991 if (mustfree)
6992 vim_free(string);
6993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 /* next try expanding things like $VIM and ${HOME} */
6995 string = expand_env_save(name - 1);
6996 if (string != NULL && *string == '$')
6997 {
6998 vim_free(string);
6999 string = NULL;
7000 }
7001 }
7002 name[len] = cc;
7003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007004 rettv->v_type = VAR_STRING;
7005 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007
7008 return OK;
7009}
7010
7011/*
7012 * Array with names and number of arguments of all internal functions
7013 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7014 */
7015static struct fst
7016{
7017 char *f_name; /* function name */
7018 char f_min_argc; /* minimal number of arguments */
7019 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007021 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022} functions[] =
7023{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007024 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {"append", 2, 2, f_append},
7026 {"argc", 0, 0, f_argc},
7027 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007028 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007030 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {"bufexists", 1, 1, f_bufexists},
7032 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7033 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7034 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7035 {"buflisted", 1, 1, f_buflisted},
7036 {"bufloaded", 1, 1, f_bufloaded},
7037 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007038 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"bufwinnr", 1, 1, f_bufwinnr},
7040 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007041 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007043 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"char2nr", 1, 1, f_char2nr},
7045 {"cindent", 1, 1, f_cindent},
7046 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007047#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007048 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007049 {"complete_add", 1, 1, f_complete_add},
7050 {"complete_check", 0, 0, f_complete_check},
7051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007053 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007054 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007056 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007057 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"delete", 1, 1, f_delete},
7059 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007060 {"diff_filler", 1, 1, f_diff_filler},
7061 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007062 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007064 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"eventhandler", 0, 0, f_eventhandler},
7066 {"executable", 1, 1, f_executable},
7067 {"exists", 1, 1, f_exists},
7068 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007069 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007070 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7072 {"filereadable", 1, 1, f_filereadable},
7073 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007074 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007075 {"finddir", 1, 3, f_finddir},
7076 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {"fnamemodify", 2, 2, f_fnamemodify},
7078 {"foldclosed", 1, 1, f_foldclosed},
7079 {"foldclosedend", 1, 1, f_foldclosedend},
7080 {"foldlevel", 1, 1, f_foldlevel},
7081 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007082 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007084 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007086 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007087 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"getbufvar", 2, 2, f_getbufvar},
7089 {"getchar", 0, 1, f_getchar},
7090 {"getcharmod", 0, 0, f_getcharmod},
7091 {"getcmdline", 0, 0, f_getcmdline},
7092 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007093 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007095 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007096 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"getfsize", 1, 1, f_getfsize},
7098 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007099 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007100 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007101 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007102 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007103 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007104 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007106 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {"getwinposx", 0, 0, f_getwinposx},
7108 {"getwinposy", 0, 0, f_getwinposy},
7109 {"getwinvar", 2, 2, f_getwinvar},
7110 {"glob", 1, 1, f_glob},
7111 {"globpath", 2, 2, f_globpath},
7112 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007114 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007115 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7117 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7118 {"histadd", 2, 2, f_histadd},
7119 {"histdel", 1, 2, f_histdel},
7120 {"histget", 1, 2, f_histget},
7121 {"histnr", 1, 1, f_histnr},
7122 {"hlID", 1, 1, f_hlID},
7123 {"hlexists", 1, 1, f_hlexists},
7124 {"hostname", 0, 0, f_hostname},
7125 {"iconv", 3, 3, f_iconv},
7126 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007127 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007128 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007130 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"inputrestore", 0, 0, f_inputrestore},
7132 {"inputsave", 0, 0, f_inputsave},
7133 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007134 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007136 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007138 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"libcall", 3, 3, f_libcall},
7143 {"libcallnr", 3, 3, f_libcallnr},
7144 {"line", 1, 1, f_line},
7145 {"line2byte", 1, 1, f_line2byte},
7146 {"lispindent", 1, 1, f_lispindent},
7147 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007148 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007149 {"maparg", 1, 3, f_maparg},
7150 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007151 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007152 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007153 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007154 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007155 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007156 {"max", 1, 1, f_max},
7157 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007158#ifdef vim_mkdir
7159 {"mkdir", 1, 3, f_mkdir},
7160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"mode", 0, 0, f_mode},
7162 {"nextnonblank", 1, 1, f_nextnonblank},
7163 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007164 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007166 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007167 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007169 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007170 {"reltime", 0, 2, f_reltime},
7171 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"remote_expr", 2, 3, f_remote_expr},
7173 {"remote_foreground", 1, 1, f_remote_foreground},
7174 {"remote_peek", 1, 2, f_remote_peek},
7175 {"remote_read", 1, 1, f_remote_read},
7176 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007177 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007179 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007181 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007182 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007183 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007184 {"searchpair", 3, 6, f_searchpair},
7185 {"searchpairpos", 3, 6, f_searchpairpos},
7186 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {"server2client", 2, 2, f_server2client},
7188 {"serverlist", 0, 0, f_serverlist},
7189 {"setbufvar", 3, 3, f_setbufvar},
7190 {"setcmdpos", 1, 1, f_setcmdpos},
7191 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007192 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007193 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007194 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007196 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007198 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007200 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007201 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007202 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007203 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007204 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007205 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#ifdef HAVE_STRFTIME
7207 {"strftime", 1, 2, f_strftime},
7208#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007210 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"strlen", 1, 1, f_strlen},
7212 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007213 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {"strtrans", 1, 1, f_strtrans},
7215 {"submatch", 1, 1, f_submatch},
7216 {"substitute", 4, 4, f_substitute},
7217 {"synID", 3, 3, f_synID},
7218 {"synIDattr", 2, 3, f_synIDattr},
7219 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007220 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007221 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007222 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007223 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007224 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007225 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007227 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {"tolower", 1, 1, f_tolower},
7229 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007230 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"virtcol", 1, 1, f_virtcol},
7234 {"visualmode", 0, 1, f_visualmode},
7235 {"winbufnr", 1, 1, f_winbufnr},
7236 {"wincol", 0, 0, f_wincol},
7237 {"winheight", 1, 1, f_winheight},
7238 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007239 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007241 {"winrestview", 1, 1, f_winrestview},
7242 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007244 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245};
7246
7247#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7248
7249/*
7250 * Function given to ExpandGeneric() to obtain the list of internal
7251 * or user defined function names.
7252 */
7253 char_u *
7254get_function_name(xp, idx)
7255 expand_T *xp;
7256 int idx;
7257{
7258 static int intidx = -1;
7259 char_u *name;
7260
7261 if (idx == 0)
7262 intidx = -1;
7263 if (intidx < 0)
7264 {
7265 name = get_user_func_name(xp, idx);
7266 if (name != NULL)
7267 return name;
7268 }
7269 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7270 {
7271 STRCPY(IObuff, functions[intidx].f_name);
7272 STRCAT(IObuff, "(");
7273 if (functions[intidx].f_max_argc == 0)
7274 STRCAT(IObuff, ")");
7275 return IObuff;
7276 }
7277
7278 return NULL;
7279}
7280
7281/*
7282 * Function given to ExpandGeneric() to obtain the list of internal or
7283 * user defined variable or function names.
7284 */
7285/*ARGSUSED*/
7286 char_u *
7287get_expr_name(xp, idx)
7288 expand_T *xp;
7289 int idx;
7290{
7291 static int intidx = -1;
7292 char_u *name;
7293
7294 if (idx == 0)
7295 intidx = -1;
7296 if (intidx < 0)
7297 {
7298 name = get_function_name(xp, idx);
7299 if (name != NULL)
7300 return name;
7301 }
7302 return get_user_var_name(xp, ++intidx);
7303}
7304
7305#endif /* FEAT_CMDL_COMPL */
7306
7307/*
7308 * Find internal function in table above.
7309 * Return index, or -1 if not found
7310 */
7311 static int
7312find_internal_func(name)
7313 char_u *name; /* name of the function */
7314{
7315 int first = 0;
7316 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7317 int cmp;
7318 int x;
7319
7320 /*
7321 * Find the function name in the table. Binary search.
7322 */
7323 while (first <= last)
7324 {
7325 x = first + ((unsigned)(last - first) >> 1);
7326 cmp = STRCMP(name, functions[x].f_name);
7327 if (cmp < 0)
7328 last = x - 1;
7329 else if (cmp > 0)
7330 first = x + 1;
7331 else
7332 return x;
7333 }
7334 return -1;
7335}
7336
7337/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007338 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7339 * name it contains, otherwise return "name".
7340 */
7341 static char_u *
7342deref_func_name(name, lenp)
7343 char_u *name;
7344 int *lenp;
7345{
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007347 int cc;
7348
7349 cc = name[*lenp];
7350 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007351 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007352 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007354 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007356 {
7357 *lenp = 0;
7358 return (char_u *)""; /* just in case */
7359 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007360 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007362 }
7363
7364 return name;
7365}
7366
7367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 * Allocate a variable for the result of a function.
7369 * Return OK or FAIL.
7370 */
7371 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7373 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 char_u *name; /* name of the function */
7375 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 char_u **arg; /* argument, pointing to the '(' */
7378 linenr_T firstline; /* first line of range */
7379 linenr_T lastline; /* last line of range */
7380 int *doesrange; /* return: function handled range */
7381 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 char_u *argp;
7385 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007386 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 int argcount = 0; /* number of arguments found */
7388
7389 /*
7390 * Get the arguments.
7391 */
7392 argp = *arg;
7393 while (argcount < MAX_FUNC_ARGS)
7394 {
7395 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7396 if (*argp == ')' || *argp == ',' || *argp == NUL)
7397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7399 {
7400 ret = FAIL;
7401 break;
7402 }
7403 ++argcount;
7404 if (*argp != ',')
7405 break;
7406 }
7407 if (*argp == ')')
7408 ++argp;
7409 else
7410 ret = FAIL;
7411
7412 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 {
7417 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 *arg = skipwhite(argp);
7427 return ret;
7428}
7429
7430
7431/*
7432 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007433 * Return OK when the function can't be called, FAIL otherwise.
7434 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 */
7436 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007438 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 char_u *name; /* name of the function */
7440 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007443 typval_T *argvars; /* vars for arguments, must have "argcount"
7444 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 linenr_T firstline; /* first line of range */
7446 linenr_T lastline; /* last line of range */
7447 int *doesrange; /* return: function handled range */
7448 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450{
7451 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452#define ERROR_UNKNOWN 0
7453#define ERROR_TOOMANY 1
7454#define ERROR_TOOFEW 2
7455#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456#define ERROR_DICT 4
7457#define ERROR_NONE 5
7458#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 int error = ERROR_NONE;
7460 int i;
7461 int llen;
7462 ufunc_T *fp;
7463 int cc;
7464#define FLEN_FIXED 40
7465 char_u fname_buf[FLEN_FIXED + 1];
7466 char_u *fname;
7467
7468 /*
7469 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7470 * Change <SNR>123_name() to K_SNR 123_name().
7471 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7472 */
7473 cc = name[len];
7474 name[len] = NUL;
7475 llen = eval_fname_script(name);
7476 if (llen > 0)
7477 {
7478 fname_buf[0] = K_SPECIAL;
7479 fname_buf[1] = KS_EXTRA;
7480 fname_buf[2] = (int)KE_SNR;
7481 i = 3;
7482 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7483 {
7484 if (current_SID <= 0)
7485 error = ERROR_SCRIPT;
7486 else
7487 {
7488 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7489 i = (int)STRLEN(fname_buf);
7490 }
7491 }
7492 if (i + STRLEN(name + llen) < FLEN_FIXED)
7493 {
7494 STRCPY(fname_buf + i, name + llen);
7495 fname = fname_buf;
7496 }
7497 else
7498 {
7499 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7500 if (fname == NULL)
7501 error = ERROR_OTHER;
7502 else
7503 {
7504 mch_memmove(fname, fname_buf, (size_t)i);
7505 STRCPY(fname + i, name + llen);
7506 }
7507 }
7508 }
7509 else
7510 fname = name;
7511
7512 *doesrange = FALSE;
7513
7514
7515 /* execute the function if no errors detected and executing */
7516 if (evaluate && error == ERROR_NONE)
7517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007518 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 error = ERROR_UNKNOWN;
7520
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007521 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {
7523 /*
7524 * User defined function.
7525 */
7526 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007529 /* Trigger FuncUndefined event, may load the function. */
7530 if (fp == NULL
7531 && apply_autocmds(EVENT_FUNCUNDEFINED,
7532 fname, fname, TRUE, NULL)
7533 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007535 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 fp = find_func(fname);
7537 }
7538#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007539 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007540 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007541 {
7542 /* loaded a package, search for the function again */
7543 fp = find_func(fname);
7544 }
7545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (fp != NULL)
7547 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007548 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007550 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007552 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007554 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007555 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 else
7557 {
7558 /*
7559 * Call the user function.
7560 * Save and restore search patterns, script variables and
7561 * redo buffer.
7562 */
7563 save_search_patterns();
7564 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007565 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007566 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007568 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7569 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7570 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007571 /* Function was unreferenced while being used, free it
7572 * now. */
7573 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 restoreRedobuff();
7575 restore_search_patterns();
7576 error = ERROR_NONE;
7577 }
7578 }
7579 }
7580 else
7581 {
7582 /*
7583 * Find the function name in the table, call its implementation.
7584 */
7585 i = find_internal_func(fname);
7586 if (i >= 0)
7587 {
7588 if (argcount < functions[i].f_min_argc)
7589 error = ERROR_TOOFEW;
7590 else if (argcount > functions[i].f_max_argc)
7591 error = ERROR_TOOMANY;
7592 else
7593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007594 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 error = ERROR_NONE;
7597 }
7598 }
7599 }
7600 /*
7601 * The function call (or "FuncUndefined" autocommand sequence) might
7602 * have been aborted by an error, an interrupt, or an explicitly thrown
7603 * exception that has not been caught so far. This situation can be
7604 * tested for by calling aborting(). For an error in an internal
7605 * function or for the "E132" error in call_user_func(), however, the
7606 * throw point at which the "force_abort" flag (temporarily reset by
7607 * emsg()) is normally updated has not been reached yet. We need to
7608 * update that flag first to make aborting() reliable.
7609 */
7610 update_force_abort();
7611 }
7612 if (error == ERROR_NONE)
7613 ret = OK;
7614
7615 /*
7616 * Report an error unless the argument evaluation or function call has been
7617 * cancelled due to an aborting error, an interrupt, or an exception.
7618 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 if (!aborting())
7620 {
7621 switch (error)
7622 {
7623 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007624 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 break;
7626 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007627 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 break;
7629 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007630 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 name);
7632 break;
7633 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007634 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 name);
7636 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007638 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 name);
7640 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 }
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644 name[len] = cc;
7645 if (fname != name && fname != fname_buf)
7646 vim_free(fname);
7647
7648 return ret;
7649}
7650
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007651/*
7652 * Give an error message with a function name. Handle <SNR> things.
7653 */
7654 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007655emsg_funcname(ermsg, name)
7656 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007657 char_u *name;
7658{
7659 char_u *p;
7660
7661 if (*name == K_SPECIAL)
7662 p = concat_str((char_u *)"<SNR>", name + 3);
7663 else
7664 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007665 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007666 if (p != name)
7667 vim_free(p);
7668}
7669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670/*********************************************
7671 * Implementation of the built-in functions
7672 */
7673
7674/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007675 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 */
7677 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007678f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007684 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007685 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007687 if ((l = argvars[0].vval.v_list) != NULL
7688 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7689 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007691 }
7692 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007693 EMSG(_(e_listreq));
7694}
7695
7696/*
7697 * "append(lnum, string/list)" function
7698 */
7699 static void
7700f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T *argvars;
7702 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007703{
7704 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007705 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 list_T *l = NULL;
7707 listitem_T *li = NULL;
7708 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007709 long added = 0;
7710
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711 lnum = get_tv_lnum(argvars);
7712 if (lnum >= 0
7713 && lnum <= curbuf->b_ml.ml_line_count
7714 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007716 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007718 l = argvars[1].vval.v_list;
7719 if (l == NULL)
7720 return;
7721 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007723 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007724 for (;;)
7725 {
7726 if (l == NULL)
7727 tv = &argvars[1]; /* append a string */
7728 else if (li == NULL)
7729 break; /* end of list */
7730 else
7731 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007732 line = get_tv_string_chk(tv);
7733 if (line == NULL) /* type error */
7734 {
7735 rettv->vval.v_number = 1; /* Failed */
7736 break;
7737 }
7738 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007739 ++added;
7740 if (l == NULL)
7741 break;
7742 li = li->li_next;
7743 }
7744
7745 appended_lines_mark(lnum, added);
7746 if (curwin->w_cursor.lnum > lnum)
7747 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 else
7750 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751}
7752
7753/*
7754 * "argc()" function
7755 */
7756/* ARGSUSED */
7757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 typval_T *argvars;
7760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764
7765/*
7766 * "argidx()" function
7767 */
7768/* ARGSUSED */
7769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 typval_T *argvars;
7772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
7777/*
7778 * "argv(nr)" function
7779 */
7780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 typval_T *argvars;
7783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 int idx;
7786
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007787 if (argvars[0].v_type != VAR_UNKNOWN)
7788 {
7789 idx = get_tv_number_chk(&argvars[0], NULL);
7790 if (idx >= 0 && idx < ARGCOUNT)
7791 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7792 else
7793 rettv->vval.v_string = NULL;
7794 rettv->v_type = VAR_STRING;
7795 }
7796 else if (rettv_list_alloc(rettv) == OK)
7797 for (idx = 0; idx < ARGCOUNT; ++idx)
7798 list_append_string(rettv->vval.v_list,
7799 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800}
7801
7802/*
7803 * "browse(save, title, initdir, default)" function
7804 */
7805/* ARGSUSED */
7806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *argvars;
7809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811#ifdef FEAT_BROWSE
7812 int save;
7813 char_u *title;
7814 char_u *initdir;
7815 char_u *defname;
7816 char_u buf[NUMBUFLEN];
7817 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007820 save = get_tv_number_chk(&argvars[0], &error);
7821 title = get_tv_string_chk(&argvars[1]);
7822 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7823 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007825 if (error || title == NULL || initdir == NULL || defname == NULL)
7826 rettv->vval.v_string = NULL;
7827 else
7828 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007829 do_browse(save ? BROWSE_SAVE : 0,
7830 title, defname, NULL, initdir, NULL, curbuf);
7831#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007833#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007835}
7836
7837/*
7838 * "browsedir(title, initdir)" function
7839 */
7840/* ARGSUSED */
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 typval_T *argvars;
7844 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007845{
7846#ifdef FEAT_BROWSE
7847 char_u *title;
7848 char_u *initdir;
7849 char_u buf[NUMBUFLEN];
7850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007851 title = get_tv_string_chk(&argvars[0]);
7852 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 if (title == NULL || initdir == NULL)
7855 rettv->vval.v_string = NULL;
7856 else
7857 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007858 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
Bram Moolenaar33570922005-01-25 22:26:29 +00007865static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007866
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867/*
7868 * Find a buffer by number or exact name.
7869 */
7870 static buf_T *
7871find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876 if (avar->v_type == VAR_NUMBER)
7877 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007878 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007881 if (buf == NULL)
7882 {
7883 /* No full path name match, try a match with a URL or a "nofile"
7884 * buffer, these don't use the full path. */
7885 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7886 if (buf->b_fname != NULL
7887 && (path_with_url(buf->b_fname)
7888#ifdef FEAT_QUICKFIX
7889 || bt_nofile(buf)
7890#endif
7891 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007893 break;
7894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 return buf;
7897}
7898
7899/*
7900 * "bufexists(expr)" function
7901 */
7902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *argvars;
7905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007907 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908}
7909
7910/*
7911 * "buflisted(expr)" function
7912 */
7913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007914f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 typval_T *argvars;
7916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
7918 buf_T *buf;
7919
7920 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922}
7923
7924/*
7925 * "bufloaded(expr)" function
7926 */
7927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 typval_T *argvars;
7930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 buf_T *buf;
7933
7934 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
Bram Moolenaar33570922005-01-25 22:26:29 +00007938static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940/*
7941 * Get buffer by number or pattern.
7942 */
7943 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 int save_magic;
7949 char_u *save_cpo;
7950 buf_T *buf;
7951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 if (tv->v_type == VAR_NUMBER)
7953 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007954 if (tv->v_type != VAR_STRING)
7955 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (name == NULL || *name == NUL)
7957 return curbuf;
7958 if (name[0] == '$' && name[1] == NUL)
7959 return lastbuf;
7960
7961 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7962 save_magic = p_magic;
7963 p_magic = TRUE;
7964 save_cpo = p_cpo;
7965 p_cpo = (char_u *)"";
7966
7967 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7968 TRUE, FALSE));
7969
7970 p_magic = save_magic;
7971 p_cpo = save_cpo;
7972
7973 /* If not found, try expanding the name, like done for bufexists(). */
7974 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
7977 return buf;
7978}
7979
7980/*
7981 * "bufname(expr)" function
7982 */
7983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 typval_T *argvars;
7986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 buf_T *buf;
7989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007990 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 buf = get_buf_tv(&argvars[0]);
7993 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 --emsg_off;
7999}
8000
8001/*
8002 * "bufnr(expr)" function
8003 */
8004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 typval_T *argvars;
8007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008010 int error = FALSE;
8011 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008016 --emsg_off;
8017
8018 /* If the buffer isn't found and the second argument is not zero create a
8019 * new buffer. */
8020 if (buf == NULL
8021 && argvars[1].v_type != VAR_UNKNOWN
8022 && get_tv_number_chk(&argvars[1], &error) != 0
8023 && !error
8024 && (name = get_tv_string_chk(&argvars[0])) != NULL
8025 && !error)
8026 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032}
8033
8034/*
8035 * "bufwinnr(nr)" function
8036 */
8037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 typval_T *argvars;
8040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042#ifdef FEAT_WINDOWS
8043 win_T *wp;
8044 int winnr = 0;
8045#endif
8046 buf_T *buf;
8047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051#ifdef FEAT_WINDOWS
8052 for (wp = firstwin; wp; wp = wp->w_next)
8053 {
8054 ++winnr;
8055 if (wp->w_buffer == buf)
8056 break;
8057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008060 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061#endif
8062 --emsg_off;
8063}
8064
8065/*
8066 * "byte2line(byte)" function
8067 */
8068/*ARGSUSED*/
8069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070f_byte2line(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#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076#else
8077 long boff = 0;
8078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 (linenr_T)0, &boff);
8085#endif
8086}
8087
8088/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008089 * "byteidx()" function
8090 */
8091/*ARGSUSED*/
8092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 typval_T *argvars;
8095 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008096{
8097#ifdef FEAT_MBYTE
8098 char_u *t;
8099#endif
8100 char_u *str;
8101 long idx;
8102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 str = get_tv_string_chk(&argvars[0]);
8104 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008107 return;
8108
8109#ifdef FEAT_MBYTE
8110 t = str;
8111 for ( ; idx > 0; idx--)
8112 {
8113 if (*t == NUL) /* EOL reached */
8114 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008115 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008116 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008117 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008118#else
8119 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121#endif
8122}
8123
8124/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 * "call(func, arglist)" function
8126 */
8127 static void
8128f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 typval_T *argvars;
8130 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008131{
8132 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008133 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008134 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008136 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008138
8139 rettv->vval.v_number = 0;
8140 if (argvars[1].v_type != VAR_LIST)
8141 {
8142 EMSG(_(e_listreq));
8143 return;
8144 }
8145 if (argvars[1].vval.v_list == NULL)
8146 return;
8147
8148 if (argvars[0].v_type == VAR_FUNC)
8149 func = argvars[0].vval.v_string;
8150 else
8151 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 if (*func == NUL)
8153 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008154
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155 if (argvars[2].v_type != VAR_UNKNOWN)
8156 {
8157 if (argvars[2].v_type != VAR_DICT)
8158 {
8159 EMSG(_(e_dictreq));
8160 return;
8161 }
8162 selfdict = argvars[2].vval.v_dict;
8163 }
8164
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008165 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8166 item = item->li_next)
8167 {
8168 if (argc == MAX_FUNC_ARGS)
8169 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008170 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008171 break;
8172 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008173 /* Make a copy of each argument. This is needed to be able to set
8174 * v_lock to VAR_FIXED in the copy without changing the original list.
8175 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008176 copy_tv(&item->li_tv, &argv[argc++]);
8177 }
8178
8179 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008180 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008181 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8182 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008183
8184 /* Free the arguments. */
8185 while (argc > 0)
8186 clear_tv(&argv[--argc]);
8187}
8188
8189/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008190 * "changenr()" function
8191 */
8192/*ARGSUSED*/
8193 static void
8194f_changenr(argvars, rettv)
8195 typval_T *argvars;
8196 typval_T *rettv;
8197{
8198 rettv->vval.v_number = curbuf->b_u_seq_cur;
8199}
8200
8201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 * "char2nr(string)" function
8203 */
8204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008205f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 typval_T *argvars;
8207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209#ifdef FEAT_MBYTE
8210 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 else
8213#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215}
8216
8217/*
8218 * "cindent(lnum)" function
8219 */
8220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008221f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 typval_T *argvars;
8223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225#ifdef FEAT_CINDENT
8226 pos_T pos;
8227 linenr_T lnum;
8228
8229 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8232 {
8233 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 curwin->w_cursor = pos;
8236 }
8237 else
8238#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240}
8241
8242/*
8243 * "col(string)" function
8244 */
8245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 typval_T *argvars;
8248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249{
8250 colnr_T col = 0;
8251 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008252 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008254 fp = var2fpos(&argvars[0], FALSE, &fnum);
8255 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 if (fp->col == MAXCOL)
8258 {
8259 /* '> can be MAXCOL, get the length of the line then */
8260 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008261 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 else
8263 col = MAXCOL;
8264 }
8265 else
8266 {
8267 col = fp->col + 1;
8268#ifdef FEAT_VIRTUALEDIT
8269 /* col(".") when the cursor is on the NUL at the end of the line
8270 * because of "coladd" can be seen as an extra column. */
8271 if (virtual_active() && fp == &curwin->w_cursor)
8272 {
8273 char_u *p = ml_get_cursor();
8274
8275 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8276 curwin->w_virtcol - curwin->w_cursor.coladd))
8277 {
8278# ifdef FEAT_MBYTE
8279 int l;
8280
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008281 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 col += l;
8283# else
8284 if (*p != NUL && p[1] == NUL)
8285 ++col;
8286# endif
8287 }
8288 }
8289#endif
8290 }
8291 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
Bram Moolenaar572cb562005-08-05 21:35:02 +00008295#if defined(FEAT_INS_EXPAND)
8296/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008297 * "complete()" function
8298 */
8299/*ARGSUSED*/
8300 static void
8301f_complete(argvars, rettv)
8302 typval_T *argvars;
8303 typval_T *rettv;
8304{
8305 int startcol;
8306
8307 if ((State & INSERT) == 0)
8308 {
8309 EMSG(_("E785: complete() can only be used in Insert mode"));
8310 return;
8311 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008312
8313 /* Check for undo allowed here, because if something was already inserted
8314 * the line was already saved for undo and this check isn't done. */
8315 if (!undo_allowed())
8316 return;
8317
Bram Moolenaarade00832006-03-10 21:46:58 +00008318 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8319 {
8320 EMSG(_(e_invarg));
8321 return;
8322 }
8323
8324 startcol = get_tv_number_chk(&argvars[0], NULL);
8325 if (startcol <= 0)
8326 return;
8327
8328 set_completion(startcol - 1, argvars[1].vval.v_list);
8329}
8330
8331/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008332 * "complete_add()" function
8333 */
8334/*ARGSUSED*/
8335 static void
8336f_complete_add(argvars, rettv)
8337 typval_T *argvars;
8338 typval_T *rettv;
8339{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008340 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008341}
8342
8343/*
8344 * "complete_check()" function
8345 */
8346/*ARGSUSED*/
8347 static void
8348f_complete_check(argvars, rettv)
8349 typval_T *argvars;
8350 typval_T *rettv;
8351{
8352 int saved = RedrawingDisabled;
8353
8354 RedrawingDisabled = 0;
8355 ins_compl_check_keys(0);
8356 rettv->vval.v_number = compl_interrupted;
8357 RedrawingDisabled = saved;
8358}
8359#endif
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361/*
8362 * "confirm(message, buttons[, default [, type]])" function
8363 */
8364/*ARGSUSED*/
8365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008367 typval_T *argvars;
8368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8371 char_u *message;
8372 char_u *buttons = NULL;
8373 char_u buf[NUMBUFLEN];
8374 char_u buf2[NUMBUFLEN];
8375 int def = 1;
8376 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377 char_u *typestr;
8378 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008380 message = get_tv_string_chk(&argvars[0]);
8381 if (message == NULL)
8382 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8386 if (buttons == NULL)
8387 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008390 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008391 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008393 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8394 if (typestr == NULL)
8395 error = TRUE;
8396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398 switch (TOUPPER_ASC(*typestr))
8399 {
8400 case 'E': type = VIM_ERROR; break;
8401 case 'Q': type = VIM_QUESTION; break;
8402 case 'I': type = VIM_INFO; break;
8403 case 'W': type = VIM_WARNING; break;
8404 case 'G': type = VIM_GENERIC; break;
8405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 }
8408 }
8409 }
8410
8411 if (buttons == NULL || *buttons == NUL)
8412 buttons = (char_u *)_("&Ok");
8413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 if (error)
8415 rettv->vval.v_number = 0;
8416 else
8417 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 def, NULL);
8419#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008420 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#endif
8422}
8423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424/*
8425 * "copy()" function
8426 */
8427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 typval_T *argvars;
8430 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008431{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008432 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
8435/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008436 * "count()" function
8437 */
8438 static void
8439f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 typval_T *argvars;
8441 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008442{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008443 long n = 0;
8444 int ic = FALSE;
8445
Bram Moolenaare9a41262005-01-15 22:18:47 +00008446 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008447 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 listitem_T *li;
8449 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008451
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 if ((l = argvars[0].vval.v_list) != NULL)
8453 {
8454 li = l->lv_first;
8455 if (argvars[2].v_type != VAR_UNKNOWN)
8456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008457 int error = FALSE;
8458
8459 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008460 if (argvars[3].v_type != VAR_UNKNOWN)
8461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 idx = get_tv_number_chk(&argvars[3], &error);
8463 if (!error)
8464 {
8465 li = list_find(l, idx);
8466 if (li == NULL)
8467 EMSGN(_(e_listidx), idx);
8468 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470 if (error)
8471 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 }
8473
8474 for ( ; li != NULL; li = li->li_next)
8475 if (tv_equal(&li->li_tv, &argvars[1], ic))
8476 ++n;
8477 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008478 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 else if (argvars[0].v_type == VAR_DICT)
8480 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 int todo;
8482 dict_T *d;
8483 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008485 if ((d = argvars[0].vval.v_dict) != NULL)
8486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 int error = FALSE;
8488
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 if (argvars[2].v_type != VAR_UNKNOWN)
8490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008492 if (argvars[3].v_type != VAR_UNKNOWN)
8493 EMSG(_(e_invarg));
8494 }
8495
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008496 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008498 {
8499 if (!HASHITEM_EMPTY(hi))
8500 {
8501 --todo;
8502 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8503 ++n;
8504 }
8505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 }
8507 }
8508 else
8509 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008510 rettv->vval.v_number = n;
8511}
8512
8513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8515 *
8516 * Checks the existence of a cscope connection.
8517 */
8518/*ARGSUSED*/
8519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 typval_T *argvars;
8522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524#ifdef FEAT_CSCOPE
8525 int num = 0;
8526 char_u *dbpath = NULL;
8527 char_u *prepend = NULL;
8528 char_u buf[NUMBUFLEN];
8529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 if (argvars[0].v_type != VAR_UNKNOWN
8531 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 num = (int)get_tv_number(&argvars[0]);
8534 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
8538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542#endif
8543}
8544
8545/*
8546 * "cursor(lnum, col)" function
8547 *
8548 * Moves the cursor to the specified line and column
8549 */
8550/*ARGSUSED*/
8551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 typval_T *argvars;
8554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008557#ifdef FEAT_VIRTUALEDIT
8558 long coladd = 0;
8559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560
Bram Moolenaara5525202006-03-02 22:52:09 +00008561 if (argvars[1].v_type == VAR_UNKNOWN)
8562 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008563 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008564
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008565 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008566 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008567 line = pos.lnum;
8568 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008569#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008570 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008571#endif
8572 }
8573 else
8574 {
8575 line = get_tv_lnum(argvars);
8576 col = get_tv_number_chk(&argvars[1], NULL);
8577#ifdef FEAT_VIRTUALEDIT
8578 if (argvars[2].v_type != VAR_UNKNOWN)
8579 coladd = get_tv_number_chk(&argvars[2], NULL);
8580#endif
8581 }
8582 if (line < 0 || col < 0
8583#ifdef FEAT_VIRTUALEDIT
8584 || coladd < 0
8585#endif
8586 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008587 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (line > 0)
8589 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 if (col > 0)
8591 curwin->w_cursor.col = col - 1;
8592#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008593 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#endif
8595
8596 /* Make sure the cursor is in a valid position. */
8597 check_cursor();
8598#ifdef FEAT_MBYTE
8599 /* Correct cursor for multi-byte character. */
8600 if (has_mbyte)
8601 mb_adjust_cursor();
8602#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008603
8604 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605}
8606
8607/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 */
8610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 typval_T *argvars;
8613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008615 int noref = 0;
8616
8617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008619 if (noref < 0 || noref > 1)
8620 EMSG(_(e_invarg));
8621 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008622 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623}
8624
8625/*
8626 * "delete()" function
8627 */
8628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *argvars;
8631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637}
8638
8639/*
8640 * "did_filetype()" function
8641 */
8642/*ARGSUSED*/
8643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 typval_T *argvars;
8646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#endif
8653}
8654
8655/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008656 * "diff_filler()" function
8657 */
8658/*ARGSUSED*/
8659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 typval_T *argvars;
8662 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008663{
8664#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008666#endif
8667}
8668
8669/*
8670 * "diff_hlID()" function
8671 */
8672/*ARGSUSED*/
8673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 typval_T *argvars;
8676 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008677{
8678#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008680 static linenr_T prev_lnum = 0;
8681 static int changedtick = 0;
8682 static int fnum = 0;
8683 static int change_start = 0;
8684 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008685 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008686 int filler_lines;
8687 int col;
8688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689 if (lnum < 0) /* ignore type error in {lnum} arg */
8690 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008691 if (lnum != prev_lnum
8692 || changedtick != curbuf->b_changedtick
8693 || fnum != curbuf->b_fnum)
8694 {
8695 /* New line, buffer, change: need to get the values. */
8696 filler_lines = diff_check(curwin, lnum);
8697 if (filler_lines < 0)
8698 {
8699 if (filler_lines == -1)
8700 {
8701 change_start = MAXCOL;
8702 change_end = -1;
8703 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8704 hlID = HLF_ADD; /* added line */
8705 else
8706 hlID = HLF_CHD; /* changed line */
8707 }
8708 else
8709 hlID = HLF_ADD; /* added line */
8710 }
8711 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008712 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008713 prev_lnum = lnum;
8714 changedtick = curbuf->b_changedtick;
8715 fnum = curbuf->b_fnum;
8716 }
8717
8718 if (hlID == HLF_CHD || hlID == HLF_TXD)
8719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008721 if (col >= change_start && col <= change_end)
8722 hlID = HLF_TXD; /* changed text */
8723 else
8724 hlID = HLF_CHD; /* changed line */
8725 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008726 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008727#endif
8728}
8729
8730/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008731 * "empty({expr})" function
8732 */
8733 static void
8734f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *argvars;
8736 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008737{
8738 int n;
8739
8740 switch (argvars[0].v_type)
8741 {
8742 case VAR_STRING:
8743 case VAR_FUNC:
8744 n = argvars[0].vval.v_string == NULL
8745 || *argvars[0].vval.v_string == NUL;
8746 break;
8747 case VAR_NUMBER:
8748 n = argvars[0].vval.v_number == 0;
8749 break;
8750 case VAR_LIST:
8751 n = argvars[0].vval.v_list == NULL
8752 || argvars[0].vval.v_list->lv_first == NULL;
8753 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 case VAR_DICT:
8755 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008758 default:
8759 EMSG2(_(e_intern2), "f_empty()");
8760 n = 0;
8761 }
8762
8763 rettv->vval.v_number = n;
8764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * "escape({string}, {chars})" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 char_u buf[NUMBUFLEN];
8775
Bram Moolenaar758711c2005-02-02 23:11:38 +00008776 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8777 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
8781/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008782 * "eval()" function
8783 */
8784/*ARGSUSED*/
8785 static void
8786f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *argvars;
8788 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008789{
8790 char_u *s;
8791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008792 s = get_tv_string_chk(&argvars[0]);
8793 if (s != NULL)
8794 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8797 {
8798 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008799 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008801 else if (*s != NUL)
8802 EMSG(_(e_trailing));
8803}
8804
8805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 * "eventhandler()" function
8807 */
8808/*ARGSUSED*/
8809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *argvars;
8812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "executable()" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
8829 * "exists()" function
8830 */
8831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *argvars;
8834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 char_u *p;
8837 char_u *name;
8838 int n = FALSE;
8839 int len = 0;
8840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (*p == '$') /* environment variable */
8843 {
8844 /* first try "normal" environment variables (fast) */
8845 if (mch_getenv(p + 1) != NULL)
8846 n = TRUE;
8847 else
8848 {
8849 /* try expanding things like $VIM and ${HOME} */
8850 p = expand_env_save(p);
8851 if (p != NULL && *p != '$')
8852 n = TRUE;
8853 vim_free(p);
8854 }
8855 }
8856 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008859 if (*skipwhite(p) != NUL)
8860 n = FALSE; /* trailing garbage */
8861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 else if (*p == '*') /* internal or user defined function */
8863 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008864 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 else if (*p == ':')
8867 {
8868 n = cmd_exists(p + 1);
8869 }
8870 else if (*p == '#')
8871 {
8872#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008873 if (p[1] == '#')
8874 n = autocmd_supported(p + 2);
8875 else
8876 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877#endif
8878 }
8879 else /* internal variable */
8880 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008881 char_u *tofree;
8882 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008884 /* get_name_len() takes care of expanding curly braces */
8885 name = p;
8886 len = get_name_len(&p, &tofree, TRUE, FALSE);
8887 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008889 if (tofree != NULL)
8890 name = tofree;
8891 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8892 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008894 /* handle d.key, l[idx], f(expr) */
8895 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8896 if (n)
8897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008900 if (*p != NUL)
8901 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008903 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908
8909/*
8910 * "expand()" function
8911 */
8912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 typval_T *argvars;
8915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 char_u *s;
8918 int len;
8919 char_u *errormsg;
8920 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8921 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->v_type = VAR_STRING;
8925 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 if (*s == '%' || *s == '#' || *s == '<')
8927 {
8928 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008929 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 --emsg_off;
8931 }
8932 else
8933 {
8934 /* When the optional second argument is non-zero, don't remove matches
8935 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008936 if (argvars[1].v_type != VAR_UNKNOWN
8937 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 if (!error)
8940 {
8941 ExpandInit(&xpc);
8942 xpc.xp_context = EXPAND_FILES;
8943 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944 }
8945 else
8946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
8948}
8949
8950/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008951 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008953 */
8954 static void
8955f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008956 typval_T *argvars;
8957 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008958{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008959 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008961 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008962 list_T *l1, *l2;
8963 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008964 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008966
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 l1 = argvars[0].vval.v_list;
8968 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008969 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8970 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 {
8972 if (argvars[2].v_type != VAR_UNKNOWN)
8973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 before = get_tv_number_chk(&argvars[2], &error);
8975 if (error)
8976 return; /* type error; errmsg already given */
8977
Bram Moolenaar758711c2005-02-02 23:11:38 +00008978 if (before == l1->lv_len)
8979 item = NULL;
8980 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008981 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008982 item = list_find(l1, before);
8983 if (item == NULL)
8984 {
8985 EMSGN(_(e_listidx), before);
8986 return;
8987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008988 }
8989 }
8990 else
8991 item = NULL;
8992 list_extend(l1, l2, item);
8993
Bram Moolenaare9a41262005-01-15 22:18:47 +00008994 copy_tv(&argvars[0], rettv);
8995 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008997 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 dict_T *d1, *d2;
9000 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 char_u *action;
9002 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009004 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005
9006 d1 = argvars[0].vval.v_dict;
9007 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009008 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9009 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 {
9011 /* Check the third argument. */
9012 if (argvars[2].v_type != VAR_UNKNOWN)
9013 {
9014 static char *(av[]) = {"keep", "force", "error"};
9015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 action = get_tv_string_chk(&argvars[2]);
9017 if (action == NULL)
9018 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 for (i = 0; i < 3; ++i)
9020 if (STRCMP(action, av[i]) == 0)
9021 break;
9022 if (i == 3)
9023 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009024 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 return;
9026 }
9027 }
9028 else
9029 action = (char_u *)"force";
9030
9031 /* Go over all entries in the second dict and add them to the
9032 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009033 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009036 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009038 --todo;
9039 di1 = dict_find(d1, hi2->hi_key, -1);
9040 if (di1 == NULL)
9041 {
9042 di1 = dictitem_copy(HI2DI(hi2));
9043 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9044 dictitem_free(di1);
9045 }
9046 else if (*action == 'e')
9047 {
9048 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9049 break;
9050 }
9051 else if (*action == 'f')
9052 {
9053 clear_tv(&di1->di_tv);
9054 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9055 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 }
9057 }
9058
Bram Moolenaare9a41262005-01-15 22:18:47 +00009059 copy_tv(&argvars[0], rettv);
9060 }
9061 }
9062 else
9063 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009064}
9065
9066/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009067 * "feedkeys()" function
9068 */
9069/*ARGSUSED*/
9070 static void
9071f_feedkeys(argvars, rettv)
9072 typval_T *argvars;
9073 typval_T *rettv;
9074{
9075 int remap = TRUE;
9076 char_u *keys, *flags;
9077 char_u nbuf[NUMBUFLEN];
9078 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009079 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009080
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009081 /* This is not allowed in the sandbox. If the commands would still be
9082 * executed in the sandbox it would be OK, but it probably happens later,
9083 * when "sandbox" is no longer set. */
9084 if (check_secure())
9085 return;
9086
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009087 rettv->vval.v_number = 0;
9088 keys = get_tv_string(&argvars[0]);
9089 if (*keys != NUL)
9090 {
9091 if (argvars[1].v_type != VAR_UNKNOWN)
9092 {
9093 flags = get_tv_string_buf(&argvars[1], nbuf);
9094 for ( ; *flags != NUL; ++flags)
9095 {
9096 switch (*flags)
9097 {
9098 case 'n': remap = FALSE; break;
9099 case 'm': remap = TRUE; break;
9100 case 't': typed = TRUE; break;
9101 }
9102 }
9103 }
9104
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009105 /* Need to escape K_SPECIAL and CSI before putting the string in the
9106 * typeahead buffer. */
9107 keys_esc = vim_strsave_escape_csi(keys);
9108 if (keys_esc != NULL)
9109 {
9110 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009111 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009112 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009113 if (vgetc_busy)
9114 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009115 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009116 }
9117}
9118
9119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 * "filereadable()" function
9121 */
9122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 typval_T *argvars;
9125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
9127 FILE *fd;
9128 char_u *p;
9129 int n;
9130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9133 {
9134 n = TRUE;
9135 fclose(fd);
9136 }
9137 else
9138 n = FALSE;
9139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141}
9142
9143/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009144 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 * rights to write into.
9146 */
9147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009149 typval_T *argvars;
9150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009152 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153}
9154
Bram Moolenaar33570922005-01-25 22:26:29 +00009155static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009156
9157 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009158findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009159 typval_T *argvars;
9160 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009161 int dir;
9162{
9163#ifdef FEAT_SEARCHPATH
9164 char_u *fname;
9165 char_u *fresult = NULL;
9166 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9167 char_u *p;
9168 char_u pathbuf[NUMBUFLEN];
9169 int count = 1;
9170 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009171 int error = FALSE;
9172#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009173
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009174 rettv->vval.v_string = NULL;
9175 rettv->v_type = VAR_STRING;
9176
9177#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009179
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009180 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9183 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009184 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 else
9186 {
9187 if (*p != NUL)
9188 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009191 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009193 }
9194
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009195 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9196 error = TRUE;
9197
9198 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 do
9201 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009202 if (rettv->v_type == VAR_STRING)
9203 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 fresult = find_file_in_path_option(first ? fname : NULL,
9205 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009206 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009207 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009209
9210 if (fresult != NULL && rettv->v_type == VAR_LIST)
9211 list_append_string(rettv->vval.v_list, fresult, -1);
9212
9213 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009215
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009216 if (rettv->v_type == VAR_STRING)
9217 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009218#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009219}
9220
Bram Moolenaar33570922005-01-25 22:26:29 +00009221static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9222static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009223
9224/*
9225 * Implementation of map() and filter().
9226 */
9227 static void
9228filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 typval_T *argvars;
9230 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009231 int map;
9232{
9233 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009234 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009235 listitem_T *li, *nli;
9236 list_T *l = NULL;
9237 dictitem_T *di;
9238 hashtab_T *ht;
9239 hashitem_T *hi;
9240 dict_T *d = NULL;
9241 typval_T save_val;
9242 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009243 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009244 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009245 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009246 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009247
9248 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009249 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009250 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009251 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009252 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009253 return;
9254 }
9255 else if (argvars[0].v_type == VAR_DICT)
9256 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009257 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009258 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009259 return;
9260 }
9261 else
9262 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009263 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009264 return;
9265 }
9266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 expr = get_tv_string_buf_chk(&argvars[1], buf);
9268 /* On type errors, the preceding call has already displayed an error
9269 * message. Avoid a misleading error message for an empty string that
9270 * was not passed as argument. */
9271 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 prepare_vimvar(VV_VAL, &save_val);
9274 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009275
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009276 /* We reset "did_emsg" to be able to detect whether an error
9277 * occurred during evaluation of the expression. */
9278 save_did_emsg = did_emsg;
9279 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 prepare_vimvar(VV_KEY, &save_key);
9284 vimvars[VV_KEY].vv_type = VAR_STRING;
9285
9286 ht = &d->dv_hashtab;
9287 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009288 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 if (!HASHITEM_EMPTY(hi))
9292 {
9293 --todo;
9294 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009295 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 break;
9297 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009298 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009299 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 break;
9301 if (!map && rem)
9302 dictitem_remove(d, di);
9303 clear_tv(&vimvars[VV_KEY].vv_tv);
9304 }
9305 }
9306 hash_unlock(ht);
9307
9308 restore_vimvar(VV_KEY, &save_key);
9309 }
9310 else
9311 {
9312 for (li = l->lv_first; li != NULL; li = nli)
9313 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009314 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009315 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009316 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009317 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009318 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009319 break;
9320 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009322 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009326
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009327 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009329
9330 copy_tv(&argvars[0], rettv);
9331}
9332
9333 static int
9334filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009336 char_u *expr;
9337 int map;
9338 int *remp;
9339{
Bram Moolenaar33570922005-01-25 22:26:29 +00009340 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009341 char_u *s;
9342
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 s = expr;
9345 if (eval1(&s, &rettv, TRUE) == FAIL)
9346 return FAIL;
9347 if (*s != NUL) /* check for trailing chars after expr */
9348 {
9349 EMSG2(_(e_invexpr2), s);
9350 return FAIL;
9351 }
9352 if (map)
9353 {
9354 /* map(): replace the list item value */
9355 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009356 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009357 *tv = rettv;
9358 }
9359 else
9360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 int error = FALSE;
9362
Bram Moolenaare9a41262005-01-15 22:18:47 +00009363 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009364 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009365 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009366 /* On type error, nothing has been removed; return FAIL to stop the
9367 * loop. The error message was given by get_tv_number_chk(). */
9368 if (error)
9369 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009370 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009372 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009373}
9374
9375/*
9376 * "filter()" function
9377 */
9378 static void
9379f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *argvars;
9381 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009382{
9383 filter_map(argvars, rettv, FALSE);
9384}
9385
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009386/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009387 * "finddir({fname}[, {path}[, {count}]])" function
9388 */
9389 static void
9390f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009393{
9394 findfilendir(argvars, rettv, TRUE);
9395}
9396
9397/*
9398 * "findfile({fname}[, {path}[, {count}]])" function
9399 */
9400 static void
9401f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 typval_T *argvars;
9403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404{
9405 findfilendir(argvars, rettv, FALSE);
9406}
9407
9408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 * "fnamemodify({fname}, {mods})" function
9410 */
9411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *argvars;
9414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415{
9416 char_u *fname;
9417 char_u *mods;
9418 int usedlen = 0;
9419 int len;
9420 char_u *fbuf = NULL;
9421 char_u buf[NUMBUFLEN];
9422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 fname = get_tv_string_chk(&argvars[0]);
9424 mods = get_tv_string_buf_chk(&argvars[1], buf);
9425 if (fname == NULL || mods == NULL)
9426 fname = NULL;
9427 else
9428 {
9429 len = (int)STRLEN(fname);
9430 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 vim_free(fbuf);
9439}
9440
Bram Moolenaar33570922005-01-25 22:26:29 +00009441static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442
9443/*
9444 * "foldclosed()" function
9445 */
9446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 int end;
9451{
9452#ifdef FEAT_FOLDING
9453 linenr_T lnum;
9454 linenr_T first, last;
9455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9458 {
9459 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9460 {
9461 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 return;
9466 }
9467 }
9468#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471
9472/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009473 * "foldclosed()" function
9474 */
9475 static void
9476f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 typval_T *argvars;
9478 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009479{
9480 foldclosed_both(argvars, rettv, FALSE);
9481}
9482
9483/*
9484 * "foldclosedend()" function
9485 */
9486 static void
9487f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009488 typval_T *argvars;
9489 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009490{
9491 foldclosed_both(argvars, rettv, TRUE);
9492}
9493
9494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 * "foldlevel()" function
9496 */
9497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009499 typval_T *argvars;
9500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#ifdef FEAT_FOLDING
9503 linenr_T lnum;
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)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 else
9509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511}
9512
9513/*
9514 * "foldtext()" function
9515 */
9516/*ARGSUSED*/
9517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009519 typval_T *argvars;
9520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
9522#ifdef FEAT_FOLDING
9523 linenr_T lnum;
9524 char_u *s;
9525 char_u *r;
9526 int len;
9527 char *txt;
9528#endif
9529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->v_type = VAR_STRING;
9531 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009533 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9534 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9535 <= curbuf->b_ml.ml_line_count
9536 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
9538 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009539 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9540 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 {
9542 if (!linewhite(lnum))
9543 break;
9544 ++lnum;
9545 }
9546
9547 /* Find interesting text in this line. */
9548 s = skipwhite(ml_get(lnum));
9549 /* skip C comment-start */
9550 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009553 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009554 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009555 {
9556 s = skipwhite(ml_get(lnum + 1));
9557 if (*s == '*')
9558 s = skipwhite(s + 1);
9559 }
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 txt = _("+-%s%3ld lines: ");
9562 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009563 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 + 20 /* for %3ld */
9565 + STRLEN(s))); /* concatenated */
9566 if (r != NULL)
9567 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009568 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9569 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9570 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 len = (int)STRLEN(r);
9572 STRCAT(r, s);
9573 /* remove 'foldmarker' and 'commentstring' */
9574 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 }
9577 }
9578#endif
9579}
9580
9581/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009582 * "foldtextresult(lnum)" function
9583 */
9584/*ARGSUSED*/
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009589{
9590#ifdef FEAT_FOLDING
9591 linenr_T lnum;
9592 char_u *text;
9593 char_u buf[51];
9594 foldinfo_T foldinfo;
9595 int fold_count;
9596#endif
9597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
9599 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009600#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 /* treat illegal types and illegal string values for {lnum} the same */
9603 if (lnum < 0)
9604 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009605 fold_count = foldedCount(curwin, lnum, &foldinfo);
9606 if (fold_count > 0)
9607 {
9608 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9609 &foldinfo, buf);
9610 if (text == buf)
9611 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009613 }
9614#endif
9615}
9616
9617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 * "foreground()" function
9619 */
9620/*ARGSUSED*/
9621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009623 typval_T *argvars;
9624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627#ifdef FEAT_GUI
9628 if (gui.in_use)
9629 gui_mch_set_foreground();
9630#else
9631# ifdef WIN32
9632 win32_set_foreground();
9633# endif
9634#endif
9635}
9636
9637/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009638 * "function()" function
9639 */
9640/*ARGSUSED*/
9641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 typval_T *argvars;
9644 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009645{
9646 char_u *s;
9647
Bram Moolenaara7043832005-01-21 11:56:39 +00009648 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009650 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009651 EMSG2(_(e_invarg2), s);
9652 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009653 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009654 else
9655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_string = vim_strsave(s);
9657 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009658 }
9659}
9660
9661/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009662 * "garbagecollect()" function
9663 */
9664/*ARGSUSED*/
9665 static void
9666f_garbagecollect(argvars, rettv)
9667 typval_T *argvars;
9668 typval_T *rettv;
9669{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009670 /* This is postponed until we are back at the toplevel, because we may be
9671 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9672 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009673}
9674
9675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009676 * "get()" function
9677 */
9678 static void
9679f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009682{
Bram Moolenaar33570922005-01-25 22:26:29 +00009683 listitem_T *li;
9684 list_T *l;
9685 dictitem_T *di;
9686 dict_T *d;
9687 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009688
Bram Moolenaare9a41262005-01-15 22:18:47 +00009689 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009690 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009691 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 int error = FALSE;
9694
9695 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9696 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009698 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 else if (argvars[0].v_type == VAR_DICT)
9701 {
9702 if ((d = argvars[0].vval.v_dict) != NULL)
9703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009704 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 if (di != NULL)
9706 tv = &di->di_tv;
9707 }
9708 }
9709 else
9710 EMSG2(_(e_listdictarg), "get()");
9711
9712 if (tv == NULL)
9713 {
9714 if (argvars[2].v_type == VAR_UNKNOWN)
9715 rettv->vval.v_number = 0;
9716 else
9717 copy_tv(&argvars[2], rettv);
9718 }
9719 else
9720 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009721}
9722
Bram Moolenaar342337a2005-07-21 21:11:17 +00009723static 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 +00009724
9725/*
9726 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009727 * Return a range (from start to end) of lines in rettv from the specified
9728 * buffer.
9729 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009730 */
9731 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009732get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009733 buf_T *buf;
9734 linenr_T start;
9735 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009736 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009737 typval_T *rettv;
9738{
9739 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009740
Bram Moolenaar342337a2005-07-21 21:11:17 +00009741 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009742 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009743 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009744 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009745 }
9746 else
9747 rettv->vval.v_number = 0;
9748
9749 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9750 return;
9751
9752 if (!retlist)
9753 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009754 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9755 p = ml_get_buf(buf, start, FALSE);
9756 else
9757 p = (char_u *)"";
9758
9759 rettv->v_type = VAR_STRING;
9760 rettv->vval.v_string = vim_strsave(p);
9761 }
9762 else
9763 {
9764 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009765 return;
9766
9767 if (start < 1)
9768 start = 1;
9769 if (end > buf->b_ml.ml_line_count)
9770 end = buf->b_ml.ml_line_count;
9771 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009772 if (list_append_string(rettv->vval.v_list,
9773 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009774 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009775 }
9776}
9777
9778/*
9779 * "getbufline()" function
9780 */
9781 static void
9782f_getbufline(argvars, rettv)
9783 typval_T *argvars;
9784 typval_T *rettv;
9785{
9786 linenr_T lnum;
9787 linenr_T end;
9788 buf_T *buf;
9789
9790 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9791 ++emsg_off;
9792 buf = get_buf_tv(&argvars[0]);
9793 --emsg_off;
9794
Bram Moolenaar661b1822005-07-28 22:36:45 +00009795 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009796 if (argvars[2].v_type == VAR_UNKNOWN)
9797 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009798 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009799 end = get_tv_lnum_buf(&argvars[2], buf);
9800
Bram Moolenaar342337a2005-07-21 21:11:17 +00009801 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802}
9803
Bram Moolenaar0d660222005-01-07 21:51:51 +00009804/*
9805 * "getbufvar()" function
9806 */
9807 static void
9808f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 typval_T *argvars;
9810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009811{
9812 buf_T *buf;
9813 buf_T *save_curbuf;
9814 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009815 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009817 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9818 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009819 ++emsg_off;
9820 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009821
9822 rettv->v_type = VAR_STRING;
9823 rettv->vval.v_string = NULL;
9824
9825 if (buf != NULL && varname != NULL)
9826 {
9827 if (*varname == '&') /* buffer-local-option */
9828 {
9829 /* set curbuf to be our buf, temporarily */
9830 save_curbuf = curbuf;
9831 curbuf = buf;
9832
9833 get_option_tv(&varname, rettv, TRUE);
9834
9835 /* restore previous notion of curbuf */
9836 curbuf = save_curbuf;
9837 }
9838 else
9839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 if (*varname == NUL)
9841 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9842 * scope prefix before the NUL byte is required by
9843 * find_var_in_ht(). */
9844 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009845 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009846 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009847 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009849 }
9850 }
9851
9852 --emsg_off;
9853}
9854
9855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 * "getchar()" function
9857 */
9858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 typval_T *argvars;
9861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009866 /* Position the cursor. Needed after a message that ends in a space. */
9867 windgoto(msg_row, msg_col);
9868
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 ++no_mapping;
9870 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009871 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 /* getchar(): blocking wait. */
9873 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 /* getchar(1): only check if char avail */
9876 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 else if (error || vpeekc() == NUL)
9878 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 n = 0;
9880 else
9881 /* getchar(0) and char avail: return char */
9882 n = safe_vgetc();
9883 --no_mapping;
9884 --allow_keys;
9885
Bram Moolenaar219b8702006-11-01 14:32:36 +00009886 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9887 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9888 vimvars[VV_MOUSE_COL].vv_nr = 0;
9889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 if (IS_SPECIAL(n) || mod_mask != 0)
9892 {
9893 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9894 int i = 0;
9895
9896 /* Turn a special key into three bytes, plus modifier. */
9897 if (mod_mask != 0)
9898 {
9899 temp[i++] = K_SPECIAL;
9900 temp[i++] = KS_MODIFIER;
9901 temp[i++] = mod_mask;
9902 }
9903 if (IS_SPECIAL(n))
9904 {
9905 temp[i++] = K_SPECIAL;
9906 temp[i++] = K_SECOND(n);
9907 temp[i++] = K_THIRD(n);
9908 }
9909#ifdef FEAT_MBYTE
9910 else if (has_mbyte)
9911 i += (*mb_char2bytes)(n, temp + i);
9912#endif
9913 else
9914 temp[i++] = n;
9915 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 rettv->v_type = VAR_STRING;
9917 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009918
9919#ifdef FEAT_MOUSE
9920 if (n == K_LEFTMOUSE
9921 || n == K_LEFTMOUSE_NM
9922 || n == K_LEFTDRAG
9923 || n == K_LEFTRELEASE
9924 || n == K_LEFTRELEASE_NM
9925 || n == K_MIDDLEMOUSE
9926 || n == K_MIDDLEDRAG
9927 || n == K_MIDDLERELEASE
9928 || n == K_RIGHTMOUSE
9929 || n == K_RIGHTDRAG
9930 || n == K_RIGHTRELEASE
9931 || n == K_X1MOUSE
9932 || n == K_X1DRAG
9933 || n == K_X1RELEASE
9934 || n == K_X2MOUSE
9935 || n == K_X2DRAG
9936 || n == K_X2RELEASE
9937 || n == K_MOUSEDOWN
9938 || n == K_MOUSEUP)
9939 {
9940 int row = mouse_row;
9941 int col = mouse_col;
9942 win_T *win;
9943 linenr_T lnum;
9944# ifdef FEAT_WINDOWS
9945 win_T *wp;
9946# endif
9947 int n = 1;
9948
9949 if (row >= 0 && col >= 0)
9950 {
9951 /* Find the window at the mouse coordinates and compute the
9952 * text position. */
9953 win = mouse_find_win(&row, &col);
9954 (void)mouse_comp_pos(win, &row, &col, &lnum);
9955# ifdef FEAT_WINDOWS
9956 for (wp = firstwin; wp != win; wp = wp->w_next)
9957 ++n;
9958# endif
9959 vimvars[VV_MOUSE_WIN].vv_nr = n;
9960 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9961 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9962 }
9963 }
9964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 }
9966}
9967
9968/*
9969 * "getcharmod()" function
9970 */
9971/*ARGSUSED*/
9972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *argvars;
9975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979
9980/*
9981 * "getcmdline()" function
9982 */
9983/*ARGSUSED*/
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->v_type = VAR_STRING;
9990 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991}
9992
9993/*
9994 * "getcmdpos()" function
9995 */
9996/*ARGSUSED*/
9997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009999 typval_T *argvars;
10000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003}
10004
10005/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010006 * "getcmdtype()" function
10007 */
10008/*ARGSUSED*/
10009 static void
10010f_getcmdtype(argvars, rettv)
10011 typval_T *argvars;
10012 typval_T *rettv;
10013{
10014 rettv->v_type = VAR_STRING;
10015 rettv->vval.v_string = alloc(2);
10016 if (rettv->vval.v_string != NULL)
10017 {
10018 rettv->vval.v_string[0] = get_cmdline_type();
10019 rettv->vval.v_string[1] = NUL;
10020 }
10021}
10022
10023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 * "getcwd()" function
10025 */
10026/*ARGSUSED*/
10027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028f_getcwd(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{
10032 char_u cwd[MAXPATHL];
10033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 else
10038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010041 if (rettv->vval.v_string != NULL)
10042 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043#endif
10044 }
10045}
10046
10047/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010048 * "getfontname()" function
10049 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010050/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->v_type = VAR_STRING;
10057 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010058#ifdef FEAT_GUI
10059 if (gui.in_use)
10060 {
10061 GuiFont font;
10062 char_u *name = NULL;
10063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010064 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010065 {
10066 /* Get the "Normal" font. Either the name saved by
10067 * hl_set_font_name() or from the font ID. */
10068 font = gui.norm_font;
10069 name = hl_get_font_name();
10070 }
10071 else
10072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010074 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10075 return;
10076 font = gui_mch_get_font(name, FALSE);
10077 if (font == NOFONT)
10078 return; /* Invalid font name, return empty string. */
10079 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010081 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010082 gui_mch_free_font(font);
10083 }
10084#endif
10085}
10086
10087/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010088 * "getfperm({fname})" function
10089 */
10090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010094{
10095 char_u *fname;
10096 struct stat st;
10097 char_u *perm = NULL;
10098 char_u flags[] = "rwx";
10099 int i;
10100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010104 if (mch_stat((char *)fname, &st) >= 0)
10105 {
10106 perm = vim_strsave((char_u *)"---------");
10107 if (perm != NULL)
10108 {
10109 for (i = 0; i < 9; i++)
10110 {
10111 if (st.st_mode & (1 << (8 - i)))
10112 perm[i] = flags[i % 3];
10113 }
10114 }
10115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010117}
10118
10119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 * "getfsize({fname})" function
10121 */
10122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 typval_T *argvars;
10125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
10127 char_u *fname;
10128 struct stat st;
10129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133
10134 if (mch_stat((char *)fname, &st) >= 0)
10135 {
10136 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010141
10142 /* non-perfect check for overflow */
10143 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10144 rettv->vval.v_number = -2;
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
10151/*
10152 * "getftime({fname})" function
10153 */
10154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010156 typval_T *argvars;
10157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159 char_u *fname;
10160 struct stat st;
10161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163
10164 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010165 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168}
10169
10170/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010171 * "getftype({fname})" function
10172 */
10173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 typval_T *argvars;
10176 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010177{
10178 char_u *fname;
10179 struct stat st;
10180 char_u *type = NULL;
10181 char *t;
10182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010186 if (mch_lstat((char *)fname, &st) >= 0)
10187 {
10188#ifdef S_ISREG
10189 if (S_ISREG(st.st_mode))
10190 t = "file";
10191 else if (S_ISDIR(st.st_mode))
10192 t = "dir";
10193# ifdef S_ISLNK
10194 else if (S_ISLNK(st.st_mode))
10195 t = "link";
10196# endif
10197# ifdef S_ISBLK
10198 else if (S_ISBLK(st.st_mode))
10199 t = "bdev";
10200# endif
10201# ifdef S_ISCHR
10202 else if (S_ISCHR(st.st_mode))
10203 t = "cdev";
10204# endif
10205# ifdef S_ISFIFO
10206 else if (S_ISFIFO(st.st_mode))
10207 t = "fifo";
10208# endif
10209# ifdef S_ISSOCK
10210 else if (S_ISSOCK(st.st_mode))
10211 t = "fifo";
10212# endif
10213 else
10214 t = "other";
10215#else
10216# ifdef S_IFMT
10217 switch (st.st_mode & S_IFMT)
10218 {
10219 case S_IFREG: t = "file"; break;
10220 case S_IFDIR: t = "dir"; break;
10221# ifdef S_IFLNK
10222 case S_IFLNK: t = "link"; break;
10223# endif
10224# ifdef S_IFBLK
10225 case S_IFBLK: t = "bdev"; break;
10226# endif
10227# ifdef S_IFCHR
10228 case S_IFCHR: t = "cdev"; break;
10229# endif
10230# ifdef S_IFIFO
10231 case S_IFIFO: t = "fifo"; break;
10232# endif
10233# ifdef S_IFSOCK
10234 case S_IFSOCK: t = "socket"; break;
10235# endif
10236 default: t = "other";
10237 }
10238# else
10239 if (mch_isdir(fname))
10240 t = "dir";
10241 else
10242 t = "file";
10243# endif
10244#endif
10245 type = vim_strsave((char_u *)t);
10246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010248}
10249
10250/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010251 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010252 */
10253 static void
10254f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 typval_T *argvars;
10256 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010257{
10258 linenr_T lnum;
10259 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010260 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010261
10262 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010263 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010264 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010265 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010266 retlist = FALSE;
10267 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010268 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010269 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010270 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010271 retlist = TRUE;
10272 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010273
Bram Moolenaar342337a2005-07-21 21:11:17 +000010274 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010275}
10276
10277/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010278 * "getpos(string)" function
10279 */
10280 static void
10281f_getpos(argvars, rettv)
10282 typval_T *argvars;
10283 typval_T *rettv;
10284{
10285 pos_T *fp;
10286 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010287 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010288
10289 if (rettv_list_alloc(rettv) == OK)
10290 {
10291 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010292 fp = var2fpos(&argvars[0], TRUE, &fnum);
10293 if (fnum != -1)
10294 list_append_number(l, (varnumber_T)fnum);
10295 else
10296 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010297 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10298 : (varnumber_T)0);
10299 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10300 : (varnumber_T)0);
10301 list_append_number(l,
10302#ifdef FEAT_VIRTUALEDIT
10303 (fp != NULL) ? (varnumber_T)fp->coladd :
10304#endif
10305 (varnumber_T)0);
10306 }
10307 else
10308 rettv->vval.v_number = FALSE;
10309}
10310
10311/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010312 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010313 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010314/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010315 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010316f_getqflist(argvars, rettv)
10317 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010318 typval_T *rettv;
10319{
10320#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010321 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010322#endif
10323
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010324 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010325#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010326 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010327 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010328 wp = NULL;
10329 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10330 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010331 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010332 if (wp == NULL)
10333 return;
10334 }
10335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010336 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010337 }
10338#endif
10339}
10340
10341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 * "getreg()" function
10343 */
10344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010346 typval_T *argvars;
10347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 char_u *strregname;
10350 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010351 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010352 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356 strregname = get_tv_string_chk(&argvars[0]);
10357 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010358 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010359 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 regname = (strregname == NULL ? '"' : *strregname);
10364 if (regname == 0)
10365 regname = '"';
10366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010367 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 rettv->vval.v_string = error ? NULL :
10369 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370}
10371
10372/*
10373 * "getregtype()" function
10374 */
10375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
10380 char_u *strregname;
10381 int regname;
10382 char_u buf[NUMBUFLEN + 2];
10383 long reglen = 0;
10384
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010385 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 {
10387 strregname = get_tv_string_chk(&argvars[0]);
10388 if (strregname == NULL) /* type error; errmsg already given */
10389 {
10390 rettv->v_type = VAR_STRING;
10391 rettv->vval.v_string = NULL;
10392 return;
10393 }
10394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 else
10396 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010397 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398
10399 regname = (strregname == NULL ? '"' : *strregname);
10400 if (regname == 0)
10401 regname = '"';
10402
10403 buf[0] = NUL;
10404 buf[1] = NUL;
10405 switch (get_reg_type(regname, &reglen))
10406 {
10407 case MLINE: buf[0] = 'V'; break;
10408 case MCHAR: buf[0] = 'v'; break;
10409#ifdef FEAT_VISUAL
10410 case MBLOCK:
10411 buf[0] = Ctrl_V;
10412 sprintf((char *)buf + 1, "%ld", reglen + 1);
10413 break;
10414#endif
10415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 rettv->v_type = VAR_STRING;
10417 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
10419
10420/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010421 * "gettabwinvar()" function
10422 */
10423 static void
10424f_gettabwinvar(argvars, rettv)
10425 typval_T *argvars;
10426 typval_T *rettv;
10427{
10428 getwinvar(argvars, rettv, 1);
10429}
10430
10431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 * "getwinposx()" function
10433 */
10434/*ARGSUSED*/
10435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 typval_T *argvars;
10438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441#ifdef FEAT_GUI
10442 if (gui.in_use)
10443 {
10444 int x, y;
10445
10446 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 }
10449#endif
10450}
10451
10452/*
10453 * "getwinposy()" function
10454 */
10455/*ARGSUSED*/
10456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010458 typval_T *argvars;
10459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462#ifdef FEAT_GUI
10463 if (gui.in_use)
10464 {
10465 int x, y;
10466
10467 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 }
10470#endif
10471}
10472
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010473/*
10474 * Find window specifed by "vp" in tabpage "tp".
10475 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010476 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010477find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010478 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010479 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010480{
10481#ifdef FEAT_WINDOWS
10482 win_T *wp;
10483#endif
10484 int nr;
10485
10486 nr = get_tv_number_chk(vp, NULL);
10487
10488#ifdef FEAT_WINDOWS
10489 if (nr < 0)
10490 return NULL;
10491 if (nr == 0)
10492 return curwin;
10493
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010494 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10495 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010496 if (--nr <= 0)
10497 break;
10498 return wp;
10499#else
10500 if (nr == 0 || nr == 1)
10501 return curwin;
10502 return NULL;
10503#endif
10504}
10505
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506/*
10507 * "getwinvar()" function
10508 */
10509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010511 typval_T *argvars;
10512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010514 getwinvar(argvars, rettv, 0);
10515}
10516
10517/*
10518 * getwinvar() and gettabwinvar()
10519 */
10520 static void
10521getwinvar(argvars, rettv, off)
10522 typval_T *argvars;
10523 typval_T *rettv;
10524 int off; /* 1 for gettabwinvar() */
10525{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 win_T *win, *oldcurwin;
10527 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010529 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010531#ifdef FEAT_WINDOWS
10532 if (off == 1)
10533 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10534 else
10535 tp = curtab;
10536#endif
10537 win = find_win_by_nr(&argvars[off], tp);
10538 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541 rettv->v_type = VAR_STRING;
10542 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543
10544 if (win != NULL && varname != NULL)
10545 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010546 /* Set curwin to be our win, temporarily. Also set curbuf, so
10547 * that we can get buffer-local options. */
10548 oldcurwin = curwin;
10549 curwin = win;
10550 curbuf = win->w_buffer;
10551
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 else
10555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 if (*varname == NUL)
10557 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10558 * scope prefix before the NUL byte is required by
10559 * find_var_in_ht(). */
10560 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010562 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010564 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010566
10567 /* restore previous notion of curwin */
10568 curwin = oldcurwin;
10569 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571
10572 --emsg_off;
10573}
10574
10575/*
10576 * "glob()" function
10577 */
10578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 typval_T *argvars;
10581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582{
10583 expand_T xpc;
10584
10585 ExpandInit(&xpc);
10586 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->v_type = VAR_STRING;
10588 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590}
10591
10592/*
10593 * "globpath()" function
10594 */
10595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010597 typval_T *argvars;
10598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599{
10600 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 if (file == NULL)
10605 rettv->vval.v_string = NULL;
10606 else
10607 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608}
10609
10610/*
10611 * "has()" function
10612 */
10613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010615 typval_T *argvars;
10616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617{
10618 int i;
10619 char_u *name;
10620 int n = FALSE;
10621 static char *(has_list[]) =
10622 {
10623#ifdef AMIGA
10624 "amiga",
10625# ifdef FEAT_ARP
10626 "arp",
10627# endif
10628#endif
10629#ifdef __BEOS__
10630 "beos",
10631#endif
10632#ifdef MSDOS
10633# ifdef DJGPP
10634 "dos32",
10635# else
10636 "dos16",
10637# endif
10638#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010639#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 "mac",
10641#endif
10642#if defined(MACOS_X_UNIX)
10643 "macunix",
10644#endif
10645#ifdef OS2
10646 "os2",
10647#endif
10648#ifdef __QNX__
10649 "qnx",
10650#endif
10651#ifdef RISCOS
10652 "riscos",
10653#endif
10654#ifdef UNIX
10655 "unix",
10656#endif
10657#ifdef VMS
10658 "vms",
10659#endif
10660#ifdef WIN16
10661 "win16",
10662#endif
10663#ifdef WIN32
10664 "win32",
10665#endif
10666#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10667 "win32unix",
10668#endif
10669#ifdef WIN64
10670 "win64",
10671#endif
10672#ifdef EBCDIC
10673 "ebcdic",
10674#endif
10675#ifndef CASE_INSENSITIVE_FILENAME
10676 "fname_case",
10677#endif
10678#ifdef FEAT_ARABIC
10679 "arabic",
10680#endif
10681#ifdef FEAT_AUTOCMD
10682 "autocmd",
10683#endif
10684#ifdef FEAT_BEVAL
10685 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010686# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10687 "balloon_multiline",
10688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689#endif
10690#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10691 "builtin_terms",
10692# ifdef ALL_BUILTIN_TCAPS
10693 "all_builtin_terms",
10694# endif
10695#endif
10696#ifdef FEAT_BYTEOFF
10697 "byte_offset",
10698#endif
10699#ifdef FEAT_CINDENT
10700 "cindent",
10701#endif
10702#ifdef FEAT_CLIENTSERVER
10703 "clientserver",
10704#endif
10705#ifdef FEAT_CLIPBOARD
10706 "clipboard",
10707#endif
10708#ifdef FEAT_CMDL_COMPL
10709 "cmdline_compl",
10710#endif
10711#ifdef FEAT_CMDHIST
10712 "cmdline_hist",
10713#endif
10714#ifdef FEAT_COMMENTS
10715 "comments",
10716#endif
10717#ifdef FEAT_CRYPT
10718 "cryptv",
10719#endif
10720#ifdef FEAT_CSCOPE
10721 "cscope",
10722#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010723#ifdef CURSOR_SHAPE
10724 "cursorshape",
10725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726#ifdef DEBUG
10727 "debug",
10728#endif
10729#ifdef FEAT_CON_DIALOG
10730 "dialog_con",
10731#endif
10732#ifdef FEAT_GUI_DIALOG
10733 "dialog_gui",
10734#endif
10735#ifdef FEAT_DIFF
10736 "diff",
10737#endif
10738#ifdef FEAT_DIGRAPHS
10739 "digraphs",
10740#endif
10741#ifdef FEAT_DND
10742 "dnd",
10743#endif
10744#ifdef FEAT_EMACS_TAGS
10745 "emacs_tags",
10746#endif
10747 "eval", /* always present, of course! */
10748#ifdef FEAT_EX_EXTRA
10749 "ex_extra",
10750#endif
10751#ifdef FEAT_SEARCH_EXTRA
10752 "extra_search",
10753#endif
10754#ifdef FEAT_FKMAP
10755 "farsi",
10756#endif
10757#ifdef FEAT_SEARCHPATH
10758 "file_in_path",
10759#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010760#if defined(UNIX) && !defined(USE_SYSTEM)
10761 "filterpipe",
10762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763#ifdef FEAT_FIND_ID
10764 "find_in_path",
10765#endif
10766#ifdef FEAT_FOLDING
10767 "folding",
10768#endif
10769#ifdef FEAT_FOOTER
10770 "footer",
10771#endif
10772#if !defined(USE_SYSTEM) && defined(UNIX)
10773 "fork",
10774#endif
10775#ifdef FEAT_GETTEXT
10776 "gettext",
10777#endif
10778#ifdef FEAT_GUI
10779 "gui",
10780#endif
10781#ifdef FEAT_GUI_ATHENA
10782# ifdef FEAT_GUI_NEXTAW
10783 "gui_neXtaw",
10784# else
10785 "gui_athena",
10786# endif
10787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788#ifdef FEAT_GUI_GTK
10789 "gui_gtk",
10790# ifdef HAVE_GTK2
10791 "gui_gtk2",
10792# endif
10793#endif
10794#ifdef FEAT_GUI_MAC
10795 "gui_mac",
10796#endif
10797#ifdef FEAT_GUI_MOTIF
10798 "gui_motif",
10799#endif
10800#ifdef FEAT_GUI_PHOTON
10801 "gui_photon",
10802#endif
10803#ifdef FEAT_GUI_W16
10804 "gui_win16",
10805#endif
10806#ifdef FEAT_GUI_W32
10807 "gui_win32",
10808#endif
10809#ifdef FEAT_HANGULIN
10810 "hangul_input",
10811#endif
10812#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10813 "iconv",
10814#endif
10815#ifdef FEAT_INS_EXPAND
10816 "insert_expand",
10817#endif
10818#ifdef FEAT_JUMPLIST
10819 "jumplist",
10820#endif
10821#ifdef FEAT_KEYMAP
10822 "keymap",
10823#endif
10824#ifdef FEAT_LANGMAP
10825 "langmap",
10826#endif
10827#ifdef FEAT_LIBCALL
10828 "libcall",
10829#endif
10830#ifdef FEAT_LINEBREAK
10831 "linebreak",
10832#endif
10833#ifdef FEAT_LISP
10834 "lispindent",
10835#endif
10836#ifdef FEAT_LISTCMDS
10837 "listcmds",
10838#endif
10839#ifdef FEAT_LOCALMAP
10840 "localmap",
10841#endif
10842#ifdef FEAT_MENU
10843 "menu",
10844#endif
10845#ifdef FEAT_SESSION
10846 "mksession",
10847#endif
10848#ifdef FEAT_MODIFY_FNAME
10849 "modify_fname",
10850#endif
10851#ifdef FEAT_MOUSE
10852 "mouse",
10853#endif
10854#ifdef FEAT_MOUSESHAPE
10855 "mouseshape",
10856#endif
10857#if defined(UNIX) || defined(VMS)
10858# ifdef FEAT_MOUSE_DEC
10859 "mouse_dec",
10860# endif
10861# ifdef FEAT_MOUSE_GPM
10862 "mouse_gpm",
10863# endif
10864# ifdef FEAT_MOUSE_JSB
10865 "mouse_jsbterm",
10866# endif
10867# ifdef FEAT_MOUSE_NET
10868 "mouse_netterm",
10869# endif
10870# ifdef FEAT_MOUSE_PTERM
10871 "mouse_pterm",
10872# endif
10873# ifdef FEAT_MOUSE_XTERM
10874 "mouse_xterm",
10875# endif
10876#endif
10877#ifdef FEAT_MBYTE
10878 "multi_byte",
10879#endif
10880#ifdef FEAT_MBYTE_IME
10881 "multi_byte_ime",
10882#endif
10883#ifdef FEAT_MULTI_LANG
10884 "multi_lang",
10885#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010886#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010887#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010888 "mzscheme",
10889#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891#ifdef FEAT_OLE
10892 "ole",
10893#endif
10894#ifdef FEAT_OSFILETYPE
10895 "osfiletype",
10896#endif
10897#ifdef FEAT_PATH_EXTRA
10898 "path_extra",
10899#endif
10900#ifdef FEAT_PERL
10901#ifndef DYNAMIC_PERL
10902 "perl",
10903#endif
10904#endif
10905#ifdef FEAT_PYTHON
10906#ifndef DYNAMIC_PYTHON
10907 "python",
10908#endif
10909#endif
10910#ifdef FEAT_POSTSCRIPT
10911 "postscript",
10912#endif
10913#ifdef FEAT_PRINTER
10914 "printer",
10915#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010916#ifdef FEAT_PROFILE
10917 "profile",
10918#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010919#ifdef FEAT_RELTIME
10920 "reltime",
10921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922#ifdef FEAT_QUICKFIX
10923 "quickfix",
10924#endif
10925#ifdef FEAT_RIGHTLEFT
10926 "rightleft",
10927#endif
10928#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10929 "ruby",
10930#endif
10931#ifdef FEAT_SCROLLBIND
10932 "scrollbind",
10933#endif
10934#ifdef FEAT_CMDL_INFO
10935 "showcmd",
10936 "cmdline_info",
10937#endif
10938#ifdef FEAT_SIGNS
10939 "signs",
10940#endif
10941#ifdef FEAT_SMARTINDENT
10942 "smartindent",
10943#endif
10944#ifdef FEAT_SNIFF
10945 "sniff",
10946#endif
10947#ifdef FEAT_STL_OPT
10948 "statusline",
10949#endif
10950#ifdef FEAT_SUN_WORKSHOP
10951 "sun_workshop",
10952#endif
10953#ifdef FEAT_NETBEANS_INTG
10954 "netbeans_intg",
10955#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010956#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010957 "spell",
10958#endif
10959#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 "syntax",
10961#endif
10962#if defined(USE_SYSTEM) || !defined(UNIX)
10963 "system",
10964#endif
10965#ifdef FEAT_TAG_BINS
10966 "tag_binary",
10967#endif
10968#ifdef FEAT_TAG_OLDSTATIC
10969 "tag_old_static",
10970#endif
10971#ifdef FEAT_TAG_ANYWHITE
10972 "tag_any_white",
10973#endif
10974#ifdef FEAT_TCL
10975# ifndef DYNAMIC_TCL
10976 "tcl",
10977# endif
10978#endif
10979#ifdef TERMINFO
10980 "terminfo",
10981#endif
10982#ifdef FEAT_TERMRESPONSE
10983 "termresponse",
10984#endif
10985#ifdef FEAT_TEXTOBJ
10986 "textobjects",
10987#endif
10988#ifdef HAVE_TGETENT
10989 "tgetent",
10990#endif
10991#ifdef FEAT_TITLE
10992 "title",
10993#endif
10994#ifdef FEAT_TOOLBAR
10995 "toolbar",
10996#endif
10997#ifdef FEAT_USR_CMDS
10998 "user-commands", /* was accidentally included in 5.4 */
10999 "user_commands",
11000#endif
11001#ifdef FEAT_VIMINFO
11002 "viminfo",
11003#endif
11004#ifdef FEAT_VERTSPLIT
11005 "vertsplit",
11006#endif
11007#ifdef FEAT_VIRTUALEDIT
11008 "virtualedit",
11009#endif
11010#ifdef FEAT_VISUAL
11011 "visual",
11012#endif
11013#ifdef FEAT_VISUALEXTRA
11014 "visualextra",
11015#endif
11016#ifdef FEAT_VREPLACE
11017 "vreplace",
11018#endif
11019#ifdef FEAT_WILDIGN
11020 "wildignore",
11021#endif
11022#ifdef FEAT_WILDMENU
11023 "wildmenu",
11024#endif
11025#ifdef FEAT_WINDOWS
11026 "windows",
11027#endif
11028#ifdef FEAT_WAK
11029 "winaltkeys",
11030#endif
11031#ifdef FEAT_WRITEBACKUP
11032 "writebackup",
11033#endif
11034#ifdef FEAT_XIM
11035 "xim",
11036#endif
11037#ifdef FEAT_XFONTSET
11038 "xfontset",
11039#endif
11040#ifdef USE_XSMP
11041 "xsmp",
11042#endif
11043#ifdef USE_XSMP_INTERACT
11044 "xsmp_interact",
11045#endif
11046#ifdef FEAT_XCLIPBOARD
11047 "xterm_clipboard",
11048#endif
11049#ifdef FEAT_XTERM_SAVE
11050 "xterm_save",
11051#endif
11052#if defined(UNIX) && defined(FEAT_X11)
11053 "X11",
11054#endif
11055 NULL
11056 };
11057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 for (i = 0; has_list[i] != NULL; ++i)
11060 if (STRICMP(name, has_list[i]) == 0)
11061 {
11062 n = TRUE;
11063 break;
11064 }
11065
11066 if (n == FALSE)
11067 {
11068 if (STRNICMP(name, "patch", 5) == 0)
11069 n = has_patch(atoi((char *)name + 5));
11070 else if (STRICMP(name, "vim_starting") == 0)
11071 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011072#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11073 else if (STRICMP(name, "balloon_multiline") == 0)
11074 n = multiline_balloon_available();
11075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076#ifdef DYNAMIC_TCL
11077 else if (STRICMP(name, "tcl") == 0)
11078 n = tcl_enabled(FALSE);
11079#endif
11080#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11081 else if (STRICMP(name, "iconv") == 0)
11082 n = iconv_enabled(FALSE);
11083#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011084#ifdef DYNAMIC_MZSCHEME
11085 else if (STRICMP(name, "mzscheme") == 0)
11086 n = mzscheme_enabled(FALSE);
11087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088#ifdef DYNAMIC_RUBY
11089 else if (STRICMP(name, "ruby") == 0)
11090 n = ruby_enabled(FALSE);
11091#endif
11092#ifdef DYNAMIC_PYTHON
11093 else if (STRICMP(name, "python") == 0)
11094 n = python_enabled(FALSE);
11095#endif
11096#ifdef DYNAMIC_PERL
11097 else if (STRICMP(name, "perl") == 0)
11098 n = perl_enabled(FALSE);
11099#endif
11100#ifdef FEAT_GUI
11101 else if (STRICMP(name, "gui_running") == 0)
11102 n = (gui.in_use || gui.starting);
11103# ifdef FEAT_GUI_W32
11104 else if (STRICMP(name, "gui_win32s") == 0)
11105 n = gui_is_win32s();
11106# endif
11107# ifdef FEAT_BROWSE
11108 else if (STRICMP(name, "browse") == 0)
11109 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11110# endif
11111#endif
11112#ifdef FEAT_SYN_HL
11113 else if (STRICMP(name, "syntax_items") == 0)
11114 n = syntax_present(curbuf);
11115#endif
11116#if defined(WIN3264)
11117 else if (STRICMP(name, "win95") == 0)
11118 n = mch_windows95();
11119#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011120#ifdef FEAT_NETBEANS_INTG
11121 else if (STRICMP(name, "netbeans_enabled") == 0)
11122 n = usingNetbeans;
11123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 }
11125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127}
11128
11129/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 * "has_key()" function
11131 */
11132 static void
11133f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 typval_T *argvars;
11135 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136{
11137 rettv->vval.v_number = 0;
11138 if (argvars[0].v_type != VAR_DICT)
11139 {
11140 EMSG(_(e_dictreq));
11141 return;
11142 }
11143 if (argvars[0].vval.v_dict == NULL)
11144 return;
11145
11146 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011147 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148}
11149
11150/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011151 * "haslocaldir()" function
11152 */
11153/*ARGSUSED*/
11154 static void
11155f_haslocaldir(argvars, rettv)
11156 typval_T *argvars;
11157 typval_T *rettv;
11158{
11159 rettv->vval.v_number = (curwin->w_localdir != NULL);
11160}
11161
11162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 * "hasmapto()" function
11164 */
11165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
11170 char_u *name;
11171 char_u *mode;
11172 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011173 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011176 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 mode = (char_u *)"nvo";
11178 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011181 if (argvars[2].v_type != VAR_UNKNOWN)
11182 abbr = get_tv_number(&argvars[2]);
11183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184
Bram Moolenaar2c932302006-03-18 21:42:09 +000011185 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189}
11190
11191/*
11192 * "histadd()" function
11193 */
11194/*ARGSUSED*/
11195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011197 typval_T *argvars;
11198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199{
11200#ifdef FEAT_CMDHIST
11201 int histype;
11202 char_u *str;
11203 char_u buf[NUMBUFLEN];
11204#endif
11205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 if (check_restricted() || check_secure())
11208 return;
11209#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11211 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 if (histype >= 0)
11213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 if (*str != NUL)
11216 {
11217 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 return;
11220 }
11221 }
11222#endif
11223}
11224
11225/*
11226 * "histdel()" function
11227 */
11228/*ARGSUSED*/
11229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011230f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 typval_T *argvars;
11232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233{
11234#ifdef FEAT_CMDHIST
11235 int n;
11236 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011237 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11240 if (str == NULL)
11241 n = 0;
11242 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011245 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 else
11250 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 get_tv_string_buf(&argvars[1], buf));
11253 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256#endif
11257}
11258
11259/*
11260 * "histget()" function
11261 */
11262/*ARGSUSED*/
11263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
11268#ifdef FEAT_CMDHIST
11269 int type;
11270 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11274 if (str == NULL)
11275 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 {
11278 type = get_histtype(str);
11279 if (argvars[1].v_type == VAR_UNKNOWN)
11280 idx = get_history_idx(type);
11281 else
11282 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11283 /* -1 on type error */
11284 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290}
11291
11292/*
11293 * "histnr()" function
11294 */
11295/*ARGSUSED*/
11296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *argvars;
11299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300{
11301 int i;
11302
11303#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 char_u *history = get_tv_string_chk(&argvars[0]);
11305
11306 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 if (i >= HIST_CMD && i < HIST_COUNT)
11308 i = get_history_idx(i);
11309 else
11310#endif
11311 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
11315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 * "highlightID(name)" function
11317 */
11318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_hlID(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324}
11325
11326/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011327 * "highlight_exists()" function
11328 */
11329 static void
11330f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011333{
11334 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11335}
11336
11337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 * "hostname()" function
11339 */
11340/*ARGSUSED*/
11341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *argvars;
11344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345{
11346 char_u hostname[256];
11347
11348 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 rettv->v_type = VAR_STRING;
11350 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351}
11352
11353/*
11354 * iconv() function
11355 */
11356/*ARGSUSED*/
11357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *argvars;
11360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362#ifdef FEAT_MBYTE
11363 char_u buf1[NUMBUFLEN];
11364 char_u buf2[NUMBUFLEN];
11365 char_u *from, *to, *str;
11366 vimconv_T vimconv;
11367#endif
11368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 rettv->v_type = VAR_STRING;
11370 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371
11372#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 str = get_tv_string(&argvars[0]);
11374 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11375 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 vimconv.vc_type = CONV_NONE;
11377 convert_setup(&vimconv, from, to);
11378
11379 /* If the encodings are equal, no conversion needed. */
11380 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384
11385 convert_setup(&vimconv, NULL, NULL);
11386 vim_free(from);
11387 vim_free(to);
11388#endif
11389}
11390
11391/*
11392 * "indent()" function
11393 */
11394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 typval_T *argvars;
11397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399 linenr_T lnum;
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406}
11407
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011408/*
11409 * "index()" function
11410 */
11411 static void
11412f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011413 typval_T *argvars;
11414 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011415{
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 list_T *l;
11417 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011418 long idx = 0;
11419 int ic = FALSE;
11420
11421 rettv->vval.v_number = -1;
11422 if (argvars[0].v_type != VAR_LIST)
11423 {
11424 EMSG(_(e_listreq));
11425 return;
11426 }
11427 l = argvars[0].vval.v_list;
11428 if (l != NULL)
11429 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011430 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011431 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 int error = FALSE;
11434
Bram Moolenaar758711c2005-02-02 23:11:38 +000011435 /* Start at specified item. Use the cached index that list_find()
11436 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011438 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011439 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011440 ic = get_tv_number_chk(&argvars[3], &error);
11441 if (error)
11442 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011443 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011444
Bram Moolenaar758711c2005-02-02 23:11:38 +000011445 for ( ; item != NULL; item = item->li_next, ++idx)
11446 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011447 {
11448 rettv->vval.v_number = idx;
11449 break;
11450 }
11451 }
11452}
11453
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454static int inputsecret_flag = 0;
11455
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011456static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11457
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011459 * This function is used by f_input() and f_inputdialog() functions. The third
11460 * argument to f_input() specifies the type of completion to use at the
11461 * prompt. The third argument to f_inputdialog() specifies the value to return
11462 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 */
11464 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011465get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 typval_T *argvars;
11467 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011468 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011470 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 char_u *p = NULL;
11472 int c;
11473 char_u buf[NUMBUFLEN];
11474 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011475 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011476 int xp_type = EXPAND_NOTHING;
11477 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480
11481#ifdef NO_CONSOLE_INPUT
11482 /* While starting up, there is no place to enter text. */
11483 if (no_console_input())
11484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 return;
11487 }
11488#endif
11489
11490 cmd_silent = FALSE; /* Want to see the prompt. */
11491 if (prompt != NULL)
11492 {
11493 /* Only the part of the message after the last NL is considered as
11494 * prompt for the command line */
11495 p = vim_strrchr(prompt, '\n');
11496 if (p == NULL)
11497 p = prompt;
11498 else
11499 {
11500 ++p;
11501 c = *p;
11502 *p = NUL;
11503 msg_start();
11504 msg_clr_eos();
11505 msg_puts_attr(prompt, echo_attr);
11506 msg_didout = FALSE;
11507 msg_starthere();
11508 *p = c;
11509 }
11510 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 if (argvars[1].v_type != VAR_UNKNOWN)
11513 {
11514 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11515 if (defstr != NULL)
11516 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011518 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011519 {
11520 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011521 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011522 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011523
Bram Moolenaar4463f292005-09-25 22:20:24 +000011524 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011525
Bram Moolenaar4463f292005-09-25 22:20:24 +000011526 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11527 if (xp_name == NULL)
11528 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011529
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011530 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011531
Bram Moolenaar4463f292005-09-25 22:20:24 +000011532 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11533 &xp_arg) == FAIL)
11534 return;
11535 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011536 }
11537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538 if (defstr != NULL)
11539 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011540 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11541 xp_type, xp_arg);
11542
11543 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 /* since the user typed this, no need to wait for return */
11546 need_wait_return = FALSE;
11547 msg_didout = FALSE;
11548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 cmd_silent = cmd_silent_save;
11550}
11551
11552/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011553 * "input()" function
11554 * Also handles inputsecret() when inputsecret is set.
11555 */
11556 static void
11557f_input(argvars, rettv)
11558 typval_T *argvars;
11559 typval_T *rettv;
11560{
11561 get_user_input(argvars, rettv, FALSE);
11562}
11563
11564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 * "inputdialog()" function
11566 */
11567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011568f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011569 typval_T *argvars;
11570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571{
11572#if defined(FEAT_GUI_TEXTDIALOG)
11573 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11574 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11575 {
11576 char_u *message;
11577 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580 message = get_tv_string_chk(&argvars[0]);
11581 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011582 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011583 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 else
11585 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011586 if (message != NULL && defstr != NULL
11587 && do_dialog(VIM_QUESTION, NULL, message,
11588 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 else
11591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 if (message != NULL && defstr != NULL
11593 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011594 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_string = vim_strsave(
11596 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 }
11602 else
11603#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011604 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605}
11606
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011607/*
11608 * "inputlist()" function
11609 */
11610 static void
11611f_inputlist(argvars, rettv)
11612 typval_T *argvars;
11613 typval_T *rettv;
11614{
11615 listitem_T *li;
11616 int selected;
11617 int mouse_used;
11618
11619 rettv->vval.v_number = 0;
11620#ifdef NO_CONSOLE_INPUT
11621 /* While starting up, there is no place to enter text. */
11622 if (no_console_input())
11623 return;
11624#endif
11625 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11626 {
11627 EMSG2(_(e_listarg), "inputlist()");
11628 return;
11629 }
11630
11631 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011632 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011633 lines_left = Rows; /* avoid more prompt */
11634 msg_scroll = TRUE;
11635 msg_clr_eos();
11636
11637 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11638 {
11639 msg_puts(get_tv_string(&li->li_tv));
11640 msg_putchar('\n');
11641 }
11642
11643 /* Ask for choice. */
11644 selected = prompt_for_number(&mouse_used);
11645 if (mouse_used)
11646 selected -= lines_left;
11647
11648 rettv->vval.v_number = selected;
11649}
11650
11651
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11653
11654/*
11655 * "inputrestore()" function
11656 */
11657/*ARGSUSED*/
11658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 typval_T *argvars;
11661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
11663 if (ga_userinput.ga_len > 0)
11664 {
11665 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11667 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 }
11670 else if (p_verbose > 1)
11671 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011672 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 }
11675}
11676
11677/*
11678 * "inputsave()" function
11679 */
11680/*ARGSUSED*/
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 /* Add an entry to the stack of typehead storage. */
11687 if (ga_grow(&ga_userinput, 1) == OK)
11688 {
11689 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11690 + ga_userinput.ga_len);
11691 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 }
11694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696}
11697
11698/*
11699 * "inputsecret()" function
11700 */
11701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 typval_T *argvars;
11704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705{
11706 ++cmdline_star;
11707 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 --cmdline_star;
11710 --inputsecret_flag;
11711}
11712
11713/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011714 * "insert()" function
11715 */
11716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011720{
11721 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011722 listitem_T *item;
11723 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011725
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011726 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011727 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011728 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011729 else if ((l = argvars[0].vval.v_list) != NULL
11730 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011731 {
11732 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 before = get_tv_number_chk(&argvars[2], &error);
11734 if (error)
11735 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011736
Bram Moolenaar758711c2005-02-02 23:11:38 +000011737 if (before == l->lv_len)
11738 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011739 else
11740 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011741 item = list_find(l, before);
11742 if (item == NULL)
11743 {
11744 EMSGN(_(e_listidx), before);
11745 l = NULL;
11746 }
11747 }
11748 if (l != NULL)
11749 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011750 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011751 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011752 }
11753 }
11754}
11755
11756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 * "isdirectory()" function
11758 */
11759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765}
11766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011767/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011768 * "islocked()" function
11769 */
11770 static void
11771f_islocked(argvars, rettv)
11772 typval_T *argvars;
11773 typval_T *rettv;
11774{
11775 lval_T lv;
11776 char_u *end;
11777 dictitem_T *di;
11778
11779 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011780 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11781 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011782 if (end != NULL && lv.ll_name != NULL)
11783 {
11784 if (*end != NUL)
11785 EMSG(_(e_trailing));
11786 else
11787 {
11788 if (lv.ll_tv == NULL)
11789 {
11790 if (check_changedtick(lv.ll_name))
11791 rettv->vval.v_number = 1; /* always locked */
11792 else
11793 {
11794 di = find_var(lv.ll_name, NULL);
11795 if (di != NULL)
11796 {
11797 /* Consider a variable locked when:
11798 * 1. the variable itself is locked
11799 * 2. the value of the variable is locked.
11800 * 3. the List or Dict value is locked.
11801 */
11802 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11803 || tv_islocked(&di->di_tv));
11804 }
11805 }
11806 }
11807 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011808 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011809 else if (lv.ll_newkey != NULL)
11810 EMSG2(_(e_dictkey), lv.ll_newkey);
11811 else if (lv.ll_list != NULL)
11812 /* List item. */
11813 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11814 else
11815 /* Dictionary item. */
11816 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11817 }
11818 }
11819
11820 clear_lval(&lv);
11821}
11822
Bram Moolenaar33570922005-01-25 22:26:29 +000011823static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011824
11825/*
11826 * Turn a dict into a list:
11827 * "what" == 0: list of keys
11828 * "what" == 1: list of values
11829 * "what" == 2: list of items
11830 */
11831 static void
11832dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 typval_T *argvars;
11834 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011835 int what;
11836{
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 list_T *l2;
11838 dictitem_T *di;
11839 hashitem_T *hi;
11840 listitem_T *li;
11841 listitem_T *li2;
11842 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011843 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011844
11845 rettv->vval.v_number = 0;
11846 if (argvars[0].v_type != VAR_DICT)
11847 {
11848 EMSG(_(e_dictreq));
11849 return;
11850 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011851 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011852 return;
11853
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011854 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011855 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011856
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011857 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011860 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011861 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011862 --todo;
11863 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011864
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011865 li = listitem_alloc();
11866 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011867 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011868 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011869
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011870 if (what == 0)
11871 {
11872 /* keys() */
11873 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011874 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011875 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11876 }
11877 else if (what == 1)
11878 {
11879 /* values() */
11880 copy_tv(&di->di_tv, &li->li_tv);
11881 }
11882 else
11883 {
11884 /* items() */
11885 l2 = list_alloc();
11886 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011887 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011888 li->li_tv.vval.v_list = l2;
11889 if (l2 == NULL)
11890 break;
11891 ++l2->lv_refcount;
11892
11893 li2 = listitem_alloc();
11894 if (li2 == NULL)
11895 break;
11896 list_append(l2, li2);
11897 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011898 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011899 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11900
11901 li2 = listitem_alloc();
11902 if (li2 == NULL)
11903 break;
11904 list_append(l2, li2);
11905 copy_tv(&di->di_tv, &li2->li_tv);
11906 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011907 }
11908 }
11909}
11910
11911/*
11912 * "items(dict)" function
11913 */
11914 static void
11915f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011916 typval_T *argvars;
11917 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011918{
11919 dict_list(argvars, rettv, 2);
11920}
11921
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923 * "join()" function
11924 */
11925 static void
11926f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *argvars;
11928 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929{
11930 garray_T ga;
11931 char_u *sep;
11932
11933 rettv->vval.v_number = 0;
11934 if (argvars[0].v_type != VAR_LIST)
11935 {
11936 EMSG(_(e_listreq));
11937 return;
11938 }
11939 if (argvars[0].vval.v_list == NULL)
11940 return;
11941 if (argvars[1].v_type == VAR_UNKNOWN)
11942 sep = (char_u *)" ";
11943 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011945
11946 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947
11948 if (sep != NULL)
11949 {
11950 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011951 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 ga_append(&ga, NUL);
11953 rettv->vval.v_string = (char_u *)ga.ga_data;
11954 }
11955 else
11956 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011957}
11958
11959/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011960 * "keys()" function
11961 */
11962 static void
11963f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011964 typval_T *argvars;
11965 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011966{
11967 dict_list(argvars, rettv, 0);
11968}
11969
11970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 * "last_buffer_nr()" function.
11972 */
11973/*ARGSUSED*/
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
11979 int n = 0;
11980 buf_T *buf;
11981
11982 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11983 if (n < buf->b_fnum)
11984 n = buf->b_fnum;
11985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011987}
11988
11989/*
11990 * "len()" function
11991 */
11992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011994 typval_T *argvars;
11995 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011996{
11997 switch (argvars[0].v_type)
11998 {
11999 case VAR_STRING:
12000 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_number = (varnumber_T)STRLEN(
12002 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012003 break;
12004 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012006 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012007 case VAR_DICT:
12008 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12009 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012010 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012011 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012012 break;
12013 }
12014}
12015
Bram Moolenaar33570922005-01-25 22:26:29 +000012016static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012017
12018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012022 int type;
12023{
12024#ifdef FEAT_LIBCALL
12025 char_u *string_in;
12026 char_u **string_result;
12027 int nr_result;
12028#endif
12029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012031 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012035
12036 if (check_restricted() || check_secure())
12037 return;
12038
12039#ifdef FEAT_LIBCALL
12040 /* The first two args must be strings, otherwise its meaningless */
12041 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12042 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012043 string_in = NULL;
12044 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012045 string_in = argvars[2].vval.v_string;
12046 if (type == VAR_NUMBER)
12047 string_result = NULL;
12048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012050 if (mch_libcall(argvars[0].vval.v_string,
12051 argvars[1].vval.v_string,
12052 string_in,
12053 argvars[2].vval.v_number,
12054 string_result,
12055 &nr_result) == OK
12056 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012058 }
12059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060}
12061
12062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063 * "libcall()" function
12064 */
12065 static void
12066f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012067 typval_T *argvars;
12068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012069{
12070 libcall_common(argvars, rettv, VAR_STRING);
12071}
12072
12073/*
12074 * "libcallnr()" function
12075 */
12076 static void
12077f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012080{
12081 libcall_common(argvars, rettv, VAR_NUMBER);
12082}
12083
12084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 * "line(string)" function
12086 */
12087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012089 typval_T *argvars;
12090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
12092 linenr_T lnum = 0;
12093 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012094 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012096 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 if (fp != NULL)
12098 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100}
12101
12102/*
12103 * "line2byte(lnum)" function
12104 */
12105/*ARGSUSED*/
12106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 typval_T *argvars;
12109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110{
12111#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113#else
12114 linenr_T lnum;
12115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12121 if (rettv->vval.v_number >= 0)
12122 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123#endif
12124}
12125
12126/*
12127 * "lispindent(lnum)" function
12128 */
12129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133{
12134#ifdef FEAT_LISP
12135 pos_T pos;
12136 linenr_T lnum;
12137
12138 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12141 {
12142 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 curwin->w_cursor = pos;
12145 }
12146 else
12147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149}
12150
12151/*
12152 * "localtime()" function
12153 */
12154/*ARGSUSED*/
12155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012157 typval_T *argvars;
12158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161}
12162
Bram Moolenaar33570922005-01-25 22:26:29 +000012163static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164
12165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012167 typval_T *argvars;
12168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 int exact;
12170{
12171 char_u *keys;
12172 char_u *which;
12173 char_u buf[NUMBUFLEN];
12174 char_u *keys_buf = NULL;
12175 char_u *rhs;
12176 int mode;
12177 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012178 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179
12180 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->v_type = VAR_STRING;
12182 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 if (*keys == NUL)
12186 return;
12187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012190 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012191 if (argvars[2].v_type != VAR_UNKNOWN)
12192 abbr = get_tv_number(&argvars[2]);
12193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 else
12195 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012196 if (which == NULL)
12197 return;
12198
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 mode = get_map_mode(&which, 0);
12200
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012201 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012202 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 vim_free(keys_buf);
12204 if (rhs != NULL)
12205 {
12206 ga_init(&ga);
12207 ga.ga_itemsize = 1;
12208 ga.ga_growsize = 40;
12209
12210 while (*rhs != NUL)
12211 ga_concat(&ga, str2special(&rhs, FALSE));
12212
12213 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 }
12216}
12217
12218/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012219 * "map()" function
12220 */
12221 static void
12222f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012223 typval_T *argvars;
12224 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012225{
12226 filter_map(argvars, rettv, TRUE);
12227}
12228
12229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012230 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 */
12232 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012233f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012234 typval_T *argvars;
12235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012237 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238}
12239
12240/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012241 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 */
12243 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012245 typval_T *argvars;
12246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249}
12250
Bram Moolenaar33570922005-01-25 22:26:29 +000012251static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252
12253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012255 typval_T *argvars;
12256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 int type;
12258{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012259 char_u *str = NULL;
12260 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 char_u *pat;
12262 regmatch_T regmatch;
12263 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012264 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 char_u *save_cpo;
12266 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012267 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012268 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012269 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012270 list_T *l = NULL;
12271 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012272 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012273 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274
12275 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12276 save_cpo = p_cpo;
12277 p_cpo = (char_u *)"";
12278
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012279 rettv->vval.v_number = -1;
12280 if (type == 3)
12281 {
12282 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012283 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012284 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012285 }
12286 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->v_type = VAR_STRING;
12289 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012292 if (argvars[0].v_type == VAR_LIST)
12293 {
12294 if ((l = argvars[0].vval.v_list) == NULL)
12295 goto theend;
12296 li = l->lv_first;
12297 }
12298 else
12299 expr = str = get_tv_string(&argvars[0]);
12300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012301 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12302 if (pat == NULL)
12303 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012305 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012307 int error = FALSE;
12308
12309 start = get_tv_number_chk(&argvars[2], &error);
12310 if (error)
12311 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012312 if (l != NULL)
12313 {
12314 li = list_find(l, start);
12315 if (li == NULL)
12316 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012317 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012318 }
12319 else
12320 {
12321 if (start < 0)
12322 start = 0;
12323 if (start > (long)STRLEN(str))
12324 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012325 /* When "count" argument is there ignore matches before "start",
12326 * otherwise skip part of the string. Differs when pattern is "^"
12327 * or "\<". */
12328 if (argvars[3].v_type != VAR_UNKNOWN)
12329 startcol = start;
12330 else
12331 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012332 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012333
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012334 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012335 nth = get_tv_number_chk(&argvars[3], &error);
12336 if (error)
12337 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 }
12339
12340 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12341 if (regmatch.regprog != NULL)
12342 {
12343 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012344
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012345 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012346 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012347 if (l != NULL)
12348 {
12349 if (li == NULL)
12350 {
12351 match = FALSE;
12352 break;
12353 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012354 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012355 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012356 if (str == NULL)
12357 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012358 }
12359
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012360 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012361
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012362 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012363 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012364 if (l == NULL && !match)
12365 break;
12366
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012367 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012368 if (l != NULL)
12369 {
12370 li = li->li_next;
12371 ++idx;
12372 }
12373 else
12374 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012375#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012376 startcol = (colnr_T)(regmatch.startp[0]
12377 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012378#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012379 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012380#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012381 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012382 }
12383
12384 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012386 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012388 int i;
12389
12390 /* return list with matched string and submatches */
12391 for (i = 0; i < NSUBEXP; ++i)
12392 {
12393 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012394 {
12395 if (list_append_string(rettv->vval.v_list,
12396 (char_u *)"", 0) == FAIL)
12397 break;
12398 }
12399 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012400 regmatch.startp[i],
12401 (int)(regmatch.endp[i] - regmatch.startp[i]))
12402 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012403 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012404 }
12405 }
12406 else if (type == 2)
12407 {
12408 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012409 if (l != NULL)
12410 copy_tv(&li->li_tv, rettv);
12411 else
12412 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012414 }
12415 else if (l != NULL)
12416 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 else
12418 {
12419 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 (varnumber_T)(regmatch.startp[0] - str);
12422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012425 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 }
12427 }
12428 vim_free(regmatch.regprog);
12429 }
12430
12431theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012432 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 p_cpo = save_cpo;
12434}
12435
12436/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012437 * "match()" function
12438 */
12439 static void
12440f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012441 typval_T *argvars;
12442 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443{
12444 find_some_match(argvars, rettv, 1);
12445}
12446
12447/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012448 * "matcharg()" function
12449 */
12450 static void
12451f_matcharg(argvars, rettv)
12452 typval_T *argvars;
12453 typval_T *rettv;
12454{
12455 if (rettv_list_alloc(rettv) == OK)
12456 {
12457#ifdef FEAT_SEARCH_EXTRA
12458 int mi = get_tv_number(&argvars[0]);
12459
12460 if (mi >= 1 && mi <= 3)
12461 {
12462 list_append_string(rettv->vval.v_list,
12463 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12464 list_append_string(rettv->vval.v_list,
12465 curwin->w_match_pat[mi - 1], -1);
12466 }
12467#endif
12468 }
12469}
12470
12471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012472 * "matchend()" function
12473 */
12474 static void
12475f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *argvars;
12477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012478{
12479 find_some_match(argvars, rettv, 0);
12480}
12481
12482/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012483 * "matchlist()" function
12484 */
12485 static void
12486f_matchlist(argvars, rettv)
12487 typval_T *argvars;
12488 typval_T *rettv;
12489{
12490 find_some_match(argvars, rettv, 3);
12491}
12492
12493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012494 * "matchstr()" function
12495 */
12496 static void
12497f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012498 typval_T *argvars;
12499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012500{
12501 find_some_match(argvars, rettv, 2);
12502}
12503
Bram Moolenaar33570922005-01-25 22:26:29 +000012504static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012505
12506 static void
12507max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *argvars;
12509 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012510 int domax;
12511{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012512 long n = 0;
12513 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012515
12516 if (argvars[0].v_type == VAR_LIST)
12517 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012518 list_T *l;
12519 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012520
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012521 l = argvars[0].vval.v_list;
12522 if (l != NULL)
12523 {
12524 li = l->lv_first;
12525 if (li != NULL)
12526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012527 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012528 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012529 {
12530 li = li->li_next;
12531 if (li == NULL)
12532 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012533 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012534 if (domax ? i > n : i < n)
12535 n = i;
12536 }
12537 }
12538 }
12539 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012540 else if (argvars[0].v_type == VAR_DICT)
12541 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012543 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012545 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012546
12547 d = argvars[0].vval.v_dict;
12548 if (d != NULL)
12549 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012550 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012551 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012553 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012554 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012555 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012556 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012557 if (first)
12558 {
12559 n = i;
12560 first = FALSE;
12561 }
12562 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012563 n = i;
12564 }
12565 }
12566 }
12567 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012568 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012569 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012570 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012571}
12572
12573/*
12574 * "max()" function
12575 */
12576 static void
12577f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012578 typval_T *argvars;
12579 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012580{
12581 max_min(argvars, rettv, TRUE);
12582}
12583
12584/*
12585 * "min()" function
12586 */
12587 static void
12588f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012589 typval_T *argvars;
12590 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012591{
12592 max_min(argvars, rettv, FALSE);
12593}
12594
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012595static int mkdir_recurse __ARGS((char_u *dir, int prot));
12596
12597/*
12598 * Create the directory in which "dir" is located, and higher levels when
12599 * needed.
12600 */
12601 static int
12602mkdir_recurse(dir, prot)
12603 char_u *dir;
12604 int prot;
12605{
12606 char_u *p;
12607 char_u *updir;
12608 int r = FAIL;
12609
12610 /* Get end of directory name in "dir".
12611 * We're done when it's "/" or "c:/". */
12612 p = gettail_sep(dir);
12613 if (p <= get_past_head(dir))
12614 return OK;
12615
12616 /* If the directory exists we're done. Otherwise: create it.*/
12617 updir = vim_strnsave(dir, (int)(p - dir));
12618 if (updir == NULL)
12619 return FAIL;
12620 if (mch_isdir(updir))
12621 r = OK;
12622 else if (mkdir_recurse(updir, prot) == OK)
12623 r = vim_mkdir_emsg(updir, prot);
12624 vim_free(updir);
12625 return r;
12626}
12627
12628#ifdef vim_mkdir
12629/*
12630 * "mkdir()" function
12631 */
12632 static void
12633f_mkdir(argvars, rettv)
12634 typval_T *argvars;
12635 typval_T *rettv;
12636{
12637 char_u *dir;
12638 char_u buf[NUMBUFLEN];
12639 int prot = 0755;
12640
12641 rettv->vval.v_number = FAIL;
12642 if (check_restricted() || check_secure())
12643 return;
12644
12645 dir = get_tv_string_buf(&argvars[0], buf);
12646 if (argvars[1].v_type != VAR_UNKNOWN)
12647 {
12648 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012649 prot = get_tv_number_chk(&argvars[2], NULL);
12650 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012651 mkdir_recurse(dir, prot);
12652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012654}
12655#endif
12656
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 * "mode()" function
12659 */
12660/*ARGSUSED*/
12661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 typval_T *argvars;
12664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665{
12666 char_u buf[2];
12667
12668#ifdef FEAT_VISUAL
12669 if (VIsual_active)
12670 {
12671 if (VIsual_select)
12672 buf[0] = VIsual_mode + 's' - 'v';
12673 else
12674 buf[0] = VIsual_mode;
12675 }
12676 else
12677#endif
12678 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12679 buf[0] = 'r';
12680 else if (State & INSERT)
12681 {
12682 if (State & REPLACE_FLAG)
12683 buf[0] = 'R';
12684 else
12685 buf[0] = 'i';
12686 }
12687 else if (State & CMDLINE)
12688 buf[0] = 'c';
12689 else
12690 buf[0] = 'n';
12691
12692 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_string = vim_strsave(buf);
12694 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695}
12696
12697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 * "nextnonblank()" function
12699 */
12700 static void
12701f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012704{
12705 linenr_T lnum;
12706
12707 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012710 {
12711 lnum = 0;
12712 break;
12713 }
12714 if (*skipwhite(ml_get(lnum)) != NUL)
12715 break;
12716 }
12717 rettv->vval.v_number = lnum;
12718}
12719
12720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 * "nr2char()" function
12722 */
12723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 typval_T *argvars;
12726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727{
12728 char_u buf[NUMBUFLEN];
12729
12730#ifdef FEAT_MBYTE
12731 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 else
12734#endif
12735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 buf[1] = NUL;
12738 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->v_type = VAR_STRING;
12740 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012741}
12742
12743/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012744 * "pathshorten()" function
12745 */
12746 static void
12747f_pathshorten(argvars, rettv)
12748 typval_T *argvars;
12749 typval_T *rettv;
12750{
12751 char_u *p;
12752
12753 rettv->v_type = VAR_STRING;
12754 p = get_tv_string_chk(&argvars[0]);
12755 if (p == NULL)
12756 rettv->vval.v_string = NULL;
12757 else
12758 {
12759 p = vim_strsave(p);
12760 rettv->vval.v_string = p;
12761 if (p != NULL)
12762 shorten_dir(p);
12763 }
12764}
12765
12766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767 * "prevnonblank()" function
12768 */
12769 static void
12770f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *argvars;
12772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012773{
12774 linenr_T lnum;
12775
12776 lnum = get_tv_lnum(argvars);
12777 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12778 lnum = 0;
12779 else
12780 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12781 --lnum;
12782 rettv->vval.v_number = lnum;
12783}
12784
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012785#ifdef HAVE_STDARG_H
12786/* This dummy va_list is here because:
12787 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12788 * - locally in the function results in a "used before set" warning
12789 * - using va_start() to initialize it gives "function with fixed args" error */
12790static va_list ap;
12791#endif
12792
Bram Moolenaar8c711452005-01-14 21:53:12 +000012793/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012794 * "printf()" function
12795 */
12796 static void
12797f_printf(argvars, rettv)
12798 typval_T *argvars;
12799 typval_T *rettv;
12800{
12801 rettv->v_type = VAR_STRING;
12802 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012803#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012804 {
12805 char_u buf[NUMBUFLEN];
12806 int len;
12807 char_u *s;
12808 int saved_did_emsg = did_emsg;
12809 char *fmt;
12810
12811 /* Get the required length, allocate the buffer and do it for real. */
12812 did_emsg = FALSE;
12813 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012814 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012815 if (!did_emsg)
12816 {
12817 s = alloc(len + 1);
12818 if (s != NULL)
12819 {
12820 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012821 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012822 }
12823 }
12824 did_emsg |= saved_did_emsg;
12825 }
12826#endif
12827}
12828
12829/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012830 * "pumvisible()" function
12831 */
12832/*ARGSUSED*/
12833 static void
12834f_pumvisible(argvars, rettv)
12835 typval_T *argvars;
12836 typval_T *rettv;
12837{
12838 rettv->vval.v_number = 0;
12839#ifdef FEAT_INS_EXPAND
12840 if (pum_visible())
12841 rettv->vval.v_number = 1;
12842#endif
12843}
12844
12845/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012846 * "range()" function
12847 */
12848 static void
12849f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012850 typval_T *argvars;
12851 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012852{
12853 long start;
12854 long end;
12855 long stride = 1;
12856 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012857 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012859 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012860 if (argvars[1].v_type == VAR_UNKNOWN)
12861 {
12862 end = start - 1;
12863 start = 0;
12864 }
12865 else
12866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012870 }
12871
12872 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 if (error)
12874 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012875 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012876 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012877 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012878 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012879 else
12880 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012881 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012882 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012883 if (list_append_number(rettv->vval.v_list,
12884 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012885 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012886 }
12887}
12888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012889/*
12890 * "readfile()" function
12891 */
12892 static void
12893f_readfile(argvars, rettv)
12894 typval_T *argvars;
12895 typval_T *rettv;
12896{
12897 int binary = FALSE;
12898 char_u *fname;
12899 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012900 listitem_T *li;
12901#define FREAD_SIZE 200 /* optimized for text lines */
12902 char_u buf[FREAD_SIZE];
12903 int readlen; /* size of last fread() */
12904 int buflen; /* nr of valid chars in buf[] */
12905 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12906 int tolist; /* first byte in buf[] still to be put in list */
12907 int chop; /* how many CR to chop off */
12908 char_u *prev = NULL; /* previously read bytes, if any */
12909 int prevlen = 0; /* length of "prev" if not NULL */
12910 char_u *s;
12911 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012912 long maxline = MAXLNUM;
12913 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012914
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012915 if (argvars[1].v_type != VAR_UNKNOWN)
12916 {
12917 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12918 binary = TRUE;
12919 if (argvars[2].v_type != VAR_UNKNOWN)
12920 maxline = get_tv_number(&argvars[2]);
12921 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012923 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012924 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012925
12926 /* Always open the file in binary mode, library functions have a mind of
12927 * their own about CR-LF conversion. */
12928 fname = get_tv_string(&argvars[0]);
12929 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12930 {
12931 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12932 return;
12933 }
12934
12935 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012936 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012937 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012938 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012939 buflen = filtd + readlen;
12940 tolist = 0;
12941 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12942 {
12943 if (buf[filtd] == '\n' || readlen <= 0)
12944 {
12945 /* Only when in binary mode add an empty list item when the
12946 * last line ends in a '\n'. */
12947 if (!binary && readlen == 0 && filtd == 0)
12948 break;
12949
12950 /* Found end-of-line or end-of-file: add a text line to the
12951 * list. */
12952 chop = 0;
12953 if (!binary)
12954 while (filtd - chop - 1 >= tolist
12955 && buf[filtd - chop - 1] == '\r')
12956 ++chop;
12957 len = filtd - tolist - chop;
12958 if (prev == NULL)
12959 s = vim_strnsave(buf + tolist, len);
12960 else
12961 {
12962 s = alloc((unsigned)(prevlen + len + 1));
12963 if (s != NULL)
12964 {
12965 mch_memmove(s, prev, prevlen);
12966 vim_free(prev);
12967 prev = NULL;
12968 mch_memmove(s + prevlen, buf + tolist, len);
12969 s[prevlen + len] = NUL;
12970 }
12971 }
12972 tolist = filtd + 1;
12973
12974 li = listitem_alloc();
12975 if (li == NULL)
12976 {
12977 vim_free(s);
12978 break;
12979 }
12980 li->li_tv.v_type = VAR_STRING;
12981 li->li_tv.v_lock = 0;
12982 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012983 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012984
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012985 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012986 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012987 if (readlen <= 0)
12988 break;
12989 }
12990 else if (buf[filtd] == NUL)
12991 buf[filtd] = '\n';
12992 }
12993 if (readlen <= 0)
12994 break;
12995
12996 if (tolist == 0)
12997 {
12998 /* "buf" is full, need to move text to an allocated buffer */
12999 if (prev == NULL)
13000 {
13001 prev = vim_strnsave(buf, buflen);
13002 prevlen = buflen;
13003 }
13004 else
13005 {
13006 s = alloc((unsigned)(prevlen + buflen));
13007 if (s != NULL)
13008 {
13009 mch_memmove(s, prev, prevlen);
13010 mch_memmove(s + prevlen, buf, buflen);
13011 vim_free(prev);
13012 prev = s;
13013 prevlen += buflen;
13014 }
13015 }
13016 filtd = 0;
13017 }
13018 else
13019 {
13020 mch_memmove(buf, buf + tolist, buflen - tolist);
13021 filtd -= tolist;
13022 }
13023 }
13024
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013025 /*
13026 * For a negative line count use only the lines at the end of the file,
13027 * free the rest.
13028 */
13029 if (maxline < 0)
13030 while (cnt > -maxline)
13031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013032 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013033 --cnt;
13034 }
13035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013036 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013037 fclose(fd);
13038}
13039
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013040#if defined(FEAT_RELTIME)
13041static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13042
13043/*
13044 * Convert a List to proftime_T.
13045 * Return FAIL when there is something wrong.
13046 */
13047 static int
13048list2proftime(arg, tm)
13049 typval_T *arg;
13050 proftime_T *tm;
13051{
13052 long n1, n2;
13053 int error = FALSE;
13054
13055 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13056 || arg->vval.v_list->lv_len != 2)
13057 return FAIL;
13058 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13059 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13060# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013061 tm->HighPart = n1;
13062 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013063# else
13064 tm->tv_sec = n1;
13065 tm->tv_usec = n2;
13066# endif
13067 return error ? FAIL : OK;
13068}
13069#endif /* FEAT_RELTIME */
13070
13071/*
13072 * "reltime()" function
13073 */
13074 static void
13075f_reltime(argvars, rettv)
13076 typval_T *argvars;
13077 typval_T *rettv;
13078{
13079#ifdef FEAT_RELTIME
13080 proftime_T res;
13081 proftime_T start;
13082
13083 if (argvars[0].v_type == VAR_UNKNOWN)
13084 {
13085 /* No arguments: get current time. */
13086 profile_start(&res);
13087 }
13088 else if (argvars[1].v_type == VAR_UNKNOWN)
13089 {
13090 if (list2proftime(&argvars[0], &res) == FAIL)
13091 return;
13092 profile_end(&res);
13093 }
13094 else
13095 {
13096 /* Two arguments: compute the difference. */
13097 if (list2proftime(&argvars[0], &start) == FAIL
13098 || list2proftime(&argvars[1], &res) == FAIL)
13099 return;
13100 profile_sub(&res, &start);
13101 }
13102
13103 if (rettv_list_alloc(rettv) == OK)
13104 {
13105 long n1, n2;
13106
13107# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013108 n1 = res.HighPart;
13109 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013110# else
13111 n1 = res.tv_sec;
13112 n2 = res.tv_usec;
13113# endif
13114 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13115 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13116 }
13117#endif
13118}
13119
13120/*
13121 * "reltimestr()" function
13122 */
13123 static void
13124f_reltimestr(argvars, rettv)
13125 typval_T *argvars;
13126 typval_T *rettv;
13127{
13128#ifdef FEAT_RELTIME
13129 proftime_T tm;
13130#endif
13131
13132 rettv->v_type = VAR_STRING;
13133 rettv->vval.v_string = NULL;
13134#ifdef FEAT_RELTIME
13135 if (list2proftime(&argvars[0], &tm) == OK)
13136 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13137#endif
13138}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013139
Bram Moolenaar0d660222005-01-07 21:51:51 +000013140#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13141static void make_connection __ARGS((void));
13142static int check_connection __ARGS((void));
13143
13144 static void
13145make_connection()
13146{
13147 if (X_DISPLAY == NULL
13148# ifdef FEAT_GUI
13149 && !gui.in_use
13150# endif
13151 )
13152 {
13153 x_force_connect = TRUE;
13154 setup_term_clip();
13155 x_force_connect = FALSE;
13156 }
13157}
13158
13159 static int
13160check_connection()
13161{
13162 make_connection();
13163 if (X_DISPLAY == NULL)
13164 {
13165 EMSG(_("E240: No connection to Vim server"));
13166 return FAIL;
13167 }
13168 return OK;
13169}
13170#endif
13171
13172#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013173static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013174
13175 static void
13176remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013179 int expr;
13180{
13181 char_u *server_name;
13182 char_u *keys;
13183 char_u *r = NULL;
13184 char_u buf[NUMBUFLEN];
13185# ifdef WIN32
13186 HWND w;
13187# else
13188 Window w;
13189# endif
13190
13191 if (check_restricted() || check_secure())
13192 return;
13193
13194# ifdef FEAT_X11
13195 if (check_connection() == FAIL)
13196 return;
13197# endif
13198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 server_name = get_tv_string_chk(&argvars[0]);
13200 if (server_name == NULL)
13201 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013202 keys = get_tv_string_buf(&argvars[1], buf);
13203# ifdef WIN32
13204 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13205# else
13206 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13207 < 0)
13208# endif
13209 {
13210 if (r != NULL)
13211 EMSG(r); /* sending worked but evaluation failed */
13212 else
13213 EMSG2(_("E241: Unable to send to %s"), server_name);
13214 return;
13215 }
13216
13217 rettv->vval.v_string = r;
13218
13219 if (argvars[2].v_type != VAR_UNKNOWN)
13220 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013222 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013224
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013225 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013226 v.di_tv.v_type = VAR_STRING;
13227 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013228 idvar = get_tv_string_chk(&argvars[2]);
13229 if (idvar != NULL)
13230 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232 }
13233}
13234#endif
13235
13236/*
13237 * "remote_expr()" function
13238 */
13239/*ARGSUSED*/
13240 static void
13241f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244{
13245 rettv->v_type = VAR_STRING;
13246 rettv->vval.v_string = NULL;
13247#ifdef FEAT_CLIENTSERVER
13248 remote_common(argvars, rettv, TRUE);
13249#endif
13250}
13251
13252/*
13253 * "remote_foreground()" function
13254 */
13255/*ARGSUSED*/
13256 static void
13257f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *argvars;
13259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260{
13261 rettv->vval.v_number = 0;
13262#ifdef FEAT_CLIENTSERVER
13263# ifdef WIN32
13264 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013265 {
13266 char_u *server_name = get_tv_string_chk(&argvars[0]);
13267
13268 if (server_name != NULL)
13269 serverForeground(server_name);
13270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013271# else
13272 /* Send a foreground() expression to the server. */
13273 argvars[1].v_type = VAR_STRING;
13274 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13275 argvars[2].v_type = VAR_UNKNOWN;
13276 remote_common(argvars, rettv, TRUE);
13277 vim_free(argvars[1].vval.v_string);
13278# endif
13279#endif
13280}
13281
13282/*ARGSUSED*/
13283 static void
13284f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *argvars;
13286 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013287{
13288#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013289 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013290 char_u *s = NULL;
13291# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013292 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013293# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013295
13296 if (check_restricted() || check_secure())
13297 {
13298 rettv->vval.v_number = -1;
13299 return;
13300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013301 serverid = get_tv_string_chk(&argvars[0]);
13302 if (serverid == NULL)
13303 {
13304 rettv->vval.v_number = -1;
13305 return; /* type error; errmsg already given */
13306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013307# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013308 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013309 if (n == 0)
13310 rettv->vval.v_number = -1;
13311 else
13312 {
13313 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13314 rettv->vval.v_number = (s != NULL);
13315 }
13316# else
13317 rettv->vval.v_number = 0;
13318 if (check_connection() == FAIL)
13319 return;
13320
13321 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323# endif
13324
13325 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 char_u *retvar;
13328
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 v.di_tv.v_type = VAR_STRING;
13330 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013331 retvar = get_tv_string_chk(&argvars[1]);
13332 if (retvar != NULL)
13333 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013335 }
13336#else
13337 rettv->vval.v_number = -1;
13338#endif
13339}
13340
13341/*ARGSUSED*/
13342 static void
13343f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013344 typval_T *argvars;
13345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013346{
13347 char_u *r = NULL;
13348
13349#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013350 char_u *serverid = get_tv_string_chk(&argvars[0]);
13351
13352 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013353 {
13354# ifdef WIN32
13355 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013356 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013357
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013358 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013359 if (n != 0)
13360 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13361 if (r == NULL)
13362# else
13363 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013365# endif
13366 EMSG(_("E277: Unable to read a server reply"));
13367 }
13368#endif
13369 rettv->v_type = VAR_STRING;
13370 rettv->vval.v_string = r;
13371}
13372
13373/*
13374 * "remote_send()" function
13375 */
13376/*ARGSUSED*/
13377 static void
13378f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013381{
13382 rettv->v_type = VAR_STRING;
13383 rettv->vval.v_string = NULL;
13384#ifdef FEAT_CLIENTSERVER
13385 remote_common(argvars, rettv, FALSE);
13386#endif
13387}
13388
13389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013390 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013391 */
13392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013393f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013394 typval_T *argvars;
13395 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013396{
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 list_T *l;
13398 listitem_T *item, *item2;
13399 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013400 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013401 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013402 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 dict_T *d;
13404 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013405
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013406 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013407 if (argvars[0].v_type == VAR_DICT)
13408 {
13409 if (argvars[2].v_type != VAR_UNKNOWN)
13410 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013411 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013412 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 key = get_tv_string_chk(&argvars[1]);
13415 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 di = dict_find(d, key, -1);
13418 if (di == NULL)
13419 EMSG2(_(e_dictkey), key);
13420 else
13421 {
13422 *rettv = di->di_tv;
13423 init_tv(&di->di_tv);
13424 dictitem_remove(d, di);
13425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013426 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013427 }
13428 }
13429 else if (argvars[0].v_type != VAR_LIST)
13430 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013431 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013432 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 int error = FALSE;
13435
13436 idx = get_tv_number_chk(&argvars[1], &error);
13437 if (error)
13438 ; /* type error: do nothing, errmsg already given */
13439 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013440 EMSGN(_(e_listidx), idx);
13441 else
13442 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013443 if (argvars[2].v_type == VAR_UNKNOWN)
13444 {
13445 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013446 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013447 *rettv = item->li_tv;
13448 vim_free(item);
13449 }
13450 else
13451 {
13452 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 end = get_tv_number_chk(&argvars[2], &error);
13454 if (error)
13455 ; /* type error: do nothing */
13456 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013457 EMSGN(_(e_listidx), end);
13458 else
13459 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013460 int cnt = 0;
13461
13462 for (li = item; li != NULL; li = li->li_next)
13463 {
13464 ++cnt;
13465 if (li == item2)
13466 break;
13467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013468 if (li == NULL) /* didn't find "item2" after "item" */
13469 EMSG(_(e_invrange));
13470 else
13471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013472 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013473 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013474 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013475 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013476 l->lv_first = item;
13477 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013478 item->li_prev = NULL;
13479 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013480 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013481 }
13482 }
13483 }
13484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485 }
13486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487}
13488
13489/*
13490 * "rename({from}, {to})" function
13491 */
13492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496{
13497 char_u buf[NUMBUFLEN];
13498
13499 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13503 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504}
13505
13506/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013507 * "repeat()" function
13508 */
13509/*ARGSUSED*/
13510 static void
13511f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013512 typval_T *argvars;
13513 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013514{
13515 char_u *p;
13516 int n;
13517 int slen;
13518 int len;
13519 char_u *r;
13520 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013521
13522 n = get_tv_number(&argvars[1]);
13523 if (argvars[0].v_type == VAR_LIST)
13524 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013525 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013526 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013527 if (list_extend(rettv->vval.v_list,
13528 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013529 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013530 }
13531 else
13532 {
13533 p = get_tv_string(&argvars[0]);
13534 rettv->v_type = VAR_STRING;
13535 rettv->vval.v_string = NULL;
13536
13537 slen = (int)STRLEN(p);
13538 len = slen * n;
13539 if (len <= 0)
13540 return;
13541
13542 r = alloc(len + 1);
13543 if (r != NULL)
13544 {
13545 for (i = 0; i < n; i++)
13546 mch_memmove(r + i * slen, p, (size_t)slen);
13547 r[len] = NUL;
13548 }
13549
13550 rettv->vval.v_string = r;
13551 }
13552}
13553
13554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 * "resolve()" function
13556 */
13557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 typval_T *argvars;
13560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561{
13562 char_u *p;
13563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565#ifdef FEAT_SHORTCUT
13566 {
13567 char_u *v = NULL;
13568
13569 v = mch_resolve_shortcut(p);
13570 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 }
13575#else
13576# ifdef HAVE_READLINK
13577 {
13578 char_u buf[MAXPATHL + 1];
13579 char_u *cpy;
13580 int len;
13581 char_u *remain = NULL;
13582 char_u *q;
13583 int is_relative_to_current = FALSE;
13584 int has_trailing_pathsep = FALSE;
13585 int limit = 100;
13586
13587 p = vim_strsave(p);
13588
13589 if (p[0] == '.' && (vim_ispathsep(p[1])
13590 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13591 is_relative_to_current = TRUE;
13592
13593 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013594 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 has_trailing_pathsep = TRUE;
13596
13597 q = getnextcomp(p);
13598 if (*q != NUL)
13599 {
13600 /* Separate the first path component in "p", and keep the
13601 * remainder (beginning with the path separator). */
13602 remain = vim_strsave(q - 1);
13603 q[-1] = NUL;
13604 }
13605
13606 for (;;)
13607 {
13608 for (;;)
13609 {
13610 len = readlink((char *)p, (char *)buf, MAXPATHL);
13611 if (len <= 0)
13612 break;
13613 buf[len] = NUL;
13614
13615 if (limit-- == 0)
13616 {
13617 vim_free(p);
13618 vim_free(remain);
13619 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 goto fail;
13622 }
13623
13624 /* Ensure that the result will have a trailing path separator
13625 * if the argument has one. */
13626 if (remain == NULL && has_trailing_pathsep)
13627 add_pathsep(buf);
13628
13629 /* Separate the first path component in the link value and
13630 * concatenate the remainders. */
13631 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13632 if (*q != NUL)
13633 {
13634 if (remain == NULL)
13635 remain = vim_strsave(q - 1);
13636 else
13637 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013638 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 if (cpy != NULL)
13640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 vim_free(remain);
13642 remain = cpy;
13643 }
13644 }
13645 q[-1] = NUL;
13646 }
13647
13648 q = gettail(p);
13649 if (q > p && *q == NUL)
13650 {
13651 /* Ignore trailing path separator. */
13652 q[-1] = NUL;
13653 q = gettail(p);
13654 }
13655 if (q > p && !mch_isFullName(buf))
13656 {
13657 /* symlink is relative to directory of argument */
13658 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13659 if (cpy != NULL)
13660 {
13661 STRCPY(cpy, p);
13662 STRCPY(gettail(cpy), buf);
13663 vim_free(p);
13664 p = cpy;
13665 }
13666 }
13667 else
13668 {
13669 vim_free(p);
13670 p = vim_strsave(buf);
13671 }
13672 }
13673
13674 if (remain == NULL)
13675 break;
13676
13677 /* Append the first path component of "remain" to "p". */
13678 q = getnextcomp(remain + 1);
13679 len = q - remain - (*q != NUL);
13680 cpy = vim_strnsave(p, STRLEN(p) + len);
13681 if (cpy != NULL)
13682 {
13683 STRNCAT(cpy, remain, len);
13684 vim_free(p);
13685 p = cpy;
13686 }
13687 /* Shorten "remain". */
13688 if (*q != NUL)
13689 STRCPY(remain, q - 1);
13690 else
13691 {
13692 vim_free(remain);
13693 remain = NULL;
13694 }
13695 }
13696
13697 /* If the result is a relative path name, make it explicitly relative to
13698 * the current directory if and only if the argument had this form. */
13699 if (!vim_ispathsep(*p))
13700 {
13701 if (is_relative_to_current
13702 && *p != NUL
13703 && !(p[0] == '.'
13704 && (p[1] == NUL
13705 || vim_ispathsep(p[1])
13706 || (p[1] == '.'
13707 && (p[2] == NUL
13708 || vim_ispathsep(p[2]))))))
13709 {
13710 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013711 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 if (cpy != NULL)
13713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 vim_free(p);
13715 p = cpy;
13716 }
13717 }
13718 else if (!is_relative_to_current)
13719 {
13720 /* Strip leading "./". */
13721 q = p;
13722 while (q[0] == '.' && vim_ispathsep(q[1]))
13723 q += 2;
13724 if (q > p)
13725 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13726 }
13727 }
13728
13729 /* Ensure that the result will have no trailing path separator
13730 * if the argument had none. But keep "/" or "//". */
13731 if (!has_trailing_pathsep)
13732 {
13733 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013734 if (after_pathsep(p, q))
13735 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736 }
13737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 }
13740# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742# endif
13743#endif
13744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746
13747#ifdef HAVE_READLINK
13748fail:
13749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751}
13752
13753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 */
13756 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013757f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *argvars;
13759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760{
Bram Moolenaar33570922005-01-25 22:26:29 +000013761 list_T *l;
13762 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763
Bram Moolenaar0d660222005-01-07 21:51:51 +000013764 rettv->vval.v_number = 0;
13765 if (argvars[0].v_type != VAR_LIST)
13766 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013767 else if ((l = argvars[0].vval.v_list) != NULL
13768 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769 {
13770 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013771 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013772 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013773 while (li != NULL)
13774 {
13775 ni = li->li_prev;
13776 list_append(l, li);
13777 li = ni;
13778 }
13779 rettv->vval.v_list = l;
13780 rettv->v_type = VAR_LIST;
13781 ++l->lv_refcount;
13782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783}
13784
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013785#define SP_NOMOVE 0x01 /* don't move cursor */
13786#define SP_REPEAT 0x02 /* repeat to find outer pair */
13787#define SP_RETCOUNT 0x04 /* return matchcount */
13788#define SP_SETPCMARK 0x08 /* set previous context mark */
13789#define SP_START 0x10 /* accept match at start position */
13790#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13791#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013792
Bram Moolenaar33570922005-01-25 22:26:29 +000013793static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013794
13795/*
13796 * Get flags for a search function.
13797 * Possibly sets "p_ws".
13798 * Returns BACKWARD, FORWARD or zero (for an error).
13799 */
13800 static int
13801get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013802 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 int *flagsp;
13804{
13805 int dir = FORWARD;
13806 char_u *flags;
13807 char_u nbuf[NUMBUFLEN];
13808 int mask;
13809
13810 if (varp->v_type != VAR_UNKNOWN)
13811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013812 flags = get_tv_string_buf_chk(varp, nbuf);
13813 if (flags == NULL)
13814 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013815 while (*flags != NUL)
13816 {
13817 switch (*flags)
13818 {
13819 case 'b': dir = BACKWARD; break;
13820 case 'w': p_ws = TRUE; break;
13821 case 'W': p_ws = FALSE; break;
13822 default: mask = 0;
13823 if (flagsp != NULL)
13824 switch (*flags)
13825 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013826 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013827 case 'e': mask = SP_END; break;
13828 case 'm': mask = SP_RETCOUNT; break;
13829 case 'n': mask = SP_NOMOVE; break;
13830 case 'p': mask = SP_SUBPAT; break;
13831 case 'r': mask = SP_REPEAT; break;
13832 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013833 }
13834 if (mask == 0)
13835 {
13836 EMSG2(_(e_invarg2), flags);
13837 dir = 0;
13838 }
13839 else
13840 *flagsp |= mask;
13841 }
13842 if (dir == 0)
13843 break;
13844 ++flags;
13845 }
13846 }
13847 return dir;
13848}
13849
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013851 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013853 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013854search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013855 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013856 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013857 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013859 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 char_u *pat;
13861 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013862 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 int save_p_ws = p_ws;
13864 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013865 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013866 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013867 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013868 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013871 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013872 if (dir == 0)
13873 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013874 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013875 if (flags & SP_START)
13876 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013877 if (flags & SP_END)
13878 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013879
13880 /* Optional extra argument: line number to stop searching. */
13881 if (argvars[1].v_type != VAR_UNKNOWN
13882 && argvars[2].v_type != VAR_UNKNOWN)
13883 {
13884 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13885 if (lnum_stop < 0)
13886 goto theend;
13887 }
13888
Bram Moolenaar231334e2005-07-25 20:46:57 +000013889 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013890 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013891 * Check to make sure only those flags are set.
13892 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13893 * flags cannot be set. Check for that condition also.
13894 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013895 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013896 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013899 goto theend;
13900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013902 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013903 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13904 options, RE_SEARCH, (linenr_T)lnum_stop);
13905 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013907 if (flags & SP_SUBPAT)
13908 retval = subpatnum;
13909 else
13910 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013911 if (flags & SP_SETPCMARK)
13912 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013914 if (match_pos != NULL)
13915 {
13916 /* Store the match cursor position */
13917 match_pos->lnum = pos.lnum;
13918 match_pos->col = pos.col + 1;
13919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 /* "/$" will put the cursor after the end of the line, may need to
13921 * correct that here */
13922 check_cursor();
13923 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013924
13925 /* If 'n' flag is used: restore cursor position. */
13926 if (flags & SP_NOMOVE)
13927 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000013928 else
13929 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013930theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013932
13933 return retval;
13934}
13935
13936/*
13937 * "search()" function
13938 */
13939 static void
13940f_search(argvars, rettv)
13941 typval_T *argvars;
13942 typval_T *rettv;
13943{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013944 int flags = 0;
13945
13946 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947}
13948
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013950 * "searchdecl()" function
13951 */
13952 static void
13953f_searchdecl(argvars, rettv)
13954 typval_T *argvars;
13955 typval_T *rettv;
13956{
13957 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013958 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013959 int error = FALSE;
13960 char_u *name;
13961
13962 rettv->vval.v_number = 1; /* default: FAIL */
13963
13964 name = get_tv_string_chk(&argvars[0]);
13965 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013966 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013967 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013968 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13969 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13970 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013971 if (!error && name != NULL)
13972 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013973 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013974}
13975
13976/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013977 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013979 static int
13980searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013981 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013982 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983{
13984 char_u *spat, *mpat, *epat;
13985 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 int dir;
13988 int flags = 0;
13989 char_u nbuf1[NUMBUFLEN];
13990 char_u nbuf2[NUMBUFLEN];
13991 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013992 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013993 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013996 spat = get_tv_string_chk(&argvars[0]);
13997 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13998 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13999 if (spat == NULL || mpat == NULL || epat == NULL)
14000 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 /* Handle the optional fourth argument: flags */
14003 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014004 if (dir == 0)
14005 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014006
14007 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014008 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14009 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014010 if ((flags & (SP_END | SP_SUBPAT)) != 0
14011 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014012 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014013 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014014 goto theend;
14015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014017 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014018 if (argvars[3].v_type == VAR_UNKNOWN
14019 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 skip = (char_u *)"";
14021 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014023 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014024 if (argvars[5].v_type != VAR_UNKNOWN)
14025 {
14026 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14027 if (lnum_stop < 0)
14028 goto theend;
14029 }
14030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 if (skip == NULL)
14032 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014034 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14035 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014036
14037theend:
14038 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014039
14040 return retval;
14041}
14042
14043/*
14044 * "searchpair()" function
14045 */
14046 static void
14047f_searchpair(argvars, rettv)
14048 typval_T *argvars;
14049 typval_T *rettv;
14050{
14051 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14052}
14053
14054/*
14055 * "searchpairpos()" function
14056 */
14057 static void
14058f_searchpairpos(argvars, rettv)
14059 typval_T *argvars;
14060 typval_T *rettv;
14061{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014062 pos_T match_pos;
14063 int lnum = 0;
14064 int col = 0;
14065
14066 rettv->vval.v_number = 0;
14067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014068 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014069 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014070
14071 if (searchpair_cmn(argvars, &match_pos) > 0)
14072 {
14073 lnum = match_pos.lnum;
14074 col = match_pos.col;
14075 }
14076
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014077 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14078 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014079}
14080
14081/*
14082 * Search for a start/middle/end thing.
14083 * Used by searchpair(), see its documentation for the details.
14084 * Returns 0 or -1 for no match,
14085 */
14086 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014087do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014088 char_u *spat; /* start pattern */
14089 char_u *mpat; /* middle pattern */
14090 char_u *epat; /* end pattern */
14091 int dir; /* BACKWARD or FORWARD */
14092 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014093 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014094 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014095 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014096{
14097 char_u *save_cpo;
14098 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14099 long retval = 0;
14100 pos_T pos;
14101 pos_T firstpos;
14102 pos_T foundpos;
14103 pos_T save_cursor;
14104 pos_T save_pos;
14105 int n;
14106 int r;
14107 int nest = 1;
14108 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014109 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014110
14111 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14112 save_cpo = p_cpo;
14113 p_cpo = (char_u *)"";
14114
14115 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14116 * start/middle/end (pat3, for the top pair). */
14117 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14118 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14119 if (pat2 == NULL || pat3 == NULL)
14120 goto theend;
14121 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14122 if (*mpat == NUL)
14123 STRCPY(pat3, pat2);
14124 else
14125 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14126 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014127 if (flags & SP_START)
14128 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014129
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 save_cursor = curwin->w_cursor;
14131 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014132 clearpos(&firstpos);
14133 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 pat = pat3;
14135 for (;;)
14136 {
14137 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014138 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14140 /* didn't find it or found the first match again: FAIL */
14141 break;
14142
14143 if (firstpos.lnum == 0)
14144 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014145 if (equalpos(pos, foundpos))
14146 {
14147 /* Found the same position again. Can happen with a pattern that
14148 * has "\zs" at the end and searching backwards. Advance one
14149 * character and try again. */
14150 if (dir == BACKWARD)
14151 decl(&pos);
14152 else
14153 incl(&pos);
14154 }
14155 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156
14157 /* If the skip pattern matches, ignore this match. */
14158 if (*skip != NUL)
14159 {
14160 save_pos = curwin->w_cursor;
14161 curwin->w_cursor = pos;
14162 r = eval_to_bool(skip, &err, NULL, FALSE);
14163 curwin->w_cursor = save_pos;
14164 if (err)
14165 {
14166 /* Evaluating {skip} caused an error, break here. */
14167 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014168 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 break;
14170 }
14171 if (r)
14172 continue;
14173 }
14174
14175 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14176 {
14177 /* Found end when searching backwards or start when searching
14178 * forward: nested pair. */
14179 ++nest;
14180 pat = pat2; /* nested, don't search for middle */
14181 }
14182 else
14183 {
14184 /* Found end when searching forward or start when searching
14185 * backward: end of (nested) pair; or found middle in outer pair. */
14186 if (--nest == 1)
14187 pat = pat3; /* outer level, search for middle */
14188 }
14189
14190 if (nest == 0)
14191 {
14192 /* Found the match: return matchcount or line number. */
14193 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014194 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014196 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014197 if (flags & SP_SETPCMARK)
14198 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 curwin->w_cursor = pos;
14200 if (!(flags & SP_REPEAT))
14201 break;
14202 nest = 1; /* search for next unmatched */
14203 }
14204 }
14205
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014206 if (match_pos != NULL)
14207 {
14208 /* Store the match cursor position */
14209 match_pos->lnum = curwin->w_cursor.lnum;
14210 match_pos->col = curwin->w_cursor.col + 1;
14211 }
14212
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014214 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 curwin->w_cursor = save_cursor;
14216
14217theend:
14218 vim_free(pat2);
14219 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014221
14222 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223}
14224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014225/*
14226 * "searchpos()" function
14227 */
14228 static void
14229f_searchpos(argvars, rettv)
14230 typval_T *argvars;
14231 typval_T *rettv;
14232{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014233 pos_T match_pos;
14234 int lnum = 0;
14235 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014236 int n;
14237 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014238
14239 rettv->vval.v_number = 0;
14240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014241 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014242 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014243
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014244 n = search_cmn(argvars, &match_pos, &flags);
14245 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014246 {
14247 lnum = match_pos.lnum;
14248 col = match_pos.col;
14249 }
14250
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014251 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14252 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014253 if (flags & SP_SUBPAT)
14254 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014255}
14256
14257
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258/*ARGSUSED*/
14259 static void
14260f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014261 typval_T *argvars;
14262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014264#ifdef FEAT_CLIENTSERVER
14265 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 char_u *server = get_tv_string_chk(&argvars[0]);
14267 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268
Bram Moolenaar0d660222005-01-07 21:51:51 +000014269 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270 if (server == NULL || reply == NULL)
14271 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272 if (check_restricted() || check_secure())
14273 return;
14274# ifdef FEAT_X11
14275 if (check_connection() == FAIL)
14276 return;
14277# endif
14278
14279 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281 EMSG(_("E258: Unable to send to client"));
14282 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014284 rettv->vval.v_number = 0;
14285#else
14286 rettv->vval.v_number = -1;
14287#endif
14288}
14289
14290/*ARGSUSED*/
14291 static void
14292f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 typval_T *argvars;
14294 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014295{
14296 char_u *r = NULL;
14297
14298#ifdef FEAT_CLIENTSERVER
14299# ifdef WIN32
14300 r = serverGetVimNames();
14301# else
14302 make_connection();
14303 if (X_DISPLAY != NULL)
14304 r = serverGetVimNames(X_DISPLAY);
14305# endif
14306#endif
14307 rettv->v_type = VAR_STRING;
14308 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309}
14310
14311/*
14312 * "setbufvar()" function
14313 */
14314/*ARGSUSED*/
14315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014317 typval_T *argvars;
14318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319{
14320 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 char_u nbuf[NUMBUFLEN];
14325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 rettv->vval.v_number = 0;
14327
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 if (check_restricted() || check_secure())
14329 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14331 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 varp = &argvars[2];
14334
14335 if (buf != NULL && varname != NULL && varp != NULL)
14336 {
14337 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339
14340 if (*varname == '&')
14341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 long numval;
14343 char_u *strval;
14344 int error = FALSE;
14345
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014347 numval = get_tv_number_chk(varp, &error);
14348 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 if (!error && strval != NULL)
14350 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 }
14352 else
14353 {
14354 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14355 if (bufvarname != NULL)
14356 {
14357 STRCPY(bufvarname, "b:");
14358 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014359 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 vim_free(bufvarname);
14361 }
14362 }
14363
14364 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
14369/*
14370 * "setcmdpos()" function
14371 */
14372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014374 typval_T *argvars;
14375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 int pos = (int)get_tv_number(&argvars[0]) - 1;
14378
14379 if (pos >= 0)
14380 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381}
14382
14383/*
14384 * "setline()" function
14385 */
14386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
14391 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014392 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014393 list_T *l = NULL;
14394 listitem_T *li = NULL;
14395 long added = 0;
14396 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014398 lnum = get_tv_lnum(&argvars[0]);
14399 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014401 l = argvars[1].vval.v_list;
14402 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014404 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014406
14407 rettv->vval.v_number = 0; /* OK */
14408 for (;;)
14409 {
14410 if (l != NULL)
14411 {
14412 /* list argument, get next string */
14413 if (li == NULL)
14414 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014416 li = li->li_next;
14417 }
14418
14419 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014421 break;
14422 if (lnum <= curbuf->b_ml.ml_line_count)
14423 {
14424 /* existing line, replace it */
14425 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14426 {
14427 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014428 if (lnum == curwin->w_cursor.lnum)
14429 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014430 rettv->vval.v_number = 0; /* OK */
14431 }
14432 }
14433 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14434 {
14435 /* lnum is one past the last line, append the line */
14436 ++added;
14437 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14438 rettv->vval.v_number = 0; /* OK */
14439 }
14440
14441 if (l == NULL) /* only one string argument */
14442 break;
14443 ++lnum;
14444 }
14445
14446 if (added > 0)
14447 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448}
14449
14450/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014451 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014452 */
14453/*ARGSUSED*/
14454 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014455set_qf_ll_list(wp, list_arg, action_arg, rettv)
14456 win_T *wp;
14457 typval_T *list_arg;
14458 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014459 typval_T *rettv;
14460{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014461#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014462 char_u *act;
14463 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014464#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014465
Bram Moolenaar2641f772005-03-25 21:58:17 +000014466 rettv->vval.v_number = -1;
14467
14468#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014469 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014470 EMSG(_(e_listreq));
14471 else
14472 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014473 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014474
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014475 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014476 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014477 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014478 if (act == NULL)
14479 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014480 if (*act == 'a' || *act == 'r')
14481 action = *act;
14482 }
14483
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014484 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014485 rettv->vval.v_number = 0;
14486 }
14487#endif
14488}
14489
14490/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014491 * "setloclist()" function
14492 */
14493/*ARGSUSED*/
14494 static void
14495f_setloclist(argvars, rettv)
14496 typval_T *argvars;
14497 typval_T *rettv;
14498{
14499 win_T *win;
14500
14501 rettv->vval.v_number = -1;
14502
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014503 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014504 if (win != NULL)
14505 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14506}
14507
14508/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014509 * "setpos()" function
14510 */
14511/*ARGSUSED*/
14512 static void
14513f_setpos(argvars, rettv)
14514 typval_T *argvars;
14515 typval_T *rettv;
14516{
14517 pos_T pos;
14518 int fnum;
14519 char_u *name;
14520
14521 name = get_tv_string_chk(argvars);
14522 if (name != NULL)
14523 {
14524 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14525 {
14526 --pos.col;
14527 if (name[0] == '.') /* cursor */
14528 {
14529 if (fnum == curbuf->b_fnum)
14530 {
14531 curwin->w_cursor = pos;
14532 check_cursor();
14533 }
14534 else
14535 EMSG(_(e_invarg));
14536 }
14537 else if (name[0] == '\'') /* mark */
14538 (void)setmark_pos(name[1], &pos, fnum);
14539 else
14540 EMSG(_(e_invarg));
14541 }
14542 }
14543}
14544
14545/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014546 * "setqflist()" function
14547 */
14548/*ARGSUSED*/
14549 static void
14550f_setqflist(argvars, rettv)
14551 typval_T *argvars;
14552 typval_T *rettv;
14553{
14554 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14555}
14556
14557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 * "setreg()" function
14559 */
14560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 typval_T *argvars;
14563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564{
14565 int regname;
14566 char_u *strregname;
14567 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 int append;
14570 char_u yank_type;
14571 long block_len;
14572
14573 block_len = -1;
14574 yank_type = MAUTO;
14575 append = FALSE;
14576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 if (strregname == NULL)
14581 return; /* type error; errmsg already given */
14582 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 if (regname == 0 || regname == '@')
14584 regname = '"';
14585 else if (regname == '=')
14586 return;
14587
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014590 stropt = get_tv_string_chk(&argvars[2]);
14591 if (stropt == NULL)
14592 return; /* type error */
14593 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 switch (*stropt)
14595 {
14596 case 'a': case 'A': /* append */
14597 append = TRUE;
14598 break;
14599 case 'v': case 'c': /* character-wise selection */
14600 yank_type = MCHAR;
14601 break;
14602 case 'V': case 'l': /* line-wise selection */
14603 yank_type = MLINE;
14604 break;
14605#ifdef FEAT_VISUAL
14606 case 'b': case Ctrl_V: /* block-wise selection */
14607 yank_type = MBLOCK;
14608 if (VIM_ISDIGIT(stropt[1]))
14609 {
14610 ++stropt;
14611 block_len = getdigits(&stropt) - 1;
14612 --stropt;
14613 }
14614 break;
14615#endif
14616 }
14617 }
14618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 strval = get_tv_string_chk(&argvars[1]);
14620 if (strval != NULL)
14621 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624}
14625
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014626/*
14627 * "settabwinvar()" function
14628 */
14629 static void
14630f_settabwinvar(argvars, rettv)
14631 typval_T *argvars;
14632 typval_T *rettv;
14633{
14634 setwinvar(argvars, rettv, 1);
14635}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636
14637/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014638 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014645 setwinvar(argvars, rettv, 0);
14646}
14647
14648/*
14649 * "setwinvar()" and "settabwinvar()" functions
14650 */
14651 static void
14652setwinvar(argvars, rettv, off)
14653 typval_T *argvars;
14654 typval_T *rettv;
14655 int off;
14656{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 win_T *win;
14658#ifdef FEAT_WINDOWS
14659 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014660 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661#endif
14662 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014665 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 rettv->vval.v_number = 0;
14668
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 if (check_restricted() || check_secure())
14670 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014671
14672#ifdef FEAT_WINDOWS
14673 if (off == 1)
14674 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14675 else
14676 tp = curtab;
14677#endif
14678 win = find_win_by_nr(&argvars[off], tp);
14679 varname = get_tv_string_chk(&argvars[off + 1]);
14680 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681
14682 if (win != NULL && varname != NULL && varp != NULL)
14683 {
14684#ifdef FEAT_WINDOWS
14685 /* set curwin to be our win, temporarily */
14686 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014687 save_curtab = curtab;
14688 goto_tabpage_tp(tp);
14689 if (!win_valid(win))
14690 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 curwin = win;
14692 curbuf = curwin->w_buffer;
14693#endif
14694
14695 if (*varname == '&')
14696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 long numval;
14698 char_u *strval;
14699 int error = FALSE;
14700
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 numval = get_tv_number_chk(varp, &error);
14703 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014704 if (!error && strval != NULL)
14705 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 }
14707 else
14708 {
14709 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14710 if (winvarname != NULL)
14711 {
14712 STRCPY(winvarname, "w:");
14713 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014714 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 vim_free(winvarname);
14716 }
14717 }
14718
14719#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014720 /* Restore current tabpage and window, if still valid (autocomands can
14721 * make them invalid). */
14722 if (valid_tabpage(save_curtab))
14723 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 if (win_valid(save_curwin))
14725 {
14726 curwin = save_curwin;
14727 curbuf = curwin->w_buffer;
14728 }
14729#endif
14730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731}
14732
14733/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014734 * "shellescape({string})" function
14735 */
14736 static void
14737f_shellescape(argvars, rettv)
14738 typval_T *argvars;
14739 typval_T *rettv;
14740{
14741 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14742 rettv->v_type = VAR_STRING;
14743}
14744
14745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014746 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 */
14748 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 typval_T *argvars;
14751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754
Bram Moolenaar0d660222005-01-07 21:51:51 +000014755 p = get_tv_string(&argvars[0]);
14756 rettv->vval.v_string = vim_strsave(p);
14757 simplify_filename(rettv->vval.v_string); /* simplify in place */
14758 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759}
14760
Bram Moolenaar0d660222005-01-07 21:51:51 +000014761static int
14762#ifdef __BORLANDC__
14763 _RTLENTRYF
14764#endif
14765 item_compare __ARGS((const void *s1, const void *s2));
14766static int
14767#ifdef __BORLANDC__
14768 _RTLENTRYF
14769#endif
14770 item_compare2 __ARGS((const void *s1, const void *s2));
14771
14772static int item_compare_ic;
14773static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775#define ITEM_COMPARE_FAIL 999
14776
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780 static int
14781#ifdef __BORLANDC__
14782_RTLENTRYF
14783#endif
14784item_compare(s1, s2)
14785 const void *s1;
14786 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788 char_u *p1, *p2;
14789 char_u *tofree1, *tofree2;
14790 int res;
14791 char_u numbuf1[NUMBUFLEN];
14792 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014794 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14795 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014796 if (item_compare_ic)
14797 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014799 res = STRCMP(p1, p2);
14800 vim_free(tofree1);
14801 vim_free(tofree2);
14802 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803}
14804
14805 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806#ifdef __BORLANDC__
14807_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014809item_compare2(s1, s2)
14810 const void *s1;
14811 const void *s2;
14812{
14813 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014815 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 /* shortcut after failure in previous call; compare all items equal */
14819 if (item_compare_func_err)
14820 return 0;
14821
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014822 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14823 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014824 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14825 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014826
14827 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014828 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014829 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014830 clear_tv(&argv[0]);
14831 clear_tv(&argv[1]);
14832
14833 if (res == FAIL)
14834 res = ITEM_COMPARE_FAIL;
14835 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014836 /* return value has wrong type */
14837 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14838 if (item_compare_func_err)
14839 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014840 clear_tv(&rettv);
14841 return res;
14842}
14843
14844/*
14845 * "sort({list})" function
14846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014848f_sort(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 Moolenaar33570922005-01-25 22:26:29 +000014852 list_T *l;
14853 listitem_T *li;
14854 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014855 long len;
14856 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857
Bram Moolenaar0d660222005-01-07 21:51:51 +000014858 rettv->vval.v_number = 0;
14859 if (argvars[0].v_type != VAR_LIST)
14860 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 else
14862 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014863 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014864 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865 return;
14866 rettv->vval.v_list = l;
14867 rettv->v_type = VAR_LIST;
14868 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869
Bram Moolenaar0d660222005-01-07 21:51:51 +000014870 len = list_len(l);
14871 if (len <= 1)
14872 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874 item_compare_ic = FALSE;
14875 item_compare_func = NULL;
14876 if (argvars[1].v_type != VAR_UNKNOWN)
14877 {
14878 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014879 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 else
14881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014882 int error = FALSE;
14883
14884 i = get_tv_number_chk(&argvars[1], &error);
14885 if (error)
14886 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014887 if (i == 1)
14888 item_compare_ic = TRUE;
14889 else
14890 item_compare_func = get_tv_string(&argvars[1]);
14891 }
14892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014895 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896 if (ptrs == NULL)
14897 return;
14898 i = 0;
14899 for (li = l->lv_first; li != NULL; li = li->li_next)
14900 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014902 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903 /* test the compare function */
14904 if (item_compare_func != NULL
14905 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14906 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014907 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014909 {
14910 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014911 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912 item_compare_func == NULL ? item_compare : item_compare2);
14913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914 if (!item_compare_func_err)
14915 {
14916 /* Clear the List and append the items in the sorted order. */
14917 l->lv_first = l->lv_last = NULL;
14918 l->lv_len = 0;
14919 for (i = 0; i < len; ++i)
14920 list_append(l, ptrs[i]);
14921 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922 }
14923
14924 vim_free(ptrs);
14925 }
14926}
14927
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014928/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014929 * "soundfold({word})" function
14930 */
14931 static void
14932f_soundfold(argvars, rettv)
14933 typval_T *argvars;
14934 typval_T *rettv;
14935{
14936 char_u *s;
14937
14938 rettv->v_type = VAR_STRING;
14939 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014940#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014941 rettv->vval.v_string = eval_soundfold(s);
14942#else
14943 rettv->vval.v_string = vim_strsave(s);
14944#endif
14945}
14946
14947/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014948 * "spellbadword()" function
14949 */
14950/* ARGSUSED */
14951 static void
14952f_spellbadword(argvars, rettv)
14953 typval_T *argvars;
14954 typval_T *rettv;
14955{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014956 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014957 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014958 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014959
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014960 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014961 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014962
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014963#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014964 if (argvars[0].v_type == VAR_UNKNOWN)
14965 {
14966 /* Find the start and length of the badly spelled word. */
14967 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14968 if (len != 0)
14969 word = ml_get_cursor();
14970 }
14971 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14972 {
14973 char_u *str = get_tv_string_chk(&argvars[0]);
14974 int capcol = -1;
14975
14976 if (str != NULL)
14977 {
14978 /* Check the argument for spelling. */
14979 while (*str != NUL)
14980 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014981 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014982 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014983 {
14984 word = str;
14985 break;
14986 }
14987 str += len;
14988 }
14989 }
14990 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014991#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014992
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014993 list_append_string(rettv->vval.v_list, word, len);
14994 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014995 attr == HLF_SPB ? "bad" :
14996 attr == HLF_SPR ? "rare" :
14997 attr == HLF_SPL ? "local" :
14998 attr == HLF_SPC ? "caps" :
14999 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015000}
15001
15002/*
15003 * "spellsuggest()" function
15004 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015005/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015006 static void
15007f_spellsuggest(argvars, rettv)
15008 typval_T *argvars;
15009 typval_T *rettv;
15010{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015011#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015012 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015013 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015014 int maxcount;
15015 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015016 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015017 listitem_T *li;
15018 int need_capital = FALSE;
15019#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015020
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015021 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015022 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015023
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015024#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015025 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15026 {
15027 str = get_tv_string(&argvars[0]);
15028 if (argvars[1].v_type != VAR_UNKNOWN)
15029 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015030 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015031 if (maxcount <= 0)
15032 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015033 if (argvars[2].v_type != VAR_UNKNOWN)
15034 {
15035 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15036 if (typeerr)
15037 return;
15038 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015039 }
15040 else
15041 maxcount = 25;
15042
Bram Moolenaar4770d092006-01-12 23:22:24 +000015043 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015044
15045 for (i = 0; i < ga.ga_len; ++i)
15046 {
15047 str = ((char_u **)ga.ga_data)[i];
15048
15049 li = listitem_alloc();
15050 if (li == NULL)
15051 vim_free(str);
15052 else
15053 {
15054 li->li_tv.v_type = VAR_STRING;
15055 li->li_tv.v_lock = 0;
15056 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015057 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015058 }
15059 }
15060 ga_clear(&ga);
15061 }
15062#endif
15063}
15064
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015066f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015067 typval_T *argvars;
15068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069{
15070 char_u *str;
15071 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015072 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 regmatch_T regmatch;
15074 char_u patbuf[NUMBUFLEN];
15075 char_u *save_cpo;
15076 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015078 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015079 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080
15081 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15082 save_cpo = p_cpo;
15083 p_cpo = (char_u *)"";
15084
15085 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015086 if (argvars[1].v_type != VAR_UNKNOWN)
15087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15089 if (pat == NULL)
15090 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015093 }
15094 if (pat == NULL || *pat == NUL)
15095 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015096
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015097 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 if (typeerr)
15100 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15103 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015106 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015108 if (*str == NUL)
15109 match = FALSE; /* empty item at the end */
15110 else
15111 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 if (match)
15113 end = regmatch.startp[0];
15114 else
15115 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015116 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15117 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015118 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015119 if (list_append_string(rettv->vval.v_list, str,
15120 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 }
15123 if (!match)
15124 break;
15125 /* Advance to just after the match. */
15126 if (regmatch.endp[0] > str)
15127 col = 0;
15128 else
15129 {
15130 /* Don't get stuck at the same match. */
15131#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015132 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133#else
15134 col = 1;
15135#endif
15136 }
15137 str = regmatch.endp[0];
15138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139
Bram Moolenaar0d660222005-01-07 21:51:51 +000015140 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144}
15145
Bram Moolenaar2c932302006-03-18 21:42:09 +000015146/*
15147 * "str2nr()" function
15148 */
15149 static void
15150f_str2nr(argvars, rettv)
15151 typval_T *argvars;
15152 typval_T *rettv;
15153{
15154 int base = 10;
15155 char_u *p;
15156 long n;
15157
15158 if (argvars[1].v_type != VAR_UNKNOWN)
15159 {
15160 base = get_tv_number(&argvars[1]);
15161 if (base != 8 && base != 10 && base != 16)
15162 {
15163 EMSG(_(e_invarg));
15164 return;
15165 }
15166 }
15167
15168 p = skipwhite(get_tv_string(&argvars[0]));
15169 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15170 rettv->vval.v_number = n;
15171}
15172
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173#ifdef HAVE_STRFTIME
15174/*
15175 * "strftime({format}[, {time}])" function
15176 */
15177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *argvars;
15180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181{
15182 char_u result_buf[256];
15183 struct tm *curtime;
15184 time_t seconds;
15185 char_u *p;
15186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015189 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 seconds = time(NULL);
15192 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 curtime = localtime(&seconds);
15195 /* MSVC returns NULL for an invalid value of seconds. */
15196 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 else
15199 {
15200# ifdef FEAT_MBYTE
15201 vimconv_T conv;
15202 char_u *enc;
15203
15204 conv.vc_type = CONV_NONE;
15205 enc = enc_locale();
15206 convert_setup(&conv, p_enc, enc);
15207 if (conv.vc_type != CONV_NONE)
15208 p = string_convert(&conv, p, NULL);
15209# endif
15210 if (p != NULL)
15211 (void)strftime((char *)result_buf, sizeof(result_buf),
15212 (char *)p, curtime);
15213 else
15214 result_buf[0] = NUL;
15215
15216# ifdef FEAT_MBYTE
15217 if (conv.vc_type != CONV_NONE)
15218 vim_free(p);
15219 convert_setup(&conv, enc, p_enc);
15220 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015221 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 else
15223# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015224 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225
15226# ifdef FEAT_MBYTE
15227 /* Release conversion descriptors */
15228 convert_setup(&conv, NULL, NULL);
15229 vim_free(enc);
15230# endif
15231 }
15232}
15233#endif
15234
15235/*
15236 * "stridx()" function
15237 */
15238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *argvars;
15241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242{
15243 char_u buf[NUMBUFLEN];
15244 char_u *needle;
15245 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015246 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 needle = get_tv_string_chk(&argvars[1]);
15251 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 if (needle == NULL || haystack == NULL)
15254 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255
Bram Moolenaar33570922005-01-25 22:26:29 +000015256 if (argvars[2].v_type != VAR_UNKNOWN)
15257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015258 int error = FALSE;
15259
15260 start_idx = get_tv_number_chk(&argvars[2], &error);
15261 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015263 if (start_idx >= 0)
15264 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015265 }
15266
15267 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15268 if (pos != NULL)
15269 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270}
15271
15272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273 * "string()" function
15274 */
15275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 typval_T *argvars;
15278 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279{
15280 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015281 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015284 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015285 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287}
15288
15289/*
15290 * "strlen()" function
15291 */
15292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015293f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015294 typval_T *argvars;
15295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015297 rettv->vval.v_number = (varnumber_T)(STRLEN(
15298 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299}
15300
15301/*
15302 * "strpart()" function
15303 */
15304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015305f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 typval_T *argvars;
15307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308{
15309 char_u *p;
15310 int n;
15311 int len;
15312 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015315 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 slen = (int)STRLEN(p);
15317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 n = get_tv_number_chk(&argvars[1], &error);
15319 if (error)
15320 len = 0;
15321 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015322 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 else
15324 len = slen - n; /* default len: all bytes that are available. */
15325
15326 /*
15327 * Only return the overlap between the specified part and the actual
15328 * string.
15329 */
15330 if (n < 0)
15331 {
15332 len += n;
15333 n = 0;
15334 }
15335 else if (n > slen)
15336 n = slen;
15337 if (len < 0)
15338 len = 0;
15339 else if (n + len > slen)
15340 len = slen - n;
15341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 rettv->v_type = VAR_STRING;
15343 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344}
15345
15346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015347 * "strridx()" function
15348 */
15349 static void
15350f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015351 typval_T *argvars;
15352 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015353{
15354 char_u buf[NUMBUFLEN];
15355 char_u *needle;
15356 char_u *haystack;
15357 char_u *rest;
15358 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015359 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015361 needle = get_tv_string_chk(&argvars[1]);
15362 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015363
15364 rettv->vval.v_number = -1;
15365 if (needle == NULL || haystack == NULL)
15366 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015367
15368 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015369 if (argvars[2].v_type != VAR_UNKNOWN)
15370 {
15371 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015372 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015373 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015374 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015375 }
15376 else
15377 end_idx = haystack_len;
15378
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015380 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015381 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015382 lastmatch = haystack + end_idx;
15383 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015385 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015386 for (rest = haystack; *rest != '\0'; ++rest)
15387 {
15388 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015389 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015390 break;
15391 lastmatch = rest;
15392 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015393 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015394
15395 if (lastmatch == NULL)
15396 rettv->vval.v_number = -1;
15397 else
15398 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15399}
15400
15401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 * "strtrans()" function
15403 */
15404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015406 typval_T *argvars;
15407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 rettv->v_type = VAR_STRING;
15410 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411}
15412
15413/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015414 * "submatch()" function
15415 */
15416 static void
15417f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015418 typval_T *argvars;
15419 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015420{
15421 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015422 rettv->vval.v_string =
15423 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015424}
15425
15426/*
15427 * "substitute()" function
15428 */
15429 static void
15430f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015431 typval_T *argvars;
15432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015433{
15434 char_u patbuf[NUMBUFLEN];
15435 char_u subbuf[NUMBUFLEN];
15436 char_u flagsbuf[NUMBUFLEN];
15437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015438 char_u *str = get_tv_string_chk(&argvars[0]);
15439 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15440 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15441 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15442
Bram Moolenaar0d660222005-01-07 21:51:51 +000015443 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015444 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15445 rettv->vval.v_string = NULL;
15446 else
15447 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015448}
15449
15450/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015451 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 */
15453/*ARGSUSED*/
15454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 typval_T *argvars;
15457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458{
15459 int id = 0;
15460#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015461 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 long col;
15463 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015464 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015466 lnum = get_tv_lnum(argvars); /* -1 on type error */
15467 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15468 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015471 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015472 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473#endif
15474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476}
15477
15478/*
15479 * "synIDattr(id, what [, mode])" function
15480 */
15481/*ARGSUSED*/
15482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 typval_T *argvars;
15485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486{
15487 char_u *p = NULL;
15488#ifdef FEAT_SYN_HL
15489 int id;
15490 char_u *what;
15491 char_u *mode;
15492 char_u modebuf[NUMBUFLEN];
15493 int modec;
15494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015495 id = get_tv_number(&argvars[0]);
15496 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 modec = TOLOWER_ASC(mode[0]);
15501 if (modec != 't' && modec != 'c'
15502#ifdef FEAT_GUI
15503 && modec != 'g'
15504#endif
15505 )
15506 modec = 0; /* replace invalid with current */
15507 }
15508 else
15509 {
15510#ifdef FEAT_GUI
15511 if (gui.in_use)
15512 modec = 'g';
15513 else
15514#endif
15515 if (t_colors > 1)
15516 modec = 'c';
15517 else
15518 modec = 't';
15519 }
15520
15521
15522 switch (TOLOWER_ASC(what[0]))
15523 {
15524 case 'b':
15525 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15526 p = highlight_color(id, what, modec);
15527 else /* bold */
15528 p = highlight_has_attr(id, HL_BOLD, modec);
15529 break;
15530
15531 case 'f': /* fg[#] */
15532 p = highlight_color(id, what, modec);
15533 break;
15534
15535 case 'i':
15536 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15537 p = highlight_has_attr(id, HL_INVERSE, modec);
15538 else /* italic */
15539 p = highlight_has_attr(id, HL_ITALIC, modec);
15540 break;
15541
15542 case 'n': /* name */
15543 p = get_highlight_name(NULL, id - 1);
15544 break;
15545
15546 case 'r': /* reverse */
15547 p = highlight_has_attr(id, HL_INVERSE, modec);
15548 break;
15549
15550 case 's': /* standout */
15551 p = highlight_has_attr(id, HL_STANDOUT, modec);
15552 break;
15553
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015554 case 'u':
15555 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15556 /* underline */
15557 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15558 else
15559 /* undercurl */
15560 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 break;
15562 }
15563
15564 if (p != NULL)
15565 p = vim_strsave(p);
15566#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->v_type = VAR_STRING;
15568 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569}
15570
15571/*
15572 * "synIDtrans(id)" function
15573 */
15574/*ARGSUSED*/
15575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015577 typval_T *argvars;
15578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579{
15580 int id;
15581
15582#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584
15585 if (id > 0)
15586 id = syn_get_final_id(id);
15587 else
15588#endif
15589 id = 0;
15590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592}
15593
15594/*
15595 * "system()" function
15596 */
15597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015598f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 typval_T *argvars;
15600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015602 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015604 char_u *infile = NULL;
15605 char_u buf[NUMBUFLEN];
15606 int err = FALSE;
15607 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015609 if (check_restricted() || check_secure())
15610 return;
15611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015612 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015613 {
15614 /*
15615 * Write the string to a temp file, to be used for input of the shell
15616 * command.
15617 */
15618 if ((infile = vim_tempname('i')) == NULL)
15619 {
15620 EMSG(_(e_notmp));
15621 return;
15622 }
15623
15624 fd = mch_fopen((char *)infile, WRITEBIN);
15625 if (fd == NULL)
15626 {
15627 EMSG2(_(e_notopen), infile);
15628 goto done;
15629 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015630 p = get_tv_string_buf_chk(&argvars[1], buf);
15631 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015632 {
15633 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015635 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015636 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15637 err = TRUE;
15638 if (fclose(fd) != 0)
15639 err = TRUE;
15640 if (err)
15641 {
15642 EMSG(_("E677: Error writing temp file"));
15643 goto done;
15644 }
15645 }
15646
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015647 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15648 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015649
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650#ifdef USE_CR
15651 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015652 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 {
15654 char_u *s;
15655
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015656 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 {
15658 if (*s == CAR)
15659 *s = NL;
15660 }
15661 }
15662#else
15663# ifdef USE_CRNL
15664 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015665 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 {
15667 char_u *s, *d;
15668
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015669 d = res;
15670 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 {
15672 if (s[0] == CAR && s[1] == NL)
15673 ++s;
15674 *d++ = *s;
15675 }
15676 *d = NUL;
15677 }
15678# endif
15679#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015680
15681done:
15682 if (infile != NULL)
15683 {
15684 mch_remove(infile);
15685 vim_free(infile);
15686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015687 rettv->v_type = VAR_STRING;
15688 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
15691/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015692 * "tabpagebuflist()" function
15693 */
15694/* ARGSUSED */
15695 static void
15696f_tabpagebuflist(argvars, rettv)
15697 typval_T *argvars;
15698 typval_T *rettv;
15699{
15700#ifndef FEAT_WINDOWS
15701 rettv->vval.v_number = 0;
15702#else
15703 tabpage_T *tp;
15704 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015705
15706 if (argvars[0].v_type == VAR_UNKNOWN)
15707 wp = firstwin;
15708 else
15709 {
15710 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15711 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015712 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015713 }
15714 if (wp == NULL)
15715 rettv->vval.v_number = 0;
15716 else
15717 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015718 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015719 rettv->vval.v_number = 0;
15720 else
15721 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015722 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015723 if (list_append_number(rettv->vval.v_list,
15724 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015725 break;
15726 }
15727 }
15728#endif
15729}
15730
15731
15732/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015733 * "tabpagenr()" function
15734 */
15735/* ARGSUSED */
15736 static void
15737f_tabpagenr(argvars, rettv)
15738 typval_T *argvars;
15739 typval_T *rettv;
15740{
15741 int nr = 1;
15742#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015743 char_u *arg;
15744
15745 if (argvars[0].v_type != VAR_UNKNOWN)
15746 {
15747 arg = get_tv_string_chk(&argvars[0]);
15748 nr = 0;
15749 if (arg != NULL)
15750 {
15751 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015752 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015753 else
15754 EMSG2(_(e_invexpr2), arg);
15755 }
15756 }
15757 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015758 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015759#endif
15760 rettv->vval.v_number = nr;
15761}
15762
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015763
15764#ifdef FEAT_WINDOWS
15765static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15766
15767/*
15768 * Common code for tabpagewinnr() and winnr().
15769 */
15770 static int
15771get_winnr(tp, argvar)
15772 tabpage_T *tp;
15773 typval_T *argvar;
15774{
15775 win_T *twin;
15776 int nr = 1;
15777 win_T *wp;
15778 char_u *arg;
15779
15780 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15781 if (argvar->v_type != VAR_UNKNOWN)
15782 {
15783 arg = get_tv_string_chk(argvar);
15784 if (arg == NULL)
15785 nr = 0; /* type error; errmsg already given */
15786 else if (STRCMP(arg, "$") == 0)
15787 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15788 else if (STRCMP(arg, "#") == 0)
15789 {
15790 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15791 if (twin == NULL)
15792 nr = 0;
15793 }
15794 else
15795 {
15796 EMSG2(_(e_invexpr2), arg);
15797 nr = 0;
15798 }
15799 }
15800
15801 if (nr > 0)
15802 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15803 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000015804 {
15805 if (wp == NULL)
15806 {
15807 /* didn't find it in this tabpage */
15808 nr = 0;
15809 break;
15810 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015811 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000015812 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015813 return nr;
15814}
15815#endif
15816
15817/*
15818 * "tabpagewinnr()" function
15819 */
15820/* ARGSUSED */
15821 static void
15822f_tabpagewinnr(argvars, rettv)
15823 typval_T *argvars;
15824 typval_T *rettv;
15825{
15826 int nr = 1;
15827#ifdef FEAT_WINDOWS
15828 tabpage_T *tp;
15829
15830 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15831 if (tp == NULL)
15832 nr = 0;
15833 else
15834 nr = get_winnr(tp, &argvars[1]);
15835#endif
15836 rettv->vval.v_number = nr;
15837}
15838
15839
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015840/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015841 * "tagfiles()" function
15842 */
15843/*ARGSUSED*/
15844 static void
15845f_tagfiles(argvars, rettv)
15846 typval_T *argvars;
15847 typval_T *rettv;
15848{
15849 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015850 tagname_T tn;
15851 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015852
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015853 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015854 {
15855 rettv->vval.v_number = 0;
15856 return;
15857 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015858
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015859 for (first = TRUE; ; first = FALSE)
15860 if (get_tagfname(&tn, first, fname) == FAIL
15861 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015862 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015863 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015864}
15865
15866/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015867 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015868 */
15869 static void
15870f_taglist(argvars, rettv)
15871 typval_T *argvars;
15872 typval_T *rettv;
15873{
15874 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015875
15876 tag_pattern = get_tv_string(&argvars[0]);
15877
15878 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 if (*tag_pattern == NUL)
15880 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015881
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015882 if (rettv_list_alloc(rettv) == OK)
15883 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015884}
15885
15886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 * "tempname()" function
15888 */
15889/*ARGSUSED*/
15890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015892 typval_T *argvars;
15893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894{
15895 static int x = 'A';
15896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015897 rettv->v_type = VAR_STRING;
15898 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899
15900 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15901 * names. Skip 'I' and 'O', they are used for shell redirection. */
15902 do
15903 {
15904 if (x == 'Z')
15905 x = '0';
15906 else if (x == '9')
15907 x = 'A';
15908 else
15909 {
15910#ifdef EBCDIC
15911 if (x == 'I')
15912 x = 'J';
15913 else if (x == 'R')
15914 x = 'S';
15915 else
15916#endif
15917 ++x;
15918 }
15919 } while (x == 'I' || x == 'O');
15920}
15921
15922/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015923 * "test(list)" function: Just checking the walls...
15924 */
15925/*ARGSUSED*/
15926 static void
15927f_test(argvars, rettv)
15928 typval_T *argvars;
15929 typval_T *rettv;
15930{
15931 /* Used for unit testing. Change the code below to your liking. */
15932#if 0
15933 listitem_T *li;
15934 list_T *l;
15935 char_u *bad, *good;
15936
15937 if (argvars[0].v_type != VAR_LIST)
15938 return;
15939 l = argvars[0].vval.v_list;
15940 if (l == NULL)
15941 return;
15942 li = l->lv_first;
15943 if (li == NULL)
15944 return;
15945 bad = get_tv_string(&li->li_tv);
15946 li = li->li_next;
15947 if (li == NULL)
15948 return;
15949 good = get_tv_string(&li->li_tv);
15950 rettv->vval.v_number = test_edit_score(bad, good);
15951#endif
15952}
15953
15954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 * "tolower(string)" function
15956 */
15957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015958f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015959 typval_T *argvars;
15960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961{
15962 char_u *p;
15963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015964 p = vim_strsave(get_tv_string(&argvars[0]));
15965 rettv->v_type = VAR_STRING;
15966 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967
15968 if (p != NULL)
15969 while (*p != NUL)
15970 {
15971#ifdef FEAT_MBYTE
15972 int l;
15973
15974 if (enc_utf8)
15975 {
15976 int c, lc;
15977
15978 c = utf_ptr2char(p);
15979 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015980 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 /* TODO: reallocate string when byte count changes. */
15982 if (utf_char2len(lc) == l)
15983 utf_char2bytes(lc, p);
15984 p += l;
15985 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015986 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 p += l; /* skip multi-byte character */
15988 else
15989#endif
15990 {
15991 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15992 ++p;
15993 }
15994 }
15995}
15996
15997/*
15998 * "toupper(string)" function
15999 */
16000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 typval_T *argvars;
16003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007}
16008
16009/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016010 * "tr(string, fromstr, tostr)" function
16011 */
16012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016013f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 typval_T *argvars;
16015 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016016{
16017 char_u *instr;
16018 char_u *fromstr;
16019 char_u *tostr;
16020 char_u *p;
16021#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016022 int inlen;
16023 int fromlen;
16024 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016025 int idx;
16026 char_u *cpstr;
16027 int cplen;
16028 int first = TRUE;
16029#endif
16030 char_u buf[NUMBUFLEN];
16031 char_u buf2[NUMBUFLEN];
16032 garray_T ga;
16033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016034 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016035 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16036 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016037
16038 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 rettv->v_type = VAR_STRING;
16040 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 if (fromstr == NULL || tostr == NULL)
16042 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016043 ga_init2(&ga, (int)sizeof(char), 80);
16044
16045#ifdef FEAT_MBYTE
16046 if (!has_mbyte)
16047#endif
16048 /* not multi-byte: fromstr and tostr must be the same length */
16049 if (STRLEN(fromstr) != STRLEN(tostr))
16050 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016051#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016052error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016053#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016054 EMSG2(_(e_invarg2), fromstr);
16055 ga_clear(&ga);
16056 return;
16057 }
16058
16059 /* fromstr and tostr have to contain the same number of chars */
16060 while (*instr != NUL)
16061 {
16062#ifdef FEAT_MBYTE
16063 if (has_mbyte)
16064 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016065 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016066 cpstr = instr;
16067 cplen = inlen;
16068 idx = 0;
16069 for (p = fromstr; *p != NUL; p += fromlen)
16070 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016071 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016072 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16073 {
16074 for (p = tostr; *p != NUL; p += tolen)
16075 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016076 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016077 if (idx-- == 0)
16078 {
16079 cplen = tolen;
16080 cpstr = p;
16081 break;
16082 }
16083 }
16084 if (*p == NUL) /* tostr is shorter than fromstr */
16085 goto error;
16086 break;
16087 }
16088 ++idx;
16089 }
16090
16091 if (first && cpstr == instr)
16092 {
16093 /* Check that fromstr and tostr have the same number of
16094 * (multi-byte) characters. Done only once when a character
16095 * of instr doesn't appear in fromstr. */
16096 first = FALSE;
16097 for (p = tostr; *p != NUL; p += tolen)
16098 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016099 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016100 --idx;
16101 }
16102 if (idx != 0)
16103 goto error;
16104 }
16105
16106 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016107 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016108 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016109
16110 instr += inlen;
16111 }
16112 else
16113#endif
16114 {
16115 /* When not using multi-byte chars we can do it faster. */
16116 p = vim_strchr(fromstr, *instr);
16117 if (p != NULL)
16118 ga_append(&ga, tostr[p - fromstr]);
16119 else
16120 ga_append(&ga, *instr);
16121 ++instr;
16122 }
16123 }
16124
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016125 /* add a terminating NUL */
16126 ga_grow(&ga, 1);
16127 ga_append(&ga, NUL);
16128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016129 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016130}
16131
16132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 * "type(expr)" function
16134 */
16135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016137 typval_T *argvars;
16138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016140 int n;
16141
16142 switch (argvars[0].v_type)
16143 {
16144 case VAR_NUMBER: n = 0; break;
16145 case VAR_STRING: n = 1; break;
16146 case VAR_FUNC: n = 2; break;
16147 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016148 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016149 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16150 }
16151 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152}
16153
16154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016155 * "values(dict)" function
16156 */
16157 static void
16158f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *argvars;
16160 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016161{
16162 dict_list(argvars, rettv, 1);
16163}
16164
16165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 * "virtcol(string)" function
16167 */
16168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016169f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016170 typval_T *argvars;
16171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172{
16173 colnr_T vcol = 0;
16174 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016175 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016177 fp = var2fpos(&argvars[0], FALSE, &fnum);
16178 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16179 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 {
16181 getvvcol(curwin, fp, NULL, NULL, &vcol);
16182 ++vcol;
16183 }
16184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186}
16187
16188/*
16189 * "visualmode()" function
16190 */
16191/*ARGSUSED*/
16192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 typval_T *argvars;
16195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196{
16197#ifdef FEAT_VISUAL
16198 char_u str[2];
16199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016200 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 str[0] = curbuf->b_visual_mode_eval;
16202 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204
16205 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016206 if ((argvars[0].v_type == VAR_NUMBER
16207 && argvars[0].vval.v_number != 0)
16208 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016209 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 curbuf->b_visual_mode_eval = NUL;
16211#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213#endif
16214}
16215
16216/*
16217 * "winbufnr(nr)" function
16218 */
16219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 typval_T *argvars;
16222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223{
16224 win_T *wp;
16225
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016226 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016230 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231}
16232
16233/*
16234 * "wincol()" function
16235 */
16236/*ARGSUSED*/
16237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016238f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 typval_T *argvars;
16240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241{
16242 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244}
16245
16246/*
16247 * "winheight(nr)" function
16248 */
16249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016251 typval_T *argvars;
16252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253{
16254 win_T *wp;
16255
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016256 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016258 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261}
16262
16263/*
16264 * "winline()" function
16265 */
16266/*ARGSUSED*/
16267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T *argvars;
16270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
16272 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274}
16275
16276/*
16277 * "winnr()" function
16278 */
16279/* ARGSUSED */
16280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 typval_T *argvars;
16283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284{
16285 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016286
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016288 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016290 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291}
16292
16293/*
16294 * "winrestcmd()" function
16295 */
16296/* ARGSUSED */
16297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 typval_T *argvars;
16300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301{
16302#ifdef FEAT_WINDOWS
16303 win_T *wp;
16304 int winnr = 1;
16305 garray_T ga;
16306 char_u buf[50];
16307
16308 ga_init2(&ga, (int)sizeof(char), 70);
16309 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16310 {
16311 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16312 ga_concat(&ga, buf);
16313# ifdef FEAT_VERTSPLIT
16314 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16315 ga_concat(&ga, buf);
16316# endif
16317 ++winnr;
16318 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016319 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016321 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326}
16327
16328/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016329 * "winrestview()" function
16330 */
16331/* ARGSUSED */
16332 static void
16333f_winrestview(argvars, rettv)
16334 typval_T *argvars;
16335 typval_T *rettv;
16336{
16337 dict_T *dict;
16338
16339 if (argvars[0].v_type != VAR_DICT
16340 || (dict = argvars[0].vval.v_dict) == NULL)
16341 EMSG(_(e_invarg));
16342 else
16343 {
16344 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16345 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16346#ifdef FEAT_VIRTUALEDIT
16347 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16348#endif
16349 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016350 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016351
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016352 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016353#ifdef FEAT_DIFF
16354 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16355#endif
16356 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16357 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16358
16359 check_cursor();
16360 changed_cline_bef_curs();
16361 invalidate_botline();
16362 redraw_later(VALID);
16363
16364 if (curwin->w_topline == 0)
16365 curwin->w_topline = 1;
16366 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16367 curwin->w_topline = curbuf->b_ml.ml_line_count;
16368#ifdef FEAT_DIFF
16369 check_topfill(curwin, TRUE);
16370#endif
16371 }
16372}
16373
16374/*
16375 * "winsaveview()" function
16376 */
16377/* ARGSUSED */
16378 static void
16379f_winsaveview(argvars, rettv)
16380 typval_T *argvars;
16381 typval_T *rettv;
16382{
16383 dict_T *dict;
16384
16385 dict = dict_alloc();
16386 if (dict == NULL)
16387 return;
16388 rettv->v_type = VAR_DICT;
16389 rettv->vval.v_dict = dict;
16390 ++dict->dv_refcount;
16391
16392 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16393 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16394#ifdef FEAT_VIRTUALEDIT
16395 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16396#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016397 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016398 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16399
16400 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16401#ifdef FEAT_DIFF
16402 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16403#endif
16404 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16405 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16406}
16407
16408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 * "winwidth(nr)" function
16410 */
16411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016413 typval_T *argvars;
16414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415{
16416 win_T *wp;
16417
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016418 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 else
16422#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426#endif
16427}
16428
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016430 * "writefile()" function
16431 */
16432 static void
16433f_writefile(argvars, rettv)
16434 typval_T *argvars;
16435 typval_T *rettv;
16436{
16437 int binary = FALSE;
16438 char_u *fname;
16439 FILE *fd;
16440 listitem_T *li;
16441 char_u *s;
16442 int ret = 0;
16443 int c;
16444
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016445 if (check_restricted() || check_secure())
16446 return;
16447
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016448 if (argvars[0].v_type != VAR_LIST)
16449 {
16450 EMSG2(_(e_listarg), "writefile()");
16451 return;
16452 }
16453 if (argvars[0].vval.v_list == NULL)
16454 return;
16455
16456 if (argvars[2].v_type != VAR_UNKNOWN
16457 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16458 binary = TRUE;
16459
16460 /* Always open the file in binary mode, library functions have a mind of
16461 * their own about CR-LF conversion. */
16462 fname = get_tv_string(&argvars[1]);
16463 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16464 {
16465 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16466 ret = -1;
16467 }
16468 else
16469 {
16470 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16471 li = li->li_next)
16472 {
16473 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16474 {
16475 if (*s == '\n')
16476 c = putc(NUL, fd);
16477 else
16478 c = putc(*s, fd);
16479 if (c == EOF)
16480 {
16481 ret = -1;
16482 break;
16483 }
16484 }
16485 if (!binary || li->li_next != NULL)
16486 if (putc('\n', fd) == EOF)
16487 {
16488 ret = -1;
16489 break;
16490 }
16491 if (ret < 0)
16492 {
16493 EMSG(_(e_write));
16494 break;
16495 }
16496 }
16497 fclose(fd);
16498 }
16499
16500 rettv->vval.v_number = ret;
16501}
16502
16503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016505 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 */
16507 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016508var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016511 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016513 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016515 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516
Bram Moolenaara5525202006-03-02 22:52:09 +000016517 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016518 if (varp->v_type == VAR_LIST)
16519 {
16520 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016521 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016522 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016523
16524 l = varp->vval.v_list;
16525 if (l == NULL)
16526 return NULL;
16527
16528 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016529 pos.lnum = list_find_nr(l, 0L, &error);
16530 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016531 return NULL; /* invalid line number */
16532
16533 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016534 pos.col = list_find_nr(l, 1L, &error);
16535 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016536 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016537 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016538 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016539 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016540 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016541 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016542
Bram Moolenaara5525202006-03-02 22:52:09 +000016543#ifdef FEAT_VIRTUALEDIT
16544 /* Get the virtual offset. Defaults to zero. */
16545 pos.coladd = list_find_nr(l, 2L, &error);
16546 if (error)
16547 pos.coladd = 0;
16548#endif
16549
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016550 return &pos;
16551 }
16552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016553 name = get_tv_string_chk(varp);
16554 if (name == NULL)
16555 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 if (name[0] == '.') /* cursor */
16557 return &curwin->w_cursor;
16558 if (name[0] == '\'') /* mark */
16559 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016560 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16562 return NULL;
16563 return pp;
16564 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016565
16566#ifdef FEAT_VIRTUALEDIT
16567 pos.coladd = 0;
16568#endif
16569
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016570 if (name[0] == 'w' && lnum)
16571 {
16572 pos.col = 0;
16573 if (name[1] == '0') /* "w0": first visible line */
16574 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016575 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016576 pos.lnum = curwin->w_topline;
16577 return &pos;
16578 }
16579 else if (name[1] == '$') /* "w$": last visible line */
16580 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016581 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016582 pos.lnum = curwin->w_botline - 1;
16583 return &pos;
16584 }
16585 }
16586 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 {
16588 if (lnum)
16589 {
16590 pos.lnum = curbuf->b_ml.ml_line_count;
16591 pos.col = 0;
16592 }
16593 else
16594 {
16595 pos.lnum = curwin->w_cursor.lnum;
16596 pos.col = (colnr_T)STRLEN(ml_get_curline());
16597 }
16598 return &pos;
16599 }
16600 return NULL;
16601}
16602
16603/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016604 * Convert list in "arg" into a position and optional file number.
16605 * When "fnump" is NULL there is no file number, only 3 items.
16606 * Note that the column is passed on as-is, the caller may want to decrement
16607 * it to use 1 for the first column.
16608 * Return FAIL when conversion is not possible, doesn't check the position for
16609 * validity.
16610 */
16611 static int
16612list2fpos(arg, posp, fnump)
16613 typval_T *arg;
16614 pos_T *posp;
16615 int *fnump;
16616{
16617 list_T *l = arg->vval.v_list;
16618 long i = 0;
16619 long n;
16620
Bram Moolenaarbde35262006-07-23 20:12:24 +000016621 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16622 * when "fnump" isn't NULL and "coladd" is optional. */
16623 if (arg->v_type != VAR_LIST
16624 || l == NULL
16625 || l->lv_len < (fnump == NULL ? 2 : 3)
16626 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016627 return FAIL;
16628
16629 if (fnump != NULL)
16630 {
16631 n = list_find_nr(l, i++, NULL); /* fnum */
16632 if (n < 0)
16633 return FAIL;
16634 if (n == 0)
16635 n = curbuf->b_fnum; /* current buffer */
16636 *fnump = n;
16637 }
16638
16639 n = list_find_nr(l, i++, NULL); /* lnum */
16640 if (n < 0)
16641 return FAIL;
16642 posp->lnum = n;
16643
16644 n = list_find_nr(l, i++, NULL); /* col */
16645 if (n < 0)
16646 return FAIL;
16647 posp->col = n;
16648
16649#ifdef FEAT_VIRTUALEDIT
16650 n = list_find_nr(l, i, NULL);
16651 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016652 posp->coladd = 0;
16653 else
16654 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016655#endif
16656
16657 return OK;
16658}
16659
16660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 * Get the length of an environment variable name.
16662 * Advance "arg" to the first character after the name.
16663 * Return 0 for error.
16664 */
16665 static int
16666get_env_len(arg)
16667 char_u **arg;
16668{
16669 char_u *p;
16670 int len;
16671
16672 for (p = *arg; vim_isIDc(*p); ++p)
16673 ;
16674 if (p == *arg) /* no name found */
16675 return 0;
16676
16677 len = (int)(p - *arg);
16678 *arg = p;
16679 return len;
16680}
16681
16682/*
16683 * Get the length of the name of a function or internal variable.
16684 * "arg" is advanced to the first non-white character after the name.
16685 * Return 0 if something is wrong.
16686 */
16687 static int
16688get_id_len(arg)
16689 char_u **arg;
16690{
16691 char_u *p;
16692 int len;
16693
16694 /* Find the end of the name. */
16695 for (p = *arg; eval_isnamec(*p); ++p)
16696 ;
16697 if (p == *arg) /* no name found */
16698 return 0;
16699
16700 len = (int)(p - *arg);
16701 *arg = skipwhite(p);
16702
16703 return len;
16704}
16705
16706/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016707 * Get the length of the name of a variable or function.
16708 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016710 * Return -1 if curly braces expansion failed.
16711 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 * If the name contains 'magic' {}'s, expand them and return the
16713 * expanded name in an allocated string via 'alias' - caller must free.
16714 */
16715 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016716get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 char_u **arg;
16718 char_u **alias;
16719 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016720 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721{
16722 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 char_u *p;
16724 char_u *expr_start;
16725 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726
16727 *alias = NULL; /* default to no alias */
16728
16729 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16730 && (*arg)[2] == (int)KE_SNR)
16731 {
16732 /* hard coded <SNR>, already translated */
16733 *arg += 3;
16734 return get_id_len(arg) + 3;
16735 }
16736 len = eval_fname_script(*arg);
16737 if (len > 0)
16738 {
16739 /* literal "<SID>", "s:" or "<SNR>" */
16740 *arg += len;
16741 }
16742
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016746 p = find_name_end(*arg, &expr_start, &expr_end,
16747 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 if (expr_start != NULL)
16749 {
16750 char_u *temp_string;
16751
16752 if (!evaluate)
16753 {
16754 len += (int)(p - *arg);
16755 *arg = skipwhite(p);
16756 return len;
16757 }
16758
16759 /*
16760 * Include any <SID> etc in the expanded string:
16761 * Thus the -len here.
16762 */
16763 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16764 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016765 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 *alias = temp_string;
16767 *arg = skipwhite(p);
16768 return (int)STRLEN(temp_string);
16769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770
16771 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016772 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 EMSG2(_(e_invexpr2), *arg);
16774
16775 return len;
16776}
16777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016778/*
16779 * Find the end of a variable or function name, taking care of magic braces.
16780 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16781 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016782 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783 * Return a pointer to just after the name. Equal to "arg" if there is no
16784 * valid name.
16785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016787find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 char_u *arg;
16789 char_u **expr_start;
16790 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016791 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016793 int mb_nest = 0;
16794 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 char_u *p;
16796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 *expr_start = NULL;
16800 *expr_end = NULL;
16801 }
16802
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016803 /* Quick check for valid starting character. */
16804 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16805 return arg;
16806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016807 for (p = arg; *p != NUL
16808 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016809 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016810 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016811 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016812 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016813 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016814 if (*p == '\'')
16815 {
16816 /* skip over 'string' to avoid counting [ and ] inside it. */
16817 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16818 ;
16819 if (*p == NUL)
16820 break;
16821 }
16822 else if (*p == '"')
16823 {
16824 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16825 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16826 if (*p == '\\' && p[1] != NUL)
16827 ++p;
16828 if (*p == NUL)
16829 break;
16830 }
16831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016832 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016834 if (*p == '[')
16835 ++br_nest;
16836 else if (*p == ']')
16837 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 if (*p == '{')
16843 {
16844 mb_nest++;
16845 if (expr_start != NULL && *expr_start == NULL)
16846 *expr_start = p;
16847 }
16848 else if (*p == '}')
16849 {
16850 mb_nest--;
16851 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16852 *expr_end = p;
16853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 }
16856
16857 return p;
16858}
16859
16860/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016861 * Expands out the 'magic' {}'s in a variable/function name.
16862 * Note that this can call itself recursively, to deal with
16863 * constructs like foo{bar}{baz}{bam}
16864 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16865 * "in_start" ^
16866 * "expr_start" ^
16867 * "expr_end" ^
16868 * "in_end" ^
16869 *
16870 * Returns a new allocated string, which the caller must free.
16871 * Returns NULL for failure.
16872 */
16873 static char_u *
16874make_expanded_name(in_start, expr_start, expr_end, in_end)
16875 char_u *in_start;
16876 char_u *expr_start;
16877 char_u *expr_end;
16878 char_u *in_end;
16879{
16880 char_u c1;
16881 char_u *retval = NULL;
16882 char_u *temp_result;
16883 char_u *nextcmd = NULL;
16884
16885 if (expr_end == NULL || in_end == NULL)
16886 return NULL;
16887 *expr_start = NUL;
16888 *expr_end = NUL;
16889 c1 = *in_end;
16890 *in_end = NUL;
16891
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016892 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016893 if (temp_result != NULL && nextcmd == NULL)
16894 {
16895 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16896 + (in_end - expr_end) + 1));
16897 if (retval != NULL)
16898 {
16899 STRCPY(retval, in_start);
16900 STRCAT(retval, temp_result);
16901 STRCAT(retval, expr_end + 1);
16902 }
16903 }
16904 vim_free(temp_result);
16905
16906 *in_end = c1; /* put char back for error messages */
16907 *expr_start = '{';
16908 *expr_end = '}';
16909
16910 if (retval != NULL)
16911 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016912 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016913 if (expr_start != NULL)
16914 {
16915 /* Further expansion! */
16916 temp_result = make_expanded_name(retval, expr_start,
16917 expr_end, temp_result);
16918 vim_free(retval);
16919 retval = temp_result;
16920 }
16921 }
16922
16923 return retval;
16924}
16925
16926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016928 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 */
16930 static int
16931eval_isnamec(c)
16932 int c;
16933{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016934 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16935}
16936
16937/*
16938 * Return TRUE if character "c" can be used as the first character in a
16939 * variable or function name (excluding '{' and '}').
16940 */
16941 static int
16942eval_isnamec1(c)
16943 int c;
16944{
16945 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946}
16947
16948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 * Set number v: variable to "val".
16950 */
16951 void
16952set_vim_var_nr(idx, val)
16953 int idx;
16954 long val;
16955{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016956 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957}
16958
16959/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016960 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 */
16962 long
16963get_vim_var_nr(idx)
16964 int idx;
16965{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016966 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967}
16968
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016969#if defined(FEAT_AUTOCMD) || defined(PROTO)
16970/*
16971 * Get string v: variable value. Uses a static buffer, can only be used once.
16972 */
16973 char_u *
16974get_vim_var_str(idx)
16975 int idx;
16976{
16977 return get_tv_string(&vimvars[idx].vv_tv);
16978}
16979#endif
16980
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981/*
16982 * Set v:count, v:count1 and v:prevcount.
16983 */
16984 void
16985set_vcount(count, count1)
16986 long count;
16987 long count1;
16988{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016989 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16990 vimvars[VV_COUNT].vv_nr = count;
16991 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992}
16993
16994/*
16995 * Set string v: variable to a copy of "val".
16996 */
16997 void
16998set_vim_var_string(idx, val, len)
16999 int idx;
17000 char_u *val;
17001 int len; /* length of "val" to use or -1 (whole string) */
17002{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017003 /* Need to do this (at least) once, since we can't initialize a union.
17004 * Will always be invoked when "v:progname" is set. */
17005 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17006
Bram Moolenaare9a41262005-01-15 22:18:47 +000017007 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017009 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017011 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017013 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014}
17015
17016/*
17017 * Set v:register if needed.
17018 */
17019 void
17020set_reg_var(c)
17021 int c;
17022{
17023 char_u regname;
17024
17025 if (c == 0 || c == ' ')
17026 regname = '"';
17027 else
17028 regname = c;
17029 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017030 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 set_vim_var_string(VV_REG, &regname, 1);
17032}
17033
17034/*
17035 * Get or set v:exception. If "oldval" == NULL, return the current value.
17036 * Otherwise, restore the value to "oldval" and return NULL.
17037 * Must always be called in pairs to save and restore v:exception! Does not
17038 * take care of memory allocations.
17039 */
17040 char_u *
17041v_exception(oldval)
17042 char_u *oldval;
17043{
17044 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017045 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046
Bram Moolenaare9a41262005-01-15 22:18:47 +000017047 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 return NULL;
17049}
17050
17051/*
17052 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17053 * Otherwise, restore the value to "oldval" and return NULL.
17054 * Must always be called in pairs to save and restore v:throwpoint! Does not
17055 * take care of memory allocations.
17056 */
17057 char_u *
17058v_throwpoint(oldval)
17059 char_u *oldval;
17060{
17061 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017062 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063
Bram Moolenaare9a41262005-01-15 22:18:47 +000017064 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 return NULL;
17066}
17067
17068#if defined(FEAT_AUTOCMD) || defined(PROTO)
17069/*
17070 * Set v:cmdarg.
17071 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17072 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17073 * Must always be called in pairs!
17074 */
17075 char_u *
17076set_cmdarg(eap, oldarg)
17077 exarg_T *eap;
17078 char_u *oldarg;
17079{
17080 char_u *oldval;
17081 char_u *newval;
17082 unsigned len;
17083
Bram Moolenaare9a41262005-01-15 22:18:47 +000017084 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017085 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017087 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017088 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017089 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 }
17091
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017092 if (eap->force_bin == FORCE_BIN)
17093 len = 6;
17094 else if (eap->force_bin == FORCE_NOBIN)
17095 len = 8;
17096 else
17097 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017098
17099 if (eap->read_edit)
17100 len += 7;
17101
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017102 if (eap->force_ff != 0)
17103 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17104# ifdef FEAT_MBYTE
17105 if (eap->force_enc != 0)
17106 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017107 if (eap->bad_char != 0)
17108 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017109# endif
17110
17111 newval = alloc(len + 1);
17112 if (newval == NULL)
17113 return NULL;
17114
17115 if (eap->force_bin == FORCE_BIN)
17116 sprintf((char *)newval, " ++bin");
17117 else if (eap->force_bin == FORCE_NOBIN)
17118 sprintf((char *)newval, " ++nobin");
17119 else
17120 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017121
17122 if (eap->read_edit)
17123 STRCAT(newval, " ++edit");
17124
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017125 if (eap->force_ff != 0)
17126 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17127 eap->cmd + eap->force_ff);
17128# ifdef FEAT_MBYTE
17129 if (eap->force_enc != 0)
17130 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17131 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017132 if (eap->bad_char != 0)
17133 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17134 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017135# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017136 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017137 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138}
17139#endif
17140
17141/*
17142 * Get the value of internal variable "name".
17143 * Return OK or FAIL.
17144 */
17145 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017146get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 char_u *name;
17148 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017150 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151{
17152 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017153 typval_T *tv = NULL;
17154 typval_T atv;
17155 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157
17158 /* truncate the name, so that we can use strcmp() */
17159 cc = name[len];
17160 name[len] = NUL;
17161
17162 /*
17163 * Check for "b:changedtick".
17164 */
17165 if (STRCMP(name, "b:changedtick") == 0)
17166 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017167 atv.v_type = VAR_NUMBER;
17168 atv.vval.v_number = curbuf->b_changedtick;
17169 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 }
17171
17172 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 * Check for user-defined variables.
17174 */
17175 else
17176 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017177 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017179 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 }
17181
Bram Moolenaare9a41262005-01-15 22:18:47 +000017182 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017184 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 ret = FAIL;
17187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017189 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190
17191 name[len] = cc;
17192
17193 return ret;
17194}
17195
17196/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017197 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17198 * Also handle function call with Funcref variable: func(expr)
17199 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17200 */
17201 static int
17202handle_subscript(arg, rettv, evaluate, verbose)
17203 char_u **arg;
17204 typval_T *rettv;
17205 int evaluate; /* do more than finding the end */
17206 int verbose; /* give error messages */
17207{
17208 int ret = OK;
17209 dict_T *selfdict = NULL;
17210 char_u *s;
17211 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017212 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017213
17214 while (ret == OK
17215 && (**arg == '['
17216 || (**arg == '.' && rettv->v_type == VAR_DICT)
17217 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17218 && !vim_iswhite(*(*arg - 1)))
17219 {
17220 if (**arg == '(')
17221 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017222 /* need to copy the funcref so that we can clear rettv */
17223 functv = *rettv;
17224 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017225
17226 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017227 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017228 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017229 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17230 &len, evaluate, selfdict);
17231
17232 /* Clear the funcref afterwards, so that deleting it while
17233 * evaluating the arguments is possible (see test55). */
17234 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017235
17236 /* Stop the expression evaluation when immediately aborting on
17237 * error, or when an interrupt occurred or an exception was thrown
17238 * but not caught. */
17239 if (aborting())
17240 {
17241 if (ret == OK)
17242 clear_tv(rettv);
17243 ret = FAIL;
17244 }
17245 dict_unref(selfdict);
17246 selfdict = NULL;
17247 }
17248 else /* **arg == '[' || **arg == '.' */
17249 {
17250 dict_unref(selfdict);
17251 if (rettv->v_type == VAR_DICT)
17252 {
17253 selfdict = rettv->vval.v_dict;
17254 if (selfdict != NULL)
17255 ++selfdict->dv_refcount;
17256 }
17257 else
17258 selfdict = NULL;
17259 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17260 {
17261 clear_tv(rettv);
17262 ret = FAIL;
17263 }
17264 }
17265 }
17266 dict_unref(selfdict);
17267 return ret;
17268}
17269
17270/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17272 * value).
17273 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017276{
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017278}
17279
17280/*
17281 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 * The string "s" must have been allocated, it is consumed.
17283 * Return NULL for out of memory, the variable otherwise.
17284 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017285 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 char_u *s;
17288{
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291 rettv = alloc_tv();
17292 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294 rettv->v_type = VAR_STRING;
17295 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 }
17297 else
17298 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300}
17301
17302/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017303 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017305 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308{
17309 if (varp != NULL)
17310 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017311 switch (varp->v_type)
17312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017313 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017314 func_unref(varp->vval.v_string);
17315 /*FALLTHROUGH*/
17316 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017317 vim_free(varp->vval.v_string);
17318 break;
17319 case VAR_LIST:
17320 list_unref(varp->vval.v_list);
17321 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017322 case VAR_DICT:
17323 dict_unref(varp->vval.v_dict);
17324 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017325 case VAR_NUMBER:
17326 case VAR_UNKNOWN:
17327 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017328 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017329 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017330 break;
17331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 vim_free(varp);
17333 }
17334}
17335
17336/*
17337 * Free the memory for a variable value and set the value to NULL or 0.
17338 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017339 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342{
17343 if (varp != NULL)
17344 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017347 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017348 func_unref(varp->vval.v_string);
17349 /*FALLTHROUGH*/
17350 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017351 vim_free(varp->vval.v_string);
17352 varp->vval.v_string = NULL;
17353 break;
17354 case VAR_LIST:
17355 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017356 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017357 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017358 case VAR_DICT:
17359 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017360 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017361 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017363 varp->vval.v_number = 0;
17364 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017365 case VAR_UNKNOWN:
17366 break;
17367 default:
17368 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017370 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 }
17372}
17373
17374/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 * Set the value of a variable to NULL without freeing items.
17376 */
17377 static void
17378init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380{
17381 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383}
17384
17385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 * Get the number value of a variable.
17387 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 * For incompatible types, return 0.
17389 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17390 * caller of incompatible types: it sets *denote to TRUE if "denote"
17391 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 */
17393 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017395 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017397 int error = FALSE;
17398
17399 return get_tv_number_chk(varp, &error); /* return 0L on error */
17400}
17401
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017402 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017403get_tv_number_chk(varp, denote)
17404 typval_T *varp;
17405 int *denote;
17406{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017407 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017409 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017411 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017412 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017413 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017414 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017415 break;
17416 case VAR_STRING:
17417 if (varp->vval.v_string != NULL)
17418 vim_str2nr(varp->vval.v_string, NULL, NULL,
17419 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017420 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017421 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017422 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017423 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017424 case VAR_DICT:
17425 EMSG(_("E728: Using a Dictionary as a number"));
17426 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017427 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017428 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017431 if (denote == NULL) /* useful for values that must be unsigned */
17432 n = -1;
17433 else
17434 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017435 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436}
17437
17438/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017439 * Get the lnum from the first argument.
17440 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 */
17443 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017444get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017445 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446{
Bram Moolenaar33570922005-01-25 22:26:29 +000017447 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 linenr_T lnum;
17449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017450 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 if (lnum == 0) /* no valid number, try using line() */
17452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017453 rettv.v_type = VAR_NUMBER;
17454 f_line(argvars, &rettv);
17455 lnum = rettv.vval.v_number;
17456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 }
17458 return lnum;
17459}
17460
17461/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017462 * Get the lnum from the first argument.
17463 * Also accepts "$", then "buf" is used.
17464 * Returns 0 on error.
17465 */
17466 static linenr_T
17467get_tv_lnum_buf(argvars, buf)
17468 typval_T *argvars;
17469 buf_T *buf;
17470{
17471 if (argvars[0].v_type == VAR_STRING
17472 && argvars[0].vval.v_string != NULL
17473 && argvars[0].vval.v_string[0] == '$'
17474 && buf != NULL)
17475 return buf->b_ml.ml_line_count;
17476 return get_tv_number_chk(&argvars[0], NULL);
17477}
17478
17479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 * Get the string value of a variable.
17481 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017482 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17483 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 * If the String variable has never been set, return an empty string.
17485 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17487 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 */
17489 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492{
17493 static char_u mybuf[NUMBUFLEN];
17494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496}
17497
17498 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017501 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017503 char_u *res = get_tv_string_buf_chk(varp, buf);
17504
17505 return res != NULL ? res : (char_u *)"";
17506}
17507
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017508 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017509get_tv_string_chk(varp)
17510 typval_T *varp;
17511{
17512 static char_u mybuf[NUMBUFLEN];
17513
17514 return get_tv_string_buf_chk(varp, mybuf);
17515}
17516
17517 static char_u *
17518get_tv_string_buf_chk(varp, buf)
17519 typval_T *varp;
17520 char_u *buf;
17521{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017522 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017524 case VAR_NUMBER:
17525 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17526 return buf;
17527 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017528 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017529 break;
17530 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017531 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017532 break;
17533 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017534 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017535 break;
17536 case VAR_STRING:
17537 if (varp->vval.v_string != NULL)
17538 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017540 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017541 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545}
17546
17547/*
17548 * Find variable "name" in the list of variables.
17549 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017550 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017551 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017552 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017554 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017555find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017557 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561
Bram Moolenaara7043832005-01-21 11:56:39 +000017562 ht = find_var_ht(name, &varname);
17563 if (htp != NULL)
17564 *htp = ht;
17565 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017567 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568}
17569
17570/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017571 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017572 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017574 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017575find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017577 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017578 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017579{
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 hashitem_T *hi;
17581
17582 if (*varname == NUL)
17583 {
17584 /* Must be something like "s:", otherwise "ht" would be NULL. */
17585 switch (varname[-2])
17586 {
17587 case 's': return &SCRIPT_SV(current_SID).sv_var;
17588 case 'g': return &globvars_var;
17589 case 'v': return &vimvars_var;
17590 case 'b': return &curbuf->b_bufvar;
17591 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017592#ifdef FEAT_WINDOWS
17593 case 't': return &curtab->tp_winvar;
17594#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017595 case 'l': return current_funccal == NULL
17596 ? NULL : &current_funccal->l_vars_var;
17597 case 'a': return current_funccal == NULL
17598 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 }
17600 return NULL;
17601 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017602
17603 hi = hash_find(ht, varname);
17604 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017605 {
17606 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017607 * worked find the variable again. Don't auto-load a script if it was
17608 * loaded already, otherwise it would be loaded every time when
17609 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017610 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017611 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017612 hi = hash_find(ht, varname);
17613 if (HASHITEM_EMPTY(hi))
17614 return NULL;
17615 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017617}
17618
17619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017620 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017621 * Set "varname" to the start of name without ':'.
17622 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017624find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 char_u *name;
17626 char_u **varname;
17627{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017628 hashitem_T *hi;
17629
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 if (name[1] != ':')
17631 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017632 /* The name must not start with a colon or #. */
17633 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 return NULL;
17635 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017636
17637 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017638 hi = hash_find(&compat_hashtab, name);
17639 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017640 return &compat_hashtab;
17641
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 return &globvarht; /* global variable */
17644 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 }
17646 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017647 if (*name == 'g') /* global variable */
17648 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017649 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17650 */
17651 if (vim_strchr(name + 2, ':') != NULL
17652 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017653 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017655 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017657 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017658#ifdef FEAT_WINDOWS
17659 if (*name == 't') /* tab page variable */
17660 return &curtab->tp_vars.dv_hashtab;
17661#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 if (*name == 'v') /* v: variable */
17663 return &vimvarht;
17664 if (*name == 'a' && current_funccal != NULL) /* function argument */
17665 return &current_funccal->l_avars.dv_hashtab;
17666 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17667 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 if (*name == 's' /* script variable */
17669 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17670 return &SCRIPT_VARS(current_SID);
17671 return NULL;
17672}
17673
17674/*
17675 * Get the string value of a (global/local) variable.
17676 * Returns NULL when it doesn't exist.
17677 */
17678 char_u *
17679get_var_value(name)
17680 char_u *name;
17681{
Bram Moolenaar33570922005-01-25 22:26:29 +000017682 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683
Bram Moolenaara7043832005-01-21 11:56:39 +000017684 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 if (v == NULL)
17686 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688}
17689
17690/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 * sourcing this script and when executing functions defined in the script.
17693 */
17694 void
17695new_script_vars(id)
17696 scid_T id;
17697{
Bram Moolenaara7043832005-01-21 11:56:39 +000017698 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 hashtab_T *ht;
17700 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017701
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17703 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017704 /* Re-allocating ga_data means that an ht_array pointing to
17705 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017706 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017707 for (i = 1; i <= ga_scripts.ga_len; ++i)
17708 {
17709 ht = &SCRIPT_VARS(i);
17710 if (ht->ht_mask == HT_INIT_SIZE - 1)
17711 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 sv = &SCRIPT_SV(i);
17713 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017714 }
17715
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 while (ga_scripts.ga_len < id)
17717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17719 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 }
17722 }
17723}
17724
17725/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17727 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 */
17729 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017730init_var_dict(dict, dict_var)
17731 dict_T *dict;
17732 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733{
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 hash_init(&dict->dv_hashtab);
17735 dict->dv_refcount = 99999;
17736 dict_var->di_tv.vval.v_dict = dict;
17737 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017738 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17740 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741}
17742
17743/*
17744 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017745 * Frees all allocated variables and the value they contain.
17746 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 */
17748 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017749vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 hashtab_T *ht;
17751{
17752 vars_clear_ext(ht, TRUE);
17753}
17754
17755/*
17756 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17757 */
17758 static void
17759vars_clear_ext(ht, free_val)
17760 hashtab_T *ht;
17761 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762{
Bram Moolenaara7043832005-01-21 11:56:39 +000017763 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 hashitem_T *hi;
17765 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017768 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017769 for (hi = ht->ht_array; todo > 0; ++hi)
17770 {
17771 if (!HASHITEM_EMPTY(hi))
17772 {
17773 --todo;
17774
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017776 * ht_array might change then. hash_clear() takes care of it
17777 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 v = HI2DI(hi);
17779 if (free_val)
17780 clear_tv(&v->di_tv);
17781 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17782 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017783 }
17784 }
17785 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017786 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787}
17788
Bram Moolenaara7043832005-01-21 11:56:39 +000017789/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 * Delete a variable from hashtab "ht" at item "hi".
17791 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017794delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017795 hashtab_T *ht;
17796 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797{
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017799
17800 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 clear_tv(&di->di_tv);
17802 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803}
17804
17805/*
17806 * List the value of one internal variable.
17807 */
17808 static void
17809list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 char_u *prefix;
17812{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017813 char_u *tofree;
17814 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017816
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017817 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017818 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017819 s == NULL ? (char_u *)"" : s);
17820 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821}
17822
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 static void
17824list_one_var_a(prefix, name, type, string)
17825 char_u *prefix;
17826 char_u *name;
17827 int type;
17828 char_u *string;
17829{
17830 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17831 if (name != NULL) /* "a:" vars don't have a name stored */
17832 msg_puts(name);
17833 msg_putchar(' ');
17834 msg_advance(22);
17835 if (type == VAR_NUMBER)
17836 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017837 else if (type == VAR_FUNC)
17838 msg_putchar('*');
17839 else if (type == VAR_LIST)
17840 {
17841 msg_putchar('[');
17842 if (*string == '[')
17843 ++string;
17844 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017845 else if (type == VAR_DICT)
17846 {
17847 msg_putchar('{');
17848 if (*string == '{')
17849 ++string;
17850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 else
17852 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017853
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017855
17856 if (type == VAR_FUNC)
17857 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858}
17859
17860/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017861 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 * If the variable already exists, the value is updated.
17863 * Otherwise the variable is created.
17864 */
17865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017866set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017868 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870{
Bram Moolenaar33570922005-01-25 22:26:29 +000017871 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017874 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017876 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017877 {
17878 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17879 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17880 ? name[2] : name[0]))
17881 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017882 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017883 return;
17884 }
17885 if (function_exists(name))
17886 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017887 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017888 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017889 return;
17890 }
17891 }
17892
Bram Moolenaara7043832005-01-21 11:56:39 +000017893 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017894 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017895 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017896 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017897 return;
17898 }
17899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017900 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017901 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017904 if (var_check_ro(v->di_flags, name)
17905 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017906 return;
17907 if (v->di_tv.v_type != tv->v_type
17908 && !((v->di_tv.v_type == VAR_STRING
17909 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017910 && (tv->v_type == VAR_STRING
17911 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017912 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017913 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017914 return;
17915 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017916
17917 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017918 * Handle setting internal v: variables separately: we don't change
17919 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017920 */
17921 if (ht == &vimvarht)
17922 {
17923 if (v->di_tv.v_type == VAR_STRING)
17924 {
17925 vim_free(v->di_tv.vval.v_string);
17926 if (copy || tv->v_type != VAR_STRING)
17927 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17928 else
17929 {
17930 /* Take over the string to avoid an extra alloc/free. */
17931 v->di_tv.vval.v_string = tv->vval.v_string;
17932 tv->vval.v_string = NULL;
17933 }
17934 }
17935 else if (v->di_tv.v_type != VAR_NUMBER)
17936 EMSG2(_(e_intern2), "set_var()");
17937 else
17938 v->di_tv.vval.v_number = get_tv_number(tv);
17939 return;
17940 }
17941
17942 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 }
17944 else /* add a new variable */
17945 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017946 /* Can't add "v:" variable. */
17947 if (ht == &vimvarht)
17948 {
17949 EMSG2(_(e_illvar), name);
17950 return;
17951 }
17952
Bram Moolenaar92124a32005-06-17 22:03:40 +000017953 /* Make sure the variable name is valid. */
17954 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017955 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17956 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017957 {
17958 EMSG2(_(e_illvar), varname);
17959 return;
17960 }
17961
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017962 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17963 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017964 if (v == NULL)
17965 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017966 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017969 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 return;
17971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017972 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017975 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017976 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017977 else
17978 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017979 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017980 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983}
17984
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017985/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017986 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 * Also give an error message.
17988 */
17989 static int
17990var_check_ro(flags, name)
17991 int flags;
17992 char_u *name;
17993{
17994 if (flags & DI_FLAGS_RO)
17995 {
17996 EMSG2(_(e_readonlyvar), name);
17997 return TRUE;
17998 }
17999 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18000 {
18001 EMSG2(_(e_readonlysbx), name);
18002 return TRUE;
18003 }
18004 return FALSE;
18005}
18006
18007/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018008 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18009 * Also give an error message.
18010 */
18011 static int
18012var_check_fixed(flags, name)
18013 int flags;
18014 char_u *name;
18015{
18016 if (flags & DI_FLAGS_FIX)
18017 {
18018 EMSG2(_("E795: Cannot delete variable %s"), name);
18019 return TRUE;
18020 }
18021 return FALSE;
18022}
18023
18024/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018025 * Return TRUE if typeval "tv" is set to be locked (immutable).
18026 * Also give an error message, using "name".
18027 */
18028 static int
18029tv_check_lock(lock, name)
18030 int lock;
18031 char_u *name;
18032{
18033 if (lock & VAR_LOCKED)
18034 {
18035 EMSG2(_("E741: Value is locked: %s"),
18036 name == NULL ? (char_u *)_("Unknown") : name);
18037 return TRUE;
18038 }
18039 if (lock & VAR_FIXED)
18040 {
18041 EMSG2(_("E742: Cannot change value of %s"),
18042 name == NULL ? (char_u *)_("Unknown") : name);
18043 return TRUE;
18044 }
18045 return FALSE;
18046}
18047
18048/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018049 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018050 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018051 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T *from;
18056 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018058 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018059 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018060 switch (from->v_type)
18061 {
18062 case VAR_NUMBER:
18063 to->vval.v_number = from->vval.v_number;
18064 break;
18065 case VAR_STRING:
18066 case VAR_FUNC:
18067 if (from->vval.v_string == NULL)
18068 to->vval.v_string = NULL;
18069 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018070 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018071 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018072 if (from->v_type == VAR_FUNC)
18073 func_ref(to->vval.v_string);
18074 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018075 break;
18076 case VAR_LIST:
18077 if (from->vval.v_list == NULL)
18078 to->vval.v_list = NULL;
18079 else
18080 {
18081 to->vval.v_list = from->vval.v_list;
18082 ++to->vval.v_list->lv_refcount;
18083 }
18084 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018085 case VAR_DICT:
18086 if (from->vval.v_dict == NULL)
18087 to->vval.v_dict = NULL;
18088 else
18089 {
18090 to->vval.v_dict = from->vval.v_dict;
18091 ++to->vval.v_dict->dv_refcount;
18092 }
18093 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018094 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018095 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018096 break;
18097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098}
18099
18100/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018101 * Make a copy of an item.
18102 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018103 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18104 * reference to an already copied list/dict can be used.
18105 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018106 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018107 static int
18108item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018109 typval_T *from;
18110 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018111 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018112 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018113{
18114 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018115 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018116
Bram Moolenaar33570922005-01-25 22:26:29 +000018117 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018118 {
18119 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018120 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018121 }
18122 ++recurse;
18123
18124 switch (from->v_type)
18125 {
18126 case VAR_NUMBER:
18127 case VAR_STRING:
18128 case VAR_FUNC:
18129 copy_tv(from, to);
18130 break;
18131 case VAR_LIST:
18132 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018133 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018134 if (from->vval.v_list == NULL)
18135 to->vval.v_list = NULL;
18136 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18137 {
18138 /* use the copy made earlier */
18139 to->vval.v_list = from->vval.v_list->lv_copylist;
18140 ++to->vval.v_list->lv_refcount;
18141 }
18142 else
18143 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18144 if (to->vval.v_list == NULL)
18145 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018146 break;
18147 case VAR_DICT:
18148 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018149 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018150 if (from->vval.v_dict == NULL)
18151 to->vval.v_dict = NULL;
18152 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18153 {
18154 /* use the copy made earlier */
18155 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18156 ++to->vval.v_dict->dv_refcount;
18157 }
18158 else
18159 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18160 if (to->vval.v_dict == NULL)
18161 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018162 break;
18163 default:
18164 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018165 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018166 }
18167 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018168 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018169}
18170
18171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 * ":echo expr1 ..." print each argument separated with a space, add a
18173 * newline at the end.
18174 * ":echon expr1 ..." print each argument plain.
18175 */
18176 void
18177ex_echo(eap)
18178 exarg_T *eap;
18179{
18180 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018181 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018182 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 char_u *p;
18184 int needclr = TRUE;
18185 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018186 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187
18188 if (eap->skip)
18189 ++emsg_skip;
18190 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18191 {
18192 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018193 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 {
18195 /*
18196 * Report the invalid expression unless the expression evaluation
18197 * has been cancelled due to an aborting error, an interrupt, or an
18198 * exception.
18199 */
18200 if (!aborting())
18201 EMSG2(_(e_invexpr2), p);
18202 break;
18203 }
18204 if (!eap->skip)
18205 {
18206 if (atstart)
18207 {
18208 atstart = FALSE;
18209 /* Call msg_start() after eval1(), evaluating the expression
18210 * may cause a message to appear. */
18211 if (eap->cmdidx == CMD_echo)
18212 msg_start();
18213 }
18214 else if (eap->cmdidx == CMD_echo)
18215 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018216 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018217 if (p != NULL)
18218 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018220 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018222 if (*p != TAB && needclr)
18223 {
18224 /* remove any text still there from the command */
18225 msg_clr_eos();
18226 needclr = FALSE;
18227 }
18228 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 }
18230 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018231 {
18232#ifdef FEAT_MBYTE
18233 if (has_mbyte)
18234 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018235 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018236
18237 (void)msg_outtrans_len_attr(p, i, echo_attr);
18238 p += i - 1;
18239 }
18240 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018242 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018245 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 arg = skipwhite(arg);
18249 }
18250 eap->nextcmd = check_nextcmd(arg);
18251
18252 if (eap->skip)
18253 --emsg_skip;
18254 else
18255 {
18256 /* remove text that may still be there from the command */
18257 if (needclr)
18258 msg_clr_eos();
18259 if (eap->cmdidx == CMD_echo)
18260 msg_end();
18261 }
18262}
18263
18264/*
18265 * ":echohl {name}".
18266 */
18267 void
18268ex_echohl(eap)
18269 exarg_T *eap;
18270{
18271 int id;
18272
18273 id = syn_name2id(eap->arg);
18274 if (id == 0)
18275 echo_attr = 0;
18276 else
18277 echo_attr = syn_id2attr(id);
18278}
18279
18280/*
18281 * ":execute expr1 ..." execute the result of an expression.
18282 * ":echomsg expr1 ..." Print a message
18283 * ":echoerr expr1 ..." Print an error
18284 * Each gets spaces around each argument and a newline at the end for
18285 * echo commands
18286 */
18287 void
18288ex_execute(eap)
18289 exarg_T *eap;
18290{
18291 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018292 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 int ret = OK;
18294 char_u *p;
18295 garray_T ga;
18296 int len;
18297 int save_did_emsg;
18298
18299 ga_init2(&ga, 1, 80);
18300
18301 if (eap->skip)
18302 ++emsg_skip;
18303 while (*arg != NUL && *arg != '|' && *arg != '\n')
18304 {
18305 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 {
18308 /*
18309 * Report the invalid expression unless the expression evaluation
18310 * has been cancelled due to an aborting error, an interrupt, or an
18311 * exception.
18312 */
18313 if (!aborting())
18314 EMSG2(_(e_invexpr2), p);
18315 ret = FAIL;
18316 break;
18317 }
18318
18319 if (!eap->skip)
18320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018321 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 len = (int)STRLEN(p);
18323 if (ga_grow(&ga, len + 2) == FAIL)
18324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018325 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 ret = FAIL;
18327 break;
18328 }
18329 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 ga.ga_len += len;
18333 }
18334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018335 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 arg = skipwhite(arg);
18337 }
18338
18339 if (ret != FAIL && ga.ga_data != NULL)
18340 {
18341 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018344 out_flush();
18345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 else if (eap->cmdidx == CMD_echoerr)
18347 {
18348 /* We don't want to abort following commands, restore did_emsg. */
18349 save_did_emsg = did_emsg;
18350 EMSG((char_u *)ga.ga_data);
18351 if (!force_abort)
18352 did_emsg = save_did_emsg;
18353 }
18354 else if (eap->cmdidx == CMD_execute)
18355 do_cmdline((char_u *)ga.ga_data,
18356 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18357 }
18358
18359 ga_clear(&ga);
18360
18361 if (eap->skip)
18362 --emsg_skip;
18363
18364 eap->nextcmd = check_nextcmd(arg);
18365}
18366
18367/*
18368 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18369 * "arg" points to the "&" or '+' when called, to "option" when returning.
18370 * Returns NULL when no option name found. Otherwise pointer to the char
18371 * after the option name.
18372 */
18373 static char_u *
18374find_option_end(arg, opt_flags)
18375 char_u **arg;
18376 int *opt_flags;
18377{
18378 char_u *p = *arg;
18379
18380 ++p;
18381 if (*p == 'g' && p[1] == ':')
18382 {
18383 *opt_flags = OPT_GLOBAL;
18384 p += 2;
18385 }
18386 else if (*p == 'l' && p[1] == ':')
18387 {
18388 *opt_flags = OPT_LOCAL;
18389 p += 2;
18390 }
18391 else
18392 *opt_flags = 0;
18393
18394 if (!ASCII_ISALPHA(*p))
18395 return NULL;
18396 *arg = p;
18397
18398 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18399 p += 4; /* termcap option */
18400 else
18401 while (ASCII_ISALPHA(*p))
18402 ++p;
18403 return p;
18404}
18405
18406/*
18407 * ":function"
18408 */
18409 void
18410ex_function(eap)
18411 exarg_T *eap;
18412{
18413 char_u *theline;
18414 int j;
18415 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 char_u *name = NULL;
18418 char_u *p;
18419 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018420 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 garray_T newargs;
18422 garray_T newlines;
18423 int varargs = FALSE;
18424 int mustend = FALSE;
18425 int flags = 0;
18426 ufunc_T *fp;
18427 int indent;
18428 int nesting;
18429 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 dictitem_T *v;
18431 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018432 static int func_nr = 0; /* number for nameless function */
18433 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018434 hashtab_T *ht;
18435 int todo;
18436 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018437 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438
18439 /*
18440 * ":function" without argument: list functions.
18441 */
18442 if (ends_excmd(*eap->arg))
18443 {
18444 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018445 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018446 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018447 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018448 {
18449 if (!HASHITEM_EMPTY(hi))
18450 {
18451 --todo;
18452 fp = HI2UF(hi);
18453 if (!isdigit(*fp->uf_name))
18454 list_func_head(fp, FALSE);
18455 }
18456 }
18457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 eap->nextcmd = check_nextcmd(eap->arg);
18459 return;
18460 }
18461
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018462 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018463 * ":function /pat": list functions matching pattern.
18464 */
18465 if (*eap->arg == '/')
18466 {
18467 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18468 if (!eap->skip)
18469 {
18470 regmatch_T regmatch;
18471
18472 c = *p;
18473 *p = NUL;
18474 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18475 *p = c;
18476 if (regmatch.regprog != NULL)
18477 {
18478 regmatch.rm_ic = p_ic;
18479
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018480 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018481 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18482 {
18483 if (!HASHITEM_EMPTY(hi))
18484 {
18485 --todo;
18486 fp = HI2UF(hi);
18487 if (!isdigit(*fp->uf_name)
18488 && vim_regexec(&regmatch, fp->uf_name, 0))
18489 list_func_head(fp, FALSE);
18490 }
18491 }
18492 }
18493 }
18494 if (*p == '/')
18495 ++p;
18496 eap->nextcmd = check_nextcmd(p);
18497 return;
18498 }
18499
18500 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018501 * Get the function name. There are these situations:
18502 * func normal function name
18503 * "name" == func, "fudi.fd_dict" == NULL
18504 * dict.func new dictionary entry
18505 * "name" == NULL, "fudi.fd_dict" set,
18506 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18507 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018508 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018509 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18510 * dict.func existing dict entry that's not a Funcref
18511 * "name" == NULL, "fudi.fd_dict" set,
18512 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018515 name = trans_function_name(&p, eap->skip, 0, &fudi);
18516 paren = (vim_strchr(p, '(') != NULL);
18517 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 {
18519 /*
18520 * Return on an invalid expression in braces, unless the expression
18521 * evaluation has been cancelled due to an aborting error, an
18522 * interrupt, or an exception.
18523 */
18524 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018525 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018526 if (!eap->skip && fudi.fd_newkey != NULL)
18527 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018528 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 else
18532 eap->skip = TRUE;
18533 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018534
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 /* An error in a function call during evaluation of an expression in magic
18536 * braces should not cause the function not to be defined. */
18537 saved_did_emsg = did_emsg;
18538 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539
18540 /*
18541 * ":function func" with only function name: list function.
18542 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018543 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 {
18545 if (!ends_excmd(*skipwhite(p)))
18546 {
18547 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018548 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 }
18550 eap->nextcmd = check_nextcmd(p);
18551 if (eap->nextcmd != NULL)
18552 *p = NUL;
18553 if (!eap->skip && !got_int)
18554 {
18555 fp = find_func(name);
18556 if (fp != NULL)
18557 {
18558 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018559 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018561 if (FUNCLINE(fp, j) == NULL)
18562 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 msg_putchar('\n');
18564 msg_outnum((long)(j + 1));
18565 if (j < 9)
18566 msg_putchar(' ');
18567 if (j < 99)
18568 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018569 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 out_flush(); /* show a line at a time */
18571 ui_breakcheck();
18572 }
18573 if (!got_int)
18574 {
18575 msg_putchar('\n');
18576 msg_puts((char_u *)" endfunction");
18577 }
18578 }
18579 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018580 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018582 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 }
18584
18585 /*
18586 * ":function name(arg1, arg2)" Define function.
18587 */
18588 p = skipwhite(p);
18589 if (*p != '(')
18590 {
18591 if (!eap->skip)
18592 {
18593 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018594 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 }
18596 /* attempt to continue by skipping some text */
18597 if (vim_strchr(p, '(') != NULL)
18598 p = vim_strchr(p, '(');
18599 }
18600 p = skipwhite(p + 1);
18601
18602 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18603 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18604
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018605 if (!eap->skip)
18606 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018607 /* Check the name of the function. Unless it's a dictionary function
18608 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018609 if (name != NULL)
18610 arg = name;
18611 else
18612 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018613 if (arg != NULL && (fudi.fd_di == NULL
18614 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018615 {
18616 if (*arg == K_SPECIAL)
18617 j = 3;
18618 else
18619 j = 0;
18620 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18621 : eval_isnamec(arg[j])))
18622 ++j;
18623 if (arg[j] != NUL)
18624 emsg_funcname(_(e_invarg2), arg);
18625 }
18626 }
18627
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 /*
18629 * Isolate the arguments: "arg1, arg2, ...)"
18630 */
18631 while (*p != ')')
18632 {
18633 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18634 {
18635 varargs = TRUE;
18636 p += 3;
18637 mustend = TRUE;
18638 }
18639 else
18640 {
18641 arg = p;
18642 while (ASCII_ISALNUM(*p) || *p == '_')
18643 ++p;
18644 if (arg == p || isdigit(*arg)
18645 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18646 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18647 {
18648 if (!eap->skip)
18649 EMSG2(_("E125: Illegal argument: %s"), arg);
18650 break;
18651 }
18652 if (ga_grow(&newargs, 1) == FAIL)
18653 goto erret;
18654 c = *p;
18655 *p = NUL;
18656 arg = vim_strsave(arg);
18657 if (arg == NULL)
18658 goto erret;
18659 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18660 *p = c;
18661 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 if (*p == ',')
18663 ++p;
18664 else
18665 mustend = TRUE;
18666 }
18667 p = skipwhite(p);
18668 if (mustend && *p != ')')
18669 {
18670 if (!eap->skip)
18671 EMSG2(_(e_invarg2), eap->arg);
18672 break;
18673 }
18674 }
18675 ++p; /* skip the ')' */
18676
Bram Moolenaare9a41262005-01-15 22:18:47 +000018677 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 for (;;)
18679 {
18680 p = skipwhite(p);
18681 if (STRNCMP(p, "range", 5) == 0)
18682 {
18683 flags |= FC_RANGE;
18684 p += 5;
18685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018686 else if (STRNCMP(p, "dict", 4) == 0)
18687 {
18688 flags |= FC_DICT;
18689 p += 4;
18690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 else if (STRNCMP(p, "abort", 5) == 0)
18692 {
18693 flags |= FC_ABORT;
18694 p += 5;
18695 }
18696 else
18697 break;
18698 }
18699
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018700 /* When there is a line break use what follows for the function body.
18701 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18702 if (*p == '\n')
18703 line_arg = p + 1;
18704 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 EMSG(_(e_trailing));
18706
18707 /*
18708 * Read the body of the function, until ":endfunction" is found.
18709 */
18710 if (KeyTyped)
18711 {
18712 /* Check if the function already exists, don't let the user type the
18713 * whole function before telling him it doesn't work! For a script we
18714 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018715 if (!eap->skip && !eap->forceit)
18716 {
18717 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18718 EMSG(_(e_funcdict));
18719 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018720 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018723 if (!eap->skip && did_emsg)
18724 goto erret;
18725
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 msg_putchar('\n'); /* don't overwrite the function name */
18727 cmdline_row = msg_row;
18728 }
18729
18730 indent = 2;
18731 nesting = 0;
18732 for (;;)
18733 {
18734 msg_scroll = TRUE;
18735 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018736 sourcing_lnum_off = sourcing_lnum;
18737
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018738 if (line_arg != NULL)
18739 {
18740 /* Use eap->arg, split up in parts by line breaks. */
18741 theline = line_arg;
18742 p = vim_strchr(theline, '\n');
18743 if (p == NULL)
18744 line_arg += STRLEN(line_arg);
18745 else
18746 {
18747 *p = NUL;
18748 line_arg = p + 1;
18749 }
18750 }
18751 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 theline = getcmdline(':', 0L, indent);
18753 else
18754 theline = eap->getline(':', eap->cookie, indent);
18755 if (KeyTyped)
18756 lines_left = Rows - 1;
18757 if (theline == NULL)
18758 {
18759 EMSG(_("E126: Missing :endfunction"));
18760 goto erret;
18761 }
18762
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018763 /* Detect line continuation: sourcing_lnum increased more than one. */
18764 if (sourcing_lnum > sourcing_lnum_off + 1)
18765 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18766 else
18767 sourcing_lnum_off = 0;
18768
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 if (skip_until != NULL)
18770 {
18771 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18772 * don't check for ":endfunc". */
18773 if (STRCMP(theline, skip_until) == 0)
18774 {
18775 vim_free(skip_until);
18776 skip_until = NULL;
18777 }
18778 }
18779 else
18780 {
18781 /* skip ':' and blanks*/
18782 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18783 ;
18784
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018785 /* Check for "endfunction". */
18786 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018788 if (line_arg == NULL)
18789 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 break;
18791 }
18792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018793 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 * at "end". */
18795 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18796 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018797 else if (STRNCMP(p, "if", 2) == 0
18798 || STRNCMP(p, "wh", 2) == 0
18799 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 || STRNCMP(p, "try", 3) == 0)
18801 indent += 2;
18802
18803 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018804 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018806 if (*p == '!')
18807 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 p += eval_fname_script(p);
18809 if (ASCII_ISALPHA(*p))
18810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018811 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 if (*skipwhite(p) == '(')
18813 {
18814 ++nesting;
18815 indent += 2;
18816 }
18817 }
18818 }
18819
18820 /* Check for ":append" or ":insert". */
18821 p = skip_range(p, NULL);
18822 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18823 || (p[0] == 'i'
18824 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18825 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18826 skip_until = vim_strsave((char_u *)".");
18827
18828 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18829 arg = skipwhite(skiptowhite(p));
18830 if (arg[0] == '<' && arg[1] =='<'
18831 && ((p[0] == 'p' && p[1] == 'y'
18832 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18833 || (p[0] == 'p' && p[1] == 'e'
18834 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18835 || (p[0] == 't' && p[1] == 'c'
18836 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18837 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18838 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018839 || (p[0] == 'm' && p[1] == 'z'
18840 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 ))
18842 {
18843 /* ":python <<" continues until a dot, like ":append" */
18844 p = skipwhite(arg + 2);
18845 if (*p == NUL)
18846 skip_until = vim_strsave((char_u *)".");
18847 else
18848 skip_until = vim_strsave(p);
18849 }
18850 }
18851
18852 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018853 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018854 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018855 if (line_arg == NULL)
18856 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018858 }
18859
18860 /* Copy the line to newly allocated memory. get_one_sourceline()
18861 * allocates 250 bytes per line, this saves 80% on average. The cost
18862 * is an extra alloc/free. */
18863 p = vim_strsave(theline);
18864 if (p != NULL)
18865 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018866 if (line_arg == NULL)
18867 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018868 theline = p;
18869 }
18870
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018871 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18872
18873 /* Add NULL lines for continuation lines, so that the line count is
18874 * equal to the index in the growarray. */
18875 while (sourcing_lnum_off-- > 0)
18876 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018877
18878 /* Check for end of eap->arg. */
18879 if (line_arg != NULL && *line_arg == NUL)
18880 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 }
18882
18883 /* Don't define the function when skipping commands or when an error was
18884 * detected. */
18885 if (eap->skip || did_emsg)
18886 goto erret;
18887
18888 /*
18889 * If there are no errors, add the function
18890 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018891 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018892 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018893 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018894 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018895 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018896 emsg_funcname("E707: Function name conflicts with variable: %s",
18897 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018898 goto erret;
18899 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018900
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018901 fp = find_func(name);
18902 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018904 if (!eap->forceit)
18905 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018906 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018907 goto erret;
18908 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018909 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018910 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018911 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018912 name);
18913 goto erret;
18914 }
18915 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018916 ga_clear_strings(&(fp->uf_args));
18917 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018918 vim_free(name);
18919 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 }
18922 else
18923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018924 char numbuf[20];
18925
18926 fp = NULL;
18927 if (fudi.fd_newkey == NULL && !eap->forceit)
18928 {
18929 EMSG(_(e_funcdict));
18930 goto erret;
18931 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018932 if (fudi.fd_di == NULL)
18933 {
18934 /* Can't add a function to a locked dictionary */
18935 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18936 goto erret;
18937 }
18938 /* Can't change an existing function if it is locked */
18939 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18940 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018941
18942 /* Give the function a sequential number. Can only be used with a
18943 * Funcref! */
18944 vim_free(name);
18945 sprintf(numbuf, "%d", ++func_nr);
18946 name = vim_strsave((char_u *)numbuf);
18947 if (name == NULL)
18948 goto erret;
18949 }
18950
18951 if (fp == NULL)
18952 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018953 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018954 {
18955 int slen, plen;
18956 char_u *scriptname;
18957
18958 /* Check that the autoload name matches the script name. */
18959 j = FAIL;
18960 if (sourcing_name != NULL)
18961 {
18962 scriptname = autoload_name(name);
18963 if (scriptname != NULL)
18964 {
18965 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018966 plen = (int)STRLEN(p);
18967 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018968 if (slen > plen && fnamecmp(p,
18969 sourcing_name + slen - plen) == 0)
18970 j = OK;
18971 vim_free(scriptname);
18972 }
18973 }
18974 if (j == FAIL)
18975 {
18976 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18977 goto erret;
18978 }
18979 }
18980
18981 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 if (fp == NULL)
18983 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018984
18985 if (fudi.fd_dict != NULL)
18986 {
18987 if (fudi.fd_di == NULL)
18988 {
18989 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018990 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018991 if (fudi.fd_di == NULL)
18992 {
18993 vim_free(fp);
18994 goto erret;
18995 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018996 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18997 {
18998 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018999 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019000 goto erret;
19001 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019002 }
19003 else
19004 /* overwrite existing dict entry */
19005 clear_tv(&fudi.fd_di->di_tv);
19006 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019007 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019008 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019009 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019010
19011 /* behave like "dict" was used */
19012 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019013 }
19014
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019016 STRCPY(fp->uf_name, name);
19017 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019019 fp->uf_args = newargs;
19020 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019021#ifdef FEAT_PROFILE
19022 fp->uf_tml_count = NULL;
19023 fp->uf_tml_total = NULL;
19024 fp->uf_tml_self = NULL;
19025 fp->uf_profiling = FALSE;
19026 if (prof_def_func())
19027 func_do_profile(fp);
19028#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019029 fp->uf_varargs = varargs;
19030 fp->uf_flags = flags;
19031 fp->uf_calls = 0;
19032 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019033 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034
19035erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 ga_clear_strings(&newargs);
19037 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019038ret_free:
19039 vim_free(skip_until);
19040 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043}
19044
19045/*
19046 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019047 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019049 * flags:
19050 * TFN_INT: internal function name OK
19051 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 * Advances "pp" to just after the function name (if no error).
19053 */
19054 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019055trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 char_u **pp;
19057 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019058 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019061 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 char_u *start;
19063 char_u *end;
19064 int lead;
19065 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019067 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019068
19069 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019070 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019072
19073 /* Check for hard coded <SNR>: already translated function ID (from a user
19074 * command). */
19075 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19076 && (*pp)[2] == (int)KE_SNR)
19077 {
19078 *pp += 3;
19079 len = get_id_len(pp) + 3;
19080 return vim_strnsave(start, len);
19081 }
19082
19083 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19084 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019086 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019088
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019089 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19090 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019091 if (end == start)
19092 {
19093 if (!skip)
19094 EMSG(_("E129: Function name required"));
19095 goto theend;
19096 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019097 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019098 {
19099 /*
19100 * Report an invalid expression in braces, unless the expression
19101 * evaluation has been cancelled due to an aborting error, an
19102 * interrupt, or an exception.
19103 */
19104 if (!aborting())
19105 {
19106 if (end != NULL)
19107 EMSG2(_(e_invarg2), start);
19108 }
19109 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019110 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019111 goto theend;
19112 }
19113
19114 if (lv.ll_tv != NULL)
19115 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019116 if (fdp != NULL)
19117 {
19118 fdp->fd_dict = lv.ll_dict;
19119 fdp->fd_newkey = lv.ll_newkey;
19120 lv.ll_newkey = NULL;
19121 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019122 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019123 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19124 {
19125 name = vim_strsave(lv.ll_tv->vval.v_string);
19126 *pp = end;
19127 }
19128 else
19129 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019130 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19131 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019132 EMSG(_(e_funcref));
19133 else
19134 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019135 name = NULL;
19136 }
19137 goto theend;
19138 }
19139
19140 if (lv.ll_name == NULL)
19141 {
19142 /* Error found, but continue after the function name. */
19143 *pp = end;
19144 goto theend;
19145 }
19146
19147 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019148 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019149 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019150 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19151 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19152 {
19153 /* When there was "s:" already or the name expanded to get a
19154 * leading "s:" then remove it. */
19155 lv.ll_name += 2;
19156 len -= 2;
19157 lead = 2;
19158 }
19159 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019160 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019161 {
19162 if (lead == 2) /* skip over "s:" */
19163 lv.ll_name += 2;
19164 len = (int)(end - lv.ll_name);
19165 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019166
19167 /*
19168 * Copy the function name to allocated memory.
19169 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19170 * Accept <SNR>123_name() outside a script.
19171 */
19172 if (skip)
19173 lead = 0; /* do nothing */
19174 else if (lead > 0)
19175 {
19176 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019177 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19178 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019179 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019180 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019181 if (current_SID <= 0)
19182 {
19183 EMSG(_(e_usingsid));
19184 goto theend;
19185 }
19186 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19187 lead += (int)STRLEN(sid_buf);
19188 }
19189 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019190 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019191 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019192 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019193 goto theend;
19194 }
19195 name = alloc((unsigned)(len + lead + 1));
19196 if (name != NULL)
19197 {
19198 if (lead > 0)
19199 {
19200 name[0] = K_SPECIAL;
19201 name[1] = KS_EXTRA;
19202 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019203 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019204 STRCPY(name + 3, sid_buf);
19205 }
19206 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19207 name[len + lead] = NUL;
19208 }
19209 *pp = end;
19210
19211theend:
19212 clear_lval(&lv);
19213 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214}
19215
19216/*
19217 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19218 * Return 2 if "p" starts with "s:".
19219 * Return 0 otherwise.
19220 */
19221 static int
19222eval_fname_script(p)
19223 char_u *p;
19224{
19225 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19226 || STRNICMP(p + 1, "SNR>", 4) == 0))
19227 return 5;
19228 if (p[0] == 's' && p[1] == ':')
19229 return 2;
19230 return 0;
19231}
19232
19233/*
19234 * Return TRUE if "p" starts with "<SID>" or "s:".
19235 * Only works if eval_fname_script() returned non-zero for "p"!
19236 */
19237 static int
19238eval_fname_sid(p)
19239 char_u *p;
19240{
19241 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19242}
19243
19244/*
19245 * List the head of the function: "name(arg1, arg2)".
19246 */
19247 static void
19248list_func_head(fp, indent)
19249 ufunc_T *fp;
19250 int indent;
19251{
19252 int j;
19253
19254 msg_start();
19255 if (indent)
19256 MSG_PUTS(" ");
19257 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019258 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 {
19260 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019261 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 }
19263 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019264 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019266 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 {
19268 if (j)
19269 MSG_PUTS(", ");
19270 msg_puts(FUNCARG(fp, j));
19271 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019272 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 {
19274 if (j)
19275 MSG_PUTS(", ");
19276 MSG_PUTS("...");
19277 }
19278 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019279 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019280 if (p_verbose > 0)
19281 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282}
19283
19284/*
19285 * Find a function by name, return pointer to it in ufuncs.
19286 * Return NULL for unknown function.
19287 */
19288 static ufunc_T *
19289find_func(name)
19290 char_u *name;
19291{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019292 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019294 hi = hash_find(&func_hashtab, name);
19295 if (!HASHITEM_EMPTY(hi))
19296 return HI2UF(hi);
19297 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298}
19299
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019300#if defined(EXITFREE) || defined(PROTO)
19301 void
19302free_all_functions()
19303{
19304 hashitem_T *hi;
19305
19306 /* Need to start all over every time, because func_free() may change the
19307 * hash table. */
19308 while (func_hashtab.ht_used > 0)
19309 for (hi = func_hashtab.ht_array; ; ++hi)
19310 if (!HASHITEM_EMPTY(hi))
19311 {
19312 func_free(HI2UF(hi));
19313 break;
19314 }
19315}
19316#endif
19317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019318/*
19319 * Return TRUE if a function "name" exists.
19320 */
19321 static int
19322function_exists(name)
19323 char_u *name;
19324{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019325 char_u *nm = name;
19326 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019327 int n = FALSE;
19328
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019329 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019330 nm = skipwhite(nm);
19331
19332 /* Only accept "funcname", "funcname ", "funcname (..." and
19333 * "funcname(...", not "funcname!...". */
19334 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019335 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019336 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019337 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019338 else
19339 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019340 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019341 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019342 return n;
19343}
19344
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019345/*
19346 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019347 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019348 */
19349 static int
19350builtin_function(name)
19351 char_u *name;
19352{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019353 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19354 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019355}
19356
Bram Moolenaar05159a02005-02-26 23:04:13 +000019357#if defined(FEAT_PROFILE) || defined(PROTO)
19358/*
19359 * Start profiling function "fp".
19360 */
19361 static void
19362func_do_profile(fp)
19363 ufunc_T *fp;
19364{
19365 fp->uf_tm_count = 0;
19366 profile_zero(&fp->uf_tm_self);
19367 profile_zero(&fp->uf_tm_total);
19368 if (fp->uf_tml_count == NULL)
19369 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19370 (sizeof(int) * fp->uf_lines.ga_len));
19371 if (fp->uf_tml_total == NULL)
19372 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19373 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19374 if (fp->uf_tml_self == NULL)
19375 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19376 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19377 fp->uf_tml_idx = -1;
19378 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19379 || fp->uf_tml_self == NULL)
19380 return; /* out of memory */
19381
19382 fp->uf_profiling = TRUE;
19383}
19384
19385/*
19386 * Dump the profiling results for all functions in file "fd".
19387 */
19388 void
19389func_dump_profile(fd)
19390 FILE *fd;
19391{
19392 hashitem_T *hi;
19393 int todo;
19394 ufunc_T *fp;
19395 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019396 ufunc_T **sorttab;
19397 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019398
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019399 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019400 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19401
Bram Moolenaar05159a02005-02-26 23:04:13 +000019402 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19403 {
19404 if (!HASHITEM_EMPTY(hi))
19405 {
19406 --todo;
19407 fp = HI2UF(hi);
19408 if (fp->uf_profiling)
19409 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019410 if (sorttab != NULL)
19411 sorttab[st_len++] = fp;
19412
Bram Moolenaar05159a02005-02-26 23:04:13 +000019413 if (fp->uf_name[0] == K_SPECIAL)
19414 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19415 else
19416 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19417 if (fp->uf_tm_count == 1)
19418 fprintf(fd, "Called 1 time\n");
19419 else
19420 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19421 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19422 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19423 fprintf(fd, "\n");
19424 fprintf(fd, "count total (s) self (s)\n");
19425
19426 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19427 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019428 if (FUNCLINE(fp, i) == NULL)
19429 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019430 prof_func_line(fd, fp->uf_tml_count[i],
19431 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019432 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19433 }
19434 fprintf(fd, "\n");
19435 }
19436 }
19437 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019438
19439 if (sorttab != NULL && st_len > 0)
19440 {
19441 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19442 prof_total_cmp);
19443 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19444 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19445 prof_self_cmp);
19446 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19447 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019448}
Bram Moolenaar73830342005-02-28 22:48:19 +000019449
19450 static void
19451prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19452 FILE *fd;
19453 ufunc_T **sorttab;
19454 int st_len;
19455 char *title;
19456 int prefer_self; /* when equal print only self time */
19457{
19458 int i;
19459 ufunc_T *fp;
19460
19461 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19462 fprintf(fd, "count total (s) self (s) function\n");
19463 for (i = 0; i < 20 && i < st_len; ++i)
19464 {
19465 fp = sorttab[i];
19466 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19467 prefer_self);
19468 if (fp->uf_name[0] == K_SPECIAL)
19469 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19470 else
19471 fprintf(fd, " %s()\n", fp->uf_name);
19472 }
19473 fprintf(fd, "\n");
19474}
19475
19476/*
19477 * Print the count and times for one function or function line.
19478 */
19479 static void
19480prof_func_line(fd, count, total, self, prefer_self)
19481 FILE *fd;
19482 int count;
19483 proftime_T *total;
19484 proftime_T *self;
19485 int prefer_self; /* when equal print only self time */
19486{
19487 if (count > 0)
19488 {
19489 fprintf(fd, "%5d ", count);
19490 if (prefer_self && profile_equal(total, self))
19491 fprintf(fd, " ");
19492 else
19493 fprintf(fd, "%s ", profile_msg(total));
19494 if (!prefer_self && profile_equal(total, self))
19495 fprintf(fd, " ");
19496 else
19497 fprintf(fd, "%s ", profile_msg(self));
19498 }
19499 else
19500 fprintf(fd, " ");
19501}
19502
19503/*
19504 * Compare function for total time sorting.
19505 */
19506 static int
19507#ifdef __BORLANDC__
19508_RTLENTRYF
19509#endif
19510prof_total_cmp(s1, s2)
19511 const void *s1;
19512 const void *s2;
19513{
19514 ufunc_T *p1, *p2;
19515
19516 p1 = *(ufunc_T **)s1;
19517 p2 = *(ufunc_T **)s2;
19518 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19519}
19520
19521/*
19522 * Compare function for self time sorting.
19523 */
19524 static int
19525#ifdef __BORLANDC__
19526_RTLENTRYF
19527#endif
19528prof_self_cmp(s1, s2)
19529 const void *s1;
19530 const void *s2;
19531{
19532 ufunc_T *p1, *p2;
19533
19534 p1 = *(ufunc_T **)s1;
19535 p2 = *(ufunc_T **)s2;
19536 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19537}
19538
Bram Moolenaar05159a02005-02-26 23:04:13 +000019539#endif
19540
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019541/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019542 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019543 * Return TRUE if a package was loaded.
19544 */
19545 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019546script_autoload(name, reload)
19547 char_u *name;
19548 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019549{
19550 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019551 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019552 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019553 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019554
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019555 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019556 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019557 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019558 return FALSE;
19559
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019560 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019561
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019562 /* Find the name in the list of previously loaded package names. Skip
19563 * "autoload/", it's always the same. */
19564 for (i = 0; i < ga_loaded.ga_len; ++i)
19565 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19566 break;
19567 if (!reload && i < ga_loaded.ga_len)
19568 ret = FALSE; /* was loaded already */
19569 else
19570 {
19571 /* Remember the name if it wasn't loaded already. */
19572 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19573 {
19574 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19575 tofree = NULL;
19576 }
19577
19578 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019579 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019580 ret = TRUE;
19581 }
19582
19583 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019584 return ret;
19585}
19586
19587/*
19588 * Return the autoload script name for a function or variable name.
19589 * Returns NULL when out of memory.
19590 */
19591 static char_u *
19592autoload_name(name)
19593 char_u *name;
19594{
19595 char_u *p;
19596 char_u *scriptname;
19597
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019598 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019599 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19600 if (scriptname == NULL)
19601 return FALSE;
19602 STRCPY(scriptname, "autoload/");
19603 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019604 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019605 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019606 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019607 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019608 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019609}
19610
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19612
19613/*
19614 * Function given to ExpandGeneric() to obtain the list of user defined
19615 * function names.
19616 */
19617 char_u *
19618get_user_func_name(xp, idx)
19619 expand_T *xp;
19620 int idx;
19621{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019622 static long_u done;
19623 static hashitem_T *hi;
19624 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625
19626 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019628 done = 0;
19629 hi = func_hashtab.ht_array;
19630 }
19631 if (done < func_hashtab.ht_used)
19632 {
19633 if (done++ > 0)
19634 ++hi;
19635 while (HASHITEM_EMPTY(hi))
19636 ++hi;
19637 fp = HI2UF(hi);
19638
19639 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19640 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641
19642 cat_func_name(IObuff, fp);
19643 if (xp->xp_context != EXPAND_USER_FUNC)
19644 {
19645 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019646 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 STRCAT(IObuff, ")");
19648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 return IObuff;
19650 }
19651 return NULL;
19652}
19653
19654#endif /* FEAT_CMDL_COMPL */
19655
19656/*
19657 * Copy the function name of "fp" to buffer "buf".
19658 * "buf" must be able to hold the function name plus three bytes.
19659 * Takes care of script-local function names.
19660 */
19661 static void
19662cat_func_name(buf, fp)
19663 char_u *buf;
19664 ufunc_T *fp;
19665{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019666 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 {
19668 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019669 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 }
19671 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019672 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673}
19674
19675/*
19676 * ":delfunction {name}"
19677 */
19678 void
19679ex_delfunction(eap)
19680 exarg_T *eap;
19681{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019682 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 char_u *p;
19684 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686
19687 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019688 name = trans_function_name(&p, eap->skip, 0, &fudi);
19689 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019691 {
19692 if (fudi.fd_dict != NULL && !eap->skip)
19693 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 if (!ends_excmd(*skipwhite(p)))
19697 {
19698 vim_free(name);
19699 EMSG(_(e_trailing));
19700 return;
19701 }
19702 eap->nextcmd = check_nextcmd(p);
19703 if (eap->nextcmd != NULL)
19704 *p = NUL;
19705
19706 if (!eap->skip)
19707 fp = find_func(name);
19708 vim_free(name);
19709
19710 if (!eap->skip)
19711 {
19712 if (fp == NULL)
19713 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019714 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 return;
19716 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019717 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 {
19719 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19720 return;
19721 }
19722
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019723 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019725 /* Delete the dict item that refers to the function, it will
19726 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019727 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019729 else
19730 func_free(fp);
19731 }
19732}
19733
19734/*
19735 * Free a function and remove it from the list of functions.
19736 */
19737 static void
19738func_free(fp)
19739 ufunc_T *fp;
19740{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019741 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019742
19743 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019744 ga_clear_strings(&(fp->uf_args));
19745 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019746#ifdef FEAT_PROFILE
19747 vim_free(fp->uf_tml_count);
19748 vim_free(fp->uf_tml_total);
19749 vim_free(fp->uf_tml_self);
19750#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019751
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019752 /* remove the function from the function hashtable */
19753 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19754 if (HASHITEM_EMPTY(hi))
19755 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019756 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019757 hash_remove(&func_hashtab, hi);
19758
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019759 vim_free(fp);
19760}
19761
19762/*
19763 * Unreference a Function: decrement the reference count and free it when it
19764 * becomes zero. Only for numbered functions.
19765 */
19766 static void
19767func_unref(name)
19768 char_u *name;
19769{
19770 ufunc_T *fp;
19771
19772 if (name != NULL && isdigit(*name))
19773 {
19774 fp = find_func(name);
19775 if (fp == NULL)
19776 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019777 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019778 {
19779 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019780 * when "uf_calls" becomes zero. */
19781 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019782 func_free(fp);
19783 }
19784 }
19785}
19786
19787/*
19788 * Count a reference to a Function.
19789 */
19790 static void
19791func_ref(name)
19792 char_u *name;
19793{
19794 ufunc_T *fp;
19795
19796 if (name != NULL && isdigit(*name))
19797 {
19798 fp = find_func(name);
19799 if (fp == NULL)
19800 EMSG2(_(e_intern2), "func_ref()");
19801 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019802 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 }
19804}
19805
19806/*
19807 * Call a user function.
19808 */
19809 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019810call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 ufunc_T *fp; /* pointer to function */
19812 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 typval_T *argvars; /* arguments */
19814 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 linenr_T firstline; /* first line of range */
19816 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
Bram Moolenaar33570922005-01-25 22:26:29 +000019819 char_u *save_sourcing_name;
19820 linenr_T save_sourcing_lnum;
19821 scid_T save_current_SID;
19822 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019823 int save_did_emsg;
19824 static int depth = 0;
19825 dictitem_T *v;
19826 int fixvar_idx = 0; /* index in fixvar[] */
19827 int i;
19828 int ai;
19829 char_u numbuf[NUMBUFLEN];
19830 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019831#ifdef FEAT_PROFILE
19832 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019833 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
19836 /* If depth of calling is getting too high, don't execute the function */
19837 if (depth >= p_mfd)
19838 {
19839 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 rettv->v_type = VAR_NUMBER;
19841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 return;
19843 }
19844 ++depth;
19845
19846 line_breakcheck(); /* check for CTRL-C hit */
19847
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019848 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851 fc.rettv = rettv;
19852 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 fc.linenr = 0;
19854 fc.returned = FALSE;
19855 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019857 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 fc.dbg_tick = debug_tick;
19859
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 /*
19861 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19862 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19863 * each argument variable and saves a lot of time.
19864 */
19865 /*
19866 * Init l: variables.
19867 */
19868 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019869 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019870 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019871 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19872 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019873 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019874 name = v->di_key;
19875 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019876 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19877 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19878 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019879 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 v->di_tv.vval.v_dict = selfdict;
19881 ++selfdict->dv_refcount;
19882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019883
Bram Moolenaar33570922005-01-25 22:26:29 +000019884 /*
19885 * Init a: variables.
19886 * Set a:0 to "argcount".
19887 * Set a:000 to a list with room for the "..." arguments.
19888 */
19889 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19890 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019891 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 v = &fc.fixvar[fixvar_idx++].var;
19893 STRCPY(v->di_key, "000");
19894 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19895 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19896 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019897 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019898 v->di_tv.vval.v_list = &fc.l_varlist;
19899 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19900 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019901 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019902
19903 /*
19904 * Set a:firstline to "firstline" and a:lastline to "lastline".
19905 * Set a:name to named arguments.
19906 * Set a:N to the "..." arguments.
19907 */
19908 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19909 (varnumber_T)firstline);
19910 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19911 (varnumber_T)lastline);
19912 for (i = 0; i < argcount; ++i)
19913 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019914 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019915 if (ai < 0)
19916 /* named argument a:name */
19917 name = FUNCARG(fp, i);
19918 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019919 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 /* "..." argument a:1, a:2, etc. */
19921 sprintf((char *)numbuf, "%d", ai + 1);
19922 name = numbuf;
19923 }
19924 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19925 {
19926 v = &fc.fixvar[fixvar_idx++].var;
19927 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19928 }
19929 else
19930 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019931 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19932 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019933 if (v == NULL)
19934 break;
19935 v->di_flags = DI_FLAGS_RO;
19936 }
19937 STRCPY(v->di_key, name);
19938 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19939
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019940 /* Note: the values are copied directly to avoid alloc/free.
19941 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019942 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019943 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019944
19945 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19946 {
19947 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19948 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019949 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019950 }
19951 }
19952
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 /* Don't redraw while executing the function. */
19954 ++RedrawingDisabled;
19955 save_sourcing_name = sourcing_name;
19956 save_sourcing_lnum = sourcing_lnum;
19957 sourcing_lnum = 1;
19958 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019959 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 if (sourcing_name != NULL)
19961 {
19962 if (save_sourcing_name != NULL
19963 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19964 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19965 else
19966 STRCPY(sourcing_name, "function ");
19967 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19968
19969 if (p_verbose >= 12)
19970 {
19971 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019972 verbose_enter_scroll();
19973
Bram Moolenaar555b2802005-05-19 21:08:39 +000019974 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 if (p_verbose >= 14)
19976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019978 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019979 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980
19981 msg_puts((char_u *)"(");
19982 for (i = 0; i < argcount; ++i)
19983 {
19984 if (i > 0)
19985 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019986 if (argvars[i].v_type == VAR_NUMBER)
19987 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 else
19989 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019990 trunc_string(tv2string(&argvars[i], &tofree,
19991 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019993 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 }
19995 }
19996 msg_puts((char_u *)")");
19997 }
19998 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019999
20000 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 --no_wait_return;
20002 }
20003 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020005 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020006 {
20007 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20008 func_do_profile(fp);
20009 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020010 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020011 {
20012 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020013 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020014 profile_zero(&fp->uf_tm_children);
20015 }
20016 script_prof_save(&wait_start);
20017 }
20018#endif
20019
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020021 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 save_did_emsg = did_emsg;
20023 did_emsg = FALSE;
20024
20025 /* call do_cmdline() to execute the lines */
20026 do_cmdline(NULL, get_func_line, (void *)&fc,
20027 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20028
20029 --RedrawingDisabled;
20030
20031 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020032 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 clear_tv(rettv);
20035 rettv->v_type = VAR_NUMBER;
20036 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
20038
Bram Moolenaar05159a02005-02-26 23:04:13 +000020039#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020040 if (do_profiling == PROF_YES && (fp->uf_profiling
20041 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020042 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020043 profile_end(&call_start);
20044 profile_sub_wait(&wait_start, &call_start);
20045 profile_add(&fp->uf_tm_total, &call_start);
20046 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020047 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020048 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020049 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20050 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020051 }
20052 }
20053#endif
20054
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 /* when being verbose, mention the return value */
20056 if (p_verbose >= 12)
20057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020059 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020062 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020064 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20065 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020068 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020069 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020070 char_u *tofree;
20071
Bram Moolenaar555b2802005-05-19 21:08:39 +000020072 /* The value may be very long. Skip the middle part, so that we
20073 * have some idea how it starts and ends. smsg() would always
20074 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000020075 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020076 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000020077 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020078 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 }
20080 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020081
20082 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 --no_wait_return;
20084 }
20085
20086 vim_free(sourcing_name);
20087 sourcing_name = save_sourcing_name;
20088 sourcing_lnum = save_sourcing_lnum;
20089 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020090#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020091 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020092 script_prof_restore(&wait_start);
20093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094
20095 if (p_verbose >= 12 && sourcing_name != NULL)
20096 {
20097 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020098 verbose_enter_scroll();
20099
Bram Moolenaar555b2802005-05-19 21:08:39 +000020100 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020102
20103 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 --no_wait_return;
20105 }
20106
20107 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020108 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 /* The a: variables typevals were not alloced, only free the allocated
20111 * variables. */
20112 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20113
20114 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 --depth;
20116}
20117
20118/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020119 * Add a number variable "name" to dict "dp" with value "nr".
20120 */
20121 static void
20122add_nr_var(dp, v, name, nr)
20123 dict_T *dp;
20124 dictitem_T *v;
20125 char *name;
20126 varnumber_T nr;
20127{
20128 STRCPY(v->di_key, name);
20129 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20130 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20131 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020132 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 v->di_tv.vval.v_number = nr;
20134}
20135
20136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 * ":return [expr]"
20138 */
20139 void
20140ex_return(eap)
20141 exarg_T *eap;
20142{
20143 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 int returning = FALSE;
20146
20147 if (current_funccal == NULL)
20148 {
20149 EMSG(_("E133: :return not inside a function"));
20150 return;
20151 }
20152
20153 if (eap->skip)
20154 ++emsg_skip;
20155
20156 eap->nextcmd = NULL;
20157 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 {
20160 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020161 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 }
20165 /* It's safer to return also on error. */
20166 else if (!eap->skip)
20167 {
20168 /*
20169 * Return unless the expression evaluation has been cancelled due to an
20170 * aborting error, an interrupt, or an exception.
20171 */
20172 if (!aborting())
20173 returning = do_return(eap, FALSE, TRUE, NULL);
20174 }
20175
20176 /* When skipping or the return gets pending, advance to the next command
20177 * in this line (!returning). Otherwise, ignore the rest of the line.
20178 * Following lines will be ignored by get_func_line(). */
20179 if (returning)
20180 eap->nextcmd = NULL;
20181 else if (eap->nextcmd == NULL) /* no argument */
20182 eap->nextcmd = check_nextcmd(arg);
20183
20184 if (eap->skip)
20185 --emsg_skip;
20186}
20187
20188/*
20189 * Return from a function. Possibly makes the return pending. Also called
20190 * for a pending return at the ":endtry" or after returning from an extra
20191 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 * FALSE when the return gets pending.
20195 */
20196 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 exarg_T *eap;
20199 int reanimate;
20200 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020201 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202{
20203 int idx;
20204 struct condstack *cstack = eap->cstack;
20205
20206 if (reanimate)
20207 /* Undo the return. */
20208 current_funccal->returned = FALSE;
20209
20210 /*
20211 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20212 * not in its finally clause (which then is to be executed next) is found.
20213 * In this case, make the ":return" pending for execution at the ":endtry".
20214 * Otherwise, return normally.
20215 */
20216 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20217 if (idx >= 0)
20218 {
20219 cstack->cs_pending[idx] = CSTP_RETURN;
20220
20221 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222 /* A pending return again gets pending. "rettv" points to an
20223 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020225 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 else
20227 {
20228 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020231 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 {
20235 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020237 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 else
20239 EMSG(_(e_outofmem));
20240 }
20241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243
20244 if (reanimate)
20245 {
20246 /* The pending return value could be overwritten by a ":return"
20247 * without argument in a finally clause; reset the default
20248 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 current_funccal->rettv->v_type = VAR_NUMBER;
20250 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 }
20252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 }
20255 else
20256 {
20257 current_funccal->returned = TRUE;
20258
20259 /* If the return is carried out now, store the return value. For
20260 * a return immediately after reanimation, the value is already
20261 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020262 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020264 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020267 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 }
20269 }
20270
20271 return idx < 0;
20272}
20273
20274/*
20275 * Free the variable with a pending return value.
20276 */
20277 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278discard_pending_return(rettv)
20279 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280{
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282}
20283
20284/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 * is an allocated string. Used by report_pending() for verbose messages.
20287 */
20288 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020289get_return_cmd(rettv)
20290 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020292 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020293 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020294 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020296 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020297 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020298 if (s == NULL)
20299 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300
20301 STRCPY(IObuff, ":return ");
20302 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20303 if (STRLEN(s) + 8 >= IOSIZE)
20304 STRCPY(IObuff + IOSIZE - 4, "...");
20305 vim_free(tofree);
20306 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307}
20308
20309/*
20310 * Get next function line.
20311 * Called by do_cmdline() to get the next line.
20312 * Returns allocated string, or NULL for end of function.
20313 */
20314/* ARGSUSED */
20315 char_u *
20316get_func_line(c, cookie, indent)
20317 int c; /* not used */
20318 void *cookie;
20319 int indent; /* not used */
20320{
Bram Moolenaar33570922005-01-25 22:26:29 +000020321 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020322 ufunc_T *fp = fcp->func;
20323 char_u *retval;
20324 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325
20326 /* If breakpoints have been added/deleted need to check for it. */
20327 if (fcp->dbg_tick != debug_tick)
20328 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020329 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 sourcing_lnum);
20331 fcp->dbg_tick = debug_tick;
20332 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020333#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020334 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020335 func_line_end(cookie);
20336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337
Bram Moolenaar05159a02005-02-26 23:04:13 +000020338 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020339 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20340 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 retval = NULL;
20342 else
20343 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020344 /* Skip NULL lines (continuation lines). */
20345 while (fcp->linenr < gap->ga_len
20346 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20347 ++fcp->linenr;
20348 if (fcp->linenr >= gap->ga_len)
20349 retval = NULL;
20350 else
20351 {
20352 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20353 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020354#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020355 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020356 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020357#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 }
20360
20361 /* Did we encounter a breakpoint? */
20362 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20363 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020364 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020366 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 sourcing_lnum);
20368 fcp->dbg_tick = debug_tick;
20369 }
20370
20371 return retval;
20372}
20373
Bram Moolenaar05159a02005-02-26 23:04:13 +000020374#if defined(FEAT_PROFILE) || defined(PROTO)
20375/*
20376 * Called when starting to read a function line.
20377 * "sourcing_lnum" must be correct!
20378 * When skipping lines it may not actually be executed, but we won't find out
20379 * until later and we need to store the time now.
20380 */
20381 void
20382func_line_start(cookie)
20383 void *cookie;
20384{
20385 funccall_T *fcp = (funccall_T *)cookie;
20386 ufunc_T *fp = fcp->func;
20387
20388 if (fp->uf_profiling && sourcing_lnum >= 1
20389 && sourcing_lnum <= fp->uf_lines.ga_len)
20390 {
20391 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020392 /* Skip continuation lines. */
20393 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20394 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020395 fp->uf_tml_execed = FALSE;
20396 profile_start(&fp->uf_tml_start);
20397 profile_zero(&fp->uf_tml_children);
20398 profile_get_wait(&fp->uf_tml_wait);
20399 }
20400}
20401
20402/*
20403 * Called when actually executing a function line.
20404 */
20405 void
20406func_line_exec(cookie)
20407 void *cookie;
20408{
20409 funccall_T *fcp = (funccall_T *)cookie;
20410 ufunc_T *fp = fcp->func;
20411
20412 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20413 fp->uf_tml_execed = TRUE;
20414}
20415
20416/*
20417 * Called when done with a function line.
20418 */
20419 void
20420func_line_end(cookie)
20421 void *cookie;
20422{
20423 funccall_T *fcp = (funccall_T *)cookie;
20424 ufunc_T *fp = fcp->func;
20425
20426 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20427 {
20428 if (fp->uf_tml_execed)
20429 {
20430 ++fp->uf_tml_count[fp->uf_tml_idx];
20431 profile_end(&fp->uf_tml_start);
20432 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020433 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020434 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20435 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020436 }
20437 fp->uf_tml_idx = -1;
20438 }
20439}
20440#endif
20441
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442/*
20443 * Return TRUE if the currently active function should be ended, because a
20444 * return was encountered or an error occured. Used inside a ":while".
20445 */
20446 int
20447func_has_ended(cookie)
20448 void *cookie;
20449{
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451
20452 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20453 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020454 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 || fcp->returned);
20456}
20457
20458/*
20459 * return TRUE if cookie indicates a function which "abort"s on errors.
20460 */
20461 int
20462func_has_abort(cookie)
20463 void *cookie;
20464{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020465 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466}
20467
20468#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20469typedef enum
20470{
20471 VAR_FLAVOUR_DEFAULT,
20472 VAR_FLAVOUR_SESSION,
20473 VAR_FLAVOUR_VIMINFO
20474} var_flavour_T;
20475
20476static var_flavour_T var_flavour __ARGS((char_u *varname));
20477
20478 static var_flavour_T
20479var_flavour(varname)
20480 char_u *varname;
20481{
20482 char_u *p = varname;
20483
20484 if (ASCII_ISUPPER(*p))
20485 {
20486 while (*(++p))
20487 if (ASCII_ISLOWER(*p))
20488 return VAR_FLAVOUR_SESSION;
20489 return VAR_FLAVOUR_VIMINFO;
20490 }
20491 else
20492 return VAR_FLAVOUR_DEFAULT;
20493}
20494#endif
20495
20496#if defined(FEAT_VIMINFO) || defined(PROTO)
20497/*
20498 * Restore global vars that start with a capital from the viminfo file
20499 */
20500 int
20501read_viminfo_varlist(virp, writing)
20502 vir_T *virp;
20503 int writing;
20504{
20505 char_u *tab;
20506 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020507 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508
20509 if (!writing && (find_viminfo_parameter('!') != NULL))
20510 {
20511 tab = vim_strchr(virp->vir_line + 1, '\t');
20512 if (tab != NULL)
20513 {
20514 *tab++ = '\0'; /* isolate the variable name */
20515 if (*tab == 'S') /* string var */
20516 is_string = TRUE;
20517
20518 tab = vim_strchr(tab, '\t');
20519 if (tab != NULL)
20520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 if (is_string)
20522 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020523 tv.v_type = VAR_STRING;
20524 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 }
20527 else
20528 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020529 tv.v_type = VAR_NUMBER;
20530 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020532 set_var(virp->vir_line + 1, &tv, FALSE);
20533 if (is_string)
20534 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 }
20536 }
20537 }
20538
20539 return viminfo_readline(virp);
20540}
20541
20542/*
20543 * Write global vars that start with a capital to the viminfo file
20544 */
20545 void
20546write_viminfo_varlist(fp)
20547 FILE *fp;
20548{
Bram Moolenaar33570922005-01-25 22:26:29 +000020549 hashitem_T *hi;
20550 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020551 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020552 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020553 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020554 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020555 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556
20557 if (find_viminfo_parameter('!') == NULL)
20558 return;
20559
20560 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020561
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020562 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020563 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020565 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020567 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 this_var = HI2DI(hi);
20569 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020570 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020571 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020572 {
20573 case VAR_STRING: s = "STR"; break;
20574 case VAR_NUMBER: s = "NUM"; break;
20575 default: continue;
20576 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020577 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020578 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020579 if (p != NULL)
20580 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020581 vim_free(tofree);
20582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 }
20584 }
20585}
20586#endif
20587
20588#if defined(FEAT_SESSION) || defined(PROTO)
20589 int
20590store_session_globals(fd)
20591 FILE *fd;
20592{
Bram Moolenaar33570922005-01-25 22:26:29 +000020593 hashitem_T *hi;
20594 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020595 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 char_u *p, *t;
20597
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020598 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020599 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020601 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020603 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020604 this_var = HI2DI(hi);
20605 if ((this_var->di_tv.v_type == VAR_NUMBER
20606 || this_var->di_tv.v_type == VAR_STRING)
20607 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020608 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020609 /* Escape special characters with a backslash. Turn a LF and
20610 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020611 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020612 (char_u *)"\\\"\n\r");
20613 if (p == NULL) /* out of memory */
20614 break;
20615 for (t = p; *t != NUL; ++t)
20616 if (*t == '\n')
20617 *t = 'n';
20618 else if (*t == '\r')
20619 *t = 'r';
20620 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020621 this_var->di_key,
20622 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20623 : ' ',
20624 p,
20625 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20626 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020627 || put_eol(fd) == FAIL)
20628 {
20629 vim_free(p);
20630 return FAIL;
20631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 vim_free(p);
20633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 }
20635 }
20636 return OK;
20637}
20638#endif
20639
Bram Moolenaar661b1822005-07-28 22:36:45 +000020640/*
20641 * Display script name where an item was last set.
20642 * Should only be invoked when 'verbose' is non-zero.
20643 */
20644 void
20645last_set_msg(scriptID)
20646 scid_T scriptID;
20647{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020648 char_u *p;
20649
Bram Moolenaar661b1822005-07-28 22:36:45 +000020650 if (scriptID != 0)
20651 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020652 p = home_replace_save(NULL, get_scriptname(scriptID));
20653 if (p != NULL)
20654 {
20655 verbose_enter();
20656 MSG_PUTS(_("\n\tLast set from "));
20657 MSG_PUTS(p);
20658 vim_free(p);
20659 verbose_leave();
20660 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020661 }
20662}
20663
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664#endif /* FEAT_EVAL */
20665
20666#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20667
20668
20669#ifdef WIN3264
20670/*
20671 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20672 */
20673static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20674static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20675static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20676
20677/*
20678 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020679 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 */
20681 static int
20682get_short_pathname(fnamep, bufp, fnamelen)
20683 char_u **fnamep;
20684 char_u **bufp;
20685 int *fnamelen;
20686{
20687 int l,len;
20688 char_u *newbuf;
20689
20690 len = *fnamelen;
20691
20692 l = GetShortPathName(*fnamep, *fnamep, len);
20693 if (l > len - 1)
20694 {
20695 /* If that doesn't work (not enough space), then save the string
20696 * and try again with a new buffer big enough
20697 */
20698 newbuf = vim_strnsave(*fnamep, l);
20699 if (newbuf == NULL)
20700 return 0;
20701
20702 vim_free(*bufp);
20703 *fnamep = *bufp = newbuf;
20704
20705 l = GetShortPathName(*fnamep,*fnamep,l+1);
20706
20707 /* Really should always succeed, as the buffer is big enough */
20708 }
20709
20710 *fnamelen = l;
20711 return 1;
20712}
20713
20714/*
20715 * Create a short path name. Returns the length of the buffer it needs.
20716 * Doesn't copy over the end of the buffer passed in.
20717 */
20718 static int
20719shortpath_for_invalid_fname(fname, bufp, fnamelen)
20720 char_u **fname;
20721 char_u **bufp;
20722 int *fnamelen;
20723{
20724 char_u *s, *p, *pbuf2, *pbuf3;
20725 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020726 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727
20728 /* Make a copy */
20729 len2 = *fnamelen;
20730 pbuf2 = vim_strnsave(*fname, len2);
20731 pbuf3 = NULL;
20732
20733 s = pbuf2 + len2 - 1; /* Find the end */
20734 slen = 1;
20735 plen = len2;
20736
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020737 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 {
20739 --s;
20740 ++slen;
20741 --plen;
20742 }
20743
20744 do
20745 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020746 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020747 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 {
20749 --s;
20750 ++slen;
20751 --plen;
20752 }
20753 if (s <= pbuf2)
20754 break;
20755
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020756 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 ch = *s;
20758 *s = 0; /* get_short_pathname requires a null-terminated string */
20759
20760 /* Try it in situ */
20761 p = pbuf2;
20762 if (!get_short_pathname(&p, &pbuf3, &plen))
20763 {
20764 vim_free(pbuf2);
20765 return -1;
20766 }
20767 *s = ch; /* Preserve the string */
20768 } while (plen == 0);
20769
20770 if (plen > 0)
20771 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020772 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 *fnamelen = len = plen + slen;
20774 vim_free(*bufp);
20775 if (len > len2)
20776 {
20777 /* If there's not enough space in the currently allocated string,
20778 * then copy it to a buffer big enough.
20779 */
20780 *fname= *bufp = vim_strnsave(p, len);
20781 if (*fname == NULL)
20782 return -1;
20783 }
20784 else
20785 {
20786 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20787 *fname = *bufp = pbuf2;
20788 if (p != pbuf2)
20789 strncpy(*fname, p, plen);
20790 pbuf2 = NULL;
20791 }
20792 /* Concat the next bit */
20793 strncpy(*fname + plen, s, slen);
20794 (*fname)[len] = '\0';
20795 }
20796 vim_free(pbuf3);
20797 vim_free(pbuf2);
20798 return 0;
20799}
20800
20801/*
20802 * Get a pathname for a partial path.
20803 */
20804 static int
20805shortpath_for_partial(fnamep, bufp, fnamelen)
20806 char_u **fnamep;
20807 char_u **bufp;
20808 int *fnamelen;
20809{
20810 int sepcount, len, tflen;
20811 char_u *p;
20812 char_u *pbuf, *tfname;
20813 int hasTilde;
20814
20815 /* Count up the path seperators from the RHS.. so we know which part
20816 * of the path to return.
20817 */
20818 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020819 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 if (vim_ispathsep(*p))
20821 ++sepcount;
20822
20823 /* Need full path first (use expand_env() to remove a "~/") */
20824 hasTilde = (**fnamep == '~');
20825 if (hasTilde)
20826 pbuf = tfname = expand_env_save(*fnamep);
20827 else
20828 pbuf = tfname = FullName_save(*fnamep, FALSE);
20829
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020830 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831
20832 if (!get_short_pathname(&tfname, &pbuf, &len))
20833 return -1;
20834
20835 if (len == 0)
20836 {
20837 /* Don't have a valid filename, so shorten the rest of the
20838 * path if we can. This CAN give us invalid 8.3 filenames, but
20839 * there's not a lot of point in guessing what it might be.
20840 */
20841 len = tflen;
20842 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20843 return -1;
20844 }
20845
20846 /* Count the paths backward to find the beginning of the desired string. */
20847 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020848 {
20849#ifdef FEAT_MBYTE
20850 if (has_mbyte)
20851 p -= mb_head_off(tfname, p);
20852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 if (vim_ispathsep(*p))
20854 {
20855 if (sepcount == 0 || (hasTilde && sepcount == 1))
20856 break;
20857 else
20858 sepcount --;
20859 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 if (hasTilde)
20862 {
20863 --p;
20864 if (p >= tfname)
20865 *p = '~';
20866 else
20867 return -1;
20868 }
20869 else
20870 ++p;
20871
20872 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20873 vim_free(*bufp);
20874 *fnamelen = (int)STRLEN(p);
20875 *bufp = pbuf;
20876 *fnamep = p;
20877
20878 return 0;
20879}
20880#endif /* WIN3264 */
20881
20882/*
20883 * Adjust a filename, according to a string of modifiers.
20884 * *fnamep must be NUL terminated when called. When returning, the length is
20885 * determined by *fnamelen.
20886 * Returns valid flags.
20887 * When there is an error, *fnamep is set to NULL.
20888 */
20889 int
20890modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20891 char_u *src; /* string with modifiers */
20892 int *usedlen; /* characters after src that are used */
20893 char_u **fnamep; /* file name so far */
20894 char_u **bufp; /* buffer for allocated file name or NULL */
20895 int *fnamelen; /* length of fnamep */
20896{
20897 int valid = 0;
20898 char_u *tail;
20899 char_u *s, *p, *pbuf;
20900 char_u dirname[MAXPATHL];
20901 int c;
20902 int has_fullname = 0;
20903#ifdef WIN3264
20904 int has_shortname = 0;
20905#endif
20906
20907repeat:
20908 /* ":p" - full path/file_name */
20909 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20910 {
20911 has_fullname = 1;
20912
20913 valid |= VALID_PATH;
20914 *usedlen += 2;
20915
20916 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20917 if ((*fnamep)[0] == '~'
20918#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20919 && ((*fnamep)[1] == '/'
20920# ifdef BACKSLASH_IN_FILENAME
20921 || (*fnamep)[1] == '\\'
20922# endif
20923 || (*fnamep)[1] == NUL)
20924
20925#endif
20926 )
20927 {
20928 *fnamep = expand_env_save(*fnamep);
20929 vim_free(*bufp); /* free any allocated file name */
20930 *bufp = *fnamep;
20931 if (*fnamep == NULL)
20932 return -1;
20933 }
20934
20935 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020936 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 {
20938 if (vim_ispathsep(*p)
20939 && p[1] == '.'
20940 && (p[2] == NUL
20941 || vim_ispathsep(p[2])
20942 || (p[2] == '.'
20943 && (p[3] == NUL || vim_ispathsep(p[3])))))
20944 break;
20945 }
20946
20947 /* FullName_save() is slow, don't use it when not needed. */
20948 if (*p != NUL || !vim_isAbsName(*fnamep))
20949 {
20950 *fnamep = FullName_save(*fnamep, *p != NUL);
20951 vim_free(*bufp); /* free any allocated file name */
20952 *bufp = *fnamep;
20953 if (*fnamep == NULL)
20954 return -1;
20955 }
20956
20957 /* Append a path separator to a directory. */
20958 if (mch_isdir(*fnamep))
20959 {
20960 /* Make room for one or two extra characters. */
20961 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20962 vim_free(*bufp); /* free any allocated file name */
20963 *bufp = *fnamep;
20964 if (*fnamep == NULL)
20965 return -1;
20966 add_pathsep(*fnamep);
20967 }
20968 }
20969
20970 /* ":." - path relative to the current directory */
20971 /* ":~" - path relative to the home directory */
20972 /* ":8" - shortname path - postponed till after */
20973 while (src[*usedlen] == ':'
20974 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20975 {
20976 *usedlen += 2;
20977 if (c == '8')
20978 {
20979#ifdef WIN3264
20980 has_shortname = 1; /* Postpone this. */
20981#endif
20982 continue;
20983 }
20984 pbuf = NULL;
20985 /* Need full path first (use expand_env() to remove a "~/") */
20986 if (!has_fullname)
20987 {
20988 if (c == '.' && **fnamep == '~')
20989 p = pbuf = expand_env_save(*fnamep);
20990 else
20991 p = pbuf = FullName_save(*fnamep, FALSE);
20992 }
20993 else
20994 p = *fnamep;
20995
20996 has_fullname = 0;
20997
20998 if (p != NULL)
20999 {
21000 if (c == '.')
21001 {
21002 mch_dirname(dirname, MAXPATHL);
21003 s = shorten_fname(p, dirname);
21004 if (s != NULL)
21005 {
21006 *fnamep = s;
21007 if (pbuf != NULL)
21008 {
21009 vim_free(*bufp); /* free any allocated file name */
21010 *bufp = pbuf;
21011 pbuf = NULL;
21012 }
21013 }
21014 }
21015 else
21016 {
21017 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21018 /* Only replace it when it starts with '~' */
21019 if (*dirname == '~')
21020 {
21021 s = vim_strsave(dirname);
21022 if (s != NULL)
21023 {
21024 *fnamep = s;
21025 vim_free(*bufp);
21026 *bufp = s;
21027 }
21028 }
21029 }
21030 vim_free(pbuf);
21031 }
21032 }
21033
21034 tail = gettail(*fnamep);
21035 *fnamelen = (int)STRLEN(*fnamep);
21036
21037 /* ":h" - head, remove "/file_name", can be repeated */
21038 /* Don't remove the first "/" or "c:\" */
21039 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21040 {
21041 valid |= VALID_HEAD;
21042 *usedlen += 2;
21043 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021044 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 --tail;
21046 *fnamelen = (int)(tail - *fnamep);
21047#ifdef VMS
21048 if (*fnamelen > 0)
21049 *fnamelen += 1; /* the path separator is part of the path */
21050#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021051 while (tail > s && !after_pathsep(s, tail))
21052 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 }
21054
21055 /* ":8" - shortname */
21056 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21057 {
21058 *usedlen += 2;
21059#ifdef WIN3264
21060 has_shortname = 1;
21061#endif
21062 }
21063
21064#ifdef WIN3264
21065 /* Check shortname after we have done 'heads' and before we do 'tails'
21066 */
21067 if (has_shortname)
21068 {
21069 pbuf = NULL;
21070 /* Copy the string if it is shortened by :h */
21071 if (*fnamelen < (int)STRLEN(*fnamep))
21072 {
21073 p = vim_strnsave(*fnamep, *fnamelen);
21074 if (p == 0)
21075 return -1;
21076 vim_free(*bufp);
21077 *bufp = *fnamep = p;
21078 }
21079
21080 /* Split into two implementations - makes it easier. First is where
21081 * there isn't a full name already, second is where there is.
21082 */
21083 if (!has_fullname && !vim_isAbsName(*fnamep))
21084 {
21085 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21086 return -1;
21087 }
21088 else
21089 {
21090 int l;
21091
21092 /* Simple case, already have the full-name
21093 * Nearly always shorter, so try first time. */
21094 l = *fnamelen;
21095 if (!get_short_pathname(fnamep, bufp, &l))
21096 return -1;
21097
21098 if (l == 0)
21099 {
21100 /* Couldn't find the filename.. search the paths.
21101 */
21102 l = *fnamelen;
21103 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21104 return -1;
21105 }
21106 *fnamelen = l;
21107 }
21108 }
21109#endif /* WIN3264 */
21110
21111 /* ":t" - tail, just the basename */
21112 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21113 {
21114 *usedlen += 2;
21115 *fnamelen -= (int)(tail - *fnamep);
21116 *fnamep = tail;
21117 }
21118
21119 /* ":e" - extension, can be repeated */
21120 /* ":r" - root, without extension, can be repeated */
21121 while (src[*usedlen] == ':'
21122 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21123 {
21124 /* find a '.' in the tail:
21125 * - for second :e: before the current fname
21126 * - otherwise: The last '.'
21127 */
21128 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21129 s = *fnamep - 2;
21130 else
21131 s = *fnamep + *fnamelen - 1;
21132 for ( ; s > tail; --s)
21133 if (s[0] == '.')
21134 break;
21135 if (src[*usedlen + 1] == 'e') /* :e */
21136 {
21137 if (s > tail)
21138 {
21139 *fnamelen += (int)(*fnamep - (s + 1));
21140 *fnamep = s + 1;
21141#ifdef VMS
21142 /* cut version from the extension */
21143 s = *fnamep + *fnamelen - 1;
21144 for ( ; s > *fnamep; --s)
21145 if (s[0] == ';')
21146 break;
21147 if (s > *fnamep)
21148 *fnamelen = s - *fnamep;
21149#endif
21150 }
21151 else if (*fnamep <= tail)
21152 *fnamelen = 0;
21153 }
21154 else /* :r */
21155 {
21156 if (s > tail) /* remove one extension */
21157 *fnamelen = (int)(s - *fnamep);
21158 }
21159 *usedlen += 2;
21160 }
21161
21162 /* ":s?pat?foo?" - substitute */
21163 /* ":gs?pat?foo?" - global substitute */
21164 if (src[*usedlen] == ':'
21165 && (src[*usedlen + 1] == 's'
21166 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21167 {
21168 char_u *str;
21169 char_u *pat;
21170 char_u *sub;
21171 int sep;
21172 char_u *flags;
21173 int didit = FALSE;
21174
21175 flags = (char_u *)"";
21176 s = src + *usedlen + 2;
21177 if (src[*usedlen + 1] == 'g')
21178 {
21179 flags = (char_u *)"g";
21180 ++s;
21181 }
21182
21183 sep = *s++;
21184 if (sep)
21185 {
21186 /* find end of pattern */
21187 p = vim_strchr(s, sep);
21188 if (p != NULL)
21189 {
21190 pat = vim_strnsave(s, (int)(p - s));
21191 if (pat != NULL)
21192 {
21193 s = p + 1;
21194 /* find end of substitution */
21195 p = vim_strchr(s, sep);
21196 if (p != NULL)
21197 {
21198 sub = vim_strnsave(s, (int)(p - s));
21199 str = vim_strnsave(*fnamep, *fnamelen);
21200 if (sub != NULL && str != NULL)
21201 {
21202 *usedlen = (int)(p + 1 - src);
21203 s = do_string_sub(str, pat, sub, flags);
21204 if (s != NULL)
21205 {
21206 *fnamep = s;
21207 *fnamelen = (int)STRLEN(s);
21208 vim_free(*bufp);
21209 *bufp = s;
21210 didit = TRUE;
21211 }
21212 }
21213 vim_free(sub);
21214 vim_free(str);
21215 }
21216 vim_free(pat);
21217 }
21218 }
21219 /* after using ":s", repeat all the modifiers */
21220 if (didit)
21221 goto repeat;
21222 }
21223 }
21224
21225 return valid;
21226}
21227
21228/*
21229 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21230 * "flags" can be "g" to do a global substitute.
21231 * Returns an allocated string, NULL for error.
21232 */
21233 char_u *
21234do_string_sub(str, pat, sub, flags)
21235 char_u *str;
21236 char_u *pat;
21237 char_u *sub;
21238 char_u *flags;
21239{
21240 int sublen;
21241 regmatch_T regmatch;
21242 int i;
21243 int do_all;
21244 char_u *tail;
21245 garray_T ga;
21246 char_u *ret;
21247 char_u *save_cpo;
21248
21249 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21250 save_cpo = p_cpo;
21251 p_cpo = (char_u *)"";
21252
21253 ga_init2(&ga, 1, 200);
21254
21255 do_all = (flags[0] == 'g');
21256
21257 regmatch.rm_ic = p_ic;
21258 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21259 if (regmatch.regprog != NULL)
21260 {
21261 tail = str;
21262 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21263 {
21264 /*
21265 * Get some space for a temporary buffer to do the substitution
21266 * into. It will contain:
21267 * - The text up to where the match is.
21268 * - The substituted text.
21269 * - The text after the match.
21270 */
21271 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21272 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21273 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21274 {
21275 ga_clear(&ga);
21276 break;
21277 }
21278
21279 /* copy the text up to where the match is */
21280 i = (int)(regmatch.startp[0] - tail);
21281 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21282 /* add the substituted text */
21283 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21284 + ga.ga_len + i, TRUE, TRUE, FALSE);
21285 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 /* avoid getting stuck on a match with an empty string */
21287 if (tail == regmatch.endp[0])
21288 {
21289 if (*tail == NUL)
21290 break;
21291 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21292 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 }
21294 else
21295 {
21296 tail = regmatch.endp[0];
21297 if (*tail == NUL)
21298 break;
21299 }
21300 if (!do_all)
21301 break;
21302 }
21303
21304 if (ga.ga_data != NULL)
21305 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21306
21307 vim_free(regmatch.regprog);
21308 }
21309
21310 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21311 ga_clear(&ga);
21312 p_cpo = save_cpo;
21313
21314 return ret;
21315}
21316
21317#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */