blob: 37db67c430a336aef82952a1b3395702d190ab67 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
373static void list_glob_vars __ARGS((int *first));
374static void list_buf_vars __ARGS((int *first));
375static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000377static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_vim_vars __ARGS((int *first));
380static void list_script_vars __ARGS((int *first));
381static void list_func_vars __ARGS((int *first));
382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000438static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
456static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000478static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000480#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000481static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000482static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
490static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000503static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000517static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000519static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000525static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000533static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000534static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000535static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000538static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000546static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000560static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000566static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000582static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000586static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000590#ifdef vim_mkdir
591static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
592#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000596static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000598static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000601static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000602static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000615static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000617static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000624static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000626static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000627static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000629static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000631static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000634static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000638static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639#ifdef HAVE_STRFTIME
640static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
642static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000654static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000655static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000657static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000658static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000660static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000674static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000680static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static int get_env_len __ARGS((char_u **arg));
682static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
685#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
686#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
687 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000690static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000691static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
692static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static typval_T *alloc_tv __ARGS((void));
694static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void init_tv __ARGS((typval_T *varp));
696static long get_tv_number __ARGS((typval_T *varp));
697static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000698static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static char_u *get_tv_string __ARGS((typval_T *varp));
700static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000701static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000703static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
705static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
706static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000707static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
708static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
710static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000711static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000712static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000714static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
716static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
717static int eval_fname_script __ARGS((char_u *p));
718static int eval_fname_sid __ARGS((char_u *p));
719static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static ufunc_T *find_func __ARGS((char_u *name));
721static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000722static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723#ifdef FEAT_PROFILE
724static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000725static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
726static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_total_cmp __ARGS((const void *s1, const void *s2));
732static int
733# ifdef __BORLANDC__
734 _RTLENTRYF
735# endif
736 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000737#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000738static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000739static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000740static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void func_free __ARGS((ufunc_T *fp));
742static void func_unref __ARGS((char_u *name));
743static void func_ref __ARGS((char_u *name));
744static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
745static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000746static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
747static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000748static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000749static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000750static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000752/* Character used as separated in autoload function/variable names. */
753#define AUTOLOAD_CHAR '#'
754
Bram Moolenaar33570922005-01-25 22:26:29 +0000755/*
756 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000757 */
758 void
759eval_init()
760{
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 int i;
762 struct vimvar *p;
763
764 init_var_dict(&globvardict, &globvars_var);
765 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000766 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000767 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768
769 for (i = 0; i < VV_LEN; ++i)
770 {
771 p = &vimvars[i];
772 STRCPY(p->vv_di.di_key, p->vv_name);
773 if (p->vv_flags & VV_RO)
774 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
775 else if (p->vv_flags & VV_RO_SBX)
776 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
777 else
778 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779
780 /* add to v: scope dict, unless the value is not always available */
781 if (p->vv_type != VAR_UNKNOWN)
782 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000784 /* add to compat scope dict */
785 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000787}
788
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789#if defined(EXITFREE) || defined(PROTO)
790 void
791eval_clear()
792{
793 int i;
794 struct vimvar *p;
795
796 for (i = 0; i < VV_LEN; ++i)
797 {
798 p = &vimvars[i];
799 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000800 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 p->vv_di.di_tv.vval.v_string = NULL;
803 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 }
805 hash_clear(&vimvarht);
806 hash_clear(&compat_hashtab);
807
808 /* script-local variables */
809 for (i = 1; i <= ga_scripts.ga_len; ++i)
810 vars_clear(&SCRIPT_VARS(i));
811 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000813
814 /* global variables */
815 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 hash_clear(&func_hashtab);
820
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000821 /* autoloaded script names */
822 ga_clear_strings(&ga_loaded);
823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000824 /* unreferenced lists and dicts */
825 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000826}
827#endif
828
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Return the name of the executed function.
831 */
832 char_u *
833func_name(cookie)
834 void *cookie;
835{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000836 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
839/*
840 * Return the address holding the next breakpoint line for a funccall cookie.
841 */
842 linenr_T *
843func_breakpoint(cookie)
844 void *cookie;
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
849/*
850 * Return the address holding the debug tick for a funccall cookie.
851 */
852 int *
853func_dbg_tick(cookie)
854 void *cookie;
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
859/*
860 * Return the nesting level for a funccall cookie.
861 */
862 int
863func_level(cookie)
864 void *cookie;
865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000870funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872/*
873 * Return TRUE when a function was ended by a ":return" command.
874 */
875 int
876current_func_returned()
877{
878 return current_funccal->returned;
879}
880
881
882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Set an internal variable to a string value. Creates the variable if it does
884 * not already exist.
885 */
886 void
887set_internal_string_var(name, value)
888 char_u *name;
889 char_u *value;
890{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 val = vim_strsave(value);
895 if (val != NULL)
896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000898 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 }
904}
905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000907static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static char_u *redir_endp = NULL;
909static char_u *redir_varname = NULL;
910
911/*
912 * Start recording command output to a variable
913 * Returns OK if successfully completed the setup. FAIL otherwise.
914 */
915 int
916var_redir_start(name, append)
917 char_u *name;
918 int append; /* append to an existing variable */
919{
920 int save_emsg;
921 int err;
922 typval_T tv;
923
924 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000925 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000926 {
927 EMSG(_(e_invarg));
928 return FAIL;
929 }
930
931 redir_varname = vim_strsave(name);
932 if (redir_varname == NULL)
933 return FAIL;
934
935 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
936 if (redir_lval == NULL)
937 {
938 var_redir_stop();
939 return FAIL;
940 }
941
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000942 /* The output is stored in growarray "redir_ga" until redirection ends. */
943 ga_init2(&redir_ga, (int)sizeof(char), 500);
944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000946 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
947 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
949 {
950 if (redir_endp != NULL && *redir_endp != NUL)
951 /* Trailing characters are present after the variable name */
952 EMSG(_(e_trailing));
953 else
954 EMSG(_(e_invarg));
955 var_redir_stop();
956 return FAIL;
957 }
958
959 /* check if we can write to the variable: set it to or append an empty
960 * string */
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 tv.v_type = VAR_STRING;
964 tv.vval.v_string = (char_u *)"";
965 if (append)
966 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
967 else
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
969 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000970 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971 if (err)
972 {
973 var_redir_stop();
974 return FAIL;
975 }
976 if (redir_lval->ll_newkey != NULL)
977 {
978 /* Dictionary item was created, don't do it again. */
979 vim_free(redir_lval->ll_newkey);
980 redir_lval->ll_newkey = NULL;
981 }
982
983 return OK;
984}
985
986/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000987 * Append "value[value_len]" to the variable set by var_redir_start().
988 * The actual appending is postponed until redirection ends, because the value
989 * appended may in fact be the string we write to, changing it may cause freed
990 * memory to be used:
991 * :redir => foo
992 * :let foo
993 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 */
995 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000996var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001000 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001
1002 if (redir_lval == NULL)
1003 return;
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001006 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011 {
1012 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 }
1015 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017}
1018
1019/*
1020 * Stop redirecting command output to a variable.
1021 */
1022 void
1023var_redir_stop()
1024{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001025 typval_T tv;
1026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 if (redir_lval != NULL)
1028 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 /* Append the trailing NUL. */
1030 ga_append(&redir_ga, NUL);
1031
1032 /* Assign the text to the variable. */
1033 tv.v_type = VAR_STRING;
1034 tv.vval.v_string = redir_ga.ga_data;
1035 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1036 vim_free(tv.vval.v_string);
1037
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 clear_lval(redir_lval);
1039 vim_free(redir_lval);
1040 redir_lval = NULL;
1041 }
1042 vim_free(redir_varname);
1043 redir_varname = NULL;
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# if defined(FEAT_MBYTE) || defined(PROTO)
1047 int
1048eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1049 char_u *enc_from;
1050 char_u *enc_to;
1051 char_u *fname_from;
1052 char_u *fname_to;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1057 set_vim_var_string(VV_CC_TO, enc_to, -1);
1058 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1059 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1060 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1061 err = TRUE;
1062 set_vim_var_string(VV_CC_FROM, NULL, -1);
1063 set_vim_var_string(VV_CC_TO, NULL, -1);
1064 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066
1067 if (err)
1068 return FAIL;
1069 return OK;
1070}
1071# endif
1072
1073# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1074 int
1075eval_printexpr(fname, args)
1076 char_u *fname;
1077 char_u *args;
1078{
1079 int err = FALSE;
1080
1081 set_vim_var_string(VV_FNAME_IN, fname, -1);
1082 set_vim_var_string(VV_CMDARG, args, -1);
1083 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1084 err = TRUE;
1085 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1086 set_vim_var_string(VV_CMDARG, NULL, -1);
1087
1088 if (err)
1089 {
1090 mch_remove(fname);
1091 return FAIL;
1092 }
1093 return OK;
1094}
1095# endif
1096
1097# if defined(FEAT_DIFF) || defined(PROTO)
1098 void
1099eval_diff(origfile, newfile, outfile)
1100 char_u *origfile;
1101 char_u *newfile;
1102 char_u *outfile;
1103{
1104 int err = FALSE;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114
1115 void
1116eval_patch(origfile, difffile, outfile)
1117 char_u *origfile;
1118 char_u *difffile;
1119 char_u *outfile;
1120{
1121 int err;
1122
1123 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1124 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1125 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1126 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1129 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1130}
1131# endif
1132
1133/*
1134 * Top level evaluation function, returning a boolean.
1135 * Sets "error" to TRUE if there was an error.
1136 * Return TRUE or FALSE.
1137 */
1138 int
1139eval_to_bool(arg, error, nextcmd, skip)
1140 char_u *arg;
1141 int *error;
1142 char_u **nextcmd;
1143 int skip; /* only parse, don't execute */
1144{
Bram Moolenaar33570922005-01-25 22:26:29 +00001145 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 int retval = FALSE;
1147
1148 if (skip)
1149 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 {
1154 *error = FALSE;
1155 if (!skip)
1156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 }
1161 if (skip)
1162 --emsg_skip;
1163
1164 return retval;
1165}
1166
1167/*
1168 * Top level evaluation function, returning a string. If "skip" is TRUE,
1169 * only parsing to "nextcmd" is done, without reporting errors. Return
1170 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1171 */
1172 char_u *
1173eval_to_string_skip(arg, nextcmd, skip)
1174 char_u *arg;
1175 char_u **nextcmd;
1176 int skip; /* only parse, don't execute */
1177{
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *retval;
1180
1181 if (skip)
1182 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 retval = NULL;
1185 else
1186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 retval = vim_strsave(get_tv_string(&tv));
1188 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 if (skip)
1191 --emsg_skip;
1192
1193 return retval;
1194}
1195
1196/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001197 * Skip over an expression at "*pp".
1198 * Return FAIL for an error, OK otherwise.
1199 */
1200 int
1201skip_expr(pp)
1202 char_u **pp;
1203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001205
1206 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208}
1209
1210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * Top level evaluation function, returning a string.
1212 * Return pointer to allocated memory, or NULL for failure.
1213 */
1214 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001215eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *arg;
1217 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 retval = NULL;
1226 else
1227 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001228 if (dolist && tv.v_type == VAR_LIST)
1229 {
1230 ga_init2(&ga, (int)sizeof(char), 80);
1231 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1232 ga_append(&ga, NUL);
1233 retval = (char_u *)ga.ga_data;
1234 }
1235 else
1236 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239
1240 return retval;
1241}
1242
1243/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 * Call eval_to_string() without using current local variables and using
1245 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
1247 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 char_u *arg;
1250 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 char_u *retval;
1254 void *save_funccalp;
1255
1256 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001257 if (use_sandbox)
1258 ++sandbox;
1259 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001260 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 if (use_sandbox)
1262 --sandbox;
1263 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 restore_funccal(save_funccalp);
1265 return retval;
1266}
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Top level evaluation function, returning a number.
1270 * Evaluates "expr" silently.
1271 * Returns -1 for an error.
1272 */
1273 int
1274eval_to_number(expr)
1275 char_u *expr;
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001279 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 ++emsg_off;
1282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 retval = -1;
1285 else
1286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001287 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 --emsg_off;
1291
1292 return retval;
1293}
1294
Bram Moolenaara40058a2005-07-11 22:42:07 +00001295/*
1296 * Prepare v: variable "idx" to be used.
1297 * Save the current typeval in "save_tv".
1298 * When not used yet add the variable to the v: hashtable.
1299 */
1300 static void
1301prepare_vimvar(idx, save_tv)
1302 int idx;
1303 typval_T *save_tv;
1304{
1305 *save_tv = vimvars[idx].vv_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1308}
1309
1310/*
1311 * Restore v: variable "idx" to typeval "save_tv".
1312 * When no longer defined, remove the variable from the v: hashtable.
1313 */
1314 static void
1315restore_vimvar(idx, save_tv)
1316 int idx;
1317 typval_T *save_tv;
1318{
1319 hashitem_T *hi;
1320
1321 clear_tv(&vimvars[idx].vv_tv);
1322 vimvars[idx].vv_tv = *save_tv;
1323 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1324 {
1325 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1326 if (HASHITEM_EMPTY(hi))
1327 EMSG2(_(e_intern2), "restore_vimvar()");
1328 else
1329 hash_remove(&vimvarht, hi);
1330 }
1331}
1332
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001333#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001334/*
1335 * Evaluate an expression to a list with suggestions.
1336 * For the "expr:" part of 'spellsuggest'.
1337 */
1338 list_T *
1339eval_spell_expr(badword, expr)
1340 char_u *badword;
1341 char_u *expr;
1342{
1343 typval_T save_val;
1344 typval_T rettv;
1345 list_T *list = NULL;
1346 char_u *p = skipwhite(expr);
1347
1348 /* Set "v:val" to the bad word. */
1349 prepare_vimvar(VV_VAL, &save_val);
1350 vimvars[VV_VAL].vv_type = VAR_STRING;
1351 vimvars[VV_VAL].vv_str = badword;
1352 if (p_verbose == 0)
1353 ++emsg_off;
1354
1355 if (eval1(&p, &rettv, TRUE) == OK)
1356 {
1357 if (rettv.v_type != VAR_LIST)
1358 clear_tv(&rettv);
1359 else
1360 list = rettv.vval.v_list;
1361 }
1362
1363 if (p_verbose == 0)
1364 --emsg_off;
1365 vimvars[VV_VAL].vv_str = NULL;
1366 restore_vimvar(VV_VAL, &save_val);
1367
1368 return list;
1369}
1370
1371/*
1372 * "list" is supposed to contain two items: a word and a number. Return the
1373 * word in "pp" and the number as the return value.
1374 * Return -1 if anything isn't right.
1375 * Used to get the good word and score from the eval_spell_expr() result.
1376 */
1377 int
1378get_spellword(list, pp)
1379 list_T *list;
1380 char_u **pp;
1381{
1382 listitem_T *li;
1383
1384 li = list->lv_first;
1385 if (li == NULL)
1386 return -1;
1387 *pp = get_tv_string(&li->li_tv);
1388
1389 li = li->li_next;
1390 if (li == NULL)
1391 return -1;
1392 return get_tv_number(&li->li_tv);
1393}
1394#endif
1395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 * Top level evaluation function.
1398 * Returns an allocated typval_T with the result.
1399 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001400 */
1401 typval_T *
1402eval_expr(arg, nextcmd)
1403 char_u *arg;
1404 char_u **nextcmd;
1405{
1406 typval_T *tv;
1407
1408 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001409 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001410 {
1411 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001412 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001413 }
1414
1415 return tv;
1416}
1417
1418
Bram Moolenaar4f688582007-07-24 12:34:30 +00001419#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1420 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 static int
1427call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 char_u *func;
1429 int argc;
1430 char_u **argv;
1431 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 long n;
1436 int len;
1437 int i;
1438 int doesrange;
1439 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001442 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 for (i = 0; i < argc; i++)
1447 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001448 /* Pass a NULL or empty argument as an empty string */
1449 if (argv[i] == NULL || *argv[i] == NUL)
1450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001451 argvars[i].v_type = VAR_STRING;
1452 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001453 continue;
1454 }
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /* Recognize a number argument, the others must be strings. */
1457 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1458 if (len != 0 && len == (int)STRLEN(argv[i]))
1459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001460 argvars[i].v_type = VAR_NUMBER;
1461 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
1464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001465 argvars[i].v_type = VAR_STRING;
1466 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 }
1469
1470 if (safe)
1471 {
1472 save_funccalp = save_funccal();
1473 ++sandbox;
1474 }
1475
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1477 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (safe)
1481 {
1482 --sandbox;
1483 restore_funccal(save_funccalp);
1484 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 vim_free(argvars);
1486
1487 if (ret == FAIL)
1488 clear_tv(rettv);
1489
1490 return ret;
1491}
1492
Bram Moolenaar4f688582007-07-24 12:34:30 +00001493# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001495 * Call vimL function "func" and return the result as a string.
1496 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 * Uses argv[argc] for the function arguments.
1498 */
1499 void *
1500call_func_retstr(func, argc, argv, safe)
1501 char_u *func;
1502 int argc;
1503 char_u **argv;
1504 int safe; /* use the sandbox */
1505{
1506 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001507 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508
1509 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1510 return NULL;
1511
1512 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 return retval;
1515}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517
Bram Moolenaar4f688582007-07-24 12:34:30 +00001518# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001519/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001520 * Call vimL function "func" and return the result as a number.
1521 * Returns -1 when calling the function fails.
1522 * Uses argv[argc] for the function arguments.
1523 */
1524 long
1525call_func_retnr(func, argc, argv, safe)
1526 char_u *func;
1527 int argc;
1528 char_u **argv;
1529 int safe; /* use the sandbox */
1530{
1531 typval_T rettv;
1532 long retval;
1533
1534 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1535 return -1;
1536
1537 retval = get_tv_number_chk(&rettv, NULL);
1538 clear_tv(&rettv);
1539 return retval;
1540}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001541# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001542
1543/*
1544 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Uses argv[argc] for the function arguments.
1546 */
1547 void *
1548call_func_retlist(func, argc, argv, safe)
1549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
1553{
1554 typval_T rettv;
1555
1556 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1557 return NULL;
1558
1559 if (rettv.v_type != VAR_LIST)
1560 {
1561 clear_tv(&rettv);
1562 return NULL;
1563 }
1564
1565 return rettv.vval.v_list;
1566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#endif
1568
Bram Moolenaar4f688582007-07-24 12:34:30 +00001569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570/*
1571 * Save the current function call pointer, and set it to NULL.
1572 * Used when executing autocommands and for ":source".
1573 */
1574 void *
1575save_funccal()
1576{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001577 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 current_funccal = NULL;
1580 return (void *)fc;
1581}
1582
1583 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001584restore_funccal(vfc)
1585 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001587 funccall_T *fc = (funccall_T *)vfc;
1588
1589 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
Bram Moolenaar05159a02005-02-26 23:04:13 +00001592#if defined(FEAT_PROFILE) || defined(PROTO)
1593/*
1594 * Prepare profiling for entering a child or something else that is not
1595 * counted for the script/function itself.
1596 * Should always be called in pair with prof_child_exit().
1597 */
1598 void
1599prof_child_enter(tm)
1600 proftime_T *tm; /* place to store waittime */
1601{
1602 funccall_T *fc = current_funccal;
1603
1604 if (fc != NULL && fc->func->uf_profiling)
1605 profile_start(&fc->prof_child);
1606 script_prof_save(tm);
1607}
1608
1609/*
1610 * Take care of time spent in a child.
1611 * Should always be called after prof_child_enter().
1612 */
1613 void
1614prof_child_exit(tm)
1615 proftime_T *tm; /* where waittime was stored */
1616{
1617 funccall_T *fc = current_funccal;
1618
1619 if (fc != NULL && fc->func->uf_profiling)
1620 {
1621 profile_end(&fc->prof_child);
1622 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1623 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1624 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1625 }
1626 script_prof_restore(tm);
1627}
1628#endif
1629
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#ifdef FEAT_FOLDING
1632/*
1633 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1634 * it in "*cp". Doesn't give error messages.
1635 */
1636 int
1637eval_foldexpr(arg, cp)
1638 char_u *arg;
1639 int *cp;
1640{
Bram Moolenaar33570922005-01-25 22:26:29 +00001641 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int retval;
1643 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001644 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1645 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001648 if (use_sandbox)
1649 ++sandbox;
1650 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 retval = 0;
1654 else
1655 {
1656 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 if (tv.v_type == VAR_NUMBER)
1658 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001659 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 retval = 0;
1661 else
1662 {
1663 /* If the result is a string, check if there is a non-digit before
1664 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (!VIM_ISDIGIT(*s) && *s != '-')
1667 *cp = *s++;
1668 retval = atol((char *)s);
1669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001673 if (use_sandbox)
1674 --sandbox;
1675 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 return retval;
1678}
1679#endif
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 * ":let" list all variable values
1683 * ":let var1 var2" list variable values
1684 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 * ":let var += expr" assignment command.
1686 * ":let var -= expr" assignment command.
1687 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
1690 void
1691ex_let(eap)
1692 exarg_T *eap;
1693{
1694 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 int var_count = 0;
1699 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001701 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001702 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaardb552d602006-03-23 22:59:57 +00001704 argend = skip_var_list(arg, &var_count, &semicolon);
1705 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001707 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1708 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001709 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001712 /*
1713 * ":let" without "=": list variables
1714 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 if (*arg == '[')
1716 EMSG(_(e_invarg));
1717 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001719 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001723 list_glob_vars(&first);
1724 list_buf_vars(&first);
1725 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001727 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001728#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001729 list_script_vars(&first);
1730 list_func_vars(&first);
1731 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 eap->nextcmd = check_nextcmd(arg);
1734 }
1735 else
1736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 op[0] = '=';
1738 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001739 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 {
1741 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1742 op[0] = expr[-1]; /* +=, -= or .= */
1743 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (eap->skip)
1747 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (eap->skip)
1750 {
1751 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 --emsg_skip;
1754 }
1755 else if (i != FAIL)
1756 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 }
1762}
1763
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764/*
1765 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1766 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 * When "nextchars" is not NULL it points to a string with characters that
1768 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1769 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 * Returns OK or FAIL;
1771 */
1772 static int
1773ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1774 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 int copy; /* copy values from "tv", don't move */
1777 int semicolon; /* from skip_var_list() */
1778 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780{
1781 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 listitem_T *item;
1785 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
1787 if (*arg != '[')
1788 {
1789 /*
1790 * ":let var = expr" or ":for var in list"
1791 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 return FAIL;
1794 return OK;
1795 }
1796
1797 /*
1798 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1799 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 {
1802 EMSG(_(e_listreq));
1803 return FAIL;
1804 }
1805
1806 i = list_len(l);
1807 if (semicolon == 0 && var_count < i)
1808 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001809 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 return FAIL;
1811 }
1812 if (var_count - semicolon > i)
1813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001814 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 return FAIL;
1816 }
1817
1818 item = l->lv_first;
1819 while (*arg != ']')
1820 {
1821 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 item = item->li_next;
1824 if (arg == NULL)
1825 return FAIL;
1826
1827 arg = skipwhite(arg);
1828 if (*arg == ';')
1829 {
1830 /* Put the rest of the list (may be empty) in the var after ';'.
1831 * Create a new list for this. */
1832 l = list_alloc();
1833 if (l == NULL)
1834 return FAIL;
1835 while (item != NULL)
1836 {
1837 list_append_tv(l, &item->li_tv);
1838 item = item->li_next;
1839 }
1840
1841 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001842 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 ltv.vval.v_list = l;
1844 l->lv_refcount = 1;
1845
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1847 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 clear_tv(&ltv);
1849 if (arg == NULL)
1850 return FAIL;
1851 break;
1852 }
1853 else if (*arg != ',' && *arg != ']')
1854 {
1855 EMSG2(_(e_intern2), "ex_let_vars()");
1856 return FAIL;
1857 }
1858 }
1859
1860 return OK;
1861}
1862
1863/*
1864 * Skip over assignable variable "var" or list of variables "[var, var]".
1865 * Used for ":let varvar = expr" and ":for varvar in expr".
1866 * For "[var, var]" increment "*var_count" for each variable.
1867 * for "[var, var; var]" set "semicolon".
1868 * Return NULL for an error.
1869 */
1870 static char_u *
1871skip_var_list(arg, var_count, semicolon)
1872 char_u *arg;
1873 int *var_count;
1874 int *semicolon;
1875{
1876 char_u *p, *s;
1877
1878 if (*arg == '[')
1879 {
1880 /* "[var, var]": find the matching ']'. */
1881 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001882 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 {
1884 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1885 s = skip_var_one(p);
1886 if (s == p)
1887 {
1888 EMSG2(_(e_invarg2), p);
1889 return NULL;
1890 }
1891 ++*var_count;
1892
1893 p = skipwhite(s);
1894 if (*p == ']')
1895 break;
1896 else if (*p == ';')
1897 {
1898 if (*semicolon == 1)
1899 {
1900 EMSG(_("Double ; in list of variables"));
1901 return NULL;
1902 }
1903 *semicolon = 1;
1904 }
1905 else if (*p != ',')
1906 {
1907 EMSG2(_(e_invarg2), p);
1908 return NULL;
1909 }
1910 }
1911 return p + 1;
1912 }
1913 else
1914 return skip_var_one(arg);
1915}
1916
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001917/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001918 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001919 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 static char_u *
1922skip_var_one(arg)
1923 char_u *arg;
1924{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001925 if (*arg == '@' && arg[1] != NUL)
1926 return arg + 2;
1927 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1928 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929}
1930
Bram Moolenaara7043832005-01-21 11:56:39 +00001931/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 * List variables for hashtab "ht" with prefix "prefix".
1933 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001938 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001940 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001941{
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 hashitem_T *hi;
1943 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001944 int todo;
1945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001946 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001947 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1948 {
1949 if (!HASHITEM_EMPTY(hi))
1950 {
1951 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 di = HI2DI(hi);
1953 if (empty || di->di_tv.v_type != VAR_STRING
1954 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001956 }
1957 }
1958}
1959
1960/*
1961 * List global variables.
1962 */
1963 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964list_glob_vars(first)
1965 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001966{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001967 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968}
1969
1970/*
1971 * List buffer variables.
1972 */
1973 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001974list_buf_vars(first)
1975 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001976{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001977 char_u numbuf[NUMBUFLEN];
1978
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001979 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1980 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001981
1982 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001983 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1984 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985}
1986
1987/*
1988 * List window variables.
1989 */
1990 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001991list_win_vars(first)
1992 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001993{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001994 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1995 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001996}
1997
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998#ifdef FEAT_WINDOWS
1999/*
2000 * List tab page variables.
2001 */
2002 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002003list_tab_vars(first)
2004 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002005{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002006 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2007 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002008}
2009#endif
2010
Bram Moolenaara7043832005-01-21 11:56:39 +00002011/*
2012 * List Vim variables.
2013 */
2014 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002015list_vim_vars(first)
2016 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002018 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019}
2020
2021/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002022 * List script-local variables, if there is a script.
2023 */
2024 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002025list_script_vars(first)
2026 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002027{
2028 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002029 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2030 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002031}
2032
2033/*
2034 * List function variables, if there is a function.
2035 */
2036 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002037list_func_vars(first)
2038 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002039{
2040 if (current_funccal != NULL)
2041 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002042 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002043}
2044
2045/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 * List variables in "arg".
2047 */
2048 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002049list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 exarg_T *eap;
2051 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002052 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002053{
2054 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 char_u *name_start;
2058 char_u *arg_subsc;
2059 char_u *tofree;
2060 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061
2062 while (!ends_excmd(*arg) && !got_int)
2063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002066 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2068 {
2069 emsg_severe = TRUE;
2070 EMSG(_(e_trailing));
2071 break;
2072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 /* get_name_len() takes care of expanding curly braces */
2077 name_start = name = arg;
2078 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2079 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 /* This is mainly to keep test 49 working: when expanding
2082 * curly braces fails overrule the exception error message. */
2083 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 emsg_severe = TRUE;
2086 EMSG2(_(e_invarg2), arg);
2087 break;
2088 }
2089 error = TRUE;
2090 }
2091 else
2092 {
2093 if (tofree != NULL)
2094 name = tofree;
2095 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 else
2098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 /* handle d.key, l[idx], f(expr) */
2100 arg_subsc = arg;
2101 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002105 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 case 'g': list_glob_vars(first); break;
2110 case 'b': list_buf_vars(first); break;
2111 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002114#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 case 'v': list_vim_vars(first); break;
2116 case 's': list_script_vars(first); break;
2117 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 default:
2119 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 {
2124 char_u numbuf[NUMBUFLEN];
2125 char_u *tf;
2126 int c;
2127 char_u *s;
2128
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002129 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 c = *arg;
2131 *arg = NUL;
2132 list_one_var_a((char_u *)"",
2133 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 tv.v_type,
2135 s == NULL ? (char_u *)"" : s,
2136 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002137 *arg = c;
2138 vim_free(tf);
2139 }
2140 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 }
2143 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144
2145 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002147
2148 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 }
2150
2151 return arg;
2152}
2153
2154/*
2155 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2156 * Returns a pointer to the char just after the var name.
2157 * Returns NULL if there is an error.
2158 */
2159 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002162 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
2167 int c1;
2168 char_u *name;
2169 char_u *p;
2170 char_u *arg_end = NULL;
2171 int len;
2172 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174
2175 /*
2176 * ":let $VAR = expr": Set environment variable.
2177 */
2178 if (*arg == '$')
2179 {
2180 /* Find the end of the name. */
2181 ++arg;
2182 name = arg;
2183 len = get_env_len(&arg);
2184 if (len == 0)
2185 EMSG2(_(e_invarg2), name - 1);
2186 else
2187 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 if (op != NULL && (*op == '+' || *op == '-'))
2189 EMSG2(_(e_letwrong), op);
2190 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 EMSG(_(e_letunexp));
2193 else
2194 {
2195 c1 = name[len];
2196 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002197 p = get_tv_string_chk(tv);
2198 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 {
2200 int mustfree = FALSE;
2201 char_u *s = vim_getenv(name, &mustfree);
2202
2203 if (s != NULL)
2204 {
2205 p = tofree = concat_str(s, p);
2206 if (mustfree)
2207 vim_free(s);
2208 }
2209 }
2210 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 if (STRICMP(name, "HOME") == 0)
2214 init_homedir();
2215 else if (didset_vim && STRICMP(name, "VIM") == 0)
2216 didset_vim = FALSE;
2217 else if (didset_vimruntime
2218 && STRICMP(name, "VIMRUNTIME") == 0)
2219 didset_vimruntime = FALSE;
2220 arg_end = arg;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
2225 }
2226 }
2227
2228 /*
2229 * ":let &option = expr": Set option value.
2230 * ":let &l:option = expr": Set local option value.
2231 * ":let &g:option = expr": Set global option value.
2232 */
2233 else if (*arg == '&')
2234 {
2235 /* Find the end of the name. */
2236 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237 if (p == NULL || (endchars != NULL
2238 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 EMSG(_(e_letunexp));
2240 else
2241 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 long n;
2243 int opt_type;
2244 long numval;
2245 char_u *stringval = NULL;
2246 char_u *s;
2247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 c1 = *p;
2249 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250
2251 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 s = get_tv_string_chk(tv); /* != NULL if number or string */
2253 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 {
2255 opt_type = get_option_value(arg, &numval,
2256 &stringval, opt_flags);
2257 if ((opt_type == 1 && *op == '.')
2258 || (opt_type == 0 && *op != '.'))
2259 EMSG2(_(e_letwrong), op);
2260 else
2261 {
2262 if (opt_type == 1) /* number */
2263 {
2264 if (*op == '+')
2265 n = numval + n;
2266 else
2267 n = numval - n;
2268 }
2269 else if (opt_type == 0 && stringval != NULL) /* string */
2270 {
2271 s = concat_str(stringval, s);
2272 vim_free(stringval);
2273 stringval = s;
2274 }
2275 }
2276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 if (s != NULL)
2278 {
2279 set_option_value(arg, n, s, opt_flags);
2280 arg_end = p;
2281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002283 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
2286
2287 /*
2288 * ":let @r = expr": Set register contents.
2289 */
2290 else if (*arg == '@')
2291 {
2292 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 if (op != NULL && (*op == '+' || *op == '-'))
2294 EMSG2(_(e_letwrong), op);
2295 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002296 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 EMSG(_(e_letunexp));
2298 else
2299 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002300 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *s;
2302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303 p = get_tv_string_chk(tv);
2304 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002306 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 if (s != NULL)
2308 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002309 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 vim_free(s);
2311 }
2312 }
2313 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 arg_end = arg + 1;
2317 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002318 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
2320 }
2321
2322 /*
2323 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002326 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002330 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2334 EMSG(_(e_letunexp));
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 arg_end = p;
2339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343
2344 else
2345 EMSG2(_(e_invarg2), arg);
2346
2347 return arg_end;
2348}
2349
2350/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2352 */
2353 static int
2354check_changedtick(arg)
2355 char_u *arg;
2356{
2357 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2358 {
2359 EMSG2(_(e_readonlyvar), arg);
2360 return TRUE;
2361 }
2362 return FALSE;
2363}
2364
2365/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 * Get an lval: variable, Dict item or List item that can be assigned a value
2367 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2368 * "name.key", "name.key[expr]" etc.
2369 * Indexing only works if "name" is an existing List or Dictionary.
2370 * "name" points to the start of the name.
2371 * If "rettv" is not NULL it points to the value to be assigned.
2372 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2373 * wrong; must end in space or cmd separator.
2374 *
2375 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002376 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 */
2379 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002380get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002382 typval_T *rettv;
2383 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 int unlet;
2385 int skip;
2386 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002387 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 char_u *expr_start, *expr_end;
2391 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002392 dictitem_T *v;
2393 typval_T var1;
2394 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002396 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002399 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002402 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403
2404 if (skip)
2405 {
2406 /* When skipping just find the end of the name. */
2407 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002408 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 }
2410
2411 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002412 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (expr_start != NULL)
2414 {
2415 /* Don't expand the name when we already know there is an error. */
2416 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2417 && *p != '[' && *p != '.')
2418 {
2419 EMSG(_(e_trailing));
2420 return NULL;
2421 }
2422
2423 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2424 if (lp->ll_exp_name == NULL)
2425 {
2426 /* Report an invalid expression in braces, unless the
2427 * expression evaluation has been cancelled due to an
2428 * aborting error, an interrupt, or an exception. */
2429 if (!aborting() && !quiet)
2430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002431 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 EMSG2(_(e_invarg2), name);
2433 return NULL;
2434 }
2435 }
2436 lp->ll_name = lp->ll_exp_name;
2437 }
2438 else
2439 lp->ll_name = name;
2440
2441 /* Without [idx] or .key we are done. */
2442 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2443 return p;
2444
2445 cc = *p;
2446 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002447 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (v == NULL && !quiet)
2449 EMSG2(_(e_undefvar), lp->ll_name);
2450 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 if (v == NULL)
2452 return NULL;
2453
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 /*
2455 * Loop until no more [idx] or .key is following.
2456 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2461 && !(lp->ll_tv->v_type == VAR_DICT
2462 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (!quiet)
2465 EMSG(_("E689: Can only index a List or Dictionary"));
2466 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (!quiet)
2471 EMSG(_("E708: [:] must come last"));
2472 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 len = -1;
2476 if (*p == '.')
2477 {
2478 key = p + 1;
2479 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2480 ;
2481 if (len == 0)
2482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (!quiet)
2484 EMSG(_(e_emptykey));
2485 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 }
2487 p = key + len;
2488 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002489 else
2490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002492 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 if (*p == ':')
2494 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002495 else
2496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 empty1 = FALSE;
2498 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 if (get_tv_string_chk(&var1) == NULL)
2501 {
2502 /* not a number or string */
2503 clear_tv(&var1);
2504 return NULL;
2505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 }
2507
2508 /* Optionally get the second index [ :expr]. */
2509 if (*p == ':')
2510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 if (!empty1)
2516 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (rettv != NULL && (rettv->v_type != VAR_LIST
2520 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (!quiet)
2523 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 if (!empty1)
2525 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
2528 p = skipwhite(p + 1);
2529 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 else
2532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 if (!empty1)
2537 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002540 if (get_tv_string_chk(&var2) == NULL)
2541 {
2542 /* not a number or string */
2543 if (!empty1)
2544 clear_tv(&var1);
2545 clear_tv(&var2);
2546 return NULL;
2547 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
2557 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (!empty1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 }
2564
2565 /* Skip to past ']'. */
2566 ++p;
2567 }
2568
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
2571 if (len == -1)
2572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002574 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 if (*key == NUL)
2576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (!quiet)
2578 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 }
2582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 lp->ll_list = NULL;
2584 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002588 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 if (len == -1)
2594 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 }
2597 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (len == -1)
2602 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 p = NULL;
2605 break;
2606 }
2607 if (len == -1)
2608 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 }
2611 else
2612 {
2613 /*
2614 * Get the number and item for the only or first index of the List.
2615 */
2616 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 else
2619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002620 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 clear_tv(&var1);
2622 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 lp->ll_list = lp->ll_tv->vval.v_list;
2625 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2626 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002628 if (lp->ll_n1 < 0)
2629 {
2630 lp->ll_n1 = 0;
2631 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2632 }
2633 }
2634 if (lp->ll_li == NULL)
2635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 }
2640
2641 /*
2642 * May need to find the item or absolute index for the second
2643 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 * When no index given: "lp->ll_empty2" is TRUE.
2645 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2660 if (lp->ll_n1 < 0)
2661 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2662 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 }
2665
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
2669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 return p;
2671}
2672
2673/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 */
2676 static void
2677clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002678 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679{
2680 vim_free(lp->ll_exp_name);
2681 vim_free(lp->ll_newkey);
2682}
2683
2684/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002685 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 */
2689 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002695 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696{
2697 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002698 listitem_T *ri;
2699 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700
2701 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 cc = *endp;
2706 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 if (op != NULL && *op != '=')
2708 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002709 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002711 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002712 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002713 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 {
2715 if (tv_op(&tv, rettv, op) == OK)
2716 set_var(lp->ll_name, &tv, FALSE);
2717 clear_tv(&tv);
2718 }
2719 }
2720 else
2721 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002725 else if (tv_check_lock(lp->ll_newkey == NULL
2726 ? lp->ll_tv->v_lock
2727 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2728 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 else if (lp->ll_range)
2730 {
2731 /*
2732 * Assign the List values to the list items.
2733 */
2734 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 if (op != NULL && *op != '=')
2737 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2738 else
2739 {
2740 clear_tv(&lp->ll_li->li_tv);
2741 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 ri = ri->li_next;
2744 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2745 break;
2746 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002749 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 ri = NULL;
2752 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_li = lp->ll_li->li_next;
2756 ++lp->ll_n1;
2757 }
2758 if (ri != NULL)
2759 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 else if (lp->ll_empty2
2761 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 : lp->ll_n1 != lp->ll_n2)
2763 EMSG(_("E711: List value has not enough items"));
2764 }
2765 else
2766 {
2767 /*
2768 * Assign to a List or Dictionary item.
2769 */
2770 if (lp->ll_newkey != NULL)
2771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 if (op != NULL && *op != '=')
2773 {
2774 EMSG2(_(e_letwrong), op);
2775 return;
2776 }
2777
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (di == NULL)
2781 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2783 {
2784 vim_free(di);
2785 return;
2786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002788 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002789 else if (op != NULL && *op != '=')
2790 {
2791 tv_op(lp->ll_tv, rettv, op);
2792 return;
2793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 /*
2798 * Assign the value to the variable or list item.
2799 */
2800 if (copy)
2801 copy_tv(rettv, lp->ll_tv);
2802 else
2803 {
2804 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002805 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 }
2808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809}
2810
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002811/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2813 * Returns OK or FAIL.
2814 */
2815 static int
2816tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 typval_T *tv1;
2818 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 char_u *op;
2820{
2821 long n;
2822 char_u numbuf[NUMBUFLEN];
2823 char_u *s;
2824
2825 /* Can't do anything with a Funcref or a Dict on the right. */
2826 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2827 {
2828 switch (tv1->v_type)
2829 {
2830 case VAR_DICT:
2831 case VAR_FUNC:
2832 break;
2833
2834 case VAR_LIST:
2835 if (*op != '+' || tv2->v_type != VAR_LIST)
2836 break;
2837 /* List += List */
2838 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2839 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2840 return OK;
2841
2842 case VAR_NUMBER:
2843 case VAR_STRING:
2844 if (tv2->v_type == VAR_LIST)
2845 break;
2846 if (*op == '+' || *op == '-')
2847 {
2848 /* nr += nr or nr -= nr*/
2849 n = get_tv_number(tv1);
2850 if (*op == '+')
2851 n += get_tv_number(tv2);
2852 else
2853 n -= get_tv_number(tv2);
2854 clear_tv(tv1);
2855 tv1->v_type = VAR_NUMBER;
2856 tv1->vval.v_number = n;
2857 }
2858 else
2859 {
2860 /* str .= str */
2861 s = get_tv_string(tv1);
2862 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2863 clear_tv(tv1);
2864 tv1->v_type = VAR_STRING;
2865 tv1->vval.v_string = s;
2866 }
2867 return OK;
2868 }
2869 }
2870
2871 EMSG2(_(e_letwrong), op);
2872 return FAIL;
2873}
2874
2875/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 * Add a watcher to a list.
2877 */
2878 static void
2879list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 list_T *l;
2881 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882{
2883 lw->lw_next = l->lv_watch;
2884 l->lv_watch = lw;
2885}
2886
2887/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002888 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 * No warning when it isn't found...
2890 */
2891 static void
2892list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 list_T *l;
2894 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895{
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897
2898 lwp = &l->lv_watch;
2899 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2900 {
2901 if (lw == lwrem)
2902 {
2903 *lwp = lw->lw_next;
2904 break;
2905 }
2906 lwp = &lw->lw_next;
2907 }
2908}
2909
2910/*
2911 * Just before removing an item from a list: advance watchers to the next
2912 * item.
2913 */
2914 static void
2915list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 list_T *l;
2917 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918{
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920
2921 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2922 if (lw->lw_item == item)
2923 lw->lw_item = item->li_next;
2924}
2925
2926/*
2927 * Evaluate the expression used in a ":for var in expr" command.
2928 * "arg" points to "var".
2929 * Set "*errp" to TRUE for an error, FALSE otherwise;
2930 * Return a pointer that holds the info. Null when there is an error.
2931 */
2932 void *
2933eval_for_line(arg, errp, nextcmdp, skip)
2934 char_u *arg;
2935 int *errp;
2936 char_u **nextcmdp;
2937 int skip;
2938{
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 typval_T tv;
2942 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943
2944 *errp = TRUE; /* default: there is an error */
2945
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947 if (fi == NULL)
2948 return NULL;
2949
2950 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2951 if (expr == NULL)
2952 return fi;
2953
2954 expr = skipwhite(expr);
2955 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002957 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002958 return fi;
2959 }
2960
2961 if (skip)
2962 ++emsg_skip;
2963 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2964 {
2965 *errp = FALSE;
2966 if (!skip)
2967 {
2968 l = tv.vval.v_list;
2969 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002970 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002972 clear_tv(&tv);
2973 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002974 else
2975 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002976 /* No need to increment the refcount, it's already set for the
2977 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978 fi->fi_list = l;
2979 list_add_watch(l, &fi->fi_lw);
2980 fi->fi_lw.lw_item = l->lv_first;
2981 }
2982 }
2983 }
2984 if (skip)
2985 --emsg_skip;
2986
2987 return fi;
2988}
2989
2990/*
2991 * Use the first item in a ":for" list. Advance to the next.
2992 * Assign the values to the variable (list). "arg" points to the first one.
2993 * Return TRUE when a valid item was found, FALSE when at end of list or
2994 * something wrong.
2995 */
2996 int
2997next_for_item(fi_void, arg)
2998 void *fi_void;
2999 char_u *arg;
3000{
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004
3005 item = fi->fi_lw.lw_item;
3006 if (item == NULL)
3007 result = FALSE;
3008 else
3009 {
3010 fi->fi_lw.lw_item = item->li_next;
3011 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3012 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3013 }
3014 return result;
3015}
3016
3017/*
3018 * Free the structure used to store info used by ":for".
3019 */
3020 void
3021free_for_info(fi_void)
3022 void *fi_void;
3023{
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003026 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003027 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003029 list_unref(fi->fi_list);
3030 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003031 vim_free(fi);
3032}
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3035
3036 void
3037set_context_for_expression(xp, arg, cmdidx)
3038 expand_T *xp;
3039 char_u *arg;
3040 cmdidx_T cmdidx;
3041{
3042 int got_eq = FALSE;
3043 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046 if (cmdidx == CMD_let)
3047 {
3048 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003049 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050 {
3051 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003052 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 {
3054 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003055 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056 if (vim_iswhite(*p))
3057 break;
3058 }
3059 return;
3060 }
3061 }
3062 else
3063 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3064 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 while ((xp->xp_pattern = vim_strpbrk(arg,
3066 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3067 {
3068 c = *xp->xp_pattern;
3069 if (c == '&')
3070 {
3071 c = xp->xp_pattern[1];
3072 if (c == '&')
3073 {
3074 ++xp->xp_pattern;
3075 xp->xp_context = cmdidx != CMD_let || got_eq
3076 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3077 }
3078 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003081 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3082 xp->xp_pattern += 2;
3083
3084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 else if (c == '$')
3087 {
3088 /* environment variable */
3089 xp->xp_context = EXPAND_ENV_VARS;
3090 }
3091 else if (c == '=')
3092 {
3093 got_eq = TRUE;
3094 xp->xp_context = EXPAND_EXPRESSION;
3095 }
3096 else if (c == '<'
3097 && xp->xp_context == EXPAND_FUNCTIONS
3098 && vim_strchr(xp->xp_pattern, '(') == NULL)
3099 {
3100 /* Function name can start with "<SNR>" */
3101 break;
3102 }
3103 else if (cmdidx != CMD_let || got_eq)
3104 {
3105 if (c == '"') /* string */
3106 {
3107 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3108 if (c == '\\' && xp->xp_pattern[1] != NUL)
3109 ++xp->xp_pattern;
3110 xp->xp_context = EXPAND_NOTHING;
3111 }
3112 else if (c == '\'') /* literal string */
3113 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003114 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3116 /* skip */ ;
3117 xp->xp_context = EXPAND_NOTHING;
3118 }
3119 else if (c == '|')
3120 {
3121 if (xp->xp_pattern[1] == '|')
3122 {
3123 ++xp->xp_pattern;
3124 xp->xp_context = EXPAND_EXPRESSION;
3125 }
3126 else
3127 xp->xp_context = EXPAND_COMMANDS;
3128 }
3129 else
3130 xp->xp_context = EXPAND_EXPRESSION;
3131 }
3132 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133 /* Doesn't look like something valid, expand as an expression
3134 * anyway. */
3135 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 arg = xp->xp_pattern;
3137 if (*arg != NUL)
3138 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3139 /* skip */ ;
3140 }
3141 xp->xp_pattern = arg;
3142}
3143
3144#endif /* FEAT_CMDL_COMPL */
3145
3146/*
3147 * ":1,25call func(arg1, arg2)" function call.
3148 */
3149 void
3150ex_call(eap)
3151 exarg_T *eap;
3152{
3153 char_u *arg = eap->arg;
3154 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 linenr_T lnum;
3160 int doesrange;
3161 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003162 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003164 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003165 if (fudi.fd_newkey != NULL)
3166 {
3167 /* Still need to give an error message for missing key. */
3168 EMSG2(_(e_dictkey), fudi.fd_newkey);
3169 vim_free(fudi.fd_newkey);
3170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003171 if (tofree == NULL)
3172 return;
3173
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 /* Increase refcount on dictionary, it could get deleted when evaluating
3175 * the arguments. */
3176 if (fudi.fd_dict != NULL)
3177 ++fudi.fd_dict->dv_refcount;
3178
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003180 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaar532c7802005-01-27 14:44:31 +00003183 /* Skip white space to allow ":call func ()". Not good, but required for
3184 * backward compatibility. */
3185 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 if (*startarg != '(')
3189 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003190 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 goto end;
3192 }
3193
3194 /*
3195 * When skipping, evaluate the function once, to find the end of the
3196 * arguments.
3197 * When the function takes a range, this is discovered after the first
3198 * call, and the loop is broken.
3199 */
3200 if (eap->skip)
3201 {
3202 ++emsg_skip;
3203 lnum = eap->line2; /* do it once, also with an invalid range */
3204 }
3205 else
3206 lnum = eap->line1;
3207 for ( ; lnum <= eap->line2; ++lnum)
3208 {
3209 if (!eap->skip && eap->addr_count > 0)
3210 {
3211 curwin->w_cursor.lnum = lnum;
3212 curwin->w_cursor.col = 0;
3213 }
3214 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003215 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003216 eap->line1, eap->line2, &doesrange,
3217 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 failed = TRUE;
3220 break;
3221 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003222
3223 /* Handle a function returning a Funcref, Dictionary or List. */
3224 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3225 {
3226 failed = TRUE;
3227 break;
3228 }
3229
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (doesrange || eap->skip)
3232 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003237 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (aborting())
3239 break;
3240 }
3241 if (eap->skip)
3242 --emsg_skip;
3243
3244 if (!failed)
3245 {
3246 /* Check for trailing illegal characters and a following command. */
3247 if (!ends_excmd(*arg))
3248 {
3249 emsg_severe = TRUE;
3250 EMSG(_(e_trailing));
3251 }
3252 else
3253 eap->nextcmd = check_nextcmd(arg);
3254 }
3255
3256end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003257 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003258 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260
3261/*
3262 * ":unlet[!] var1 ... " command.
3263 */
3264 void
3265ex_unlet(eap)
3266 exarg_T *eap;
3267{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 ex_unletlock(eap, eap->arg, 0);
3269}
3270
3271/*
3272 * ":lockvar" and ":unlockvar" commands
3273 */
3274 void
3275ex_lockvar(eap)
3276 exarg_T *eap;
3277{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 int deep = 2;
3280
3281 if (eap->forceit)
3282 deep = -1;
3283 else if (vim_isdigit(*arg))
3284 {
3285 deep = getdigits(&arg);
3286 arg = skipwhite(arg);
3287 }
3288
3289 ex_unletlock(eap, arg, deep);
3290}
3291
3292/*
3293 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3294 */
3295 static void
3296ex_unletlock(eap, argstart, deep)
3297 exarg_T *eap;
3298 char_u *argstart;
3299 int deep;
3300{
3301 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003304 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 do
3307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003309 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3310 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 if (lv.ll_name == NULL)
3312 error = TRUE; /* error but continue parsing */
3313 if (name_end == NULL || (!vim_iswhite(*name_end)
3314 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 if (name_end != NULL)
3317 {
3318 emsg_severe = TRUE;
3319 EMSG(_(e_trailing));
3320 }
3321 if (!(eap->skip || error))
3322 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324 }
3325
3326 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003327 {
3328 if (eap->cmdidx == CMD_unlet)
3329 {
3330 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3331 error = TRUE;
3332 }
3333 else
3334 {
3335 if (do_lock_var(&lv, name_end, deep,
3336 eap->cmdidx == CMD_lockvar) == FAIL)
3337 error = TRUE;
3338 }
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 if (!eap->skip)
3342 clear_lval(&lv);
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 arg = skipwhite(name_end);
3345 } while (!ends_excmd(*arg));
3346
3347 eap->nextcmd = check_nextcmd(arg);
3348}
3349
Bram Moolenaar8c711452005-01-14 21:53:12 +00003350 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003353 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 int forceit;
3355{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003356 int ret = OK;
3357 int cc;
3358
3359 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003361 cc = *name_end;
3362 *name_end = NUL;
3363
3364 /* Normal name or expanded name. */
3365 if (check_changedtick(lp->ll_name))
3366 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003368 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3372 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003373 else if (lp->ll_range)
3374 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003375 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003376
3377 /* Delete a range of List items. */
3378 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3379 {
3380 li = lp->ll_li->li_next;
3381 listitem_remove(lp->ll_list, lp->ll_li);
3382 lp->ll_li = li;
3383 ++lp->ll_n1;
3384 }
3385 }
3386 else
3387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003388 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 /* unlet a List item. */
3390 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003391 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003394 }
3395
3396 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003404do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003406 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar33570922005-01-25 22:26:29 +00003408 hashtab_T *ht;
3409 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003410 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar33570922005-01-25 22:26:29 +00003412 ht = find_var_ht(name, &varname);
3413 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003415 hi = hash_find(ht, varname);
3416 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003417 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003418 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3419 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003420 if (var_check_ro(HI2DI(hi)->di_flags, name))
3421 return FAIL;
3422 delete_var(ht, hi);
3423 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003426 if (forceit)
3427 return OK;
3428 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 return FAIL;
3430}
3431
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003432/*
3433 * Lock or unlock variable indicated by "lp".
3434 * "deep" is the levels to go (-1 for unlimited);
3435 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3436 */
3437 static int
3438do_lock_var(lp, name_end, deep, lock)
3439 lval_T *lp;
3440 char_u *name_end;
3441 int deep;
3442 int lock;
3443{
3444 int ret = OK;
3445 int cc;
3446 dictitem_T *di;
3447
3448 if (deep == 0) /* nothing to do */
3449 return OK;
3450
3451 if (lp->ll_tv == NULL)
3452 {
3453 cc = *name_end;
3454 *name_end = NUL;
3455
3456 /* Normal name or expanded name. */
3457 if (check_changedtick(lp->ll_name))
3458 ret = FAIL;
3459 else
3460 {
3461 di = find_var(lp->ll_name, NULL);
3462 if (di == NULL)
3463 ret = FAIL;
3464 else
3465 {
3466 if (lock)
3467 di->di_flags |= DI_FLAGS_LOCK;
3468 else
3469 di->di_flags &= ~DI_FLAGS_LOCK;
3470 item_lock(&di->di_tv, deep, lock);
3471 }
3472 }
3473 *name_end = cc;
3474 }
3475 else if (lp->ll_range)
3476 {
3477 listitem_T *li = lp->ll_li;
3478
3479 /* (un)lock a range of List items. */
3480 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3481 {
3482 item_lock(&li->li_tv, deep, lock);
3483 li = li->li_next;
3484 ++lp->ll_n1;
3485 }
3486 }
3487 else if (lp->ll_list != NULL)
3488 /* (un)lock a List item. */
3489 item_lock(&lp->ll_li->li_tv, deep, lock);
3490 else
3491 /* un(lock) a Dictionary item. */
3492 item_lock(&lp->ll_di->di_tv, deep, lock);
3493
3494 return ret;
3495}
3496
3497/*
3498 * Lock or unlock an item. "deep" is nr of levels to go.
3499 */
3500 static void
3501item_lock(tv, deep, lock)
3502 typval_T *tv;
3503 int deep;
3504 int lock;
3505{
3506 static int recurse = 0;
3507 list_T *l;
3508 listitem_T *li;
3509 dict_T *d;
3510 hashitem_T *hi;
3511 int todo;
3512
3513 if (recurse >= DICT_MAXNEST)
3514 {
3515 EMSG(_("E743: variable nested too deep for (un)lock"));
3516 return;
3517 }
3518 if (deep == 0)
3519 return;
3520 ++recurse;
3521
3522 /* lock/unlock the item itself */
3523 if (lock)
3524 tv->v_lock |= VAR_LOCKED;
3525 else
3526 tv->v_lock &= ~VAR_LOCKED;
3527
3528 switch (tv->v_type)
3529 {
3530 case VAR_LIST:
3531 if ((l = tv->vval.v_list) != NULL)
3532 {
3533 if (lock)
3534 l->lv_lock |= VAR_LOCKED;
3535 else
3536 l->lv_lock &= ~VAR_LOCKED;
3537 if (deep < 0 || deep > 1)
3538 /* recursive: lock/unlock the items the List contains */
3539 for (li = l->lv_first; li != NULL; li = li->li_next)
3540 item_lock(&li->li_tv, deep - 1, lock);
3541 }
3542 break;
3543 case VAR_DICT:
3544 if ((d = tv->vval.v_dict) != NULL)
3545 {
3546 if (lock)
3547 d->dv_lock |= VAR_LOCKED;
3548 else
3549 d->dv_lock &= ~VAR_LOCKED;
3550 if (deep < 0 || deep > 1)
3551 {
3552 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3555 {
3556 if (!HASHITEM_EMPTY(hi))
3557 {
3558 --todo;
3559 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3560 }
3561 }
3562 }
3563 }
3564 }
3565 --recurse;
3566}
3567
Bram Moolenaara40058a2005-07-11 22:42:07 +00003568/*
3569 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3570 * it refers to a List or Dictionary that is locked.
3571 */
3572 static int
3573tv_islocked(tv)
3574 typval_T *tv;
3575{
3576 return (tv->v_lock & VAR_LOCKED)
3577 || (tv->v_type == VAR_LIST
3578 && tv->vval.v_list != NULL
3579 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3580 || (tv->v_type == VAR_DICT
3581 && tv->vval.v_dict != NULL
3582 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3583}
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3586/*
3587 * Delete all "menutrans_" variables.
3588 */
3589 void
3590del_menutrans_vars()
3591{
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003596 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003598 {
3599 if (!HASHITEM_EMPTY(hi))
3600 {
3601 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3603 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003604 }
3605 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607}
3608#endif
3609
3610#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3611
3612/*
3613 * Local string buffer for the next two functions to store a variable name
3614 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3615 * get_user_var_name().
3616 */
3617
3618static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3619
3620static char_u *varnamebuf = NULL;
3621static int varnamebuflen = 0;
3622
3623/*
3624 * Function to concatenate a prefix and a variable name.
3625 */
3626 static char_u *
3627cat_prefix_varname(prefix, name)
3628 int prefix;
3629 char_u *name;
3630{
3631 int len;
3632
3633 len = (int)STRLEN(name) + 3;
3634 if (len > varnamebuflen)
3635 {
3636 vim_free(varnamebuf);
3637 len += 10; /* some additional space */
3638 varnamebuf = alloc(len);
3639 if (varnamebuf == NULL)
3640 {
3641 varnamebuflen = 0;
3642 return NULL;
3643 }
3644 varnamebuflen = len;
3645 }
3646 *varnamebuf = prefix;
3647 varnamebuf[1] = ':';
3648 STRCPY(varnamebuf + 2, name);
3649 return varnamebuf;
3650}
3651
3652/*
3653 * Function given to ExpandGeneric() to obtain the list of user defined
3654 * (global/buffer/window/built-in) variable names.
3655 */
3656/*ARGSUSED*/
3657 char_u *
3658get_user_var_name(xp, idx)
3659 expand_T *xp;
3660 int idx;
3661{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003662 static long_u gdone;
3663 static long_u bdone;
3664 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003665#ifdef FEAT_WINDOWS
3666 static long_u tdone;
3667#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003668 static int vidx;
3669 static hashitem_T *hi;
3670 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003673 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675#ifdef FEAT_WINDOWS
3676 tdone = 0;
3677#endif
3678 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003679
3680 /* Global variables */
3681 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003685 else
3686 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003687 while (HASHITEM_EMPTY(hi))
3688 ++hi;
3689 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3690 return cat_prefix_varname('g', hi->hi_key);
3691 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003693
3694 /* b: variables */
3695 ht = &curbuf->b_vars.dv_hashtab;
3696 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003698 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003700 else
3701 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003702 while (HASHITEM_EMPTY(hi))
3703 ++hi;
3704 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003708 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return (char_u *)"b:changedtick";
3710 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003711
3712 /* w: variables */
3713 ht = &curwin->w_vars.dv_hashtab;
3714 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003716 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003718 else
3719 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003720 while (HASHITEM_EMPTY(hi))
3721 ++hi;
3722 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003724
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003725#ifdef FEAT_WINDOWS
3726 /* t: variables */
3727 ht = &curtab->tp_vars.dv_hashtab;
3728 if (tdone < ht->ht_used)
3729 {
3730 if (tdone++ == 0)
3731 hi = ht->ht_array;
3732 else
3733 ++hi;
3734 while (HASHITEM_EMPTY(hi))
3735 ++hi;
3736 return cat_prefix_varname('t', hi->hi_key);
3737 }
3738#endif
3739
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 /* v: variables */
3741 if (vidx < VV_LEN)
3742 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 vim_free(varnamebuf);
3745 varnamebuf = NULL;
3746 varnamebuflen = 0;
3747 return NULL;
3748}
3749
3750#endif /* FEAT_CMDL_COMPL */
3751
3752/*
3753 * types for expressions.
3754 */
3755typedef enum
3756{
3757 TYPE_UNKNOWN = 0
3758 , TYPE_EQUAL /* == */
3759 , TYPE_NEQUAL /* != */
3760 , TYPE_GREATER /* > */
3761 , TYPE_GEQUAL /* >= */
3762 , TYPE_SMALLER /* < */
3763 , TYPE_SEQUAL /* <= */
3764 , TYPE_MATCH /* =~ */
3765 , TYPE_NOMATCH /* !~ */
3766} exptype_T;
3767
3768/*
3769 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3772 */
3773
3774/*
3775 * Handle zero level expression.
3776 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003777 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003778 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 * Return OK or FAIL.
3780 */
3781 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u **nextcmd;
3786 int evaluate;
3787{
3788 int ret;
3789 char_u *p;
3790
3791 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003792 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (ret == FAIL || !ends_excmd(*p))
3794 {
3795 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 /*
3798 * Report the invalid expression unless the expression evaluation has
3799 * been cancelled due to an aborting error, an interrupt, or an
3800 * exception.
3801 */
3802 if (!aborting())
3803 EMSG2(_(e_invexpr2), arg);
3804 ret = FAIL;
3805 }
3806 if (nextcmd != NULL)
3807 *nextcmd = check_nextcmd(p);
3808
3809 return ret;
3810}
3811
3812/*
3813 * Handle top level expression:
3814 * expr1 ? expr0 : expr0
3815 *
3816 * "arg" must point to the first non-white of the expression.
3817 * "arg" is advanced to the next non-white after the recognized expression.
3818 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003819 * Note: "rettv.v_lock" is not set.
3820 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 * Return OK or FAIL.
3822 */
3823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 int evaluate;
3828{
3829 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 /*
3833 * Get the first variable.
3834 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837
3838 if ((*arg)[0] == '?')
3839 {
3840 result = FALSE;
3841 if (evaluate)
3842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 int error = FALSE;
3844
3845 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848 if (error)
3849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851
3852 /*
3853 * Get the second variable.
3854 */
3855 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return FAIL;
3858
3859 /*
3860 * Check for the ":".
3861 */
3862 if ((*arg)[0] != ':')
3863 {
3864 EMSG(_("E109: Missing ':' after '?'"));
3865 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return FAIL;
3868 }
3869
3870 /*
3871 * Get the third variable.
3872 */
3873 *arg = skipwhite(*arg + 1);
3874 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3875 {
3876 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 return FAIL;
3879 }
3880 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883
3884 return OK;
3885}
3886
3887/*
3888 * Handle first level expression:
3889 * expr2 || expr2 || expr2 logical OR
3890 *
3891 * "arg" must point to the first non-white of the expression.
3892 * "arg" is advanced to the next non-white after the recognized expression.
3893 *
3894 * Return OK or FAIL.
3895 */
3896 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 int evaluate;
3901{
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 long result;
3904 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 /*
3908 * Get the first variable.
3909 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return FAIL;
3912
3913 /*
3914 * Repeat until there is no following "||".
3915 */
3916 first = TRUE;
3917 result = FALSE;
3918 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3919 {
3920 if (evaluate && first)
3921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003922 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003924 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 if (error)
3926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 first = FALSE;
3928 }
3929
3930 /*
3931 * Get the second variable.
3932 */
3933 *arg = skipwhite(*arg + 2);
3934 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3935 return FAIL;
3936
3937 /*
3938 * Compute the result.
3939 */
3940 if (evaluate && !result)
3941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 if (error)
3946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 if (evaluate)
3949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950 rettv->v_type = VAR_NUMBER;
3951 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 }
3954
3955 return OK;
3956}
3957
3958/*
3959 * Handle second level expression:
3960 * expr3 && expr3 && expr3 logical AND
3961 *
3962 * "arg" must point to the first non-white of the expression.
3963 * "arg" is advanced to the next non-white after the recognized expression.
3964 *
3965 * Return OK or FAIL.
3966 */
3967 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int evaluate;
3972{
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 long result;
3975 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 /*
3979 * Get the first variable.
3980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
3983
3984 /*
3985 * Repeat until there is no following "&&".
3986 */
3987 first = TRUE;
3988 result = TRUE;
3989 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3990 {
3991 if (evaluate && first)
3992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (error)
3997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 first = FALSE;
3999 }
4000
4001 /*
4002 * Get the second variable.
4003 */
4004 *arg = skipwhite(*arg + 2);
4005 if (eval4(arg, &var2, evaluate && result) == FAIL)
4006 return FAIL;
4007
4008 /*
4009 * Compute the result.
4010 */
4011 if (evaluate && result)
4012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 if (error)
4017 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 if (evaluate)
4020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004021 rettv->v_type = VAR_NUMBER;
4022 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 }
4025
4026 return OK;
4027}
4028
4029/*
4030 * Handle third level expression:
4031 * var1 == var2
4032 * var1 =~ var2
4033 * var1 != var2
4034 * var1 !~ var2
4035 * var1 > var2
4036 * var1 >= var2
4037 * var1 < var2
4038 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 * var1 is var2
4040 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 *
4042 * "arg" must point to the first non-white of the expression.
4043 * "arg" is advanced to the next non-white after the recognized expression.
4044 *
4045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 int evaluate;
4052{
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *p;
4055 int i;
4056 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int len = 2;
4059 long n1, n2;
4060 char_u *s1, *s2;
4061 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4062 regmatch_T regmatch;
4063 int ic;
4064 char_u *save_cpo;
4065
4066 /*
4067 * Get the first variable.
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
4071
4072 p = *arg;
4073 switch (p[0])
4074 {
4075 case '=': if (p[1] == '=')
4076 type = TYPE_EQUAL;
4077 else if (p[1] == '~')
4078 type = TYPE_MATCH;
4079 break;
4080 case '!': if (p[1] == '=')
4081 type = TYPE_NEQUAL;
4082 else if (p[1] == '~')
4083 type = TYPE_NOMATCH;
4084 break;
4085 case '>': if (p[1] != '=')
4086 {
4087 type = TYPE_GREATER;
4088 len = 1;
4089 }
4090 else
4091 type = TYPE_GEQUAL;
4092 break;
4093 case '<': if (p[1] != '=')
4094 {
4095 type = TYPE_SMALLER;
4096 len = 1;
4097 }
4098 else
4099 type = TYPE_SEQUAL;
4100 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004101 case 'i': if (p[1] == 's')
4102 {
4103 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4104 len = 5;
4105 if (!vim_isIDc(p[len]))
4106 {
4107 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4108 type_is = TRUE;
4109 }
4110 }
4111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 /*
4115 * If there is a comparitive operator, use it.
4116 */
4117 if (type != TYPE_UNKNOWN)
4118 {
4119 /* extra question mark appended: ignore case */
4120 if (p[len] == '?')
4121 {
4122 ic = TRUE;
4123 ++len;
4124 }
4125 /* extra '#' appended: match case */
4126 else if (p[len] == '#')
4127 {
4128 ic = FALSE;
4129 ++len;
4130 }
4131 /* nothing appened: use 'ignorecase' */
4132 else
4133 ic = p_ic;
4134
4135 /*
4136 * Get the second variable.
4137 */
4138 *arg = skipwhite(p + len);
4139 if (eval5(arg, &var2, evaluate) == FAIL)
4140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143 }
4144
4145 if (evaluate)
4146 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004147 if (type_is && rettv->v_type != var2.v_type)
4148 {
4149 /* For "is" a different type always means FALSE, for "notis"
4150 * it means TRUE. */
4151 n1 = (type == TYPE_NEQUAL);
4152 }
4153 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4154 {
4155 if (type_is)
4156 {
4157 n1 = (rettv->v_type == var2.v_type
4158 && rettv->vval.v_list == var2.vval.v_list);
4159 if (type == TYPE_NEQUAL)
4160 n1 = !n1;
4161 }
4162 else if (rettv->v_type != var2.v_type
4163 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4164 {
4165 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 clear_tv(rettv);
4170 clear_tv(&var2);
4171 return FAIL;
4172 }
4173 else
4174 {
4175 /* Compare two Lists for being equal or unequal. */
4176 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4177 if (type == TYPE_NEQUAL)
4178 n1 = !n1;
4179 }
4180 }
4181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004182 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4183 {
4184 if (type_is)
4185 {
4186 n1 = (rettv->v_type == var2.v_type
4187 && rettv->vval.v_dict == var2.vval.v_dict);
4188 if (type == TYPE_NEQUAL)
4189 n1 = !n1;
4190 }
4191 else if (rettv->v_type != var2.v_type
4192 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4193 {
4194 if (rettv->v_type != var2.v_type)
4195 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4196 else
4197 EMSG(_("E736: Invalid operation for Dictionary"));
4198 clear_tv(rettv);
4199 clear_tv(&var2);
4200 return FAIL;
4201 }
4202 else
4203 {
4204 /* Compare two Dictionaries for being equal or unequal. */
4205 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4206 if (type == TYPE_NEQUAL)
4207 n1 = !n1;
4208 }
4209 }
4210
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004211 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4212 {
4213 if (rettv->v_type != var2.v_type
4214 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4215 {
4216 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004217 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004219 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 clear_tv(rettv);
4221 clear_tv(&var2);
4222 return FAIL;
4223 }
4224 else
4225 {
4226 /* Compare two Funcrefs for being equal or unequal. */
4227 if (rettv->vval.v_string == NULL
4228 || var2.vval.v_string == NULL)
4229 n1 = FALSE;
4230 else
4231 n1 = STRCMP(rettv->vval.v_string,
4232 var2.vval.v_string) == 0;
4233 if (type == TYPE_NEQUAL)
4234 n1 = !n1;
4235 }
4236 }
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 /*
4239 * If one of the two variables is a number, compare as a number.
4240 * When using "=~" or "!~", always compare as string.
4241 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004242 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 n1 = get_tv_number(rettv);
4246 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 switch (type)
4248 {
4249 case TYPE_EQUAL: n1 = (n1 == n2); break;
4250 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4251 case TYPE_GREATER: n1 = (n1 > n2); break;
4252 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4253 case TYPE_SMALLER: n1 = (n1 < n2); break;
4254 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4255 case TYPE_UNKNOWN:
4256 case TYPE_MATCH:
4257 case TYPE_NOMATCH: break; /* avoid gcc warning */
4258 }
4259 }
4260 else
4261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 s1 = get_tv_string_buf(rettv, buf1);
4263 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4265 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4266 else
4267 i = 0;
4268 n1 = FALSE;
4269 switch (type)
4270 {
4271 case TYPE_EQUAL: n1 = (i == 0); break;
4272 case TYPE_NEQUAL: n1 = (i != 0); break;
4273 case TYPE_GREATER: n1 = (i > 0); break;
4274 case TYPE_GEQUAL: n1 = (i >= 0); break;
4275 case TYPE_SMALLER: n1 = (i < 0); break;
4276 case TYPE_SEQUAL: n1 = (i <= 0); break;
4277
4278 case TYPE_MATCH:
4279 case TYPE_NOMATCH:
4280 /* avoid 'l' flag in 'cpoptions' */
4281 save_cpo = p_cpo;
4282 p_cpo = (char_u *)"";
4283 regmatch.regprog = vim_regcomp(s2,
4284 RE_MAGIC + RE_STRING);
4285 regmatch.rm_ic = ic;
4286 if (regmatch.regprog != NULL)
4287 {
4288 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4289 vim_free(regmatch.regprog);
4290 if (type == TYPE_NOMATCH)
4291 n1 = !n1;
4292 }
4293 p_cpo = save_cpo;
4294 break;
4295
4296 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4297 }
4298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(rettv);
4300 clear_tv(&var2);
4301 rettv->v_type = VAR_NUMBER;
4302 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 }
4305
4306 return OK;
4307}
4308
4309/*
4310 * Handle fourth level expression:
4311 * + number addition
4312 * - number subtraction
4313 * . string concatenation
4314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int evaluate;
4325{
Bram Moolenaar33570922005-01-25 22:26:29 +00004326 typval_T var2;
4327 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int op;
4329 long n1, n2;
4330 char_u *s1, *s2;
4331 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4332 char_u *p;
4333
4334 /*
4335 * Get the first variable.
4336 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 return FAIL;
4339
4340 /*
4341 * Repeat computing, until no '+', '-' or '.' is following.
4342 */
4343 for (;;)
4344 {
4345 op = **arg;
4346 if (op != '+' && op != '-' && op != '.')
4347 break;
4348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (op != '+' || rettv->v_type != VAR_LIST)
4350 {
4351 /* For "list + ...", an illegal use of the first operand as
4352 * a number cannot be determined before evaluating the 2nd
4353 * operand: if this is also a list, all is ok.
4354 * For "something . ...", "something - ..." or "non-list + ...",
4355 * we know that the first operand needs to be a string or number
4356 * without evaluating the 2nd operand. So check before to avoid
4357 * side effects after an error. */
4358 if (evaluate && get_tv_string_chk(rettv) == NULL)
4359 {
4360 clear_tv(rettv);
4361 return FAIL;
4362 }
4363 }
4364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /*
4366 * Get the second variable.
4367 */
4368 *arg = skipwhite(*arg + 1);
4369 if (eval6(arg, &var2, evaluate) == FAIL)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
4373 }
4374
4375 if (evaluate)
4376 {
4377 /*
4378 * Compute the result.
4379 */
4380 if (op == '.')
4381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004382 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4383 s2 = get_tv_string_buf_chk(&var2, buf2);
4384 if (s2 == NULL) /* type error ? */
4385 {
4386 clear_tv(rettv);
4387 clear_tv(&var2);
4388 return FAIL;
4389 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004390 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
4392 rettv->v_type = VAR_STRING;
4393 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004395 else if (op == '+' && rettv->v_type == VAR_LIST
4396 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 {
4398 /* concatenate Lists */
4399 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4400 &var3) == FAIL)
4401 {
4402 clear_tv(rettv);
4403 clear_tv(&var2);
4404 return FAIL;
4405 }
4406 clear_tv(rettv);
4407 *rettv = var3;
4408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else
4410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 int error = FALSE;
4412
4413 n1 = get_tv_number_chk(rettv, &error);
4414 if (error)
4415 {
4416 /* This can only happen for "list + non-list".
4417 * For "non-list + ..." or "something - ...", we returned
4418 * before evaluating the 2nd operand. */
4419 clear_tv(rettv);
4420 return FAIL;
4421 }
4422 n2 = get_tv_number_chk(&var2, &error);
4423 if (error)
4424 {
4425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (op == '+')
4431 n1 = n1 + n2;
4432 else
4433 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 rettv->v_type = VAR_NUMBER;
4435 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 }
4440 return OK;
4441}
4442
4443/*
4444 * Handle fifth level expression:
4445 * * number multiplication
4446 * / number division
4447 * % number modulo
4448 *
4449 * "arg" must point to the first non-white of the expression.
4450 * "arg" is advanced to the next non-white after the recognized expression.
4451 *
4452 * Return OK or FAIL.
4453 */
4454 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 int evaluate;
4459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 int op;
4462 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 /*
4466 * Get the first variable.
4467 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
4470
4471 /*
4472 * Repeat computing, until no '*', '/' or '%' is following.
4473 */
4474 for (;;)
4475 {
4476 op = **arg;
4477 if (op != '*' && op != '/' && op != '%')
4478 break;
4479
4480 if (evaluate)
4481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 if (error)
4485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else
4488 n1 = 0;
4489
4490 /*
4491 * Get the second variable.
4492 */
4493 *arg = skipwhite(*arg + 1);
4494 if (eval7(arg, &var2, evaluate) == FAIL)
4495 return FAIL;
4496
4497 if (evaluate)
4498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 if (error)
4502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 /*
4505 * Compute the result.
4506 */
4507 if (op == '*')
4508 n1 = n1 * n2;
4509 else if (op == '/')
4510 {
4511 if (n2 == 0) /* give an error message? */
4512 n1 = 0x7fffffffL;
4513 else
4514 n1 = n1 / n2;
4515 }
4516 else
4517 {
4518 if (n2 == 0) /* give an error message? */
4519 n1 = 0;
4520 else
4521 n1 = n1 % n2;
4522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 rettv->v_type = VAR_NUMBER;
4524 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527
4528 return OK;
4529}
4530
4531/*
4532 * Handle sixth level expression:
4533 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004534 * "string" string constant
4535 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 * &option-name option value
4537 * @r register contents
4538 * identifier variable value
4539 * function() function call
4540 * $VAR environment variable
4541 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004542 * [expr, expr] List
4543 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 *
4545 * Also handle:
4546 * ! in front logical NOT
4547 * - in front unary minus
4548 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 * trailing [] subscript in String or List
4550 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 *
4552 * "arg" must point to the first non-white of the expression.
4553 * "arg" is advanced to the next non-white after the recognized expression.
4554 *
4555 * Return OK or FAIL.
4556 */
4557 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int evaluate;
4562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 long n;
4564 int len;
4565 char_u *s;
4566 int val;
4567 char_u *start_leader, *end_leader;
4568 int ret = OK;
4569 char_u *alias;
4570
4571 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 /*
4578 * Skip '!' and '-' characters. They are handled later.
4579 */
4580 start_leader = *arg;
4581 while (**arg == '!' || **arg == '-' || **arg == '+')
4582 *arg = skipwhite(*arg + 1);
4583 end_leader = *arg;
4584
4585 switch (**arg)
4586 {
4587 /*
4588 * Number constant.
4589 */
4590 case '0':
4591 case '1':
4592 case '2':
4593 case '3':
4594 case '4':
4595 case '5':
4596 case '6':
4597 case '7':
4598 case '8':
4599 case '9':
4600 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4601 *arg += len;
4602 if (evaluate)
4603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 rettv->v_type = VAR_NUMBER;
4605 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607 break;
4608
4609 /*
4610 * String constant: "string".
4611 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614
4615 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 break;
4620
4621 /*
4622 * List: [expr, expr]
4623 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626
4627 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 * Dictionary: {key: val, key: val}
4629 */
4630 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4631 break;
4632
4633 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004634 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004636 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638
4639 /*
4640 * Environment variable: $VAR.
4641 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 break;
4644
4645 /*
4646 * Register contents: @r.
4647 */
4648 case '@': ++*arg;
4649 if (evaluate)
4650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004652 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 if (**arg != NUL)
4655 ++*arg;
4656 break;
4657
4658 /*
4659 * nested expression: (expression).
4660 */
4661 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (**arg == ')')
4664 ++*arg;
4665 else if (ret == OK)
4666 {
4667 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 ret = FAIL;
4670 }
4671 break;
4672
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 break;
4675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676
4677 if (ret == NOTDONE)
4678 {
4679 /*
4680 * Must be a variable or function name.
4681 * Can also be a curly-braces kind of name: {expr}.
4682 */
4683 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004684 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 if (alias != NULL)
4686 s = alias;
4687
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 ret = FAIL;
4690 else
4691 {
4692 if (**arg == '(') /* recursive! */
4693 {
4694 /* If "s" is the name of a variable of type VAR_FUNC
4695 * use its contents. */
4696 s = deref_func_name(s, &len);
4697
4698 /* Invoke the function. */
4699 ret = get_func_tv(s, len, rettv, arg,
4700 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004701 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 /* Stop the expression evaluation when immediately
4703 * aborting on error, or when an interrupt occurred or
4704 * an exception was thrown but not caught. */
4705 if (aborting())
4706 {
4707 if (ret == OK)
4708 clear_tv(rettv);
4709 ret = FAIL;
4710 }
4711 }
4712 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004713 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004714 else
4715 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 }
4717
4718 if (alias != NULL)
4719 vim_free(alias);
4720 }
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 *arg = skipwhite(*arg);
4723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4725 * expr(expr). */
4726 if (ret == OK)
4727 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
4730 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4731 */
4732 if (ret == OK && evaluate && end_leader > start_leader)
4733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 int error = FALSE;
4735
4736 val = get_tv_number_chk(rettv, &error);
4737 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 clear_tv(rettv);
4740 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 else
4743 {
4744 while (end_leader > start_leader)
4745 {
4746 --end_leader;
4747 if (*end_leader == '!')
4748 val = !val;
4749 else if (*end_leader == '-')
4750 val = -val;
4751 }
4752 clear_tv(rettv);
4753 rettv->v_type = VAR_NUMBER;
4754 rettv->vval.v_number = val;
4755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757
4758 return ret;
4759}
4760
4761/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004762 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4763 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4765 */
4766 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004769 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004771 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772{
4773 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 long len = -1;
4777 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004783 if (verbose)
4784 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 return FAIL;
4786 }
4787
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 /*
4791 * dict.name
4792 */
4793 key = *arg + 1;
4794 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4795 ;
4796 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 }
4800 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 /*
4803 * something[idx]
4804 *
4805 * Get the (first) variable from inside the [].
4806 */
4807 *arg = skipwhite(*arg + 1);
4808 if (**arg == ':')
4809 empty1 = TRUE;
4810 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4811 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4813 {
4814 /* not a number or string */
4815 clear_tv(&var1);
4816 return FAIL;
4817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818
4819 /*
4820 * Get the second variable from inside the [:].
4821 */
4822 if (**arg == ':')
4823 {
4824 range = TRUE;
4825 *arg = skipwhite(*arg + 1);
4826 if (**arg == ']')
4827 empty2 = TRUE;
4828 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (!empty1)
4831 clear_tv(&var1);
4832 return FAIL;
4833 }
4834 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4835 {
4836 /* not a number or string */
4837 if (!empty1)
4838 clear_tv(&var1);
4839 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 return FAIL;
4841 }
4842 }
4843
4844 /* Check for the ']'. */
4845 if (**arg != ']')
4846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004847 if (verbose)
4848 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 clear_tv(&var1);
4850 if (range)
4851 clear_tv(&var2);
4852 return FAIL;
4853 }
4854 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 }
4856
4857 if (evaluate)
4858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 n1 = 0;
4860 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 n1 = get_tv_number(&var1);
4863 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 }
4865 if (range)
4866 {
4867 if (empty2)
4868 n2 = -1;
4869 else
4870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 n2 = get_tv_number(&var2);
4872 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 }
4874 }
4875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 {
4878 case VAR_NUMBER:
4879 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 len = (long)STRLEN(s);
4882 if (range)
4883 {
4884 /* The resulting variable is a substring. If the indexes
4885 * are out of range the result is empty. */
4886 if (n1 < 0)
4887 {
4888 n1 = len + n1;
4889 if (n1 < 0)
4890 n1 = 0;
4891 }
4892 if (n2 < 0)
4893 n2 = len + n2;
4894 else if (n2 >= len)
4895 n2 = len;
4896 if (n1 >= len || n2 < 0 || n1 > n2)
4897 s = NULL;
4898 else
4899 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4900 }
4901 else
4902 {
4903 /* The resulting variable is a string of a single
4904 * character. If the index is too big or negative the
4905 * result is empty. */
4906 if (n1 >= len || n1 < 0)
4907 s = NULL;
4908 else
4909 s = vim_strnsave(s + n1, 1);
4910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
4912 rettv->v_type = VAR_STRING;
4913 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 break;
4915
4916 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 if (n1 < 0)
4919 n1 = len + n1;
4920 if (!empty1 && (n1 < 0 || n1 >= len))
4921 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004922 /* For a range we allow invalid values and return an empty
4923 * list. A list index out of range is an error. */
4924 if (!range)
4925 {
4926 if (verbose)
4927 EMSGN(_(e_listidx), n1);
4928 return FAIL;
4929 }
4930 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004931 }
4932 if (range)
4933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 list_T *l;
4935 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004936
4937 if (n2 < 0)
4938 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004939 else if (n2 >= len)
4940 n2 = len - 1;
4941 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004942 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 l = list_alloc();
4944 if (l == NULL)
4945 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 n1 <= n2; ++n1)
4948 {
4949 if (list_append_tv(l, &item->li_tv) == FAIL)
4950 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 return FAIL;
4953 }
4954 item = item->li_next;
4955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 clear_tv(rettv);
4957 rettv->v_type = VAR_LIST;
4958 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004959 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 }
4961 else
4962 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004963 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
4965 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 }
4967 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968
4969 case VAR_DICT:
4970 if (range)
4971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004972 if (verbose)
4973 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 if (len == -1)
4975 clear_tv(&var1);
4976 return FAIL;
4977 }
4978 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004979 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980
4981 if (len == -1)
4982 {
4983 key = get_tv_string(&var1);
4984 if (*key == NUL)
4985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004986 if (verbose)
4987 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 clear_tv(&var1);
4989 return FAIL;
4990 }
4991 }
4992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004993 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004996 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 if (len == -1)
4998 clear_tv(&var1);
4999 if (item == NULL)
5000 return FAIL;
5001
5002 copy_tv(&item->di_tv, &var1);
5003 clear_tv(rettv);
5004 *rettv = var1;
5005 }
5006 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007 }
5008 }
5009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 return OK;
5011}
5012
5013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * Get an option value.
5015 * "arg" points to the '&' or '+' before the option name.
5016 * "arg" is advanced to character after the option name.
5017 * Return OK or FAIL.
5018 */
5019 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005020get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005022 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 int evaluate;
5024{
5025 char_u *option_end;
5026 long numval;
5027 char_u *stringval;
5028 int opt_type;
5029 int c;
5030 int working = (**arg == '+'); /* has("+option") */
5031 int ret = OK;
5032 int opt_flags;
5033
5034 /*
5035 * Isolate the option name and find its value.
5036 */
5037 option_end = find_option_end(arg, &opt_flags);
5038 if (option_end == NULL)
5039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 EMSG2(_("E112: Option name missing: %s"), *arg);
5042 return FAIL;
5043 }
5044
5045 if (!evaluate)
5046 {
5047 *arg = option_end;
5048 return OK;
5049 }
5050
5051 c = *option_end;
5052 *option_end = NUL;
5053 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 if (opt_type == -3) /* invalid name */
5057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005058 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 EMSG2(_("E113: Unknown option: %s"), *arg);
5060 ret = FAIL;
5061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 if (opt_type == -2) /* hidden string option */
5065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 rettv->v_type = VAR_STRING;
5067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 else if (opt_type == -1) /* hidden number option */
5070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_NUMBER;
5072 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 else if (opt_type == 1) /* number option */
5075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 rettv->v_type = VAR_NUMBER;
5077 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else /* string option */
5080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 rettv->v_type = VAR_STRING;
5082 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 }
5085 else if (working && (opt_type == -2 || opt_type == -1))
5086 ret = FAIL;
5087
5088 *option_end = c; /* put back for error messages */
5089 *arg = option_end;
5090
5091 return ret;
5092}
5093
5094/*
5095 * Allocate a variable for a string constant.
5096 * Return OK or FAIL.
5097 */
5098 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 int evaluate;
5103{
5104 char_u *p;
5105 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 int extra = 0;
5107
5108 /*
5109 * Find the end of the string, skipping backslashed characters.
5110 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 if (*p == '\\' && p[1] != NUL)
5114 {
5115 ++p;
5116 /* A "\<x>" form occupies at least 4 characters, and produces up
5117 * to 6 characters: reserve space for 2 extra */
5118 if (*p == '<')
5119 extra += 2;
5120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122
5123 if (*p != '"')
5124 {
5125 EMSG2(_("E114: Missing quote: %s"), *arg);
5126 return FAIL;
5127 }
5128
5129 /* If only parsing, set *arg and return here */
5130 if (!evaluate)
5131 {
5132 *arg = p + 1;
5133 return OK;
5134 }
5135
5136 /*
5137 * Copy the string into allocated memory, handling backslashed
5138 * characters.
5139 */
5140 name = alloc((unsigned)(p - *arg + extra));
5141 if (name == NULL)
5142 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 rettv->v_type = VAR_STRING;
5144 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 if (*p == '\\')
5149 {
5150 switch (*++p)
5151 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 case 'b': *name++ = BS; ++p; break;
5153 case 'e': *name++ = ESC; ++p; break;
5154 case 'f': *name++ = FF; ++p; break;
5155 case 'n': *name++ = NL; ++p; break;
5156 case 'r': *name++ = CAR; ++p; break;
5157 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159 case 'X': /* hex: "\x1", "\x12" */
5160 case 'x':
5161 case 'u': /* Unicode: "\u0023" */
5162 case 'U':
5163 if (vim_isxdigit(p[1]))
5164 {
5165 int n, nr;
5166 int c = toupper(*p);
5167
5168 if (c == 'X')
5169 n = 2;
5170 else
5171 n = 4;
5172 nr = 0;
5173 while (--n >= 0 && vim_isxdigit(p[1]))
5174 {
5175 ++p;
5176 nr = (nr << 4) + hex2nr(*p);
5177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_MBYTE
5180 /* For "\u" store the number according to
5181 * 'encoding'. */
5182 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else
5185#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /* octal: "\1", "\12", "\123" */
5191 case '0':
5192 case '1':
5193 case '2':
5194 case '3':
5195 case '4':
5196 case '5':
5197 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 case '7': *name = *p++ - '0';
5199 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 *name = (*name << 3) + *p++ - '0';
5202 if (*p >= '0' && *p <= '7')
5203 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207
5208 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (extra != 0)
5211 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
5215 /* FALLTHROUGH */
5216
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219 }
5220 }
5221 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 *arg = p + 1;
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 return OK;
5229}
5230
5231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 * Return OK or FAIL.
5234 */
5235 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 int evaluate;
5240{
5241 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 int reduce = 0;
5244
5245 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005247 */
5248 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005253 break;
5254 ++reduce;
5255 ++p;
5256 }
5257 }
5258
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 return FAIL;
5263 }
5264
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005266 if (!evaluate)
5267 {
5268 *arg = p + 1;
5269 return OK;
5270 }
5271
5272 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005274 */
5275 str = alloc((unsigned)((p - *arg) - reduce));
5276 if (str == NULL)
5277 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 rettv->v_type = VAR_STRING;
5279 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005286 break;
5287 ++p;
5288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 *arg = p + 1;
5293
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005294 return OK;
5295}
5296
5297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 * Allocate a variable for a List and fill it from "*arg".
5299 * Return OK or FAIL.
5300 */
5301 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005302get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005304 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 int evaluate;
5306{
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 list_T *l = NULL;
5308 typval_T tv;
5309 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
5311 if (evaluate)
5312 {
5313 l = list_alloc();
5314 if (l == NULL)
5315 return FAIL;
5316 }
5317
5318 *arg = skipwhite(*arg + 1);
5319 while (**arg != ']' && **arg != NUL)
5320 {
5321 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5322 goto failret;
5323 if (evaluate)
5324 {
5325 item = listitem_alloc();
5326 if (item != NULL)
5327 {
5328 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005329 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 list_append(l, item);
5331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005332 else
5333 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 }
5335
5336 if (**arg == ']')
5337 break;
5338 if (**arg != ',')
5339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 goto failret;
5342 }
5343 *arg = skipwhite(*arg + 1);
5344 }
5345
5346 if (**arg != ']')
5347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349failret:
5350 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005351 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 return FAIL;
5353 }
5354
5355 *arg = skipwhite(*arg + 1);
5356 if (evaluate)
5357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 rettv->v_type = VAR_LIST;
5359 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 ++l->lv_refcount;
5361 }
5362
5363 return OK;
5364}
5365
5366/*
5367 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005368 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005370 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371list_alloc()
5372{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005373 list_T *l;
5374
5375 l = (list_T *)alloc_clear(sizeof(list_T));
5376 if (l != NULL)
5377 {
5378 /* Prepend the list to the list of lists for garbage collection. */
5379 if (first_list != NULL)
5380 first_list->lv_used_prev = l;
5381 l->lv_used_prev = NULL;
5382 l->lv_used_next = first_list;
5383 first_list = l;
5384 }
5385 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386}
5387
5388/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005389 * Allocate an empty list for a return value.
5390 * Returns OK or FAIL.
5391 */
5392 static int
5393rettv_list_alloc(rettv)
5394 typval_T *rettv;
5395{
5396 list_T *l = list_alloc();
5397
5398 if (l == NULL)
5399 return FAIL;
5400
5401 rettv->vval.v_list = l;
5402 rettv->v_type = VAR_LIST;
5403 ++l->lv_refcount;
5404 return OK;
5405}
5406
5407/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 * Unreference a list: decrement the reference count and free it when it
5409 * becomes zero.
5410 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005411 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005415 if (l != NULL && --l->lv_refcount <= 0)
5416 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417}
5418
5419/*
5420 * Free a list, including all items it points to.
5421 * Ignores the reference count.
5422 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005423 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005424list_free(l, recurse)
5425 list_T *l;
5426 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427{
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430 /* Remove the list from the list of lists for garbage collection. */
5431 if (l->lv_used_prev == NULL)
5432 first_list = l->lv_used_next;
5433 else
5434 l->lv_used_prev->lv_used_next = l->lv_used_next;
5435 if (l->lv_used_next != NULL)
5436 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5437
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005440 /* Remove the item before deleting it. */
5441 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005442 if (recurse || (item->li_tv.v_type != VAR_LIST
5443 && item->li_tv.v_type != VAR_DICT))
5444 clear_tv(&item->li_tv);
5445 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447 vim_free(l);
5448}
5449
5450/*
5451 * Allocate a list item.
5452 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454listitem_alloc()
5455{
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457}
5458
5459/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005460 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 */
5462 static void
5463listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 vim_free(item);
5468}
5469
5470/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005471 * Remove a list item from a List and free it. Also clears the value.
5472 */
5473 static void
5474listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 list_T *l;
5476 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005477{
5478 list_remove(l, item, item);
5479 listitem_free(item);
5480}
5481
5482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 * Get the number of items in a list.
5484 */
5485 static long
5486list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 if (l == NULL)
5490 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005491 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492}
5493
5494/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005495 * Return TRUE when two lists have exactly the same values.
5496 */
5497 static int
5498list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 list_T *l1;
5500 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005501 int ic; /* ignore case for strings */
5502{
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005505 if (l1 == l2)
5506 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005507 if (list_len(l1) != list_len(l2))
5508 return FALSE;
5509
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 for (item1 = l1->lv_first, item2 = l2->lv_first;
5511 item1 != NULL && item2 != NULL;
5512 item1 = item1->li_next, item2 = item2->li_next)
5513 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5514 return FALSE;
5515 return item1 == NULL && item2 == NULL;
5516}
5517
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005518#if defined(FEAT_PYTHON) || defined(PROTO)
5519/*
5520 * Return the dictitem that an entry in a hashtable points to.
5521 */
5522 dictitem_T *
5523dict_lookup(hi)
5524 hashitem_T *hi;
5525{
5526 return HI2DI(hi);
5527}
5528#endif
5529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005530/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005531 * Return TRUE when two dictionaries have exactly the same key/values.
5532 */
5533 static int
5534dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 dict_T *d1;
5536 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005537 int ic; /* ignore case for strings */
5538{
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 hashitem_T *hi;
5540 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005541 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005542
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005543 if (d1 == d2)
5544 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005545 if (dict_len(d1) != dict_len(d2))
5546 return FALSE;
5547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005548 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 if (!HASHITEM_EMPTY(hi))
5552 {
5553 item2 = dict_find(d2, hi->hi_key, -1);
5554 if (item2 == NULL)
5555 return FALSE;
5556 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5557 return FALSE;
5558 --todo;
5559 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005560 }
5561 return TRUE;
5562}
5563
5564/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005566 * Compares the items just like "==" would compare them, but strings and
5567 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005568 */
5569 static int
5570tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005571 typval_T *tv1;
5572 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573 int ic; /* ignore case */
5574{
5575 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005577 static int recursive = 0; /* cach recursive loops */
5578 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005580 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005581 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005582 /* Catch lists and dicts that have an endless loop by limiting
5583 * recursiveness to 1000. We guess they are equal then. */
5584 if (recursive >= 1000)
5585 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005586
5587 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005589 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005590 ++recursive;
5591 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5592 --recursive;
5593 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005594
5595 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005596 ++recursive;
5597 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5598 --recursive;
5599 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005600
5601 case VAR_FUNC:
5602 return (tv1->vval.v_string != NULL
5603 && tv2->vval.v_string != NULL
5604 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5605
5606 case VAR_NUMBER:
5607 return tv1->vval.v_number == tv2->vval.v_number;
5608
5609 case VAR_STRING:
5610 s1 = get_tv_string_buf(tv1, buf1);
5611 s2 = get_tv_string_buf(tv2, buf2);
5612 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005614
5615 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005616 return TRUE;
5617}
5618
5619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 * Locate item with index "n" in list "l" and return it.
5621 * A negative index is counted from the end; -1 is the last item.
5622 * Returns NULL when "n" is out of range.
5623 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 long n;
5628{
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 long idx;
5631
5632 if (l == NULL)
5633 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634
5635 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 n = l->lv_len + n;
5638
5639 /* Check for index out of range. */
5640 if (n < 0 || n >= l->lv_len)
5641 return NULL;
5642
5643 /* When there is a cached index may start search from there. */
5644 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005646 if (n < l->lv_idx / 2)
5647 {
5648 /* closest to the start of the list */
5649 item = l->lv_first;
5650 idx = 0;
5651 }
5652 else if (n > (l->lv_idx + l->lv_len) / 2)
5653 {
5654 /* closest to the end of the list */
5655 item = l->lv_last;
5656 idx = l->lv_len - 1;
5657 }
5658 else
5659 {
5660 /* closest to the cached index */
5661 item = l->lv_idx_item;
5662 idx = l->lv_idx;
5663 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 }
5665 else
5666 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005667 if (n < l->lv_len / 2)
5668 {
5669 /* closest to the start of the list */
5670 item = l->lv_first;
5671 idx = 0;
5672 }
5673 else
5674 {
5675 /* closest to the end of the list */
5676 item = l->lv_last;
5677 idx = l->lv_len - 1;
5678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005680
5681 while (n > idx)
5682 {
5683 /* search forward */
5684 item = item->li_next;
5685 ++idx;
5686 }
5687 while (n < idx)
5688 {
5689 /* search backward */
5690 item = item->li_prev;
5691 --idx;
5692 }
5693
5694 /* cache the used index */
5695 l->lv_idx = idx;
5696 l->lv_idx_item = item;
5697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 return item;
5699}
5700
5701/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005702 * Get list item "l[idx]" as a number.
5703 */
5704 static long
5705list_find_nr(l, idx, errorp)
5706 list_T *l;
5707 long idx;
5708 int *errorp; /* set to TRUE when something wrong */
5709{
5710 listitem_T *li;
5711
5712 li = list_find(l, idx);
5713 if (li == NULL)
5714 {
5715 if (errorp != NULL)
5716 *errorp = TRUE;
5717 return -1L;
5718 }
5719 return get_tv_number_chk(&li->li_tv, errorp);
5720}
5721
5722/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005723 * Locate "item" list "l" and return its index.
5724 * Returns -1 when "item" is not in the list.
5725 */
5726 static long
5727list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *l;
5729 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005730{
5731 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005733
5734 if (l == NULL)
5735 return -1;
5736 idx = 0;
5737 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5738 ++idx;
5739 if (li == NULL)
5740 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005741 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005742}
5743
5744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 * Append item "item" to the end of list "l".
5746 */
5747 static void
5748list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
5750 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751{
5752 if (l->lv_last == NULL)
5753 {
5754 /* empty list */
5755 l->lv_first = item;
5756 l->lv_last = item;
5757 item->li_prev = NULL;
5758 }
5759 else
5760 {
5761 l->lv_last->li_next = item;
5762 item->li_prev = l->lv_last;
5763 l->lv_last = item;
5764 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005765 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 item->li_next = NULL;
5767}
5768
5769/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772 */
5773 static int
5774list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005775 list_T *l;
5776 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005782 copy_tv(tv, &li->li_tv);
5783 list_append(l, li);
5784 return OK;
5785}
5786
5787/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005788 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005789 * Return FAIL when out of memory.
5790 */
5791 int
5792list_append_dict(list, dict)
5793 list_T *list;
5794 dict_T *dict;
5795{
5796 listitem_T *li = listitem_alloc();
5797
5798 if (li == NULL)
5799 return FAIL;
5800 li->li_tv.v_type = VAR_DICT;
5801 li->li_tv.v_lock = 0;
5802 li->li_tv.vval.v_dict = dict;
5803 list_append(list, li);
5804 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 return OK;
5806}
5807
5808/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005809 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005810 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005811 * Returns FAIL when out of memory.
5812 */
5813 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005814list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005815 list_T *l;
5816 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005817 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005818{
5819 listitem_T *li = listitem_alloc();
5820
5821 if (li == NULL)
5822 return FAIL;
5823 list_append(l, li);
5824 li->li_tv.v_type = VAR_STRING;
5825 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005826 if (str == NULL)
5827 li->li_tv.vval.v_string = NULL;
5828 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005829 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005830 return FAIL;
5831 return OK;
5832}
5833
5834/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005835 * Append "n" to list "l".
5836 * Returns FAIL when out of memory.
5837 */
5838 static int
5839list_append_number(l, n)
5840 list_T *l;
5841 varnumber_T n;
5842{
5843 listitem_T *li;
5844
5845 li = listitem_alloc();
5846 if (li == NULL)
5847 return FAIL;
5848 li->li_tv.v_type = VAR_NUMBER;
5849 li->li_tv.v_lock = 0;
5850 li->li_tv.vval.v_number = n;
5851 list_append(l, li);
5852 return OK;
5853}
5854
5855/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005857 * If "item" is NULL append at the end.
5858 * Return FAIL when out of memory.
5859 */
5860 static int
5861list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 list_T *l;
5863 typval_T *tv;
5864 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865{
Bram Moolenaar33570922005-01-25 22:26:29 +00005866 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867
5868 if (ni == NULL)
5869 return FAIL;
5870 copy_tv(tv, &ni->li_tv);
5871 if (item == NULL)
5872 /* Append new item at end of list. */
5873 list_append(l, ni);
5874 else
5875 {
5876 /* Insert new item before existing item. */
5877 ni->li_prev = item->li_prev;
5878 ni->li_next = item;
5879 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 ++l->lv_idx;
5883 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005886 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005887 l->lv_idx_item = NULL;
5888 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005890 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 }
5892 return OK;
5893}
5894
5895/*
5896 * Extend "l1" with "l2".
5897 * If "bef" is NULL append at the end, otherwise insert before this item.
5898 * Returns FAIL when out of memory.
5899 */
5900 static int
5901list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *l1;
5903 list_T *l2;
5904 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907
5908 for (item = l2->lv_first; item != NULL; item = item->li_next)
5909 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5910 return FAIL;
5911 return OK;
5912}
5913
5914/*
5915 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5916 * Return FAIL when out of memory.
5917 */
5918 static int
5919list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *l1;
5921 list_T *l2;
5922 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925
5926 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 if (l == NULL)
5929 return FAIL;
5930 tv->v_type = VAR_LIST;
5931 tv->vval.v_list = l;
5932
5933 /* append all items from the second list */
5934 return list_extend(l, l2, NULL);
5935}
5936
5937/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 * Returns NULL when out of memory.
5942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005944list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005947 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 list_T *copy;
5950 listitem_T *item;
5951 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
5953 if (orig == NULL)
5954 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955
5956 copy = list_alloc();
5957 if (copy != NULL)
5958 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005959 if (copyID != 0)
5960 {
5961 /* Do this before adding the items, because one of the items may
5962 * refer back to this list. */
5963 orig->lv_copyID = copyID;
5964 orig->lv_copylist = copy;
5965 }
5966 for (item = orig->lv_first; item != NULL && !got_int;
5967 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 {
5969 ni = listitem_alloc();
5970 if (ni == NULL)
5971 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005973 {
5974 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5975 {
5976 vim_free(ni);
5977 break;
5978 }
5979 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 list_append(copy, ni);
5983 }
5984 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005985 if (item != NULL)
5986 {
5987 list_unref(copy);
5988 copy = NULL;
5989 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 }
5991
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 return copy;
5993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l;
6002 listitem_T *item;
6003 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 /* notify watchers */
6008 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006010 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 list_fix_watch(l, ip);
6012 if (ip == item2)
6013 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015
6016 if (item2->li_next == NULL)
6017 l->lv_last = item->li_prev;
6018 else
6019 item2->li_next->li_prev = item->li_prev;
6020 if (item->li_prev == NULL)
6021 l->lv_first = item2->li_next;
6022 else
6023 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006024 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025}
6026
6027/*
6028 * Return an allocated string with the string representation of a list.
6029 * May return NULL.
6030 */
6031 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006034 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
6036 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037
6038 if (tv->vval.v_list == NULL)
6039 return NULL;
6040 ga_init2(&ga, (int)sizeof(char), 80);
6041 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006042 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006043 {
6044 vim_free(ga.ga_data);
6045 return NULL;
6046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 ga_append(&ga, ']');
6048 ga_append(&ga, NUL);
6049 return (char_u *)ga.ga_data;
6050}
6051
6052/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006053 * Join list "l" into a string in "*gap", using separator "sep".
6054 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006058list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006061 char_u *sep;
6062 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006063 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064{
6065 int first = TRUE;
6066 char_u *tofree;
6067 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069 char_u *s;
6070
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006071 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072 {
6073 if (first)
6074 first = FALSE;
6075 else
6076 ga_concat(gap, sep);
6077
6078 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006082 if (s != NULL)
6083 ga_concat(gap, s);
6084 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006085 if (s == NULL)
6086 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006088 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006089}
6090
6091/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 * Garbage collection for lists and dictionaries.
6093 *
6094 * We use reference counts to be able to free most items right away when they
6095 * are no longer used. But for composite items it's possible that it becomes
6096 * unused while the reference count is > 0: When there is a recursive
6097 * reference. Example:
6098 * :let l = [1, 2, 3]
6099 * :let d = {9: l}
6100 * :let l[1] = d
6101 *
6102 * Since this is quite unusual we handle this with garbage collection: every
6103 * once in a while find out which lists and dicts are not referenced from any
6104 * variable.
6105 *
6106 * Here is a good reference text about garbage collection (refers to Python
6107 * but it applies to all reference-counting mechanisms):
6108 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110
6111/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 * Do garbage collection for lists and dicts.
6113 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 int
6116garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006117{
6118 dict_T *dd;
6119 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120 int copyID = ++current_copyID;
6121 buf_T *buf;
6122 win_T *wp;
6123 int i;
6124 funccall_T *fc;
6125 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006126#ifdef FEAT_WINDOWS
6127 tabpage_T *tp;
6128#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006129
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006130 /* Only do this once. */
6131 want_garbage_collect = FALSE;
6132 may_garbage_collect = FALSE;
6133
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006134 /*
6135 * 1. Go through all accessible variables and mark all lists and dicts
6136 * with copyID.
6137 */
6138 /* script-local variables */
6139 for (i = 1; i <= ga_scripts.ga_len; ++i)
6140 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6141
6142 /* buffer-local variables */
6143 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6144 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6145
6146 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006147 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6149
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006150#ifdef FEAT_WINDOWS
6151 /* tabpage-local variables */
6152 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6153 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6154#endif
6155
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 /* global variables */
6157 set_ref_in_ht(&globvarht, copyID);
6158
6159 /* function-local variables */
6160 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6161 {
6162 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6163 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6164 }
6165
6166 /*
6167 * 2. Go through the list of dicts and free items without the copyID.
6168 */
6169 for (dd = first_dict; dd != NULL; )
6170 if (dd->dv_copyID != copyID)
6171 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006172 /* Free the Dictionary and ordinary items it contains, but don't
6173 * recurse into Lists and Dictionaries, they will be in the list
6174 * of dicts or list of lists. */
6175 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006176 did_free = TRUE;
6177
6178 /* restart, next dict may also have been freed */
6179 dd = first_dict;
6180 }
6181 else
6182 dd = dd->dv_used_next;
6183
6184 /*
6185 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006186 * But don't free a list that has a watcher (used in a for loop), these
6187 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 */
6189 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006190 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006192 /* Free the List and ordinary items it contains, but don't recurse
6193 * into Lists and Dictionaries, they will be in the list of dicts
6194 * or list of lists. */
6195 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 did_free = TRUE;
6197
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006198 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 ll = first_list;
6200 }
6201 else
6202 ll = ll->lv_used_next;
6203
6204 return did_free;
6205}
6206
6207/*
6208 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6209 */
6210 static void
6211set_ref_in_ht(ht, copyID)
6212 hashtab_T *ht;
6213 int copyID;
6214{
6215 int todo;
6216 hashitem_T *hi;
6217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006218 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006219 for (hi = ht->ht_array; todo > 0; ++hi)
6220 if (!HASHITEM_EMPTY(hi))
6221 {
6222 --todo;
6223 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6224 }
6225}
6226
6227/*
6228 * Mark all lists and dicts referenced through list "l" with "copyID".
6229 */
6230 static void
6231set_ref_in_list(l, copyID)
6232 list_T *l;
6233 int copyID;
6234{
6235 listitem_T *li;
6236
6237 for (li = l->lv_first; li != NULL; li = li->li_next)
6238 set_ref_in_item(&li->li_tv, copyID);
6239}
6240
6241/*
6242 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6243 */
6244 static void
6245set_ref_in_item(tv, copyID)
6246 typval_T *tv;
6247 int copyID;
6248{
6249 dict_T *dd;
6250 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006251
6252 switch (tv->v_type)
6253 {
6254 case VAR_DICT:
6255 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006256 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006257 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006258 /* Didn't see this dict yet. */
6259 dd->dv_copyID = copyID;
6260 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006261 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006262 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006263
6264 case VAR_LIST:
6265 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006266 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006267 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006268 /* Didn't see this list yet. */
6269 ll->lv_copyID = copyID;
6270 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006271 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006274 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275}
6276
6277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006278 * Allocate an empty header for a dictionary.
6279 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006280 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281dict_alloc()
6282{
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006287 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006288 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006289 if (first_dict != NULL)
6290 first_dict->dv_used_prev = d;
6291 d->dv_used_next = first_dict;
6292 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006293 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006294
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006296 d->dv_lock = 0;
6297 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006298 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006301}
6302
6303/*
6304 * Unreference a Dictionary: decrement the reference count and free it when it
6305 * becomes zero.
6306 */
6307 static void
6308dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006311 if (d != NULL && --d->dv_refcount <= 0)
6312 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313}
6314
6315/*
6316 * Free a Dictionary, including all items it contains.
6317 * Ignores the reference count.
6318 */
6319 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006320dict_free(d, recurse)
6321 dict_T *d;
6322 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006326 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006328 /* Remove the dict from the list of dicts for garbage collection. */
6329 if (d->dv_used_prev == NULL)
6330 first_dict = d->dv_used_next;
6331 else
6332 d->dv_used_prev->dv_used_next = d->dv_used_next;
6333 if (d->dv_used_next != NULL)
6334 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6335
6336 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006337 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006338 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 if (!HASHITEM_EMPTY(hi))
6342 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006343 /* Remove the item before deleting it, just in case there is
6344 * something recursive causing trouble. */
6345 di = HI2DI(hi);
6346 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006347 if (recurse || (di->di_tv.v_type != VAR_LIST
6348 && di->di_tv.v_type != VAR_DICT))
6349 clear_tv(&di->di_tv);
6350 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 --todo;
6352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 vim_free(d);
6356}
6357
6358/*
6359 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006360 * The "key" is copied to the new item.
6361 * Note that the value of the item "di_tv" still needs to be initialized!
6362 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006363 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365dictitem_alloc(key)
6366 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367{
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006370 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006374 di->di_flags = 0;
6375 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377}
6378
6379/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 * Make a copy of a Dictionary item.
6381 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385{
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006388 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6389 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 if (di != NULL)
6391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006393 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 copy_tv(&org->di_tv, &di->di_tv);
6395 }
6396 return di;
6397}
6398
6399/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 * Remove item "item" from Dictionary "dict" and free it.
6401 */
6402 static void
6403dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 dict_T *dict;
6405 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406{
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410 if (HASHITEM_EMPTY(hi))
6411 EMSG2(_(e_intern2), "dictitem_remove()");
6412 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 dictitem_free(item);
6415}
6416
6417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418 * Free a dict item. Also clears the value.
6419 */
6420 static void
6421dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 clear_tv(&item->di_tv);
6425 vim_free(item);
6426}
6427
6428/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006429 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6430 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006431 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 * Returns NULL when out of memory.
6433 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 dict_T *copy;
6441 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006442 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006444
6445 if (orig == NULL)
6446 return NULL;
6447
6448 copy = dict_alloc();
6449 if (copy != NULL)
6450 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 if (copyID != 0)
6452 {
6453 orig->dv_copyID = copyID;
6454 orig->dv_copydict = copy;
6455 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006456 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006457 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 --todo;
6462
6463 di = dictitem_alloc(hi->hi_key);
6464 if (di == NULL)
6465 break;
6466 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 {
6468 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6469 copyID) == FAIL)
6470 {
6471 vim_free(di);
6472 break;
6473 }
6474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475 else
6476 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6477 if (dict_add(copy, di) == FAIL)
6478 {
6479 dictitem_free(di);
6480 break;
6481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006484
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (todo > 0)
6487 {
6488 dict_unref(copy);
6489 copy = NULL;
6490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 }
6492
6493 return copy;
6494}
6495
6496/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 dict_T *d;
6503 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504{
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506}
6507
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006509 * Add a number or string entry to dictionary "d".
6510 * When "str" is NULL use number "nr", otherwise use "str".
6511 * Returns FAIL when out of memory and when key already exists.
6512 */
6513 int
6514dict_add_nr_str(d, key, nr, str)
6515 dict_T *d;
6516 char *key;
6517 long nr;
6518 char_u *str;
6519{
6520 dictitem_T *item;
6521
6522 item = dictitem_alloc((char_u *)key);
6523 if (item == NULL)
6524 return FAIL;
6525 item->di_tv.v_lock = 0;
6526 if (str == NULL)
6527 {
6528 item->di_tv.v_type = VAR_NUMBER;
6529 item->di_tv.vval.v_number = nr;
6530 }
6531 else
6532 {
6533 item->di_tv.v_type = VAR_STRING;
6534 item->di_tv.vval.v_string = vim_strsave(str);
6535 }
6536 if (dict_add(d, item) == FAIL)
6537 {
6538 dictitem_free(item);
6539 return FAIL;
6540 }
6541 return OK;
6542}
6543
6544/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 * Get the number of items in a Dictionary.
6546 */
6547 static long
6548dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 if (d == NULL)
6552 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006553 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554}
6555
6556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 * Find item "key[len]" in Dictionary "d".
6558 * If "len" is negative use strlen(key).
6559 * Returns NULL when not found.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006562dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 char_u *key;
6565 int len;
6566{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006567#define AKEYLEN 200
6568 char_u buf[AKEYLEN];
6569 char_u *akey;
6570 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006573 if (len < 0)
6574 akey = key;
6575 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006577 tofree = akey = vim_strnsave(key, len);
6578 if (akey == NULL)
6579 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 else
6582 {
6583 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006584 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 akey = buf;
6586 }
6587
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 vim_free(tofree);
6590 if (HASHITEM_EMPTY(hi))
6591 return NULL;
6592 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593}
6594
6595/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006596 * Get a string item from a dictionary.
6597 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006598 * Returns NULL if the entry doesn't exist or out of memory.
6599 */
6600 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006601get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006602 dict_T *d;
6603 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006604 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006605{
6606 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006607 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006608
6609 di = dict_find(d, key, -1);
6610 if (di == NULL)
6611 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006612 s = get_tv_string(&di->di_tv);
6613 if (save && s != NULL)
6614 s = vim_strsave(s);
6615 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006616}
6617
6618/*
6619 * Get a number item from a dictionary.
6620 * Returns 0 if the entry doesn't exist or out of memory.
6621 */
6622 long
6623get_dict_number(d, key)
6624 dict_T *d;
6625 char_u *key;
6626{
6627 dictitem_T *di;
6628
6629 di = dict_find(d, key, -1);
6630 if (di == NULL)
6631 return 0;
6632 return get_tv_number(&di->di_tv);
6633}
6634
6635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 * Return an allocated string with the string representation of a Dictionary.
6637 * May return NULL.
6638 */
6639 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643{
6644 garray_T ga;
6645 int first = TRUE;
6646 char_u *tofree;
6647 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006648 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006651 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 return NULL;
6655 ga_init2(&ga, (int)sizeof(char), 80);
6656 ga_append(&ga, '{');
6657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006658 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 --todo;
6664
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(&ga, (char_u *)", ");
6669
6670 tofree = string_quote(hi->hi_key, FALSE);
6671 if (tofree != NULL)
6672 {
6673 ga_concat(&ga, tofree);
6674 vim_free(tofree);
6675 }
6676 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006678 if (s != NULL)
6679 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006680 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 if (s == NULL)
6682 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 if (todo > 0)
6686 {
6687 vim_free(ga.ga_data);
6688 return NULL;
6689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006690
6691 ga_append(&ga, '}');
6692 ga_append(&ga, NUL);
6693 return (char_u *)ga.ga_data;
6694}
6695
6696/*
6697 * Allocate a variable for a Dictionary and fill it from "*arg".
6698 * Return OK or FAIL. Returns NOTDONE for {expr}.
6699 */
6700 static int
6701get_dict_tv(arg, rettv, evaluate)
6702 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006703 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 int evaluate;
6705{
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 dict_T *d = NULL;
6707 typval_T tvkey;
6708 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006710 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006712 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713
6714 /*
6715 * First check if it's not a curly-braces thing: {expr}.
6716 * Must do this without evaluating, otherwise a function may be called
6717 * twice. Unfortunately this means we need to call eval1() twice for the
6718 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721 if (*start != '}')
6722 {
6723 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6724 return FAIL;
6725 if (*start == '}')
6726 return NOTDONE;
6727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006728
6729 if (evaluate)
6730 {
6731 d = dict_alloc();
6732 if (d == NULL)
6733 return FAIL;
6734 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006735 tvkey.v_type = VAR_UNKNOWN;
6736 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737
6738 *arg = skipwhite(*arg + 1);
6739 while (**arg != '}' && **arg != NUL)
6740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006741 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 goto failret;
6743 if (**arg != ':')
6744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006745 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006746 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 goto failret;
6748 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006749 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006751 key = get_tv_string_buf_chk(&tvkey, buf);
6752 if (key == NULL || *key == NUL)
6753 {
6754 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6755 if (key != NULL)
6756 EMSG(_(e_emptykey));
6757 clear_tv(&tvkey);
6758 goto failret;
6759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761
6762 *arg = skipwhite(*arg + 1);
6763 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6764 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006765 if (evaluate)
6766 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 goto failret;
6768 }
6769 if (evaluate)
6770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006772 if (item != NULL)
6773 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006774 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 clear_tv(&tv);
6777 goto failret;
6778 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 item = dictitem_alloc(key);
6780 clear_tv(&tvkey);
6781 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006783 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006784 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006785 if (dict_add(d, item) == FAIL)
6786 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 }
6788 }
6789
6790 if (**arg == '}')
6791 break;
6792 if (**arg != ',')
6793 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006794 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795 goto failret;
6796 }
6797 *arg = skipwhite(*arg + 1);
6798 }
6799
6800 if (**arg != '}')
6801 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006802 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006803failret:
6804 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006805 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006806 return FAIL;
6807 }
6808
6809 *arg = skipwhite(*arg + 1);
6810 if (evaluate)
6811 {
6812 rettv->v_type = VAR_DICT;
6813 rettv->vval.v_dict = d;
6814 ++d->dv_refcount;
6815 }
6816
6817 return OK;
6818}
6819
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006821 * Return a string with the string representation of a variable.
6822 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006823 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006825 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006826 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 */
6828 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006829echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006830 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006832 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006833 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 static int recurse = 0;
6836 char_u *r = NULL;
6837
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006840 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 *tofree = NULL;
6842 return NULL;
6843 }
6844 ++recurse;
6845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 switch (tv->v_type)
6847 {
6848 case VAR_FUNC:
6849 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 r = tv->vval.v_string;
6851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006852
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006853 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006854 if (tv->vval.v_list == NULL)
6855 {
6856 *tofree = NULL;
6857 r = NULL;
6858 }
6859 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6860 {
6861 *tofree = NULL;
6862 r = (char_u *)"[...]";
6863 }
6864 else
6865 {
6866 tv->vval.v_list->lv_copyID = copyID;
6867 *tofree = list2string(tv, copyID);
6868 r = *tofree;
6869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006871
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006873 if (tv->vval.v_dict == NULL)
6874 {
6875 *tofree = NULL;
6876 r = NULL;
6877 }
6878 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6879 {
6880 *tofree = NULL;
6881 r = (char_u *)"{...}";
6882 }
6883 else
6884 {
6885 tv->vval.v_dict->dv_copyID = copyID;
6886 *tofree = dict2string(tv, copyID);
6887 r = *tofree;
6888 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006891 case VAR_STRING:
6892 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006893 *tofree = NULL;
6894 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006895 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006897 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901
6902 --recurse;
6903 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904}
6905
6906/*
6907 * Return a string with the string representation of a variable.
6908 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6909 * "numbuf" is used for a number.
6910 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006911 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 */
6913 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006914tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006916 char_u **tofree;
6917 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006918 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006919{
6920 switch (tv->v_type)
6921 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006922 case VAR_FUNC:
6923 *tofree = string_quote(tv->vval.v_string, TRUE);
6924 return *tofree;
6925 case VAR_STRING:
6926 *tofree = string_quote(tv->vval.v_string, FALSE);
6927 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006929 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006931 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006932 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006933 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006934 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006935 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006936}
6937
6938/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 * Return string "str" in ' quotes, doubling ' characters.
6940 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006941 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006942 */
6943 static char_u *
6944string_quote(str, function)
6945 char_u *str;
6946 int function;
6947{
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006949 char_u *p, *r, *s;
6950
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 len = (function ? 13 : 3);
6952 if (str != NULL)
6953 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006954 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 for (p = str; *p != NUL; mb_ptr_adv(p))
6956 if (*p == '\'')
6957 ++len;
6958 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006959 s = r = alloc(len);
6960 if (r != NULL)
6961 {
6962 if (function)
6963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965 r += 10;
6966 }
6967 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 if (str != NULL)
6970 for (p = str; *p != NUL; )
6971 {
6972 if (*p == '\'')
6973 *r++ = '\'';
6974 MB_COPY_CHAR(p, r);
6975 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006977 if (function)
6978 *r++ = ')';
6979 *r++ = NUL;
6980 }
6981 return s;
6982}
6983
6984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 * Get the value of an environment variable.
6986 * "arg" is pointing to the '$'. It is advanced to after the name.
6987 * If the environment variable was not set, silently assume it is empty.
6988 * Always return OK.
6989 */
6990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006991get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 int evaluate;
6995{
6996 char_u *string = NULL;
6997 int len;
6998 int cc;
6999 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007000 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 ++*arg;
7003 name = *arg;
7004 len = get_env_len(arg);
7005 if (evaluate)
7006 {
7007 if (len != 0)
7008 {
7009 cc = name[len];
7010 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007011 /* first try vim_getenv(), fast for normal environment vars */
7012 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007014 {
7015 if (!mustfree)
7016 string = vim_strsave(string);
7017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 else
7019 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007020 if (mustfree)
7021 vim_free(string);
7022
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 /* next try expanding things like $VIM and ${HOME} */
7024 string = expand_env_save(name - 1);
7025 if (string != NULL && *string == '$')
7026 {
7027 vim_free(string);
7028 string = NULL;
7029 }
7030 }
7031 name[len] = cc;
7032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007033 rettv->v_type = VAR_STRING;
7034 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 }
7036
7037 return OK;
7038}
7039
7040/*
7041 * Array with names and number of arguments of all internal functions
7042 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7043 */
7044static struct fst
7045{
7046 char *f_name; /* function name */
7047 char f_min_argc; /* minimal number of arguments */
7048 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007049 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007050 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051} functions[] =
7052{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007053 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {"append", 2, 2, f_append},
7055 {"argc", 0, 0, f_argc},
7056 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007057 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007059 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"bufexists", 1, 1, f_bufexists},
7061 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7062 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7063 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7064 {"buflisted", 1, 1, f_buflisted},
7065 {"bufloaded", 1, 1, f_bufloaded},
7066 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007067 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"bufwinnr", 1, 1, f_bufwinnr},
7069 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007070 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007072 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"char2nr", 1, 1, f_char2nr},
7074 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007075 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007077#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007078 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007079 {"complete_add", 1, 1, f_complete_add},
7080 {"complete_check", 0, 0, f_complete_check},
7081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007084 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007086 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007087 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"delete", 1, 1, f_delete},
7089 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007090 {"diff_filler", 1, 1, f_diff_filler},
7091 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007092 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007094 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"eventhandler", 0, 0, f_eventhandler},
7096 {"executable", 1, 1, f_executable},
7097 {"exists", 1, 1, f_exists},
7098 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007099 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007100 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7102 {"filereadable", 1, 1, f_filereadable},
7103 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007104 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007105 {"finddir", 1, 3, f_finddir},
7106 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {"fnamemodify", 2, 2, f_fnamemodify},
7108 {"foldclosed", 1, 1, f_foldclosed},
7109 {"foldclosedend", 1, 1, f_foldclosedend},
7110 {"foldlevel", 1, 1, f_foldlevel},
7111 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007112 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007116 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007117 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {"getbufvar", 2, 2, f_getbufvar},
7119 {"getchar", 0, 1, f_getchar},
7120 {"getcharmod", 0, 0, f_getcharmod},
7121 {"getcmdline", 0, 0, f_getcmdline},
7122 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007123 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007125 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007126 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {"getfsize", 1, 1, f_getfsize},
7128 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007129 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007130 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007131 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007132 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007133 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007134 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007135 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007137 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"getwinposx", 0, 0, f_getwinposx},
7139 {"getwinposy", 0, 0, f_getwinposy},
7140 {"getwinvar", 2, 2, f_getwinvar},
7141 {"glob", 1, 1, f_glob},
7142 {"globpath", 2, 2, f_globpath},
7143 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007145 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007146 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7148 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7149 {"histadd", 2, 2, f_histadd},
7150 {"histdel", 1, 2, f_histdel},
7151 {"histget", 1, 2, f_histget},
7152 {"histnr", 1, 1, f_histnr},
7153 {"hlID", 1, 1, f_hlID},
7154 {"hlexists", 1, 1, f_hlexists},
7155 {"hostname", 0, 0, f_hostname},
7156 {"iconv", 3, 3, f_iconv},
7157 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007158 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007159 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007161 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {"inputrestore", 0, 0, f_inputrestore},
7163 {"inputsave", 0, 0, f_inputsave},
7164 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007165 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007167 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007169 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {"libcall", 3, 3, f_libcall},
7174 {"libcallnr", 3, 3, f_libcallnr},
7175 {"line", 1, 1, f_line},
7176 {"line2byte", 1, 1, f_line2byte},
7177 {"lispindent", 1, 1, f_lispindent},
7178 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007179 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007180 {"maparg", 1, 3, f_maparg},
7181 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007182 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007183 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007184 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007185 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007186 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007187 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007188 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007189 {"max", 1, 1, f_max},
7190 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007191#ifdef vim_mkdir
7192 {"mkdir", 1, 3, f_mkdir},
7193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {"mode", 0, 0, f_mode},
7195 {"nextnonblank", 1, 1, f_nextnonblank},
7196 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007197 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007199 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007202 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007203 {"reltime", 0, 2, f_reltime},
7204 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {"remote_expr", 2, 3, f_remote_expr},
7206 {"remote_foreground", 1, 1, f_remote_foreground},
7207 {"remote_peek", 1, 2, f_remote_peek},
7208 {"remote_read", 1, 1, f_remote_read},
7209 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007210 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007212 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007214 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007215 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007216 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007217 {"searchpair", 3, 6, f_searchpair},
7218 {"searchpairpos", 3, 6, f_searchpairpos},
7219 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {"server2client", 2, 2, f_server2client},
7221 {"serverlist", 0, 0, f_serverlist},
7222 {"setbufvar", 3, 3, f_setbufvar},
7223 {"setcmdpos", 1, 1, f_setcmdpos},
7224 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007225 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007226 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007227 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007228 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007230 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007232 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007234 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007235 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007236 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007237 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007238 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007239 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240#ifdef HAVE_STRFTIME
7241 {"strftime", 1, 2, f_strftime},
7242#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {"strlen", 1, 1, f_strlen},
7246 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007247 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {"strtrans", 1, 1, f_strtrans},
7249 {"submatch", 1, 1, f_submatch},
7250 {"substitute", 4, 4, f_substitute},
7251 {"synID", 3, 3, f_synID},
7252 {"synIDattr", 2, 3, f_synIDattr},
7253 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007254 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007256 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007257 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007258 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007259 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007261 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {"tolower", 1, 1, f_tolower},
7263 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007264 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {"virtcol", 1, 1, f_virtcol},
7268 {"visualmode", 0, 1, f_visualmode},
7269 {"winbufnr", 1, 1, f_winbufnr},
7270 {"wincol", 0, 0, f_wincol},
7271 {"winheight", 1, 1, f_winheight},
7272 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007273 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007275 {"winrestview", 1, 1, f_winrestview},
7276 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007278 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279};
7280
7281#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7282
7283/*
7284 * Function given to ExpandGeneric() to obtain the list of internal
7285 * or user defined function names.
7286 */
7287 char_u *
7288get_function_name(xp, idx)
7289 expand_T *xp;
7290 int idx;
7291{
7292 static int intidx = -1;
7293 char_u *name;
7294
7295 if (idx == 0)
7296 intidx = -1;
7297 if (intidx < 0)
7298 {
7299 name = get_user_func_name(xp, idx);
7300 if (name != NULL)
7301 return name;
7302 }
7303 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7304 {
7305 STRCPY(IObuff, functions[intidx].f_name);
7306 STRCAT(IObuff, "(");
7307 if (functions[intidx].f_max_argc == 0)
7308 STRCAT(IObuff, ")");
7309 return IObuff;
7310 }
7311
7312 return NULL;
7313}
7314
7315/*
7316 * Function given to ExpandGeneric() to obtain the list of internal or
7317 * user defined variable or function names.
7318 */
7319/*ARGSUSED*/
7320 char_u *
7321get_expr_name(xp, idx)
7322 expand_T *xp;
7323 int idx;
7324{
7325 static int intidx = -1;
7326 char_u *name;
7327
7328 if (idx == 0)
7329 intidx = -1;
7330 if (intidx < 0)
7331 {
7332 name = get_function_name(xp, idx);
7333 if (name != NULL)
7334 return name;
7335 }
7336 return get_user_var_name(xp, ++intidx);
7337}
7338
7339#endif /* FEAT_CMDL_COMPL */
7340
7341/*
7342 * Find internal function in table above.
7343 * Return index, or -1 if not found
7344 */
7345 static int
7346find_internal_func(name)
7347 char_u *name; /* name of the function */
7348{
7349 int first = 0;
7350 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7351 int cmp;
7352 int x;
7353
7354 /*
7355 * Find the function name in the table. Binary search.
7356 */
7357 while (first <= last)
7358 {
7359 x = first + ((unsigned)(last - first) >> 1);
7360 cmp = STRCMP(name, functions[x].f_name);
7361 if (cmp < 0)
7362 last = x - 1;
7363 else if (cmp > 0)
7364 first = x + 1;
7365 else
7366 return x;
7367 }
7368 return -1;
7369}
7370
7371/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7373 * name it contains, otherwise return "name".
7374 */
7375 static char_u *
7376deref_func_name(name, lenp)
7377 char_u *name;
7378 int *lenp;
7379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007381 int cc;
7382
7383 cc = name[*lenp];
7384 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007385 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007390 {
7391 *lenp = 0;
7392 return (char_u *)""; /* just in case */
7393 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007394 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 }
7397
7398 return name;
7399}
7400
7401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 * Allocate a variable for the result of a function.
7403 * Return OK or FAIL.
7404 */
7405 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7407 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 char_u *name; /* name of the function */
7409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u **arg; /* argument, pointing to the '(' */
7412 linenr_T firstline; /* first line of range */
7413 linenr_T lastline; /* last line of range */
7414 int *doesrange; /* return: function handled range */
7415 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 char_u *argp;
7419 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007420 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int argcount = 0; /* number of arguments found */
7422
7423 /*
7424 * Get the arguments.
7425 */
7426 argp = *arg;
7427 while (argcount < MAX_FUNC_ARGS)
7428 {
7429 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7430 if (*argp == ')' || *argp == ',' || *argp == NUL)
7431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7433 {
7434 ret = FAIL;
7435 break;
7436 }
7437 ++argcount;
7438 if (*argp != ',')
7439 break;
7440 }
7441 if (*argp == ')')
7442 ++argp;
7443 else
7444 ret = FAIL;
7445
7446 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 {
7451 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007454 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007458 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460 *arg = skipwhite(argp);
7461 return ret;
7462}
7463
7464
7465/*
7466 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007467 * Return OK when the function can't be called, FAIL otherwise.
7468 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 */
7470 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 char_u *name; /* name of the function */
7474 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007477 typval_T *argvars; /* vars for arguments, must have "argcount"
7478 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 linenr_T firstline; /* first line of range */
7480 linenr_T lastline; /* last line of range */
7481 int *doesrange; /* return: function handled range */
7482 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486#define ERROR_UNKNOWN 0
7487#define ERROR_TOOMANY 1
7488#define ERROR_TOOFEW 2
7489#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007490#define ERROR_DICT 4
7491#define ERROR_NONE 5
7492#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 int error = ERROR_NONE;
7494 int i;
7495 int llen;
7496 ufunc_T *fp;
7497 int cc;
7498#define FLEN_FIXED 40
7499 char_u fname_buf[FLEN_FIXED + 1];
7500 char_u *fname;
7501
7502 /*
7503 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7504 * Change <SNR>123_name() to K_SNR 123_name().
7505 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7506 */
7507 cc = name[len];
7508 name[len] = NUL;
7509 llen = eval_fname_script(name);
7510 if (llen > 0)
7511 {
7512 fname_buf[0] = K_SPECIAL;
7513 fname_buf[1] = KS_EXTRA;
7514 fname_buf[2] = (int)KE_SNR;
7515 i = 3;
7516 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7517 {
7518 if (current_SID <= 0)
7519 error = ERROR_SCRIPT;
7520 else
7521 {
7522 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7523 i = (int)STRLEN(fname_buf);
7524 }
7525 }
7526 if (i + STRLEN(name + llen) < FLEN_FIXED)
7527 {
7528 STRCPY(fname_buf + i, name + llen);
7529 fname = fname_buf;
7530 }
7531 else
7532 {
7533 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7534 if (fname == NULL)
7535 error = ERROR_OTHER;
7536 else
7537 {
7538 mch_memmove(fname, fname_buf, (size_t)i);
7539 STRCPY(fname + i, name + llen);
7540 }
7541 }
7542 }
7543 else
7544 fname = name;
7545
7546 *doesrange = FALSE;
7547
7548
7549 /* execute the function if no errors detected and executing */
7550 if (evaluate && error == ERROR_NONE)
7551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 error = ERROR_UNKNOWN;
7554
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007555 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
7557 /*
7558 * User defined function.
7559 */
7560 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007563 /* Trigger FuncUndefined event, may load the function. */
7564 if (fp == NULL
7565 && apply_autocmds(EVENT_FUNCUNDEFINED,
7566 fname, fname, TRUE, NULL)
7567 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007569 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 fp = find_func(fname);
7571 }
7572#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007573 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007574 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007575 {
7576 /* loaded a package, search for the function again */
7577 fp = find_func(fname);
7578 }
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (fp != NULL)
7581 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007588 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 else
7591 {
7592 /*
7593 * Call the user function.
7594 * Save and restore search patterns, script variables and
7595 * redo buffer.
7596 */
7597 save_search_patterns();
7598 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007599 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007602 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7603 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7604 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007605 /* Function was unreferenced while being used, free it
7606 * now. */
7607 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 restoreRedobuff();
7609 restore_search_patterns();
7610 error = ERROR_NONE;
7611 }
7612 }
7613 }
7614 else
7615 {
7616 /*
7617 * Find the function name in the table, call its implementation.
7618 */
7619 i = find_internal_func(fname);
7620 if (i >= 0)
7621 {
7622 if (argcount < functions[i].f_min_argc)
7623 error = ERROR_TOOFEW;
7624 else if (argcount > functions[i].f_max_argc)
7625 error = ERROR_TOOMANY;
7626 else
7627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 error = ERROR_NONE;
7631 }
7632 }
7633 }
7634 /*
7635 * The function call (or "FuncUndefined" autocommand sequence) might
7636 * have been aborted by an error, an interrupt, or an explicitly thrown
7637 * exception that has not been caught so far. This situation can be
7638 * tested for by calling aborting(). For an error in an internal
7639 * function or for the "E132" error in call_user_func(), however, the
7640 * throw point at which the "force_abort" flag (temporarily reset by
7641 * emsg()) is normally updated has not been reached yet. We need to
7642 * update that flag first to make aborting() reliable.
7643 */
7644 update_force_abort();
7645 }
7646 if (error == ERROR_NONE)
7647 ret = OK;
7648
7649 /*
7650 * Report an error unless the argument evaluation or function call has been
7651 * cancelled due to an aborting error, an interrupt, or an exception.
7652 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 if (!aborting())
7654 {
7655 switch (error)
7656 {
7657 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007658 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 break;
7660 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007661 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 break;
7663 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007664 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 name);
7666 break;
7667 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007668 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 name);
7670 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007672 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 name);
7674 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 }
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 name[len] = cc;
7679 if (fname != name && fname != fname_buf)
7680 vim_free(fname);
7681
7682 return ret;
7683}
7684
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007685/*
7686 * Give an error message with a function name. Handle <SNR> things.
7687 */
7688 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007689emsg_funcname(ermsg, name)
7690 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007691 char_u *name;
7692{
7693 char_u *p;
7694
7695 if (*name == K_SPECIAL)
7696 p = concat_str((char_u *)"<SNR>", name + 3);
7697 else
7698 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007699 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007700 if (p != name)
7701 vim_free(p);
7702}
7703
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704/*********************************************
7705 * Implementation of the built-in functions
7706 */
7707
7708/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007709 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007712f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 typval_T *argvars;
7714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007721 if ((l = argvars[0].vval.v_list) != NULL
7722 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7723 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 }
7726 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007727 EMSG(_(e_listreq));
7728}
7729
7730/*
7731 * "append(lnum, string/list)" function
7732 */
7733 static void
7734f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T *argvars;
7736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007737{
7738 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 list_T *l = NULL;
7741 listitem_T *li = NULL;
7742 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007743 long added = 0;
7744
Bram Moolenaar0d660222005-01-07 21:51:51 +00007745 lnum = get_tv_lnum(argvars);
7746 if (lnum >= 0
7747 && lnum <= curbuf->b_ml.ml_line_count
7748 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007751 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007752 l = argvars[1].vval.v_list;
7753 if (l == NULL)
7754 return;
7755 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007757 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007758 for (;;)
7759 {
7760 if (l == NULL)
7761 tv = &argvars[1]; /* append a string */
7762 else if (li == NULL)
7763 break; /* end of list */
7764 else
7765 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 line = get_tv_string_chk(tv);
7767 if (line == NULL) /* type error */
7768 {
7769 rettv->vval.v_number = 1; /* Failed */
7770 break;
7771 }
7772 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007773 ++added;
7774 if (l == NULL)
7775 break;
7776 li = li->li_next;
7777 }
7778
7779 appended_lines_mark(lnum, added);
7780 if (curwin->w_cursor.lnum > lnum)
7781 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 else
7784 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
7787/*
7788 * "argc()" function
7789 */
7790/* ARGSUSED */
7791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T *argvars;
7794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
7799/*
7800 * "argidx()" function
7801 */
7802/* ARGSUSED */
7803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *argvars;
7806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810
7811/*
7812 * "argv(nr)" function
7813 */
7814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 typval_T *argvars;
7817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 int idx;
7820
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007821 if (argvars[0].v_type != VAR_UNKNOWN)
7822 {
7823 idx = get_tv_number_chk(&argvars[0], NULL);
7824 if (idx >= 0 && idx < ARGCOUNT)
7825 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7826 else
7827 rettv->vval.v_string = NULL;
7828 rettv->v_type = VAR_STRING;
7829 }
7830 else if (rettv_list_alloc(rettv) == OK)
7831 for (idx = 0; idx < ARGCOUNT; ++idx)
7832 list_append_string(rettv->vval.v_list,
7833 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834}
7835
7836/*
7837 * "browse(save, title, initdir, default)" function
7838 */
7839/* ARGSUSED */
7840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 typval_T *argvars;
7843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845#ifdef FEAT_BROWSE
7846 int save;
7847 char_u *title;
7848 char_u *initdir;
7849 char_u *defname;
7850 char_u buf[NUMBUFLEN];
7851 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 save = get_tv_number_chk(&argvars[0], &error);
7855 title = get_tv_string_chk(&argvars[1]);
7856 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7857 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 if (error || title == NULL || initdir == NULL || defname == NULL)
7860 rettv->vval.v_string = NULL;
7861 else
7862 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007863 do_browse(save ? BROWSE_SAVE : 0,
7864 title, defname, NULL, initdir, NULL, curbuf);
7865#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007869}
7870
7871/*
7872 * "browsedir(title, initdir)" function
7873 */
7874/* ARGSUSED */
7875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007876f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 typval_T *argvars;
7878 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007879{
7880#ifdef FEAT_BROWSE
7881 char_u *title;
7882 char_u *initdir;
7883 char_u buf[NUMBUFLEN];
7884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885 title = get_tv_string_chk(&argvars[0]);
7886 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 if (title == NULL || initdir == NULL)
7889 rettv->vval.v_string = NULL;
7890 else
7891 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007892 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
Bram Moolenaar33570922005-01-25 22:26:29 +00007899static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007900
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901/*
7902 * Find a buffer by number or exact name.
7903 */
7904 static buf_T *
7905find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 if (avar->v_type == VAR_NUMBER)
7911 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007912 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007915 if (buf == NULL)
7916 {
7917 /* No full path name match, try a match with a URL or a "nofile"
7918 * buffer, these don't use the full path. */
7919 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7920 if (buf->b_fname != NULL
7921 && (path_with_url(buf->b_fname)
7922#ifdef FEAT_QUICKFIX
7923 || bt_nofile(buf)
7924#endif
7925 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007927 break;
7928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 return buf;
7931}
7932
7933/*
7934 * "bufexists(expr)" function
7935 */
7936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 typval_T *argvars;
7939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
7943
7944/*
7945 * "buflisted(expr)" function
7946 */
7947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 buf_T *buf;
7953
7954 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956}
7957
7958/*
7959 * "bufloaded(expr)" function
7960 */
7961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 typval_T *argvars;
7964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 buf_T *buf;
7967
7968 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007969 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970}
7971
Bram Moolenaar33570922005-01-25 22:26:29 +00007972static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974/*
7975 * Get buffer by number or pattern.
7976 */
7977 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int save_magic;
7983 char_u *save_cpo;
7984 buf_T *buf;
7985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 if (tv->v_type == VAR_NUMBER)
7987 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007988 if (tv->v_type != VAR_STRING)
7989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 if (name == NULL || *name == NUL)
7991 return curbuf;
7992 if (name[0] == '$' && name[1] == NUL)
7993 return lastbuf;
7994
7995 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7996 save_magic = p_magic;
7997 p_magic = TRUE;
7998 save_cpo = p_cpo;
7999 p_cpo = (char_u *)"";
8000
8001 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8002 TRUE, FALSE));
8003
8004 p_magic = save_magic;
8005 p_cpo = save_cpo;
8006
8007 /* If not found, try expanding the name, like done for bufexists(). */
8008 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
8011 return buf;
8012}
8013
8014/*
8015 * "bufname(expr)" function
8016 */
8017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 typval_T *argvars;
8020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022 buf_T *buf;
8023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008024 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008026 buf = get_buf_tv(&argvars[0]);
8027 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 --emsg_off;
8033}
8034
8035/*
8036 * "bufnr(expr)" function
8037 */
8038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 typval_T *argvars;
8041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
8043 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008044 int error = FALSE;
8045 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008047 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008050 --emsg_off;
8051
8052 /* If the buffer isn't found and the second argument is not zero create a
8053 * new buffer. */
8054 if (buf == NULL
8055 && argvars[1].v_type != VAR_UNKNOWN
8056 && get_tv_number_chk(&argvars[1], &error) != 0
8057 && !error
8058 && (name = get_tv_string_chk(&argvars[0])) != NULL
8059 && !error)
8060 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8061
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066}
8067
8068/*
8069 * "bufwinnr(nr)" function
8070 */
8071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 typval_T *argvars;
8074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076#ifdef FEAT_WINDOWS
8077 win_T *wp;
8078 int winnr = 0;
8079#endif
8080 buf_T *buf;
8081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008082 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008084 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085#ifdef FEAT_WINDOWS
8086 for (wp = firstwin; wp; wp = wp->w_next)
8087 {
8088 ++winnr;
8089 if (wp->w_buffer == buf)
8090 break;
8091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095#endif
8096 --emsg_off;
8097}
8098
8099/*
8100 * "byte2line(byte)" function
8101 */
8102/*ARGSUSED*/
8103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008104f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 typval_T *argvars;
8106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110#else
8111 long boff = 0;
8112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008113 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008117 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 (linenr_T)0, &boff);
8119#endif
8120}
8121
8122/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008123 * "byteidx()" function
8124 */
8125/*ARGSUSED*/
8126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 typval_T *argvars;
8129 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008130{
8131#ifdef FEAT_MBYTE
8132 char_u *t;
8133#endif
8134 char_u *str;
8135 long idx;
8136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008137 str = get_tv_string_chk(&argvars[0]);
8138 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008140 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008141 return;
8142
8143#ifdef FEAT_MBYTE
8144 t = str;
8145 for ( ; idx > 0; idx--)
8146 {
8147 if (*t == NUL) /* EOL reached */
8148 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008149 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008150 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008151 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008152#else
8153 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008154 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008155#endif
8156}
8157
8158/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008159 * "call(func, arglist)" function
8160 */
8161 static void
8162f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 typval_T *argvars;
8164 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008165{
8166 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008167 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008172
8173 rettv->vval.v_number = 0;
8174 if (argvars[1].v_type != VAR_LIST)
8175 {
8176 EMSG(_(e_listreq));
8177 return;
8178 }
8179 if (argvars[1].vval.v_list == NULL)
8180 return;
8181
8182 if (argvars[0].v_type == VAR_FUNC)
8183 func = argvars[0].vval.v_string;
8184 else
8185 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008186 if (*func == NUL)
8187 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008188
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 if (argvars[2].v_type != VAR_UNKNOWN)
8190 {
8191 if (argvars[2].v_type != VAR_DICT)
8192 {
8193 EMSG(_(e_dictreq));
8194 return;
8195 }
8196 selfdict = argvars[2].vval.v_dict;
8197 }
8198
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008199 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8200 item = item->li_next)
8201 {
8202 if (argc == MAX_FUNC_ARGS)
8203 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008204 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008205 break;
8206 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008207 /* Make a copy of each argument. This is needed to be able to set
8208 * v_lock to VAR_FIXED in the copy without changing the original list.
8209 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008210 copy_tv(&item->li_tv, &argv[argc++]);
8211 }
8212
8213 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008214 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008215 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8216 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008217
8218 /* Free the arguments. */
8219 while (argc > 0)
8220 clear_tv(&argv[--argc]);
8221}
8222
8223/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008224 * "changenr()" function
8225 */
8226/*ARGSUSED*/
8227 static void
8228f_changenr(argvars, rettv)
8229 typval_T *argvars;
8230 typval_T *rettv;
8231{
8232 rettv->vval.v_number = curbuf->b_u_seq_cur;
8233}
8234
8235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 * "char2nr(string)" function
8237 */
8238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 typval_T *argvars;
8241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242{
8243#ifdef FEAT_MBYTE
8244 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 else
8247#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249}
8250
8251/*
8252 * "cindent(lnum)" function
8253 */
8254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 typval_T *argvars;
8257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259#ifdef FEAT_CINDENT
8260 pos_T pos;
8261 linenr_T lnum;
8262
8263 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8266 {
8267 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 curwin->w_cursor = pos;
8270 }
8271 else
8272#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274}
8275
8276/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008277 * "clearmatches()" function
8278 */
8279/*ARGSUSED*/
8280 static void
8281f_clearmatches(argvars, rettv)
8282 typval_T *argvars;
8283 typval_T *rettv;
8284{
8285#ifdef FEAT_SEARCH_EXTRA
8286 clear_matches(curwin);
8287#endif
8288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * "col(string)" function
8292 */
8293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *argvars;
8296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 colnr_T col = 0;
8299 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008300 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008302 fp = var2fpos(&argvars[0], FALSE, &fnum);
8303 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {
8305 if (fp->col == MAXCOL)
8306 {
8307 /* '> can be MAXCOL, get the length of the line then */
8308 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008309 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 else
8311 col = MAXCOL;
8312 }
8313 else
8314 {
8315 col = fp->col + 1;
8316#ifdef FEAT_VIRTUALEDIT
8317 /* col(".") when the cursor is on the NUL at the end of the line
8318 * because of "coladd" can be seen as an extra column. */
8319 if (virtual_active() && fp == &curwin->w_cursor)
8320 {
8321 char_u *p = ml_get_cursor();
8322
8323 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8324 curwin->w_virtcol - curwin->w_cursor.coladd))
8325 {
8326# ifdef FEAT_MBYTE
8327 int l;
8328
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008329 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 col += l;
8331# else
8332 if (*p != NUL && p[1] == NUL)
8333 ++col;
8334# endif
8335 }
8336 }
8337#endif
8338 }
8339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
Bram Moolenaar572cb562005-08-05 21:35:02 +00008343#if defined(FEAT_INS_EXPAND)
8344/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008345 * "complete()" function
8346 */
8347/*ARGSUSED*/
8348 static void
8349f_complete(argvars, rettv)
8350 typval_T *argvars;
8351 typval_T *rettv;
8352{
8353 int startcol;
8354
8355 if ((State & INSERT) == 0)
8356 {
8357 EMSG(_("E785: complete() can only be used in Insert mode"));
8358 return;
8359 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008360
8361 /* Check for undo allowed here, because if something was already inserted
8362 * the line was already saved for undo and this check isn't done. */
8363 if (!undo_allowed())
8364 return;
8365
Bram Moolenaarade00832006-03-10 21:46:58 +00008366 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8367 {
8368 EMSG(_(e_invarg));
8369 return;
8370 }
8371
8372 startcol = get_tv_number_chk(&argvars[0], NULL);
8373 if (startcol <= 0)
8374 return;
8375
8376 set_completion(startcol - 1, argvars[1].vval.v_list);
8377}
8378
8379/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008380 * "complete_add()" function
8381 */
8382/*ARGSUSED*/
8383 static void
8384f_complete_add(argvars, rettv)
8385 typval_T *argvars;
8386 typval_T *rettv;
8387{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008388 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008389}
8390
8391/*
8392 * "complete_check()" function
8393 */
8394/*ARGSUSED*/
8395 static void
8396f_complete_check(argvars, rettv)
8397 typval_T *argvars;
8398 typval_T *rettv;
8399{
8400 int saved = RedrawingDisabled;
8401
8402 RedrawingDisabled = 0;
8403 ins_compl_check_keys(0);
8404 rettv->vval.v_number = compl_interrupted;
8405 RedrawingDisabled = saved;
8406}
8407#endif
8408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409/*
8410 * "confirm(message, buttons[, default [, type]])" function
8411 */
8412/*ARGSUSED*/
8413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 typval_T *argvars;
8416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8419 char_u *message;
8420 char_u *buttons = NULL;
8421 char_u buf[NUMBUFLEN];
8422 char_u buf2[NUMBUFLEN];
8423 int def = 1;
8424 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425 char_u *typestr;
8426 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 message = get_tv_string_chk(&argvars[0]);
8429 if (message == NULL)
8430 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008431 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8434 if (buttons == NULL)
8435 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8442 if (typestr == NULL)
8443 error = TRUE;
8444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 switch (TOUPPER_ASC(*typestr))
8447 {
8448 case 'E': type = VIM_ERROR; break;
8449 case 'Q': type = VIM_QUESTION; break;
8450 case 'I': type = VIM_INFO; break;
8451 case 'W': type = VIM_WARNING; break;
8452 case 'G': type = VIM_GENERIC; break;
8453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 }
8456 }
8457 }
8458
8459 if (buttons == NULL || *buttons == NUL)
8460 buttons = (char_u *)_("&Ok");
8461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 if (error)
8463 rettv->vval.v_number = 0;
8464 else
8465 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 def, NULL);
8467#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469#endif
8470}
8471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472/*
8473 * "copy()" function
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008480 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
8483/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008484 * "count()" function
8485 */
8486 static void
8487f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008488 typval_T *argvars;
8489 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008490{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008491 long n = 0;
8492 int ic = FALSE;
8493
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008495 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 listitem_T *li;
8497 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008499
Bram Moolenaare9a41262005-01-15 22:18:47 +00008500 if ((l = argvars[0].vval.v_list) != NULL)
8501 {
8502 li = l->lv_first;
8503 if (argvars[2].v_type != VAR_UNKNOWN)
8504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008505 int error = FALSE;
8506
8507 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008508 if (argvars[3].v_type != VAR_UNKNOWN)
8509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 idx = get_tv_number_chk(&argvars[3], &error);
8511 if (!error)
8512 {
8513 li = list_find(l, idx);
8514 if (li == NULL)
8515 EMSGN(_(e_listidx), idx);
8516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008517 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008518 if (error)
8519 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008520 }
8521
8522 for ( ; li != NULL; li = li->li_next)
8523 if (tv_equal(&li->li_tv, &argvars[1], ic))
8524 ++n;
8525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008527 else if (argvars[0].v_type == VAR_DICT)
8528 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 int todo;
8530 dict_T *d;
8531 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008533 if ((d = argvars[0].vval.v_dict) != NULL)
8534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008535 int error = FALSE;
8536
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 if (argvars[2].v_type != VAR_UNKNOWN)
8538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 if (argvars[3].v_type != VAR_UNKNOWN)
8541 EMSG(_(e_invarg));
8542 }
8543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008544 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008545 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008546 {
8547 if (!HASHITEM_EMPTY(hi))
8548 {
8549 --todo;
8550 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8551 ++n;
8552 }
8553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 }
8555 }
8556 else
8557 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008558 rettv->vval.v_number = n;
8559}
8560
8561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8563 *
8564 * Checks the existence of a cscope connection.
8565 */
8566/*ARGSUSED*/
8567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 typval_T *argvars;
8570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572#ifdef FEAT_CSCOPE
8573 int num = 0;
8574 char_u *dbpath = NULL;
8575 char_u *prepend = NULL;
8576 char_u buf[NUMBUFLEN];
8577
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 if (argvars[0].v_type != VAR_UNKNOWN
8579 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581 num = (int)get_tv_number(&argvars[0]);
8582 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 }
8586
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590#endif
8591}
8592
8593/*
8594 * "cursor(lnum, col)" function
8595 *
8596 * Moves the cursor to the specified line and column
8597 */
8598/*ARGSUSED*/
8599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *argvars;
8602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
8604 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008605#ifdef FEAT_VIRTUALEDIT
8606 long coladd = 0;
8607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
Bram Moolenaara5525202006-03-02 22:52:09 +00008609 if (argvars[1].v_type == VAR_UNKNOWN)
8610 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008611 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008612
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008613 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008614 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008615 line = pos.lnum;
8616 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008617#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008619#endif
8620 }
8621 else
8622 {
8623 line = get_tv_lnum(argvars);
8624 col = get_tv_number_chk(&argvars[1], NULL);
8625#ifdef FEAT_VIRTUALEDIT
8626 if (argvars[2].v_type != VAR_UNKNOWN)
8627 coladd = get_tv_number_chk(&argvars[2], NULL);
8628#endif
8629 }
8630 if (line < 0 || col < 0
8631#ifdef FEAT_VIRTUALEDIT
8632 || coladd < 0
8633#endif
8634 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (line > 0)
8637 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (col > 0)
8639 curwin->w_cursor.col = col - 1;
8640#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008641 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#endif
8643
8644 /* Make sure the cursor is in a valid position. */
8645 check_cursor();
8646#ifdef FEAT_MBYTE
8647 /* Correct cursor for multi-byte character. */
8648 if (has_mbyte)
8649 mb_adjust_cursor();
8650#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008651
8652 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653}
8654
8655/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 */
8658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 typval_T *argvars;
8661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008663 int noref = 0;
8664
8665 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008667 if (noref < 0 || noref > 1)
8668 EMSG(_(e_invarg));
8669 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008670 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671}
8672
8673/*
8674 * "delete()" function
8675 */
8676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685}
8686
8687/*
8688 * "did_filetype()" function
8689 */
8690/*ARGSUSED*/
8691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 typval_T *argvars;
8694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
8701}
8702
8703/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008704 * "diff_filler()" function
8705 */
8706/*ARGSUSED*/
8707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 typval_T *argvars;
8710 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008711{
8712#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008714#endif
8715}
8716
8717/*
8718 * "diff_hlID()" function
8719 */
8720/*ARGSUSED*/
8721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008725{
8726#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008728 static linenr_T prev_lnum = 0;
8729 static int changedtick = 0;
8730 static int fnum = 0;
8731 static int change_start = 0;
8732 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008733 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008734 int filler_lines;
8735 int col;
8736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 if (lnum < 0) /* ignore type error in {lnum} arg */
8738 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008739 if (lnum != prev_lnum
8740 || changedtick != curbuf->b_changedtick
8741 || fnum != curbuf->b_fnum)
8742 {
8743 /* New line, buffer, change: need to get the values. */
8744 filler_lines = diff_check(curwin, lnum);
8745 if (filler_lines < 0)
8746 {
8747 if (filler_lines == -1)
8748 {
8749 change_start = MAXCOL;
8750 change_end = -1;
8751 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8752 hlID = HLF_ADD; /* added line */
8753 else
8754 hlID = HLF_CHD; /* changed line */
8755 }
8756 else
8757 hlID = HLF_ADD; /* added line */
8758 }
8759 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008760 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008761 prev_lnum = lnum;
8762 changedtick = curbuf->b_changedtick;
8763 fnum = curbuf->b_fnum;
8764 }
8765
8766 if (hlID == HLF_CHD || hlID == HLF_TXD)
8767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008769 if (col >= change_start && col <= change_end)
8770 hlID = HLF_TXD; /* changed text */
8771 else
8772 hlID = HLF_CHD; /* changed line */
8773 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008774 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008775#endif
8776}
8777
8778/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008779 * "empty({expr})" function
8780 */
8781 static void
8782f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 typval_T *argvars;
8784 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008785{
8786 int n;
8787
8788 switch (argvars[0].v_type)
8789 {
8790 case VAR_STRING:
8791 case VAR_FUNC:
8792 n = argvars[0].vval.v_string == NULL
8793 || *argvars[0].vval.v_string == NUL;
8794 break;
8795 case VAR_NUMBER:
8796 n = argvars[0].vval.v_number == 0;
8797 break;
8798 case VAR_LIST:
8799 n = argvars[0].vval.v_list == NULL
8800 || argvars[0].vval.v_list->lv_first == NULL;
8801 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 case VAR_DICT:
8803 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008805 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008806 default:
8807 EMSG2(_(e_intern2), "f_empty()");
8808 n = 0;
8809 }
8810
8811 rettv->vval.v_number = n;
8812}
8813
8814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 * "escape({string}, {chars})" function
8816 */
8817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008819 typval_T *argvars;
8820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 char_u buf[NUMBUFLEN];
8823
Bram Moolenaar758711c2005-02-02 23:11:38 +00008824 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8825 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
8829/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008830 * "eval()" function
8831 */
8832/*ARGSUSED*/
8833 static void
8834f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 typval_T *argvars;
8836 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008837{
8838 char_u *s;
8839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008840 s = get_tv_string_chk(&argvars[0]);
8841 if (s != NULL)
8842 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008844 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8845 {
8846 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008847 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008848 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008849 else if (*s != NUL)
8850 EMSG(_(e_trailing));
8851}
8852
8853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * "eventhandler()" function
8855 */
8856/*ARGSUSED*/
8857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 typval_T *argvars;
8860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863}
8864
8865/*
8866 * "executable()" function
8867 */
8868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 typval_T *argvars;
8871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874}
8875
8876/*
8877 * "exists()" function
8878 */
8879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008881 typval_T *argvars;
8882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884 char_u *p;
8885 char_u *name;
8886 int n = FALSE;
8887 int len = 0;
8888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (*p == '$') /* environment variable */
8891 {
8892 /* first try "normal" environment variables (fast) */
8893 if (mch_getenv(p + 1) != NULL)
8894 n = TRUE;
8895 else
8896 {
8897 /* try expanding things like $VIM and ${HOME} */
8898 p = expand_env_save(p);
8899 if (p != NULL && *p != '$')
8900 n = TRUE;
8901 vim_free(p);
8902 }
8903 }
8904 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008907 if (*skipwhite(p) != NUL)
8908 n = FALSE; /* trailing garbage */
8909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 else if (*p == '*') /* internal or user defined function */
8911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 else if (*p == ':')
8915 {
8916 n = cmd_exists(p + 1);
8917 }
8918 else if (*p == '#')
8919 {
8920#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008921 if (p[1] == '#')
8922 n = autocmd_supported(p + 2);
8923 else
8924 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#endif
8926 }
8927 else /* internal variable */
8928 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008929 char_u *tofree;
8930 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008932 /* get_name_len() takes care of expanding curly braces */
8933 name = p;
8934 len = get_name_len(&p, &tofree, TRUE, FALSE);
8935 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008937 if (tofree != NULL)
8938 name = tofree;
8939 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8940 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008942 /* handle d.key, l[idx], f(expr) */
8943 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8944 if (n)
8945 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008948 if (*p != NUL)
8949 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008951 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955}
8956
8957/*
8958 * "expand()" function
8959 */
8960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008962 typval_T *argvars;
8963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 char_u *s;
8966 int len;
8967 char_u *errormsg;
8968 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8969 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 rettv->v_type = VAR_STRING;
8973 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (*s == '%' || *s == '#' || *s == '<')
8975 {
8976 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008977 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 --emsg_off;
8979 }
8980 else
8981 {
8982 /* When the optional second argument is non-zero, don't remove matches
8983 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 if (argvars[1].v_type != VAR_UNKNOWN
8985 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 if (!error)
8988 {
8989 ExpandInit(&xpc);
8990 xpc.xp_context = EXPAND_FILES;
8991 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 }
8993 else
8994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 }
8996}
8997
8998/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008999 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009000 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009001 */
9002 static void
9003f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009004 typval_T *argvars;
9005 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 list_T *l1, *l2;
9011 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009012 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 l1 = argvars[0].vval.v_list;
9016 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009017 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9018 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 {
9020 if (argvars[2].v_type != VAR_UNKNOWN)
9021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022 before = get_tv_number_chk(&argvars[2], &error);
9023 if (error)
9024 return; /* type error; errmsg already given */
9025
Bram Moolenaar758711c2005-02-02 23:11:38 +00009026 if (before == l1->lv_len)
9027 item = NULL;
9028 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009029 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009030 item = list_find(l1, before);
9031 if (item == NULL)
9032 {
9033 EMSGN(_(e_listidx), before);
9034 return;
9035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009036 }
9037 }
9038 else
9039 item = NULL;
9040 list_extend(l1, l2, item);
9041
Bram Moolenaare9a41262005-01-15 22:18:47 +00009042 copy_tv(&argvars[0], rettv);
9043 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9046 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 dict_T *d1, *d2;
9048 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 char_u *action;
9050 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009052 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009053
9054 d1 = argvars[0].vval.v_dict;
9055 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009056 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9057 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058 {
9059 /* Check the third argument. */
9060 if (argvars[2].v_type != VAR_UNKNOWN)
9061 {
9062 static char *(av[]) = {"keep", "force", "error"};
9063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 action = get_tv_string_chk(&argvars[2]);
9065 if (action == NULL)
9066 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009067 for (i = 0; i < 3; ++i)
9068 if (STRCMP(action, av[i]) == 0)
9069 break;
9070 if (i == 3)
9071 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009072 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 return;
9074 }
9075 }
9076 else
9077 action = (char_u *)"force";
9078
9079 /* Go over all entries in the second dict and add them to the
9080 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009081 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009083 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009084 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009086 --todo;
9087 di1 = dict_find(d1, hi2->hi_key, -1);
9088 if (di1 == NULL)
9089 {
9090 di1 = dictitem_copy(HI2DI(hi2));
9091 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9092 dictitem_free(di1);
9093 }
9094 else if (*action == 'e')
9095 {
9096 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9097 break;
9098 }
9099 else if (*action == 'f')
9100 {
9101 clear_tv(&di1->di_tv);
9102 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9103 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009104 }
9105 }
9106
Bram Moolenaare9a41262005-01-15 22:18:47 +00009107 copy_tv(&argvars[0], rettv);
9108 }
9109 }
9110 else
9111 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009112}
9113
9114/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009115 * "feedkeys()" function
9116 */
9117/*ARGSUSED*/
9118 static void
9119f_feedkeys(argvars, rettv)
9120 typval_T *argvars;
9121 typval_T *rettv;
9122{
9123 int remap = TRUE;
9124 char_u *keys, *flags;
9125 char_u nbuf[NUMBUFLEN];
9126 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009127 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009128
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009129 /* This is not allowed in the sandbox. If the commands would still be
9130 * executed in the sandbox it would be OK, but it probably happens later,
9131 * when "sandbox" is no longer set. */
9132 if (check_secure())
9133 return;
9134
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009135 rettv->vval.v_number = 0;
9136 keys = get_tv_string(&argvars[0]);
9137 if (*keys != NUL)
9138 {
9139 if (argvars[1].v_type != VAR_UNKNOWN)
9140 {
9141 flags = get_tv_string_buf(&argvars[1], nbuf);
9142 for ( ; *flags != NUL; ++flags)
9143 {
9144 switch (*flags)
9145 {
9146 case 'n': remap = FALSE; break;
9147 case 'm': remap = TRUE; break;
9148 case 't': typed = TRUE; break;
9149 }
9150 }
9151 }
9152
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009153 /* Need to escape K_SPECIAL and CSI before putting the string in the
9154 * typeahead buffer. */
9155 keys_esc = vim_strsave_escape_csi(keys);
9156 if (keys_esc != NULL)
9157 {
9158 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009159 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009160 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009161 if (vgetc_busy)
9162 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009163 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009164 }
9165}
9166
9167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 * "filereadable()" function
9169 */
9170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 typval_T *argvars;
9173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 FILE *fd;
9176 char_u *p;
9177 int n;
9178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9181 {
9182 n = TRUE;
9183 fclose(fd);
9184 }
9185 else
9186 n = FALSE;
9187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189}
9190
9191/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009192 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 * rights to write into.
9194 */
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009200 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
Bram Moolenaar33570922005-01-25 22:26:29 +00009203static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009204
9205 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009206findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009207 typval_T *argvars;
9208 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009209 int dir;
9210{
9211#ifdef FEAT_SEARCHPATH
9212 char_u *fname;
9213 char_u *fresult = NULL;
9214 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9215 char_u *p;
9216 char_u pathbuf[NUMBUFLEN];
9217 int count = 1;
9218 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009219 int error = FALSE;
9220#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009221
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009222 rettv->vval.v_string = NULL;
9223 rettv->v_type = VAR_STRING;
9224
9225#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009227
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009228 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9231 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009232 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 else
9234 {
9235 if (*p != NUL)
9236 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009239 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009241 }
9242
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009243 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9244 error = TRUE;
9245
9246 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 do
9249 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009250 if (rettv->v_type == VAR_STRING)
9251 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 fresult = find_file_in_path_option(first ? fname : NULL,
9253 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009254 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009255 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009257
9258 if (fresult != NULL && rettv->v_type == VAR_LIST)
9259 list_append_string(rettv->vval.v_list, fresult, -1);
9260
9261 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009263
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009264 if (rettv->v_type == VAR_STRING)
9265 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009266#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009267}
9268
Bram Moolenaar33570922005-01-25 22:26:29 +00009269static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9270static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009271
9272/*
9273 * Implementation of map() and filter().
9274 */
9275 static void
9276filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T *argvars;
9278 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009279 int map;
9280{
9281 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 listitem_T *li, *nli;
9284 list_T *l = NULL;
9285 dictitem_T *di;
9286 hashtab_T *ht;
9287 hashitem_T *hi;
9288 dict_T *d = NULL;
9289 typval_T save_val;
9290 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009291 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009292 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009293 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009294 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009295
9296 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009298 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009299 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009300 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301 return;
9302 }
9303 else if (argvars[0].v_type == VAR_DICT)
9304 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009305 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009306 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 return;
9308 }
9309 else
9310 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009311 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312 return;
9313 }
9314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009315 expr = get_tv_string_buf_chk(&argvars[1], buf);
9316 /* On type errors, the preceding call has already displayed an error
9317 * message. Avoid a misleading error message for an empty string that
9318 * was not passed as argument. */
9319 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 prepare_vimvar(VV_VAL, &save_val);
9322 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009323
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009324 /* We reset "did_emsg" to be able to detect whether an error
9325 * occurred during evaluation of the expression. */
9326 save_did_emsg = did_emsg;
9327 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 prepare_vimvar(VV_KEY, &save_key);
9332 vimvars[VV_KEY].vv_type = VAR_STRING;
9333
9334 ht = &d->dv_hashtab;
9335 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009336 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 if (!HASHITEM_EMPTY(hi))
9340 {
9341 --todo;
9342 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009343 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 break;
9345 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009346 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009347 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 break;
9349 if (!map && rem)
9350 dictitem_remove(d, di);
9351 clear_tv(&vimvars[VV_KEY].vv_tv);
9352 }
9353 }
9354 hash_unlock(ht);
9355
9356 restore_vimvar(VV_KEY, &save_key);
9357 }
9358 else
9359 {
9360 for (li = l->lv_first; li != NULL; li = nli)
9361 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009362 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009363 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009364 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009365 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009366 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009367 break;
9368 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009370 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009371 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009374
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009375 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009377
9378 copy_tv(&argvars[0], rettv);
9379}
9380
9381 static int
9382filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009384 char_u *expr;
9385 int map;
9386 int *remp;
9387{
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389 char_u *s;
9390
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 s = expr;
9393 if (eval1(&s, &rettv, TRUE) == FAIL)
9394 return FAIL;
9395 if (*s != NUL) /* check for trailing chars after expr */
9396 {
9397 EMSG2(_(e_invexpr2), s);
9398 return FAIL;
9399 }
9400 if (map)
9401 {
9402 /* map(): replace the list item value */
9403 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009404 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 *tv = rettv;
9406 }
9407 else
9408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 int error = FALSE;
9410
Bram Moolenaare9a41262005-01-15 22:18:47 +00009411 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009413 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 /* On type error, nothing has been removed; return FAIL to stop the
9415 * loop. The error message was given by get_tv_number_chk(). */
9416 if (error)
9417 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009418 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009420 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009421}
9422
9423/*
9424 * "filter()" function
9425 */
9426 static void
9427f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 typval_T *argvars;
9429 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009430{
9431 filter_map(argvars, rettv, FALSE);
9432}
9433
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009434/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009435 * "finddir({fname}[, {path}[, {count}]])" function
9436 */
9437 static void
9438f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009441{
9442 findfilendir(argvars, rettv, TRUE);
9443}
9444
9445/*
9446 * "findfile({fname}[, {path}[, {count}]])" function
9447 */
9448 static void
9449f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452{
9453 findfilendir(argvars, rettv, FALSE);
9454}
9455
9456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 * "fnamemodify({fname}, {mods})" function
9458 */
9459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009461 typval_T *argvars;
9462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
9464 char_u *fname;
9465 char_u *mods;
9466 int usedlen = 0;
9467 int len;
9468 char_u *fbuf = NULL;
9469 char_u buf[NUMBUFLEN];
9470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 fname = get_tv_string_chk(&argvars[0]);
9472 mods = get_tv_string_buf_chk(&argvars[1], buf);
9473 if (fname == NULL || mods == NULL)
9474 fname = NULL;
9475 else
9476 {
9477 len = (int)STRLEN(fname);
9478 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 vim_free(fbuf);
9487}
9488
Bram Moolenaar33570922005-01-25 22:26:29 +00009489static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490
9491/*
9492 * "foldclosed()" function
9493 */
9494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009496 typval_T *argvars;
9497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 int end;
9499{
9500#ifdef FEAT_FOLDING
9501 linenr_T lnum;
9502 linenr_T first, last;
9503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9506 {
9507 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9508 {
9509 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 return;
9514 }
9515 }
9516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518}
9519
9520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009521 * "foldclosed()" function
9522 */
9523 static void
9524f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009525 typval_T *argvars;
9526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009527{
9528 foldclosed_both(argvars, rettv, FALSE);
9529}
9530
9531/*
9532 * "foldclosedend()" function
9533 */
9534 static void
9535f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009536 typval_T *argvars;
9537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538{
9539 foldclosed_both(argvars, rettv, TRUE);
9540}
9541
9542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 * "foldlevel()" function
9544 */
9545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550#ifdef FEAT_FOLDING
9551 linenr_T lnum;
9552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
9557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561/*
9562 * "foldtext()" function
9563 */
9564/*ARGSUSED*/
9565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570#ifdef FEAT_FOLDING
9571 linenr_T lnum;
9572 char_u *s;
9573 char_u *r;
9574 int len;
9575 char *txt;
9576#endif
9577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->v_type = VAR_STRING;
9579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009581 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9582 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9583 <= curbuf->b_ml.ml_line_count
9584 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 {
9586 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009587 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9588 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 {
9590 if (!linewhite(lnum))
9591 break;
9592 ++lnum;
9593 }
9594
9595 /* Find interesting text in this line. */
9596 s = skipwhite(ml_get(lnum));
9597 /* skip C comment-start */
9598 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009601 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009602 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009603 {
9604 s = skipwhite(ml_get(lnum + 1));
9605 if (*s == '*')
9606 s = skipwhite(s + 1);
9607 }
9608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 txt = _("+-%s%3ld lines: ");
9610 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 + 20 /* for %3ld */
9613 + STRLEN(s))); /* concatenated */
9614 if (r != NULL)
9615 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9617 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9618 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 len = (int)STRLEN(r);
9620 STRCAT(r, s);
9621 /* remove 'foldmarker' and 'commentstring' */
9622 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 }
9625 }
9626#endif
9627}
9628
9629/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009630 * "foldtextresult(lnum)" function
9631 */
9632/*ARGSUSED*/
9633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009637{
9638#ifdef FEAT_FOLDING
9639 linenr_T lnum;
9640 char_u *text;
9641 char_u buf[51];
9642 foldinfo_T foldinfo;
9643 int fold_count;
9644#endif
9645
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 rettv->v_type = VAR_STRING;
9647 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009648#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009650 /* treat illegal types and illegal string values for {lnum} the same */
9651 if (lnum < 0)
9652 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009653 fold_count = foldedCount(curwin, lnum, &foldinfo);
9654 if (fold_count > 0)
9655 {
9656 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9657 &foldinfo, buf);
9658 if (text == buf)
9659 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009661 }
9662#endif
9663}
9664
9665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 * "foreground()" function
9667 */
9668/*ARGSUSED*/
9669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *argvars;
9672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#ifdef FEAT_GUI
9676 if (gui.in_use)
9677 gui_mch_set_foreground();
9678#else
9679# ifdef WIN32
9680 win32_set_foreground();
9681# endif
9682#endif
9683}
9684
9685/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 * "function()" function
9687 */
9688/*ARGSUSED*/
9689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009691 typval_T *argvars;
9692 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693{
9694 char_u *s;
9695
Bram Moolenaara7043832005-01-21 11:56:39 +00009696 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009698 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699 EMSG2(_(e_invarg2), s);
9700 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009701 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 else
9703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_string = vim_strsave(s);
9705 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009706 }
9707}
9708
9709/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009710 * "garbagecollect()" function
9711 */
9712/*ARGSUSED*/
9713 static void
9714f_garbagecollect(argvars, rettv)
9715 typval_T *argvars;
9716 typval_T *rettv;
9717{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009718 /* This is postponed until we are back at the toplevel, because we may be
9719 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9720 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009721}
9722
9723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009724 * "get()" function
9725 */
9726 static void
9727f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009730{
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 listitem_T *li;
9732 list_T *l;
9733 dictitem_T *di;
9734 dict_T *d;
9735 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009736
Bram Moolenaare9a41262005-01-15 22:18:47 +00009737 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009738 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009739 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009741 int error = FALSE;
9742
9743 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9744 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009745 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009746 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009747 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009748 else if (argvars[0].v_type == VAR_DICT)
9749 {
9750 if ((d = argvars[0].vval.v_dict) != NULL)
9751 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009752 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009753 if (di != NULL)
9754 tv = &di->di_tv;
9755 }
9756 }
9757 else
9758 EMSG2(_(e_listdictarg), "get()");
9759
9760 if (tv == NULL)
9761 {
9762 if (argvars[2].v_type == VAR_UNKNOWN)
9763 rettv->vval.v_number = 0;
9764 else
9765 copy_tv(&argvars[2], rettv);
9766 }
9767 else
9768 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009769}
9770
Bram Moolenaar342337a2005-07-21 21:11:17 +00009771static 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 +00009772
9773/*
9774 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009775 * Return a range (from start to end) of lines in rettv from the specified
9776 * buffer.
9777 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009778 */
9779 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009780get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009781 buf_T *buf;
9782 linenr_T start;
9783 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009784 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009785 typval_T *rettv;
9786{
9787 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009788
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009790 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009792 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009793 }
9794 else
9795 rettv->vval.v_number = 0;
9796
9797 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9798 return;
9799
9800 if (!retlist)
9801 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9803 p = ml_get_buf(buf, start, FALSE);
9804 else
9805 p = (char_u *)"";
9806
9807 rettv->v_type = VAR_STRING;
9808 rettv->vval.v_string = vim_strsave(p);
9809 }
9810 else
9811 {
9812 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009813 return;
9814
9815 if (start < 1)
9816 start = 1;
9817 if (end > buf->b_ml.ml_line_count)
9818 end = buf->b_ml.ml_line_count;
9819 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009820 if (list_append_string(rettv->vval.v_list,
9821 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009822 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009823 }
9824}
9825
9826/*
9827 * "getbufline()" function
9828 */
9829 static void
9830f_getbufline(argvars, rettv)
9831 typval_T *argvars;
9832 typval_T *rettv;
9833{
9834 linenr_T lnum;
9835 linenr_T end;
9836 buf_T *buf;
9837
9838 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9839 ++emsg_off;
9840 buf = get_buf_tv(&argvars[0]);
9841 --emsg_off;
9842
Bram Moolenaar661b1822005-07-28 22:36:45 +00009843 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009844 if (argvars[2].v_type == VAR_UNKNOWN)
9845 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009846 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009847 end = get_tv_lnum_buf(&argvars[2], buf);
9848
Bram Moolenaar342337a2005-07-21 21:11:17 +00009849 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009850}
9851
Bram Moolenaar0d660222005-01-07 21:51:51 +00009852/*
9853 * "getbufvar()" function
9854 */
9855 static void
9856f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009857 typval_T *argvars;
9858 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009859{
9860 buf_T *buf;
9861 buf_T *save_curbuf;
9862 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009865 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9866 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009867 ++emsg_off;
9868 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009869
9870 rettv->v_type = VAR_STRING;
9871 rettv->vval.v_string = NULL;
9872
9873 if (buf != NULL && varname != NULL)
9874 {
9875 if (*varname == '&') /* buffer-local-option */
9876 {
9877 /* set curbuf to be our buf, temporarily */
9878 save_curbuf = curbuf;
9879 curbuf = buf;
9880
9881 get_option_tv(&varname, rettv, TRUE);
9882
9883 /* restore previous notion of curbuf */
9884 curbuf = save_curbuf;
9885 }
9886 else
9887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 if (*varname == NUL)
9889 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9890 * scope prefix before the NUL byte is required by
9891 * find_var_in_ht(). */
9892 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009893 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009894 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009895 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009897 }
9898 }
9899
9900 --emsg_off;
9901}
9902
9903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 * "getchar()" function
9905 */
9906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009912 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009914 /* Position the cursor. Needed after a message that ends in a space. */
9915 windgoto(msg_row, msg_col);
9916
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 ++no_mapping;
9918 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009919 for (;;)
9920 {
9921 if (argvars[0].v_type == VAR_UNKNOWN)
9922 /* getchar(): blocking wait. */
9923 n = safe_vgetc();
9924 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9925 /* getchar(1): only check if char avail */
9926 n = vpeekc();
9927 else if (error || vpeekc() == NUL)
9928 /* illegal argument or getchar(0) and no char avail: return zero */
9929 n = 0;
9930 else
9931 /* getchar(0) and char avail: return char */
9932 n = safe_vgetc();
9933 if (n == K_IGNORE)
9934 continue;
9935 break;
9936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 --no_mapping;
9938 --allow_keys;
9939
Bram Moolenaar219b8702006-11-01 14:32:36 +00009940 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9941 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9942 vimvars[VV_MOUSE_COL].vv_nr = 0;
9943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 if (IS_SPECIAL(n) || mod_mask != 0)
9946 {
9947 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9948 int i = 0;
9949
9950 /* Turn a special key into three bytes, plus modifier. */
9951 if (mod_mask != 0)
9952 {
9953 temp[i++] = K_SPECIAL;
9954 temp[i++] = KS_MODIFIER;
9955 temp[i++] = mod_mask;
9956 }
9957 if (IS_SPECIAL(n))
9958 {
9959 temp[i++] = K_SPECIAL;
9960 temp[i++] = K_SECOND(n);
9961 temp[i++] = K_THIRD(n);
9962 }
9963#ifdef FEAT_MBYTE
9964 else if (has_mbyte)
9965 i += (*mb_char2bytes)(n, temp + i);
9966#endif
9967 else
9968 temp[i++] = n;
9969 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 rettv->v_type = VAR_STRING;
9971 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009972
9973#ifdef FEAT_MOUSE
9974 if (n == K_LEFTMOUSE
9975 || n == K_LEFTMOUSE_NM
9976 || n == K_LEFTDRAG
9977 || n == K_LEFTRELEASE
9978 || n == K_LEFTRELEASE_NM
9979 || n == K_MIDDLEMOUSE
9980 || n == K_MIDDLEDRAG
9981 || n == K_MIDDLERELEASE
9982 || n == K_RIGHTMOUSE
9983 || n == K_RIGHTDRAG
9984 || n == K_RIGHTRELEASE
9985 || n == K_X1MOUSE
9986 || n == K_X1DRAG
9987 || n == K_X1RELEASE
9988 || n == K_X2MOUSE
9989 || n == K_X2DRAG
9990 || n == K_X2RELEASE
9991 || n == K_MOUSEDOWN
9992 || n == K_MOUSEUP)
9993 {
9994 int row = mouse_row;
9995 int col = mouse_col;
9996 win_T *win;
9997 linenr_T lnum;
9998# ifdef FEAT_WINDOWS
9999 win_T *wp;
10000# endif
10001 int n = 1;
10002
10003 if (row >= 0 && col >= 0)
10004 {
10005 /* Find the window at the mouse coordinates and compute the
10006 * text position. */
10007 win = mouse_find_win(&row, &col);
10008 (void)mouse_comp_pos(win, &row, &col, &lnum);
10009# ifdef FEAT_WINDOWS
10010 for (wp = firstwin; wp != win; wp = wp->w_next)
10011 ++n;
10012# endif
10013 vimvars[VV_MOUSE_WIN].vv_nr = n;
10014 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10015 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10016 }
10017 }
10018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020}
10021
10022/*
10023 * "getcharmod()" function
10024 */
10025/*ARGSUSED*/
10026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032}
10033
10034/*
10035 * "getcmdline()" function
10036 */
10037/*ARGSUSED*/
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->v_type = VAR_STRING;
10044 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046
10047/*
10048 * "getcmdpos()" function
10049 */
10050/*ARGSUSED*/
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
10059/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010060 * "getcmdtype()" function
10061 */
10062/*ARGSUSED*/
10063 static void
10064f_getcmdtype(argvars, rettv)
10065 typval_T *argvars;
10066 typval_T *rettv;
10067{
10068 rettv->v_type = VAR_STRING;
10069 rettv->vval.v_string = alloc(2);
10070 if (rettv->vval.v_string != NULL)
10071 {
10072 rettv->vval.v_string[0] = get_cmdline_type();
10073 rettv->vval.v_string[1] = NUL;
10074 }
10075}
10076
10077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 * "getcwd()" function
10079 */
10080/*ARGSUSED*/
10081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010083 typval_T *argvars;
10084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086 char_u cwd[MAXPATHL];
10087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 else
10092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010095 if (rettv->vval.v_string != NULL)
10096 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097#endif
10098 }
10099}
10100
10101/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010102 * "getfontname()" function
10103 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010104/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010107 typval_T *argvars;
10108 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->v_type = VAR_STRING;
10111 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010112#ifdef FEAT_GUI
10113 if (gui.in_use)
10114 {
10115 GuiFont font;
10116 char_u *name = NULL;
10117
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010118 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010119 {
10120 /* Get the "Normal" font. Either the name saved by
10121 * hl_set_font_name() or from the font ID. */
10122 font = gui.norm_font;
10123 name = hl_get_font_name();
10124 }
10125 else
10126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010128 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10129 return;
10130 font = gui_mch_get_font(name, FALSE);
10131 if (font == NOFONT)
10132 return; /* Invalid font name, return empty string. */
10133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010135 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010136 gui_mch_free_font(font);
10137 }
10138#endif
10139}
10140
10141/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010142 * "getfperm({fname})" function
10143 */
10144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010146 typval_T *argvars;
10147 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010148{
10149 char_u *fname;
10150 struct stat st;
10151 char_u *perm = NULL;
10152 char_u flags[] = "rwx";
10153 int i;
10154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010158 if (mch_stat((char *)fname, &st) >= 0)
10159 {
10160 perm = vim_strsave((char_u *)"---------");
10161 if (perm != NULL)
10162 {
10163 for (i = 0; i < 9; i++)
10164 {
10165 if (st.st_mode & (1 << (8 - i)))
10166 perm[i] = flags[i % 3];
10167 }
10168 }
10169 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010171}
10172
10173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 * "getfsize({fname})" function
10175 */
10176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010178 typval_T *argvars;
10179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 char_u *fname;
10182 struct stat st;
10183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187
10188 if (mch_stat((char *)fname, &st) >= 0)
10189 {
10190 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010195
10196 /* non-perfect check for overflow */
10197 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10198 rettv->vval.v_number = -2;
10199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 }
10201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203}
10204
10205/*
10206 * "getftime({fname})" function
10207 */
10208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010210 typval_T *argvars;
10211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212{
10213 char_u *fname;
10214 struct stat st;
10215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
10218 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010219 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222}
10223
10224/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010225 * "getftype({fname})" function
10226 */
10227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 typval_T *argvars;
10230 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010231{
10232 char_u *fname;
10233 struct stat st;
10234 char_u *type = NULL;
10235 char *t;
10236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010240 if (mch_lstat((char *)fname, &st) >= 0)
10241 {
10242#ifdef S_ISREG
10243 if (S_ISREG(st.st_mode))
10244 t = "file";
10245 else if (S_ISDIR(st.st_mode))
10246 t = "dir";
10247# ifdef S_ISLNK
10248 else if (S_ISLNK(st.st_mode))
10249 t = "link";
10250# endif
10251# ifdef S_ISBLK
10252 else if (S_ISBLK(st.st_mode))
10253 t = "bdev";
10254# endif
10255# ifdef S_ISCHR
10256 else if (S_ISCHR(st.st_mode))
10257 t = "cdev";
10258# endif
10259# ifdef S_ISFIFO
10260 else if (S_ISFIFO(st.st_mode))
10261 t = "fifo";
10262# endif
10263# ifdef S_ISSOCK
10264 else if (S_ISSOCK(st.st_mode))
10265 t = "fifo";
10266# endif
10267 else
10268 t = "other";
10269#else
10270# ifdef S_IFMT
10271 switch (st.st_mode & S_IFMT)
10272 {
10273 case S_IFREG: t = "file"; break;
10274 case S_IFDIR: t = "dir"; break;
10275# ifdef S_IFLNK
10276 case S_IFLNK: t = "link"; break;
10277# endif
10278# ifdef S_IFBLK
10279 case S_IFBLK: t = "bdev"; break;
10280# endif
10281# ifdef S_IFCHR
10282 case S_IFCHR: t = "cdev"; break;
10283# endif
10284# ifdef S_IFIFO
10285 case S_IFIFO: t = "fifo"; break;
10286# endif
10287# ifdef S_IFSOCK
10288 case S_IFSOCK: t = "socket"; break;
10289# endif
10290 default: t = "other";
10291 }
10292# else
10293 if (mch_isdir(fname))
10294 t = "dir";
10295 else
10296 t = "file";
10297# endif
10298#endif
10299 type = vim_strsave((char_u *)t);
10300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010302}
10303
10304/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010305 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010306 */
10307 static void
10308f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010309 typval_T *argvars;
10310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010311{
10312 linenr_T lnum;
10313 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010314 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010315
10316 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010317 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010318 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010319 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010320 retlist = FALSE;
10321 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010322 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010324 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010325 retlist = TRUE;
10326 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010327
Bram Moolenaar342337a2005-07-21 21:11:17 +000010328 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329}
10330
10331/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010332 * "getmatches()" function
10333 */
10334/*ARGSUSED*/
10335 static void
10336f_getmatches(argvars, rettv)
10337 typval_T *argvars;
10338 typval_T *rettv;
10339{
10340#ifdef FEAT_SEARCH_EXTRA
10341 dict_T *dict;
10342 matchitem_T *cur = curwin->w_match_head;
10343
10344 rettv->vval.v_number = 0;
10345
10346 if (rettv_list_alloc(rettv) == OK)
10347 {
10348 while (cur != NULL)
10349 {
10350 dict = dict_alloc();
10351 if (dict == NULL)
10352 return;
10353 ++dict->dv_refcount;
10354 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10355 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10356 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10357 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10358 list_append_dict(rettv->vval.v_list, dict);
10359 cur = cur->next;
10360 }
10361 }
10362#endif
10363}
10364
10365/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010366 * "getpos(string)" function
10367 */
10368 static void
10369f_getpos(argvars, rettv)
10370 typval_T *argvars;
10371 typval_T *rettv;
10372{
10373 pos_T *fp;
10374 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010375 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010376
10377 if (rettv_list_alloc(rettv) == OK)
10378 {
10379 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010380 fp = var2fpos(&argvars[0], TRUE, &fnum);
10381 if (fnum != -1)
10382 list_append_number(l, (varnumber_T)fnum);
10383 else
10384 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010385 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10386 : (varnumber_T)0);
10387 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10388 : (varnumber_T)0);
10389 list_append_number(l,
10390#ifdef FEAT_VIRTUALEDIT
10391 (fp != NULL) ? (varnumber_T)fp->coladd :
10392#endif
10393 (varnumber_T)0);
10394 }
10395 else
10396 rettv->vval.v_number = FALSE;
10397}
10398
10399/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010400 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010401 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010402/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010403 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010404f_getqflist(argvars, rettv)
10405 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010406 typval_T *rettv;
10407{
10408#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010409 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010410#endif
10411
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010412 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010413#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010414 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010415 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010416 wp = NULL;
10417 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10418 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010419 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010420 if (wp == NULL)
10421 return;
10422 }
10423
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010424 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010425 }
10426#endif
10427}
10428
10429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 * "getreg()" function
10431 */
10432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 typval_T *argvars;
10435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436{
10437 char_u *strregname;
10438 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010439 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010442 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 strregname = get_tv_string_chk(&argvars[0]);
10445 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010446 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010450 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 regname = (strregname == NULL ? '"' : *strregname);
10452 if (regname == 0)
10453 regname = '"';
10454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 rettv->vval.v_string = error ? NULL :
10457 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458}
10459
10460/*
10461 * "getregtype()" function
10462 */
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *argvars;
10466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468 char_u *strregname;
10469 int regname;
10470 char_u buf[NUMBUFLEN + 2];
10471 long reglen = 0;
10472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010473 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 {
10475 strregname = get_tv_string_chk(&argvars[0]);
10476 if (strregname == NULL) /* type error; errmsg already given */
10477 {
10478 rettv->v_type = VAR_STRING;
10479 rettv->vval.v_string = NULL;
10480 return;
10481 }
10482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 else
10484 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010485 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
10487 regname = (strregname == NULL ? '"' : *strregname);
10488 if (regname == 0)
10489 regname = '"';
10490
10491 buf[0] = NUL;
10492 buf[1] = NUL;
10493 switch (get_reg_type(regname, &reglen))
10494 {
10495 case MLINE: buf[0] = 'V'; break;
10496 case MCHAR: buf[0] = 'v'; break;
10497#ifdef FEAT_VISUAL
10498 case MBLOCK:
10499 buf[0] = Ctrl_V;
10500 sprintf((char *)buf + 1, "%ld", reglen + 1);
10501 break;
10502#endif
10503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 rettv->v_type = VAR_STRING;
10505 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506}
10507
10508/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010509 * "gettabwinvar()" function
10510 */
10511 static void
10512f_gettabwinvar(argvars, rettv)
10513 typval_T *argvars;
10514 typval_T *rettv;
10515{
10516 getwinvar(argvars, rettv, 1);
10517}
10518
10519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 * "getwinposx()" function
10521 */
10522/*ARGSUSED*/
10523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529#ifdef FEAT_GUI
10530 if (gui.in_use)
10531 {
10532 int x, y;
10533
10534 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 }
10537#endif
10538}
10539
10540/*
10541 * "getwinposy()" function
10542 */
10543/*ARGSUSED*/
10544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010546 typval_T *argvars;
10547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550#ifdef FEAT_GUI
10551 if (gui.in_use)
10552 {
10553 int x, y;
10554
10555 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 }
10558#endif
10559}
10560
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010561/*
10562 * Find window specifed by "vp" in tabpage "tp".
10563 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010564 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010565find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010566 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010567 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010568{
10569#ifdef FEAT_WINDOWS
10570 win_T *wp;
10571#endif
10572 int nr;
10573
10574 nr = get_tv_number_chk(vp, NULL);
10575
10576#ifdef FEAT_WINDOWS
10577 if (nr < 0)
10578 return NULL;
10579 if (nr == 0)
10580 return curwin;
10581
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010582 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10583 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010584 if (--nr <= 0)
10585 break;
10586 return wp;
10587#else
10588 if (nr == 0 || nr == 1)
10589 return curwin;
10590 return NULL;
10591#endif
10592}
10593
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594/*
10595 * "getwinvar()" function
10596 */
10597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010602 getwinvar(argvars, rettv, 0);
10603}
10604
10605/*
10606 * getwinvar() and gettabwinvar()
10607 */
10608 static void
10609getwinvar(argvars, rettv, off)
10610 typval_T *argvars;
10611 typval_T *rettv;
10612 int off; /* 1 for gettabwinvar() */
10613{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 win_T *win, *oldcurwin;
10615 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010616 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010617 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010619#ifdef FEAT_WINDOWS
10620 if (off == 1)
10621 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10622 else
10623 tp = curtab;
10624#endif
10625 win = find_win_by_nr(&argvars[off], tp);
10626 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 rettv->v_type = VAR_STRING;
10630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631
10632 if (win != NULL && varname != NULL)
10633 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010634 /* Set curwin to be our win, temporarily. Also set curbuf, so
10635 * that we can get buffer-local options. */
10636 oldcurwin = curwin;
10637 curwin = win;
10638 curbuf = win->w_buffer;
10639
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 else
10643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 if (*varname == NUL)
10645 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10646 * scope prefix before the NUL byte is required by
10647 * find_var_in_ht(). */
10648 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010650 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010654
10655 /* restore previous notion of curwin */
10656 curwin = oldcurwin;
10657 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659
10660 --emsg_off;
10661}
10662
10663/*
10664 * "glob()" function
10665 */
10666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *argvars;
10669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671 expand_T xpc;
10672
10673 ExpandInit(&xpc);
10674 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675 rettv->v_type = VAR_STRING;
10676 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678}
10679
10680/*
10681 * "globpath()" function
10682 */
10683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T *argvars;
10686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 if (file == NULL)
10693 rettv->vval.v_string = NULL;
10694 else
10695 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * "has()" function
10700 */
10701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010703 typval_T *argvars;
10704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705{
10706 int i;
10707 char_u *name;
10708 int n = FALSE;
10709 static char *(has_list[]) =
10710 {
10711#ifdef AMIGA
10712 "amiga",
10713# ifdef FEAT_ARP
10714 "arp",
10715# endif
10716#endif
10717#ifdef __BEOS__
10718 "beos",
10719#endif
10720#ifdef MSDOS
10721# ifdef DJGPP
10722 "dos32",
10723# else
10724 "dos16",
10725# endif
10726#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010727#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 "mac",
10729#endif
10730#if defined(MACOS_X_UNIX)
10731 "macunix",
10732#endif
10733#ifdef OS2
10734 "os2",
10735#endif
10736#ifdef __QNX__
10737 "qnx",
10738#endif
10739#ifdef RISCOS
10740 "riscos",
10741#endif
10742#ifdef UNIX
10743 "unix",
10744#endif
10745#ifdef VMS
10746 "vms",
10747#endif
10748#ifdef WIN16
10749 "win16",
10750#endif
10751#ifdef WIN32
10752 "win32",
10753#endif
10754#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10755 "win32unix",
10756#endif
10757#ifdef WIN64
10758 "win64",
10759#endif
10760#ifdef EBCDIC
10761 "ebcdic",
10762#endif
10763#ifndef CASE_INSENSITIVE_FILENAME
10764 "fname_case",
10765#endif
10766#ifdef FEAT_ARABIC
10767 "arabic",
10768#endif
10769#ifdef FEAT_AUTOCMD
10770 "autocmd",
10771#endif
10772#ifdef FEAT_BEVAL
10773 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010774# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10775 "balloon_multiline",
10776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777#endif
10778#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10779 "builtin_terms",
10780# ifdef ALL_BUILTIN_TCAPS
10781 "all_builtin_terms",
10782# endif
10783#endif
10784#ifdef FEAT_BYTEOFF
10785 "byte_offset",
10786#endif
10787#ifdef FEAT_CINDENT
10788 "cindent",
10789#endif
10790#ifdef FEAT_CLIENTSERVER
10791 "clientserver",
10792#endif
10793#ifdef FEAT_CLIPBOARD
10794 "clipboard",
10795#endif
10796#ifdef FEAT_CMDL_COMPL
10797 "cmdline_compl",
10798#endif
10799#ifdef FEAT_CMDHIST
10800 "cmdline_hist",
10801#endif
10802#ifdef FEAT_COMMENTS
10803 "comments",
10804#endif
10805#ifdef FEAT_CRYPT
10806 "cryptv",
10807#endif
10808#ifdef FEAT_CSCOPE
10809 "cscope",
10810#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010811#ifdef CURSOR_SHAPE
10812 "cursorshape",
10813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814#ifdef DEBUG
10815 "debug",
10816#endif
10817#ifdef FEAT_CON_DIALOG
10818 "dialog_con",
10819#endif
10820#ifdef FEAT_GUI_DIALOG
10821 "dialog_gui",
10822#endif
10823#ifdef FEAT_DIFF
10824 "diff",
10825#endif
10826#ifdef FEAT_DIGRAPHS
10827 "digraphs",
10828#endif
10829#ifdef FEAT_DND
10830 "dnd",
10831#endif
10832#ifdef FEAT_EMACS_TAGS
10833 "emacs_tags",
10834#endif
10835 "eval", /* always present, of course! */
10836#ifdef FEAT_EX_EXTRA
10837 "ex_extra",
10838#endif
10839#ifdef FEAT_SEARCH_EXTRA
10840 "extra_search",
10841#endif
10842#ifdef FEAT_FKMAP
10843 "farsi",
10844#endif
10845#ifdef FEAT_SEARCHPATH
10846 "file_in_path",
10847#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010848#if defined(UNIX) && !defined(USE_SYSTEM)
10849 "filterpipe",
10850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851#ifdef FEAT_FIND_ID
10852 "find_in_path",
10853#endif
10854#ifdef FEAT_FOLDING
10855 "folding",
10856#endif
10857#ifdef FEAT_FOOTER
10858 "footer",
10859#endif
10860#if !defined(USE_SYSTEM) && defined(UNIX)
10861 "fork",
10862#endif
10863#ifdef FEAT_GETTEXT
10864 "gettext",
10865#endif
10866#ifdef FEAT_GUI
10867 "gui",
10868#endif
10869#ifdef FEAT_GUI_ATHENA
10870# ifdef FEAT_GUI_NEXTAW
10871 "gui_neXtaw",
10872# else
10873 "gui_athena",
10874# endif
10875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876#ifdef FEAT_GUI_GTK
10877 "gui_gtk",
10878# ifdef HAVE_GTK2
10879 "gui_gtk2",
10880# endif
10881#endif
10882#ifdef FEAT_GUI_MAC
10883 "gui_mac",
10884#endif
10885#ifdef FEAT_GUI_MOTIF
10886 "gui_motif",
10887#endif
10888#ifdef FEAT_GUI_PHOTON
10889 "gui_photon",
10890#endif
10891#ifdef FEAT_GUI_W16
10892 "gui_win16",
10893#endif
10894#ifdef FEAT_GUI_W32
10895 "gui_win32",
10896#endif
10897#ifdef FEAT_HANGULIN
10898 "hangul_input",
10899#endif
10900#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10901 "iconv",
10902#endif
10903#ifdef FEAT_INS_EXPAND
10904 "insert_expand",
10905#endif
10906#ifdef FEAT_JUMPLIST
10907 "jumplist",
10908#endif
10909#ifdef FEAT_KEYMAP
10910 "keymap",
10911#endif
10912#ifdef FEAT_LANGMAP
10913 "langmap",
10914#endif
10915#ifdef FEAT_LIBCALL
10916 "libcall",
10917#endif
10918#ifdef FEAT_LINEBREAK
10919 "linebreak",
10920#endif
10921#ifdef FEAT_LISP
10922 "lispindent",
10923#endif
10924#ifdef FEAT_LISTCMDS
10925 "listcmds",
10926#endif
10927#ifdef FEAT_LOCALMAP
10928 "localmap",
10929#endif
10930#ifdef FEAT_MENU
10931 "menu",
10932#endif
10933#ifdef FEAT_SESSION
10934 "mksession",
10935#endif
10936#ifdef FEAT_MODIFY_FNAME
10937 "modify_fname",
10938#endif
10939#ifdef FEAT_MOUSE
10940 "mouse",
10941#endif
10942#ifdef FEAT_MOUSESHAPE
10943 "mouseshape",
10944#endif
10945#if defined(UNIX) || defined(VMS)
10946# ifdef FEAT_MOUSE_DEC
10947 "mouse_dec",
10948# endif
10949# ifdef FEAT_MOUSE_GPM
10950 "mouse_gpm",
10951# endif
10952# ifdef FEAT_MOUSE_JSB
10953 "mouse_jsbterm",
10954# endif
10955# ifdef FEAT_MOUSE_NET
10956 "mouse_netterm",
10957# endif
10958# ifdef FEAT_MOUSE_PTERM
10959 "mouse_pterm",
10960# endif
10961# ifdef FEAT_MOUSE_XTERM
10962 "mouse_xterm",
10963# endif
10964#endif
10965#ifdef FEAT_MBYTE
10966 "multi_byte",
10967#endif
10968#ifdef FEAT_MBYTE_IME
10969 "multi_byte_ime",
10970#endif
10971#ifdef FEAT_MULTI_LANG
10972 "multi_lang",
10973#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010974#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010975#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010976 "mzscheme",
10977#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979#ifdef FEAT_OLE
10980 "ole",
10981#endif
10982#ifdef FEAT_OSFILETYPE
10983 "osfiletype",
10984#endif
10985#ifdef FEAT_PATH_EXTRA
10986 "path_extra",
10987#endif
10988#ifdef FEAT_PERL
10989#ifndef DYNAMIC_PERL
10990 "perl",
10991#endif
10992#endif
10993#ifdef FEAT_PYTHON
10994#ifndef DYNAMIC_PYTHON
10995 "python",
10996#endif
10997#endif
10998#ifdef FEAT_POSTSCRIPT
10999 "postscript",
11000#endif
11001#ifdef FEAT_PRINTER
11002 "printer",
11003#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011004#ifdef FEAT_PROFILE
11005 "profile",
11006#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011007#ifdef FEAT_RELTIME
11008 "reltime",
11009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010#ifdef FEAT_QUICKFIX
11011 "quickfix",
11012#endif
11013#ifdef FEAT_RIGHTLEFT
11014 "rightleft",
11015#endif
11016#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11017 "ruby",
11018#endif
11019#ifdef FEAT_SCROLLBIND
11020 "scrollbind",
11021#endif
11022#ifdef FEAT_CMDL_INFO
11023 "showcmd",
11024 "cmdline_info",
11025#endif
11026#ifdef FEAT_SIGNS
11027 "signs",
11028#endif
11029#ifdef FEAT_SMARTINDENT
11030 "smartindent",
11031#endif
11032#ifdef FEAT_SNIFF
11033 "sniff",
11034#endif
11035#ifdef FEAT_STL_OPT
11036 "statusline",
11037#endif
11038#ifdef FEAT_SUN_WORKSHOP
11039 "sun_workshop",
11040#endif
11041#ifdef FEAT_NETBEANS_INTG
11042 "netbeans_intg",
11043#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011044#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011045 "spell",
11046#endif
11047#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 "syntax",
11049#endif
11050#if defined(USE_SYSTEM) || !defined(UNIX)
11051 "system",
11052#endif
11053#ifdef FEAT_TAG_BINS
11054 "tag_binary",
11055#endif
11056#ifdef FEAT_TAG_OLDSTATIC
11057 "tag_old_static",
11058#endif
11059#ifdef FEAT_TAG_ANYWHITE
11060 "tag_any_white",
11061#endif
11062#ifdef FEAT_TCL
11063# ifndef DYNAMIC_TCL
11064 "tcl",
11065# endif
11066#endif
11067#ifdef TERMINFO
11068 "terminfo",
11069#endif
11070#ifdef FEAT_TERMRESPONSE
11071 "termresponse",
11072#endif
11073#ifdef FEAT_TEXTOBJ
11074 "textobjects",
11075#endif
11076#ifdef HAVE_TGETENT
11077 "tgetent",
11078#endif
11079#ifdef FEAT_TITLE
11080 "title",
11081#endif
11082#ifdef FEAT_TOOLBAR
11083 "toolbar",
11084#endif
11085#ifdef FEAT_USR_CMDS
11086 "user-commands", /* was accidentally included in 5.4 */
11087 "user_commands",
11088#endif
11089#ifdef FEAT_VIMINFO
11090 "viminfo",
11091#endif
11092#ifdef FEAT_VERTSPLIT
11093 "vertsplit",
11094#endif
11095#ifdef FEAT_VIRTUALEDIT
11096 "virtualedit",
11097#endif
11098#ifdef FEAT_VISUAL
11099 "visual",
11100#endif
11101#ifdef FEAT_VISUALEXTRA
11102 "visualextra",
11103#endif
11104#ifdef FEAT_VREPLACE
11105 "vreplace",
11106#endif
11107#ifdef FEAT_WILDIGN
11108 "wildignore",
11109#endif
11110#ifdef FEAT_WILDMENU
11111 "wildmenu",
11112#endif
11113#ifdef FEAT_WINDOWS
11114 "windows",
11115#endif
11116#ifdef FEAT_WAK
11117 "winaltkeys",
11118#endif
11119#ifdef FEAT_WRITEBACKUP
11120 "writebackup",
11121#endif
11122#ifdef FEAT_XIM
11123 "xim",
11124#endif
11125#ifdef FEAT_XFONTSET
11126 "xfontset",
11127#endif
11128#ifdef USE_XSMP
11129 "xsmp",
11130#endif
11131#ifdef USE_XSMP_INTERACT
11132 "xsmp_interact",
11133#endif
11134#ifdef FEAT_XCLIPBOARD
11135 "xterm_clipboard",
11136#endif
11137#ifdef FEAT_XTERM_SAVE
11138 "xterm_save",
11139#endif
11140#if defined(UNIX) && defined(FEAT_X11)
11141 "X11",
11142#endif
11143 NULL
11144 };
11145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 for (i = 0; has_list[i] != NULL; ++i)
11148 if (STRICMP(name, has_list[i]) == 0)
11149 {
11150 n = TRUE;
11151 break;
11152 }
11153
11154 if (n == FALSE)
11155 {
11156 if (STRNICMP(name, "patch", 5) == 0)
11157 n = has_patch(atoi((char *)name + 5));
11158 else if (STRICMP(name, "vim_starting") == 0)
11159 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011160#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11161 else if (STRICMP(name, "balloon_multiline") == 0)
11162 n = multiline_balloon_available();
11163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164#ifdef DYNAMIC_TCL
11165 else if (STRICMP(name, "tcl") == 0)
11166 n = tcl_enabled(FALSE);
11167#endif
11168#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11169 else if (STRICMP(name, "iconv") == 0)
11170 n = iconv_enabled(FALSE);
11171#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011172#ifdef DYNAMIC_MZSCHEME
11173 else if (STRICMP(name, "mzscheme") == 0)
11174 n = mzscheme_enabled(FALSE);
11175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176#ifdef DYNAMIC_RUBY
11177 else if (STRICMP(name, "ruby") == 0)
11178 n = ruby_enabled(FALSE);
11179#endif
11180#ifdef DYNAMIC_PYTHON
11181 else if (STRICMP(name, "python") == 0)
11182 n = python_enabled(FALSE);
11183#endif
11184#ifdef DYNAMIC_PERL
11185 else if (STRICMP(name, "perl") == 0)
11186 n = perl_enabled(FALSE);
11187#endif
11188#ifdef FEAT_GUI
11189 else if (STRICMP(name, "gui_running") == 0)
11190 n = (gui.in_use || gui.starting);
11191# ifdef FEAT_GUI_W32
11192 else if (STRICMP(name, "gui_win32s") == 0)
11193 n = gui_is_win32s();
11194# endif
11195# ifdef FEAT_BROWSE
11196 else if (STRICMP(name, "browse") == 0)
11197 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11198# endif
11199#endif
11200#ifdef FEAT_SYN_HL
11201 else if (STRICMP(name, "syntax_items") == 0)
11202 n = syntax_present(curbuf);
11203#endif
11204#if defined(WIN3264)
11205 else if (STRICMP(name, "win95") == 0)
11206 n = mch_windows95();
11207#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011208#ifdef FEAT_NETBEANS_INTG
11209 else if (STRICMP(name, "netbeans_enabled") == 0)
11210 n = usingNetbeans;
11211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 }
11213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215}
11216
11217/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011218 * "has_key()" function
11219 */
11220 static void
11221f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011222 typval_T *argvars;
11223 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224{
11225 rettv->vval.v_number = 0;
11226 if (argvars[0].v_type != VAR_DICT)
11227 {
11228 EMSG(_(e_dictreq));
11229 return;
11230 }
11231 if (argvars[0].vval.v_dict == NULL)
11232 return;
11233
11234 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011235 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236}
11237
11238/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011239 * "haslocaldir()" function
11240 */
11241/*ARGSUSED*/
11242 static void
11243f_haslocaldir(argvars, rettv)
11244 typval_T *argvars;
11245 typval_T *rettv;
11246{
11247 rettv->vval.v_number = (curwin->w_localdir != NULL);
11248}
11249
11250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 * "hasmapto()" function
11252 */
11253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011255 typval_T *argvars;
11256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
11258 char_u *name;
11259 char_u *mode;
11260 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011261 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 mode = (char_u *)"nvo";
11266 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011269 if (argvars[2].v_type != VAR_UNKNOWN)
11270 abbr = get_tv_number(&argvars[2]);
11271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaar2c932302006-03-18 21:42:09 +000011273 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277}
11278
11279/*
11280 * "histadd()" function
11281 */
11282/*ARGSUSED*/
11283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
11288#ifdef FEAT_CMDHIST
11289 int histype;
11290 char_u *str;
11291 char_u buf[NUMBUFLEN];
11292#endif
11293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 if (check_restricted() || check_secure())
11296 return;
11297#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11299 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 if (histype >= 0)
11301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 if (*str != NUL)
11304 {
11305 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 return;
11308 }
11309 }
11310#endif
11311}
11312
11313/*
11314 * "histdel()" function
11315 */
11316/*ARGSUSED*/
11317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322#ifdef FEAT_CMDHIST
11323 int n;
11324 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11328 if (str == NULL)
11329 n = 0;
11330 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011333 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 else
11338 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 get_tv_string_buf(&argvars[1], buf));
11341 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344#endif
11345}
11346
11347/*
11348 * "histget()" function
11349 */
11350/*ARGSUSED*/
11351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *argvars;
11354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355{
11356#ifdef FEAT_CMDHIST
11357 int type;
11358 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11362 if (str == NULL)
11363 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 {
11366 type = get_histtype(str);
11367 if (argvars[1].v_type == VAR_UNKNOWN)
11368 idx = get_history_idx(type);
11369 else
11370 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11371 /* -1 on type error */
11372 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378}
11379
11380/*
11381 * "histnr()" function
11382 */
11383/*ARGSUSED*/
11384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011386 typval_T *argvars;
11387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
11389 int i;
11390
11391#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 char_u *history = get_tv_string_chk(&argvars[0]);
11393
11394 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 if (i >= HIST_CMD && i < HIST_COUNT)
11396 i = get_history_idx(i);
11397 else
11398#endif
11399 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
11403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 * "highlightID(name)" function
11405 */
11406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011408 typval_T *argvars;
11409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412}
11413
11414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011415 * "highlight_exists()" function
11416 */
11417 static void
11418f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011419 typval_T *argvars;
11420 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011421{
11422 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11423}
11424
11425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 * "hostname()" function
11427 */
11428/*ARGSUSED*/
11429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *argvars;
11432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433{
11434 char_u hostname[256];
11435
11436 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->v_type = VAR_STRING;
11438 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439}
11440
11441/*
11442 * iconv() function
11443 */
11444/*ARGSUSED*/
11445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011447 typval_T *argvars;
11448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449{
11450#ifdef FEAT_MBYTE
11451 char_u buf1[NUMBUFLEN];
11452 char_u buf2[NUMBUFLEN];
11453 char_u *from, *to, *str;
11454 vimconv_T vimconv;
11455#endif
11456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->v_type = VAR_STRING;
11458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459
11460#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 str = get_tv_string(&argvars[0]);
11462 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11463 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 vimconv.vc_type = CONV_NONE;
11465 convert_setup(&vimconv, from, to);
11466
11467 /* If the encodings are equal, no conversion needed. */
11468 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472
11473 convert_setup(&vimconv, NULL, NULL);
11474 vim_free(from);
11475 vim_free(to);
11476#endif
11477}
11478
11479/*
11480 * "indent()" function
11481 */
11482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 typval_T *argvars;
11485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
11487 linenr_T lnum;
11488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011496/*
11497 * "index()" function
11498 */
11499 static void
11500f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011503{
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 list_T *l;
11505 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011506 long idx = 0;
11507 int ic = FALSE;
11508
11509 rettv->vval.v_number = -1;
11510 if (argvars[0].v_type != VAR_LIST)
11511 {
11512 EMSG(_(e_listreq));
11513 return;
11514 }
11515 l = argvars[0].vval.v_list;
11516 if (l != NULL)
11517 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011518 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011519 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 int error = FALSE;
11522
Bram Moolenaar758711c2005-02-02 23:11:38 +000011523 /* Start at specified item. Use the cached index that list_find()
11524 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011526 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011527 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 ic = get_tv_number_chk(&argvars[3], &error);
11529 if (error)
11530 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011531 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011532
Bram Moolenaar758711c2005-02-02 23:11:38 +000011533 for ( ; item != NULL; item = item->li_next, ++idx)
11534 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011535 {
11536 rettv->vval.v_number = idx;
11537 break;
11538 }
11539 }
11540}
11541
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542static int inputsecret_flag = 0;
11543
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011544static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11545
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011547 * This function is used by f_input() and f_inputdialog() functions. The third
11548 * argument to f_input() specifies the type of completion to use at the
11549 * prompt. The third argument to f_inputdialog() specifies the value to return
11550 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 */
11552 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011553get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011554 typval_T *argvars;
11555 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011556 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011558 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 char_u *p = NULL;
11560 int c;
11561 char_u buf[NUMBUFLEN];
11562 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011563 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011564 int xp_type = EXPAND_NOTHING;
11565 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569
11570#ifdef NO_CONSOLE_INPUT
11571 /* While starting up, there is no place to enter text. */
11572 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574#endif
11575
11576 cmd_silent = FALSE; /* Want to see the prompt. */
11577 if (prompt != NULL)
11578 {
11579 /* Only the part of the message after the last NL is considered as
11580 * prompt for the command line */
11581 p = vim_strrchr(prompt, '\n');
11582 if (p == NULL)
11583 p = prompt;
11584 else
11585 {
11586 ++p;
11587 c = *p;
11588 *p = NUL;
11589 msg_start();
11590 msg_clr_eos();
11591 msg_puts_attr(prompt, echo_attr);
11592 msg_didout = FALSE;
11593 msg_starthere();
11594 *p = c;
11595 }
11596 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011598 if (argvars[1].v_type != VAR_UNKNOWN)
11599 {
11600 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11601 if (defstr != NULL)
11602 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011604 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011605 {
11606 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011607 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011608 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011609
Bram Moolenaar4463f292005-09-25 22:20:24 +000011610 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011611
Bram Moolenaar4463f292005-09-25 22:20:24 +000011612 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11613 if (xp_name == NULL)
11614 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011616 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011617
Bram Moolenaar4463f292005-09-25 22:20:24 +000011618 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11619 &xp_arg) == FAIL)
11620 return;
11621 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011622 }
11623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 if (defstr != NULL)
11625 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011626 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11627 xp_type, xp_arg);
11628
11629 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 /* since the user typed this, no need to wait for return */
11632 need_wait_return = FALSE;
11633 msg_didout = FALSE;
11634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 cmd_silent = cmd_silent_save;
11636}
11637
11638/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011639 * "input()" function
11640 * Also handles inputsecret() when inputsecret is set.
11641 */
11642 static void
11643f_input(argvars, rettv)
11644 typval_T *argvars;
11645 typval_T *rettv;
11646{
11647 get_user_input(argvars, rettv, FALSE);
11648}
11649
11650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 * "inputdialog()" function
11652 */
11653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657{
11658#if defined(FEAT_GUI_TEXTDIALOG)
11659 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11660 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11661 {
11662 char_u *message;
11663 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011666 message = get_tv_string_chk(&argvars[0]);
11667 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011668 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011669 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 else
11671 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 if (message != NULL && defstr != NULL
11673 && do_dialog(VIM_QUESTION, NULL, message,
11674 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 else
11677 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 if (message != NULL && defstr != NULL
11679 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011680 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_string = vim_strsave(
11682 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 }
11688 else
11689#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011690 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691}
11692
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011693/*
11694 * "inputlist()" function
11695 */
11696 static void
11697f_inputlist(argvars, rettv)
11698 typval_T *argvars;
11699 typval_T *rettv;
11700{
11701 listitem_T *li;
11702 int selected;
11703 int mouse_used;
11704
11705 rettv->vval.v_number = 0;
11706#ifdef NO_CONSOLE_INPUT
11707 /* While starting up, there is no place to enter text. */
11708 if (no_console_input())
11709 return;
11710#endif
11711 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11712 {
11713 EMSG2(_(e_listarg), "inputlist()");
11714 return;
11715 }
11716
11717 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011718 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011719 lines_left = Rows; /* avoid more prompt */
11720 msg_scroll = TRUE;
11721 msg_clr_eos();
11722
11723 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11724 {
11725 msg_puts(get_tv_string(&li->li_tv));
11726 msg_putchar('\n');
11727 }
11728
11729 /* Ask for choice. */
11730 selected = prompt_for_number(&mouse_used);
11731 if (mouse_used)
11732 selected -= lines_left;
11733
11734 rettv->vval.v_number = selected;
11735}
11736
11737
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11739
11740/*
11741 * "inputrestore()" function
11742 */
11743/*ARGSUSED*/
11744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011745f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011746 typval_T *argvars;
11747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748{
11749 if (ga_userinput.ga_len > 0)
11750 {
11751 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11753 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 }
11756 else if (p_verbose > 1)
11757 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011758 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 }
11761}
11762
11763/*
11764 * "inputsave()" function
11765 */
11766/*ARGSUSED*/
11767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011768f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011769 typval_T *argvars;
11770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771{
11772 /* Add an entry to the stack of typehead storage. */
11773 if (ga_grow(&ga_userinput, 1) == OK)
11774 {
11775 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11776 + ga_userinput.ga_len);
11777 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 }
11780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782}
11783
11784/*
11785 * "inputsecret()" function
11786 */
11787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
11792 ++cmdline_star;
11793 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 --cmdline_star;
11796 --inputsecret_flag;
11797}
11798
11799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011800 * "insert()" function
11801 */
11802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011806{
11807 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011808 listitem_T *item;
11809 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011812 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011815 else if ((l = argvars[0].vval.v_list) != NULL
11816 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 {
11818 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 before = get_tv_number_chk(&argvars[2], &error);
11820 if (error)
11821 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011822
Bram Moolenaar758711c2005-02-02 23:11:38 +000011823 if (before == l->lv_len)
11824 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011825 else
11826 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011827 item = list_find(l, before);
11828 if (item == NULL)
11829 {
11830 EMSGN(_(e_listidx), before);
11831 l = NULL;
11832 }
11833 }
11834 if (l != NULL)
11835 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011836 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011837 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011838 }
11839 }
11840}
11841
11842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 * "isdirectory()" function
11844 */
11845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *argvars;
11848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851}
11852
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011853/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011854 * "islocked()" function
11855 */
11856 static void
11857f_islocked(argvars, rettv)
11858 typval_T *argvars;
11859 typval_T *rettv;
11860{
11861 lval_T lv;
11862 char_u *end;
11863 dictitem_T *di;
11864
11865 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011866 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11867 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011868 if (end != NULL && lv.ll_name != NULL)
11869 {
11870 if (*end != NUL)
11871 EMSG(_(e_trailing));
11872 else
11873 {
11874 if (lv.ll_tv == NULL)
11875 {
11876 if (check_changedtick(lv.ll_name))
11877 rettv->vval.v_number = 1; /* always locked */
11878 else
11879 {
11880 di = find_var(lv.ll_name, NULL);
11881 if (di != NULL)
11882 {
11883 /* Consider a variable locked when:
11884 * 1. the variable itself is locked
11885 * 2. the value of the variable is locked.
11886 * 3. the List or Dict value is locked.
11887 */
11888 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11889 || tv_islocked(&di->di_tv));
11890 }
11891 }
11892 }
11893 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011894 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011895 else if (lv.ll_newkey != NULL)
11896 EMSG2(_(e_dictkey), lv.ll_newkey);
11897 else if (lv.ll_list != NULL)
11898 /* List item. */
11899 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11900 else
11901 /* Dictionary item. */
11902 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11903 }
11904 }
11905
11906 clear_lval(&lv);
11907}
11908
Bram Moolenaar33570922005-01-25 22:26:29 +000011909static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011910
11911/*
11912 * Turn a dict into a list:
11913 * "what" == 0: list of keys
11914 * "what" == 1: list of values
11915 * "what" == 2: list of items
11916 */
11917 static void
11918dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011921 int what;
11922{
Bram Moolenaar33570922005-01-25 22:26:29 +000011923 list_T *l2;
11924 dictitem_T *di;
11925 hashitem_T *hi;
11926 listitem_T *li;
11927 listitem_T *li2;
11928 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011929 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011930
11931 rettv->vval.v_number = 0;
11932 if (argvars[0].v_type != VAR_DICT)
11933 {
11934 EMSG(_(e_dictreq));
11935 return;
11936 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011937 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011938 return;
11939
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011940 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011941 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011943 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011946 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011947 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011948 --todo;
11949 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011951 li = listitem_alloc();
11952 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011953 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011954 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011955
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011956 if (what == 0)
11957 {
11958 /* keys() */
11959 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011960 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011961 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11962 }
11963 else if (what == 1)
11964 {
11965 /* values() */
11966 copy_tv(&di->di_tv, &li->li_tv);
11967 }
11968 else
11969 {
11970 /* items() */
11971 l2 = list_alloc();
11972 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011973 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011974 li->li_tv.vval.v_list = l2;
11975 if (l2 == NULL)
11976 break;
11977 ++l2->lv_refcount;
11978
11979 li2 = listitem_alloc();
11980 if (li2 == NULL)
11981 break;
11982 list_append(l2, li2);
11983 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011984 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011985 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11986
11987 li2 = listitem_alloc();
11988 if (li2 == NULL)
11989 break;
11990 list_append(l2, li2);
11991 copy_tv(&di->di_tv, &li2->li_tv);
11992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011993 }
11994 }
11995}
11996
11997/*
11998 * "items(dict)" function
11999 */
12000 static void
12001f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012004{
12005 dict_list(argvars, rettv, 2);
12006}
12007
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012009 * "join()" function
12010 */
12011 static void
12012f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 typval_T *argvars;
12014 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012015{
12016 garray_T ga;
12017 char_u *sep;
12018
12019 rettv->vval.v_number = 0;
12020 if (argvars[0].v_type != VAR_LIST)
12021 {
12022 EMSG(_(e_listreq));
12023 return;
12024 }
12025 if (argvars[0].vval.v_list == NULL)
12026 return;
12027 if (argvars[1].v_type == VAR_UNKNOWN)
12028 sep = (char_u *)" ";
12029 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012031
12032 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012033
12034 if (sep != NULL)
12035 {
12036 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012037 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012038 ga_append(&ga, NUL);
12039 rettv->vval.v_string = (char_u *)ga.ga_data;
12040 }
12041 else
12042 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012043}
12044
12045/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012046 * "keys()" function
12047 */
12048 static void
12049f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012050 typval_T *argvars;
12051 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012052{
12053 dict_list(argvars, rettv, 0);
12054}
12055
12056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 * "last_buffer_nr()" function.
12058 */
12059/*ARGSUSED*/
12060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 typval_T *argvars;
12063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064{
12065 int n = 0;
12066 buf_T *buf;
12067
12068 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12069 if (n < buf->b_fnum)
12070 n = buf->b_fnum;
12071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012073}
12074
12075/*
12076 * "len()" function
12077 */
12078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012080 typval_T *argvars;
12081 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012082{
12083 switch (argvars[0].v_type)
12084 {
12085 case VAR_STRING:
12086 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = (varnumber_T)STRLEN(
12088 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012089 break;
12090 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012092 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012093 case VAR_DICT:
12094 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12095 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012096 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012097 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012098 break;
12099 }
12100}
12101
Bram Moolenaar33570922005-01-25 22:26:29 +000012102static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012103
12104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012108 int type;
12109{
12110#ifdef FEAT_LIBCALL
12111 char_u *string_in;
12112 char_u **string_result;
12113 int nr_result;
12114#endif
12115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012117 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012121
12122 if (check_restricted() || check_secure())
12123 return;
12124
12125#ifdef FEAT_LIBCALL
12126 /* The first two args must be strings, otherwise its meaningless */
12127 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12128 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012129 string_in = NULL;
12130 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012131 string_in = argvars[2].vval.v_string;
12132 if (type == VAR_NUMBER)
12133 string_result = NULL;
12134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012136 if (mch_libcall(argvars[0].vval.v_string,
12137 argvars[1].vval.v_string,
12138 string_in,
12139 argvars[2].vval.v_number,
12140 string_result,
12141 &nr_result) == OK
12142 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012144 }
12145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146}
12147
12148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012149 * "libcall()" function
12150 */
12151 static void
12152f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012153 typval_T *argvars;
12154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155{
12156 libcall_common(argvars, rettv, VAR_STRING);
12157}
12158
12159/*
12160 * "libcallnr()" function
12161 */
12162 static void
12163f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012164 typval_T *argvars;
12165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012166{
12167 libcall_common(argvars, rettv, VAR_NUMBER);
12168}
12169
12170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 * "line(string)" function
12172 */
12173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012175 typval_T *argvars;
12176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177{
12178 linenr_T lnum = 0;
12179 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012180 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012182 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 if (fp != NULL)
12184 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186}
12187
12188/*
12189 * "line2byte(lnum)" function
12190 */
12191/*ARGSUSED*/
12192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 typval_T *argvars;
12195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196{
12197#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199#else
12200 linenr_T lnum;
12201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12207 if (rettv->vval.v_number >= 0)
12208 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209#endif
12210}
12211
12212/*
12213 * "lispindent(lnum)" function
12214 */
12215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012217 typval_T *argvars;
12218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219{
12220#ifdef FEAT_LISP
12221 pos_T pos;
12222 linenr_T lnum;
12223
12224 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12227 {
12228 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 curwin->w_cursor = pos;
12231 }
12232 else
12233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
12238 * "localtime()" function
12239 */
12240/*ARGSUSED*/
12241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 typval_T *argvars;
12244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247}
12248
Bram Moolenaar33570922005-01-25 22:26:29 +000012249static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250
12251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 typval_T *argvars;
12254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 int exact;
12256{
12257 char_u *keys;
12258 char_u *which;
12259 char_u buf[NUMBUFLEN];
12260 char_u *keys_buf = NULL;
12261 char_u *rhs;
12262 int mode;
12263 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012264 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265
12266 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->v_type = VAR_STRING;
12268 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 if (*keys == NUL)
12272 return;
12273
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012277 if (argvars[2].v_type != VAR_UNKNOWN)
12278 abbr = get_tv_number(&argvars[2]);
12279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 else
12281 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012282 if (which == NULL)
12283 return;
12284
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 mode = get_map_mode(&which, 0);
12286
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012287 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012288 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 vim_free(keys_buf);
12290 if (rhs != NULL)
12291 {
12292 ga_init(&ga);
12293 ga.ga_itemsize = 1;
12294 ga.ga_growsize = 40;
12295
12296 while (*rhs != NUL)
12297 ga_concat(&ga, str2special(&rhs, FALSE));
12298
12299 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 }
12302}
12303
12304/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012305 * "map()" function
12306 */
12307 static void
12308f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *argvars;
12310 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012311{
12312 filter_map(argvars, rettv, TRUE);
12313}
12314
12315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012316 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317 */
12318 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012320 typval_T *argvars;
12321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012323 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324}
12325
12326/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012327 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 */
12329 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 typval_T *argvars;
12332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012334 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335}
12336
Bram Moolenaar33570922005-01-25 22:26:29 +000012337static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338
12339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012341 typval_T *argvars;
12342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 int type;
12344{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012345 char_u *str = NULL;
12346 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 char_u *pat;
12348 regmatch_T regmatch;
12349 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012350 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 char_u *save_cpo;
12352 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012353 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012354 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012355 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012356 list_T *l = NULL;
12357 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012358 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012359 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360
12361 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12362 save_cpo = p_cpo;
12363 p_cpo = (char_u *)"";
12364
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012365 rettv->vval.v_number = -1;
12366 if (type == 3)
12367 {
12368 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012370 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012371 }
12372 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 rettv->v_type = VAR_STRING;
12375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012378 if (argvars[0].v_type == VAR_LIST)
12379 {
12380 if ((l = argvars[0].vval.v_list) == NULL)
12381 goto theend;
12382 li = l->lv_first;
12383 }
12384 else
12385 expr = str = get_tv_string(&argvars[0]);
12386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12388 if (pat == NULL)
12389 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012390
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012391 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012393 int error = FALSE;
12394
12395 start = get_tv_number_chk(&argvars[2], &error);
12396 if (error)
12397 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012398 if (l != NULL)
12399 {
12400 li = list_find(l, start);
12401 if (li == NULL)
12402 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012403 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012404 }
12405 else
12406 {
12407 if (start < 0)
12408 start = 0;
12409 if (start > (long)STRLEN(str))
12410 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012411 /* When "count" argument is there ignore matches before "start",
12412 * otherwise skip part of the string. Differs when pattern is "^"
12413 * or "\<". */
12414 if (argvars[3].v_type != VAR_UNKNOWN)
12415 startcol = start;
12416 else
12417 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012418 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012420 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012421 nth = get_tv_number_chk(&argvars[3], &error);
12422 if (error)
12423 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 }
12425
12426 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12427 if (regmatch.regprog != NULL)
12428 {
12429 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012430
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012431 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012432 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012433 if (l != NULL)
12434 {
12435 if (li == NULL)
12436 {
12437 match = FALSE;
12438 break;
12439 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012440 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012441 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012442 if (str == NULL)
12443 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012444 }
12445
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012446 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012447
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012448 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012449 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012450 if (l == NULL && !match)
12451 break;
12452
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012453 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012454 if (l != NULL)
12455 {
12456 li = li->li_next;
12457 ++idx;
12458 }
12459 else
12460 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012461#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012462 startcol = (colnr_T)(regmatch.startp[0]
12463 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012464#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012465 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012466#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012467 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012468 }
12469
12470 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012472 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012473 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012474 int i;
12475
12476 /* return list with matched string and submatches */
12477 for (i = 0; i < NSUBEXP; ++i)
12478 {
12479 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012480 {
12481 if (list_append_string(rettv->vval.v_list,
12482 (char_u *)"", 0) == FAIL)
12483 break;
12484 }
12485 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012486 regmatch.startp[i],
12487 (int)(regmatch.endp[i] - regmatch.startp[i]))
12488 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012489 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012490 }
12491 }
12492 else if (type == 2)
12493 {
12494 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012495 if (l != NULL)
12496 copy_tv(&li->li_tv, rettv);
12497 else
12498 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012500 }
12501 else if (l != NULL)
12502 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 else
12504 {
12505 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 (varnumber_T)(regmatch.startp[0] - str);
12508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012511 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 }
12513 }
12514 vim_free(regmatch.regprog);
12515 }
12516
12517theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012518 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 p_cpo = save_cpo;
12520}
12521
12522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 * "match()" function
12524 */
12525 static void
12526f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 typval_T *argvars;
12528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529{
12530 find_some_match(argvars, rettv, 1);
12531}
12532
12533/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012534 * "matchadd()" function
12535 */
12536 static void
12537f_matchadd(argvars, rettv)
12538 typval_T *argvars;
12539 typval_T *rettv;
12540{
12541#ifdef FEAT_SEARCH_EXTRA
12542 char_u buf[NUMBUFLEN];
12543 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12544 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12545 int prio = 10; /* default priority */
12546 int id = -1;
12547 int error = FALSE;
12548
12549 rettv->vval.v_number = -1;
12550
12551 if (grp == NULL || pat == NULL)
12552 return;
12553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012554 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012555 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012556 if (argvars[3].v_type != VAR_UNKNOWN)
12557 id = get_tv_number_chk(&argvars[3], &error);
12558 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012559 if (error == TRUE)
12560 return;
12561 if (id >= 1 && id <= 3)
12562 {
12563 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12564 return;
12565 }
12566
12567 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12568#endif
12569}
12570
12571/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012572 * "matcharg()" function
12573 */
12574 static void
12575f_matcharg(argvars, rettv)
12576 typval_T *argvars;
12577 typval_T *rettv;
12578{
12579 if (rettv_list_alloc(rettv) == OK)
12580 {
12581#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012582 int id = get_tv_number(&argvars[0]);
12583 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012584
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012585 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012586 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012587 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12588 {
12589 list_append_string(rettv->vval.v_list,
12590 syn_id2name(m->hlg_id), -1);
12591 list_append_string(rettv->vval.v_list, m->pattern, -1);
12592 }
12593 else
12594 {
12595 list_append_string(rettv->vval.v_list, NUL, -1);
12596 list_append_string(rettv->vval.v_list, NUL, -1);
12597 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012598 }
12599#endif
12600 }
12601}
12602
12603/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012604 * "matchdelete()" function
12605 */
12606 static void
12607f_matchdelete(argvars, rettv)
12608 typval_T *argvars;
12609 typval_T *rettv;
12610{
12611#ifdef FEAT_SEARCH_EXTRA
12612 rettv->vval.v_number = match_delete(curwin,
12613 (int)get_tv_number(&argvars[0]), TRUE);
12614#endif
12615}
12616
12617/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012618 * "matchend()" function
12619 */
12620 static void
12621f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012622 typval_T *argvars;
12623 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624{
12625 find_some_match(argvars, rettv, 0);
12626}
12627
12628/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012629 * "matchlist()" function
12630 */
12631 static void
12632f_matchlist(argvars, rettv)
12633 typval_T *argvars;
12634 typval_T *rettv;
12635{
12636 find_some_match(argvars, rettv, 3);
12637}
12638
12639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012640 * "matchstr()" function
12641 */
12642 static void
12643f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *argvars;
12645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646{
12647 find_some_match(argvars, rettv, 2);
12648}
12649
Bram Moolenaar33570922005-01-25 22:26:29 +000012650static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012651
12652 static void
12653max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *argvars;
12655 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012656 int domax;
12657{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012658 long n = 0;
12659 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012661
12662 if (argvars[0].v_type == VAR_LIST)
12663 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012664 list_T *l;
12665 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012666
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012667 l = argvars[0].vval.v_list;
12668 if (l != NULL)
12669 {
12670 li = l->lv_first;
12671 if (li != NULL)
12672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012674 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012675 {
12676 li = li->li_next;
12677 if (li == NULL)
12678 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012680 if (domax ? i > n : i < n)
12681 n = i;
12682 }
12683 }
12684 }
12685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012686 else if (argvars[0].v_type == VAR_DICT)
12687 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012689 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012691 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012692
12693 d = argvars[0].vval.v_dict;
12694 if (d != NULL)
12695 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012696 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012699 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012700 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012701 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012703 if (first)
12704 {
12705 n = i;
12706 first = FALSE;
12707 }
12708 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012709 n = i;
12710 }
12711 }
12712 }
12713 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012714 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012715 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012717}
12718
12719/*
12720 * "max()" function
12721 */
12722 static void
12723f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012724 typval_T *argvars;
12725 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012726{
12727 max_min(argvars, rettv, TRUE);
12728}
12729
12730/*
12731 * "min()" function
12732 */
12733 static void
12734f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 typval_T *argvars;
12736 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012737{
12738 max_min(argvars, rettv, FALSE);
12739}
12740
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012741static int mkdir_recurse __ARGS((char_u *dir, int prot));
12742
12743/*
12744 * Create the directory in which "dir" is located, and higher levels when
12745 * needed.
12746 */
12747 static int
12748mkdir_recurse(dir, prot)
12749 char_u *dir;
12750 int prot;
12751{
12752 char_u *p;
12753 char_u *updir;
12754 int r = FAIL;
12755
12756 /* Get end of directory name in "dir".
12757 * We're done when it's "/" or "c:/". */
12758 p = gettail_sep(dir);
12759 if (p <= get_past_head(dir))
12760 return OK;
12761
12762 /* If the directory exists we're done. Otherwise: create it.*/
12763 updir = vim_strnsave(dir, (int)(p - dir));
12764 if (updir == NULL)
12765 return FAIL;
12766 if (mch_isdir(updir))
12767 r = OK;
12768 else if (mkdir_recurse(updir, prot) == OK)
12769 r = vim_mkdir_emsg(updir, prot);
12770 vim_free(updir);
12771 return r;
12772}
12773
12774#ifdef vim_mkdir
12775/*
12776 * "mkdir()" function
12777 */
12778 static void
12779f_mkdir(argvars, rettv)
12780 typval_T *argvars;
12781 typval_T *rettv;
12782{
12783 char_u *dir;
12784 char_u buf[NUMBUFLEN];
12785 int prot = 0755;
12786
12787 rettv->vval.v_number = FAIL;
12788 if (check_restricted() || check_secure())
12789 return;
12790
12791 dir = get_tv_string_buf(&argvars[0], buf);
12792 if (argvars[1].v_type != VAR_UNKNOWN)
12793 {
12794 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 prot = get_tv_number_chk(&argvars[2], NULL);
12796 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012797 mkdir_recurse(dir, prot);
12798 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012800}
12801#endif
12802
Bram Moolenaar0d660222005-01-07 21:51:51 +000012803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 * "mode()" function
12805 */
12806/*ARGSUSED*/
12807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012809 typval_T *argvars;
12810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811{
12812 char_u buf[2];
12813
12814#ifdef FEAT_VISUAL
12815 if (VIsual_active)
12816 {
12817 if (VIsual_select)
12818 buf[0] = VIsual_mode + 's' - 'v';
12819 else
12820 buf[0] = VIsual_mode;
12821 }
12822 else
12823#endif
12824 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12825 buf[0] = 'r';
12826 else if (State & INSERT)
12827 {
12828 if (State & REPLACE_FLAG)
12829 buf[0] = 'R';
12830 else
12831 buf[0] = 'i';
12832 }
12833 else if (State & CMDLINE)
12834 buf[0] = 'c';
12835 else
12836 buf[0] = 'n';
12837
12838 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_string = vim_strsave(buf);
12840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841}
12842
12843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012844 * "nextnonblank()" function
12845 */
12846 static void
12847f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012848 typval_T *argvars;
12849 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012850{
12851 linenr_T lnum;
12852
12853 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012855 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012856 {
12857 lnum = 0;
12858 break;
12859 }
12860 if (*skipwhite(ml_get(lnum)) != NUL)
12861 break;
12862 }
12863 rettv->vval.v_number = lnum;
12864}
12865
12866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 * "nr2char()" function
12868 */
12869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012871 typval_T *argvars;
12872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873{
12874 char_u buf[NUMBUFLEN];
12875
12876#ifdef FEAT_MBYTE
12877 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 else
12880#endif
12881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 buf[1] = NUL;
12884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->v_type = VAR_STRING;
12886 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012887}
12888
12889/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012890 * "pathshorten()" function
12891 */
12892 static void
12893f_pathshorten(argvars, rettv)
12894 typval_T *argvars;
12895 typval_T *rettv;
12896{
12897 char_u *p;
12898
12899 rettv->v_type = VAR_STRING;
12900 p = get_tv_string_chk(&argvars[0]);
12901 if (p == NULL)
12902 rettv->vval.v_string = NULL;
12903 else
12904 {
12905 p = vim_strsave(p);
12906 rettv->vval.v_string = p;
12907 if (p != NULL)
12908 shorten_dir(p);
12909 }
12910}
12911
12912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012913 * "prevnonblank()" function
12914 */
12915 static void
12916f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012917 typval_T *argvars;
12918 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012919{
12920 linenr_T lnum;
12921
12922 lnum = get_tv_lnum(argvars);
12923 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12924 lnum = 0;
12925 else
12926 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12927 --lnum;
12928 rettv->vval.v_number = lnum;
12929}
12930
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012931#ifdef HAVE_STDARG_H
12932/* This dummy va_list is here because:
12933 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12934 * - locally in the function results in a "used before set" warning
12935 * - using va_start() to initialize it gives "function with fixed args" error */
12936static va_list ap;
12937#endif
12938
Bram Moolenaar8c711452005-01-14 21:53:12 +000012939/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012940 * "printf()" function
12941 */
12942 static void
12943f_printf(argvars, rettv)
12944 typval_T *argvars;
12945 typval_T *rettv;
12946{
12947 rettv->v_type = VAR_STRING;
12948 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012949#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012950 {
12951 char_u buf[NUMBUFLEN];
12952 int len;
12953 char_u *s;
12954 int saved_did_emsg = did_emsg;
12955 char *fmt;
12956
12957 /* Get the required length, allocate the buffer and do it for real. */
12958 did_emsg = FALSE;
12959 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012960 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012961 if (!did_emsg)
12962 {
12963 s = alloc(len + 1);
12964 if (s != NULL)
12965 {
12966 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012967 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012968 }
12969 }
12970 did_emsg |= saved_did_emsg;
12971 }
12972#endif
12973}
12974
12975/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012976 * "pumvisible()" function
12977 */
12978/*ARGSUSED*/
12979 static void
12980f_pumvisible(argvars, rettv)
12981 typval_T *argvars;
12982 typval_T *rettv;
12983{
12984 rettv->vval.v_number = 0;
12985#ifdef FEAT_INS_EXPAND
12986 if (pum_visible())
12987 rettv->vval.v_number = 1;
12988#endif
12989}
12990
12991/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012992 * "range()" function
12993 */
12994 static void
12995f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012996 typval_T *argvars;
12997 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012998{
12999 long start;
13000 long end;
13001 long stride = 1;
13002 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013003 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013005 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013006 if (argvars[1].v_type == VAR_UNKNOWN)
13007 {
13008 end = start - 1;
13009 start = 0;
13010 }
13011 else
13012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013014 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013016 }
13017
13018 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013019 if (error)
13020 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013022 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013023 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013024 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013025 else
13026 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013027 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013029 if (list_append_number(rettv->vval.v_list,
13030 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013031 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013032 }
13033}
13034
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013035/*
13036 * "readfile()" function
13037 */
13038 static void
13039f_readfile(argvars, rettv)
13040 typval_T *argvars;
13041 typval_T *rettv;
13042{
13043 int binary = FALSE;
13044 char_u *fname;
13045 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013046 listitem_T *li;
13047#define FREAD_SIZE 200 /* optimized for text lines */
13048 char_u buf[FREAD_SIZE];
13049 int readlen; /* size of last fread() */
13050 int buflen; /* nr of valid chars in buf[] */
13051 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13052 int tolist; /* first byte in buf[] still to be put in list */
13053 int chop; /* how many CR to chop off */
13054 char_u *prev = NULL; /* previously read bytes, if any */
13055 int prevlen = 0; /* length of "prev" if not NULL */
13056 char_u *s;
13057 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013058 long maxline = MAXLNUM;
13059 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013061 if (argvars[1].v_type != VAR_UNKNOWN)
13062 {
13063 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13064 binary = TRUE;
13065 if (argvars[2].v_type != VAR_UNKNOWN)
13066 maxline = get_tv_number(&argvars[2]);
13067 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013068
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013069 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013070 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013071
13072 /* Always open the file in binary mode, library functions have a mind of
13073 * their own about CR-LF conversion. */
13074 fname = get_tv_string(&argvars[0]);
13075 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13076 {
13077 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13078 return;
13079 }
13080
13081 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013082 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013083 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013084 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013085 buflen = filtd + readlen;
13086 tolist = 0;
13087 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13088 {
13089 if (buf[filtd] == '\n' || readlen <= 0)
13090 {
13091 /* Only when in binary mode add an empty list item when the
13092 * last line ends in a '\n'. */
13093 if (!binary && readlen == 0 && filtd == 0)
13094 break;
13095
13096 /* Found end-of-line or end-of-file: add a text line to the
13097 * list. */
13098 chop = 0;
13099 if (!binary)
13100 while (filtd - chop - 1 >= tolist
13101 && buf[filtd - chop - 1] == '\r')
13102 ++chop;
13103 len = filtd - tolist - chop;
13104 if (prev == NULL)
13105 s = vim_strnsave(buf + tolist, len);
13106 else
13107 {
13108 s = alloc((unsigned)(prevlen + len + 1));
13109 if (s != NULL)
13110 {
13111 mch_memmove(s, prev, prevlen);
13112 vim_free(prev);
13113 prev = NULL;
13114 mch_memmove(s + prevlen, buf + tolist, len);
13115 s[prevlen + len] = NUL;
13116 }
13117 }
13118 tolist = filtd + 1;
13119
13120 li = listitem_alloc();
13121 if (li == NULL)
13122 {
13123 vim_free(s);
13124 break;
13125 }
13126 li->li_tv.v_type = VAR_STRING;
13127 li->li_tv.v_lock = 0;
13128 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013129 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013130
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013131 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013132 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013133 if (readlen <= 0)
13134 break;
13135 }
13136 else if (buf[filtd] == NUL)
13137 buf[filtd] = '\n';
13138 }
13139 if (readlen <= 0)
13140 break;
13141
13142 if (tolist == 0)
13143 {
13144 /* "buf" is full, need to move text to an allocated buffer */
13145 if (prev == NULL)
13146 {
13147 prev = vim_strnsave(buf, buflen);
13148 prevlen = buflen;
13149 }
13150 else
13151 {
13152 s = alloc((unsigned)(prevlen + buflen));
13153 if (s != NULL)
13154 {
13155 mch_memmove(s, prev, prevlen);
13156 mch_memmove(s + prevlen, buf, buflen);
13157 vim_free(prev);
13158 prev = s;
13159 prevlen += buflen;
13160 }
13161 }
13162 filtd = 0;
13163 }
13164 else
13165 {
13166 mch_memmove(buf, buf + tolist, buflen - tolist);
13167 filtd -= tolist;
13168 }
13169 }
13170
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013171 /*
13172 * For a negative line count use only the lines at the end of the file,
13173 * free the rest.
13174 */
13175 if (maxline < 0)
13176 while (cnt > -maxline)
13177 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013178 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013179 --cnt;
13180 }
13181
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013182 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013183 fclose(fd);
13184}
13185
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013186#if defined(FEAT_RELTIME)
13187static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13188
13189/*
13190 * Convert a List to proftime_T.
13191 * Return FAIL when there is something wrong.
13192 */
13193 static int
13194list2proftime(arg, tm)
13195 typval_T *arg;
13196 proftime_T *tm;
13197{
13198 long n1, n2;
13199 int error = FALSE;
13200
13201 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13202 || arg->vval.v_list->lv_len != 2)
13203 return FAIL;
13204 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13205 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13206# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013207 tm->HighPart = n1;
13208 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013209# else
13210 tm->tv_sec = n1;
13211 tm->tv_usec = n2;
13212# endif
13213 return error ? FAIL : OK;
13214}
13215#endif /* FEAT_RELTIME */
13216
13217/*
13218 * "reltime()" function
13219 */
13220 static void
13221f_reltime(argvars, rettv)
13222 typval_T *argvars;
13223 typval_T *rettv;
13224{
13225#ifdef FEAT_RELTIME
13226 proftime_T res;
13227 proftime_T start;
13228
13229 if (argvars[0].v_type == VAR_UNKNOWN)
13230 {
13231 /* No arguments: get current time. */
13232 profile_start(&res);
13233 }
13234 else if (argvars[1].v_type == VAR_UNKNOWN)
13235 {
13236 if (list2proftime(&argvars[0], &res) == FAIL)
13237 return;
13238 profile_end(&res);
13239 }
13240 else
13241 {
13242 /* Two arguments: compute the difference. */
13243 if (list2proftime(&argvars[0], &start) == FAIL
13244 || list2proftime(&argvars[1], &res) == FAIL)
13245 return;
13246 profile_sub(&res, &start);
13247 }
13248
13249 if (rettv_list_alloc(rettv) == OK)
13250 {
13251 long n1, n2;
13252
13253# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013254 n1 = res.HighPart;
13255 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013256# else
13257 n1 = res.tv_sec;
13258 n2 = res.tv_usec;
13259# endif
13260 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13261 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13262 }
13263#endif
13264}
13265
13266/*
13267 * "reltimestr()" function
13268 */
13269 static void
13270f_reltimestr(argvars, rettv)
13271 typval_T *argvars;
13272 typval_T *rettv;
13273{
13274#ifdef FEAT_RELTIME
13275 proftime_T tm;
13276#endif
13277
13278 rettv->v_type = VAR_STRING;
13279 rettv->vval.v_string = NULL;
13280#ifdef FEAT_RELTIME
13281 if (list2proftime(&argvars[0], &tm) == OK)
13282 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13283#endif
13284}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013285
Bram Moolenaar0d660222005-01-07 21:51:51 +000013286#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13287static void make_connection __ARGS((void));
13288static int check_connection __ARGS((void));
13289
13290 static void
13291make_connection()
13292{
13293 if (X_DISPLAY == NULL
13294# ifdef FEAT_GUI
13295 && !gui.in_use
13296# endif
13297 )
13298 {
13299 x_force_connect = TRUE;
13300 setup_term_clip();
13301 x_force_connect = FALSE;
13302 }
13303}
13304
13305 static int
13306check_connection()
13307{
13308 make_connection();
13309 if (X_DISPLAY == NULL)
13310 {
13311 EMSG(_("E240: No connection to Vim server"));
13312 return FAIL;
13313 }
13314 return OK;
13315}
13316#endif
13317
13318#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013319static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013320
13321 static void
13322remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013323 typval_T *argvars;
13324 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013325 int expr;
13326{
13327 char_u *server_name;
13328 char_u *keys;
13329 char_u *r = NULL;
13330 char_u buf[NUMBUFLEN];
13331# ifdef WIN32
13332 HWND w;
13333# else
13334 Window w;
13335# endif
13336
13337 if (check_restricted() || check_secure())
13338 return;
13339
13340# ifdef FEAT_X11
13341 if (check_connection() == FAIL)
13342 return;
13343# endif
13344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013345 server_name = get_tv_string_chk(&argvars[0]);
13346 if (server_name == NULL)
13347 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013348 keys = get_tv_string_buf(&argvars[1], buf);
13349# ifdef WIN32
13350 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13351# else
13352 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13353 < 0)
13354# endif
13355 {
13356 if (r != NULL)
13357 EMSG(r); /* sending worked but evaluation failed */
13358 else
13359 EMSG2(_("E241: Unable to send to %s"), server_name);
13360 return;
13361 }
13362
13363 rettv->vval.v_string = r;
13364
13365 if (argvars[2].v_type != VAR_UNKNOWN)
13366 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013368 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013369 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013370
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013371 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 v.di_tv.v_type = VAR_STRING;
13373 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 idvar = get_tv_string_chk(&argvars[2]);
13375 if (idvar != NULL)
13376 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378 }
13379}
13380#endif
13381
13382/*
13383 * "remote_expr()" function
13384 */
13385/*ARGSUSED*/
13386 static void
13387f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 typval_T *argvars;
13389 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390{
13391 rettv->v_type = VAR_STRING;
13392 rettv->vval.v_string = NULL;
13393#ifdef FEAT_CLIENTSERVER
13394 remote_common(argvars, rettv, TRUE);
13395#endif
13396}
13397
13398/*
13399 * "remote_foreground()" function
13400 */
13401/*ARGSUSED*/
13402 static void
13403f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013404 typval_T *argvars;
13405 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013406{
13407 rettv->vval.v_number = 0;
13408#ifdef FEAT_CLIENTSERVER
13409# ifdef WIN32
13410 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 {
13412 char_u *server_name = get_tv_string_chk(&argvars[0]);
13413
13414 if (server_name != NULL)
13415 serverForeground(server_name);
13416 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013417# else
13418 /* Send a foreground() expression to the server. */
13419 argvars[1].v_type = VAR_STRING;
13420 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13421 argvars[2].v_type = VAR_UNKNOWN;
13422 remote_common(argvars, rettv, TRUE);
13423 vim_free(argvars[1].vval.v_string);
13424# endif
13425#endif
13426}
13427
13428/*ARGSUSED*/
13429 static void
13430f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 typval_T *argvars;
13432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013433{
13434#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013435 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436 char_u *s = NULL;
13437# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013438 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013441
13442 if (check_restricted() || check_secure())
13443 {
13444 rettv->vval.v_number = -1;
13445 return;
13446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 serverid = get_tv_string_chk(&argvars[0]);
13448 if (serverid == NULL)
13449 {
13450 rettv->vval.v_number = -1;
13451 return; /* type error; errmsg already given */
13452 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013454 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013455 if (n == 0)
13456 rettv->vval.v_number = -1;
13457 else
13458 {
13459 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13460 rettv->vval.v_number = (s != NULL);
13461 }
13462# else
13463 rettv->vval.v_number = 0;
13464 if (check_connection() == FAIL)
13465 return;
13466
13467 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013469# endif
13470
13471 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473 char_u *retvar;
13474
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 v.di_tv.v_type = VAR_STRING;
13476 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 retvar = get_tv_string_chk(&argvars[1]);
13478 if (retvar != NULL)
13479 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481 }
13482#else
13483 rettv->vval.v_number = -1;
13484#endif
13485}
13486
13487/*ARGSUSED*/
13488 static void
13489f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013490 typval_T *argvars;
13491 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492{
13493 char_u *r = NULL;
13494
13495#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 char_u *serverid = get_tv_string_chk(&argvars[0]);
13497
13498 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499 {
13500# ifdef WIN32
13501 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013502 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013503
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013504 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013505 if (n != 0)
13506 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13507 if (r == NULL)
13508# else
13509 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013511# endif
13512 EMSG(_("E277: Unable to read a server reply"));
13513 }
13514#endif
13515 rettv->v_type = VAR_STRING;
13516 rettv->vval.v_string = r;
13517}
13518
13519/*
13520 * "remote_send()" function
13521 */
13522/*ARGSUSED*/
13523 static void
13524f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013525 typval_T *argvars;
13526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013527{
13528 rettv->v_type = VAR_STRING;
13529 rettv->vval.v_string = NULL;
13530#ifdef FEAT_CLIENTSERVER
13531 remote_common(argvars, rettv, FALSE);
13532#endif
13533}
13534
13535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013536 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013537 */
13538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 typval_T *argvars;
13541 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013542{
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 list_T *l;
13544 listitem_T *item, *item2;
13545 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013546 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013547 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013548 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 dict_T *d;
13550 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013551
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013552 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013553 if (argvars[0].v_type == VAR_DICT)
13554 {
13555 if (argvars[2].v_type != VAR_UNKNOWN)
13556 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013557 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013558 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560 key = get_tv_string_chk(&argvars[1]);
13561 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 di = dict_find(d, key, -1);
13564 if (di == NULL)
13565 EMSG2(_(e_dictkey), key);
13566 else
13567 {
13568 *rettv = di->di_tv;
13569 init_tv(&di->di_tv);
13570 dictitem_remove(d, di);
13571 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013572 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013573 }
13574 }
13575 else if (argvars[0].v_type != VAR_LIST)
13576 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013577 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013578 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 int error = FALSE;
13581
13582 idx = get_tv_number_chk(&argvars[1], &error);
13583 if (error)
13584 ; /* type error: do nothing, errmsg already given */
13585 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013586 EMSGN(_(e_listidx), idx);
13587 else
13588 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013589 if (argvars[2].v_type == VAR_UNKNOWN)
13590 {
13591 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013592 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013593 *rettv = item->li_tv;
13594 vim_free(item);
13595 }
13596 else
13597 {
13598 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013599 end = get_tv_number_chk(&argvars[2], &error);
13600 if (error)
13601 ; /* type error: do nothing */
13602 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013603 EMSGN(_(e_listidx), end);
13604 else
13605 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013606 int cnt = 0;
13607
13608 for (li = item; li != NULL; li = li->li_next)
13609 {
13610 ++cnt;
13611 if (li == item2)
13612 break;
13613 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013614 if (li == NULL) /* didn't find "item2" after "item" */
13615 EMSG(_(e_invrange));
13616 else
13617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013618 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013619 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013620 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013621 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013622 l->lv_first = item;
13623 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013624 item->li_prev = NULL;
13625 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013626 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013627 }
13628 }
13629 }
13630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013631 }
13632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633}
13634
13635/*
13636 * "rename({from}, {to})" function
13637 */
13638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642{
13643 char_u buf[NUMBUFLEN];
13644
13645 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13649 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650}
13651
13652/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013653 * "repeat()" function
13654 */
13655/*ARGSUSED*/
13656 static void
13657f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013660{
13661 char_u *p;
13662 int n;
13663 int slen;
13664 int len;
13665 char_u *r;
13666 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013667
13668 n = get_tv_number(&argvars[1]);
13669 if (argvars[0].v_type == VAR_LIST)
13670 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013671 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013672 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013673 if (list_extend(rettv->vval.v_list,
13674 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013675 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013676 }
13677 else
13678 {
13679 p = get_tv_string(&argvars[0]);
13680 rettv->v_type = VAR_STRING;
13681 rettv->vval.v_string = NULL;
13682
13683 slen = (int)STRLEN(p);
13684 len = slen * n;
13685 if (len <= 0)
13686 return;
13687
13688 r = alloc(len + 1);
13689 if (r != NULL)
13690 {
13691 for (i = 0; i < n; i++)
13692 mch_memmove(r + i * slen, p, (size_t)slen);
13693 r[len] = NUL;
13694 }
13695
13696 rettv->vval.v_string = r;
13697 }
13698}
13699
13700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 * "resolve()" function
13702 */
13703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *argvars;
13706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707{
13708 char_u *p;
13709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711#ifdef FEAT_SHORTCUT
13712 {
13713 char_u *v = NULL;
13714
13715 v = mch_resolve_shortcut(p);
13716 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 }
13721#else
13722# ifdef HAVE_READLINK
13723 {
13724 char_u buf[MAXPATHL + 1];
13725 char_u *cpy;
13726 int len;
13727 char_u *remain = NULL;
13728 char_u *q;
13729 int is_relative_to_current = FALSE;
13730 int has_trailing_pathsep = FALSE;
13731 int limit = 100;
13732
13733 p = vim_strsave(p);
13734
13735 if (p[0] == '.' && (vim_ispathsep(p[1])
13736 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13737 is_relative_to_current = TRUE;
13738
13739 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013740 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 has_trailing_pathsep = TRUE;
13742
13743 q = getnextcomp(p);
13744 if (*q != NUL)
13745 {
13746 /* Separate the first path component in "p", and keep the
13747 * remainder (beginning with the path separator). */
13748 remain = vim_strsave(q - 1);
13749 q[-1] = NUL;
13750 }
13751
13752 for (;;)
13753 {
13754 for (;;)
13755 {
13756 len = readlink((char *)p, (char *)buf, MAXPATHL);
13757 if (len <= 0)
13758 break;
13759 buf[len] = NUL;
13760
13761 if (limit-- == 0)
13762 {
13763 vim_free(p);
13764 vim_free(remain);
13765 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 goto fail;
13768 }
13769
13770 /* Ensure that the result will have a trailing path separator
13771 * if the argument has one. */
13772 if (remain == NULL && has_trailing_pathsep)
13773 add_pathsep(buf);
13774
13775 /* Separate the first path component in the link value and
13776 * concatenate the remainders. */
13777 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13778 if (*q != NUL)
13779 {
13780 if (remain == NULL)
13781 remain = vim_strsave(q - 1);
13782 else
13783 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013784 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 if (cpy != NULL)
13786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 vim_free(remain);
13788 remain = cpy;
13789 }
13790 }
13791 q[-1] = NUL;
13792 }
13793
13794 q = gettail(p);
13795 if (q > p && *q == NUL)
13796 {
13797 /* Ignore trailing path separator. */
13798 q[-1] = NUL;
13799 q = gettail(p);
13800 }
13801 if (q > p && !mch_isFullName(buf))
13802 {
13803 /* symlink is relative to directory of argument */
13804 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13805 if (cpy != NULL)
13806 {
13807 STRCPY(cpy, p);
13808 STRCPY(gettail(cpy), buf);
13809 vim_free(p);
13810 p = cpy;
13811 }
13812 }
13813 else
13814 {
13815 vim_free(p);
13816 p = vim_strsave(buf);
13817 }
13818 }
13819
13820 if (remain == NULL)
13821 break;
13822
13823 /* Append the first path component of "remain" to "p". */
13824 q = getnextcomp(remain + 1);
13825 len = q - remain - (*q != NUL);
13826 cpy = vim_strnsave(p, STRLEN(p) + len);
13827 if (cpy != NULL)
13828 {
13829 STRNCAT(cpy, remain, len);
13830 vim_free(p);
13831 p = cpy;
13832 }
13833 /* Shorten "remain". */
13834 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013835 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 else
13837 {
13838 vim_free(remain);
13839 remain = NULL;
13840 }
13841 }
13842
13843 /* If the result is a relative path name, make it explicitly relative to
13844 * the current directory if and only if the argument had this form. */
13845 if (!vim_ispathsep(*p))
13846 {
13847 if (is_relative_to_current
13848 && *p != NUL
13849 && !(p[0] == '.'
13850 && (p[1] == NUL
13851 || vim_ispathsep(p[1])
13852 || (p[1] == '.'
13853 && (p[2] == NUL
13854 || vim_ispathsep(p[2]))))))
13855 {
13856 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013857 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 if (cpy != NULL)
13859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 vim_free(p);
13861 p = cpy;
13862 }
13863 }
13864 else if (!is_relative_to_current)
13865 {
13866 /* Strip leading "./". */
13867 q = p;
13868 while (q[0] == '.' && vim_ispathsep(q[1]))
13869 q += 2;
13870 if (q > p)
13871 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13872 }
13873 }
13874
13875 /* Ensure that the result will have no trailing path separator
13876 * if the argument had none. But keep "/" or "//". */
13877 if (!has_trailing_pathsep)
13878 {
13879 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013880 if (after_pathsep(p, q))
13881 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 }
13883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013884 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 }
13886# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888# endif
13889#endif
13890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892
13893#ifdef HAVE_READLINK
13894fail:
13895#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897}
13898
13899/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013900 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 */
13902 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013904 typval_T *argvars;
13905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906{
Bram Moolenaar33570922005-01-25 22:26:29 +000013907 list_T *l;
13908 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909
Bram Moolenaar0d660222005-01-07 21:51:51 +000013910 rettv->vval.v_number = 0;
13911 if (argvars[0].v_type != VAR_LIST)
13912 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013913 else if ((l = argvars[0].vval.v_list) != NULL
13914 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915 {
13916 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013917 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013918 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013919 while (li != NULL)
13920 {
13921 ni = li->li_prev;
13922 list_append(l, li);
13923 li = ni;
13924 }
13925 rettv->vval.v_list = l;
13926 rettv->v_type = VAR_LIST;
13927 ++l->lv_refcount;
13928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929}
13930
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013931#define SP_NOMOVE 0x01 /* don't move cursor */
13932#define SP_REPEAT 0x02 /* repeat to find outer pair */
13933#define SP_RETCOUNT 0x04 /* return matchcount */
13934#define SP_SETPCMARK 0x08 /* set previous context mark */
13935#define SP_START 0x10 /* accept match at start position */
13936#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13937#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013938
Bram Moolenaar33570922005-01-25 22:26:29 +000013939static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013940
13941/*
13942 * Get flags for a search function.
13943 * Possibly sets "p_ws".
13944 * Returns BACKWARD, FORWARD or zero (for an error).
13945 */
13946 static int
13947get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013948 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949 int *flagsp;
13950{
13951 int dir = FORWARD;
13952 char_u *flags;
13953 char_u nbuf[NUMBUFLEN];
13954 int mask;
13955
13956 if (varp->v_type != VAR_UNKNOWN)
13957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 flags = get_tv_string_buf_chk(varp, nbuf);
13959 if (flags == NULL)
13960 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961 while (*flags != NUL)
13962 {
13963 switch (*flags)
13964 {
13965 case 'b': dir = BACKWARD; break;
13966 case 'w': p_ws = TRUE; break;
13967 case 'W': p_ws = FALSE; break;
13968 default: mask = 0;
13969 if (flagsp != NULL)
13970 switch (*flags)
13971 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013972 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013973 case 'e': mask = SP_END; break;
13974 case 'm': mask = SP_RETCOUNT; break;
13975 case 'n': mask = SP_NOMOVE; break;
13976 case 'p': mask = SP_SUBPAT; break;
13977 case 'r': mask = SP_REPEAT; break;
13978 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 }
13980 if (mask == 0)
13981 {
13982 EMSG2(_(e_invarg2), flags);
13983 dir = 0;
13984 }
13985 else
13986 *flagsp |= mask;
13987 }
13988 if (dir == 0)
13989 break;
13990 ++flags;
13991 }
13992 }
13993 return dir;
13994}
13995
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013997 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013999 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014000search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014001 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014002 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014003 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014005 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 char_u *pat;
14007 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014008 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 int save_p_ws = p_ws;
14010 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014011 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014012 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014013 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014014 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014017 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014018 if (dir == 0)
14019 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014020 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014021 if (flags & SP_START)
14022 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014023 if (flags & SP_END)
14024 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014025
14026 /* Optional extra argument: line number to stop searching. */
14027 if (argvars[1].v_type != VAR_UNKNOWN
14028 && argvars[2].v_type != VAR_UNKNOWN)
14029 {
14030 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14031 if (lnum_stop < 0)
14032 goto theend;
14033 }
14034
Bram Moolenaar231334e2005-07-25 20:46:57 +000014035 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014036 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014037 * Check to make sure only those flags are set.
14038 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14039 * flags cannot be set. Check for that condition also.
14040 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014041 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014042 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014045 goto theend;
14046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014048 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014049 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14050 options, RE_SEARCH, (linenr_T)lnum_stop);
14051 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014053 if (flags & SP_SUBPAT)
14054 retval = subpatnum;
14055 else
14056 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014057 if (flags & SP_SETPCMARK)
14058 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014060 if (match_pos != NULL)
14061 {
14062 /* Store the match cursor position */
14063 match_pos->lnum = pos.lnum;
14064 match_pos->col = pos.col + 1;
14065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 /* "/$" will put the cursor after the end of the line, may need to
14067 * correct that here */
14068 check_cursor();
14069 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014070
14071 /* If 'n' flag is used: restore cursor position. */
14072 if (flags & SP_NOMOVE)
14073 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014074 else
14075 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014076theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014078
14079 return retval;
14080}
14081
14082/*
14083 * "search()" function
14084 */
14085 static void
14086f_search(argvars, rettv)
14087 typval_T *argvars;
14088 typval_T *rettv;
14089{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014090 int flags = 0;
14091
14092 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093}
14094
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014096 * "searchdecl()" function
14097 */
14098 static void
14099f_searchdecl(argvars, rettv)
14100 typval_T *argvars;
14101 typval_T *rettv;
14102{
14103 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014104 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014105 int error = FALSE;
14106 char_u *name;
14107
14108 rettv->vval.v_number = 1; /* default: FAIL */
14109
14110 name = get_tv_string_chk(&argvars[0]);
14111 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014112 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014113 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014114 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14115 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14116 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014117 if (!error && name != NULL)
14118 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014119 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014120}
14121
14122/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014123 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014125 static int
14126searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014128 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129{
14130 char_u *spat, *mpat, *epat;
14131 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 int dir;
14134 int flags = 0;
14135 char_u nbuf1[NUMBUFLEN];
14136 char_u nbuf2[NUMBUFLEN];
14137 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014138 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014139 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 spat = get_tv_string_chk(&argvars[0]);
14143 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14144 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14145 if (spat == NULL || mpat == NULL || epat == NULL)
14146 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 /* Handle the optional fourth argument: flags */
14149 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014150 if (dir == 0)
14151 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014152
14153 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014154 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14155 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014156 if ((flags & (SP_END | SP_SUBPAT)) != 0
14157 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014158 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014159 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014160 goto theend;
14161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014163 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014164 if (argvars[3].v_type == VAR_UNKNOWN
14165 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 skip = (char_u *)"";
14167 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014170 if (argvars[5].v_type != VAR_UNKNOWN)
14171 {
14172 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14173 if (lnum_stop < 0)
14174 goto theend;
14175 }
14176 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 if (skip == NULL)
14178 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014180 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14181 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014182
14183theend:
14184 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014185
14186 return retval;
14187}
14188
14189/*
14190 * "searchpair()" function
14191 */
14192 static void
14193f_searchpair(argvars, rettv)
14194 typval_T *argvars;
14195 typval_T *rettv;
14196{
14197 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14198}
14199
14200/*
14201 * "searchpairpos()" function
14202 */
14203 static void
14204f_searchpairpos(argvars, rettv)
14205 typval_T *argvars;
14206 typval_T *rettv;
14207{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014208 pos_T match_pos;
14209 int lnum = 0;
14210 int col = 0;
14211
14212 rettv->vval.v_number = 0;
14213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014214 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014215 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014216
14217 if (searchpair_cmn(argvars, &match_pos) > 0)
14218 {
14219 lnum = match_pos.lnum;
14220 col = match_pos.col;
14221 }
14222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014223 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14224 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014225}
14226
14227/*
14228 * Search for a start/middle/end thing.
14229 * Used by searchpair(), see its documentation for the details.
14230 * Returns 0 or -1 for no match,
14231 */
14232 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014233do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014234 char_u *spat; /* start pattern */
14235 char_u *mpat; /* middle pattern */
14236 char_u *epat; /* end pattern */
14237 int dir; /* BACKWARD or FORWARD */
14238 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014239 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014240 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014241 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014242{
14243 char_u *save_cpo;
14244 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14245 long retval = 0;
14246 pos_T pos;
14247 pos_T firstpos;
14248 pos_T foundpos;
14249 pos_T save_cursor;
14250 pos_T save_pos;
14251 int n;
14252 int r;
14253 int nest = 1;
14254 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014255 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014256
14257 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14258 save_cpo = p_cpo;
14259 p_cpo = (char_u *)"";
14260
14261 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14262 * start/middle/end (pat3, for the top pair). */
14263 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14264 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14265 if (pat2 == NULL || pat3 == NULL)
14266 goto theend;
14267 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14268 if (*mpat == NUL)
14269 STRCPY(pat3, pat2);
14270 else
14271 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14272 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014273 if (flags & SP_START)
14274 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014275
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 save_cursor = curwin->w_cursor;
14277 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014278 clearpos(&firstpos);
14279 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 pat = pat3;
14281 for (;;)
14282 {
14283 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014284 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14286 /* didn't find it or found the first match again: FAIL */
14287 break;
14288
14289 if (firstpos.lnum == 0)
14290 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014291 if (equalpos(pos, foundpos))
14292 {
14293 /* Found the same position again. Can happen with a pattern that
14294 * has "\zs" at the end and searching backwards. Advance one
14295 * character and try again. */
14296 if (dir == BACKWARD)
14297 decl(&pos);
14298 else
14299 incl(&pos);
14300 }
14301 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302
14303 /* If the skip pattern matches, ignore this match. */
14304 if (*skip != NUL)
14305 {
14306 save_pos = curwin->w_cursor;
14307 curwin->w_cursor = pos;
14308 r = eval_to_bool(skip, &err, NULL, FALSE);
14309 curwin->w_cursor = save_pos;
14310 if (err)
14311 {
14312 /* Evaluating {skip} caused an error, break here. */
14313 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014314 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 break;
14316 }
14317 if (r)
14318 continue;
14319 }
14320
14321 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14322 {
14323 /* Found end when searching backwards or start when searching
14324 * forward: nested pair. */
14325 ++nest;
14326 pat = pat2; /* nested, don't search for middle */
14327 }
14328 else
14329 {
14330 /* Found end when searching forward or start when searching
14331 * backward: end of (nested) pair; or found middle in outer pair. */
14332 if (--nest == 1)
14333 pat = pat3; /* outer level, search for middle */
14334 }
14335
14336 if (nest == 0)
14337 {
14338 /* Found the match: return matchcount or line number. */
14339 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014340 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014342 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014343 if (flags & SP_SETPCMARK)
14344 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 curwin->w_cursor = pos;
14346 if (!(flags & SP_REPEAT))
14347 break;
14348 nest = 1; /* search for next unmatched */
14349 }
14350 }
14351
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014352 if (match_pos != NULL)
14353 {
14354 /* Store the match cursor position */
14355 match_pos->lnum = curwin->w_cursor.lnum;
14356 match_pos->col = curwin->w_cursor.col + 1;
14357 }
14358
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014360 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 curwin->w_cursor = save_cursor;
14362
14363theend:
14364 vim_free(pat2);
14365 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014367
14368 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369}
14370
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014371/*
14372 * "searchpos()" function
14373 */
14374 static void
14375f_searchpos(argvars, rettv)
14376 typval_T *argvars;
14377 typval_T *rettv;
14378{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014379 pos_T match_pos;
14380 int lnum = 0;
14381 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014382 int n;
14383 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014384
14385 rettv->vval.v_number = 0;
14386
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014387 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014388 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014389
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014390 n = search_cmn(argvars, &match_pos, &flags);
14391 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014392 {
14393 lnum = match_pos.lnum;
14394 col = match_pos.col;
14395 }
14396
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014397 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14398 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014399 if (flags & SP_SUBPAT)
14400 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014401}
14402
14403
Bram Moolenaar0d660222005-01-07 21:51:51 +000014404/*ARGSUSED*/
14405 static void
14406f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *argvars;
14408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014410#ifdef FEAT_CLIENTSERVER
14411 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014412 char_u *server = get_tv_string_chk(&argvars[0]);
14413 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414
Bram Moolenaar0d660222005-01-07 21:51:51 +000014415 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 if (server == NULL || reply == NULL)
14417 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418 if (check_restricted() || check_secure())
14419 return;
14420# ifdef FEAT_X11
14421 if (check_connection() == FAIL)
14422 return;
14423# endif
14424
14425 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427 EMSG(_("E258: Unable to send to client"));
14428 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430 rettv->vval.v_number = 0;
14431#else
14432 rettv->vval.v_number = -1;
14433#endif
14434}
14435
14436/*ARGSUSED*/
14437 static void
14438f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 typval_T *argvars;
14440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441{
14442 char_u *r = NULL;
14443
14444#ifdef FEAT_CLIENTSERVER
14445# ifdef WIN32
14446 r = serverGetVimNames();
14447# else
14448 make_connection();
14449 if (X_DISPLAY != NULL)
14450 r = serverGetVimNames(X_DISPLAY);
14451# endif
14452#endif
14453 rettv->v_type = VAR_STRING;
14454 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455}
14456
14457/*
14458 * "setbufvar()" function
14459 */
14460/*ARGSUSED*/
14461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014463 typval_T *argvars;
14464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465{
14466 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014469 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 char_u nbuf[NUMBUFLEN];
14471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014472 rettv->vval.v_number = 0;
14473
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 if (check_restricted() || check_secure())
14475 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14477 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 varp = &argvars[2];
14480
14481 if (buf != NULL && varname != NULL && varp != NULL)
14482 {
14483 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485
14486 if (*varname == '&')
14487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014488 long numval;
14489 char_u *strval;
14490 int error = FALSE;
14491
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 numval = get_tv_number_chk(varp, &error);
14494 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 if (!error && strval != NULL)
14496 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 }
14498 else
14499 {
14500 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14501 if (bufvarname != NULL)
14502 {
14503 STRCPY(bufvarname, "b:");
14504 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014505 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 vim_free(bufvarname);
14507 }
14508 }
14509
14510 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513}
14514
14515/*
14516 * "setcmdpos()" function
14517 */
14518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014520 typval_T *argvars;
14521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014523 int pos = (int)get_tv_number(&argvars[0]) - 1;
14524
14525 if (pos >= 0)
14526 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527}
14528
14529/*
14530 * "setline()" function
14531 */
14532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 typval_T *argvars;
14535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536{
14537 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014538 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014539 list_T *l = NULL;
14540 listitem_T *li = NULL;
14541 long added = 0;
14542 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014544 lnum = get_tv_lnum(&argvars[0]);
14545 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014547 l = argvars[1].vval.v_list;
14548 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014550 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014552
14553 rettv->vval.v_number = 0; /* OK */
14554 for (;;)
14555 {
14556 if (l != NULL)
14557 {
14558 /* list argument, get next string */
14559 if (li == NULL)
14560 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014562 li = li->li_next;
14563 }
14564
14565 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014566 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014567 break;
14568 if (lnum <= curbuf->b_ml.ml_line_count)
14569 {
14570 /* existing line, replace it */
14571 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14572 {
14573 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014574 if (lnum == curwin->w_cursor.lnum)
14575 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014576 rettv->vval.v_number = 0; /* OK */
14577 }
14578 }
14579 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14580 {
14581 /* lnum is one past the last line, append the line */
14582 ++added;
14583 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14584 rettv->vval.v_number = 0; /* OK */
14585 }
14586
14587 if (l == NULL) /* only one string argument */
14588 break;
14589 ++lnum;
14590 }
14591
14592 if (added > 0)
14593 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594}
14595
14596/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014597 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014598 */
14599/*ARGSUSED*/
14600 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014601set_qf_ll_list(wp, list_arg, action_arg, rettv)
14602 win_T *wp;
14603 typval_T *list_arg;
14604 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014605 typval_T *rettv;
14606{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014607#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014608 char_u *act;
14609 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014610#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014611
Bram Moolenaar2641f772005-03-25 21:58:17 +000014612 rettv->vval.v_number = -1;
14613
14614#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014615 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014616 EMSG(_(e_listreq));
14617 else
14618 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014619 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014620
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014621 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014622 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014623 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 if (act == NULL)
14625 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014626 if (*act == 'a' || *act == 'r')
14627 action = *act;
14628 }
14629
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014630 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014631 rettv->vval.v_number = 0;
14632 }
14633#endif
14634}
14635
14636/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014637 * "setloclist()" function
14638 */
14639/*ARGSUSED*/
14640 static void
14641f_setloclist(argvars, rettv)
14642 typval_T *argvars;
14643 typval_T *rettv;
14644{
14645 win_T *win;
14646
14647 rettv->vval.v_number = -1;
14648
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014649 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014650 if (win != NULL)
14651 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14652}
14653
14654/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014655 * "setmatches()" function
14656 */
14657 static void
14658f_setmatches(argvars, rettv)
14659 typval_T *argvars;
14660 typval_T *rettv;
14661{
14662#ifdef FEAT_SEARCH_EXTRA
14663 list_T *l;
14664 listitem_T *li;
14665 dict_T *d;
14666
14667 rettv->vval.v_number = -1;
14668 if (argvars[0].v_type != VAR_LIST)
14669 {
14670 EMSG(_(e_listreq));
14671 return;
14672 }
14673 if ((l = argvars[0].vval.v_list) != NULL)
14674 {
14675
14676 /* To some extent make sure that we are dealing with a list from
14677 * "getmatches()". */
14678 li = l->lv_first;
14679 while (li != NULL)
14680 {
14681 if (li->li_tv.v_type != VAR_DICT
14682 || (d = li->li_tv.vval.v_dict) == NULL)
14683 {
14684 EMSG(_(e_invarg));
14685 return;
14686 }
14687 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14688 && dict_find(d, (char_u *)"pattern", -1) != NULL
14689 && dict_find(d, (char_u *)"priority", -1) != NULL
14690 && dict_find(d, (char_u *)"id", -1) != NULL))
14691 {
14692 EMSG(_(e_invarg));
14693 return;
14694 }
14695 li = li->li_next;
14696 }
14697
14698 clear_matches(curwin);
14699 li = l->lv_first;
14700 while (li != NULL)
14701 {
14702 d = li->li_tv.vval.v_dict;
14703 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14704 get_dict_string(d, (char_u *)"pattern", FALSE),
14705 (int)get_dict_number(d, (char_u *)"priority"),
14706 (int)get_dict_number(d, (char_u *)"id"));
14707 li = li->li_next;
14708 }
14709 rettv->vval.v_number = 0;
14710 }
14711#endif
14712}
14713
14714/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014715 * "setpos()" function
14716 */
14717/*ARGSUSED*/
14718 static void
14719f_setpos(argvars, rettv)
14720 typval_T *argvars;
14721 typval_T *rettv;
14722{
14723 pos_T pos;
14724 int fnum;
14725 char_u *name;
14726
14727 name = get_tv_string_chk(argvars);
14728 if (name != NULL)
14729 {
14730 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14731 {
14732 --pos.col;
14733 if (name[0] == '.') /* cursor */
14734 {
14735 if (fnum == curbuf->b_fnum)
14736 {
14737 curwin->w_cursor = pos;
14738 check_cursor();
14739 }
14740 else
14741 EMSG(_(e_invarg));
14742 }
14743 else if (name[0] == '\'') /* mark */
14744 (void)setmark_pos(name[1], &pos, fnum);
14745 else
14746 EMSG(_(e_invarg));
14747 }
14748 }
14749}
14750
14751/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014752 * "setqflist()" function
14753 */
14754/*ARGSUSED*/
14755 static void
14756f_setqflist(argvars, rettv)
14757 typval_T *argvars;
14758 typval_T *rettv;
14759{
14760 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14761}
14762
14763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 * "setreg()" function
14765 */
14766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014767f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014768 typval_T *argvars;
14769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770{
14771 int regname;
14772 char_u *strregname;
14773 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 int append;
14776 char_u yank_type;
14777 long block_len;
14778
14779 block_len = -1;
14780 yank_type = MAUTO;
14781 append = FALSE;
14782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014783 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 if (strregname == NULL)
14787 return; /* type error; errmsg already given */
14788 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 if (regname == 0 || regname == '@')
14790 regname = '"';
14791 else if (regname == '=')
14792 return;
14793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014794 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 stropt = get_tv_string_chk(&argvars[2]);
14797 if (stropt == NULL)
14798 return; /* type error */
14799 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800 switch (*stropt)
14801 {
14802 case 'a': case 'A': /* append */
14803 append = TRUE;
14804 break;
14805 case 'v': case 'c': /* character-wise selection */
14806 yank_type = MCHAR;
14807 break;
14808 case 'V': case 'l': /* line-wise selection */
14809 yank_type = MLINE;
14810 break;
14811#ifdef FEAT_VISUAL
14812 case 'b': case Ctrl_V: /* block-wise selection */
14813 yank_type = MBLOCK;
14814 if (VIM_ISDIGIT(stropt[1]))
14815 {
14816 ++stropt;
14817 block_len = getdigits(&stropt) - 1;
14818 --stropt;
14819 }
14820 break;
14821#endif
14822 }
14823 }
14824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 strval = get_tv_string_chk(&argvars[1]);
14826 if (strval != NULL)
14827 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830}
14831
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014832/*
14833 * "settabwinvar()" function
14834 */
14835 static void
14836f_settabwinvar(argvars, rettv)
14837 typval_T *argvars;
14838 typval_T *rettv;
14839{
14840 setwinvar(argvars, rettv, 1);
14841}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842
14843/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014844 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014847f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 typval_T *argvars;
14849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014851 setwinvar(argvars, rettv, 0);
14852}
14853
14854/*
14855 * "setwinvar()" and "settabwinvar()" functions
14856 */
14857 static void
14858setwinvar(argvars, rettv, off)
14859 typval_T *argvars;
14860 typval_T *rettv;
14861 int off;
14862{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 win_T *win;
14864#ifdef FEAT_WINDOWS
14865 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014866 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867#endif
14868 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014869 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014871 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014873 rettv->vval.v_number = 0;
14874
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 if (check_restricted() || check_secure())
14876 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014877
14878#ifdef FEAT_WINDOWS
14879 if (off == 1)
14880 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14881 else
14882 tp = curtab;
14883#endif
14884 win = find_win_by_nr(&argvars[off], tp);
14885 varname = get_tv_string_chk(&argvars[off + 1]);
14886 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887
14888 if (win != NULL && varname != NULL && varp != NULL)
14889 {
14890#ifdef FEAT_WINDOWS
14891 /* set curwin to be our win, temporarily */
14892 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014893 save_curtab = curtab;
14894 goto_tabpage_tp(tp);
14895 if (!win_valid(win))
14896 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 curwin = win;
14898 curbuf = curwin->w_buffer;
14899#endif
14900
14901 if (*varname == '&')
14902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014903 long numval;
14904 char_u *strval;
14905 int error = FALSE;
14906
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014908 numval = get_tv_number_chk(varp, &error);
14909 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910 if (!error && strval != NULL)
14911 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 }
14913 else
14914 {
14915 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14916 if (winvarname != NULL)
14917 {
14918 STRCPY(winvarname, "w:");
14919 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014920 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 vim_free(winvarname);
14922 }
14923 }
14924
14925#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014926 /* Restore current tabpage and window, if still valid (autocomands can
14927 * make them invalid). */
14928 if (valid_tabpage(save_curtab))
14929 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 if (win_valid(save_curwin))
14931 {
14932 curwin = save_curwin;
14933 curbuf = curwin->w_buffer;
14934 }
14935#endif
14936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937}
14938
14939/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014940 * "shellescape({string})" function
14941 */
14942 static void
14943f_shellescape(argvars, rettv)
14944 typval_T *argvars;
14945 typval_T *rettv;
14946{
14947 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14948 rettv->v_type = VAR_STRING;
14949}
14950
14951/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014952 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 */
14954 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014956 typval_T *argvars;
14957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 p = get_tv_string(&argvars[0]);
14962 rettv->vval.v_string = vim_strsave(p);
14963 simplify_filename(rettv->vval.v_string); /* simplify in place */
14964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965}
14966
Bram Moolenaar0d660222005-01-07 21:51:51 +000014967static int
14968#ifdef __BORLANDC__
14969 _RTLENTRYF
14970#endif
14971 item_compare __ARGS((const void *s1, const void *s2));
14972static int
14973#ifdef __BORLANDC__
14974 _RTLENTRYF
14975#endif
14976 item_compare2 __ARGS((const void *s1, const void *s2));
14977
14978static int item_compare_ic;
14979static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014980static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981#define ITEM_COMPARE_FAIL 999
14982
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014986 static int
14987#ifdef __BORLANDC__
14988_RTLENTRYF
14989#endif
14990item_compare(s1, s2)
14991 const void *s1;
14992 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994 char_u *p1, *p2;
14995 char_u *tofree1, *tofree2;
14996 int res;
14997 char_u numbuf1[NUMBUFLEN];
14998 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015000 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15001 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015002 if (p1 == NULL)
15003 p1 = (char_u *)"";
15004 if (p2 == NULL)
15005 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006 if (item_compare_ic)
15007 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015009 res = STRCMP(p1, p2);
15010 vim_free(tofree1);
15011 vim_free(tofree2);
15012 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013}
15014
15015 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016#ifdef __BORLANDC__
15017_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019item_compare2(s1, s2)
15020 const void *s1;
15021 const void *s2;
15022{
15023 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015025 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015028 /* shortcut after failure in previous call; compare all items equal */
15029 if (item_compare_func_err)
15030 return 0;
15031
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015032 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15033 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15035 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036
15037 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015038 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015039 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040 clear_tv(&argv[0]);
15041 clear_tv(&argv[1]);
15042
15043 if (res == FAIL)
15044 res = ITEM_COMPARE_FAIL;
15045 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046 /* return value has wrong type */
15047 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15048 if (item_compare_func_err)
15049 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050 clear_tv(&rettv);
15051 return res;
15052}
15053
15054/*
15055 * "sort({list})" function
15056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015059 typval_T *argvars;
15060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061{
Bram Moolenaar33570922005-01-25 22:26:29 +000015062 list_T *l;
15063 listitem_T *li;
15064 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 long len;
15066 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 rettv->vval.v_number = 0;
15069 if (argvars[0].v_type != VAR_LIST)
15070 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 else
15072 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015074 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 return;
15076 rettv->vval.v_list = l;
15077 rettv->v_type = VAR_LIST;
15078 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 len = list_len(l);
15081 if (len <= 1)
15082 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 item_compare_ic = FALSE;
15085 item_compare_func = NULL;
15086 if (argvars[1].v_type != VAR_UNKNOWN)
15087 {
15088 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090 else
15091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 int error = FALSE;
15093
15094 i = get_tv_number_chk(&argvars[1], &error);
15095 if (error)
15096 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 if (i == 1)
15098 item_compare_ic = TRUE;
15099 else
15100 item_compare_func = get_tv_string(&argvars[1]);
15101 }
15102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 if (ptrs == NULL)
15107 return;
15108 i = 0;
15109 for (li = l->lv_first; li != NULL; li = li->li_next)
15110 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015112 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 /* test the compare function */
15114 if (item_compare_func != NULL
15115 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15116 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015117 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 {
15120 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 item_compare_func == NULL ? item_compare : item_compare2);
15123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015124 if (!item_compare_func_err)
15125 {
15126 /* Clear the List and append the items in the sorted order. */
15127 l->lv_first = l->lv_last = NULL;
15128 l->lv_len = 0;
15129 for (i = 0; i < len; ++i)
15130 list_append(l, ptrs[i]);
15131 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132 }
15133
15134 vim_free(ptrs);
15135 }
15136}
15137
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015138/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015139 * "soundfold({word})" function
15140 */
15141 static void
15142f_soundfold(argvars, rettv)
15143 typval_T *argvars;
15144 typval_T *rettv;
15145{
15146 char_u *s;
15147
15148 rettv->v_type = VAR_STRING;
15149 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015150#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015151 rettv->vval.v_string = eval_soundfold(s);
15152#else
15153 rettv->vval.v_string = vim_strsave(s);
15154#endif
15155}
15156
15157/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015158 * "spellbadword()" function
15159 */
15160/* ARGSUSED */
15161 static void
15162f_spellbadword(argvars, rettv)
15163 typval_T *argvars;
15164 typval_T *rettv;
15165{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015166 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015167 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015168 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015169
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015170 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015171 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015172
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015173#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015174 if (argvars[0].v_type == VAR_UNKNOWN)
15175 {
15176 /* Find the start and length of the badly spelled word. */
15177 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15178 if (len != 0)
15179 word = ml_get_cursor();
15180 }
15181 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15182 {
15183 char_u *str = get_tv_string_chk(&argvars[0]);
15184 int capcol = -1;
15185
15186 if (str != NULL)
15187 {
15188 /* Check the argument for spelling. */
15189 while (*str != NUL)
15190 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015191 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015192 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015193 {
15194 word = str;
15195 break;
15196 }
15197 str += len;
15198 }
15199 }
15200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015201#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015202
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015203 list_append_string(rettv->vval.v_list, word, len);
15204 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015205 attr == HLF_SPB ? "bad" :
15206 attr == HLF_SPR ? "rare" :
15207 attr == HLF_SPL ? "local" :
15208 attr == HLF_SPC ? "caps" :
15209 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015210}
15211
15212/*
15213 * "spellsuggest()" function
15214 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015215/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015216 static void
15217f_spellsuggest(argvars, rettv)
15218 typval_T *argvars;
15219 typval_T *rettv;
15220{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015221#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015222 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015223 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015224 int maxcount;
15225 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015226 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015227 listitem_T *li;
15228 int need_capital = FALSE;
15229#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015230
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015231 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015232 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015233
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015234#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015235 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15236 {
15237 str = get_tv_string(&argvars[0]);
15238 if (argvars[1].v_type != VAR_UNKNOWN)
15239 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015240 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015241 if (maxcount <= 0)
15242 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015243 if (argvars[2].v_type != VAR_UNKNOWN)
15244 {
15245 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15246 if (typeerr)
15247 return;
15248 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015249 }
15250 else
15251 maxcount = 25;
15252
Bram Moolenaar4770d092006-01-12 23:22:24 +000015253 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015254
15255 for (i = 0; i < ga.ga_len; ++i)
15256 {
15257 str = ((char_u **)ga.ga_data)[i];
15258
15259 li = listitem_alloc();
15260 if (li == NULL)
15261 vim_free(str);
15262 else
15263 {
15264 li->li_tv.v_type = VAR_STRING;
15265 li->li_tv.v_lock = 0;
15266 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015267 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015268 }
15269 }
15270 ga_clear(&ga);
15271 }
15272#endif
15273}
15274
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 typval_T *argvars;
15278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279{
15280 char_u *str;
15281 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015282 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015283 regmatch_T regmatch;
15284 char_u patbuf[NUMBUFLEN];
15285 char_u *save_cpo;
15286 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015288 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290
15291 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15292 save_cpo = p_cpo;
15293 p_cpo = (char_u *)"";
15294
15295 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015296 if (argvars[1].v_type != VAR_UNKNOWN)
15297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015298 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15299 if (pat == NULL)
15300 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015301 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015302 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015303 }
15304 if (pat == NULL || *pat == NUL)
15305 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015306
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015307 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 if (typeerr)
15310 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311
Bram Moolenaar0d660222005-01-07 21:51:51 +000015312 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15313 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015316 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015318 if (*str == NUL)
15319 match = FALSE; /* empty item at the end */
15320 else
15321 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 if (match)
15323 end = regmatch.startp[0];
15324 else
15325 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015326 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15327 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 if (list_append_string(rettv->vval.v_list, str,
15330 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332 }
15333 if (!match)
15334 break;
15335 /* Advance to just after the match. */
15336 if (regmatch.endp[0] > str)
15337 col = 0;
15338 else
15339 {
15340 /* Don't get stuck at the same match. */
15341#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015342 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343#else
15344 col = 1;
15345#endif
15346 }
15347 str = regmatch.endp[0];
15348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352
Bram Moolenaar0d660222005-01-07 21:51:51 +000015353 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354}
15355
Bram Moolenaar2c932302006-03-18 21:42:09 +000015356/*
15357 * "str2nr()" function
15358 */
15359 static void
15360f_str2nr(argvars, rettv)
15361 typval_T *argvars;
15362 typval_T *rettv;
15363{
15364 int base = 10;
15365 char_u *p;
15366 long n;
15367
15368 if (argvars[1].v_type != VAR_UNKNOWN)
15369 {
15370 base = get_tv_number(&argvars[1]);
15371 if (base != 8 && base != 10 && base != 16)
15372 {
15373 EMSG(_(e_invarg));
15374 return;
15375 }
15376 }
15377
15378 p = skipwhite(get_tv_string(&argvars[0]));
15379 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15380 rettv->vval.v_number = n;
15381}
15382
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383#ifdef HAVE_STRFTIME
15384/*
15385 * "strftime({format}[, {time}])" function
15386 */
15387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015389 typval_T *argvars;
15390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391{
15392 char_u result_buf[256];
15393 struct tm *curtime;
15394 time_t seconds;
15395 char_u *p;
15396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015400 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 seconds = time(NULL);
15402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 curtime = localtime(&seconds);
15405 /* MSVC returns NULL for an invalid value of seconds. */
15406 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 else
15409 {
15410# ifdef FEAT_MBYTE
15411 vimconv_T conv;
15412 char_u *enc;
15413
15414 conv.vc_type = CONV_NONE;
15415 enc = enc_locale();
15416 convert_setup(&conv, p_enc, enc);
15417 if (conv.vc_type != CONV_NONE)
15418 p = string_convert(&conv, p, NULL);
15419# endif
15420 if (p != NULL)
15421 (void)strftime((char *)result_buf, sizeof(result_buf),
15422 (char *)p, curtime);
15423 else
15424 result_buf[0] = NUL;
15425
15426# ifdef FEAT_MBYTE
15427 if (conv.vc_type != CONV_NONE)
15428 vim_free(p);
15429 convert_setup(&conv, enc, p_enc);
15430 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 else
15433# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015434 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435
15436# ifdef FEAT_MBYTE
15437 /* Release conversion descriptors */
15438 convert_setup(&conv, NULL, NULL);
15439 vim_free(enc);
15440# endif
15441 }
15442}
15443#endif
15444
15445/*
15446 * "stridx()" function
15447 */
15448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015450 typval_T *argvars;
15451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452{
15453 char_u buf[NUMBUFLEN];
15454 char_u *needle;
15455 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015458 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015460 needle = get_tv_string_chk(&argvars[1]);
15461 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015462 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015463 if (needle == NULL || haystack == NULL)
15464 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465
Bram Moolenaar33570922005-01-25 22:26:29 +000015466 if (argvars[2].v_type != VAR_UNKNOWN)
15467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015468 int error = FALSE;
15469
15470 start_idx = get_tv_number_chk(&argvars[2], &error);
15471 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015473 if (start_idx >= 0)
15474 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 }
15476
15477 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15478 if (pos != NULL)
15479 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480}
15481
15482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015483 * "string()" function
15484 */
15485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 typval_T *argvars;
15488 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015489{
15490 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015491 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015494 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015495 /* Make a copy if we have a value but it's not in allocate memory. */
15496 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498}
15499
15500/*
15501 * "strlen()" function
15502 */
15503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015504f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *argvars;
15506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 rettv->vval.v_number = (varnumber_T)(STRLEN(
15509 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510}
15511
15512/*
15513 * "strpart()" function
15514 */
15515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015517 typval_T *argvars;
15518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519{
15520 char_u *p;
15521 int n;
15522 int len;
15523 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015524 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015526 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 slen = (int)STRLEN(p);
15528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015529 n = get_tv_number_chk(&argvars[1], &error);
15530 if (error)
15531 len = 0;
15532 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 else
15535 len = slen - n; /* default len: all bytes that are available. */
15536
15537 /*
15538 * Only return the overlap between the specified part and the actual
15539 * string.
15540 */
15541 if (n < 0)
15542 {
15543 len += n;
15544 n = 0;
15545 }
15546 else if (n > slen)
15547 n = slen;
15548 if (len < 0)
15549 len = 0;
15550 else if (n + len > slen)
15551 len = slen - n;
15552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->v_type = VAR_STRING;
15554 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555}
15556
15557/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015558 * "strridx()" function
15559 */
15560 static void
15561f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015562 typval_T *argvars;
15563 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015564{
15565 char_u buf[NUMBUFLEN];
15566 char_u *needle;
15567 char_u *haystack;
15568 char_u *rest;
15569 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015570 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015572 needle = get_tv_string_chk(&argvars[1]);
15573 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015574
15575 rettv->vval.v_number = -1;
15576 if (needle == NULL || haystack == NULL)
15577 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015578
15579 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015580 if (argvars[2].v_type != VAR_UNKNOWN)
15581 {
15582 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015584 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015585 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015586 }
15587 else
15588 end_idx = haystack_len;
15589
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015591 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015593 lastmatch = haystack + end_idx;
15594 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015595 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015596 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 for (rest = haystack; *rest != '\0'; ++rest)
15598 {
15599 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015600 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601 break;
15602 lastmatch = rest;
15603 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015604 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015605
15606 if (lastmatch == NULL)
15607 rettv->vval.v_number = -1;
15608 else
15609 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15610}
15611
15612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 * "strtrans()" function
15614 */
15615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015616f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015617 typval_T *argvars;
15618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015620 rettv->v_type = VAR_STRING;
15621 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622}
15623
15624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015625 * "submatch()" function
15626 */
15627 static void
15628f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015629 typval_T *argvars;
15630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015631{
15632 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015633 rettv->vval.v_string =
15634 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015635}
15636
15637/*
15638 * "substitute()" function
15639 */
15640 static void
15641f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015642 typval_T *argvars;
15643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015644{
15645 char_u patbuf[NUMBUFLEN];
15646 char_u subbuf[NUMBUFLEN];
15647 char_u flagsbuf[NUMBUFLEN];
15648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015649 char_u *str = get_tv_string_chk(&argvars[0]);
15650 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15651 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15652 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15653
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15656 rettv->vval.v_string = NULL;
15657 else
15658 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015659}
15660
15661/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015662 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 */
15664/*ARGSUSED*/
15665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015666f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015667 typval_T *argvars;
15668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669{
15670 int id = 0;
15671#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015672 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 long col;
15674 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015675 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015677 lnum = get_tv_lnum(argvars); /* -1 on type error */
15678 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15679 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015682 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015683 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684#endif
15685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015686 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687}
15688
15689/*
15690 * "synIDattr(id, what [, mode])" function
15691 */
15692/*ARGSUSED*/
15693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015694f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 typval_T *argvars;
15696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697{
15698 char_u *p = NULL;
15699#ifdef FEAT_SYN_HL
15700 int id;
15701 char_u *what;
15702 char_u *mode;
15703 char_u modebuf[NUMBUFLEN];
15704 int modec;
15705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015706 id = get_tv_number(&argvars[0]);
15707 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015710 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 modec = TOLOWER_ASC(mode[0]);
15712 if (modec != 't' && modec != 'c'
15713#ifdef FEAT_GUI
15714 && modec != 'g'
15715#endif
15716 )
15717 modec = 0; /* replace invalid with current */
15718 }
15719 else
15720 {
15721#ifdef FEAT_GUI
15722 if (gui.in_use)
15723 modec = 'g';
15724 else
15725#endif
15726 if (t_colors > 1)
15727 modec = 'c';
15728 else
15729 modec = 't';
15730 }
15731
15732
15733 switch (TOLOWER_ASC(what[0]))
15734 {
15735 case 'b':
15736 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15737 p = highlight_color(id, what, modec);
15738 else /* bold */
15739 p = highlight_has_attr(id, HL_BOLD, modec);
15740 break;
15741
15742 case 'f': /* fg[#] */
15743 p = highlight_color(id, what, modec);
15744 break;
15745
15746 case 'i':
15747 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15748 p = highlight_has_attr(id, HL_INVERSE, modec);
15749 else /* italic */
15750 p = highlight_has_attr(id, HL_ITALIC, modec);
15751 break;
15752
15753 case 'n': /* name */
15754 p = get_highlight_name(NULL, id - 1);
15755 break;
15756
15757 case 'r': /* reverse */
15758 p = highlight_has_attr(id, HL_INVERSE, modec);
15759 break;
15760
15761 case 's': /* standout */
15762 p = highlight_has_attr(id, HL_STANDOUT, modec);
15763 break;
15764
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015765 case 'u':
15766 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15767 /* underline */
15768 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15769 else
15770 /* undercurl */
15771 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 break;
15773 }
15774
15775 if (p != NULL)
15776 p = vim_strsave(p);
15777#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015778 rettv->v_type = VAR_STRING;
15779 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780}
15781
15782/*
15783 * "synIDtrans(id)" function
15784 */
15785/*ARGSUSED*/
15786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 typval_T *argvars;
15789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790{
15791 int id;
15792
15793#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795
15796 if (id > 0)
15797 id = syn_get_final_id(id);
15798 else
15799#endif
15800 id = 0;
15801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803}
15804
15805/*
15806 * "system()" function
15807 */
15808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015810 typval_T *argvars;
15811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015813 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015815 char_u *infile = NULL;
15816 char_u buf[NUMBUFLEN];
15817 int err = FALSE;
15818 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015820 if (check_restricted() || check_secure())
15821 return;
15822
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015823 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015824 {
15825 /*
15826 * Write the string to a temp file, to be used for input of the shell
15827 * command.
15828 */
15829 if ((infile = vim_tempname('i')) == NULL)
15830 {
15831 EMSG(_(e_notmp));
15832 return;
15833 }
15834
15835 fd = mch_fopen((char *)infile, WRITEBIN);
15836 if (fd == NULL)
15837 {
15838 EMSG2(_(e_notopen), infile);
15839 goto done;
15840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 p = get_tv_string_buf_chk(&argvars[1], buf);
15842 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015843 {
15844 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015845 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015846 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015847 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15848 err = TRUE;
15849 if (fclose(fd) != 0)
15850 err = TRUE;
15851 if (err)
15852 {
15853 EMSG(_("E677: Error writing temp file"));
15854 goto done;
15855 }
15856 }
15857
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015858 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15859 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015860
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861#ifdef USE_CR
15862 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015863 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 {
15865 char_u *s;
15866
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015867 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 {
15869 if (*s == CAR)
15870 *s = NL;
15871 }
15872 }
15873#else
15874# ifdef USE_CRNL
15875 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015876 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 {
15878 char_u *s, *d;
15879
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015880 d = res;
15881 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 {
15883 if (s[0] == CAR && s[1] == NL)
15884 ++s;
15885 *d++ = *s;
15886 }
15887 *d = NUL;
15888 }
15889# endif
15890#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015891
15892done:
15893 if (infile != NULL)
15894 {
15895 mch_remove(infile);
15896 vim_free(infile);
15897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015898 rettv->v_type = VAR_STRING;
15899 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900}
15901
15902/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015903 * "tabpagebuflist()" function
15904 */
15905/* ARGSUSED */
15906 static void
15907f_tabpagebuflist(argvars, rettv)
15908 typval_T *argvars;
15909 typval_T *rettv;
15910{
15911#ifndef FEAT_WINDOWS
15912 rettv->vval.v_number = 0;
15913#else
15914 tabpage_T *tp;
15915 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015916
15917 if (argvars[0].v_type == VAR_UNKNOWN)
15918 wp = firstwin;
15919 else
15920 {
15921 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15922 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015923 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015924 }
15925 if (wp == NULL)
15926 rettv->vval.v_number = 0;
15927 else
15928 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015929 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015930 rettv->vval.v_number = 0;
15931 else
15932 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015933 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015934 if (list_append_number(rettv->vval.v_list,
15935 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015936 break;
15937 }
15938 }
15939#endif
15940}
15941
15942
15943/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015944 * "tabpagenr()" function
15945 */
15946/* ARGSUSED */
15947 static void
15948f_tabpagenr(argvars, rettv)
15949 typval_T *argvars;
15950 typval_T *rettv;
15951{
15952 int nr = 1;
15953#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015954 char_u *arg;
15955
15956 if (argvars[0].v_type != VAR_UNKNOWN)
15957 {
15958 arg = get_tv_string_chk(&argvars[0]);
15959 nr = 0;
15960 if (arg != NULL)
15961 {
15962 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015963 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015964 else
15965 EMSG2(_(e_invexpr2), arg);
15966 }
15967 }
15968 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015969 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015970#endif
15971 rettv->vval.v_number = nr;
15972}
15973
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015974
15975#ifdef FEAT_WINDOWS
15976static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15977
15978/*
15979 * Common code for tabpagewinnr() and winnr().
15980 */
15981 static int
15982get_winnr(tp, argvar)
15983 tabpage_T *tp;
15984 typval_T *argvar;
15985{
15986 win_T *twin;
15987 int nr = 1;
15988 win_T *wp;
15989 char_u *arg;
15990
15991 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15992 if (argvar->v_type != VAR_UNKNOWN)
15993 {
15994 arg = get_tv_string_chk(argvar);
15995 if (arg == NULL)
15996 nr = 0; /* type error; errmsg already given */
15997 else if (STRCMP(arg, "$") == 0)
15998 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15999 else if (STRCMP(arg, "#") == 0)
16000 {
16001 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16002 if (twin == NULL)
16003 nr = 0;
16004 }
16005 else
16006 {
16007 EMSG2(_(e_invexpr2), arg);
16008 nr = 0;
16009 }
16010 }
16011
16012 if (nr > 0)
16013 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16014 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016015 {
16016 if (wp == NULL)
16017 {
16018 /* didn't find it in this tabpage */
16019 nr = 0;
16020 break;
16021 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016022 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016023 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016024 return nr;
16025}
16026#endif
16027
16028/*
16029 * "tabpagewinnr()" function
16030 */
16031/* ARGSUSED */
16032 static void
16033f_tabpagewinnr(argvars, rettv)
16034 typval_T *argvars;
16035 typval_T *rettv;
16036{
16037 int nr = 1;
16038#ifdef FEAT_WINDOWS
16039 tabpage_T *tp;
16040
16041 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16042 if (tp == NULL)
16043 nr = 0;
16044 else
16045 nr = get_winnr(tp, &argvars[1]);
16046#endif
16047 rettv->vval.v_number = nr;
16048}
16049
16050
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016051/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016052 * "tagfiles()" function
16053 */
16054/*ARGSUSED*/
16055 static void
16056f_tagfiles(argvars, rettv)
16057 typval_T *argvars;
16058 typval_T *rettv;
16059{
16060 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016061 tagname_T tn;
16062 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016064 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016065 {
16066 rettv->vval.v_number = 0;
16067 return;
16068 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016069
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016070 for (first = TRUE; ; first = FALSE)
16071 if (get_tagfname(&tn, first, fname) == FAIL
16072 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016073 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016074 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016075}
16076
16077/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016078 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016079 */
16080 static void
16081f_taglist(argvars, rettv)
16082 typval_T *argvars;
16083 typval_T *rettv;
16084{
16085 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016086
16087 tag_pattern = get_tv_string(&argvars[0]);
16088
16089 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016090 if (*tag_pattern == NUL)
16091 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016093 if (rettv_list_alloc(rettv) == OK)
16094 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016095}
16096
16097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 * "tempname()" function
16099 */
16100/*ARGSUSED*/
16101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016102f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016103 typval_T *argvars;
16104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105{
16106 static int x = 'A';
16107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016108 rettv->v_type = VAR_STRING;
16109 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110
16111 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16112 * names. Skip 'I' and 'O', they are used for shell redirection. */
16113 do
16114 {
16115 if (x == 'Z')
16116 x = '0';
16117 else if (x == '9')
16118 x = 'A';
16119 else
16120 {
16121#ifdef EBCDIC
16122 if (x == 'I')
16123 x = 'J';
16124 else if (x == 'R')
16125 x = 'S';
16126 else
16127#endif
16128 ++x;
16129 }
16130 } while (x == 'I' || x == 'O');
16131}
16132
16133/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016134 * "test(list)" function: Just checking the walls...
16135 */
16136/*ARGSUSED*/
16137 static void
16138f_test(argvars, rettv)
16139 typval_T *argvars;
16140 typval_T *rettv;
16141{
16142 /* Used for unit testing. Change the code below to your liking. */
16143#if 0
16144 listitem_T *li;
16145 list_T *l;
16146 char_u *bad, *good;
16147
16148 if (argvars[0].v_type != VAR_LIST)
16149 return;
16150 l = argvars[0].vval.v_list;
16151 if (l == NULL)
16152 return;
16153 li = l->lv_first;
16154 if (li == NULL)
16155 return;
16156 bad = get_tv_string(&li->li_tv);
16157 li = li->li_next;
16158 if (li == NULL)
16159 return;
16160 good = get_tv_string(&li->li_tv);
16161 rettv->vval.v_number = test_edit_score(bad, good);
16162#endif
16163}
16164
16165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 * "tolower(string)" function
16167 */
16168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016169f_tolower(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 char_u *p;
16174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175 p = vim_strsave(get_tv_string(&argvars[0]));
16176 rettv->v_type = VAR_STRING;
16177 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178
16179 if (p != NULL)
16180 while (*p != NUL)
16181 {
16182#ifdef FEAT_MBYTE
16183 int l;
16184
16185 if (enc_utf8)
16186 {
16187 int c, lc;
16188
16189 c = utf_ptr2char(p);
16190 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016191 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 /* TODO: reallocate string when byte count changes. */
16193 if (utf_char2len(lc) == l)
16194 utf_char2bytes(lc, p);
16195 p += l;
16196 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016197 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 p += l; /* skip multi-byte character */
16199 else
16200#endif
16201 {
16202 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16203 ++p;
16204 }
16205 }
16206}
16207
16208/*
16209 * "toupper(string)" function
16210 */
16211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 typval_T *argvars;
16214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016216 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218}
16219
16220/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016221 * "tr(string, fromstr, tostr)" function
16222 */
16223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016225 typval_T *argvars;
16226 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016227{
16228 char_u *instr;
16229 char_u *fromstr;
16230 char_u *tostr;
16231 char_u *p;
16232#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016233 int inlen;
16234 int fromlen;
16235 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016236 int idx;
16237 char_u *cpstr;
16238 int cplen;
16239 int first = TRUE;
16240#endif
16241 char_u buf[NUMBUFLEN];
16242 char_u buf2[NUMBUFLEN];
16243 garray_T ga;
16244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16247 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016248
16249 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 rettv->v_type = VAR_STRING;
16251 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016252 if (fromstr == NULL || tostr == NULL)
16253 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016254 ga_init2(&ga, (int)sizeof(char), 80);
16255
16256#ifdef FEAT_MBYTE
16257 if (!has_mbyte)
16258#endif
16259 /* not multi-byte: fromstr and tostr must be the same length */
16260 if (STRLEN(fromstr) != STRLEN(tostr))
16261 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016262#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016263error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016264#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016265 EMSG2(_(e_invarg2), fromstr);
16266 ga_clear(&ga);
16267 return;
16268 }
16269
16270 /* fromstr and tostr have to contain the same number of chars */
16271 while (*instr != NUL)
16272 {
16273#ifdef FEAT_MBYTE
16274 if (has_mbyte)
16275 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016276 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016277 cpstr = instr;
16278 cplen = inlen;
16279 idx = 0;
16280 for (p = fromstr; *p != NUL; p += fromlen)
16281 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016282 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016283 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16284 {
16285 for (p = tostr; *p != NUL; p += tolen)
16286 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016287 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016288 if (idx-- == 0)
16289 {
16290 cplen = tolen;
16291 cpstr = p;
16292 break;
16293 }
16294 }
16295 if (*p == NUL) /* tostr is shorter than fromstr */
16296 goto error;
16297 break;
16298 }
16299 ++idx;
16300 }
16301
16302 if (first && cpstr == instr)
16303 {
16304 /* Check that fromstr and tostr have the same number of
16305 * (multi-byte) characters. Done only once when a character
16306 * of instr doesn't appear in fromstr. */
16307 first = FALSE;
16308 for (p = tostr; *p != NUL; p += tolen)
16309 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016310 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016311 --idx;
16312 }
16313 if (idx != 0)
16314 goto error;
16315 }
16316
16317 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016318 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016319 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016320
16321 instr += inlen;
16322 }
16323 else
16324#endif
16325 {
16326 /* When not using multi-byte chars we can do it faster. */
16327 p = vim_strchr(fromstr, *instr);
16328 if (p != NULL)
16329 ga_append(&ga, tostr[p - fromstr]);
16330 else
16331 ga_append(&ga, *instr);
16332 ++instr;
16333 }
16334 }
16335
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016336 /* add a terminating NUL */
16337 ga_grow(&ga, 1);
16338 ga_append(&ga, NUL);
16339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016340 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016341}
16342
16343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 * "type(expr)" function
16345 */
16346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016347f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016348 typval_T *argvars;
16349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016351 int n;
16352
16353 switch (argvars[0].v_type)
16354 {
16355 case VAR_NUMBER: n = 0; break;
16356 case VAR_STRING: n = 1; break;
16357 case VAR_FUNC: n = 2; break;
16358 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016359 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016360 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16361 }
16362 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363}
16364
16365/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016366 * "values(dict)" function
16367 */
16368 static void
16369f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 typval_T *argvars;
16371 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016372{
16373 dict_list(argvars, rettv, 1);
16374}
16375
16376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 * "virtcol(string)" function
16378 */
16379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016380f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 typval_T *argvars;
16382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383{
16384 colnr_T vcol = 0;
16385 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016386 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016388 fp = var2fpos(&argvars[0], FALSE, &fnum);
16389 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16390 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 {
16392 getvvcol(curwin, fp, NULL, NULL, &vcol);
16393 ++vcol;
16394 }
16395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397}
16398
16399/*
16400 * "visualmode()" function
16401 */
16402/*ARGSUSED*/
16403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016404f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016405 typval_T *argvars;
16406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407{
16408#ifdef FEAT_VISUAL
16409 char_u str[2];
16410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 str[0] = curbuf->b_visual_mode_eval;
16413 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016414 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415
16416 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016417 if ((argvars[0].v_type == VAR_NUMBER
16418 && argvars[0].vval.v_number != 0)
16419 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016420 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 curbuf->b_visual_mode_eval = NUL;
16422#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424#endif
16425}
16426
16427/*
16428 * "winbufnr(nr)" function
16429 */
16430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016432 typval_T *argvars;
16433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434{
16435 win_T *wp;
16436
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016437 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442}
16443
16444/*
16445 * "wincol()" function
16446 */
16447/*ARGSUSED*/
16448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016450 typval_T *argvars;
16451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452{
16453 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457/*
16458 * "winheight(nr)" function
16459 */
16460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 typval_T *argvars;
16463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464{
16465 win_T *wp;
16466
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016467 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016471 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472}
16473
16474/*
16475 * "winline()" function
16476 */
16477/*ARGSUSED*/
16478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016479f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016480 typval_T *argvars;
16481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482{
16483 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016484 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485}
16486
16487/*
16488 * "winnr()" function
16489 */
16490/* ARGSUSED */
16491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 typval_T *argvars;
16494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495{
16496 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016497
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016499 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016501 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502}
16503
16504/*
16505 * "winrestcmd()" function
16506 */
16507/* ARGSUSED */
16508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016509f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016510 typval_T *argvars;
16511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512{
16513#ifdef FEAT_WINDOWS
16514 win_T *wp;
16515 int winnr = 1;
16516 garray_T ga;
16517 char_u buf[50];
16518
16519 ga_init2(&ga, (int)sizeof(char), 70);
16520 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16521 {
16522 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16523 ga_concat(&ga, buf);
16524# ifdef FEAT_VERTSPLIT
16525 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16526 ga_concat(&ga, buf);
16527# endif
16528 ++winnr;
16529 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016530 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016532 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537}
16538
16539/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016540 * "winrestview()" function
16541 */
16542/* ARGSUSED */
16543 static void
16544f_winrestview(argvars, rettv)
16545 typval_T *argvars;
16546 typval_T *rettv;
16547{
16548 dict_T *dict;
16549
16550 if (argvars[0].v_type != VAR_DICT
16551 || (dict = argvars[0].vval.v_dict) == NULL)
16552 EMSG(_(e_invarg));
16553 else
16554 {
16555 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16556 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16557#ifdef FEAT_VIRTUALEDIT
16558 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16559#endif
16560 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016561 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016562
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016563 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016564#ifdef FEAT_DIFF
16565 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16566#endif
16567 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16568 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16569
16570 check_cursor();
16571 changed_cline_bef_curs();
16572 invalidate_botline();
16573 redraw_later(VALID);
16574
16575 if (curwin->w_topline == 0)
16576 curwin->w_topline = 1;
16577 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16578 curwin->w_topline = curbuf->b_ml.ml_line_count;
16579#ifdef FEAT_DIFF
16580 check_topfill(curwin, TRUE);
16581#endif
16582 }
16583}
16584
16585/*
16586 * "winsaveview()" function
16587 */
16588/* ARGSUSED */
16589 static void
16590f_winsaveview(argvars, rettv)
16591 typval_T *argvars;
16592 typval_T *rettv;
16593{
16594 dict_T *dict;
16595
16596 dict = dict_alloc();
16597 if (dict == NULL)
16598 return;
16599 rettv->v_type = VAR_DICT;
16600 rettv->vval.v_dict = dict;
16601 ++dict->dv_refcount;
16602
16603 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16604 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16605#ifdef FEAT_VIRTUALEDIT
16606 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16607#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016608 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016609 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16610
16611 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16612#ifdef FEAT_DIFF
16613 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16614#endif
16615 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16616 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16617}
16618
16619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 * "winwidth(nr)" function
16621 */
16622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 typval_T *argvars;
16625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626{
16627 win_T *wp;
16628
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016629 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 else
16633#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637#endif
16638}
16639
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641 * "writefile()" function
16642 */
16643 static void
16644f_writefile(argvars, rettv)
16645 typval_T *argvars;
16646 typval_T *rettv;
16647{
16648 int binary = FALSE;
16649 char_u *fname;
16650 FILE *fd;
16651 listitem_T *li;
16652 char_u *s;
16653 int ret = 0;
16654 int c;
16655
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016656 if (check_restricted() || check_secure())
16657 return;
16658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659 if (argvars[0].v_type != VAR_LIST)
16660 {
16661 EMSG2(_(e_listarg), "writefile()");
16662 return;
16663 }
16664 if (argvars[0].vval.v_list == NULL)
16665 return;
16666
16667 if (argvars[2].v_type != VAR_UNKNOWN
16668 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16669 binary = TRUE;
16670
16671 /* Always open the file in binary mode, library functions have a mind of
16672 * their own about CR-LF conversion. */
16673 fname = get_tv_string(&argvars[1]);
16674 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16675 {
16676 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16677 ret = -1;
16678 }
16679 else
16680 {
16681 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16682 li = li->li_next)
16683 {
16684 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16685 {
16686 if (*s == '\n')
16687 c = putc(NUL, fd);
16688 else
16689 c = putc(*s, fd);
16690 if (c == EOF)
16691 {
16692 ret = -1;
16693 break;
16694 }
16695 }
16696 if (!binary || li->li_next != NULL)
16697 if (putc('\n', fd) == EOF)
16698 {
16699 ret = -1;
16700 break;
16701 }
16702 if (ret < 0)
16703 {
16704 EMSG(_(e_write));
16705 break;
16706 }
16707 }
16708 fclose(fd);
16709 }
16710
16711 rettv->vval.v_number = ret;
16712}
16713
16714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016716 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 */
16718 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016719var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016720 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016721 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016722 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016724 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016726 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727
Bram Moolenaara5525202006-03-02 22:52:09 +000016728 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016729 if (varp->v_type == VAR_LIST)
16730 {
16731 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016732 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016733 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016734 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016735
16736 l = varp->vval.v_list;
16737 if (l == NULL)
16738 return NULL;
16739
16740 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016741 pos.lnum = list_find_nr(l, 0L, &error);
16742 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016743 return NULL; /* invalid line number */
16744
16745 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016746 pos.col = list_find_nr(l, 1L, &error);
16747 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016748 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016749 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016750
16751 /* We accept "$" for the column number: last column. */
16752 li = list_find(l, 1L);
16753 if (li != NULL && li->li_tv.v_type == VAR_STRING
16754 && li->li_tv.vval.v_string != NULL
16755 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16756 pos.col = len + 1;
16757
Bram Moolenaara5525202006-03-02 22:52:09 +000016758 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016759 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016760 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016761 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016762
Bram Moolenaara5525202006-03-02 22:52:09 +000016763#ifdef FEAT_VIRTUALEDIT
16764 /* Get the virtual offset. Defaults to zero. */
16765 pos.coladd = list_find_nr(l, 2L, &error);
16766 if (error)
16767 pos.coladd = 0;
16768#endif
16769
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016770 return &pos;
16771 }
16772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016773 name = get_tv_string_chk(varp);
16774 if (name == NULL)
16775 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 if (name[0] == '.') /* cursor */
16777 return &curwin->w_cursor;
16778 if (name[0] == '\'') /* mark */
16779 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016780 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16782 return NULL;
16783 return pp;
16784 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016785
16786#ifdef FEAT_VIRTUALEDIT
16787 pos.coladd = 0;
16788#endif
16789
Bram Moolenaar477933c2007-07-17 14:32:23 +000016790 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016791 {
16792 pos.col = 0;
16793 if (name[1] == '0') /* "w0": first visible line */
16794 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016795 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016796 pos.lnum = curwin->w_topline;
16797 return &pos;
16798 }
16799 else if (name[1] == '$') /* "w$": last visible line */
16800 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016801 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016802 pos.lnum = curwin->w_botline - 1;
16803 return &pos;
16804 }
16805 }
16806 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016808 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 {
16810 pos.lnum = curbuf->b_ml.ml_line_count;
16811 pos.col = 0;
16812 }
16813 else
16814 {
16815 pos.lnum = curwin->w_cursor.lnum;
16816 pos.col = (colnr_T)STRLEN(ml_get_curline());
16817 }
16818 return &pos;
16819 }
16820 return NULL;
16821}
16822
16823/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016824 * Convert list in "arg" into a position and optional file number.
16825 * When "fnump" is NULL there is no file number, only 3 items.
16826 * Note that the column is passed on as-is, the caller may want to decrement
16827 * it to use 1 for the first column.
16828 * Return FAIL when conversion is not possible, doesn't check the position for
16829 * validity.
16830 */
16831 static int
16832list2fpos(arg, posp, fnump)
16833 typval_T *arg;
16834 pos_T *posp;
16835 int *fnump;
16836{
16837 list_T *l = arg->vval.v_list;
16838 long i = 0;
16839 long n;
16840
Bram Moolenaarbde35262006-07-23 20:12:24 +000016841 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16842 * when "fnump" isn't NULL and "coladd" is optional. */
16843 if (arg->v_type != VAR_LIST
16844 || l == NULL
16845 || l->lv_len < (fnump == NULL ? 2 : 3)
16846 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016847 return FAIL;
16848
16849 if (fnump != NULL)
16850 {
16851 n = list_find_nr(l, i++, NULL); /* fnum */
16852 if (n < 0)
16853 return FAIL;
16854 if (n == 0)
16855 n = curbuf->b_fnum; /* current buffer */
16856 *fnump = n;
16857 }
16858
16859 n = list_find_nr(l, i++, NULL); /* lnum */
16860 if (n < 0)
16861 return FAIL;
16862 posp->lnum = n;
16863
16864 n = list_find_nr(l, i++, NULL); /* col */
16865 if (n < 0)
16866 return FAIL;
16867 posp->col = n;
16868
16869#ifdef FEAT_VIRTUALEDIT
16870 n = list_find_nr(l, i, NULL);
16871 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016872 posp->coladd = 0;
16873 else
16874 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016875#endif
16876
16877 return OK;
16878}
16879
16880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 * Get the length of an environment variable name.
16882 * Advance "arg" to the first character after the name.
16883 * Return 0 for error.
16884 */
16885 static int
16886get_env_len(arg)
16887 char_u **arg;
16888{
16889 char_u *p;
16890 int len;
16891
16892 for (p = *arg; vim_isIDc(*p); ++p)
16893 ;
16894 if (p == *arg) /* no name found */
16895 return 0;
16896
16897 len = (int)(p - *arg);
16898 *arg = p;
16899 return len;
16900}
16901
16902/*
16903 * Get the length of the name of a function or internal variable.
16904 * "arg" is advanced to the first non-white character after the name.
16905 * Return 0 if something is wrong.
16906 */
16907 static int
16908get_id_len(arg)
16909 char_u **arg;
16910{
16911 char_u *p;
16912 int len;
16913
16914 /* Find the end of the name. */
16915 for (p = *arg; eval_isnamec(*p); ++p)
16916 ;
16917 if (p == *arg) /* no name found */
16918 return 0;
16919
16920 len = (int)(p - *arg);
16921 *arg = skipwhite(p);
16922
16923 return len;
16924}
16925
16926/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016927 * Get the length of the name of a variable or function.
16928 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016930 * Return -1 if curly braces expansion failed.
16931 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 * If the name contains 'magic' {}'s, expand them and return the
16933 * expanded name in an allocated string via 'alias' - caller must free.
16934 */
16935 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016936get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 char_u **arg;
16938 char_u **alias;
16939 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016940 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941{
16942 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 char_u *p;
16944 char_u *expr_start;
16945 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946
16947 *alias = NULL; /* default to no alias */
16948
16949 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16950 && (*arg)[2] == (int)KE_SNR)
16951 {
16952 /* hard coded <SNR>, already translated */
16953 *arg += 3;
16954 return get_id_len(arg) + 3;
16955 }
16956 len = eval_fname_script(*arg);
16957 if (len > 0)
16958 {
16959 /* literal "<SID>", "s:" or "<SNR>" */
16960 *arg += len;
16961 }
16962
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016964 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016966 p = find_name_end(*arg, &expr_start, &expr_end,
16967 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 if (expr_start != NULL)
16969 {
16970 char_u *temp_string;
16971
16972 if (!evaluate)
16973 {
16974 len += (int)(p - *arg);
16975 *arg = skipwhite(p);
16976 return len;
16977 }
16978
16979 /*
16980 * Include any <SID> etc in the expanded string:
16981 * Thus the -len here.
16982 */
16983 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16984 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016985 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 *alias = temp_string;
16987 *arg = skipwhite(p);
16988 return (int)STRLEN(temp_string);
16989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990
16991 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016992 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 EMSG2(_(e_invexpr2), *arg);
16994
16995 return len;
16996}
16997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998/*
16999 * Find the end of a variable or function name, taking care of magic braces.
17000 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17001 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017002 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003 * Return a pointer to just after the name. Equal to "arg" if there is no
17004 * valid name.
17005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017007find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 char_u *arg;
17009 char_u **expr_start;
17010 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017011 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 int mb_nest = 0;
17014 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 char_u *p;
17016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017017 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017019 *expr_start = NULL;
17020 *expr_end = NULL;
17021 }
17022
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017023 /* Quick check for valid starting character. */
17024 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17025 return arg;
17026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017027 for (p = arg; *p != NUL
17028 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017029 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017030 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017031 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017032 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017033 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017034 if (*p == '\'')
17035 {
17036 /* skip over 'string' to avoid counting [ and ] inside it. */
17037 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17038 ;
17039 if (*p == NUL)
17040 break;
17041 }
17042 else if (*p == '"')
17043 {
17044 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17045 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17046 if (*p == '\\' && p[1] != NUL)
17047 ++p;
17048 if (*p == NUL)
17049 break;
17050 }
17051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017052 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017054 if (*p == '[')
17055 ++br_nest;
17056 else if (*p == ']')
17057 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017062 if (*p == '{')
17063 {
17064 mb_nest++;
17065 if (expr_start != NULL && *expr_start == NULL)
17066 *expr_start = p;
17067 }
17068 else if (*p == '}')
17069 {
17070 mb_nest--;
17071 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17072 *expr_end = p;
17073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 }
17076
17077 return p;
17078}
17079
17080/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017081 * Expands out the 'magic' {}'s in a variable/function name.
17082 * Note that this can call itself recursively, to deal with
17083 * constructs like foo{bar}{baz}{bam}
17084 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17085 * "in_start" ^
17086 * "expr_start" ^
17087 * "expr_end" ^
17088 * "in_end" ^
17089 *
17090 * Returns a new allocated string, which the caller must free.
17091 * Returns NULL for failure.
17092 */
17093 static char_u *
17094make_expanded_name(in_start, expr_start, expr_end, in_end)
17095 char_u *in_start;
17096 char_u *expr_start;
17097 char_u *expr_end;
17098 char_u *in_end;
17099{
17100 char_u c1;
17101 char_u *retval = NULL;
17102 char_u *temp_result;
17103 char_u *nextcmd = NULL;
17104
17105 if (expr_end == NULL || in_end == NULL)
17106 return NULL;
17107 *expr_start = NUL;
17108 *expr_end = NUL;
17109 c1 = *in_end;
17110 *in_end = NUL;
17111
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017112 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017113 if (temp_result != NULL && nextcmd == NULL)
17114 {
17115 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17116 + (in_end - expr_end) + 1));
17117 if (retval != NULL)
17118 {
17119 STRCPY(retval, in_start);
17120 STRCAT(retval, temp_result);
17121 STRCAT(retval, expr_end + 1);
17122 }
17123 }
17124 vim_free(temp_result);
17125
17126 *in_end = c1; /* put char back for error messages */
17127 *expr_start = '{';
17128 *expr_end = '}';
17129
17130 if (retval != NULL)
17131 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017132 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017133 if (expr_start != NULL)
17134 {
17135 /* Further expansion! */
17136 temp_result = make_expanded_name(retval, expr_start,
17137 expr_end, temp_result);
17138 vim_free(retval);
17139 retval = temp_result;
17140 }
17141 }
17142
17143 return retval;
17144}
17145
17146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017148 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 */
17150 static int
17151eval_isnamec(c)
17152 int c;
17153{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017154 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17155}
17156
17157/*
17158 * Return TRUE if character "c" can be used as the first character in a
17159 * variable or function name (excluding '{' and '}').
17160 */
17161 static int
17162eval_isnamec1(c)
17163 int c;
17164{
17165 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166}
17167
17168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 * Set number v: variable to "val".
17170 */
17171 void
17172set_vim_var_nr(idx, val)
17173 int idx;
17174 long val;
17175{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017176 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177}
17178
17179/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017180 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 */
17182 long
17183get_vim_var_nr(idx)
17184 int idx;
17185{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017186 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187}
17188
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017189#if defined(FEAT_AUTOCMD) || defined(PROTO)
17190/*
17191 * Get string v: variable value. Uses a static buffer, can only be used once.
17192 */
17193 char_u *
17194get_vim_var_str(idx)
17195 int idx;
17196{
17197 return get_tv_string(&vimvars[idx].vv_tv);
17198}
17199#endif
17200
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201/*
17202 * Set v:count, v:count1 and v:prevcount.
17203 */
17204 void
17205set_vcount(count, count1)
17206 long count;
17207 long count1;
17208{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017209 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17210 vimvars[VV_COUNT].vv_nr = count;
17211 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212}
17213
17214/*
17215 * Set string v: variable to a copy of "val".
17216 */
17217 void
17218set_vim_var_string(idx, val, len)
17219 int idx;
17220 char_u *val;
17221 int len; /* length of "val" to use or -1 (whole string) */
17222{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017223 /* Need to do this (at least) once, since we can't initialize a union.
17224 * Will always be invoked when "v:progname" is set. */
17225 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17226
Bram Moolenaare9a41262005-01-15 22:18:47 +000017227 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017229 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017231 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017233 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234}
17235
17236/*
17237 * Set v:register if needed.
17238 */
17239 void
17240set_reg_var(c)
17241 int c;
17242{
17243 char_u regname;
17244
17245 if (c == 0 || c == ' ')
17246 regname = '"';
17247 else
17248 regname = c;
17249 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017250 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 set_vim_var_string(VV_REG, &regname, 1);
17252}
17253
17254/*
17255 * Get or set v:exception. If "oldval" == NULL, return the current value.
17256 * Otherwise, restore the value to "oldval" and return NULL.
17257 * Must always be called in pairs to save and restore v:exception! Does not
17258 * take care of memory allocations.
17259 */
17260 char_u *
17261v_exception(oldval)
17262 char_u *oldval;
17263{
17264 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017265 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266
Bram Moolenaare9a41262005-01-15 22:18:47 +000017267 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 return NULL;
17269}
17270
17271/*
17272 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17273 * Otherwise, restore the value to "oldval" and return NULL.
17274 * Must always be called in pairs to save and restore v:throwpoint! Does not
17275 * take care of memory allocations.
17276 */
17277 char_u *
17278v_throwpoint(oldval)
17279 char_u *oldval;
17280{
17281 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017282 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283
Bram Moolenaare9a41262005-01-15 22:18:47 +000017284 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 return NULL;
17286}
17287
17288#if defined(FEAT_AUTOCMD) || defined(PROTO)
17289/*
17290 * Set v:cmdarg.
17291 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17292 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17293 * Must always be called in pairs!
17294 */
17295 char_u *
17296set_cmdarg(eap, oldarg)
17297 exarg_T *eap;
17298 char_u *oldarg;
17299{
17300 char_u *oldval;
17301 char_u *newval;
17302 unsigned len;
17303
Bram Moolenaare9a41262005-01-15 22:18:47 +000017304 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017305 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017307 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017308 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017309 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 }
17311
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017312 if (eap->force_bin == FORCE_BIN)
17313 len = 6;
17314 else if (eap->force_bin == FORCE_NOBIN)
17315 len = 8;
17316 else
17317 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017318
17319 if (eap->read_edit)
17320 len += 7;
17321
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017322 if (eap->force_ff != 0)
17323 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17324# ifdef FEAT_MBYTE
17325 if (eap->force_enc != 0)
17326 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017327 if (eap->bad_char != 0)
17328 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017329# endif
17330
17331 newval = alloc(len + 1);
17332 if (newval == NULL)
17333 return NULL;
17334
17335 if (eap->force_bin == FORCE_BIN)
17336 sprintf((char *)newval, " ++bin");
17337 else if (eap->force_bin == FORCE_NOBIN)
17338 sprintf((char *)newval, " ++nobin");
17339 else
17340 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017341
17342 if (eap->read_edit)
17343 STRCAT(newval, " ++edit");
17344
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017345 if (eap->force_ff != 0)
17346 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17347 eap->cmd + eap->force_ff);
17348# ifdef FEAT_MBYTE
17349 if (eap->force_enc != 0)
17350 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17351 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017352 if (eap->bad_char != 0)
17353 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17354 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017355# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017356 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017357 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359#endif
17360
17361/*
17362 * Get the value of internal variable "name".
17363 * Return OK or FAIL.
17364 */
17365 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017366get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 char_u *name;
17368 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017370 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371{
17372 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 typval_T *tv = NULL;
17374 typval_T atv;
17375 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377
17378 /* truncate the name, so that we can use strcmp() */
17379 cc = name[len];
17380 name[len] = NUL;
17381
17382 /*
17383 * Check for "b:changedtick".
17384 */
17385 if (STRCMP(name, "b:changedtick") == 0)
17386 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017387 atv.v_type = VAR_NUMBER;
17388 atv.vval.v_number = curbuf->b_changedtick;
17389 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 }
17391
17392 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 * Check for user-defined variables.
17394 */
17395 else
17396 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017397 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017399 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 }
17401
Bram Moolenaare9a41262005-01-15 22:18:47 +000017402 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017404 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017405 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 ret = FAIL;
17407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017409 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410
17411 name[len] = cc;
17412
17413 return ret;
17414}
17415
17416/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017417 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17418 * Also handle function call with Funcref variable: func(expr)
17419 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17420 */
17421 static int
17422handle_subscript(arg, rettv, evaluate, verbose)
17423 char_u **arg;
17424 typval_T *rettv;
17425 int evaluate; /* do more than finding the end */
17426 int verbose; /* give error messages */
17427{
17428 int ret = OK;
17429 dict_T *selfdict = NULL;
17430 char_u *s;
17431 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017432 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017433
17434 while (ret == OK
17435 && (**arg == '['
17436 || (**arg == '.' && rettv->v_type == VAR_DICT)
17437 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17438 && !vim_iswhite(*(*arg - 1)))
17439 {
17440 if (**arg == '(')
17441 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017442 /* need to copy the funcref so that we can clear rettv */
17443 functv = *rettv;
17444 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017445
17446 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017447 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017448 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017449 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17450 &len, evaluate, selfdict);
17451
17452 /* Clear the funcref afterwards, so that deleting it while
17453 * evaluating the arguments is possible (see test55). */
17454 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017455
17456 /* Stop the expression evaluation when immediately aborting on
17457 * error, or when an interrupt occurred or an exception was thrown
17458 * but not caught. */
17459 if (aborting())
17460 {
17461 if (ret == OK)
17462 clear_tv(rettv);
17463 ret = FAIL;
17464 }
17465 dict_unref(selfdict);
17466 selfdict = NULL;
17467 }
17468 else /* **arg == '[' || **arg == '.' */
17469 {
17470 dict_unref(selfdict);
17471 if (rettv->v_type == VAR_DICT)
17472 {
17473 selfdict = rettv->vval.v_dict;
17474 if (selfdict != NULL)
17475 ++selfdict->dv_refcount;
17476 }
17477 else
17478 selfdict = NULL;
17479 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17480 {
17481 clear_tv(rettv);
17482 ret = FAIL;
17483 }
17484 }
17485 }
17486 dict_unref(selfdict);
17487 return ret;
17488}
17489
17490/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017491 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17492 * value).
17493 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017494 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017496{
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017498}
17499
17500/*
17501 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 * The string "s" must have been allocated, it is consumed.
17503 * Return NULL for out of memory, the variable otherwise.
17504 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017505 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017506alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 char_u *s;
17508{
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017511 rettv = alloc_tv();
17512 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 rettv->v_type = VAR_STRING;
17515 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 }
17517 else
17518 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520}
17521
17522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017523 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017525 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017527 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528{
17529 if (varp != NULL)
17530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017531 switch (varp->v_type)
17532 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017533 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017534 func_unref(varp->vval.v_string);
17535 /*FALLTHROUGH*/
17536 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017537 vim_free(varp->vval.v_string);
17538 break;
17539 case VAR_LIST:
17540 list_unref(varp->vval.v_list);
17541 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017542 case VAR_DICT:
17543 dict_unref(varp->vval.v_dict);
17544 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017545 case VAR_NUMBER:
17546 case VAR_UNKNOWN:
17547 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017548 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017549 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017550 break;
17551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 vim_free(varp);
17553 }
17554}
17555
17556/*
17557 * Free the memory for a variable value and set the value to NULL or 0.
17558 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017559 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562{
17563 if (varp != NULL)
17564 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017565 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017567 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017568 func_unref(varp->vval.v_string);
17569 /*FALLTHROUGH*/
17570 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017571 vim_free(varp->vval.v_string);
17572 varp->vval.v_string = NULL;
17573 break;
17574 case VAR_LIST:
17575 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017576 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017577 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017578 case VAR_DICT:
17579 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017580 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017581 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017583 varp->vval.v_number = 0;
17584 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017585 case VAR_UNKNOWN:
17586 break;
17587 default:
17588 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017590 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 }
17592}
17593
17594/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017595 * Set the value of a variable to NULL without freeing items.
17596 */
17597 static void
17598init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017600{
17601 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017602 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017603}
17604
17605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 * Get the number value of a variable.
17607 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017608 * For incompatible types, return 0.
17609 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17610 * caller of incompatible types: it sets *denote to TRUE if "denote"
17611 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 */
17613 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017615 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017617 int error = FALSE;
17618
17619 return get_tv_number_chk(varp, &error); /* return 0L on error */
17620}
17621
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017622 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017623get_tv_number_chk(varp, denote)
17624 typval_T *varp;
17625 int *denote;
17626{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017627 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017629 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017631 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017632 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017633 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017634 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017635 break;
17636 case VAR_STRING:
17637 if (varp->vval.v_string != NULL)
17638 vim_str2nr(varp->vval.v_string, NULL, NULL,
17639 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017640 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017641 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017642 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017643 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017644 case VAR_DICT:
17645 EMSG(_("E728: Using a Dictionary as a number"));
17646 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017648 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017649 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017651 if (denote == NULL) /* useful for values that must be unsigned */
17652 n = -1;
17653 else
17654 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017655 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656}
17657
17658/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017659 * Get the lnum from the first argument.
17660 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017661 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 */
17663 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666{
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 linenr_T lnum;
17669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017670 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 if (lnum == 0) /* no valid number, try using line() */
17672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017673 rettv.v_type = VAR_NUMBER;
17674 f_line(argvars, &rettv);
17675 lnum = rettv.vval.v_number;
17676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 }
17678 return lnum;
17679}
17680
17681/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017682 * Get the lnum from the first argument.
17683 * Also accepts "$", then "buf" is used.
17684 * Returns 0 on error.
17685 */
17686 static linenr_T
17687get_tv_lnum_buf(argvars, buf)
17688 typval_T *argvars;
17689 buf_T *buf;
17690{
17691 if (argvars[0].v_type == VAR_STRING
17692 && argvars[0].vval.v_string != NULL
17693 && argvars[0].vval.v_string[0] == '$'
17694 && buf != NULL)
17695 return buf->b_ml.ml_line_count;
17696 return get_tv_number_chk(&argvars[0], NULL);
17697}
17698
17699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 * Get the string value of a variable.
17701 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017702 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17703 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 * If the String variable has never been set, return an empty string.
17705 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017706 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17707 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 */
17709 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712{
17713 static char_u mybuf[NUMBUFLEN];
17714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017715 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716}
17717
17718 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017720 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017721 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017723 char_u *res = get_tv_string_buf_chk(varp, buf);
17724
17725 return res != NULL ? res : (char_u *)"";
17726}
17727
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017728 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017729get_tv_string_chk(varp)
17730 typval_T *varp;
17731{
17732 static char_u mybuf[NUMBUFLEN];
17733
17734 return get_tv_string_buf_chk(varp, mybuf);
17735}
17736
17737 static char_u *
17738get_tv_string_buf_chk(varp, buf)
17739 typval_T *varp;
17740 char_u *buf;
17741{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017742 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017744 case VAR_NUMBER:
17745 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17746 return buf;
17747 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017748 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749 break;
17750 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017751 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017752 break;
17753 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017754 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017755 break;
17756 case VAR_STRING:
17757 if (varp->vval.v_string != NULL)
17758 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017759 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017760 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017761 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017762 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017764 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765}
17766
17767/*
17768 * Find variable "name" in the list of variables.
17769 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017770 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017771 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017772 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017774 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017775find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781
Bram Moolenaara7043832005-01-21 11:56:39 +000017782 ht = find_var_ht(name, &varname);
17783 if (htp != NULL)
17784 *htp = ht;
17785 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017787 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788}
17789
17790/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017791 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017792 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017794 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017795find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017796 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017797 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017798 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017799{
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 hashitem_T *hi;
17801
17802 if (*varname == NUL)
17803 {
17804 /* Must be something like "s:", otherwise "ht" would be NULL. */
17805 switch (varname[-2])
17806 {
17807 case 's': return &SCRIPT_SV(current_SID).sv_var;
17808 case 'g': return &globvars_var;
17809 case 'v': return &vimvars_var;
17810 case 'b': return &curbuf->b_bufvar;
17811 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017812#ifdef FEAT_WINDOWS
17813 case 't': return &curtab->tp_winvar;
17814#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017815 case 'l': return current_funccal == NULL
17816 ? NULL : &current_funccal->l_vars_var;
17817 case 'a': return current_funccal == NULL
17818 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017819 }
17820 return NULL;
17821 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017822
17823 hi = hash_find(ht, varname);
17824 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017825 {
17826 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017827 * worked find the variable again. Don't auto-load a script if it was
17828 * loaded already, otherwise it would be loaded every time when
17829 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017830 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017831 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017832 hi = hash_find(ht, varname);
17833 if (HASHITEM_EMPTY(hi))
17834 return NULL;
17835 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017837}
17838
17839/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017840 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017841 * Set "varname" to the start of name without ':'.
17842 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017844find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 char_u *name;
17846 char_u **varname;
17847{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017848 hashitem_T *hi;
17849
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 if (name[1] != ':')
17851 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017852 /* The name must not start with a colon or #. */
17853 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 return NULL;
17855 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017856
17857 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017858 hi = hash_find(&compat_hashtab, name);
17859 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017860 return &compat_hashtab;
17861
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017863 return &globvarht; /* global variable */
17864 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 }
17866 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017867 if (*name == 'g') /* global variable */
17868 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017869 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17870 */
17871 if (vim_strchr(name + 2, ':') != NULL
17872 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017873 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017877 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017878#ifdef FEAT_WINDOWS
17879 if (*name == 't') /* tab page variable */
17880 return &curtab->tp_vars.dv_hashtab;
17881#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017882 if (*name == 'v') /* v: variable */
17883 return &vimvarht;
17884 if (*name == 'a' && current_funccal != NULL) /* function argument */
17885 return &current_funccal->l_avars.dv_hashtab;
17886 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17887 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 if (*name == 's' /* script variable */
17889 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17890 return &SCRIPT_VARS(current_SID);
17891 return NULL;
17892}
17893
17894/*
17895 * Get the string value of a (global/local) variable.
17896 * Returns NULL when it doesn't exist.
17897 */
17898 char_u *
17899get_var_value(name)
17900 char_u *name;
17901{
Bram Moolenaar33570922005-01-25 22:26:29 +000017902 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903
Bram Moolenaara7043832005-01-21 11:56:39 +000017904 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 if (v == NULL)
17906 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017907 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908}
17909
17910/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 * sourcing this script and when executing functions defined in the script.
17913 */
17914 void
17915new_script_vars(id)
17916 scid_T id;
17917{
Bram Moolenaara7043832005-01-21 11:56:39 +000017918 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 hashtab_T *ht;
17920 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017921
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17923 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017924 /* Re-allocating ga_data means that an ht_array pointing to
17925 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017927 for (i = 1; i <= ga_scripts.ga_len; ++i)
17928 {
17929 ht = &SCRIPT_VARS(i);
17930 if (ht->ht_mask == HT_INIT_SIZE - 1)
17931 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 sv = &SCRIPT_SV(i);
17933 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017934 }
17935
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 while (ga_scripts.ga_len < id)
17937 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017938 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17939 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941 }
17942 }
17943}
17944
17945/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017946 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17947 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 */
17949 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017950init_var_dict(dict, dict_var)
17951 dict_T *dict;
17952 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953{
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 hash_init(&dict->dv_hashtab);
17955 dict->dv_refcount = 99999;
17956 dict_var->di_tv.vval.v_dict = dict;
17957 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017958 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017959 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17960 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961}
17962
17963/*
17964 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017965 * Frees all allocated variables and the value they contain.
17966 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 */
17968 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017969vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017970 hashtab_T *ht;
17971{
17972 vars_clear_ext(ht, TRUE);
17973}
17974
17975/*
17976 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17977 */
17978 static void
17979vars_clear_ext(ht, free_val)
17980 hashtab_T *ht;
17981 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982{
Bram Moolenaara7043832005-01-21 11:56:39 +000017983 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017984 hashitem_T *hi;
17985 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017988 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017989 for (hi = ht->ht_array; todo > 0; ++hi)
17990 {
17991 if (!HASHITEM_EMPTY(hi))
17992 {
17993 --todo;
17994
Bram Moolenaar33570922005-01-25 22:26:29 +000017995 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017996 * ht_array might change then. hash_clear() takes care of it
17997 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 v = HI2DI(hi);
17999 if (free_val)
18000 clear_tv(&v->di_tv);
18001 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18002 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018003 }
18004 }
18005 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018006 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007}
18008
Bram Moolenaara7043832005-01-21 11:56:39 +000018009/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018010 * Delete a variable from hashtab "ht" at item "hi".
18011 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018014delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018015 hashtab_T *ht;
18016 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017{
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018019
18020 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 clear_tv(&di->di_tv);
18022 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023}
18024
18025/*
18026 * List the value of one internal variable.
18027 */
18028 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018029list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018032 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018034 char_u *tofree;
18035 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018036 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018037
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018038 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018039 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018040 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018041 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042}
18043
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018045list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 char_u *prefix;
18047 char_u *name;
18048 int type;
18049 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018050 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051{
Bram Moolenaar31859182007-08-14 20:41:13 +000018052 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18053 msg_start();
18054 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 if (name != NULL) /* "a:" vars don't have a name stored */
18056 msg_puts(name);
18057 msg_putchar(' ');
18058 msg_advance(22);
18059 if (type == VAR_NUMBER)
18060 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018061 else if (type == VAR_FUNC)
18062 msg_putchar('*');
18063 else if (type == VAR_LIST)
18064 {
18065 msg_putchar('[');
18066 if (*string == '[')
18067 ++string;
18068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018069 else if (type == VAR_DICT)
18070 {
18071 msg_putchar('{');
18072 if (*string == '{')
18073 ++string;
18074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 else
18076 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018077
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018079
18080 if (type == VAR_FUNC)
18081 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018082 if (*first)
18083 {
18084 msg_clr_eos();
18085 *first = FALSE;
18086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087}
18088
18089/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018090 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 * If the variable already exists, the value is updated.
18092 * Otherwise the variable is created.
18093 */
18094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018095set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018098 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099{
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018103 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018105 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018106 {
18107 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18108 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18109 ? name[2] : name[0]))
18110 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018111 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018112 return;
18113 }
18114 if (function_exists(name))
18115 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018116 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018117 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018118 return;
18119 }
18120 }
18121
Bram Moolenaara7043832005-01-21 11:56:39 +000018122 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018124 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018125 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018126 return;
18127 }
18128
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018129 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018133 if (var_check_ro(v->di_flags, name)
18134 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018135 return;
18136 if (v->di_tv.v_type != tv->v_type
18137 && !((v->di_tv.v_type == VAR_STRING
18138 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018139 && (tv->v_type == VAR_STRING
18140 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018141 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018142 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018143 return;
18144 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018145
18146 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018147 * Handle setting internal v: variables separately: we don't change
18148 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018149 */
18150 if (ht == &vimvarht)
18151 {
18152 if (v->di_tv.v_type == VAR_STRING)
18153 {
18154 vim_free(v->di_tv.vval.v_string);
18155 if (copy || tv->v_type != VAR_STRING)
18156 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18157 else
18158 {
18159 /* Take over the string to avoid an extra alloc/free. */
18160 v->di_tv.vval.v_string = tv->vval.v_string;
18161 tv->vval.v_string = NULL;
18162 }
18163 }
18164 else if (v->di_tv.v_type != VAR_NUMBER)
18165 EMSG2(_(e_intern2), "set_var()");
18166 else
18167 v->di_tv.vval.v_number = get_tv_number(tv);
18168 return;
18169 }
18170
18171 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 }
18173 else /* add a new variable */
18174 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018175 /* Can't add "v:" variable. */
18176 if (ht == &vimvarht)
18177 {
18178 EMSG2(_(e_illvar), name);
18179 return;
18180 }
18181
Bram Moolenaar92124a32005-06-17 22:03:40 +000018182 /* Make sure the variable name is valid. */
18183 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018184 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18185 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018186 {
18187 EMSG2(_(e_illvar), varname);
18188 return;
18189 }
18190
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018191 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18192 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018193 if (v == NULL)
18194 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018195 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018198 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 return;
18200 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018201 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018204 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018205 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018206 else
18207 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018208 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018209 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018210 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212}
18213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018214/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018215 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 * Also give an error message.
18217 */
18218 static int
18219var_check_ro(flags, name)
18220 int flags;
18221 char_u *name;
18222{
18223 if (flags & DI_FLAGS_RO)
18224 {
18225 EMSG2(_(e_readonlyvar), name);
18226 return TRUE;
18227 }
18228 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18229 {
18230 EMSG2(_(e_readonlysbx), name);
18231 return TRUE;
18232 }
18233 return FALSE;
18234}
18235
18236/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018237 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18238 * Also give an error message.
18239 */
18240 static int
18241var_check_fixed(flags, name)
18242 int flags;
18243 char_u *name;
18244{
18245 if (flags & DI_FLAGS_FIX)
18246 {
18247 EMSG2(_("E795: Cannot delete variable %s"), name);
18248 return TRUE;
18249 }
18250 return FALSE;
18251}
18252
18253/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018254 * Return TRUE if typeval "tv" is set to be locked (immutable).
18255 * Also give an error message, using "name".
18256 */
18257 static int
18258tv_check_lock(lock, name)
18259 int lock;
18260 char_u *name;
18261{
18262 if (lock & VAR_LOCKED)
18263 {
18264 EMSG2(_("E741: Value is locked: %s"),
18265 name == NULL ? (char_u *)_("Unknown") : name);
18266 return TRUE;
18267 }
18268 if (lock & VAR_FIXED)
18269 {
18270 EMSG2(_("E742: Cannot change value of %s"),
18271 name == NULL ? (char_u *)_("Unknown") : name);
18272 return TRUE;
18273 }
18274 return FALSE;
18275}
18276
18277/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018278 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018279 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018280 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018284 typval_T *from;
18285 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018287 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018288 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018289 switch (from->v_type)
18290 {
18291 case VAR_NUMBER:
18292 to->vval.v_number = from->vval.v_number;
18293 break;
18294 case VAR_STRING:
18295 case VAR_FUNC:
18296 if (from->vval.v_string == NULL)
18297 to->vval.v_string = NULL;
18298 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018300 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018301 if (from->v_type == VAR_FUNC)
18302 func_ref(to->vval.v_string);
18303 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018304 break;
18305 case VAR_LIST:
18306 if (from->vval.v_list == NULL)
18307 to->vval.v_list = NULL;
18308 else
18309 {
18310 to->vval.v_list = from->vval.v_list;
18311 ++to->vval.v_list->lv_refcount;
18312 }
18313 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018314 case VAR_DICT:
18315 if (from->vval.v_dict == NULL)
18316 to->vval.v_dict = NULL;
18317 else
18318 {
18319 to->vval.v_dict = from->vval.v_dict;
18320 ++to->vval.v_dict->dv_refcount;
18321 }
18322 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018323 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018324 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018325 break;
18326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327}
18328
18329/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018330 * Make a copy of an item.
18331 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018332 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18333 * reference to an already copied list/dict can be used.
18334 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018335 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018336 static int
18337item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018338 typval_T *from;
18339 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018340 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018341 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018342{
18343 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018344 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018345
Bram Moolenaar33570922005-01-25 22:26:29 +000018346 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018347 {
18348 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018349 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018350 }
18351 ++recurse;
18352
18353 switch (from->v_type)
18354 {
18355 case VAR_NUMBER:
18356 case VAR_STRING:
18357 case VAR_FUNC:
18358 copy_tv(from, to);
18359 break;
18360 case VAR_LIST:
18361 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018362 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018363 if (from->vval.v_list == NULL)
18364 to->vval.v_list = NULL;
18365 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18366 {
18367 /* use the copy made earlier */
18368 to->vval.v_list = from->vval.v_list->lv_copylist;
18369 ++to->vval.v_list->lv_refcount;
18370 }
18371 else
18372 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18373 if (to->vval.v_list == NULL)
18374 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018375 break;
18376 case VAR_DICT:
18377 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018378 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018379 if (from->vval.v_dict == NULL)
18380 to->vval.v_dict = NULL;
18381 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18382 {
18383 /* use the copy made earlier */
18384 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18385 ++to->vval.v_dict->dv_refcount;
18386 }
18387 else
18388 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18389 if (to->vval.v_dict == NULL)
18390 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018391 break;
18392 default:
18393 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018394 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018395 }
18396 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018397 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018398}
18399
18400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 * ":echo expr1 ..." print each argument separated with a space, add a
18402 * newline at the end.
18403 * ":echon expr1 ..." print each argument plain.
18404 */
18405 void
18406ex_echo(eap)
18407 exarg_T *eap;
18408{
18409 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018410 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018411 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 char_u *p;
18413 int needclr = TRUE;
18414 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416
18417 if (eap->skip)
18418 ++emsg_skip;
18419 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18420 {
18421 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018422 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 {
18424 /*
18425 * Report the invalid expression unless the expression evaluation
18426 * has been cancelled due to an aborting error, an interrupt, or an
18427 * exception.
18428 */
18429 if (!aborting())
18430 EMSG2(_(e_invexpr2), p);
18431 break;
18432 }
18433 if (!eap->skip)
18434 {
18435 if (atstart)
18436 {
18437 atstart = FALSE;
18438 /* Call msg_start() after eval1(), evaluating the expression
18439 * may cause a message to appear. */
18440 if (eap->cmdidx == CMD_echo)
18441 msg_start();
18442 }
18443 else if (eap->cmdidx == CMD_echo)
18444 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018445 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018446 if (p != NULL)
18447 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018449 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018451 if (*p != TAB && needclr)
18452 {
18453 /* remove any text still there from the command */
18454 msg_clr_eos();
18455 needclr = FALSE;
18456 }
18457 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 }
18459 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018460 {
18461#ifdef FEAT_MBYTE
18462 if (has_mbyte)
18463 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018464 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018465
18466 (void)msg_outtrans_len_attr(p, i, echo_attr);
18467 p += i - 1;
18468 }
18469 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018471 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018474 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 arg = skipwhite(arg);
18478 }
18479 eap->nextcmd = check_nextcmd(arg);
18480
18481 if (eap->skip)
18482 --emsg_skip;
18483 else
18484 {
18485 /* remove text that may still be there from the command */
18486 if (needclr)
18487 msg_clr_eos();
18488 if (eap->cmdidx == CMD_echo)
18489 msg_end();
18490 }
18491}
18492
18493/*
18494 * ":echohl {name}".
18495 */
18496 void
18497ex_echohl(eap)
18498 exarg_T *eap;
18499{
18500 int id;
18501
18502 id = syn_name2id(eap->arg);
18503 if (id == 0)
18504 echo_attr = 0;
18505 else
18506 echo_attr = syn_id2attr(id);
18507}
18508
18509/*
18510 * ":execute expr1 ..." execute the result of an expression.
18511 * ":echomsg expr1 ..." Print a message
18512 * ":echoerr expr1 ..." Print an error
18513 * Each gets spaces around each argument and a newline at the end for
18514 * echo commands
18515 */
18516 void
18517ex_execute(eap)
18518 exarg_T *eap;
18519{
18520 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018521 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 int ret = OK;
18523 char_u *p;
18524 garray_T ga;
18525 int len;
18526 int save_did_emsg;
18527
18528 ga_init2(&ga, 1, 80);
18529
18530 if (eap->skip)
18531 ++emsg_skip;
18532 while (*arg != NUL && *arg != '|' && *arg != '\n')
18533 {
18534 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 {
18537 /*
18538 * Report the invalid expression unless the expression evaluation
18539 * has been cancelled due to an aborting error, an interrupt, or an
18540 * exception.
18541 */
18542 if (!aborting())
18543 EMSG2(_(e_invexpr2), p);
18544 ret = FAIL;
18545 break;
18546 }
18547
18548 if (!eap->skip)
18549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 len = (int)STRLEN(p);
18552 if (ga_grow(&ga, len + 2) == FAIL)
18553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 ret = FAIL;
18556 break;
18557 }
18558 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 ga.ga_len += len;
18562 }
18563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018564 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 arg = skipwhite(arg);
18566 }
18567
18568 if (ret != FAIL && ga.ga_data != NULL)
18569 {
18570 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018573 out_flush();
18574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 else if (eap->cmdidx == CMD_echoerr)
18576 {
18577 /* We don't want to abort following commands, restore did_emsg. */
18578 save_did_emsg = did_emsg;
18579 EMSG((char_u *)ga.ga_data);
18580 if (!force_abort)
18581 did_emsg = save_did_emsg;
18582 }
18583 else if (eap->cmdidx == CMD_execute)
18584 do_cmdline((char_u *)ga.ga_data,
18585 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18586 }
18587
18588 ga_clear(&ga);
18589
18590 if (eap->skip)
18591 --emsg_skip;
18592
18593 eap->nextcmd = check_nextcmd(arg);
18594}
18595
18596/*
18597 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18598 * "arg" points to the "&" or '+' when called, to "option" when returning.
18599 * Returns NULL when no option name found. Otherwise pointer to the char
18600 * after the option name.
18601 */
18602 static char_u *
18603find_option_end(arg, opt_flags)
18604 char_u **arg;
18605 int *opt_flags;
18606{
18607 char_u *p = *arg;
18608
18609 ++p;
18610 if (*p == 'g' && p[1] == ':')
18611 {
18612 *opt_flags = OPT_GLOBAL;
18613 p += 2;
18614 }
18615 else if (*p == 'l' && p[1] == ':')
18616 {
18617 *opt_flags = OPT_LOCAL;
18618 p += 2;
18619 }
18620 else
18621 *opt_flags = 0;
18622
18623 if (!ASCII_ISALPHA(*p))
18624 return NULL;
18625 *arg = p;
18626
18627 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18628 p += 4; /* termcap option */
18629 else
18630 while (ASCII_ISALPHA(*p))
18631 ++p;
18632 return p;
18633}
18634
18635/*
18636 * ":function"
18637 */
18638 void
18639ex_function(eap)
18640 exarg_T *eap;
18641{
18642 char_u *theline;
18643 int j;
18644 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 char_u *name = NULL;
18647 char_u *p;
18648 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018649 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 garray_T newargs;
18651 garray_T newlines;
18652 int varargs = FALSE;
18653 int mustend = FALSE;
18654 int flags = 0;
18655 ufunc_T *fp;
18656 int indent;
18657 int nesting;
18658 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018659 dictitem_T *v;
18660 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018661 static int func_nr = 0; /* number for nameless function */
18662 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018663 hashtab_T *ht;
18664 int todo;
18665 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018666 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667
18668 /*
18669 * ":function" without argument: list functions.
18670 */
18671 if (ends_excmd(*eap->arg))
18672 {
18673 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018674 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018675 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018676 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018677 {
18678 if (!HASHITEM_EMPTY(hi))
18679 {
18680 --todo;
18681 fp = HI2UF(hi);
18682 if (!isdigit(*fp->uf_name))
18683 list_func_head(fp, FALSE);
18684 }
18685 }
18686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 eap->nextcmd = check_nextcmd(eap->arg);
18688 return;
18689 }
18690
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018691 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018692 * ":function /pat": list functions matching pattern.
18693 */
18694 if (*eap->arg == '/')
18695 {
18696 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18697 if (!eap->skip)
18698 {
18699 regmatch_T regmatch;
18700
18701 c = *p;
18702 *p = NUL;
18703 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18704 *p = c;
18705 if (regmatch.regprog != NULL)
18706 {
18707 regmatch.rm_ic = p_ic;
18708
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018709 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018710 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18711 {
18712 if (!HASHITEM_EMPTY(hi))
18713 {
18714 --todo;
18715 fp = HI2UF(hi);
18716 if (!isdigit(*fp->uf_name)
18717 && vim_regexec(&regmatch, fp->uf_name, 0))
18718 list_func_head(fp, FALSE);
18719 }
18720 }
18721 }
18722 }
18723 if (*p == '/')
18724 ++p;
18725 eap->nextcmd = check_nextcmd(p);
18726 return;
18727 }
18728
18729 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018730 * Get the function name. There are these situations:
18731 * func normal function name
18732 * "name" == func, "fudi.fd_dict" == NULL
18733 * dict.func new dictionary entry
18734 * "name" == NULL, "fudi.fd_dict" set,
18735 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18736 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018737 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018738 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18739 * dict.func existing dict entry that's not a Funcref
18740 * "name" == NULL, "fudi.fd_dict" set,
18741 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018744 name = trans_function_name(&p, eap->skip, 0, &fudi);
18745 paren = (vim_strchr(p, '(') != NULL);
18746 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 {
18748 /*
18749 * Return on an invalid expression in braces, unless the expression
18750 * evaluation has been cancelled due to an aborting error, an
18751 * interrupt, or an exception.
18752 */
18753 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018754 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018755 if (!eap->skip && fudi.fd_newkey != NULL)
18756 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018757 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 else
18761 eap->skip = TRUE;
18762 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018763
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 /* An error in a function call during evaluation of an expression in magic
18765 * braces should not cause the function not to be defined. */
18766 saved_did_emsg = did_emsg;
18767 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768
18769 /*
18770 * ":function func" with only function name: list function.
18771 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018772 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 {
18774 if (!ends_excmd(*skipwhite(p)))
18775 {
18776 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018777 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 }
18779 eap->nextcmd = check_nextcmd(p);
18780 if (eap->nextcmd != NULL)
18781 *p = NUL;
18782 if (!eap->skip && !got_int)
18783 {
18784 fp = find_func(name);
18785 if (fp != NULL)
18786 {
18787 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018788 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018790 if (FUNCLINE(fp, j) == NULL)
18791 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 msg_putchar('\n');
18793 msg_outnum((long)(j + 1));
18794 if (j < 9)
18795 msg_putchar(' ');
18796 if (j < 99)
18797 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018798 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 out_flush(); /* show a line at a time */
18800 ui_breakcheck();
18801 }
18802 if (!got_int)
18803 {
18804 msg_putchar('\n');
18805 msg_puts((char_u *)" endfunction");
18806 }
18807 }
18808 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018809 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018811 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 }
18813
18814 /*
18815 * ":function name(arg1, arg2)" Define function.
18816 */
18817 p = skipwhite(p);
18818 if (*p != '(')
18819 {
18820 if (!eap->skip)
18821 {
18822 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018823 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 }
18825 /* attempt to continue by skipping some text */
18826 if (vim_strchr(p, '(') != NULL)
18827 p = vim_strchr(p, '(');
18828 }
18829 p = skipwhite(p + 1);
18830
18831 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18832 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18833
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018834 if (!eap->skip)
18835 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018836 /* Check the name of the function. Unless it's a dictionary function
18837 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018838 if (name != NULL)
18839 arg = name;
18840 else
18841 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018842 if (arg != NULL && (fudi.fd_di == NULL
18843 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018844 {
18845 if (*arg == K_SPECIAL)
18846 j = 3;
18847 else
18848 j = 0;
18849 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18850 : eval_isnamec(arg[j])))
18851 ++j;
18852 if (arg[j] != NUL)
18853 emsg_funcname(_(e_invarg2), arg);
18854 }
18855 }
18856
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 /*
18858 * Isolate the arguments: "arg1, arg2, ...)"
18859 */
18860 while (*p != ')')
18861 {
18862 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18863 {
18864 varargs = TRUE;
18865 p += 3;
18866 mustend = TRUE;
18867 }
18868 else
18869 {
18870 arg = p;
18871 while (ASCII_ISALNUM(*p) || *p == '_')
18872 ++p;
18873 if (arg == p || isdigit(*arg)
18874 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18875 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18876 {
18877 if (!eap->skip)
18878 EMSG2(_("E125: Illegal argument: %s"), arg);
18879 break;
18880 }
18881 if (ga_grow(&newargs, 1) == FAIL)
18882 goto erret;
18883 c = *p;
18884 *p = NUL;
18885 arg = vim_strsave(arg);
18886 if (arg == NULL)
18887 goto erret;
18888 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18889 *p = c;
18890 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 if (*p == ',')
18892 ++p;
18893 else
18894 mustend = TRUE;
18895 }
18896 p = skipwhite(p);
18897 if (mustend && *p != ')')
18898 {
18899 if (!eap->skip)
18900 EMSG2(_(e_invarg2), eap->arg);
18901 break;
18902 }
18903 }
18904 ++p; /* skip the ')' */
18905
Bram Moolenaare9a41262005-01-15 22:18:47 +000018906 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 for (;;)
18908 {
18909 p = skipwhite(p);
18910 if (STRNCMP(p, "range", 5) == 0)
18911 {
18912 flags |= FC_RANGE;
18913 p += 5;
18914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018915 else if (STRNCMP(p, "dict", 4) == 0)
18916 {
18917 flags |= FC_DICT;
18918 p += 4;
18919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 else if (STRNCMP(p, "abort", 5) == 0)
18921 {
18922 flags |= FC_ABORT;
18923 p += 5;
18924 }
18925 else
18926 break;
18927 }
18928
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018929 /* When there is a line break use what follows for the function body.
18930 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18931 if (*p == '\n')
18932 line_arg = p + 1;
18933 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 EMSG(_(e_trailing));
18935
18936 /*
18937 * Read the body of the function, until ":endfunction" is found.
18938 */
18939 if (KeyTyped)
18940 {
18941 /* Check if the function already exists, don't let the user type the
18942 * whole function before telling him it doesn't work! For a script we
18943 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018944 if (!eap->skip && !eap->forceit)
18945 {
18946 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18947 EMSG(_(e_funcdict));
18948 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018949 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018952 if (!eap->skip && did_emsg)
18953 goto erret;
18954
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 msg_putchar('\n'); /* don't overwrite the function name */
18956 cmdline_row = msg_row;
18957 }
18958
18959 indent = 2;
18960 nesting = 0;
18961 for (;;)
18962 {
18963 msg_scroll = TRUE;
18964 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018965 sourcing_lnum_off = sourcing_lnum;
18966
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018967 if (line_arg != NULL)
18968 {
18969 /* Use eap->arg, split up in parts by line breaks. */
18970 theline = line_arg;
18971 p = vim_strchr(theline, '\n');
18972 if (p == NULL)
18973 line_arg += STRLEN(line_arg);
18974 else
18975 {
18976 *p = NUL;
18977 line_arg = p + 1;
18978 }
18979 }
18980 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 theline = getcmdline(':', 0L, indent);
18982 else
18983 theline = eap->getline(':', eap->cookie, indent);
18984 if (KeyTyped)
18985 lines_left = Rows - 1;
18986 if (theline == NULL)
18987 {
18988 EMSG(_("E126: Missing :endfunction"));
18989 goto erret;
18990 }
18991
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018992 /* Detect line continuation: sourcing_lnum increased more than one. */
18993 if (sourcing_lnum > sourcing_lnum_off + 1)
18994 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18995 else
18996 sourcing_lnum_off = 0;
18997
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 if (skip_until != NULL)
18999 {
19000 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19001 * don't check for ":endfunc". */
19002 if (STRCMP(theline, skip_until) == 0)
19003 {
19004 vim_free(skip_until);
19005 skip_until = NULL;
19006 }
19007 }
19008 else
19009 {
19010 /* skip ':' and blanks*/
19011 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19012 ;
19013
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019014 /* Check for "endfunction". */
19015 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019017 if (line_arg == NULL)
19018 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 break;
19020 }
19021
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019022 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 * at "end". */
19024 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19025 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019026 else if (STRNCMP(p, "if", 2) == 0
19027 || STRNCMP(p, "wh", 2) == 0
19028 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 || STRNCMP(p, "try", 3) == 0)
19030 indent += 2;
19031
19032 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019033 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019035 if (*p == '!')
19036 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 p += eval_fname_script(p);
19038 if (ASCII_ISALPHA(*p))
19039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019040 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 if (*skipwhite(p) == '(')
19042 {
19043 ++nesting;
19044 indent += 2;
19045 }
19046 }
19047 }
19048
19049 /* Check for ":append" or ":insert". */
19050 p = skip_range(p, NULL);
19051 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19052 || (p[0] == 'i'
19053 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19054 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19055 skip_until = vim_strsave((char_u *)".");
19056
19057 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19058 arg = skipwhite(skiptowhite(p));
19059 if (arg[0] == '<' && arg[1] =='<'
19060 && ((p[0] == 'p' && p[1] == 'y'
19061 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19062 || (p[0] == 'p' && p[1] == 'e'
19063 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19064 || (p[0] == 't' && p[1] == 'c'
19065 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19066 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19067 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019068 || (p[0] == 'm' && p[1] == 'z'
19069 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 ))
19071 {
19072 /* ":python <<" continues until a dot, like ":append" */
19073 p = skipwhite(arg + 2);
19074 if (*p == NUL)
19075 skip_until = vim_strsave((char_u *)".");
19076 else
19077 skip_until = vim_strsave(p);
19078 }
19079 }
19080
19081 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019082 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019083 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019084 if (line_arg == NULL)
19085 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019087 }
19088
19089 /* Copy the line to newly allocated memory. get_one_sourceline()
19090 * allocates 250 bytes per line, this saves 80% on average. The cost
19091 * is an extra alloc/free. */
19092 p = vim_strsave(theline);
19093 if (p != NULL)
19094 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019095 if (line_arg == NULL)
19096 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019097 theline = p;
19098 }
19099
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019100 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19101
19102 /* Add NULL lines for continuation lines, so that the line count is
19103 * equal to the index in the growarray. */
19104 while (sourcing_lnum_off-- > 0)
19105 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019106
19107 /* Check for end of eap->arg. */
19108 if (line_arg != NULL && *line_arg == NUL)
19109 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 }
19111
19112 /* Don't define the function when skipping commands or when an error was
19113 * detected. */
19114 if (eap->skip || did_emsg)
19115 goto erret;
19116
19117 /*
19118 * If there are no errors, add the function
19119 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019120 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019121 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019122 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019123 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019124 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019125 emsg_funcname("E707: Function name conflicts with variable: %s",
19126 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019127 goto erret;
19128 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019129
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019130 fp = find_func(name);
19131 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019133 if (!eap->forceit)
19134 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019135 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019136 goto erret;
19137 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019138 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019139 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019140 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019141 name);
19142 goto erret;
19143 }
19144 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019145 ga_clear_strings(&(fp->uf_args));
19146 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019147 vim_free(name);
19148 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 }
19151 else
19152 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019153 char numbuf[20];
19154
19155 fp = NULL;
19156 if (fudi.fd_newkey == NULL && !eap->forceit)
19157 {
19158 EMSG(_(e_funcdict));
19159 goto erret;
19160 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019161 if (fudi.fd_di == NULL)
19162 {
19163 /* Can't add a function to a locked dictionary */
19164 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19165 goto erret;
19166 }
19167 /* Can't change an existing function if it is locked */
19168 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19169 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019170
19171 /* Give the function a sequential number. Can only be used with a
19172 * Funcref! */
19173 vim_free(name);
19174 sprintf(numbuf, "%d", ++func_nr);
19175 name = vim_strsave((char_u *)numbuf);
19176 if (name == NULL)
19177 goto erret;
19178 }
19179
19180 if (fp == NULL)
19181 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019182 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019183 {
19184 int slen, plen;
19185 char_u *scriptname;
19186
19187 /* Check that the autoload name matches the script name. */
19188 j = FAIL;
19189 if (sourcing_name != NULL)
19190 {
19191 scriptname = autoload_name(name);
19192 if (scriptname != NULL)
19193 {
19194 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019195 plen = (int)STRLEN(p);
19196 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019197 if (slen > plen && fnamecmp(p,
19198 sourcing_name + slen - plen) == 0)
19199 j = OK;
19200 vim_free(scriptname);
19201 }
19202 }
19203 if (j == FAIL)
19204 {
19205 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19206 goto erret;
19207 }
19208 }
19209
19210 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 if (fp == NULL)
19212 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019213
19214 if (fudi.fd_dict != NULL)
19215 {
19216 if (fudi.fd_di == NULL)
19217 {
19218 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019219 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019220 if (fudi.fd_di == NULL)
19221 {
19222 vim_free(fp);
19223 goto erret;
19224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019225 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19226 {
19227 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019228 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019229 goto erret;
19230 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019231 }
19232 else
19233 /* overwrite existing dict entry */
19234 clear_tv(&fudi.fd_di->di_tv);
19235 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019236 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019237 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019238 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019239
19240 /* behave like "dict" was used */
19241 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019242 }
19243
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019245 STRCPY(fp->uf_name, name);
19246 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019248 fp->uf_args = newargs;
19249 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019250#ifdef FEAT_PROFILE
19251 fp->uf_tml_count = NULL;
19252 fp->uf_tml_total = NULL;
19253 fp->uf_tml_self = NULL;
19254 fp->uf_profiling = FALSE;
19255 if (prof_def_func())
19256 func_do_profile(fp);
19257#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019258 fp->uf_varargs = varargs;
19259 fp->uf_flags = flags;
19260 fp->uf_calls = 0;
19261 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019262 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263
19264erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 ga_clear_strings(&newargs);
19266 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267ret_free:
19268 vim_free(skip_until);
19269 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272}
19273
19274/*
19275 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019276 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019278 * flags:
19279 * TFN_INT: internal function name OK
19280 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 * Advances "pp" to just after the function name (if no error).
19282 */
19283 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019284trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 char_u **pp;
19286 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019287 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019288 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019290 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 char_u *start;
19292 char_u *end;
19293 int lead;
19294 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019296 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019297
19298 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019301
19302 /* Check for hard coded <SNR>: already translated function ID (from a user
19303 * command). */
19304 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19305 && (*pp)[2] == (int)KE_SNR)
19306 {
19307 *pp += 3;
19308 len = get_id_len(pp) + 3;
19309 return vim_strnsave(start, len);
19310 }
19311
19312 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19313 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019315 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019317
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019318 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19319 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019320 if (end == start)
19321 {
19322 if (!skip)
19323 EMSG(_("E129: Function name required"));
19324 goto theend;
19325 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019326 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019327 {
19328 /*
19329 * Report an invalid expression in braces, unless the expression
19330 * evaluation has been cancelled due to an aborting error, an
19331 * interrupt, or an exception.
19332 */
19333 if (!aborting())
19334 {
19335 if (end != NULL)
19336 EMSG2(_(e_invarg2), start);
19337 }
19338 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019339 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019340 goto theend;
19341 }
19342
19343 if (lv.ll_tv != NULL)
19344 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019345 if (fdp != NULL)
19346 {
19347 fdp->fd_dict = lv.ll_dict;
19348 fdp->fd_newkey = lv.ll_newkey;
19349 lv.ll_newkey = NULL;
19350 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019351 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019352 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19353 {
19354 name = vim_strsave(lv.ll_tv->vval.v_string);
19355 *pp = end;
19356 }
19357 else
19358 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019359 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19360 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019361 EMSG(_(e_funcref));
19362 else
19363 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019364 name = NULL;
19365 }
19366 goto theend;
19367 }
19368
19369 if (lv.ll_name == NULL)
19370 {
19371 /* Error found, but continue after the function name. */
19372 *pp = end;
19373 goto theend;
19374 }
19375
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019376 /* Check if the name is a Funcref. If so, use the value. */
19377 if (lv.ll_exp_name != NULL)
19378 {
19379 len = (int)STRLEN(lv.ll_exp_name);
19380 name = deref_func_name(lv.ll_exp_name, &len);
19381 if (name == lv.ll_exp_name)
19382 name = NULL;
19383 }
19384 else
19385 {
19386 len = (int)(end - *pp);
19387 name = deref_func_name(*pp, &len);
19388 if (name == *pp)
19389 name = NULL;
19390 }
19391 if (name != NULL)
19392 {
19393 name = vim_strsave(name);
19394 *pp = end;
19395 goto theend;
19396 }
19397
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019398 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019399 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019400 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019401 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19402 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19403 {
19404 /* When there was "s:" already or the name expanded to get a
19405 * leading "s:" then remove it. */
19406 lv.ll_name += 2;
19407 len -= 2;
19408 lead = 2;
19409 }
19410 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019411 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019412 {
19413 if (lead == 2) /* skip over "s:" */
19414 lv.ll_name += 2;
19415 len = (int)(end - lv.ll_name);
19416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019417
19418 /*
19419 * Copy the function name to allocated memory.
19420 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19421 * Accept <SNR>123_name() outside a script.
19422 */
19423 if (skip)
19424 lead = 0; /* do nothing */
19425 else if (lead > 0)
19426 {
19427 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019428 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19429 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019430 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019431 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019432 if (current_SID <= 0)
19433 {
19434 EMSG(_(e_usingsid));
19435 goto theend;
19436 }
19437 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19438 lead += (int)STRLEN(sid_buf);
19439 }
19440 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019441 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019442 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019443 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019444 goto theend;
19445 }
19446 name = alloc((unsigned)(len + lead + 1));
19447 if (name != NULL)
19448 {
19449 if (lead > 0)
19450 {
19451 name[0] = K_SPECIAL;
19452 name[1] = KS_EXTRA;
19453 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019454 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019455 STRCPY(name + 3, sid_buf);
19456 }
19457 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19458 name[len + lead] = NUL;
19459 }
19460 *pp = end;
19461
19462theend:
19463 clear_lval(&lv);
19464 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465}
19466
19467/*
19468 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19469 * Return 2 if "p" starts with "s:".
19470 * Return 0 otherwise.
19471 */
19472 static int
19473eval_fname_script(p)
19474 char_u *p;
19475{
19476 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19477 || STRNICMP(p + 1, "SNR>", 4) == 0))
19478 return 5;
19479 if (p[0] == 's' && p[1] == ':')
19480 return 2;
19481 return 0;
19482}
19483
19484/*
19485 * Return TRUE if "p" starts with "<SID>" or "s:".
19486 * Only works if eval_fname_script() returned non-zero for "p"!
19487 */
19488 static int
19489eval_fname_sid(p)
19490 char_u *p;
19491{
19492 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19493}
19494
19495/*
19496 * List the head of the function: "name(arg1, arg2)".
19497 */
19498 static void
19499list_func_head(fp, indent)
19500 ufunc_T *fp;
19501 int indent;
19502{
19503 int j;
19504
19505 msg_start();
19506 if (indent)
19507 MSG_PUTS(" ");
19508 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019509 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 {
19511 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019512 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 }
19514 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019515 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019517 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 {
19519 if (j)
19520 MSG_PUTS(", ");
19521 msg_puts(FUNCARG(fp, j));
19522 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019523 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 {
19525 if (j)
19526 MSG_PUTS(", ");
19527 MSG_PUTS("...");
19528 }
19529 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019530 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019531 if (p_verbose > 0)
19532 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533}
19534
19535/*
19536 * Find a function by name, return pointer to it in ufuncs.
19537 * Return NULL for unknown function.
19538 */
19539 static ufunc_T *
19540find_func(name)
19541 char_u *name;
19542{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019543 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019545 hi = hash_find(&func_hashtab, name);
19546 if (!HASHITEM_EMPTY(hi))
19547 return HI2UF(hi);
19548 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549}
19550
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019551#if defined(EXITFREE) || defined(PROTO)
19552 void
19553free_all_functions()
19554{
19555 hashitem_T *hi;
19556
19557 /* Need to start all over every time, because func_free() may change the
19558 * hash table. */
19559 while (func_hashtab.ht_used > 0)
19560 for (hi = func_hashtab.ht_array; ; ++hi)
19561 if (!HASHITEM_EMPTY(hi))
19562 {
19563 func_free(HI2UF(hi));
19564 break;
19565 }
19566}
19567#endif
19568
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019569/*
19570 * Return TRUE if a function "name" exists.
19571 */
19572 static int
19573function_exists(name)
19574 char_u *name;
19575{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019576 char_u *nm = name;
19577 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019578 int n = FALSE;
19579
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019580 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019581 nm = skipwhite(nm);
19582
19583 /* Only accept "funcname", "funcname ", "funcname (..." and
19584 * "funcname(...", not "funcname!...". */
19585 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019586 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019587 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019588 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019589 else
19590 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019592 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019593 return n;
19594}
19595
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019596/*
19597 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019598 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019599 */
19600 static int
19601builtin_function(name)
19602 char_u *name;
19603{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019604 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19605 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019606}
19607
Bram Moolenaar05159a02005-02-26 23:04:13 +000019608#if defined(FEAT_PROFILE) || defined(PROTO)
19609/*
19610 * Start profiling function "fp".
19611 */
19612 static void
19613func_do_profile(fp)
19614 ufunc_T *fp;
19615{
19616 fp->uf_tm_count = 0;
19617 profile_zero(&fp->uf_tm_self);
19618 profile_zero(&fp->uf_tm_total);
19619 if (fp->uf_tml_count == NULL)
19620 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19621 (sizeof(int) * fp->uf_lines.ga_len));
19622 if (fp->uf_tml_total == NULL)
19623 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19624 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19625 if (fp->uf_tml_self == NULL)
19626 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19627 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19628 fp->uf_tml_idx = -1;
19629 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19630 || fp->uf_tml_self == NULL)
19631 return; /* out of memory */
19632
19633 fp->uf_profiling = TRUE;
19634}
19635
19636/*
19637 * Dump the profiling results for all functions in file "fd".
19638 */
19639 void
19640func_dump_profile(fd)
19641 FILE *fd;
19642{
19643 hashitem_T *hi;
19644 int todo;
19645 ufunc_T *fp;
19646 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019647 ufunc_T **sorttab;
19648 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019649
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019650 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019651 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19652
Bram Moolenaar05159a02005-02-26 23:04:13 +000019653 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19654 {
19655 if (!HASHITEM_EMPTY(hi))
19656 {
19657 --todo;
19658 fp = HI2UF(hi);
19659 if (fp->uf_profiling)
19660 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019661 if (sorttab != NULL)
19662 sorttab[st_len++] = fp;
19663
Bram Moolenaar05159a02005-02-26 23:04:13 +000019664 if (fp->uf_name[0] == K_SPECIAL)
19665 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19666 else
19667 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19668 if (fp->uf_tm_count == 1)
19669 fprintf(fd, "Called 1 time\n");
19670 else
19671 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19672 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19673 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19674 fprintf(fd, "\n");
19675 fprintf(fd, "count total (s) self (s)\n");
19676
19677 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19678 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019679 if (FUNCLINE(fp, i) == NULL)
19680 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019681 prof_func_line(fd, fp->uf_tml_count[i],
19682 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019683 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19684 }
19685 fprintf(fd, "\n");
19686 }
19687 }
19688 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019689
19690 if (sorttab != NULL && st_len > 0)
19691 {
19692 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19693 prof_total_cmp);
19694 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19695 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19696 prof_self_cmp);
19697 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19698 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019699}
Bram Moolenaar73830342005-02-28 22:48:19 +000019700
19701 static void
19702prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19703 FILE *fd;
19704 ufunc_T **sorttab;
19705 int st_len;
19706 char *title;
19707 int prefer_self; /* when equal print only self time */
19708{
19709 int i;
19710 ufunc_T *fp;
19711
19712 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19713 fprintf(fd, "count total (s) self (s) function\n");
19714 for (i = 0; i < 20 && i < st_len; ++i)
19715 {
19716 fp = sorttab[i];
19717 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19718 prefer_self);
19719 if (fp->uf_name[0] == K_SPECIAL)
19720 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19721 else
19722 fprintf(fd, " %s()\n", fp->uf_name);
19723 }
19724 fprintf(fd, "\n");
19725}
19726
19727/*
19728 * Print the count and times for one function or function line.
19729 */
19730 static void
19731prof_func_line(fd, count, total, self, prefer_self)
19732 FILE *fd;
19733 int count;
19734 proftime_T *total;
19735 proftime_T *self;
19736 int prefer_self; /* when equal print only self time */
19737{
19738 if (count > 0)
19739 {
19740 fprintf(fd, "%5d ", count);
19741 if (prefer_self && profile_equal(total, self))
19742 fprintf(fd, " ");
19743 else
19744 fprintf(fd, "%s ", profile_msg(total));
19745 if (!prefer_self && profile_equal(total, self))
19746 fprintf(fd, " ");
19747 else
19748 fprintf(fd, "%s ", profile_msg(self));
19749 }
19750 else
19751 fprintf(fd, " ");
19752}
19753
19754/*
19755 * Compare function for total time sorting.
19756 */
19757 static int
19758#ifdef __BORLANDC__
19759_RTLENTRYF
19760#endif
19761prof_total_cmp(s1, s2)
19762 const void *s1;
19763 const void *s2;
19764{
19765 ufunc_T *p1, *p2;
19766
19767 p1 = *(ufunc_T **)s1;
19768 p2 = *(ufunc_T **)s2;
19769 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19770}
19771
19772/*
19773 * Compare function for self time sorting.
19774 */
19775 static int
19776#ifdef __BORLANDC__
19777_RTLENTRYF
19778#endif
19779prof_self_cmp(s1, s2)
19780 const void *s1;
19781 const void *s2;
19782{
19783 ufunc_T *p1, *p2;
19784
19785 p1 = *(ufunc_T **)s1;
19786 p2 = *(ufunc_T **)s2;
19787 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19788}
19789
Bram Moolenaar05159a02005-02-26 23:04:13 +000019790#endif
19791
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019792/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019793 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019794 * Return TRUE if a package was loaded.
19795 */
19796 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019797script_autoload(name, reload)
19798 char_u *name;
19799 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019800{
19801 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019802 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019803 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019804 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019805
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019806 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019807 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019808 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019809 return FALSE;
19810
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019811 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019812
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019813 /* Find the name in the list of previously loaded package names. Skip
19814 * "autoload/", it's always the same. */
19815 for (i = 0; i < ga_loaded.ga_len; ++i)
19816 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19817 break;
19818 if (!reload && i < ga_loaded.ga_len)
19819 ret = FALSE; /* was loaded already */
19820 else
19821 {
19822 /* Remember the name if it wasn't loaded already. */
19823 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19824 {
19825 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19826 tofree = NULL;
19827 }
19828
19829 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019830 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019831 ret = TRUE;
19832 }
19833
19834 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019835 return ret;
19836}
19837
19838/*
19839 * Return the autoload script name for a function or variable name.
19840 * Returns NULL when out of memory.
19841 */
19842 static char_u *
19843autoload_name(name)
19844 char_u *name;
19845{
19846 char_u *p;
19847 char_u *scriptname;
19848
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019849 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019850 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19851 if (scriptname == NULL)
19852 return FALSE;
19853 STRCPY(scriptname, "autoload/");
19854 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019855 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019856 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019857 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019858 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019859 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019860}
19861
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19863
19864/*
19865 * Function given to ExpandGeneric() to obtain the list of user defined
19866 * function names.
19867 */
19868 char_u *
19869get_user_func_name(xp, idx)
19870 expand_T *xp;
19871 int idx;
19872{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019873 static long_u done;
19874 static hashitem_T *hi;
19875 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876
19877 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019879 done = 0;
19880 hi = func_hashtab.ht_array;
19881 }
19882 if (done < func_hashtab.ht_used)
19883 {
19884 if (done++ > 0)
19885 ++hi;
19886 while (HASHITEM_EMPTY(hi))
19887 ++hi;
19888 fp = HI2UF(hi);
19889
19890 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19891 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892
19893 cat_func_name(IObuff, fp);
19894 if (xp->xp_context != EXPAND_USER_FUNC)
19895 {
19896 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019897 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 STRCAT(IObuff, ")");
19899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 return IObuff;
19901 }
19902 return NULL;
19903}
19904
19905#endif /* FEAT_CMDL_COMPL */
19906
19907/*
19908 * Copy the function name of "fp" to buffer "buf".
19909 * "buf" must be able to hold the function name plus three bytes.
19910 * Takes care of script-local function names.
19911 */
19912 static void
19913cat_func_name(buf, fp)
19914 char_u *buf;
19915 ufunc_T *fp;
19916{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019917 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 {
19919 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019920 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 }
19922 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019923 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924}
19925
19926/*
19927 * ":delfunction {name}"
19928 */
19929 void
19930ex_delfunction(eap)
19931 exarg_T *eap;
19932{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019933 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 char_u *p;
19935 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019936 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937
19938 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019939 name = trans_function_name(&p, eap->skip, 0, &fudi);
19940 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019942 {
19943 if (fudi.fd_dict != NULL && !eap->skip)
19944 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 if (!ends_excmd(*skipwhite(p)))
19948 {
19949 vim_free(name);
19950 EMSG(_(e_trailing));
19951 return;
19952 }
19953 eap->nextcmd = check_nextcmd(p);
19954 if (eap->nextcmd != NULL)
19955 *p = NUL;
19956
19957 if (!eap->skip)
19958 fp = find_func(name);
19959 vim_free(name);
19960
19961 if (!eap->skip)
19962 {
19963 if (fp == NULL)
19964 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019965 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 return;
19967 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019968 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 {
19970 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19971 return;
19972 }
19973
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019974 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019976 /* Delete the dict item that refers to the function, it will
19977 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019978 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019980 else
19981 func_free(fp);
19982 }
19983}
19984
19985/*
19986 * Free a function and remove it from the list of functions.
19987 */
19988 static void
19989func_free(fp)
19990 ufunc_T *fp;
19991{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019992 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019993
19994 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019995 ga_clear_strings(&(fp->uf_args));
19996 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019997#ifdef FEAT_PROFILE
19998 vim_free(fp->uf_tml_count);
19999 vim_free(fp->uf_tml_total);
20000 vim_free(fp->uf_tml_self);
20001#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020002
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020003 /* remove the function from the function hashtable */
20004 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20005 if (HASHITEM_EMPTY(hi))
20006 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020007 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020008 hash_remove(&func_hashtab, hi);
20009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020010 vim_free(fp);
20011}
20012
20013/*
20014 * Unreference a Function: decrement the reference count and free it when it
20015 * becomes zero. Only for numbered functions.
20016 */
20017 static void
20018func_unref(name)
20019 char_u *name;
20020{
20021 ufunc_T *fp;
20022
20023 if (name != NULL && isdigit(*name))
20024 {
20025 fp = find_func(name);
20026 if (fp == NULL)
20027 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020028 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020029 {
20030 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020031 * when "uf_calls" becomes zero. */
20032 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020033 func_free(fp);
20034 }
20035 }
20036}
20037
20038/*
20039 * Count a reference to a Function.
20040 */
20041 static void
20042func_ref(name)
20043 char_u *name;
20044{
20045 ufunc_T *fp;
20046
20047 if (name != NULL && isdigit(*name))
20048 {
20049 fp = find_func(name);
20050 if (fp == NULL)
20051 EMSG2(_(e_intern2), "func_ref()");
20052 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020053 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 }
20055}
20056
20057/*
20058 * Call a user function.
20059 */
20060 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020061call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 ufunc_T *fp; /* pointer to function */
20063 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 typval_T *argvars; /* arguments */
20065 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 linenr_T firstline; /* first line of range */
20067 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069{
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 char_u *save_sourcing_name;
20071 linenr_T save_sourcing_lnum;
20072 scid_T save_current_SID;
20073 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 int save_did_emsg;
20075 static int depth = 0;
20076 dictitem_T *v;
20077 int fixvar_idx = 0; /* index in fixvar[] */
20078 int i;
20079 int ai;
20080 char_u numbuf[NUMBUFLEN];
20081 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020082#ifdef FEAT_PROFILE
20083 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020084 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086
20087 /* If depth of calling is getting too high, don't execute the function */
20088 if (depth >= p_mfd)
20089 {
20090 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 rettv->v_type = VAR_NUMBER;
20092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 return;
20094 }
20095 ++depth;
20096
20097 line_breakcheck(); /* check for CTRL-C hit */
20098
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020099 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102 fc.rettv = rettv;
20103 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 fc.linenr = 0;
20105 fc.returned = FALSE;
20106 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020108 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 fc.dbg_tick = debug_tick;
20110
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 /*
20112 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20113 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20114 * each argument variable and saves a lot of time.
20115 */
20116 /*
20117 * Init l: variables.
20118 */
20119 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020120 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020121 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020122 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20123 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020125 name = v->di_key;
20126 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20128 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20129 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020130 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 v->di_tv.vval.v_dict = selfdict;
20132 ++selfdict->dv_refcount;
20133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020134
Bram Moolenaar33570922005-01-25 22:26:29 +000020135 /*
20136 * Init a: variables.
20137 * Set a:0 to "argcount".
20138 * Set a:000 to a list with room for the "..." arguments.
20139 */
20140 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20141 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020142 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020143 v = &fc.fixvar[fixvar_idx++].var;
20144 STRCPY(v->di_key, "000");
20145 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20146 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20147 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020148 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 v->di_tv.vval.v_list = &fc.l_varlist;
20150 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20151 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020152 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020153
20154 /*
20155 * Set a:firstline to "firstline" and a:lastline to "lastline".
20156 * Set a:name to named arguments.
20157 * Set a:N to the "..." arguments.
20158 */
20159 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20160 (varnumber_T)firstline);
20161 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20162 (varnumber_T)lastline);
20163 for (i = 0; i < argcount; ++i)
20164 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020165 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 if (ai < 0)
20167 /* named argument a:name */
20168 name = FUNCARG(fp, i);
20169 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020170 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020171 /* "..." argument a:1, a:2, etc. */
20172 sprintf((char *)numbuf, "%d", ai + 1);
20173 name = numbuf;
20174 }
20175 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20176 {
20177 v = &fc.fixvar[fixvar_idx++].var;
20178 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20179 }
20180 else
20181 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020182 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20183 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 if (v == NULL)
20185 break;
20186 v->di_flags = DI_FLAGS_RO;
20187 }
20188 STRCPY(v->di_key, name);
20189 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20190
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020191 /* Note: the values are copied directly to avoid alloc/free.
20192 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020194 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020195
20196 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20197 {
20198 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20199 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020200 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020201 }
20202 }
20203
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 /* Don't redraw while executing the function. */
20205 ++RedrawingDisabled;
20206 save_sourcing_name = sourcing_name;
20207 save_sourcing_lnum = sourcing_lnum;
20208 sourcing_lnum = 1;
20209 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020210 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 if (sourcing_name != NULL)
20212 {
20213 if (save_sourcing_name != NULL
20214 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20215 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20216 else
20217 STRCPY(sourcing_name, "function ");
20218 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20219
20220 if (p_verbose >= 12)
20221 {
20222 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020223 verbose_enter_scroll();
20224
Bram Moolenaar555b2802005-05-19 21:08:39 +000020225 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 if (p_verbose >= 14)
20227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020229 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020230 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020231 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232
20233 msg_puts((char_u *)"(");
20234 for (i = 0; i < argcount; ++i)
20235 {
20236 if (i > 0)
20237 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020238 if (argvars[i].v_type == VAR_NUMBER)
20239 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 else
20241 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020242 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20243 if (s != NULL)
20244 {
20245 trunc_string(s, buf, MSG_BUF_CLEN);
20246 msg_puts(buf);
20247 vim_free(tofree);
20248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 }
20250 }
20251 msg_puts((char_u *)")");
20252 }
20253 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020254
20255 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 --no_wait_return;
20257 }
20258 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020259#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020260 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020261 {
20262 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20263 func_do_profile(fp);
20264 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020265 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020266 {
20267 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020268 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020269 profile_zero(&fp->uf_tm_children);
20270 }
20271 script_prof_save(&wait_start);
20272 }
20273#endif
20274
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020276 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 save_did_emsg = did_emsg;
20278 did_emsg = FALSE;
20279
20280 /* call do_cmdline() to execute the lines */
20281 do_cmdline(NULL, get_func_line, (void *)&fc,
20282 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20283
20284 --RedrawingDisabled;
20285
20286 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020287 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020289 clear_tv(rettv);
20290 rettv->v_type = VAR_NUMBER;
20291 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 }
20293
Bram Moolenaar05159a02005-02-26 23:04:13 +000020294#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020295 if (do_profiling == PROF_YES && (fp->uf_profiling
20296 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020297 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020298 profile_end(&call_start);
20299 profile_sub_wait(&wait_start, &call_start);
20300 profile_add(&fp->uf_tm_total, &call_start);
20301 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020302 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020303 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020304 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20305 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020306 }
20307 }
20308#endif
20309
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 /* when being verbose, mention the return value */
20311 if (p_verbose >= 12)
20312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020314 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020317 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020319 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20320 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020321 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020323 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020324 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020325 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020326 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020327
Bram Moolenaar555b2802005-05-19 21:08:39 +000020328 /* The value may be very long. Skip the middle part, so that we
20329 * have some idea how it starts and ends. smsg() would always
20330 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020331 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20332 if (s != NULL)
20333 {
20334 trunc_string(s, buf, MSG_BUF_CLEN);
20335 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20336 vim_free(tofree);
20337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 }
20339 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020340
20341 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 --no_wait_return;
20343 }
20344
20345 vim_free(sourcing_name);
20346 sourcing_name = save_sourcing_name;
20347 sourcing_lnum = save_sourcing_lnum;
20348 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020349#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020350 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020351 script_prof_restore(&wait_start);
20352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353
20354 if (p_verbose >= 12 && sourcing_name != NULL)
20355 {
20356 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020357 verbose_enter_scroll();
20358
Bram Moolenaar555b2802005-05-19 21:08:39 +000020359 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020361
20362 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 --no_wait_return;
20364 }
20365
20366 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020367 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 /* The a: variables typevals were not alloced, only free the allocated
20370 * variables. */
20371 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20372
20373 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 --depth;
20375}
20376
20377/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 * Add a number variable "name" to dict "dp" with value "nr".
20379 */
20380 static void
20381add_nr_var(dp, v, name, nr)
20382 dict_T *dp;
20383 dictitem_T *v;
20384 char *name;
20385 varnumber_T nr;
20386{
20387 STRCPY(v->di_key, name);
20388 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20389 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20390 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020391 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 v->di_tv.vval.v_number = nr;
20393}
20394
20395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 * ":return [expr]"
20397 */
20398 void
20399ex_return(eap)
20400 exarg_T *eap;
20401{
20402 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 int returning = FALSE;
20405
20406 if (current_funccal == NULL)
20407 {
20408 EMSG(_("E133: :return not inside a function"));
20409 return;
20410 }
20411
20412 if (eap->skip)
20413 ++emsg_skip;
20414
20415 eap->nextcmd = NULL;
20416 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020417 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 {
20419 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020420 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 }
20424 /* It's safer to return also on error. */
20425 else if (!eap->skip)
20426 {
20427 /*
20428 * Return unless the expression evaluation has been cancelled due to an
20429 * aborting error, an interrupt, or an exception.
20430 */
20431 if (!aborting())
20432 returning = do_return(eap, FALSE, TRUE, NULL);
20433 }
20434
20435 /* When skipping or the return gets pending, advance to the next command
20436 * in this line (!returning). Otherwise, ignore the rest of the line.
20437 * Following lines will be ignored by get_func_line(). */
20438 if (returning)
20439 eap->nextcmd = NULL;
20440 else if (eap->nextcmd == NULL) /* no argument */
20441 eap->nextcmd = check_nextcmd(arg);
20442
20443 if (eap->skip)
20444 --emsg_skip;
20445}
20446
20447/*
20448 * Return from a function. Possibly makes the return pending. Also called
20449 * for a pending return at the ":endtry" or after returning from an extra
20450 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020451 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020452 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 * FALSE when the return gets pending.
20454 */
20455 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020456do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 exarg_T *eap;
20458 int reanimate;
20459 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020460 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461{
20462 int idx;
20463 struct condstack *cstack = eap->cstack;
20464
20465 if (reanimate)
20466 /* Undo the return. */
20467 current_funccal->returned = FALSE;
20468
20469 /*
20470 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20471 * not in its finally clause (which then is to be executed next) is found.
20472 * In this case, make the ":return" pending for execution at the ":endtry".
20473 * Otherwise, return normally.
20474 */
20475 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20476 if (idx >= 0)
20477 {
20478 cstack->cs_pending[idx] = CSTP_RETURN;
20479
20480 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020481 /* A pending return again gets pending. "rettv" points to an
20482 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020484 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 else
20486 {
20487 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020488 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020492 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 {
20494 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 else
20498 EMSG(_(e_outofmem));
20499 }
20500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020501 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502
20503 if (reanimate)
20504 {
20505 /* The pending return value could be overwritten by a ":return"
20506 * without argument in a finally clause; reset the default
20507 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508 current_funccal->rettv->v_type = VAR_NUMBER;
20509 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 }
20511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 }
20514 else
20515 {
20516 current_funccal->returned = TRUE;
20517
20518 /* If the return is carried out now, store the return value. For
20519 * a return immediately after reanimation, the value is already
20520 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020523 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020526 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 }
20528 }
20529
20530 return idx < 0;
20531}
20532
20533/*
20534 * Free the variable with a pending return value.
20535 */
20536 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020537discard_pending_return(rettv)
20538 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539{
Bram Moolenaar33570922005-01-25 22:26:29 +000020540 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541}
20542
20543/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020544 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 * is an allocated string. Used by report_pending() for verbose messages.
20546 */
20547 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020548get_return_cmd(rettv)
20549 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020551 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020552 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020553 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020555 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020556 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020557 if (s == NULL)
20558 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559
20560 STRCPY(IObuff, ":return ");
20561 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20562 if (STRLEN(s) + 8 >= IOSIZE)
20563 STRCPY(IObuff + IOSIZE - 4, "...");
20564 vim_free(tofree);
20565 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566}
20567
20568/*
20569 * Get next function line.
20570 * Called by do_cmdline() to get the next line.
20571 * Returns allocated string, or NULL for end of function.
20572 */
20573/* ARGSUSED */
20574 char_u *
20575get_func_line(c, cookie, indent)
20576 int c; /* not used */
20577 void *cookie;
20578 int indent; /* not used */
20579{
Bram Moolenaar33570922005-01-25 22:26:29 +000020580 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020581 ufunc_T *fp = fcp->func;
20582 char_u *retval;
20583 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584
20585 /* If breakpoints have been added/deleted need to check for it. */
20586 if (fcp->dbg_tick != debug_tick)
20587 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020588 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 sourcing_lnum);
20590 fcp->dbg_tick = debug_tick;
20591 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020592#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020593 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020594 func_line_end(cookie);
20595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596
Bram Moolenaar05159a02005-02-26 23:04:13 +000020597 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020598 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20599 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 retval = NULL;
20601 else
20602 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020603 /* Skip NULL lines (continuation lines). */
20604 while (fcp->linenr < gap->ga_len
20605 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20606 ++fcp->linenr;
20607 if (fcp->linenr >= gap->ga_len)
20608 retval = NULL;
20609 else
20610 {
20611 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20612 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020613#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020614 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020615 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020616#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 }
20619
20620 /* Did we encounter a breakpoint? */
20621 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20622 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020623 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020625 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 sourcing_lnum);
20627 fcp->dbg_tick = debug_tick;
20628 }
20629
20630 return retval;
20631}
20632
Bram Moolenaar05159a02005-02-26 23:04:13 +000020633#if defined(FEAT_PROFILE) || defined(PROTO)
20634/*
20635 * Called when starting to read a function line.
20636 * "sourcing_lnum" must be correct!
20637 * When skipping lines it may not actually be executed, but we won't find out
20638 * until later and we need to store the time now.
20639 */
20640 void
20641func_line_start(cookie)
20642 void *cookie;
20643{
20644 funccall_T *fcp = (funccall_T *)cookie;
20645 ufunc_T *fp = fcp->func;
20646
20647 if (fp->uf_profiling && sourcing_lnum >= 1
20648 && sourcing_lnum <= fp->uf_lines.ga_len)
20649 {
20650 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020651 /* Skip continuation lines. */
20652 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20653 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020654 fp->uf_tml_execed = FALSE;
20655 profile_start(&fp->uf_tml_start);
20656 profile_zero(&fp->uf_tml_children);
20657 profile_get_wait(&fp->uf_tml_wait);
20658 }
20659}
20660
20661/*
20662 * Called when actually executing a function line.
20663 */
20664 void
20665func_line_exec(cookie)
20666 void *cookie;
20667{
20668 funccall_T *fcp = (funccall_T *)cookie;
20669 ufunc_T *fp = fcp->func;
20670
20671 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20672 fp->uf_tml_execed = TRUE;
20673}
20674
20675/*
20676 * Called when done with a function line.
20677 */
20678 void
20679func_line_end(cookie)
20680 void *cookie;
20681{
20682 funccall_T *fcp = (funccall_T *)cookie;
20683 ufunc_T *fp = fcp->func;
20684
20685 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20686 {
20687 if (fp->uf_tml_execed)
20688 {
20689 ++fp->uf_tml_count[fp->uf_tml_idx];
20690 profile_end(&fp->uf_tml_start);
20691 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020692 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020693 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20694 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020695 }
20696 fp->uf_tml_idx = -1;
20697 }
20698}
20699#endif
20700
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701/*
20702 * Return TRUE if the currently active function should be ended, because a
20703 * return was encountered or an error occured. Used inside a ":while".
20704 */
20705 int
20706func_has_ended(cookie)
20707 void *cookie;
20708{
Bram Moolenaar33570922005-01-25 22:26:29 +000020709 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710
20711 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20712 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020713 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 || fcp->returned);
20715}
20716
20717/*
20718 * return TRUE if cookie indicates a function which "abort"s on errors.
20719 */
20720 int
20721func_has_abort(cookie)
20722 void *cookie;
20723{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020724 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725}
20726
20727#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20728typedef enum
20729{
20730 VAR_FLAVOUR_DEFAULT,
20731 VAR_FLAVOUR_SESSION,
20732 VAR_FLAVOUR_VIMINFO
20733} var_flavour_T;
20734
20735static var_flavour_T var_flavour __ARGS((char_u *varname));
20736
20737 static var_flavour_T
20738var_flavour(varname)
20739 char_u *varname;
20740{
20741 char_u *p = varname;
20742
20743 if (ASCII_ISUPPER(*p))
20744 {
20745 while (*(++p))
20746 if (ASCII_ISLOWER(*p))
20747 return VAR_FLAVOUR_SESSION;
20748 return VAR_FLAVOUR_VIMINFO;
20749 }
20750 else
20751 return VAR_FLAVOUR_DEFAULT;
20752}
20753#endif
20754
20755#if defined(FEAT_VIMINFO) || defined(PROTO)
20756/*
20757 * Restore global vars that start with a capital from the viminfo file
20758 */
20759 int
20760read_viminfo_varlist(virp, writing)
20761 vir_T *virp;
20762 int writing;
20763{
20764 char_u *tab;
20765 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020766 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767
20768 if (!writing && (find_viminfo_parameter('!') != NULL))
20769 {
20770 tab = vim_strchr(virp->vir_line + 1, '\t');
20771 if (tab != NULL)
20772 {
20773 *tab++ = '\0'; /* isolate the variable name */
20774 if (*tab == 'S') /* string var */
20775 is_string = TRUE;
20776
20777 tab = vim_strchr(tab, '\t');
20778 if (tab != NULL)
20779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 if (is_string)
20781 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020782 tv.v_type = VAR_STRING;
20783 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 }
20786 else
20787 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020788 tv.v_type = VAR_NUMBER;
20789 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020791 set_var(virp->vir_line + 1, &tv, FALSE);
20792 if (is_string)
20793 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 }
20795 }
20796 }
20797
20798 return viminfo_readline(virp);
20799}
20800
20801/*
20802 * Write global vars that start with a capital to the viminfo file
20803 */
20804 void
20805write_viminfo_varlist(fp)
20806 FILE *fp;
20807{
Bram Moolenaar33570922005-01-25 22:26:29 +000020808 hashitem_T *hi;
20809 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020810 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020811 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020812 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020814 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815
20816 if (find_viminfo_parameter('!') == NULL)
20817 return;
20818
20819 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020820
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020821 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020824 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020826 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020827 this_var = HI2DI(hi);
20828 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020829 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020830 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020831 {
20832 case VAR_STRING: s = "STR"; break;
20833 case VAR_NUMBER: s = "NUM"; break;
20834 default: continue;
20835 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020836 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020837 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020838 if (p != NULL)
20839 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020840 vim_free(tofree);
20841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 }
20843 }
20844}
20845#endif
20846
20847#if defined(FEAT_SESSION) || defined(PROTO)
20848 int
20849store_session_globals(fd)
20850 FILE *fd;
20851{
Bram Moolenaar33570922005-01-25 22:26:29 +000020852 hashitem_T *hi;
20853 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020854 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 char_u *p, *t;
20856
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020857 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020858 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020860 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020862 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020863 this_var = HI2DI(hi);
20864 if ((this_var->di_tv.v_type == VAR_NUMBER
20865 || this_var->di_tv.v_type == VAR_STRING)
20866 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020867 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020868 /* Escape special characters with a backslash. Turn a LF and
20869 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020870 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020871 (char_u *)"\\\"\n\r");
20872 if (p == NULL) /* out of memory */
20873 break;
20874 for (t = p; *t != NUL; ++t)
20875 if (*t == '\n')
20876 *t = 'n';
20877 else if (*t == '\r')
20878 *t = 'r';
20879 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020880 this_var->di_key,
20881 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20882 : ' ',
20883 p,
20884 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20885 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020886 || put_eol(fd) == FAIL)
20887 {
20888 vim_free(p);
20889 return FAIL;
20890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 vim_free(p);
20892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 }
20894 }
20895 return OK;
20896}
20897#endif
20898
Bram Moolenaar661b1822005-07-28 22:36:45 +000020899/*
20900 * Display script name where an item was last set.
20901 * Should only be invoked when 'verbose' is non-zero.
20902 */
20903 void
20904last_set_msg(scriptID)
20905 scid_T scriptID;
20906{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020907 char_u *p;
20908
Bram Moolenaar661b1822005-07-28 22:36:45 +000020909 if (scriptID != 0)
20910 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020911 p = home_replace_save(NULL, get_scriptname(scriptID));
20912 if (p != NULL)
20913 {
20914 verbose_enter();
20915 MSG_PUTS(_("\n\tLast set from "));
20916 MSG_PUTS(p);
20917 vim_free(p);
20918 verbose_leave();
20919 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020920 }
20921}
20922
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923#endif /* FEAT_EVAL */
20924
20925#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20926
20927
20928#ifdef WIN3264
20929/*
20930 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20931 */
20932static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20933static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20934static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20935
20936/*
20937 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020938 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 */
20940 static int
20941get_short_pathname(fnamep, bufp, fnamelen)
20942 char_u **fnamep;
20943 char_u **bufp;
20944 int *fnamelen;
20945{
20946 int l,len;
20947 char_u *newbuf;
20948
20949 len = *fnamelen;
20950
20951 l = GetShortPathName(*fnamep, *fnamep, len);
20952 if (l > len - 1)
20953 {
20954 /* If that doesn't work (not enough space), then save the string
20955 * and try again with a new buffer big enough
20956 */
20957 newbuf = vim_strnsave(*fnamep, l);
20958 if (newbuf == NULL)
20959 return 0;
20960
20961 vim_free(*bufp);
20962 *fnamep = *bufp = newbuf;
20963
20964 l = GetShortPathName(*fnamep,*fnamep,l+1);
20965
20966 /* Really should always succeed, as the buffer is big enough */
20967 }
20968
20969 *fnamelen = l;
20970 return 1;
20971}
20972
20973/*
20974 * Create a short path name. Returns the length of the buffer it needs.
20975 * Doesn't copy over the end of the buffer passed in.
20976 */
20977 static int
20978shortpath_for_invalid_fname(fname, bufp, fnamelen)
20979 char_u **fname;
20980 char_u **bufp;
20981 int *fnamelen;
20982{
20983 char_u *s, *p, *pbuf2, *pbuf3;
20984 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020985 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986
20987 /* Make a copy */
20988 len2 = *fnamelen;
20989 pbuf2 = vim_strnsave(*fname, len2);
20990 pbuf3 = NULL;
20991
20992 s = pbuf2 + len2 - 1; /* Find the end */
20993 slen = 1;
20994 plen = len2;
20995
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020996 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 {
20998 --s;
20999 ++slen;
21000 --plen;
21001 }
21002
21003 do
21004 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021005 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021006 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 {
21008 --s;
21009 ++slen;
21010 --plen;
21011 }
21012 if (s <= pbuf2)
21013 break;
21014
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021015 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 ch = *s;
21017 *s = 0; /* get_short_pathname requires a null-terminated string */
21018
21019 /* Try it in situ */
21020 p = pbuf2;
21021 if (!get_short_pathname(&p, &pbuf3, &plen))
21022 {
21023 vim_free(pbuf2);
21024 return -1;
21025 }
21026 *s = ch; /* Preserve the string */
21027 } while (plen == 0);
21028
21029 if (plen > 0)
21030 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021031 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 *fnamelen = len = plen + slen;
21033 vim_free(*bufp);
21034 if (len > len2)
21035 {
21036 /* If there's not enough space in the currently allocated string,
21037 * then copy it to a buffer big enough.
21038 */
21039 *fname= *bufp = vim_strnsave(p, len);
21040 if (*fname == NULL)
21041 return -1;
21042 }
21043 else
21044 {
21045 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21046 *fname = *bufp = pbuf2;
21047 if (p != pbuf2)
21048 strncpy(*fname, p, plen);
21049 pbuf2 = NULL;
21050 }
21051 /* Concat the next bit */
21052 strncpy(*fname + plen, s, slen);
21053 (*fname)[len] = '\0';
21054 }
21055 vim_free(pbuf3);
21056 vim_free(pbuf2);
21057 return 0;
21058}
21059
21060/*
21061 * Get a pathname for a partial path.
21062 */
21063 static int
21064shortpath_for_partial(fnamep, bufp, fnamelen)
21065 char_u **fnamep;
21066 char_u **bufp;
21067 int *fnamelen;
21068{
21069 int sepcount, len, tflen;
21070 char_u *p;
21071 char_u *pbuf, *tfname;
21072 int hasTilde;
21073
21074 /* Count up the path seperators from the RHS.. so we know which part
21075 * of the path to return.
21076 */
21077 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021078 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 if (vim_ispathsep(*p))
21080 ++sepcount;
21081
21082 /* Need full path first (use expand_env() to remove a "~/") */
21083 hasTilde = (**fnamep == '~');
21084 if (hasTilde)
21085 pbuf = tfname = expand_env_save(*fnamep);
21086 else
21087 pbuf = tfname = FullName_save(*fnamep, FALSE);
21088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021089 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090
21091 if (!get_short_pathname(&tfname, &pbuf, &len))
21092 return -1;
21093
21094 if (len == 0)
21095 {
21096 /* Don't have a valid filename, so shorten the rest of the
21097 * path if we can. This CAN give us invalid 8.3 filenames, but
21098 * there's not a lot of point in guessing what it might be.
21099 */
21100 len = tflen;
21101 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21102 return -1;
21103 }
21104
21105 /* Count the paths backward to find the beginning of the desired string. */
21106 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021107 {
21108#ifdef FEAT_MBYTE
21109 if (has_mbyte)
21110 p -= mb_head_off(tfname, p);
21111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 if (vim_ispathsep(*p))
21113 {
21114 if (sepcount == 0 || (hasTilde && sepcount == 1))
21115 break;
21116 else
21117 sepcount --;
21118 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 if (hasTilde)
21121 {
21122 --p;
21123 if (p >= tfname)
21124 *p = '~';
21125 else
21126 return -1;
21127 }
21128 else
21129 ++p;
21130
21131 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21132 vim_free(*bufp);
21133 *fnamelen = (int)STRLEN(p);
21134 *bufp = pbuf;
21135 *fnamep = p;
21136
21137 return 0;
21138}
21139#endif /* WIN3264 */
21140
21141/*
21142 * Adjust a filename, according to a string of modifiers.
21143 * *fnamep must be NUL terminated when called. When returning, the length is
21144 * determined by *fnamelen.
21145 * Returns valid flags.
21146 * When there is an error, *fnamep is set to NULL.
21147 */
21148 int
21149modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21150 char_u *src; /* string with modifiers */
21151 int *usedlen; /* characters after src that are used */
21152 char_u **fnamep; /* file name so far */
21153 char_u **bufp; /* buffer for allocated file name or NULL */
21154 int *fnamelen; /* length of fnamep */
21155{
21156 int valid = 0;
21157 char_u *tail;
21158 char_u *s, *p, *pbuf;
21159 char_u dirname[MAXPATHL];
21160 int c;
21161 int has_fullname = 0;
21162#ifdef WIN3264
21163 int has_shortname = 0;
21164#endif
21165
21166repeat:
21167 /* ":p" - full path/file_name */
21168 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21169 {
21170 has_fullname = 1;
21171
21172 valid |= VALID_PATH;
21173 *usedlen += 2;
21174
21175 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21176 if ((*fnamep)[0] == '~'
21177#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21178 && ((*fnamep)[1] == '/'
21179# ifdef BACKSLASH_IN_FILENAME
21180 || (*fnamep)[1] == '\\'
21181# endif
21182 || (*fnamep)[1] == NUL)
21183
21184#endif
21185 )
21186 {
21187 *fnamep = expand_env_save(*fnamep);
21188 vim_free(*bufp); /* free any allocated file name */
21189 *bufp = *fnamep;
21190 if (*fnamep == NULL)
21191 return -1;
21192 }
21193
21194 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021195 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 {
21197 if (vim_ispathsep(*p)
21198 && p[1] == '.'
21199 && (p[2] == NUL
21200 || vim_ispathsep(p[2])
21201 || (p[2] == '.'
21202 && (p[3] == NUL || vim_ispathsep(p[3])))))
21203 break;
21204 }
21205
21206 /* FullName_save() is slow, don't use it when not needed. */
21207 if (*p != NUL || !vim_isAbsName(*fnamep))
21208 {
21209 *fnamep = FullName_save(*fnamep, *p != NUL);
21210 vim_free(*bufp); /* free any allocated file name */
21211 *bufp = *fnamep;
21212 if (*fnamep == NULL)
21213 return -1;
21214 }
21215
21216 /* Append a path separator to a directory. */
21217 if (mch_isdir(*fnamep))
21218 {
21219 /* Make room for one or two extra characters. */
21220 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21221 vim_free(*bufp); /* free any allocated file name */
21222 *bufp = *fnamep;
21223 if (*fnamep == NULL)
21224 return -1;
21225 add_pathsep(*fnamep);
21226 }
21227 }
21228
21229 /* ":." - path relative to the current directory */
21230 /* ":~" - path relative to the home directory */
21231 /* ":8" - shortname path - postponed till after */
21232 while (src[*usedlen] == ':'
21233 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21234 {
21235 *usedlen += 2;
21236 if (c == '8')
21237 {
21238#ifdef WIN3264
21239 has_shortname = 1; /* Postpone this. */
21240#endif
21241 continue;
21242 }
21243 pbuf = NULL;
21244 /* Need full path first (use expand_env() to remove a "~/") */
21245 if (!has_fullname)
21246 {
21247 if (c == '.' && **fnamep == '~')
21248 p = pbuf = expand_env_save(*fnamep);
21249 else
21250 p = pbuf = FullName_save(*fnamep, FALSE);
21251 }
21252 else
21253 p = *fnamep;
21254
21255 has_fullname = 0;
21256
21257 if (p != NULL)
21258 {
21259 if (c == '.')
21260 {
21261 mch_dirname(dirname, MAXPATHL);
21262 s = shorten_fname(p, dirname);
21263 if (s != NULL)
21264 {
21265 *fnamep = s;
21266 if (pbuf != NULL)
21267 {
21268 vim_free(*bufp); /* free any allocated file name */
21269 *bufp = pbuf;
21270 pbuf = NULL;
21271 }
21272 }
21273 }
21274 else
21275 {
21276 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21277 /* Only replace it when it starts with '~' */
21278 if (*dirname == '~')
21279 {
21280 s = vim_strsave(dirname);
21281 if (s != NULL)
21282 {
21283 *fnamep = s;
21284 vim_free(*bufp);
21285 *bufp = s;
21286 }
21287 }
21288 }
21289 vim_free(pbuf);
21290 }
21291 }
21292
21293 tail = gettail(*fnamep);
21294 *fnamelen = (int)STRLEN(*fnamep);
21295
21296 /* ":h" - head, remove "/file_name", can be repeated */
21297 /* Don't remove the first "/" or "c:\" */
21298 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21299 {
21300 valid |= VALID_HEAD;
21301 *usedlen += 2;
21302 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021303 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 --tail;
21305 *fnamelen = (int)(tail - *fnamep);
21306#ifdef VMS
21307 if (*fnamelen > 0)
21308 *fnamelen += 1; /* the path separator is part of the path */
21309#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021310 while (tail > s && !after_pathsep(s, tail))
21311 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 }
21313
21314 /* ":8" - shortname */
21315 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21316 {
21317 *usedlen += 2;
21318#ifdef WIN3264
21319 has_shortname = 1;
21320#endif
21321 }
21322
21323#ifdef WIN3264
21324 /* Check shortname after we have done 'heads' and before we do 'tails'
21325 */
21326 if (has_shortname)
21327 {
21328 pbuf = NULL;
21329 /* Copy the string if it is shortened by :h */
21330 if (*fnamelen < (int)STRLEN(*fnamep))
21331 {
21332 p = vim_strnsave(*fnamep, *fnamelen);
21333 if (p == 0)
21334 return -1;
21335 vim_free(*bufp);
21336 *bufp = *fnamep = p;
21337 }
21338
21339 /* Split into two implementations - makes it easier. First is where
21340 * there isn't a full name already, second is where there is.
21341 */
21342 if (!has_fullname && !vim_isAbsName(*fnamep))
21343 {
21344 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21345 return -1;
21346 }
21347 else
21348 {
21349 int l;
21350
21351 /* Simple case, already have the full-name
21352 * Nearly always shorter, so try first time. */
21353 l = *fnamelen;
21354 if (!get_short_pathname(fnamep, bufp, &l))
21355 return -1;
21356
21357 if (l == 0)
21358 {
21359 /* Couldn't find the filename.. search the paths.
21360 */
21361 l = *fnamelen;
21362 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21363 return -1;
21364 }
21365 *fnamelen = l;
21366 }
21367 }
21368#endif /* WIN3264 */
21369
21370 /* ":t" - tail, just the basename */
21371 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21372 {
21373 *usedlen += 2;
21374 *fnamelen -= (int)(tail - *fnamep);
21375 *fnamep = tail;
21376 }
21377
21378 /* ":e" - extension, can be repeated */
21379 /* ":r" - root, without extension, can be repeated */
21380 while (src[*usedlen] == ':'
21381 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21382 {
21383 /* find a '.' in the tail:
21384 * - for second :e: before the current fname
21385 * - otherwise: The last '.'
21386 */
21387 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21388 s = *fnamep - 2;
21389 else
21390 s = *fnamep + *fnamelen - 1;
21391 for ( ; s > tail; --s)
21392 if (s[0] == '.')
21393 break;
21394 if (src[*usedlen + 1] == 'e') /* :e */
21395 {
21396 if (s > tail)
21397 {
21398 *fnamelen += (int)(*fnamep - (s + 1));
21399 *fnamep = s + 1;
21400#ifdef VMS
21401 /* cut version from the extension */
21402 s = *fnamep + *fnamelen - 1;
21403 for ( ; s > *fnamep; --s)
21404 if (s[0] == ';')
21405 break;
21406 if (s > *fnamep)
21407 *fnamelen = s - *fnamep;
21408#endif
21409 }
21410 else if (*fnamep <= tail)
21411 *fnamelen = 0;
21412 }
21413 else /* :r */
21414 {
21415 if (s > tail) /* remove one extension */
21416 *fnamelen = (int)(s - *fnamep);
21417 }
21418 *usedlen += 2;
21419 }
21420
21421 /* ":s?pat?foo?" - substitute */
21422 /* ":gs?pat?foo?" - global substitute */
21423 if (src[*usedlen] == ':'
21424 && (src[*usedlen + 1] == 's'
21425 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21426 {
21427 char_u *str;
21428 char_u *pat;
21429 char_u *sub;
21430 int sep;
21431 char_u *flags;
21432 int didit = FALSE;
21433
21434 flags = (char_u *)"";
21435 s = src + *usedlen + 2;
21436 if (src[*usedlen + 1] == 'g')
21437 {
21438 flags = (char_u *)"g";
21439 ++s;
21440 }
21441
21442 sep = *s++;
21443 if (sep)
21444 {
21445 /* find end of pattern */
21446 p = vim_strchr(s, sep);
21447 if (p != NULL)
21448 {
21449 pat = vim_strnsave(s, (int)(p - s));
21450 if (pat != NULL)
21451 {
21452 s = p + 1;
21453 /* find end of substitution */
21454 p = vim_strchr(s, sep);
21455 if (p != NULL)
21456 {
21457 sub = vim_strnsave(s, (int)(p - s));
21458 str = vim_strnsave(*fnamep, *fnamelen);
21459 if (sub != NULL && str != NULL)
21460 {
21461 *usedlen = (int)(p + 1 - src);
21462 s = do_string_sub(str, pat, sub, flags);
21463 if (s != NULL)
21464 {
21465 *fnamep = s;
21466 *fnamelen = (int)STRLEN(s);
21467 vim_free(*bufp);
21468 *bufp = s;
21469 didit = TRUE;
21470 }
21471 }
21472 vim_free(sub);
21473 vim_free(str);
21474 }
21475 vim_free(pat);
21476 }
21477 }
21478 /* after using ":s", repeat all the modifiers */
21479 if (didit)
21480 goto repeat;
21481 }
21482 }
21483
21484 return valid;
21485}
21486
21487/*
21488 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21489 * "flags" can be "g" to do a global substitute.
21490 * Returns an allocated string, NULL for error.
21491 */
21492 char_u *
21493do_string_sub(str, pat, sub, flags)
21494 char_u *str;
21495 char_u *pat;
21496 char_u *sub;
21497 char_u *flags;
21498{
21499 int sublen;
21500 regmatch_T regmatch;
21501 int i;
21502 int do_all;
21503 char_u *tail;
21504 garray_T ga;
21505 char_u *ret;
21506 char_u *save_cpo;
21507
21508 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21509 save_cpo = p_cpo;
21510 p_cpo = (char_u *)"";
21511
21512 ga_init2(&ga, 1, 200);
21513
21514 do_all = (flags[0] == 'g');
21515
21516 regmatch.rm_ic = p_ic;
21517 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21518 if (regmatch.regprog != NULL)
21519 {
21520 tail = str;
21521 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21522 {
21523 /*
21524 * Get some space for a temporary buffer to do the substitution
21525 * into. It will contain:
21526 * - The text up to where the match is.
21527 * - The substituted text.
21528 * - The text after the match.
21529 */
21530 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21531 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21532 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21533 {
21534 ga_clear(&ga);
21535 break;
21536 }
21537
21538 /* copy the text up to where the match is */
21539 i = (int)(regmatch.startp[0] - tail);
21540 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21541 /* add the substituted text */
21542 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21543 + ga.ga_len + i, TRUE, TRUE, FALSE);
21544 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 /* avoid getting stuck on a match with an empty string */
21546 if (tail == regmatch.endp[0])
21547 {
21548 if (*tail == NUL)
21549 break;
21550 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21551 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 }
21553 else
21554 {
21555 tail = regmatch.endp[0];
21556 if (*tail == NUL)
21557 break;
21558 }
21559 if (!do_all)
21560 break;
21561 }
21562
21563 if (ga.ga_data != NULL)
21564 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21565
21566 vim_free(regmatch.regprog);
21567 }
21568
21569 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21570 ga_clear(&ga);
21571 p_cpo = save_cpo;
21572
21573 return ret;
21574}
21575
21576#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */