blob: 0dd8bd0b75019aa378670ae6209cb347d7504d41 [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 Moolenaar8af1fbf2008-01-05 12:35:21 +0000348 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000349};
350
351/* shorthand */
352#define vv_type vv_di.di_tv.v_type
353#define vv_nr vv_di.di_tv.vval.v_number
354#define vv_str vv_di.di_tv.vval.v_string
355#define vv_tv vv_di.di_tv
356
357/*
358 * The v: variables are stored in dictionary "vimvardict".
359 * "vimvars_var" is the variable that is used for the "l:" scope.
360 */
361static dict_T vimvardict;
362static dictitem_T vimvars_var;
363#define vimvarht vimvardict.dv_hashtab
364
Bram Moolenaara40058a2005-07-11 22:42:07 +0000365static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
366static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
367#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
368static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
369#endif
370static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
371static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
372static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000373static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
374static void list_glob_vars __ARGS((int *first));
375static void list_buf_vars __ARGS((int *first));
376static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000377#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000378static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000379#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000380static void list_vim_vars __ARGS((int *first));
381static void list_script_vars __ARGS((int *first));
382static void list_func_vars __ARGS((int *first));
383static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
385static int check_changedtick __ARGS((char_u *arg));
386static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
387static void clear_lval __ARGS((lval_T *lp));
388static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
389static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
390static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
391static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
392static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
393static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
394static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
395static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
396static void item_lock __ARGS((typval_T *tv, int deep, int lock));
397static int tv_islocked __ARGS((typval_T *tv));
398
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
400static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000408static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000413static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static listitem_T *listitem_alloc __ARGS((void));
415static void listitem_free __ARGS((listitem_T *item));
416static void listitem_remove __ARGS((list_T *l, listitem_T *item));
417static long list_len __ARGS((list_T *l));
418static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
419static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
420static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_append __ARGS((list_T *l, listitem_T *item));
425static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000426static int list_append_string __ARGS((list_T *l, char_u *str, int len));
427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000433static char_u *list2string __ARGS((typval_T *tv, int copyID));
434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000435static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
436static void set_ref_in_list __ARGS((list_T *l, int copyID));
437static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000439static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_alloc __ARGS((char_u *key));
441static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
442static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
443static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000444static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int dict_add __ARGS((dict_T *d, dictitem_T *item));
446static long dict_len __ARGS((dict_T *d));
447static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
453static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
454static int find_internal_func __ARGS((char_u *name));
455static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
456static 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));
457static 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 +0000458static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000476static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000479static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000482static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000483static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
491static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000504static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000518static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000520static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000526static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000527static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000534static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000535static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000536static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000539static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000547static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000561static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000567static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000583static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000584static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000585static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000587static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000591#ifdef vim_mkdir
592static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
593#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000597static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000599static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000600static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000602static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000603static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000616static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000618static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000625static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000627static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000628static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000630static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000632static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000635static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000636static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000639static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640#ifdef HAVE_STRFTIME
641static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
643static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000654static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000657static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000658static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000659static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000660static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000662static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000676static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000679static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000682static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static int get_env_len __ARGS((char_u **arg));
684static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000685static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000686static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
687#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
688#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
689 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000690static 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 +0000691static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000692static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000693static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
694static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static typval_T *alloc_tv __ARGS((void));
696static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void init_tv __ARGS((typval_T *varp));
698static long get_tv_number __ARGS((typval_T *varp));
699static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000700static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static char_u *get_tv_string __ARGS((typval_T *varp));
702static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000703static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000705static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
707static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
708static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000709static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
710static 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 +0000711static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
712static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000713static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000714static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000716static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
718static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
719static int eval_fname_script __ARGS((char_u *p));
720static int eval_fname_sid __ARGS((char_u *p));
721static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static ufunc_T *find_func __ARGS((char_u *name));
723static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000724static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000725#ifdef FEAT_PROFILE
726static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000727static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
728static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
729static int
730# ifdef __BORLANDC__
731 _RTLENTRYF
732# endif
733 prof_total_cmp __ARGS((const void *s1, const void *s2));
734static int
735# ifdef __BORLANDC__
736 _RTLENTRYF
737# endif
738 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000739#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000740static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000741static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000742static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void func_free __ARGS((ufunc_T *fp));
744static void func_unref __ARGS((char_u *name));
745static void func_ref __ARGS((char_u *name));
746static 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));
747static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000748static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
749static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000750static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000751static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000752static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000754/* Character used as separated in autoload function/variable names. */
755#define AUTOLOAD_CHAR '#'
756
Bram Moolenaar33570922005-01-25 22:26:29 +0000757/*
758 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000759 */
760 void
761eval_init()
762{
Bram Moolenaar33570922005-01-25 22:26:29 +0000763 int i;
764 struct vimvar *p;
765
766 init_var_dict(&globvardict, &globvars_var);
767 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000768 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000769 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770
771 for (i = 0; i < VV_LEN; ++i)
772 {
773 p = &vimvars[i];
774 STRCPY(p->vv_di.di_key, p->vv_name);
775 if (p->vv_flags & VV_RO)
776 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
777 else if (p->vv_flags & VV_RO_SBX)
778 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
779 else
780 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000781
782 /* add to v: scope dict, unless the value is not always available */
783 if (p->vv_type != VAR_UNKNOWN)
784 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000786 /* add to compat scope dict */
787 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000788 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000789}
790
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000791#if defined(EXITFREE) || defined(PROTO)
792 void
793eval_clear()
794{
795 int i;
796 struct vimvar *p;
797
798 for (i = 0; i < VV_LEN; ++i)
799 {
800 p = &vimvars[i];
801 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000803 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804 p->vv_di.di_tv.vval.v_string = NULL;
805 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000806 }
807 hash_clear(&vimvarht);
808 hash_clear(&compat_hashtab);
809
810 /* script-local variables */
811 for (i = 1; i <= ga_scripts.ga_len; ++i)
812 vars_clear(&SCRIPT_VARS(i));
813 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000814 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000815
816 /* global variables */
817 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000820 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000821 hash_clear(&func_hashtab);
822
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000823 /* autoloaded script names */
824 ga_clear_strings(&ga_loaded);
825
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000826 /* unreferenced lists and dicts */
827 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000828}
829#endif
830
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Return the name of the executed function.
833 */
834 char_u *
835func_name(cookie)
836 void *cookie;
837{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000838 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * Return the address holding the next breakpoint line for a funccall cookie.
843 */
844 linenr_T *
845func_breakpoint(cookie)
846 void *cookie;
847{
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
851/*
852 * Return the address holding the debug tick for a funccall cookie.
853 */
854 int *
855func_dbg_tick(cookie)
856 void *cookie;
857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/*
862 * Return the nesting level for a funccall cookie.
863 */
864 int
865func_level(cookie)
866 void *cookie;
867{
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000872funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874/*
875 * Return TRUE when a function was ended by a ":return" command.
876 */
877 int
878current_func_returned()
879{
880 return current_funccal->returned;
881}
882
883
884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * Set an internal variable to a string value. Creates the variable if it does
886 * not already exist.
887 */
888 void
889set_internal_string_var(name, value)
890 char_u *name;
891 char_u *value;
892{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000894 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896 val = vim_strsave(value);
897 if (val != NULL)
898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000902 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000903 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905 }
906}
907
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000909static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910static char_u *redir_endp = NULL;
911static char_u *redir_varname = NULL;
912
913/*
914 * Start recording command output to a variable
915 * Returns OK if successfully completed the setup. FAIL otherwise.
916 */
917 int
918var_redir_start(name, append)
919 char_u *name;
920 int append; /* append to an existing variable */
921{
922 int save_emsg;
923 int err;
924 typval_T tv;
925
926 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000927 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000928 {
929 EMSG(_(e_invarg));
930 return FAIL;
931 }
932
933 redir_varname = vim_strsave(name);
934 if (redir_varname == NULL)
935 return FAIL;
936
937 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
938 if (redir_lval == NULL)
939 {
940 var_redir_stop();
941 return FAIL;
942 }
943
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000944 /* The output is stored in growarray "redir_ga" until redirection ends. */
945 ga_init2(&redir_ga, (int)sizeof(char), 500);
946
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000947 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000948 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
949 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000950 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
951 {
952 if (redir_endp != NULL && *redir_endp != NUL)
953 /* Trailing characters are present after the variable name */
954 EMSG(_(e_trailing));
955 else
956 EMSG(_(e_invarg));
957 var_redir_stop();
958 return FAIL;
959 }
960
961 /* check if we can write to the variable: set it to or append an empty
962 * string */
963 save_emsg = did_emsg;
964 did_emsg = FALSE;
965 tv.v_type = VAR_STRING;
966 tv.vval.v_string = (char_u *)"";
967 if (append)
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
969 else
970 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
971 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000972 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000973 if (err)
974 {
975 var_redir_stop();
976 return FAIL;
977 }
978 if (redir_lval->ll_newkey != NULL)
979 {
980 /* Dictionary item was created, don't do it again. */
981 vim_free(redir_lval->ll_newkey);
982 redir_lval->ll_newkey = NULL;
983 }
984
985 return OK;
986}
987
988/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000989 * Append "value[value_len]" to the variable set by var_redir_start().
990 * The actual appending is postponed until redirection ends, because the value
991 * appended may in fact be the string we write to, changing it may cause freed
992 * memory to be used:
993 * :redir => foo
994 * :let foo
995 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000996 */
997 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001000 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001002 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001003
1004 if (redir_lval == NULL)
1005 return;
1006
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001007 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001012 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001013 {
1014 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001015 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001016 }
1017 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019}
1020
1021/*
1022 * Stop redirecting command output to a variable.
1023 */
1024 void
1025var_redir_stop()
1026{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001027 typval_T tv;
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 if (redir_lval != NULL)
1030 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001031 /* Append the trailing NUL. */
1032 ga_append(&redir_ga, NUL);
1033
1034 /* Assign the text to the variable. */
1035 tv.v_type = VAR_STRING;
1036 tv.vval.v_string = redir_ga.ga_data;
1037 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1038 vim_free(tv.vval.v_string);
1039
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 clear_lval(redir_lval);
1041 vim_free(redir_lval);
1042 redir_lval = NULL;
1043 }
1044 vim_free(redir_varname);
1045 redir_varname = NULL;
1046}
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048# if defined(FEAT_MBYTE) || defined(PROTO)
1049 int
1050eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1051 char_u *enc_from;
1052 char_u *enc_to;
1053 char_u *fname_from;
1054 char_u *fname_to;
1055{
1056 int err = FALSE;
1057
1058 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1059 set_vim_var_string(VV_CC_TO, enc_to, -1);
1060 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1061 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1062 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1063 err = TRUE;
1064 set_vim_var_string(VV_CC_FROM, NULL, -1);
1065 set_vim_var_string(VV_CC_TO, NULL, -1);
1066 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1067 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1068
1069 if (err)
1070 return FAIL;
1071 return OK;
1072}
1073# endif
1074
1075# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1076 int
1077eval_printexpr(fname, args)
1078 char_u *fname;
1079 char_u *args;
1080{
1081 int err = FALSE;
1082
1083 set_vim_var_string(VV_FNAME_IN, fname, -1);
1084 set_vim_var_string(VV_CMDARG, args, -1);
1085 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1086 err = TRUE;
1087 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1088 set_vim_var_string(VV_CMDARG, NULL, -1);
1089
1090 if (err)
1091 {
1092 mch_remove(fname);
1093 return FAIL;
1094 }
1095 return OK;
1096}
1097# endif
1098
1099# if defined(FEAT_DIFF) || defined(PROTO)
1100 void
1101eval_diff(origfile, newfile, outfile)
1102 char_u *origfile;
1103 char_u *newfile;
1104 char_u *outfile;
1105{
1106 int err = FALSE;
1107
1108 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1109 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1110 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1111 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1112 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1113 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1114 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1115}
1116
1117 void
1118eval_patch(origfile, difffile, outfile)
1119 char_u *origfile;
1120 char_u *difffile;
1121 char_u *outfile;
1122{
1123 int err;
1124
1125 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1126 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1127 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1128 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1129 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1130 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1131 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1132}
1133# endif
1134
1135/*
1136 * Top level evaluation function, returning a boolean.
1137 * Sets "error" to TRUE if there was an error.
1138 * Return TRUE or FALSE.
1139 */
1140 int
1141eval_to_bool(arg, error, nextcmd, skip)
1142 char_u *arg;
1143 int *error;
1144 char_u **nextcmd;
1145 int skip; /* only parse, don't execute */
1146{
Bram Moolenaar33570922005-01-25 22:26:29 +00001147 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 int retval = FALSE;
1149
1150 if (skip)
1151 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 else
1155 {
1156 *error = FALSE;
1157 if (!skip)
1158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001159 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 }
1163 if (skip)
1164 --emsg_skip;
1165
1166 return retval;
1167}
1168
1169/*
1170 * Top level evaluation function, returning a string. If "skip" is TRUE,
1171 * only parsing to "nextcmd" is done, without reporting errors. Return
1172 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1173 */
1174 char_u *
1175eval_to_string_skip(arg, nextcmd, skip)
1176 char_u *arg;
1177 char_u **nextcmd;
1178 int skip; /* only parse, don't execute */
1179{
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 char_u *retval;
1182
1183 if (skip)
1184 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001185 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 retval = NULL;
1187 else
1188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001189 retval = vim_strsave(get_tv_string(&tv));
1190 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 if (skip)
1193 --emsg_skip;
1194
1195 return retval;
1196}
1197
1198/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001199 * Skip over an expression at "*pp".
1200 * Return FAIL for an error, OK otherwise.
1201 */
1202 int
1203skip_expr(pp)
1204 char_u **pp;
1205{
Bram Moolenaar33570922005-01-25 22:26:29 +00001206 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001207
1208 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001210}
1211
1212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 * Top level evaluation function, returning a string.
1214 * Return pointer to allocated memory, or NULL for failure.
1215 */
1216 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001217eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 char_u *arg;
1219 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001220 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
Bram Moolenaar33570922005-01-25 22:26:29 +00001222 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001224 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 retval = NULL;
1228 else
1229 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001230 if (dolist && tv.v_type == VAR_LIST)
1231 {
1232 ga_init2(&ga, (int)sizeof(char), 80);
1233 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1234 ga_append(&ga, NUL);
1235 retval = (char_u *)ga.ga_data;
1236 }
1237 else
1238 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241
1242 return retval;
1243}
1244
1245/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 * Call eval_to_string() without using current local variables and using
1247 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
1249 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001250eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 char_u *arg;
1252 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001253 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255 char_u *retval;
1256 void *save_funccalp;
1257
1258 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001259 if (use_sandbox)
1260 ++sandbox;
1261 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001262 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001263 if (use_sandbox)
1264 --sandbox;
1265 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 restore_funccal(save_funccalp);
1267 return retval;
1268}
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
1271 * Top level evaluation function, returning a number.
1272 * Evaluates "expr" silently.
1273 * Returns -1 for an error.
1274 */
1275 int
1276eval_to_number(expr)
1277 char_u *expr;
1278{
Bram Moolenaar33570922005-01-25 22:26:29 +00001279 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001281 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 ++emsg_off;
1284
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001285 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 retval = -1;
1287 else
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 --emsg_off;
1293
1294 return retval;
1295}
1296
Bram Moolenaara40058a2005-07-11 22:42:07 +00001297/*
1298 * Prepare v: variable "idx" to be used.
1299 * Save the current typeval in "save_tv".
1300 * When not used yet add the variable to the v: hashtable.
1301 */
1302 static void
1303prepare_vimvar(idx, save_tv)
1304 int idx;
1305 typval_T *save_tv;
1306{
1307 *save_tv = vimvars[idx].vv_tv;
1308 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1309 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1310}
1311
1312/*
1313 * Restore v: variable "idx" to typeval "save_tv".
1314 * When no longer defined, remove the variable from the v: hashtable.
1315 */
1316 static void
1317restore_vimvar(idx, save_tv)
1318 int idx;
1319 typval_T *save_tv;
1320{
1321 hashitem_T *hi;
1322
Bram Moolenaara40058a2005-07-11 22:42:07 +00001323 vimvars[idx].vv_tv = *save_tv;
1324 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1325 {
1326 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1327 if (HASHITEM_EMPTY(hi))
1328 EMSG2(_(e_intern2), "restore_vimvar()");
1329 else
1330 hash_remove(&vimvarht, hi);
1331 }
1332}
1333
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001334#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001335/*
1336 * Evaluate an expression to a list with suggestions.
1337 * For the "expr:" part of 'spellsuggest'.
1338 */
1339 list_T *
1340eval_spell_expr(badword, expr)
1341 char_u *badword;
1342 char_u *expr;
1343{
1344 typval_T save_val;
1345 typval_T rettv;
1346 list_T *list = NULL;
1347 char_u *p = skipwhite(expr);
1348
1349 /* Set "v:val" to the bad word. */
1350 prepare_vimvar(VV_VAL, &save_val);
1351 vimvars[VV_VAL].vv_type = VAR_STRING;
1352 vimvars[VV_VAL].vv_str = badword;
1353 if (p_verbose == 0)
1354 ++emsg_off;
1355
1356 if (eval1(&p, &rettv, TRUE) == OK)
1357 {
1358 if (rettv.v_type != VAR_LIST)
1359 clear_tv(&rettv);
1360 else
1361 list = rettv.vval.v_list;
1362 }
1363
1364 if (p_verbose == 0)
1365 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001366 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 Moolenaarafbdeb82008-01-05 21:16:31 +00003411 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 ht = find_var_ht(name, &varname);
3414 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 hi = hash_find(ht, varname);
3417 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003418 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003419 di = HI2DI(hi);
3420 if (var_check_fixed(di->di_flags, name)
3421 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003422 return FAIL;
3423 delete_var(ht, hi);
3424 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003427 if (forceit)
3428 return OK;
3429 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return FAIL;
3431}
3432
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003433/*
3434 * Lock or unlock variable indicated by "lp".
3435 * "deep" is the levels to go (-1 for unlimited);
3436 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3437 */
3438 static int
3439do_lock_var(lp, name_end, deep, lock)
3440 lval_T *lp;
3441 char_u *name_end;
3442 int deep;
3443 int lock;
3444{
3445 int ret = OK;
3446 int cc;
3447 dictitem_T *di;
3448
3449 if (deep == 0) /* nothing to do */
3450 return OK;
3451
3452 if (lp->ll_tv == NULL)
3453 {
3454 cc = *name_end;
3455 *name_end = NUL;
3456
3457 /* Normal name or expanded name. */
3458 if (check_changedtick(lp->ll_name))
3459 ret = FAIL;
3460 else
3461 {
3462 di = find_var(lp->ll_name, NULL);
3463 if (di == NULL)
3464 ret = FAIL;
3465 else
3466 {
3467 if (lock)
3468 di->di_flags |= DI_FLAGS_LOCK;
3469 else
3470 di->di_flags &= ~DI_FLAGS_LOCK;
3471 item_lock(&di->di_tv, deep, lock);
3472 }
3473 }
3474 *name_end = cc;
3475 }
3476 else if (lp->ll_range)
3477 {
3478 listitem_T *li = lp->ll_li;
3479
3480 /* (un)lock a range of List items. */
3481 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3482 {
3483 item_lock(&li->li_tv, deep, lock);
3484 li = li->li_next;
3485 ++lp->ll_n1;
3486 }
3487 }
3488 else if (lp->ll_list != NULL)
3489 /* (un)lock a List item. */
3490 item_lock(&lp->ll_li->li_tv, deep, lock);
3491 else
3492 /* un(lock) a Dictionary item. */
3493 item_lock(&lp->ll_di->di_tv, deep, lock);
3494
3495 return ret;
3496}
3497
3498/*
3499 * Lock or unlock an item. "deep" is nr of levels to go.
3500 */
3501 static void
3502item_lock(tv, deep, lock)
3503 typval_T *tv;
3504 int deep;
3505 int lock;
3506{
3507 static int recurse = 0;
3508 list_T *l;
3509 listitem_T *li;
3510 dict_T *d;
3511 hashitem_T *hi;
3512 int todo;
3513
3514 if (recurse >= DICT_MAXNEST)
3515 {
3516 EMSG(_("E743: variable nested too deep for (un)lock"));
3517 return;
3518 }
3519 if (deep == 0)
3520 return;
3521 ++recurse;
3522
3523 /* lock/unlock the item itself */
3524 if (lock)
3525 tv->v_lock |= VAR_LOCKED;
3526 else
3527 tv->v_lock &= ~VAR_LOCKED;
3528
3529 switch (tv->v_type)
3530 {
3531 case VAR_LIST:
3532 if ((l = tv->vval.v_list) != NULL)
3533 {
3534 if (lock)
3535 l->lv_lock |= VAR_LOCKED;
3536 else
3537 l->lv_lock &= ~VAR_LOCKED;
3538 if (deep < 0 || deep > 1)
3539 /* recursive: lock/unlock the items the List contains */
3540 for (li = l->lv_first; li != NULL; li = li->li_next)
3541 item_lock(&li->li_tv, deep - 1, lock);
3542 }
3543 break;
3544 case VAR_DICT:
3545 if ((d = tv->vval.v_dict) != NULL)
3546 {
3547 if (lock)
3548 d->dv_lock |= VAR_LOCKED;
3549 else
3550 d->dv_lock &= ~VAR_LOCKED;
3551 if (deep < 0 || deep > 1)
3552 {
3553 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003554 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003555 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3556 {
3557 if (!HASHITEM_EMPTY(hi))
3558 {
3559 --todo;
3560 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3561 }
3562 }
3563 }
3564 }
3565 }
3566 --recurse;
3567}
3568
Bram Moolenaara40058a2005-07-11 22:42:07 +00003569/*
3570 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3571 * it refers to a List or Dictionary that is locked.
3572 */
3573 static int
3574tv_islocked(tv)
3575 typval_T *tv;
3576{
3577 return (tv->v_lock & VAR_LOCKED)
3578 || (tv->v_type == VAR_LIST
3579 && tv->vval.v_list != NULL
3580 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3581 || (tv->v_type == VAR_DICT
3582 && tv->vval.v_dict != NULL
3583 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3584}
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3587/*
3588 * Delete all "menutrans_" variables.
3589 */
3590 void
3591del_menutrans_vars()
3592{
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003594 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003597 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003599 {
3600 if (!HASHITEM_EMPTY(hi))
3601 {
3602 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003603 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3604 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003605 }
3606 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003607 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608}
3609#endif
3610
3611#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3612
3613/*
3614 * Local string buffer for the next two functions to store a variable name
3615 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3616 * get_user_var_name().
3617 */
3618
3619static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3620
3621static char_u *varnamebuf = NULL;
3622static int varnamebuflen = 0;
3623
3624/*
3625 * Function to concatenate a prefix and a variable name.
3626 */
3627 static char_u *
3628cat_prefix_varname(prefix, name)
3629 int prefix;
3630 char_u *name;
3631{
3632 int len;
3633
3634 len = (int)STRLEN(name) + 3;
3635 if (len > varnamebuflen)
3636 {
3637 vim_free(varnamebuf);
3638 len += 10; /* some additional space */
3639 varnamebuf = alloc(len);
3640 if (varnamebuf == NULL)
3641 {
3642 varnamebuflen = 0;
3643 return NULL;
3644 }
3645 varnamebuflen = len;
3646 }
3647 *varnamebuf = prefix;
3648 varnamebuf[1] = ':';
3649 STRCPY(varnamebuf + 2, name);
3650 return varnamebuf;
3651}
3652
3653/*
3654 * Function given to ExpandGeneric() to obtain the list of user defined
3655 * (global/buffer/window/built-in) variable names.
3656 */
3657/*ARGSUSED*/
3658 char_u *
3659get_user_var_name(xp, idx)
3660 expand_T *xp;
3661 int idx;
3662{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003663 static long_u gdone;
3664 static long_u bdone;
3665 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003666#ifdef FEAT_WINDOWS
3667 static long_u tdone;
3668#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003669 static int vidx;
3670 static hashitem_T *hi;
3671 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003674 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003676#ifdef FEAT_WINDOWS
3677 tdone = 0;
3678#endif
3679 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003680
3681 /* Global variables */
3682 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003686 else
3687 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003688 while (HASHITEM_EMPTY(hi))
3689 ++hi;
3690 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3691 return cat_prefix_varname('g', hi->hi_key);
3692 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003694
3695 /* b: variables */
3696 ht = &curbuf->b_vars.dv_hashtab;
3697 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003699 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003700 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003701 else
3702 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003703 while (HASHITEM_EMPTY(hi))
3704 ++hi;
3705 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003707 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003709 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 return (char_u *)"b:changedtick";
3711 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003712
3713 /* w: variables */
3714 ht = &curwin->w_vars.dv_hashtab;
3715 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003717 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003719 else
3720 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003721 while (HASHITEM_EMPTY(hi))
3722 ++hi;
3723 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003725
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003726#ifdef FEAT_WINDOWS
3727 /* t: variables */
3728 ht = &curtab->tp_vars.dv_hashtab;
3729 if (tdone < ht->ht_used)
3730 {
3731 if (tdone++ == 0)
3732 hi = ht->ht_array;
3733 else
3734 ++hi;
3735 while (HASHITEM_EMPTY(hi))
3736 ++hi;
3737 return cat_prefix_varname('t', hi->hi_key);
3738 }
3739#endif
3740
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 /* v: variables */
3742 if (vidx < VV_LEN)
3743 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 vim_free(varnamebuf);
3746 varnamebuf = NULL;
3747 varnamebuflen = 0;
3748 return NULL;
3749}
3750
3751#endif /* FEAT_CMDL_COMPL */
3752
3753/*
3754 * types for expressions.
3755 */
3756typedef enum
3757{
3758 TYPE_UNKNOWN = 0
3759 , TYPE_EQUAL /* == */
3760 , TYPE_NEQUAL /* != */
3761 , TYPE_GREATER /* > */
3762 , TYPE_GEQUAL /* >= */
3763 , TYPE_SMALLER /* < */
3764 , TYPE_SEQUAL /* <= */
3765 , TYPE_MATCH /* =~ */
3766 , TYPE_NOMATCH /* !~ */
3767} exptype_T;
3768
3769/*
3770 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3773 */
3774
3775/*
3776 * Handle zero level expression.
3777 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003778 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003779 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * Return OK or FAIL.
3781 */
3782 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003783eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 char_u **nextcmd;
3787 int evaluate;
3788{
3789 int ret;
3790 char_u *p;
3791
3792 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (ret == FAIL || !ends_excmd(*p))
3795 {
3796 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 /*
3799 * Report the invalid expression unless the expression evaluation has
3800 * been cancelled due to an aborting error, an interrupt, or an
3801 * exception.
3802 */
3803 if (!aborting())
3804 EMSG2(_(e_invexpr2), arg);
3805 ret = FAIL;
3806 }
3807 if (nextcmd != NULL)
3808 *nextcmd = check_nextcmd(p);
3809
3810 return ret;
3811}
3812
3813/*
3814 * Handle top level expression:
3815 * expr1 ? expr0 : expr0
3816 *
3817 * "arg" must point to the first non-white of the expression.
3818 * "arg" is advanced to the next non-white after the recognized expression.
3819 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003820 * Note: "rettv.v_lock" is not set.
3821 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 * Return OK or FAIL.
3823 */
3824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 int evaluate;
3829{
3830 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 /*
3834 * Get the first variable.
3835 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 return FAIL;
3838
3839 if ((*arg)[0] == '?')
3840 {
3841 result = FALSE;
3842 if (evaluate)
3843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003844 int error = FALSE;
3845
3846 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003849 if (error)
3850 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853 /*
3854 * Get the second variable.
3855 */
3856 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return FAIL;
3859
3860 /*
3861 * Check for the ":".
3862 */
3863 if ((*arg)[0] != ':')
3864 {
3865 EMSG(_("E109: Missing ':' after '?'"));
3866 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return FAIL;
3869 }
3870
3871 /*
3872 * Get the third variable.
3873 */
3874 *arg = skipwhite(*arg + 1);
3875 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3876 {
3877 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003878 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return FAIL;
3880 }
3881 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
3885 return OK;
3886}
3887
3888/*
3889 * Handle first level expression:
3890 * expr2 || expr2 || expr2 logical OR
3891 *
3892 * "arg" must point to the first non-white of the expression.
3893 * "arg" is advanced to the next non-white after the recognized expression.
3894 *
3895 * Return OK or FAIL.
3896 */
3897 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 int evaluate;
3902{
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 long result;
3905 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003906 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 /*
3909 * Get the first variable.
3910 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 return FAIL;
3913
3914 /*
3915 * Repeat until there is no following "||".
3916 */
3917 first = TRUE;
3918 result = FALSE;
3919 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3920 {
3921 if (evaluate && first)
3922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 if (error)
3927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 first = FALSE;
3929 }
3930
3931 /*
3932 * Get the second variable.
3933 */
3934 *arg = skipwhite(*arg + 2);
3935 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3936 return FAIL;
3937
3938 /*
3939 * Compute the result.
3940 */
3941 if (evaluate && !result)
3942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 if (error)
3947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 if (evaluate)
3950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951 rettv->v_type = VAR_NUMBER;
3952 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 }
3955
3956 return OK;
3957}
3958
3959/*
3960 * Handle second level expression:
3961 * expr3 && expr3 && expr3 logical AND
3962 *
3963 * "arg" must point to the first non-white of the expression.
3964 * "arg" is advanced to the next non-white after the recognized expression.
3965 *
3966 * Return OK or FAIL.
3967 */
3968 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 int evaluate;
3973{
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 long result;
3976 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003977 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 /*
3980 * Get the first variable.
3981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return FAIL;
3984
3985 /*
3986 * Repeat until there is no following "&&".
3987 */
3988 first = TRUE;
3989 result = TRUE;
3990 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3991 {
3992 if (evaluate && first)
3993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 if (error)
3998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 first = FALSE;
4000 }
4001
4002 /*
4003 * Get the second variable.
4004 */
4005 *arg = skipwhite(*arg + 2);
4006 if (eval4(arg, &var2, evaluate && result) == FAIL)
4007 return FAIL;
4008
4009 /*
4010 * Compute the result.
4011 */
4012 if (evaluate && result)
4013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004017 if (error)
4018 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 if (evaluate)
4021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 rettv->v_type = VAR_NUMBER;
4023 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025 }
4026
4027 return OK;
4028}
4029
4030/*
4031 * Handle third level expression:
4032 * var1 == var2
4033 * var1 =~ var2
4034 * var1 != var2
4035 * var1 !~ var2
4036 * var1 > var2
4037 * var1 >= var2
4038 * var1 < var2
4039 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 * var1 is var2
4041 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 *
4043 * "arg" must point to the first non-white of the expression.
4044 * "arg" is advanced to the next non-white after the recognized expression.
4045 *
4046 * Return OK or FAIL.
4047 */
4048 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 int evaluate;
4053{
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_u *p;
4056 int i;
4057 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004058 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 int len = 2;
4060 long n1, n2;
4061 char_u *s1, *s2;
4062 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4063 regmatch_T regmatch;
4064 int ic;
4065 char_u *save_cpo;
4066
4067 /*
4068 * Get the first variable.
4069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return FAIL;
4072
4073 p = *arg;
4074 switch (p[0])
4075 {
4076 case '=': if (p[1] == '=')
4077 type = TYPE_EQUAL;
4078 else if (p[1] == '~')
4079 type = TYPE_MATCH;
4080 break;
4081 case '!': if (p[1] == '=')
4082 type = TYPE_NEQUAL;
4083 else if (p[1] == '~')
4084 type = TYPE_NOMATCH;
4085 break;
4086 case '>': if (p[1] != '=')
4087 {
4088 type = TYPE_GREATER;
4089 len = 1;
4090 }
4091 else
4092 type = TYPE_GEQUAL;
4093 break;
4094 case '<': if (p[1] != '=')
4095 {
4096 type = TYPE_SMALLER;
4097 len = 1;
4098 }
4099 else
4100 type = TYPE_SEQUAL;
4101 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004102 case 'i': if (p[1] == 's')
4103 {
4104 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4105 len = 5;
4106 if (!vim_isIDc(p[len]))
4107 {
4108 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4109 type_is = TRUE;
4110 }
4111 }
4112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 /*
4116 * If there is a comparitive operator, use it.
4117 */
4118 if (type != TYPE_UNKNOWN)
4119 {
4120 /* extra question mark appended: ignore case */
4121 if (p[len] == '?')
4122 {
4123 ic = TRUE;
4124 ++len;
4125 }
4126 /* extra '#' appended: match case */
4127 else if (p[len] == '#')
4128 {
4129 ic = FALSE;
4130 ++len;
4131 }
4132 /* nothing appened: use 'ignorecase' */
4133 else
4134 ic = p_ic;
4135
4136 /*
4137 * Get the second variable.
4138 */
4139 *arg = skipwhite(p + len);
4140 if (eval5(arg, &var2, evaluate) == FAIL)
4141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return FAIL;
4144 }
4145
4146 if (evaluate)
4147 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004148 if (type_is && rettv->v_type != var2.v_type)
4149 {
4150 /* For "is" a different type always means FALSE, for "notis"
4151 * it means TRUE. */
4152 n1 = (type == TYPE_NEQUAL);
4153 }
4154 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4155 {
4156 if (type_is)
4157 {
4158 n1 = (rettv->v_type == var2.v_type
4159 && rettv->vval.v_list == var2.vval.v_list);
4160 if (type == TYPE_NEQUAL)
4161 n1 = !n1;
4162 }
4163 else if (rettv->v_type != var2.v_type
4164 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4165 {
4166 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004167 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004169 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004170 clear_tv(rettv);
4171 clear_tv(&var2);
4172 return FAIL;
4173 }
4174 else
4175 {
4176 /* Compare two Lists for being equal or unequal. */
4177 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4178 if (type == TYPE_NEQUAL)
4179 n1 = !n1;
4180 }
4181 }
4182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004183 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4184 {
4185 if (type_is)
4186 {
4187 n1 = (rettv->v_type == var2.v_type
4188 && rettv->vval.v_dict == var2.vval.v_dict);
4189 if (type == TYPE_NEQUAL)
4190 n1 = !n1;
4191 }
4192 else if (rettv->v_type != var2.v_type
4193 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4194 {
4195 if (rettv->v_type != var2.v_type)
4196 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4197 else
4198 EMSG(_("E736: Invalid operation for Dictionary"));
4199 clear_tv(rettv);
4200 clear_tv(&var2);
4201 return FAIL;
4202 }
4203 else
4204 {
4205 /* Compare two Dictionaries for being equal or unequal. */
4206 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4207 if (type == TYPE_NEQUAL)
4208 n1 = !n1;
4209 }
4210 }
4211
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004212 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4213 {
4214 if (rettv->v_type != var2.v_type
4215 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4216 {
4217 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004218 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004220 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004221 clear_tv(rettv);
4222 clear_tv(&var2);
4223 return FAIL;
4224 }
4225 else
4226 {
4227 /* Compare two Funcrefs for being equal or unequal. */
4228 if (rettv->vval.v_string == NULL
4229 || var2.vval.v_string == NULL)
4230 n1 = FALSE;
4231 else
4232 n1 = STRCMP(rettv->vval.v_string,
4233 var2.vval.v_string) == 0;
4234 if (type == TYPE_NEQUAL)
4235 n1 = !n1;
4236 }
4237 }
4238
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 /*
4240 * If one of the two variables is a number, compare as a number.
4241 * When using "=~" or "!~", always compare as string.
4242 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004243 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 n1 = get_tv_number(rettv);
4247 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 switch (type)
4249 {
4250 case TYPE_EQUAL: n1 = (n1 == n2); break;
4251 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4252 case TYPE_GREATER: n1 = (n1 > n2); break;
4253 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4254 case TYPE_SMALLER: n1 = (n1 < n2); break;
4255 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4256 case TYPE_UNKNOWN:
4257 case TYPE_MATCH:
4258 case TYPE_NOMATCH: break; /* avoid gcc warning */
4259 }
4260 }
4261 else
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 s1 = get_tv_string_buf(rettv, buf1);
4264 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4266 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4267 else
4268 i = 0;
4269 n1 = FALSE;
4270 switch (type)
4271 {
4272 case TYPE_EQUAL: n1 = (i == 0); break;
4273 case TYPE_NEQUAL: n1 = (i != 0); break;
4274 case TYPE_GREATER: n1 = (i > 0); break;
4275 case TYPE_GEQUAL: n1 = (i >= 0); break;
4276 case TYPE_SMALLER: n1 = (i < 0); break;
4277 case TYPE_SEQUAL: n1 = (i <= 0); break;
4278
4279 case TYPE_MATCH:
4280 case TYPE_NOMATCH:
4281 /* avoid 'l' flag in 'cpoptions' */
4282 save_cpo = p_cpo;
4283 p_cpo = (char_u *)"";
4284 regmatch.regprog = vim_regcomp(s2,
4285 RE_MAGIC + RE_STRING);
4286 regmatch.rm_ic = ic;
4287 if (regmatch.regprog != NULL)
4288 {
4289 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4290 vim_free(regmatch.regprog);
4291 if (type == TYPE_NOMATCH)
4292 n1 = !n1;
4293 }
4294 p_cpo = save_cpo;
4295 break;
4296
4297 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4298 }
4299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 clear_tv(rettv);
4301 clear_tv(&var2);
4302 rettv->v_type = VAR_NUMBER;
4303 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306
4307 return OK;
4308}
4309
4310/*
4311 * Handle fourth level expression:
4312 * + number addition
4313 * - number subtraction
4314 * . string concatenation
4315 *
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4318 *
4319 * Return OK or FAIL.
4320 */
4321 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 int evaluate;
4326{
Bram Moolenaar33570922005-01-25 22:26:29 +00004327 typval_T var2;
4328 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int op;
4330 long n1, n2;
4331 char_u *s1, *s2;
4332 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4333 char_u *p;
4334
4335 /*
4336 * Get the first variable.
4337 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 return FAIL;
4340
4341 /*
4342 * Repeat computing, until no '+', '-' or '.' is following.
4343 */
4344 for (;;)
4345 {
4346 op = **arg;
4347 if (op != '+' && op != '-' && op != '.')
4348 break;
4349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (op != '+' || rettv->v_type != VAR_LIST)
4351 {
4352 /* For "list + ...", an illegal use of the first operand as
4353 * a number cannot be determined before evaluating the 2nd
4354 * operand: if this is also a list, all is ok.
4355 * For "something . ...", "something - ..." or "non-list + ...",
4356 * we know that the first operand needs to be a string or number
4357 * without evaluating the 2nd operand. So check before to avoid
4358 * side effects after an error. */
4359 if (evaluate && get_tv_string_chk(rettv) == NULL)
4360 {
4361 clear_tv(rettv);
4362 return FAIL;
4363 }
4364 }
4365
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 /*
4367 * Get the second variable.
4368 */
4369 *arg = skipwhite(*arg + 1);
4370 if (eval6(arg, &var2, evaluate) == FAIL)
4371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 return FAIL;
4374 }
4375
4376 if (evaluate)
4377 {
4378 /*
4379 * Compute the result.
4380 */
4381 if (op == '.')
4382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4384 s2 = get_tv_string_buf_chk(&var2, buf2);
4385 if (s2 == NULL) /* type error ? */
4386 {
4387 clear_tv(rettv);
4388 clear_tv(&var2);
4389 return FAIL;
4390 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004391 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 clear_tv(rettv);
4393 rettv->v_type = VAR_STRING;
4394 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004396 else if (op == '+' && rettv->v_type == VAR_LIST
4397 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 {
4399 /* concatenate Lists */
4400 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4401 &var3) == FAIL)
4402 {
4403 clear_tv(rettv);
4404 clear_tv(&var2);
4405 return FAIL;
4406 }
4407 clear_tv(rettv);
4408 *rettv = var3;
4409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 else
4411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 int error = FALSE;
4413
4414 n1 = get_tv_number_chk(rettv, &error);
4415 if (error)
4416 {
4417 /* This can only happen for "list + non-list".
4418 * For "non-list + ..." or "something - ...", we returned
4419 * before evaluating the 2nd operand. */
4420 clear_tv(rettv);
4421 return FAIL;
4422 }
4423 n2 = get_tv_number_chk(&var2, &error);
4424 if (error)
4425 {
4426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (op == '+')
4432 n1 = n1 + n2;
4433 else
4434 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_NUMBER;
4436 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 }
4441 return OK;
4442}
4443
4444/*
4445 * Handle fifth level expression:
4446 * * number multiplication
4447 * / number division
4448 * % number modulo
4449 *
4450 * "arg" must point to the first non-white of the expression.
4451 * "arg" is advanced to the next non-white after the recognized expression.
4452 *
4453 * Return OK or FAIL.
4454 */
4455 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int evaluate;
4460{
Bram Moolenaar33570922005-01-25 22:26:29 +00004461 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 int op;
4463 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004464 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 /*
4467 * Get the first variable.
4468 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return FAIL;
4471
4472 /*
4473 * Repeat computing, until no '*', '/' or '%' is following.
4474 */
4475 for (;;)
4476 {
4477 op = **arg;
4478 if (op != '*' && op != '/' && op != '%')
4479 break;
4480
4481 if (evaluate)
4482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485 if (error)
4486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 else
4489 n1 = 0;
4490
4491 /*
4492 * Get the second variable.
4493 */
4494 *arg = skipwhite(*arg + 1);
4495 if (eval7(arg, &var2, evaluate) == FAIL)
4496 return FAIL;
4497
4498 if (evaluate)
4499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004501 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 if (error)
4503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /*
4506 * Compute the result.
4507 */
4508 if (op == '*')
4509 n1 = n1 * n2;
4510 else if (op == '/')
4511 {
4512 if (n2 == 0) /* give an error message? */
4513 n1 = 0x7fffffffL;
4514 else
4515 n1 = n1 / n2;
4516 }
4517 else
4518 {
4519 if (n2 == 0) /* give an error message? */
4520 n1 = 0;
4521 else
4522 n1 = n1 % n2;
4523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 rettv->v_type = VAR_NUMBER;
4525 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 }
4528
4529 return OK;
4530}
4531
4532/*
4533 * Handle sixth level expression:
4534 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004535 * "string" string constant
4536 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * &option-name option value
4538 * @r register contents
4539 * identifier variable value
4540 * function() function call
4541 * $VAR environment variable
4542 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004543 * [expr, expr] List
4544 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 *
4546 * Also handle:
4547 * ! in front logical NOT
4548 * - in front unary minus
4549 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 * trailing [] subscript in String or List
4551 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 *
4553 * "arg" must point to the first non-white of the expression.
4554 * "arg" is advanced to the next non-white after the recognized expression.
4555 *
4556 * Return OK or FAIL.
4557 */
4558 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 int evaluate;
4563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 long n;
4565 int len;
4566 char_u *s;
4567 int val;
4568 char_u *start_leader, *end_leader;
4569 int ret = OK;
4570 char_u *alias;
4571
4572 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 /*
4579 * Skip '!' and '-' characters. They are handled later.
4580 */
4581 start_leader = *arg;
4582 while (**arg == '!' || **arg == '-' || **arg == '+')
4583 *arg = skipwhite(*arg + 1);
4584 end_leader = *arg;
4585
4586 switch (**arg)
4587 {
4588 /*
4589 * Number constant.
4590 */
4591 case '0':
4592 case '1':
4593 case '2':
4594 case '3':
4595 case '4':
4596 case '5':
4597 case '6':
4598 case '7':
4599 case '8':
4600 case '9':
4601 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4602 *arg += len;
4603 if (evaluate)
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 rettv->v_type = VAR_NUMBER;
4606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 break;
4609
4610 /*
4611 * String constant: "string".
4612 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 break;
4615
4616 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 break;
4621
4622 /*
4623 * List: [expr, expr]
4624 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
4627
4628 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 * Dictionary: {key: val, key: val}
4630 */
4631 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4632 break;
4633
4634 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004635 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004637 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 break;
4639
4640 /*
4641 * Environment variable: $VAR.
4642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 break;
4645
4646 /*
4647 * Register contents: @r.
4648 */
4649 case '@': ++*arg;
4650 if (evaluate)
4651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004653 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 if (**arg != NUL)
4656 ++*arg;
4657 break;
4658
4659 /*
4660 * nested expression: (expression).
4661 */
4662 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (**arg == ')')
4665 ++*arg;
4666 else if (ret == OK)
4667 {
4668 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 ret = FAIL;
4671 }
4672 break;
4673
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 break;
4676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677
4678 if (ret == NOTDONE)
4679 {
4680 /*
4681 * Must be a variable or function name.
4682 * Can also be a curly-braces kind of name: {expr}.
4683 */
4684 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004685 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004686 if (alias != NULL)
4687 s = alias;
4688
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004689 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 ret = FAIL;
4691 else
4692 {
4693 if (**arg == '(') /* recursive! */
4694 {
4695 /* If "s" is the name of a variable of type VAR_FUNC
4696 * use its contents. */
4697 s = deref_func_name(s, &len);
4698
4699 /* Invoke the function. */
4700 ret = get_func_tv(s, len, rettv, arg,
4701 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004702 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703 /* Stop the expression evaluation when immediately
4704 * aborting on error, or when an interrupt occurred or
4705 * an exception was thrown but not caught. */
4706 if (aborting())
4707 {
4708 if (ret == OK)
4709 clear_tv(rettv);
4710 ret = FAIL;
4711 }
4712 }
4713 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004714 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004715 else
4716 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 }
4718
4719 if (alias != NULL)
4720 vim_free(alias);
4721 }
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 *arg = skipwhite(*arg);
4724
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004725 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4726 * expr(expr). */
4727 if (ret == OK)
4728 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 /*
4731 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4732 */
4733 if (ret == OK && evaluate && end_leader > start_leader)
4734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004735 int error = FALSE;
4736
4737 val = get_tv_number_chk(rettv, &error);
4738 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 clear_tv(rettv);
4741 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004743 else
4744 {
4745 while (end_leader > start_leader)
4746 {
4747 --end_leader;
4748 if (*end_leader == '!')
4749 val = !val;
4750 else if (*end_leader == '-')
4751 val = -val;
4752 }
4753 clear_tv(rettv);
4754 rettv->v_type = VAR_NUMBER;
4755 rettv->vval.v_number = val;
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758
4759 return ret;
4760}
4761
4762/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004763 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4764 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4766 */
4767 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004770 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773{
4774 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 long len = -1;
4778 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004784 if (verbose)
4785 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786 return FAIL;
4787 }
4788
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 /*
4792 * dict.name
4793 */
4794 key = *arg + 1;
4795 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4796 ;
4797 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 }
4801 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 /*
4804 * something[idx]
4805 *
4806 * Get the (first) variable from inside the [].
4807 */
4808 *arg = skipwhite(*arg + 1);
4809 if (**arg == ':')
4810 empty1 = TRUE;
4811 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4812 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4814 {
4815 /* not a number or string */
4816 clear_tv(&var1);
4817 return FAIL;
4818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819
4820 /*
4821 * Get the second variable from inside the [:].
4822 */
4823 if (**arg == ':')
4824 {
4825 range = TRUE;
4826 *arg = skipwhite(*arg + 1);
4827 if (**arg == ']')
4828 empty2 = TRUE;
4829 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 if (!empty1)
4832 clear_tv(&var1);
4833 return FAIL;
4834 }
4835 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4836 {
4837 /* not a number or string */
4838 if (!empty1)
4839 clear_tv(&var1);
4840 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 return FAIL;
4842 }
4843 }
4844
4845 /* Check for the ']'. */
4846 if (**arg != ']')
4847 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004848 if (verbose)
4849 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004850 clear_tv(&var1);
4851 if (range)
4852 clear_tv(&var2);
4853 return FAIL;
4854 }
4855 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 }
4857
4858 if (evaluate)
4859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 n1 = 0;
4861 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 n1 = get_tv_number(&var1);
4864 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 }
4866 if (range)
4867 {
4868 if (empty2)
4869 n2 = -1;
4870 else
4871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 n2 = get_tv_number(&var2);
4873 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874 }
4875 }
4876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 {
4879 case VAR_NUMBER:
4880 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 len = (long)STRLEN(s);
4883 if (range)
4884 {
4885 /* The resulting variable is a substring. If the indexes
4886 * are out of range the result is empty. */
4887 if (n1 < 0)
4888 {
4889 n1 = len + n1;
4890 if (n1 < 0)
4891 n1 = 0;
4892 }
4893 if (n2 < 0)
4894 n2 = len + n2;
4895 else if (n2 >= len)
4896 n2 = len;
4897 if (n1 >= len || n2 < 0 || n1 > n2)
4898 s = NULL;
4899 else
4900 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4901 }
4902 else
4903 {
4904 /* The resulting variable is a string of a single
4905 * character. If the index is too big or negative the
4906 * result is empty. */
4907 if (n1 >= len || n1 < 0)
4908 s = NULL;
4909 else
4910 s = vim_strnsave(s + n1, 1);
4911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 clear_tv(rettv);
4913 rettv->v_type = VAR_STRING;
4914 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004915 break;
4916
4917 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 if (n1 < 0)
4920 n1 = len + n1;
4921 if (!empty1 && (n1 < 0 || n1 >= len))
4922 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004923 /* For a range we allow invalid values and return an empty
4924 * list. A list index out of range is an error. */
4925 if (!range)
4926 {
4927 if (verbose)
4928 EMSGN(_(e_listidx), n1);
4929 return FAIL;
4930 }
4931 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932 }
4933 if (range)
4934 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004935 list_T *l;
4936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937
4938 if (n2 < 0)
4939 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004940 else if (n2 >= len)
4941 n2 = len - 1;
4942 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004943 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004944 l = list_alloc();
4945 if (l == NULL)
4946 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 n1 <= n2; ++n1)
4949 {
4950 if (list_append_tv(l, &item->li_tv) == FAIL)
4951 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004952 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004953 return FAIL;
4954 }
4955 item = item->li_next;
4956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 clear_tv(rettv);
4958 rettv->v_type = VAR_LIST;
4959 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004960 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004961 }
4962 else
4963 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004964 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 clear_tv(rettv);
4966 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 }
4968 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969
4970 case VAR_DICT:
4971 if (range)
4972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004973 if (verbose)
4974 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 if (len == -1)
4976 clear_tv(&var1);
4977 return FAIL;
4978 }
4979 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004980 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981
4982 if (len == -1)
4983 {
4984 key = get_tv_string(&var1);
4985 if (*key == NUL)
4986 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004987 if (verbose)
4988 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 clear_tv(&var1);
4990 return FAIL;
4991 }
4992 }
4993
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004994 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004996 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004997 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 if (len == -1)
4999 clear_tv(&var1);
5000 if (item == NULL)
5001 return FAIL;
5002
5003 copy_tv(&item->di_tv, &var1);
5004 clear_tv(rettv);
5005 *rettv = var1;
5006 }
5007 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 }
5009 }
5010
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 return OK;
5012}
5013
5014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * Get an option value.
5016 * "arg" points to the '&' or '+' before the option name.
5017 * "arg" is advanced to character after the option name.
5018 * Return OK or FAIL.
5019 */
5020 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005023 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 int evaluate;
5025{
5026 char_u *option_end;
5027 long numval;
5028 char_u *stringval;
5029 int opt_type;
5030 int c;
5031 int working = (**arg == '+'); /* has("+option") */
5032 int ret = OK;
5033 int opt_flags;
5034
5035 /*
5036 * Isolate the option name and find its value.
5037 */
5038 option_end = find_option_end(arg, &opt_flags);
5039 if (option_end == NULL)
5040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 EMSG2(_("E112: Option name missing: %s"), *arg);
5043 return FAIL;
5044 }
5045
5046 if (!evaluate)
5047 {
5048 *arg = option_end;
5049 return OK;
5050 }
5051
5052 c = *option_end;
5053 *option_end = NUL;
5054 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
5057 if (opt_type == -3) /* invalid name */
5058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 EMSG2(_("E113: Unknown option: %s"), *arg);
5061 ret = FAIL;
5062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 if (opt_type == -2) /* hidden string option */
5066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 rettv->v_type = VAR_STRING;
5068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 else if (opt_type == -1) /* hidden number option */
5071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 rettv->v_type = VAR_NUMBER;
5073 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 else if (opt_type == 1) /* number option */
5076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else /* string option */
5081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 rettv->v_type = VAR_STRING;
5083 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085 }
5086 else if (working && (opt_type == -2 || opt_type == -1))
5087 ret = FAIL;
5088
5089 *option_end = c; /* put back for error messages */
5090 *arg = option_end;
5091
5092 return ret;
5093}
5094
5095/*
5096 * Allocate a variable for a string constant.
5097 * Return OK or FAIL.
5098 */
5099 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 int evaluate;
5104{
5105 char_u *p;
5106 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 int extra = 0;
5108
5109 /*
5110 * Find the end of the string, skipping backslashed characters.
5111 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 if (*p == '\\' && p[1] != NUL)
5115 {
5116 ++p;
5117 /* A "\<x>" form occupies at least 4 characters, and produces up
5118 * to 6 characters: reserve space for 2 extra */
5119 if (*p == '<')
5120 extra += 2;
5121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123
5124 if (*p != '"')
5125 {
5126 EMSG2(_("E114: Missing quote: %s"), *arg);
5127 return FAIL;
5128 }
5129
5130 /* If only parsing, set *arg and return here */
5131 if (!evaluate)
5132 {
5133 *arg = p + 1;
5134 return OK;
5135 }
5136
5137 /*
5138 * Copy the string into allocated memory, handling backslashed
5139 * characters.
5140 */
5141 name = alloc((unsigned)(p - *arg + extra));
5142 if (name == NULL)
5143 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 rettv->v_type = VAR_STRING;
5145 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (*p == '\\')
5150 {
5151 switch (*++p)
5152 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 case 'b': *name++ = BS; ++p; break;
5154 case 'e': *name++ = ESC; ++p; break;
5155 case 'f': *name++ = FF; ++p; break;
5156 case 'n': *name++ = NL; ++p; break;
5157 case 'r': *name++ = CAR; ++p; break;
5158 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 case 'X': /* hex: "\x1", "\x12" */
5161 case 'x':
5162 case 'u': /* Unicode: "\u0023" */
5163 case 'U':
5164 if (vim_isxdigit(p[1]))
5165 {
5166 int n, nr;
5167 int c = toupper(*p);
5168
5169 if (c == 'X')
5170 n = 2;
5171 else
5172 n = 4;
5173 nr = 0;
5174 while (--n >= 0 && vim_isxdigit(p[1]))
5175 {
5176 ++p;
5177 nr = (nr << 4) + hex2nr(*p);
5178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_MBYTE
5181 /* For "\u" store the number according to
5182 * 'encoding'. */
5183 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 else
5186#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 break;
5190
5191 /* octal: "\1", "\12", "\123" */
5192 case '0':
5193 case '1':
5194 case '2':
5195 case '3':
5196 case '4':
5197 case '5':
5198 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 case '7': *name = *p++ - '0';
5200 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 *name = (*name << 3) + *p++ - '0';
5203 if (*p >= '0' && *p <= '7')
5204 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208
5209 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (extra != 0)
5212 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
5216 /* FALLTHROUGH */
5217
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 break;
5220 }
5221 }
5222 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 *arg = p + 1;
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 return OK;
5230}
5231
5232/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 * Return OK or FAIL.
5235 */
5236 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 int evaluate;
5241{
5242 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 int reduce = 0;
5245
5246 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005248 */
5249 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005254 break;
5255 ++reduce;
5256 ++p;
5257 }
5258 }
5259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005263 return FAIL;
5264 }
5265
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005267 if (!evaluate)
5268 {
5269 *arg = p + 1;
5270 return OK;
5271 }
5272
5273 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005275 */
5276 str = alloc((unsigned)((p - *arg) - reduce));
5277 if (str == NULL)
5278 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 rettv->v_type = VAR_STRING;
5280 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005281
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005287 break;
5288 ++p;
5289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005293 *arg = p + 1;
5294
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005295 return OK;
5296}
5297
5298/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 * Allocate a variable for a List and fill it from "*arg".
5300 * Return OK or FAIL.
5301 */
5302 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 int evaluate;
5307{
Bram Moolenaar33570922005-01-25 22:26:29 +00005308 list_T *l = NULL;
5309 typval_T tv;
5310 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311
5312 if (evaluate)
5313 {
5314 l = list_alloc();
5315 if (l == NULL)
5316 return FAIL;
5317 }
5318
5319 *arg = skipwhite(*arg + 1);
5320 while (**arg != ']' && **arg != NUL)
5321 {
5322 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5323 goto failret;
5324 if (evaluate)
5325 {
5326 item = listitem_alloc();
5327 if (item != NULL)
5328 {
5329 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005330 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 list_append(l, item);
5332 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005333 else
5334 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 }
5336
5337 if (**arg == ']')
5338 break;
5339 if (**arg != ',')
5340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 goto failret;
5343 }
5344 *arg = skipwhite(*arg + 1);
5345 }
5346
5347 if (**arg != ']')
5348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350failret:
5351 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005352 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 return FAIL;
5354 }
5355
5356 *arg = skipwhite(*arg + 1);
5357 if (evaluate)
5358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005359 rettv->v_type = VAR_LIST;
5360 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 ++l->lv_refcount;
5362 }
5363
5364 return OK;
5365}
5366
5367/*
5368 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005369 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005371 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372list_alloc()
5373{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374 list_T *l;
5375
5376 l = (list_T *)alloc_clear(sizeof(list_T));
5377 if (l != NULL)
5378 {
5379 /* Prepend the list to the list of lists for garbage collection. */
5380 if (first_list != NULL)
5381 first_list->lv_used_prev = l;
5382 l->lv_used_prev = NULL;
5383 l->lv_used_next = first_list;
5384 first_list = l;
5385 }
5386 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387}
5388
5389/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005390 * Allocate an empty list for a return value.
5391 * Returns OK or FAIL.
5392 */
5393 static int
5394rettv_list_alloc(rettv)
5395 typval_T *rettv;
5396{
5397 list_T *l = list_alloc();
5398
5399 if (l == NULL)
5400 return FAIL;
5401
5402 rettv->vval.v_list = l;
5403 rettv->v_type = VAR_LIST;
5404 ++l->lv_refcount;
5405 return OK;
5406}
5407
5408/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 * Unreference a list: decrement the reference count and free it when it
5410 * becomes zero.
5411 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005412 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005414 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005416 if (l != NULL && --l->lv_refcount <= 0)
5417 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418}
5419
5420/*
5421 * Free a list, including all items it points to.
5422 * Ignores the reference count.
5423 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005424 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005425list_free(l, recurse)
5426 list_T *l;
5427 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428{
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005431 /* Remove the list from the list of lists for garbage collection. */
5432 if (l->lv_used_prev == NULL)
5433 first_list = l->lv_used_next;
5434 else
5435 l->lv_used_prev->lv_used_next = l->lv_used_next;
5436 if (l->lv_used_next != NULL)
5437 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5438
Bram Moolenaard9fba312005-06-26 22:34:35 +00005439 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005441 /* Remove the item before deleting it. */
5442 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005443 if (recurse || (item->li_tv.v_type != VAR_LIST
5444 && item->li_tv.v_type != VAR_DICT))
5445 clear_tv(&item->li_tv);
5446 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 }
5448 vim_free(l);
5449}
5450
5451/*
5452 * Allocate a list item.
5453 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455listitem_alloc()
5456{
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458}
5459
5460/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005461 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 */
5463 static void
5464listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 vim_free(item);
5469}
5470
5471/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005472 * Remove a list item from a List and free it. Also clears the value.
5473 */
5474 static void
5475listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 list_T *l;
5477 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005478{
5479 list_remove(l, item, item);
5480 listitem_free(item);
5481}
5482
5483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 * Get the number of items in a list.
5485 */
5486 static long
5487list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005488 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 if (l == NULL)
5491 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005492 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493}
5494
5495/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005496 * Return TRUE when two lists have exactly the same values.
5497 */
5498 static int
5499list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 list_T *l1;
5501 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005502 int ic; /* ignore case for strings */
5503{
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005505
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005506 if (l1 == l2)
5507 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005508 if (list_len(l1) != list_len(l2))
5509 return FALSE;
5510
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005511 for (item1 = l1->lv_first, item2 = l2->lv_first;
5512 item1 != NULL && item2 != NULL;
5513 item1 = item1->li_next, item2 = item2->li_next)
5514 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5515 return FALSE;
5516 return item1 == NULL && item2 == NULL;
5517}
5518
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005519#if defined(FEAT_PYTHON) || defined(PROTO)
5520/*
5521 * Return the dictitem that an entry in a hashtable points to.
5522 */
5523 dictitem_T *
5524dict_lookup(hi)
5525 hashitem_T *hi;
5526{
5527 return HI2DI(hi);
5528}
5529#endif
5530
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005531/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005532 * Return TRUE when two dictionaries have exactly the same key/values.
5533 */
5534 static int
5535dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005536 dict_T *d1;
5537 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005538 int ic; /* ignore case for strings */
5539{
Bram Moolenaar33570922005-01-25 22:26:29 +00005540 hashitem_T *hi;
5541 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005542 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005544 if (d1 == d2)
5545 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005546 if (dict_len(d1) != dict_len(d2))
5547 return FALSE;
5548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005549 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005552 if (!HASHITEM_EMPTY(hi))
5553 {
5554 item2 = dict_find(d2, hi->hi_key, -1);
5555 if (item2 == NULL)
5556 return FALSE;
5557 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5558 return FALSE;
5559 --todo;
5560 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005561 }
5562 return TRUE;
5563}
5564
5565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005566 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005567 * Compares the items just like "==" would compare them, but strings and
5568 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005569 */
5570 static int
5571tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 typval_T *tv1;
5573 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005574 int ic; /* ignore case */
5575{
5576 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005578 static int recursive = 0; /* cach recursive loops */
5579 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005580
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005581 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005582 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005583 /* Catch lists and dicts that have an endless loop by limiting
5584 * recursiveness to 1000. We guess they are equal then. */
5585 if (recursive >= 1000)
5586 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005587
5588 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005590 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005591 ++recursive;
5592 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5593 --recursive;
5594 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005595
5596 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005597 ++recursive;
5598 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5599 --recursive;
5600 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005601
5602 case VAR_FUNC:
5603 return (tv1->vval.v_string != NULL
5604 && tv2->vval.v_string != NULL
5605 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5606
5607 case VAR_NUMBER:
5608 return tv1->vval.v_number == tv2->vval.v_number;
5609
5610 case VAR_STRING:
5611 s1 = get_tv_string_buf(tv1, buf1);
5612 s2 = get_tv_string_buf(tv2, buf2);
5613 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005615
5616 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005617 return TRUE;
5618}
5619
5620/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 * Locate item with index "n" in list "l" and return it.
5622 * A negative index is counted from the end; -1 is the last item.
5623 * Returns NULL when "n" is out of range.
5624 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 long n;
5629{
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 long idx;
5632
5633 if (l == NULL)
5634 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635
5636 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005638 n = l->lv_len + n;
5639
5640 /* Check for index out of range. */
5641 if (n < 0 || n >= l->lv_len)
5642 return NULL;
5643
5644 /* When there is a cached index may start search from there. */
5645 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005647 if (n < l->lv_idx / 2)
5648 {
5649 /* closest to the start of the list */
5650 item = l->lv_first;
5651 idx = 0;
5652 }
5653 else if (n > (l->lv_idx + l->lv_len) / 2)
5654 {
5655 /* closest to the end of the list */
5656 item = l->lv_last;
5657 idx = l->lv_len - 1;
5658 }
5659 else
5660 {
5661 /* closest to the cached index */
5662 item = l->lv_idx_item;
5663 idx = l->lv_idx;
5664 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 }
5666 else
5667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005668 if (n < l->lv_len / 2)
5669 {
5670 /* closest to the start of the list */
5671 item = l->lv_first;
5672 idx = 0;
5673 }
5674 else
5675 {
5676 /* closest to the end of the list */
5677 item = l->lv_last;
5678 idx = l->lv_len - 1;
5679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005681
5682 while (n > idx)
5683 {
5684 /* search forward */
5685 item = item->li_next;
5686 ++idx;
5687 }
5688 while (n < idx)
5689 {
5690 /* search backward */
5691 item = item->li_prev;
5692 --idx;
5693 }
5694
5695 /* cache the used index */
5696 l->lv_idx = idx;
5697 l->lv_idx_item = item;
5698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 return item;
5700}
5701
5702/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005703 * Get list item "l[idx]" as a number.
5704 */
5705 static long
5706list_find_nr(l, idx, errorp)
5707 list_T *l;
5708 long idx;
5709 int *errorp; /* set to TRUE when something wrong */
5710{
5711 listitem_T *li;
5712
5713 li = list_find(l, idx);
5714 if (li == NULL)
5715 {
5716 if (errorp != NULL)
5717 *errorp = TRUE;
5718 return -1L;
5719 }
5720 return get_tv_number_chk(&li->li_tv, errorp);
5721}
5722
5723/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005724 * Locate "item" list "l" and return its index.
5725 * Returns -1 when "item" is not in the list.
5726 */
5727 static long
5728list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *l;
5730 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005731{
5732 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005734
5735 if (l == NULL)
5736 return -1;
5737 idx = 0;
5738 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5739 ++idx;
5740 if (li == NULL)
5741 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005742 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005743}
5744
5745/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 * Append item "item" to the end of list "l".
5747 */
5748 static void
5749list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005750 list_T *l;
5751 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752{
5753 if (l->lv_last == NULL)
5754 {
5755 /* empty list */
5756 l->lv_first = item;
5757 l->lv_last = item;
5758 item->li_prev = NULL;
5759 }
5760 else
5761 {
5762 l->lv_last->li_next = item;
5763 item->li_prev = l->lv_last;
5764 l->lv_last = item;
5765 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005766 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 item->li_next = NULL;
5768}
5769
5770/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005771 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005772 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 */
5774 static int
5775list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 list_T *l;
5777 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005779 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780
Bram Moolenaar05159a02005-02-26 23:04:13 +00005781 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005783 copy_tv(tv, &li->li_tv);
5784 list_append(l, li);
5785 return OK;
5786}
5787
5788/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005789 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005790 * Return FAIL when out of memory.
5791 */
5792 int
5793list_append_dict(list, dict)
5794 list_T *list;
5795 dict_T *dict;
5796{
5797 listitem_T *li = listitem_alloc();
5798
5799 if (li == NULL)
5800 return FAIL;
5801 li->li_tv.v_type = VAR_DICT;
5802 li->li_tv.v_lock = 0;
5803 li->li_tv.vval.v_dict = dict;
5804 list_append(list, li);
5805 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 return OK;
5807}
5808
5809/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005810 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005811 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005812 * Returns FAIL when out of memory.
5813 */
5814 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005815list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005816 list_T *l;
5817 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005818 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005819{
5820 listitem_T *li = listitem_alloc();
5821
5822 if (li == NULL)
5823 return FAIL;
5824 list_append(l, li);
5825 li->li_tv.v_type = VAR_STRING;
5826 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005827 if (str == NULL)
5828 li->li_tv.vval.v_string = NULL;
5829 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005830 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005831 return FAIL;
5832 return OK;
5833}
5834
5835/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005836 * Append "n" to list "l".
5837 * Returns FAIL when out of memory.
5838 */
5839 static int
5840list_append_number(l, n)
5841 list_T *l;
5842 varnumber_T n;
5843{
5844 listitem_T *li;
5845
5846 li = listitem_alloc();
5847 if (li == NULL)
5848 return FAIL;
5849 li->li_tv.v_type = VAR_NUMBER;
5850 li->li_tv.v_lock = 0;
5851 li->li_tv.vval.v_number = n;
5852 list_append(l, li);
5853 return OK;
5854}
5855
5856/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005857 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858 * If "item" is NULL append at the end.
5859 * Return FAIL when out of memory.
5860 */
5861 static int
5862list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 list_T *l;
5864 typval_T *tv;
5865 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866{
Bram Moolenaar33570922005-01-25 22:26:29 +00005867 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005868
5869 if (ni == NULL)
5870 return FAIL;
5871 copy_tv(tv, &ni->li_tv);
5872 if (item == NULL)
5873 /* Append new item at end of list. */
5874 list_append(l, ni);
5875 else
5876 {
5877 /* Insert new item before existing item. */
5878 ni->li_prev = item->li_prev;
5879 ni->li_next = item;
5880 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005881 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005883 ++l->lv_idx;
5884 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005886 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 l->lv_idx_item = NULL;
5889 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005891 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005892 }
5893 return OK;
5894}
5895
5896/*
5897 * Extend "l1" with "l2".
5898 * If "bef" is NULL append at the end, otherwise insert before this item.
5899 * Returns FAIL when out of memory.
5900 */
5901 static int
5902list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l1;
5904 list_T *l2;
5905 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005908
5909 for (item = l2->lv_first; item != NULL; item = item->li_next)
5910 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5911 return FAIL;
5912 return OK;
5913}
5914
5915/*
5916 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5917 * Return FAIL when out of memory.
5918 */
5919 static int
5920list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 list_T *l1;
5922 list_T *l2;
5923 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005924{
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926
5927 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005928 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929 if (l == NULL)
5930 return FAIL;
5931 tv->v_type = VAR_LIST;
5932 tv->vval.v_list = l;
5933
5934 /* append all items from the second list */
5935 return list_extend(l, l2, NULL);
5936}
5937
5938/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005939 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 * Returns NULL when out of memory.
5943 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005948 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 list_T *copy;
5951 listitem_T *item;
5952 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953
5954 if (orig == NULL)
5955 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956
5957 copy = list_alloc();
5958 if (copy != NULL)
5959 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 if (copyID != 0)
5961 {
5962 /* Do this before adding the items, because one of the items may
5963 * refer back to this list. */
5964 orig->lv_copyID = copyID;
5965 orig->lv_copylist = copy;
5966 }
5967 for (item = orig->lv_first; item != NULL && !got_int;
5968 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 {
5970 ni = listitem_alloc();
5971 if (ni == NULL)
5972 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005974 {
5975 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5976 {
5977 vim_free(ni);
5978 break;
5979 }
5980 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005982 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 list_append(copy, ni);
5984 }
5985 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005986 if (item != NULL)
5987 {
5988 list_unref(copy);
5989 copy = NULL;
5990 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 }
5992
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 return copy;
5994}
5995
5996/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006000 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006001list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 list_T *l;
6003 listitem_T *item;
6004 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 /* notify watchers */
6009 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006011 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006012 list_fix_watch(l, ip);
6013 if (ip == item2)
6014 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016
6017 if (item2->li_next == NULL)
6018 l->lv_last = item->li_prev;
6019 else
6020 item2->li_next->li_prev = item->li_prev;
6021 if (item->li_prev == NULL)
6022 l->lv_first = item2->li_next;
6023 else
6024 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006025 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
6029 * Return an allocated string with the string representation of a list.
6030 * May return NULL.
6031 */
6032 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006035 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
6037 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038
6039 if (tv->vval.v_list == NULL)
6040 return NULL;
6041 ga_init2(&ga, (int)sizeof(char), 80);
6042 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006043 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 {
6045 vim_free(ga.ga_data);
6046 return NULL;
6047 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 ga_append(&ga, ']');
6049 ga_append(&ga, NUL);
6050 return (char_u *)ga.ga_data;
6051}
6052
6053/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 * Join list "l" into a string in "*gap", using separator "sep".
6055 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006056 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006059list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006060 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062 char_u *sep;
6063 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006064 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006065{
6066 int first = TRUE;
6067 char_u *tofree;
6068 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 char_u *s;
6071
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006072 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006073 {
6074 if (first)
6075 first = FALSE;
6076 else
6077 ga_concat(gap, sep);
6078
6079 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006080 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006081 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006082 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006083 if (s != NULL)
6084 ga_concat(gap, s);
6085 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006086 if (s == NULL)
6087 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006088 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006089 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006090}
6091
6092/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006093 * Garbage collection for lists and dictionaries.
6094 *
6095 * We use reference counts to be able to free most items right away when they
6096 * are no longer used. But for composite items it's possible that it becomes
6097 * unused while the reference count is > 0: When there is a recursive
6098 * reference. Example:
6099 * :let l = [1, 2, 3]
6100 * :let d = {9: l}
6101 * :let l[1] = d
6102 *
6103 * Since this is quite unusual we handle this with garbage collection: every
6104 * once in a while find out which lists and dicts are not referenced from any
6105 * variable.
6106 *
6107 * Here is a good reference text about garbage collection (refers to Python
6108 * but it applies to all reference-counting mechanisms):
6109 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111
6112/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 * Do garbage collection for lists and dicts.
6114 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 int
6117garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006118{
6119 dict_T *dd;
6120 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 int copyID = ++current_copyID;
6122 buf_T *buf;
6123 win_T *wp;
6124 int i;
6125 funccall_T *fc;
6126 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006127#ifdef FEAT_WINDOWS
6128 tabpage_T *tp;
6129#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006130
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006131 /* Only do this once. */
6132 want_garbage_collect = FALSE;
6133 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006134 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006135
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006136 /*
6137 * 1. Go through all accessible variables and mark all lists and dicts
6138 * with copyID.
6139 */
6140 /* script-local variables */
6141 for (i = 1; i <= ga_scripts.ga_len; ++i)
6142 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6143
6144 /* buffer-local variables */
6145 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6146 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6147
6148 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006149 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006150 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6151
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006152#ifdef FEAT_WINDOWS
6153 /* tabpage-local variables */
6154 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6155 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6156#endif
6157
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006158 /* global variables */
6159 set_ref_in_ht(&globvarht, copyID);
6160
6161 /* function-local variables */
6162 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6163 {
6164 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6165 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6166 }
6167
6168 /*
6169 * 2. Go through the list of dicts and free items without the copyID.
6170 */
6171 for (dd = first_dict; dd != NULL; )
6172 if (dd->dv_copyID != copyID)
6173 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006174 /* Free the Dictionary and ordinary items it contains, but don't
6175 * recurse into Lists and Dictionaries, they will be in the list
6176 * of dicts or list of lists. */
6177 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006178 did_free = TRUE;
6179
6180 /* restart, next dict may also have been freed */
6181 dd = first_dict;
6182 }
6183 else
6184 dd = dd->dv_used_next;
6185
6186 /*
6187 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006188 * But don't free a list that has a watcher (used in a for loop), these
6189 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190 */
6191 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006192 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006193 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006194 /* Free the List and ordinary items it contains, but don't recurse
6195 * into Lists and Dictionaries, they will be in the list of dicts
6196 * or list of lists. */
6197 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006198 did_free = TRUE;
6199
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006200 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006201 ll = first_list;
6202 }
6203 else
6204 ll = ll->lv_used_next;
6205
6206 return did_free;
6207}
6208
6209/*
6210 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6211 */
6212 static void
6213set_ref_in_ht(ht, copyID)
6214 hashtab_T *ht;
6215 int copyID;
6216{
6217 int todo;
6218 hashitem_T *hi;
6219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006220 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006221 for (hi = ht->ht_array; todo > 0; ++hi)
6222 if (!HASHITEM_EMPTY(hi))
6223 {
6224 --todo;
6225 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6226 }
6227}
6228
6229/*
6230 * Mark all lists and dicts referenced through list "l" with "copyID".
6231 */
6232 static void
6233set_ref_in_list(l, copyID)
6234 list_T *l;
6235 int copyID;
6236{
6237 listitem_T *li;
6238
6239 for (li = l->lv_first; li != NULL; li = li->li_next)
6240 set_ref_in_item(&li->li_tv, copyID);
6241}
6242
6243/*
6244 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6245 */
6246 static void
6247set_ref_in_item(tv, copyID)
6248 typval_T *tv;
6249 int copyID;
6250{
6251 dict_T *dd;
6252 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006253
6254 switch (tv->v_type)
6255 {
6256 case VAR_DICT:
6257 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006258 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006259 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006260 /* Didn't see this dict yet. */
6261 dd->dv_copyID = copyID;
6262 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006263 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006264 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006265
6266 case VAR_LIST:
6267 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006268 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006269 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006270 /* Didn't see this list yet. */
6271 ll->lv_copyID = copyID;
6272 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006274 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006276 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006277}
6278
6279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280 * Allocate an empty header for a dictionary.
6281 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006282 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283dict_alloc()
6284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006289 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006290 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006291 if (first_dict != NULL)
6292 first_dict->dv_used_prev = d;
6293 d->dv_used_next = first_dict;
6294 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006295 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006296
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006298 d->dv_lock = 0;
6299 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006300 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006301 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006302 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006303}
6304
6305/*
6306 * Unreference a Dictionary: decrement the reference count and free it when it
6307 * becomes zero.
6308 */
6309 static void
6310dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006313 if (d != NULL && --d->dv_refcount <= 0)
6314 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315}
6316
6317/*
6318 * Free a Dictionary, including all items it contains.
6319 * Ignores the reference count.
6320 */
6321 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006322dict_free(d, recurse)
6323 dict_T *d;
6324 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006325{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006328 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006330 /* Remove the dict from the list of dicts for garbage collection. */
6331 if (d->dv_used_prev == NULL)
6332 first_dict = d->dv_used_next;
6333 else
6334 d->dv_used_prev->dv_used_next = d->dv_used_next;
6335 if (d->dv_used_next != NULL)
6336 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6337
6338 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006339 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006340 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 if (!HASHITEM_EMPTY(hi))
6344 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006345 /* Remove the item before deleting it, just in case there is
6346 * something recursive causing trouble. */
6347 di = HI2DI(hi);
6348 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006349 if (recurse || (di->di_tv.v_type != VAR_LIST
6350 && di->di_tv.v_type != VAR_DICT))
6351 clear_tv(&di->di_tv);
6352 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006353 --todo;
6354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006357 vim_free(d);
6358}
6359
6360/*
6361 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006362 * The "key" is copied to the new item.
6363 * Note that the value of the item "di_tv" still needs to be initialized!
6364 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367dictitem_alloc(key)
6368 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006369{
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006372 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006376 di->di_flags = 0;
6377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379}
6380
6381/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006382 * Make a copy of a Dictionary item.
6383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387{
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006390 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6391 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 if (di != NULL)
6393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006394 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006395 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 copy_tv(&org->di_tv, &di->di_tv);
6397 }
6398 return di;
6399}
6400
6401/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006402 * Remove item "item" from Dictionary "dict" and free it.
6403 */
6404 static void
6405dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 dict_T *dict;
6407 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408{
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 if (HASHITEM_EMPTY(hi))
6413 EMSG2(_(e_intern2), "dictitem_remove()");
6414 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006416 dictitem_free(item);
6417}
6418
6419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 * Free a dict item. Also clears the value.
6421 */
6422 static void
6423dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 clear_tv(&item->di_tv);
6427 vim_free(item);
6428}
6429
6430/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006431 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6432 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006433 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006434 * Returns NULL when out of memory.
6435 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006437dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006441{
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 dict_T *copy;
6443 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006444 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006446
6447 if (orig == NULL)
6448 return NULL;
6449
6450 copy = dict_alloc();
6451 if (copy != NULL)
6452 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006453 if (copyID != 0)
6454 {
6455 orig->dv_copyID = copyID;
6456 orig->dv_copydict = copy;
6457 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006458 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006463 --todo;
6464
6465 di = dictitem_alloc(hi->hi_key);
6466 if (di == NULL)
6467 break;
6468 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 {
6470 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6471 copyID) == FAIL)
6472 {
6473 vim_free(di);
6474 break;
6475 }
6476 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006477 else
6478 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6479 if (dict_add(copy, di) == FAIL)
6480 {
6481 dictitem_free(di);
6482 break;
6483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 if (todo > 0)
6489 {
6490 dict_unref(copy);
6491 copy = NULL;
6492 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006493 }
6494
6495 return copy;
6496}
6497
6498/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006502 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006503dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 dict_T *d;
6505 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508}
6509
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006511 * Add a number or string entry to dictionary "d".
6512 * When "str" is NULL use number "nr", otherwise use "str".
6513 * Returns FAIL when out of memory and when key already exists.
6514 */
6515 int
6516dict_add_nr_str(d, key, nr, str)
6517 dict_T *d;
6518 char *key;
6519 long nr;
6520 char_u *str;
6521{
6522 dictitem_T *item;
6523
6524 item = dictitem_alloc((char_u *)key);
6525 if (item == NULL)
6526 return FAIL;
6527 item->di_tv.v_lock = 0;
6528 if (str == NULL)
6529 {
6530 item->di_tv.v_type = VAR_NUMBER;
6531 item->di_tv.vval.v_number = nr;
6532 }
6533 else
6534 {
6535 item->di_tv.v_type = VAR_STRING;
6536 item->di_tv.vval.v_string = vim_strsave(str);
6537 }
6538 if (dict_add(d, item) == FAIL)
6539 {
6540 dictitem_free(item);
6541 return FAIL;
6542 }
6543 return OK;
6544}
6545
6546/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547 * Get the number of items in a Dictionary.
6548 */
6549 static long
6550dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 if (d == NULL)
6554 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006555 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556}
6557
6558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 * Find item "key[len]" in Dictionary "d".
6560 * If "len" is negative use strlen(key).
6561 * Returns NULL when not found.
6562 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006564dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566 char_u *key;
6567 int len;
6568{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006569#define AKEYLEN 200
6570 char_u buf[AKEYLEN];
6571 char_u *akey;
6572 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006575 if (len < 0)
6576 akey = key;
6577 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006579 tofree = akey = vim_strnsave(key, len);
6580 if (akey == NULL)
6581 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006582 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 else
6584 {
6585 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006586 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006587 akey = buf;
6588 }
6589
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 vim_free(tofree);
6592 if (HASHITEM_EMPTY(hi))
6593 return NULL;
6594 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595}
6596
6597/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006598 * Get a string item from a dictionary.
6599 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006600 * Returns NULL if the entry doesn't exist or out of memory.
6601 */
6602 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006603get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006604 dict_T *d;
6605 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006606 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006607{
6608 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006609 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006610
6611 di = dict_find(d, key, -1);
6612 if (di == NULL)
6613 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006614 s = get_tv_string(&di->di_tv);
6615 if (save && s != NULL)
6616 s = vim_strsave(s);
6617 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006618}
6619
6620/*
6621 * Get a number item from a dictionary.
6622 * Returns 0 if the entry doesn't exist or out of memory.
6623 */
6624 long
6625get_dict_number(d, key)
6626 dict_T *d;
6627 char_u *key;
6628{
6629 dictitem_T *di;
6630
6631 di = dict_find(d, key, -1);
6632 if (di == NULL)
6633 return 0;
6634 return get_tv_number(&di->di_tv);
6635}
6636
6637/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006638 * Return an allocated string with the string representation of a Dictionary.
6639 * May return NULL.
6640 */
6641 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006644 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645{
6646 garray_T ga;
6647 int first = TRUE;
6648 char_u *tofree;
6649 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006651 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006655 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 return NULL;
6657 ga_init2(&ga, (int)sizeof(char), 80);
6658 ga_append(&ga, '{');
6659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006660 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006661 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006665 --todo;
6666
6667 if (first)
6668 first = FALSE;
6669 else
6670 ga_concat(&ga, (char_u *)", ");
6671
6672 tofree = string_quote(hi->hi_key, FALSE);
6673 if (tofree != NULL)
6674 {
6675 ga_concat(&ga, tofree);
6676 vim_free(tofree);
6677 }
6678 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006679 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006680 if (s != NULL)
6681 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 if (s == NULL)
6684 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006687 if (todo > 0)
6688 {
6689 vim_free(ga.ga_data);
6690 return NULL;
6691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692
6693 ga_append(&ga, '}');
6694 ga_append(&ga, NUL);
6695 return (char_u *)ga.ga_data;
6696}
6697
6698/*
6699 * Allocate a variable for a Dictionary and fill it from "*arg".
6700 * Return OK or FAIL. Returns NOTDONE for {expr}.
6701 */
6702 static int
6703get_dict_tv(arg, rettv, evaluate)
6704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706 int evaluate;
6707{
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 dict_T *d = NULL;
6709 typval_T tvkey;
6710 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006711 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006712 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006714 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715
6716 /*
6717 * First check if it's not a curly-braces thing: {expr}.
6718 * Must do this without evaluating, otherwise a function may be called
6719 * twice. Unfortunately this means we need to call eval1() twice for the
6720 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006723 if (*start != '}')
6724 {
6725 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6726 return FAIL;
6727 if (*start == '}')
6728 return NOTDONE;
6729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006730
6731 if (evaluate)
6732 {
6733 d = dict_alloc();
6734 if (d == NULL)
6735 return FAIL;
6736 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 tvkey.v_type = VAR_UNKNOWN;
6738 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739
6740 *arg = skipwhite(*arg + 1);
6741 while (**arg != '}' && **arg != NUL)
6742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006743 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006744 goto failret;
6745 if (**arg != ':')
6746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006747 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006748 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 goto failret;
6750 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006751 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006753 key = get_tv_string_buf_chk(&tvkey, buf);
6754 if (key == NULL || *key == NUL)
6755 {
6756 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6757 if (key != NULL)
6758 EMSG(_(e_emptykey));
6759 clear_tv(&tvkey);
6760 goto failret;
6761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763
6764 *arg = skipwhite(*arg + 1);
6765 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6766 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006767 if (evaluate)
6768 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006769 goto failret;
6770 }
6771 if (evaluate)
6772 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 if (item != NULL)
6775 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006776 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006778 clear_tv(&tv);
6779 goto failret;
6780 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006781 item = dictitem_alloc(key);
6782 clear_tv(&tvkey);
6783 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006786 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006787 if (dict_add(d, item) == FAIL)
6788 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006789 }
6790 }
6791
6792 if (**arg == '}')
6793 break;
6794 if (**arg != ',')
6795 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006796 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006797 goto failret;
6798 }
6799 *arg = skipwhite(*arg + 1);
6800 }
6801
6802 if (**arg != '}')
6803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006804 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805failret:
6806 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006807 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808 return FAIL;
6809 }
6810
6811 *arg = skipwhite(*arg + 1);
6812 if (evaluate)
6813 {
6814 rettv->v_type = VAR_DICT;
6815 rettv->vval.v_dict = d;
6816 ++d->dv_refcount;
6817 }
6818
6819 return OK;
6820}
6821
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 * Return a string with the string representation of a variable.
6824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006825 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006827 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006828 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 */
6830 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006831echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006834 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006835 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 static int recurse = 0;
6838 char_u *r = NULL;
6839
Bram Moolenaar33570922005-01-25 22:26:29 +00006840 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006842 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 *tofree = NULL;
6844 return NULL;
6845 }
6846 ++recurse;
6847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006848 switch (tv->v_type)
6849 {
6850 case VAR_FUNC:
6851 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006852 r = tv->vval.v_string;
6853 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006854
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006856 if (tv->vval.v_list == NULL)
6857 {
6858 *tofree = NULL;
6859 r = NULL;
6860 }
6861 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6862 {
6863 *tofree = NULL;
6864 r = (char_u *)"[...]";
6865 }
6866 else
6867 {
6868 tv->vval.v_list->lv_copyID = copyID;
6869 *tofree = list2string(tv, copyID);
6870 r = *tofree;
6871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006873
Bram Moolenaar8c711452005-01-14 21:53:12 +00006874 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006875 if (tv->vval.v_dict == NULL)
6876 {
6877 *tofree = NULL;
6878 r = NULL;
6879 }
6880 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6881 {
6882 *tofree = NULL;
6883 r = (char_u *)"{...}";
6884 }
6885 else
6886 {
6887 tv->vval.v_dict->dv_copyID = copyID;
6888 *tofree = dict2string(tv, copyID);
6889 r = *tofree;
6890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006891 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 case VAR_STRING:
6894 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006895 *tofree = NULL;
6896 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903
6904 --recurse;
6905 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006906}
6907
6908/*
6909 * Return a string with the string representation of a variable.
6910 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6911 * "numbuf" is used for a number.
6912 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006913 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006914 */
6915 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006916tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006918 char_u **tofree;
6919 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006920 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006921{
6922 switch (tv->v_type)
6923 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 case VAR_FUNC:
6925 *tofree = string_quote(tv->vval.v_string, TRUE);
6926 return *tofree;
6927 case VAR_STRING:
6928 *tofree = string_quote(tv->vval.v_string, FALSE);
6929 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006930 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006931 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006933 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006934 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006936 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006937 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938}
6939
6940/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 * Return string "str" in ' quotes, doubling ' characters.
6942 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006944 */
6945 static char_u *
6946string_quote(str, function)
6947 char_u *str;
6948 int function;
6949{
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006951 char_u *p, *r, *s;
6952
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 len = (function ? 13 : 3);
6954 if (str != NULL)
6955 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006956 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 for (p = str; *p != NUL; mb_ptr_adv(p))
6958 if (*p == '\'')
6959 ++len;
6960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 s = r = alloc(len);
6962 if (r != NULL)
6963 {
6964 if (function)
6965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006967 r += 10;
6968 }
6969 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 if (str != NULL)
6972 for (p = str; *p != NUL; )
6973 {
6974 if (*p == '\'')
6975 *r++ = '\'';
6976 MB_COPY_CHAR(p, r);
6977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006979 if (function)
6980 *r++ = ')';
6981 *r++ = NUL;
6982 }
6983 return s;
6984}
6985
6986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 * Get the value of an environment variable.
6988 * "arg" is pointing to the '$'. It is advanced to after the name.
6989 * If the environment variable was not set, silently assume it is empty.
6990 * Always return OK.
6991 */
6992 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006993get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 int evaluate;
6997{
6998 char_u *string = NULL;
6999 int len;
7000 int cc;
7001 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007002 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 ++*arg;
7005 name = *arg;
7006 len = get_env_len(arg);
7007 if (evaluate)
7008 {
7009 if (len != 0)
7010 {
7011 cc = name[len];
7012 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007013 /* first try vim_getenv(), fast for normal environment vars */
7014 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007016 {
7017 if (!mustfree)
7018 string = vim_strsave(string);
7019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 else
7021 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007022 if (mustfree)
7023 vim_free(string);
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 /* next try expanding things like $VIM and ${HOME} */
7026 string = expand_env_save(name - 1);
7027 if (string != NULL && *string == '$')
7028 {
7029 vim_free(string);
7030 string = NULL;
7031 }
7032 }
7033 name[len] = cc;
7034 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007035 rettv->v_type = VAR_STRING;
7036 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
7038
7039 return OK;
7040}
7041
7042/*
7043 * Array with names and number of arguments of all internal functions
7044 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7045 */
7046static struct fst
7047{
7048 char *f_name; /* function name */
7049 char f_min_argc; /* minimal number of arguments */
7050 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007052 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053} functions[] =
7054{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007055 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"append", 2, 2, f_append},
7057 {"argc", 0, 0, f_argc},
7058 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007059 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007061 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {"bufexists", 1, 1, f_bufexists},
7063 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7064 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7065 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7066 {"buflisted", 1, 1, f_buflisted},
7067 {"bufloaded", 1, 1, f_bufloaded},
7068 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007069 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"bufwinnr", 1, 1, f_bufwinnr},
7071 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007072 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007074 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"char2nr", 1, 1, f_char2nr},
7076 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007077 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007079#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007080 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007081 {"complete_add", 1, 1, f_complete_add},
7082 {"complete_check", 0, 0, f_complete_check},
7083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007086 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007088 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007089 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {"delete", 1, 1, f_delete},
7091 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007092 {"diff_filler", 1, 1, f_diff_filler},
7093 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007094 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007096 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"eventhandler", 0, 0, f_eventhandler},
7098 {"executable", 1, 1, f_executable},
7099 {"exists", 1, 1, f_exists},
7100 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007101 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007102 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7104 {"filereadable", 1, 1, f_filereadable},
7105 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007106 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007107 {"finddir", 1, 3, f_finddir},
7108 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"fnamemodify", 2, 2, f_fnamemodify},
7110 {"foldclosed", 1, 1, f_foldclosed},
7111 {"foldclosedend", 1, 1, f_foldclosedend},
7112 {"foldlevel", 1, 1, f_foldlevel},
7113 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007114 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007117 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007118 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007119 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {"getbufvar", 2, 2, f_getbufvar},
7121 {"getchar", 0, 1, f_getchar},
7122 {"getcharmod", 0, 0, f_getcharmod},
7123 {"getcmdline", 0, 0, f_getcmdline},
7124 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007125 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007127 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007128 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"getfsize", 1, 1, f_getfsize},
7130 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007131 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007132 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007133 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007134 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007135 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007136 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007137 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007139 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"getwinposx", 0, 0, f_getwinposx},
7141 {"getwinposy", 0, 0, f_getwinposy},
7142 {"getwinvar", 2, 2, f_getwinvar},
7143 {"glob", 1, 1, f_glob},
7144 {"globpath", 2, 2, f_globpath},
7145 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007147 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007148 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7150 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7151 {"histadd", 2, 2, f_histadd},
7152 {"histdel", 1, 2, f_histdel},
7153 {"histget", 1, 2, f_histget},
7154 {"histnr", 1, 1, f_histnr},
7155 {"hlID", 1, 1, f_hlID},
7156 {"hlexists", 1, 1, f_hlexists},
7157 {"hostname", 0, 0, f_hostname},
7158 {"iconv", 3, 3, f_iconv},
7159 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007160 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007161 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007163 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"inputrestore", 0, 0, f_inputrestore},
7165 {"inputsave", 0, 0, f_inputsave},
7166 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007167 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007169 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007171 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"libcall", 3, 3, f_libcall},
7176 {"libcallnr", 3, 3, f_libcallnr},
7177 {"line", 1, 1, f_line},
7178 {"line2byte", 1, 1, f_line2byte},
7179 {"lispindent", 1, 1, f_lispindent},
7180 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007181 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007182 {"maparg", 1, 3, f_maparg},
7183 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007184 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007185 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007186 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007187 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007188 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007189 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007190 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007191 {"max", 1, 1, f_max},
7192 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007193#ifdef vim_mkdir
7194 {"mkdir", 1, 3, f_mkdir},
7195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {"mode", 0, 0, f_mode},
7197 {"nextnonblank", 1, 1, f_nextnonblank},
7198 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007199 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007201 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007204 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007205 {"reltime", 0, 2, f_reltime},
7206 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {"remote_expr", 2, 3, f_remote_expr},
7208 {"remote_foreground", 1, 1, f_remote_foreground},
7209 {"remote_peek", 1, 2, f_remote_peek},
7210 {"remote_read", 1, 1, f_remote_read},
7211 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007212 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007214 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007216 {"reverse", 1, 1, f_reverse},
Bram Moolenaar76929292008-01-06 19:07:36 +00007217 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007218 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007219 {"searchpair", 3, 7, f_searchpair},
7220 {"searchpairpos", 3, 7, f_searchpairpos},
7221 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {"server2client", 2, 2, f_server2client},
7223 {"serverlist", 0, 0, f_serverlist},
7224 {"setbufvar", 3, 3, f_setbufvar},
7225 {"setcmdpos", 1, 1, f_setcmdpos},
7226 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007227 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007228 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007229 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007230 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007232 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007234 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007236 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007237 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007238 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007239 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007240 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007241 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#ifdef HAVE_STRFTIME
7243 {"strftime", 1, 2, f_strftime},
7244#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007246 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {"strlen", 1, 1, f_strlen},
7248 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007249 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {"strtrans", 1, 1, f_strtrans},
7251 {"submatch", 1, 1, f_submatch},
7252 {"substitute", 4, 4, f_substitute},
7253 {"synID", 3, 3, f_synID},
7254 {"synIDattr", 2, 3, f_synIDattr},
7255 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007256 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007257 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007259 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007261 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007262 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007264 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"tolower", 1, 1, f_tolower},
7266 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007267 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {"virtcol", 1, 1, f_virtcol},
7271 {"visualmode", 0, 1, f_visualmode},
7272 {"winbufnr", 1, 1, f_winbufnr},
7273 {"wincol", 0, 0, f_wincol},
7274 {"winheight", 1, 1, f_winheight},
7275 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007276 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007278 {"winrestview", 1, 1, f_winrestview},
7279 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007281 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282};
7283
7284#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7285
7286/*
7287 * Function given to ExpandGeneric() to obtain the list of internal
7288 * or user defined function names.
7289 */
7290 char_u *
7291get_function_name(xp, idx)
7292 expand_T *xp;
7293 int idx;
7294{
7295 static int intidx = -1;
7296 char_u *name;
7297
7298 if (idx == 0)
7299 intidx = -1;
7300 if (intidx < 0)
7301 {
7302 name = get_user_func_name(xp, idx);
7303 if (name != NULL)
7304 return name;
7305 }
7306 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7307 {
7308 STRCPY(IObuff, functions[intidx].f_name);
7309 STRCAT(IObuff, "(");
7310 if (functions[intidx].f_max_argc == 0)
7311 STRCAT(IObuff, ")");
7312 return IObuff;
7313 }
7314
7315 return NULL;
7316}
7317
7318/*
7319 * Function given to ExpandGeneric() to obtain the list of internal or
7320 * user defined variable or function names.
7321 */
7322/*ARGSUSED*/
7323 char_u *
7324get_expr_name(xp, idx)
7325 expand_T *xp;
7326 int idx;
7327{
7328 static int intidx = -1;
7329 char_u *name;
7330
7331 if (idx == 0)
7332 intidx = -1;
7333 if (intidx < 0)
7334 {
7335 name = get_function_name(xp, idx);
7336 if (name != NULL)
7337 return name;
7338 }
7339 return get_user_var_name(xp, ++intidx);
7340}
7341
7342#endif /* FEAT_CMDL_COMPL */
7343
7344/*
7345 * Find internal function in table above.
7346 * Return index, or -1 if not found
7347 */
7348 static int
7349find_internal_func(name)
7350 char_u *name; /* name of the function */
7351{
7352 int first = 0;
7353 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7354 int cmp;
7355 int x;
7356
7357 /*
7358 * Find the function name in the table. Binary search.
7359 */
7360 while (first <= last)
7361 {
7362 x = first + ((unsigned)(last - first) >> 1);
7363 cmp = STRCMP(name, functions[x].f_name);
7364 if (cmp < 0)
7365 last = x - 1;
7366 else if (cmp > 0)
7367 first = x + 1;
7368 else
7369 return x;
7370 }
7371 return -1;
7372}
7373
7374/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007375 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7376 * name it contains, otherwise return "name".
7377 */
7378 static char_u *
7379deref_func_name(name, lenp)
7380 char_u *name;
7381 int *lenp;
7382{
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 int cc;
7385
7386 cc = name[*lenp];
7387 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007388 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 {
7394 *lenp = 0;
7395 return (char_u *)""; /* just in case */
7396 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 }
7400
7401 return name;
7402}
7403
7404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 * Allocate a variable for the result of a function.
7406 * Return OK or FAIL.
7407 */
7408 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7410 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u *name; /* name of the function */
7412 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 char_u **arg; /* argument, pointing to the '(' */
7415 linenr_T firstline; /* first line of range */
7416 linenr_T lastline; /* last line of range */
7417 int *doesrange; /* return: function handled range */
7418 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 char_u *argp;
7422 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007423 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 int argcount = 0; /* number of arguments found */
7425
7426 /*
7427 * Get the arguments.
7428 */
7429 argp = *arg;
7430 while (argcount < MAX_FUNC_ARGS)
7431 {
7432 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7433 if (*argp == ')' || *argp == ',' || *argp == NUL)
7434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7436 {
7437 ret = FAIL;
7438 break;
7439 }
7440 ++argcount;
7441 if (*argp != ',')
7442 break;
7443 }
7444 if (*argp == ')')
7445 ++argp;
7446 else
7447 ret = FAIL;
7448
7449 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 {
7454 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462
7463 *arg = skipwhite(argp);
7464 return ret;
7465}
7466
7467
7468/*
7469 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007470 * Return OK when the function can't be called, FAIL otherwise.
7471 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 */
7473 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 char_u *name; /* name of the function */
7477 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007480 typval_T *argvars; /* vars for arguments, must have "argcount"
7481 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 linenr_T firstline; /* first line of range */
7483 linenr_T lastline; /* last line of range */
7484 int *doesrange; /* return: function handled range */
7485 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489#define ERROR_UNKNOWN 0
7490#define ERROR_TOOMANY 1
7491#define ERROR_TOOFEW 2
7492#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493#define ERROR_DICT 4
7494#define ERROR_NONE 5
7495#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 int error = ERROR_NONE;
7497 int i;
7498 int llen;
7499 ufunc_T *fp;
7500 int cc;
7501#define FLEN_FIXED 40
7502 char_u fname_buf[FLEN_FIXED + 1];
7503 char_u *fname;
7504
7505 /*
7506 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7507 * Change <SNR>123_name() to K_SNR 123_name().
7508 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7509 */
7510 cc = name[len];
7511 name[len] = NUL;
7512 llen = eval_fname_script(name);
7513 if (llen > 0)
7514 {
7515 fname_buf[0] = K_SPECIAL;
7516 fname_buf[1] = KS_EXTRA;
7517 fname_buf[2] = (int)KE_SNR;
7518 i = 3;
7519 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7520 {
7521 if (current_SID <= 0)
7522 error = ERROR_SCRIPT;
7523 else
7524 {
7525 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7526 i = (int)STRLEN(fname_buf);
7527 }
7528 }
7529 if (i + STRLEN(name + llen) < FLEN_FIXED)
7530 {
7531 STRCPY(fname_buf + i, name + llen);
7532 fname = fname_buf;
7533 }
7534 else
7535 {
7536 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7537 if (fname == NULL)
7538 error = ERROR_OTHER;
7539 else
7540 {
7541 mch_memmove(fname, fname_buf, (size_t)i);
7542 STRCPY(fname + i, name + llen);
7543 }
7544 }
7545 }
7546 else
7547 fname = name;
7548
7549 *doesrange = FALSE;
7550
7551
7552 /* execute the function if no errors detected and executing */
7553 if (evaluate && error == ERROR_NONE)
7554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 error = ERROR_UNKNOWN;
7557
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007558 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
7560 /*
7561 * User defined function.
7562 */
7563 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007566 /* Trigger FuncUndefined event, may load the function. */
7567 if (fp == NULL
7568 && apply_autocmds(EVENT_FUNCUNDEFINED,
7569 fname, fname, TRUE, NULL)
7570 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007572 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 fp = find_func(fname);
7574 }
7575#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007576 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007577 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007578 {
7579 /* loaded a package, search for the function again */
7580 fp = find_func(fname);
7581 }
7582
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (fp != NULL)
7584 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007585 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007591 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 else
7594 {
7595 /*
7596 * Call the user function.
7597 * Save and restore search patterns, script variables and
7598 * redo buffer.
7599 */
7600 save_search_patterns();
7601 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007602 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007605 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7606 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7607 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 /* Function was unreferenced while being used, free it
7609 * now. */
7610 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 restoreRedobuff();
7612 restore_search_patterns();
7613 error = ERROR_NONE;
7614 }
7615 }
7616 }
7617 else
7618 {
7619 /*
7620 * Find the function name in the table, call its implementation.
7621 */
7622 i = find_internal_func(fname);
7623 if (i >= 0)
7624 {
7625 if (argcount < functions[i].f_min_argc)
7626 error = ERROR_TOOFEW;
7627 else if (argcount > functions[i].f_max_argc)
7628 error = ERROR_TOOMANY;
7629 else
7630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007632 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 error = ERROR_NONE;
7634 }
7635 }
7636 }
7637 /*
7638 * The function call (or "FuncUndefined" autocommand sequence) might
7639 * have been aborted by an error, an interrupt, or an explicitly thrown
7640 * exception that has not been caught so far. This situation can be
7641 * tested for by calling aborting(). For an error in an internal
7642 * function or for the "E132" error in call_user_func(), however, the
7643 * throw point at which the "force_abort" flag (temporarily reset by
7644 * emsg()) is normally updated has not been reached yet. We need to
7645 * update that flag first to make aborting() reliable.
7646 */
7647 update_force_abort();
7648 }
7649 if (error == ERROR_NONE)
7650 ret = OK;
7651
7652 /*
7653 * Report an error unless the argument evaluation or function call has been
7654 * cancelled due to an aborting error, an interrupt, or an exception.
7655 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 if (!aborting())
7657 {
7658 switch (error)
7659 {
7660 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007661 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 break;
7663 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 break;
7666 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007667 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 name);
7669 break;
7670 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 name);
7673 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007675 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 name);
7677 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 }
7679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 name[len] = cc;
7682 if (fname != name && fname != fname_buf)
7683 vim_free(fname);
7684
7685 return ret;
7686}
7687
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007688/*
7689 * Give an error message with a function name. Handle <SNR> things.
7690 */
7691 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007692emsg_funcname(ermsg, name)
7693 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007694 char_u *name;
7695{
7696 char_u *p;
7697
7698 if (*name == K_SPECIAL)
7699 p = concat_str((char_u *)"<SNR>", name + 3);
7700 else
7701 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007702 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007703 if (p != name)
7704 vim_free(p);
7705}
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*********************************************
7708 * Implementation of the built-in functions
7709 */
7710
7711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007712 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 */
7714 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007715f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 typval_T *argvars;
7717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007724 if ((l = argvars[0].vval.v_list) != NULL
7725 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7726 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 }
7729 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007730 EMSG(_(e_listreq));
7731}
7732
7733/*
7734 * "append(lnum, string/list)" function
7735 */
7736 static void
7737f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 typval_T *argvars;
7739 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007740{
7741 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 list_T *l = NULL;
7744 listitem_T *li = NULL;
7745 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007746 long added = 0;
7747
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 lnum = get_tv_lnum(argvars);
7749 if (lnum >= 0
7750 && lnum <= curbuf->b_ml.ml_line_count
7751 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007753 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007755 l = argvars[1].vval.v_list;
7756 if (l == NULL)
7757 return;
7758 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007759 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007760 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007761 for (;;)
7762 {
7763 if (l == NULL)
7764 tv = &argvars[1]; /* append a string */
7765 else if (li == NULL)
7766 break; /* end of list */
7767 else
7768 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 line = get_tv_string_chk(tv);
7770 if (line == NULL) /* type error */
7771 {
7772 rettv->vval.v_number = 1; /* Failed */
7773 break;
7774 }
7775 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007776 ++added;
7777 if (l == NULL)
7778 break;
7779 li = li->li_next;
7780 }
7781
7782 appended_lines_mark(lnum, added);
7783 if (curwin->w_cursor.lnum > lnum)
7784 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007786 else
7787 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790/*
7791 * "argc()" function
7792 */
7793/* ARGSUSED */
7794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *argvars;
7797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800}
7801
7802/*
7803 * "argidx()" function
7804 */
7805/* ARGSUSED */
7806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *argvars;
7809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812}
7813
7814/*
7815 * "argv(nr)" function
7816 */
7817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 typval_T *argvars;
7820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 int idx;
7823
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007824 if (argvars[0].v_type != VAR_UNKNOWN)
7825 {
7826 idx = get_tv_number_chk(&argvars[0], NULL);
7827 if (idx >= 0 && idx < ARGCOUNT)
7828 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7829 else
7830 rettv->vval.v_string = NULL;
7831 rettv->v_type = VAR_STRING;
7832 }
7833 else if (rettv_list_alloc(rettv) == OK)
7834 for (idx = 0; idx < ARGCOUNT; ++idx)
7835 list_append_string(rettv->vval.v_list,
7836 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
7839/*
7840 * "browse(save, title, initdir, default)" function
7841 */
7842/* ARGSUSED */
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848#ifdef FEAT_BROWSE
7849 int save;
7850 char_u *title;
7851 char_u *initdir;
7852 char_u *defname;
7853 char_u buf[NUMBUFLEN];
7854 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 save = get_tv_number_chk(&argvars[0], &error);
7858 title = get_tv_string_chk(&argvars[1]);
7859 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7860 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 if (error || title == NULL || initdir == NULL || defname == NULL)
7863 rettv->vval.v_string = NULL;
7864 else
7865 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007866 do_browse(save ? BROWSE_SAVE : 0,
7867 title, defname, NULL, initdir, NULL, curbuf);
7868#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007870#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872}
7873
7874/*
7875 * "browsedir(title, initdir)" function
7876 */
7877/* ARGSUSED */
7878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 typval_T *argvars;
7881 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007882{
7883#ifdef FEAT_BROWSE
7884 char_u *title;
7885 char_u *initdir;
7886 char_u buf[NUMBUFLEN];
7887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 title = get_tv_string_chk(&argvars[0]);
7889 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007891 if (title == NULL || initdir == NULL)
7892 rettv->vval.v_string = NULL;
7893 else
7894 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007895 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901
Bram Moolenaar33570922005-01-25 22:26:29 +00007902static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007903
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904/*
7905 * Find a buffer by number or exact name.
7906 */
7907 static buf_T *
7908find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 if (avar->v_type == VAR_NUMBER)
7914 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007915 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007918 if (buf == NULL)
7919 {
7920 /* No full path name match, try a match with a URL or a "nofile"
7921 * buffer, these don't use the full path. */
7922 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7923 if (buf->b_fname != NULL
7924 && (path_with_url(buf->b_fname)
7925#ifdef FEAT_QUICKFIX
7926 || bt_nofile(buf)
7927#endif
7928 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007930 break;
7931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933 return buf;
7934}
7935
7936/*
7937 * "bufexists(expr)" function
7938 */
7939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 typval_T *argvars;
7942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945}
7946
7947/*
7948 * "buflisted(expr)" function
7949 */
7950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 typval_T *argvars;
7953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 buf_T *buf;
7956
7957 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
7961/*
7962 * "bufloaded(expr)" function
7963 */
7964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 typval_T *argvars;
7967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 buf_T *buf;
7970
7971 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973}
7974
Bram Moolenaar33570922005-01-25 22:26:29 +00007975static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977/*
7978 * Get buffer by number or pattern.
7979 */
7980 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 int save_magic;
7986 char_u *save_cpo;
7987 buf_T *buf;
7988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989 if (tv->v_type == VAR_NUMBER)
7990 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007991 if (tv->v_type != VAR_STRING)
7992 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (name == NULL || *name == NUL)
7994 return curbuf;
7995 if (name[0] == '$' && name[1] == NUL)
7996 return lastbuf;
7997
7998 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7999 save_magic = p_magic;
8000 p_magic = TRUE;
8001 save_cpo = p_cpo;
8002 p_cpo = (char_u *)"";
8003
8004 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8005 TRUE, FALSE));
8006
8007 p_magic = save_magic;
8008 p_cpo = save_cpo;
8009
8010 /* If not found, try expanding the name, like done for bufexists(). */
8011 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 return buf;
8015}
8016
8017/*
8018 * "bufname(expr)" function
8019 */
8020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 typval_T *argvars;
8023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 buf_T *buf;
8026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008027 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 buf = get_buf_tv(&argvars[0]);
8030 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 --emsg_off;
8036}
8037
8038/*
8039 * "bufnr(expr)" function
8040 */
8041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 typval_T *argvars;
8044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008047 int error = FALSE;
8048 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008050 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008052 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008053 --emsg_off;
8054
8055 /* If the buffer isn't found and the second argument is not zero create a
8056 * new buffer. */
8057 if (buf == NULL
8058 && argvars[1].v_type != VAR_UNKNOWN
8059 && get_tv_number_chk(&argvars[1], &error) != 0
8060 && !error
8061 && (name = get_tv_string_chk(&argvars[0])) != NULL
8062 && !error)
8063 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8064
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069}
8070
8071/*
8072 * "bufwinnr(nr)" function
8073 */
8074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008075f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 typval_T *argvars;
8077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079#ifdef FEAT_WINDOWS
8080 win_T *wp;
8081 int winnr = 0;
8082#endif
8083 buf_T *buf;
8084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008085 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088#ifdef FEAT_WINDOWS
8089 for (wp = firstwin; wp; wp = wp->w_next)
8090 {
8091 ++winnr;
8092 if (wp->w_buffer == buf)
8093 break;
8094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008095 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#endif
8099 --emsg_off;
8100}
8101
8102/*
8103 * "byte2line(byte)" function
8104 */
8105/*ARGSUSED*/
8106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 typval_T *argvars;
8109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#else
8114 long boff = 0;
8115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 (linenr_T)0, &boff);
8122#endif
8123}
8124
8125/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008126 * "byteidx()" function
8127 */
8128/*ARGSUSED*/
8129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008130f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008131 typval_T *argvars;
8132 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008133{
8134#ifdef FEAT_MBYTE
8135 char_u *t;
8136#endif
8137 char_u *str;
8138 long idx;
8139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008140 str = get_tv_string_chk(&argvars[0]);
8141 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008143 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008144 return;
8145
8146#ifdef FEAT_MBYTE
8147 t = str;
8148 for ( ; idx > 0; idx--)
8149 {
8150 if (*t == NUL) /* EOL reached */
8151 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008152 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008153 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008154 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008155#else
8156 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008158#endif
8159}
8160
8161/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008162 * "call(func, arglist)" function
8163 */
8164 static void
8165f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 typval_T *argvars;
8167 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168{
8169 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008170 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008171 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008173 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008175
8176 rettv->vval.v_number = 0;
8177 if (argvars[1].v_type != VAR_LIST)
8178 {
8179 EMSG(_(e_listreq));
8180 return;
8181 }
8182 if (argvars[1].vval.v_list == NULL)
8183 return;
8184
8185 if (argvars[0].v_type == VAR_FUNC)
8186 func = argvars[0].vval.v_string;
8187 else
8188 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008189 if (*func == NUL)
8190 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008191
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 if (argvars[2].v_type != VAR_UNKNOWN)
8193 {
8194 if (argvars[2].v_type != VAR_DICT)
8195 {
8196 EMSG(_(e_dictreq));
8197 return;
8198 }
8199 selfdict = argvars[2].vval.v_dict;
8200 }
8201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008202 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8203 item = item->li_next)
8204 {
8205 if (argc == MAX_FUNC_ARGS)
8206 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008207 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008208 break;
8209 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008210 /* Make a copy of each argument. This is needed to be able to set
8211 * v_lock to VAR_FIXED in the copy without changing the original list.
8212 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008213 copy_tv(&item->li_tv, &argv[argc++]);
8214 }
8215
8216 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008217 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8219 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008220
8221 /* Free the arguments. */
8222 while (argc > 0)
8223 clear_tv(&argv[--argc]);
8224}
8225
8226/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008227 * "changenr()" function
8228 */
8229/*ARGSUSED*/
8230 static void
8231f_changenr(argvars, rettv)
8232 typval_T *argvars;
8233 typval_T *rettv;
8234{
8235 rettv->vval.v_number = curbuf->b_u_seq_cur;
8236}
8237
8238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 * "char2nr(string)" function
8240 */
8241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 typval_T *argvars;
8244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246#ifdef FEAT_MBYTE
8247 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008248 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 else
8250#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252}
8253
8254/*
8255 * "cindent(lnum)" function
8256 */
8257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 typval_T *argvars;
8260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262#ifdef FEAT_CINDENT
8263 pos_T pos;
8264 linenr_T lnum;
8265
8266 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8269 {
8270 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 curwin->w_cursor = pos;
8273 }
8274 else
8275#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
8279/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008280 * "clearmatches()" function
8281 */
8282/*ARGSUSED*/
8283 static void
8284f_clearmatches(argvars, rettv)
8285 typval_T *argvars;
8286 typval_T *rettv;
8287{
8288#ifdef FEAT_SEARCH_EXTRA
8289 clear_matches(curwin);
8290#endif
8291}
8292
8293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 * "col(string)" function
8295 */
8296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T *argvars;
8299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 colnr_T col = 0;
8302 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008303 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008305 fp = var2fpos(&argvars[0], FALSE, &fnum);
8306 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 if (fp->col == MAXCOL)
8309 {
8310 /* '> can be MAXCOL, get the length of the line then */
8311 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008312 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 else
8314 col = MAXCOL;
8315 }
8316 else
8317 {
8318 col = fp->col + 1;
8319#ifdef FEAT_VIRTUALEDIT
8320 /* col(".") when the cursor is on the NUL at the end of the line
8321 * because of "coladd" can be seen as an extra column. */
8322 if (virtual_active() && fp == &curwin->w_cursor)
8323 {
8324 char_u *p = ml_get_cursor();
8325
8326 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8327 curwin->w_virtcol - curwin->w_cursor.coladd))
8328 {
8329# ifdef FEAT_MBYTE
8330 int l;
8331
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008332 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 col += l;
8334# else
8335 if (*p != NUL && p[1] == NUL)
8336 ++col;
8337# endif
8338 }
8339 }
8340#endif
8341 }
8342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
Bram Moolenaar572cb562005-08-05 21:35:02 +00008346#if defined(FEAT_INS_EXPAND)
8347/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008348 * "complete()" function
8349 */
8350/*ARGSUSED*/
8351 static void
8352f_complete(argvars, rettv)
8353 typval_T *argvars;
8354 typval_T *rettv;
8355{
8356 int startcol;
8357
8358 if ((State & INSERT) == 0)
8359 {
8360 EMSG(_("E785: complete() can only be used in Insert mode"));
8361 return;
8362 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008363
8364 /* Check for undo allowed here, because if something was already inserted
8365 * the line was already saved for undo and this check isn't done. */
8366 if (!undo_allowed())
8367 return;
8368
Bram Moolenaarade00832006-03-10 21:46:58 +00008369 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8370 {
8371 EMSG(_(e_invarg));
8372 return;
8373 }
8374
8375 startcol = get_tv_number_chk(&argvars[0], NULL);
8376 if (startcol <= 0)
8377 return;
8378
8379 set_completion(startcol - 1, argvars[1].vval.v_list);
8380}
8381
8382/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008383 * "complete_add()" function
8384 */
8385/*ARGSUSED*/
8386 static void
8387f_complete_add(argvars, rettv)
8388 typval_T *argvars;
8389 typval_T *rettv;
8390{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008391 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008392}
8393
8394/*
8395 * "complete_check()" function
8396 */
8397/*ARGSUSED*/
8398 static void
8399f_complete_check(argvars, rettv)
8400 typval_T *argvars;
8401 typval_T *rettv;
8402{
8403 int saved = RedrawingDisabled;
8404
8405 RedrawingDisabled = 0;
8406 ins_compl_check_keys(0);
8407 rettv->vval.v_number = compl_interrupted;
8408 RedrawingDisabled = saved;
8409}
8410#endif
8411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412/*
8413 * "confirm(message, buttons[, default [, type]])" function
8414 */
8415/*ARGSUSED*/
8416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 typval_T *argvars;
8419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8422 char_u *message;
8423 char_u *buttons = NULL;
8424 char_u buf[NUMBUFLEN];
8425 char_u buf2[NUMBUFLEN];
8426 int def = 1;
8427 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 char_u *typestr;
8429 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 message = get_tv_string_chk(&argvars[0]);
8432 if (message == NULL)
8433 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008436 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8437 if (buttons == NULL)
8438 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008442 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008444 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8445 if (typestr == NULL)
8446 error = TRUE;
8447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008449 switch (TOUPPER_ASC(*typestr))
8450 {
8451 case 'E': type = VIM_ERROR; break;
8452 case 'Q': type = VIM_QUESTION; break;
8453 case 'I': type = VIM_INFO; break;
8454 case 'W': type = VIM_WARNING; break;
8455 case 'G': type = VIM_GENERIC; break;
8456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 }
8458 }
8459 }
8460 }
8461
8462 if (buttons == NULL || *buttons == NUL)
8463 buttons = (char_u *)_("&Ok");
8464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 if (error)
8466 rettv->vval.v_number = 0;
8467 else
8468 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 def, NULL);
8470#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#endif
8473}
8474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475/*
8476 * "copy()" function
8477 */
8478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008479f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008480 typval_T *argvars;
8481 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008483 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485
8486/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008487 * "count()" function
8488 */
8489 static void
8490f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008493{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008494 long n = 0;
8495 int ic = FALSE;
8496
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008498 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 listitem_T *li;
8500 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008502
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 if ((l = argvars[0].vval.v_list) != NULL)
8504 {
8505 li = l->lv_first;
8506 if (argvars[2].v_type != VAR_UNKNOWN)
8507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 int error = FALSE;
8509
8510 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511 if (argvars[3].v_type != VAR_UNKNOWN)
8512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 idx = get_tv_number_chk(&argvars[3], &error);
8514 if (!error)
8515 {
8516 li = list_find(l, idx);
8517 if (li == NULL)
8518 EMSGN(_(e_listidx), idx);
8519 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008521 if (error)
8522 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 }
8524
8525 for ( ; li != NULL; li = li->li_next)
8526 if (tv_equal(&li->li_tv, &argvars[1], ic))
8527 ++n;
8528 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008530 else if (argvars[0].v_type == VAR_DICT)
8531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 int todo;
8533 dict_T *d;
8534 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008536 if ((d = argvars[0].vval.v_dict) != NULL)
8537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 int error = FALSE;
8539
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 if (argvars[2].v_type != VAR_UNKNOWN)
8541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008542 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 if (argvars[3].v_type != VAR_UNKNOWN)
8544 EMSG(_(e_invarg));
8545 }
8546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008547 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008549 {
8550 if (!HASHITEM_EMPTY(hi))
8551 {
8552 --todo;
8553 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8554 ++n;
8555 }
8556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 }
8558 }
8559 else
8560 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008561 rettv->vval.v_number = n;
8562}
8563
8564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8566 *
8567 * Checks the existence of a cscope connection.
8568 */
8569/*ARGSUSED*/
8570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575#ifdef FEAT_CSCOPE
8576 int num = 0;
8577 char_u *dbpath = NULL;
8578 char_u *prepend = NULL;
8579 char_u buf[NUMBUFLEN];
8580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 if (argvars[0].v_type != VAR_UNKNOWN
8582 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 num = (int)get_tv_number(&argvars[0]);
8585 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#endif
8594}
8595
8596/*
8597 * "cursor(lnum, col)" function
8598 *
8599 * Moves the cursor to the specified line and column
8600 */
8601/*ARGSUSED*/
8602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *argvars;
8605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008608#ifdef FEAT_VIRTUALEDIT
8609 long coladd = 0;
8610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaara5525202006-03-02 22:52:09 +00008612 if (argvars[1].v_type == VAR_UNKNOWN)
8613 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008614 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008615
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008617 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 line = pos.lnum;
8619 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008621 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008622#endif
8623 }
8624 else
8625 {
8626 line = get_tv_lnum(argvars);
8627 col = get_tv_number_chk(&argvars[1], NULL);
8628#ifdef FEAT_VIRTUALEDIT
8629 if (argvars[2].v_type != VAR_UNKNOWN)
8630 coladd = get_tv_number_chk(&argvars[2], NULL);
8631#endif
8632 }
8633 if (line < 0 || col < 0
8634#ifdef FEAT_VIRTUALEDIT
8635 || coladd < 0
8636#endif
8637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (line > 0)
8640 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (col > 0)
8642 curwin->w_cursor.col = col - 1;
8643#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008644 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#endif
8646
8647 /* Make sure the cursor is in a valid position. */
8648 check_cursor();
8649#ifdef FEAT_MBYTE
8650 /* Correct cursor for multi-byte character. */
8651 if (has_mbyte)
8652 mb_adjust_cursor();
8653#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008654
8655 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657
8658/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 */
8661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 typval_T *argvars;
8664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 int noref = 0;
8667
8668 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008669 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008670 if (noref < 0 || noref > 1)
8671 EMSG(_(e_invarg));
8672 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008673 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676/*
8677 * "delete()" function
8678 */
8679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688}
8689
8690/*
8691 * "did_filetype()" function
8692 */
8693/*ARGSUSED*/
8694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *argvars;
8697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#endif
8704}
8705
8706/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008707 * "diff_filler()" function
8708 */
8709/*ARGSUSED*/
8710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 typval_T *argvars;
8713 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008714{
8715#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008717#endif
8718}
8719
8720/*
8721 * "diff_hlID()" function
8722 */
8723/*ARGSUSED*/
8724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008728{
8729#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008731 static linenr_T prev_lnum = 0;
8732 static int changedtick = 0;
8733 static int fnum = 0;
8734 static int change_start = 0;
8735 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008736 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008737 int filler_lines;
8738 int col;
8739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 if (lnum < 0) /* ignore type error in {lnum} arg */
8741 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008742 if (lnum != prev_lnum
8743 || changedtick != curbuf->b_changedtick
8744 || fnum != curbuf->b_fnum)
8745 {
8746 /* New line, buffer, change: need to get the values. */
8747 filler_lines = diff_check(curwin, lnum);
8748 if (filler_lines < 0)
8749 {
8750 if (filler_lines == -1)
8751 {
8752 change_start = MAXCOL;
8753 change_end = -1;
8754 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8755 hlID = HLF_ADD; /* added line */
8756 else
8757 hlID = HLF_CHD; /* changed line */
8758 }
8759 else
8760 hlID = HLF_ADD; /* added line */
8761 }
8762 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008763 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008764 prev_lnum = lnum;
8765 changedtick = curbuf->b_changedtick;
8766 fnum = curbuf->b_fnum;
8767 }
8768
8769 if (hlID == HLF_CHD || hlID == HLF_TXD)
8770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008772 if (col >= change_start && col <= change_end)
8773 hlID = HLF_TXD; /* changed text */
8774 else
8775 hlID = HLF_CHD; /* changed line */
8776 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008777 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008778#endif
8779}
8780
8781/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008782 * "empty({expr})" function
8783 */
8784 static void
8785f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *argvars;
8787 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008788{
8789 int n;
8790
8791 switch (argvars[0].v_type)
8792 {
8793 case VAR_STRING:
8794 case VAR_FUNC:
8795 n = argvars[0].vval.v_string == NULL
8796 || *argvars[0].vval.v_string == NUL;
8797 break;
8798 case VAR_NUMBER:
8799 n = argvars[0].vval.v_number == 0;
8800 break;
8801 case VAR_LIST:
8802 n = argvars[0].vval.v_list == NULL
8803 || argvars[0].vval.v_list->lv_first == NULL;
8804 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008805 case VAR_DICT:
8806 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008807 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008808 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008809 default:
8810 EMSG2(_(e_intern2), "f_empty()");
8811 n = 0;
8812 }
8813
8814 rettv->vval.v_number = n;
8815}
8816
8817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 * "escape({string}, {chars})" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 char_u buf[NUMBUFLEN];
8826
Bram Moolenaar758711c2005-02-02 23:11:38 +00008827 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8828 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
8831
8832/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008833 * "eval()" function
8834 */
8835/*ARGSUSED*/
8836 static void
8837f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 typval_T *argvars;
8839 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008840{
8841 char_u *s;
8842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008843 s = get_tv_string_chk(&argvars[0]);
8844 if (s != NULL)
8845 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8848 {
8849 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008850 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008852 else if (*s != NUL)
8853 EMSG(_(e_trailing));
8854}
8855
8856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 * "eventhandler()" function
8858 */
8859/*ARGSUSED*/
8860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 typval_T *argvars;
8863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866}
8867
8868/*
8869 * "executable()" function
8870 */
8871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 typval_T *argvars;
8874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877}
8878
8879/*
8880 * "exists()" function
8881 */
8882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 typval_T *argvars;
8885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
8887 char_u *p;
8888 char_u *name;
8889 int n = FALSE;
8890 int len = 0;
8891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 if (*p == '$') /* environment variable */
8894 {
8895 /* first try "normal" environment variables (fast) */
8896 if (mch_getenv(p + 1) != NULL)
8897 n = TRUE;
8898 else
8899 {
8900 /* try expanding things like $VIM and ${HOME} */
8901 p = expand_env_save(p);
8902 if (p != NULL && *p != '$')
8903 n = TRUE;
8904 vim_free(p);
8905 }
8906 }
8907 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008910 if (*skipwhite(p) != NUL)
8911 n = FALSE; /* trailing garbage */
8912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 else if (*p == '*') /* internal or user defined function */
8914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008915 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 }
8917 else if (*p == ':')
8918 {
8919 n = cmd_exists(p + 1);
8920 }
8921 else if (*p == '#')
8922 {
8923#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008924 if (p[1] == '#')
8925 n = autocmd_supported(p + 2);
8926 else
8927 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#endif
8929 }
8930 else /* internal variable */
8931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008932 char_u *tofree;
8933 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008935 /* get_name_len() takes care of expanding curly braces */
8936 name = p;
8937 len = get_name_len(&p, &tofree, TRUE, FALSE);
8938 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008940 if (tofree != NULL)
8941 name = tofree;
8942 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8943 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008945 /* handle d.key, l[idx], f(expr) */
8946 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8947 if (n)
8948 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 }
8950 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008951 if (*p != NUL)
8952 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008954 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 }
8956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960/*
8961 * "expand()" function
8962 */
8963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967{
8968 char_u *s;
8969 int len;
8970 char_u *errormsg;
8971 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8972 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->v_type = VAR_STRING;
8976 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 if (*s == '%' || *s == '#' || *s == '<')
8978 {
8979 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008980 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 --emsg_off;
8982 }
8983 else
8984 {
8985 /* When the optional second argument is non-zero, don't remove matches
8986 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 if (argvars[1].v_type != VAR_UNKNOWN
8988 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (!error)
8991 {
8992 ExpandInit(&xpc);
8993 xpc.xp_context = EXPAND_FILES;
8994 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995 }
8996 else
8997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 }
8999}
9000
9001/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009002 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 */
9005 static void
9006f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 list_T *l1, *l2;
9014 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009017
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 l1 = argvars[0].vval.v_list;
9019 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009020 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9021 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009022 {
9023 if (argvars[2].v_type != VAR_UNKNOWN)
9024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 before = get_tv_number_chk(&argvars[2], &error);
9026 if (error)
9027 return; /* type error; errmsg already given */
9028
Bram Moolenaar758711c2005-02-02 23:11:38 +00009029 if (before == l1->lv_len)
9030 item = NULL;
9031 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009033 item = list_find(l1, before);
9034 if (item == NULL)
9035 {
9036 EMSGN(_(e_listidx), before);
9037 return;
9038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 }
9040 }
9041 else
9042 item = NULL;
9043 list_extend(l1, l2, item);
9044
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 copy_tv(&argvars[0], rettv);
9046 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9049 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 dict_T *d1, *d2;
9051 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052 char_u *action;
9053 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009055 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056
9057 d1 = argvars[0].vval.v_dict;
9058 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009059 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9060 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009061 {
9062 /* Check the third argument. */
9063 if (argvars[2].v_type != VAR_UNKNOWN)
9064 {
9065 static char *(av[]) = {"keep", "force", "error"};
9066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 action = get_tv_string_chk(&argvars[2]);
9068 if (action == NULL)
9069 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009070 for (i = 0; i < 3; ++i)
9071 if (STRCMP(action, av[i]) == 0)
9072 break;
9073 if (i == 3)
9074 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009075 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009076 return;
9077 }
9078 }
9079 else
9080 action = (char_u *)"force";
9081
9082 /* Go over all entries in the second dict and add them to the
9083 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009084 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009087 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009089 --todo;
9090 di1 = dict_find(d1, hi2->hi_key, -1);
9091 if (di1 == NULL)
9092 {
9093 di1 = dictitem_copy(HI2DI(hi2));
9094 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9095 dictitem_free(di1);
9096 }
9097 else if (*action == 'e')
9098 {
9099 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9100 break;
9101 }
9102 else if (*action == 'f')
9103 {
9104 clear_tv(&di1->di_tv);
9105 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009107 }
9108 }
9109
Bram Moolenaare9a41262005-01-15 22:18:47 +00009110 copy_tv(&argvars[0], rettv);
9111 }
9112 }
9113 else
9114 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009115}
9116
9117/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009118 * "feedkeys()" function
9119 */
9120/*ARGSUSED*/
9121 static void
9122f_feedkeys(argvars, rettv)
9123 typval_T *argvars;
9124 typval_T *rettv;
9125{
9126 int remap = TRUE;
9127 char_u *keys, *flags;
9128 char_u nbuf[NUMBUFLEN];
9129 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009130 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009131
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009132 /* This is not allowed in the sandbox. If the commands would still be
9133 * executed in the sandbox it would be OK, but it probably happens later,
9134 * when "sandbox" is no longer set. */
9135 if (check_secure())
9136 return;
9137
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009138 rettv->vval.v_number = 0;
9139 keys = get_tv_string(&argvars[0]);
9140 if (*keys != NUL)
9141 {
9142 if (argvars[1].v_type != VAR_UNKNOWN)
9143 {
9144 flags = get_tv_string_buf(&argvars[1], nbuf);
9145 for ( ; *flags != NUL; ++flags)
9146 {
9147 switch (*flags)
9148 {
9149 case 'n': remap = FALSE; break;
9150 case 'm': remap = TRUE; break;
9151 case 't': typed = TRUE; break;
9152 }
9153 }
9154 }
9155
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009156 /* Need to escape K_SPECIAL and CSI before putting the string in the
9157 * typeahead buffer. */
9158 keys_esc = vim_strsave_escape_csi(keys);
9159 if (keys_esc != NULL)
9160 {
9161 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009162 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009163 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009164 if (vgetc_busy)
9165 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009166 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009167 }
9168}
9169
9170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 * "filereadable()" function
9172 */
9173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009175 typval_T *argvars;
9176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 FILE *fd;
9179 char_u *p;
9180 int n;
9181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9184 {
9185 n = TRUE;
9186 fclose(fd);
9187 }
9188 else
9189 n = FALSE;
9190
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009195 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 * rights to write into.
9197 */
9198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009200 typval_T *argvars;
9201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009203 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204}
9205
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009206static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009207
9208 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009209findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009212 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009213{
9214#ifdef FEAT_SEARCHPATH
9215 char_u *fname;
9216 char_u *fresult = NULL;
9217 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9218 char_u *p;
9219 char_u pathbuf[NUMBUFLEN];
9220 int count = 1;
9221 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009222 int error = FALSE;
9223#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009224
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009225 rettv->vval.v_string = NULL;
9226 rettv->v_type = VAR_STRING;
9227
9228#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009231 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9234 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009235 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 else
9237 {
9238 if (*p != NUL)
9239 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009242 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009244 }
9245
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009246 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9247 error = TRUE;
9248
9249 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 do
9252 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009253 if (rettv->v_type == VAR_STRING)
9254 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 fresult = find_file_in_path_option(first ? fname : NULL,
9256 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009257 0, first, path,
9258 find_what,
9259 curbuf->b_ffname,
9260 find_what == FINDFILE_DIR
9261 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009263
9264 if (fresult != NULL && rettv->v_type == VAR_LIST)
9265 list_append_string(rettv->vval.v_list, fresult, -1);
9266
9267 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009269
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009270 if (rettv->v_type == VAR_STRING)
9271 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009272#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009273}
9274
Bram Moolenaar33570922005-01-25 22:26:29 +00009275static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9276static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009277
9278/*
9279 * Implementation of map() and filter().
9280 */
9281 static void
9282filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *argvars;
9284 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009285 int map;
9286{
9287 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 listitem_T *li, *nli;
9290 list_T *l = NULL;
9291 dictitem_T *di;
9292 hashtab_T *ht;
9293 hashitem_T *hi;
9294 dict_T *d = NULL;
9295 typval_T save_val;
9296 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009298 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009299 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009300 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009301
9302 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009304 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009305 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009306 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 return;
9308 }
9309 else if (argvars[0].v_type == VAR_DICT)
9310 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009311 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009312 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009313 return;
9314 }
9315 else
9316 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009317 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009318 return;
9319 }
9320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 expr = get_tv_string_buf_chk(&argvars[1], buf);
9322 /* On type errors, the preceding call has already displayed an error
9323 * message. Avoid a misleading error message for an empty string that
9324 * was not passed as argument. */
9325 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 prepare_vimvar(VV_VAL, &save_val);
9328 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009329
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009330 /* We reset "did_emsg" to be able to detect whether an error
9331 * occurred during evaluation of the expression. */
9332 save_did_emsg = did_emsg;
9333 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 prepare_vimvar(VV_KEY, &save_key);
9338 vimvars[VV_KEY].vv_type = VAR_STRING;
9339
9340 ht = &d->dv_hashtab;
9341 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009342 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 if (!HASHITEM_EMPTY(hi))
9346 {
9347 --todo;
9348 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009349 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009350 break;
9351 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009352 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009353 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 break;
9355 if (!map && rem)
9356 dictitem_remove(d, di);
9357 clear_tv(&vimvars[VV_KEY].vv_tv);
9358 }
9359 }
9360 hash_unlock(ht);
9361
9362 restore_vimvar(VV_KEY, &save_key);
9363 }
9364 else
9365 {
9366 for (li = l->lv_first; li != NULL; li = nli)
9367 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009368 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009369 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009370 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009371 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009372 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009373 break;
9374 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009375 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009376 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009380
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009381 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009383
9384 copy_tv(&argvars[0], rettv);
9385}
9386
9387 static int
9388filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009389 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390 char_u *expr;
9391 int map;
9392 int *remp;
9393{
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009396 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397
Bram Moolenaar33570922005-01-25 22:26:29 +00009398 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399 s = expr;
9400 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009401 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 if (*s != NUL) /* check for trailing chars after expr */
9403 {
9404 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009405 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009406 }
9407 if (map)
9408 {
9409 /* map(): replace the list item value */
9410 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009411 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 *tv = rettv;
9413 }
9414 else
9415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009416 int error = FALSE;
9417
Bram Moolenaare9a41262005-01-15 22:18:47 +00009418 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009420 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 /* On type error, nothing has been removed; return FAIL to stop the
9422 * loop. The error message was given by get_tv_number_chk(). */
9423 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009424 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009425 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009426 retval = OK;
9427theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009429 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009430}
9431
9432/*
9433 * "filter()" function
9434 */
9435 static void
9436f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009439{
9440 filter_map(argvars, rettv, FALSE);
9441}
9442
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009443/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009444 * "finddir({fname}[, {path}[, {count}]])" function
9445 */
9446 static void
9447f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009450{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009451 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452}
9453
9454/*
9455 * "findfile({fname}[, {path}[, {count}]])" function
9456 */
9457 static void
9458f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009461{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009462 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463}
9464
9465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 * "fnamemodify({fname}, {mods})" function
9467 */
9468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009470 typval_T *argvars;
9471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 char_u *fname;
9474 char_u *mods;
9475 int usedlen = 0;
9476 int len;
9477 char_u *fbuf = NULL;
9478 char_u buf[NUMBUFLEN];
9479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 fname = get_tv_string_chk(&argvars[0]);
9481 mods = get_tv_string_buf_chk(&argvars[1], buf);
9482 if (fname == NULL || mods == NULL)
9483 fname = NULL;
9484 else
9485 {
9486 len = (int)STRLEN(fname);
9487 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 vim_free(fbuf);
9496}
9497
Bram Moolenaar33570922005-01-25 22:26:29 +00009498static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
9500/*
9501 * "foldclosed()" function
9502 */
9503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009505 typval_T *argvars;
9506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 int end;
9508{
9509#ifdef FEAT_FOLDING
9510 linenr_T lnum;
9511 linenr_T first, last;
9512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9515 {
9516 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9517 {
9518 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 return;
9523 }
9524 }
9525#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527}
9528
9529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009530 * "foldclosed()" function
9531 */
9532 static void
9533f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009534 typval_T *argvars;
9535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536{
9537 foldclosed_both(argvars, rettv, FALSE);
9538}
9539
9540/*
9541 * "foldclosedend()" function
9542 */
9543 static void
9544f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009545 typval_T *argvars;
9546 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009547{
9548 foldclosed_both(argvars, rettv, TRUE);
9549}
9550
9551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 * "foldlevel()" function
9553 */
9554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 typval_T *argvars;
9557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559#ifdef FEAT_FOLDING
9560 linenr_T lnum;
9561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 else
9566#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568}
9569
9570/*
9571 * "foldtext()" function
9572 */
9573/*ARGSUSED*/
9574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579#ifdef FEAT_FOLDING
9580 linenr_T lnum;
9581 char_u *s;
9582 char_u *r;
9583 int len;
9584 char *txt;
9585#endif
9586
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 rettv->v_type = VAR_STRING;
9588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009590 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9591 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9592 <= curbuf->b_ml.ml_line_count
9593 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
9595 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9597 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 {
9599 if (!linewhite(lnum))
9600 break;
9601 ++lnum;
9602 }
9603
9604 /* Find interesting text in this line. */
9605 s = skipwhite(ml_get(lnum));
9606 /* skip C comment-start */
9607 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009610 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009612 {
9613 s = skipwhite(ml_get(lnum + 1));
9614 if (*s == '*')
9615 s = skipwhite(s + 1);
9616 }
9617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 txt = _("+-%s%3ld lines: ");
9619 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 + 20 /* for %3ld */
9622 + STRLEN(s))); /* concatenated */
9623 if (r != NULL)
9624 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9626 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9627 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 len = (int)STRLEN(r);
9629 STRCAT(r, s);
9630 /* remove 'foldmarker' and 'commentstring' */
9631 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 }
9634 }
9635#endif
9636}
9637
9638/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009639 * "foldtextresult(lnum)" function
9640 */
9641/*ARGSUSED*/
9642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009644 typval_T *argvars;
9645 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009646{
9647#ifdef FEAT_FOLDING
9648 linenr_T lnum;
9649 char_u *text;
9650 char_u buf[51];
9651 foldinfo_T foldinfo;
9652 int fold_count;
9653#endif
9654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->v_type = VAR_STRING;
9656 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009657#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 /* treat illegal types and illegal string values for {lnum} the same */
9660 if (lnum < 0)
9661 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009662 fold_count = foldedCount(curwin, lnum, &foldinfo);
9663 if (fold_count > 0)
9664 {
9665 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9666 &foldinfo, buf);
9667 if (text == buf)
9668 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009670 }
9671#endif
9672}
9673
9674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 * "foreground()" function
9676 */
9677/*ARGSUSED*/
9678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684#ifdef FEAT_GUI
9685 if (gui.in_use)
9686 gui_mch_set_foreground();
9687#else
9688# ifdef WIN32
9689 win32_set_foreground();
9690# endif
9691#endif
9692}
9693
9694/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695 * "function()" function
9696 */
9697/*ARGSUSED*/
9698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 typval_T *argvars;
9701 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702{
9703 char_u *s;
9704
Bram Moolenaara7043832005-01-21 11:56:39 +00009705 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009707 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009708 EMSG2(_(e_invarg2), s);
9709 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009710 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009711 else
9712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_string = vim_strsave(s);
9714 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009715 }
9716}
9717
9718/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009719 * "garbagecollect()" function
9720 */
9721/*ARGSUSED*/
9722 static void
9723f_garbagecollect(argvars, rettv)
9724 typval_T *argvars;
9725 typval_T *rettv;
9726{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009727 /* This is postponed until we are back at the toplevel, because we may be
9728 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9729 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009730
9731 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9732 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009733}
9734
9735/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009736 * "get()" function
9737 */
9738 static void
9739f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009740 typval_T *argvars;
9741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009742{
Bram Moolenaar33570922005-01-25 22:26:29 +00009743 listitem_T *li;
9744 list_T *l;
9745 dictitem_T *di;
9746 dict_T *d;
9747 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009748
Bram Moolenaare9a41262005-01-15 22:18:47 +00009749 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009750 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009751 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009753 int error = FALSE;
9754
9755 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9756 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009757 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009758 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009760 else if (argvars[0].v_type == VAR_DICT)
9761 {
9762 if ((d = argvars[0].vval.v_dict) != NULL)
9763 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009764 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009765 if (di != NULL)
9766 tv = &di->di_tv;
9767 }
9768 }
9769 else
9770 EMSG2(_(e_listdictarg), "get()");
9771
9772 if (tv == NULL)
9773 {
9774 if (argvars[2].v_type == VAR_UNKNOWN)
9775 rettv->vval.v_number = 0;
9776 else
9777 copy_tv(&argvars[2], rettv);
9778 }
9779 else
9780 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009781}
9782
Bram Moolenaar342337a2005-07-21 21:11:17 +00009783static 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 +00009784
9785/*
9786 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009787 * Return a range (from start to end) of lines in rettv from the specified
9788 * buffer.
9789 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009790 */
9791 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009792get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009793 buf_T *buf;
9794 linenr_T start;
9795 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009796 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009797 typval_T *rettv;
9798{
9799 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009800
Bram Moolenaar342337a2005-07-21 21:11:17 +00009801 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009803 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009804 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009805 }
9806 else
9807 rettv->vval.v_number = 0;
9808
9809 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9810 return;
9811
9812 if (!retlist)
9813 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009814 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9815 p = ml_get_buf(buf, start, FALSE);
9816 else
9817 p = (char_u *)"";
9818
9819 rettv->v_type = VAR_STRING;
9820 rettv->vval.v_string = vim_strsave(p);
9821 }
9822 else
9823 {
9824 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009825 return;
9826
9827 if (start < 1)
9828 start = 1;
9829 if (end > buf->b_ml.ml_line_count)
9830 end = buf->b_ml.ml_line_count;
9831 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009832 if (list_append_string(rettv->vval.v_list,
9833 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009834 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009835 }
9836}
9837
9838/*
9839 * "getbufline()" function
9840 */
9841 static void
9842f_getbufline(argvars, rettv)
9843 typval_T *argvars;
9844 typval_T *rettv;
9845{
9846 linenr_T lnum;
9847 linenr_T end;
9848 buf_T *buf;
9849
9850 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9851 ++emsg_off;
9852 buf = get_buf_tv(&argvars[0]);
9853 --emsg_off;
9854
Bram Moolenaar661b1822005-07-28 22:36:45 +00009855 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009856 if (argvars[2].v_type == VAR_UNKNOWN)
9857 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009858 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009859 end = get_tv_lnum_buf(&argvars[2], buf);
9860
Bram Moolenaar342337a2005-07-21 21:11:17 +00009861 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009862}
9863
Bram Moolenaar0d660222005-01-07 21:51:51 +00009864/*
9865 * "getbufvar()" function
9866 */
9867 static void
9868f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009871{
9872 buf_T *buf;
9873 buf_T *save_curbuf;
9874 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9878 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009879 ++emsg_off;
9880 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009881
9882 rettv->v_type = VAR_STRING;
9883 rettv->vval.v_string = NULL;
9884
9885 if (buf != NULL && varname != NULL)
9886 {
9887 if (*varname == '&') /* buffer-local-option */
9888 {
9889 /* set curbuf to be our buf, temporarily */
9890 save_curbuf = curbuf;
9891 curbuf = buf;
9892
9893 get_option_tv(&varname, rettv, TRUE);
9894
9895 /* restore previous notion of curbuf */
9896 curbuf = save_curbuf;
9897 }
9898 else
9899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 if (*varname == NUL)
9901 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9902 * scope prefix before the NUL byte is required by
9903 * find_var_in_ht(). */
9904 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009905 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009906 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009907 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009909 }
9910 }
9911
9912 --emsg_off;
9913}
9914
9915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 * "getchar()" function
9917 */
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T *argvars;
9921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009926 /* Position the cursor. Needed after a message that ends in a space. */
9927 windgoto(msg_row, msg_col);
9928
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 ++no_mapping;
9930 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009931 for (;;)
9932 {
9933 if (argvars[0].v_type == VAR_UNKNOWN)
9934 /* getchar(): blocking wait. */
9935 n = safe_vgetc();
9936 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9937 /* getchar(1): only check if char avail */
9938 n = vpeekc();
9939 else if (error || vpeekc() == NUL)
9940 /* illegal argument or getchar(0) and no char avail: return zero */
9941 n = 0;
9942 else
9943 /* getchar(0) and char avail: return char */
9944 n = safe_vgetc();
9945 if (n == K_IGNORE)
9946 continue;
9947 break;
9948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 --no_mapping;
9950 --allow_keys;
9951
Bram Moolenaar219b8702006-11-01 14:32:36 +00009952 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9953 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9954 vimvars[VV_MOUSE_COL].vv_nr = 0;
9955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 if (IS_SPECIAL(n) || mod_mask != 0)
9958 {
9959 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9960 int i = 0;
9961
9962 /* Turn a special key into three bytes, plus modifier. */
9963 if (mod_mask != 0)
9964 {
9965 temp[i++] = K_SPECIAL;
9966 temp[i++] = KS_MODIFIER;
9967 temp[i++] = mod_mask;
9968 }
9969 if (IS_SPECIAL(n))
9970 {
9971 temp[i++] = K_SPECIAL;
9972 temp[i++] = K_SECOND(n);
9973 temp[i++] = K_THIRD(n);
9974 }
9975#ifdef FEAT_MBYTE
9976 else if (has_mbyte)
9977 i += (*mb_char2bytes)(n, temp + i);
9978#endif
9979 else
9980 temp[i++] = n;
9981 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->v_type = VAR_STRING;
9983 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009984
9985#ifdef FEAT_MOUSE
9986 if (n == K_LEFTMOUSE
9987 || n == K_LEFTMOUSE_NM
9988 || n == K_LEFTDRAG
9989 || n == K_LEFTRELEASE
9990 || n == K_LEFTRELEASE_NM
9991 || n == K_MIDDLEMOUSE
9992 || n == K_MIDDLEDRAG
9993 || n == K_MIDDLERELEASE
9994 || n == K_RIGHTMOUSE
9995 || n == K_RIGHTDRAG
9996 || n == K_RIGHTRELEASE
9997 || n == K_X1MOUSE
9998 || n == K_X1DRAG
9999 || n == K_X1RELEASE
10000 || n == K_X2MOUSE
10001 || n == K_X2DRAG
10002 || n == K_X2RELEASE
10003 || n == K_MOUSEDOWN
10004 || n == K_MOUSEUP)
10005 {
10006 int row = mouse_row;
10007 int col = mouse_col;
10008 win_T *win;
10009 linenr_T lnum;
10010# ifdef FEAT_WINDOWS
10011 win_T *wp;
10012# endif
10013 int n = 1;
10014
10015 if (row >= 0 && col >= 0)
10016 {
10017 /* Find the window at the mouse coordinates and compute the
10018 * text position. */
10019 win = mouse_find_win(&row, &col);
10020 (void)mouse_comp_pos(win, &row, &col, &lnum);
10021# ifdef FEAT_WINDOWS
10022 for (wp = firstwin; wp != win; wp = wp->w_next)
10023 ++n;
10024# endif
10025 vimvars[VV_MOUSE_WIN].vv_nr = n;
10026 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10027 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10028 }
10029 }
10030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032}
10033
10034/*
10035 * "getcharmod()" function
10036 */
10037/*ARGSUSED*/
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_getcharmod(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->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044}
10045
10046/*
10047 * "getcmdline()" function
10048 */
10049/*ARGSUSED*/
10050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010052 typval_T *argvars;
10053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 rettv->v_type = VAR_STRING;
10056 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
10059/*
10060 * "getcmdpos()" function
10061 */
10062/*ARGSUSED*/
10063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069}
10070
10071/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010072 * "getcmdtype()" function
10073 */
10074/*ARGSUSED*/
10075 static void
10076f_getcmdtype(argvars, rettv)
10077 typval_T *argvars;
10078 typval_T *rettv;
10079{
10080 rettv->v_type = VAR_STRING;
10081 rettv->vval.v_string = alloc(2);
10082 if (rettv->vval.v_string != NULL)
10083 {
10084 rettv->vval.v_string[0] = get_cmdline_type();
10085 rettv->vval.v_string[1] = NUL;
10086 }
10087}
10088
10089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 * "getcwd()" function
10091 */
10092/*ARGSUSED*/
10093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010095 typval_T *argvars;
10096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 char_u cwd[MAXPATHL];
10099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else
10104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010107 if (rettv->vval.v_string != NULL)
10108 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109#endif
10110 }
10111}
10112
10113/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010114 * "getfontname()" function
10115 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010116/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 typval_T *argvars;
10120 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010121{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->v_type = VAR_STRING;
10123 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010124#ifdef FEAT_GUI
10125 if (gui.in_use)
10126 {
10127 GuiFont font;
10128 char_u *name = NULL;
10129
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010130 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010131 {
10132 /* Get the "Normal" font. Either the name saved by
10133 * hl_set_font_name() or from the font ID. */
10134 font = gui.norm_font;
10135 name = hl_get_font_name();
10136 }
10137 else
10138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010140 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10141 return;
10142 font = gui_mch_get_font(name, FALSE);
10143 if (font == NOFONT)
10144 return; /* Invalid font name, return empty string. */
10145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010147 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010148 gui_mch_free_font(font);
10149 }
10150#endif
10151}
10152
10153/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010154 * "getfperm({fname})" function
10155 */
10156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010158 typval_T *argvars;
10159 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010160{
10161 char_u *fname;
10162 struct stat st;
10163 char_u *perm = NULL;
10164 char_u flags[] = "rwx";
10165 int i;
10166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010170 if (mch_stat((char *)fname, &st) >= 0)
10171 {
10172 perm = vim_strsave((char_u *)"---------");
10173 if (perm != NULL)
10174 {
10175 for (i = 0; i < 9; i++)
10176 {
10177 if (st.st_mode & (1 << (8 - i)))
10178 perm[i] = flags[i % 3];
10179 }
10180 }
10181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010183}
10184
10185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 * "getfsize({fname})" function
10187 */
10188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010190 typval_T *argvars;
10191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192{
10193 char_u *fname;
10194 struct stat st;
10195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010196 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
10200 if (mch_stat((char *)fname, &st) >= 0)
10201 {
10202 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010203 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010207
10208 /* non-perfect check for overflow */
10209 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10210 rettv->vval.v_number = -2;
10211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 }
10213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215}
10216
10217/*
10218 * "getftime({fname})" function
10219 */
10220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 typval_T *argvars;
10223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225 char_u *fname;
10226 struct stat st;
10227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229
10230 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010231 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234}
10235
10236/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010237 * "getftype({fname})" function
10238 */
10239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010241 typval_T *argvars;
10242 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010243{
10244 char_u *fname;
10245 struct stat st;
10246 char_u *type = NULL;
10247 char *t;
10248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010252 if (mch_lstat((char *)fname, &st) >= 0)
10253 {
10254#ifdef S_ISREG
10255 if (S_ISREG(st.st_mode))
10256 t = "file";
10257 else if (S_ISDIR(st.st_mode))
10258 t = "dir";
10259# ifdef S_ISLNK
10260 else if (S_ISLNK(st.st_mode))
10261 t = "link";
10262# endif
10263# ifdef S_ISBLK
10264 else if (S_ISBLK(st.st_mode))
10265 t = "bdev";
10266# endif
10267# ifdef S_ISCHR
10268 else if (S_ISCHR(st.st_mode))
10269 t = "cdev";
10270# endif
10271# ifdef S_ISFIFO
10272 else if (S_ISFIFO(st.st_mode))
10273 t = "fifo";
10274# endif
10275# ifdef S_ISSOCK
10276 else if (S_ISSOCK(st.st_mode))
10277 t = "fifo";
10278# endif
10279 else
10280 t = "other";
10281#else
10282# ifdef S_IFMT
10283 switch (st.st_mode & S_IFMT)
10284 {
10285 case S_IFREG: t = "file"; break;
10286 case S_IFDIR: t = "dir"; break;
10287# ifdef S_IFLNK
10288 case S_IFLNK: t = "link"; break;
10289# endif
10290# ifdef S_IFBLK
10291 case S_IFBLK: t = "bdev"; break;
10292# endif
10293# ifdef S_IFCHR
10294 case S_IFCHR: t = "cdev"; break;
10295# endif
10296# ifdef S_IFIFO
10297 case S_IFIFO: t = "fifo"; break;
10298# endif
10299# ifdef S_IFSOCK
10300 case S_IFSOCK: t = "socket"; break;
10301# endif
10302 default: t = "other";
10303 }
10304# else
10305 if (mch_isdir(fname))
10306 t = "dir";
10307 else
10308 t = "file";
10309# endif
10310#endif
10311 type = vim_strsave((char_u *)t);
10312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010314}
10315
10316/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010317 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010318 */
10319 static void
10320f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323{
10324 linenr_T lnum;
10325 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010326 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010327
10328 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010329 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010330 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010331 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010332 retlist = FALSE;
10333 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010334 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010335 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010337 retlist = TRUE;
10338 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010339
Bram Moolenaar342337a2005-07-21 21:11:17 +000010340 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010341}
10342
10343/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010344 * "getmatches()" function
10345 */
10346/*ARGSUSED*/
10347 static void
10348f_getmatches(argvars, rettv)
10349 typval_T *argvars;
10350 typval_T *rettv;
10351{
10352#ifdef FEAT_SEARCH_EXTRA
10353 dict_T *dict;
10354 matchitem_T *cur = curwin->w_match_head;
10355
10356 rettv->vval.v_number = 0;
10357
10358 if (rettv_list_alloc(rettv) == OK)
10359 {
10360 while (cur != NULL)
10361 {
10362 dict = dict_alloc();
10363 if (dict == NULL)
10364 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010365 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10366 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10367 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10368 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10369 list_append_dict(rettv->vval.v_list, dict);
10370 cur = cur->next;
10371 }
10372 }
10373#endif
10374}
10375
10376/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010377 * "getpos(string)" function
10378 */
10379 static void
10380f_getpos(argvars, rettv)
10381 typval_T *argvars;
10382 typval_T *rettv;
10383{
10384 pos_T *fp;
10385 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010386 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010387
10388 if (rettv_list_alloc(rettv) == OK)
10389 {
10390 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010391 fp = var2fpos(&argvars[0], TRUE, &fnum);
10392 if (fnum != -1)
10393 list_append_number(l, (varnumber_T)fnum);
10394 else
10395 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010396 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10397 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010398 list_append_number(l, (fp != NULL)
10399 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010400 : (varnumber_T)0);
10401 list_append_number(l,
10402#ifdef FEAT_VIRTUALEDIT
10403 (fp != NULL) ? (varnumber_T)fp->coladd :
10404#endif
10405 (varnumber_T)0);
10406 }
10407 else
10408 rettv->vval.v_number = FALSE;
10409}
10410
10411/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010412 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010413 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010414/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010415 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010416f_getqflist(argvars, rettv)
10417 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010418 typval_T *rettv;
10419{
10420#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010421 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010422#endif
10423
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010424 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010425#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010426 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010427 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010428 wp = NULL;
10429 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10430 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010431 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010432 if (wp == NULL)
10433 return;
10434 }
10435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010436 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010437 }
10438#endif
10439}
10440
10441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 * "getreg()" function
10443 */
10444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *argvars;
10447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
10449 char_u *strregname;
10450 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010451 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010454 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 strregname = get_tv_string_chk(&argvars[0]);
10457 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010458 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010459 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010462 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 regname = (strregname == NULL ? '"' : *strregname);
10464 if (regname == 0)
10465 regname = '"';
10466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 rettv->vval.v_string = error ? NULL :
10469 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470}
10471
10472/*
10473 * "getregtype()" function
10474 */
10475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010477 typval_T *argvars;
10478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479{
10480 char_u *strregname;
10481 int regname;
10482 char_u buf[NUMBUFLEN + 2];
10483 long reglen = 0;
10484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010485 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 {
10487 strregname = get_tv_string_chk(&argvars[0]);
10488 if (strregname == NULL) /* type error; errmsg already given */
10489 {
10490 rettv->v_type = VAR_STRING;
10491 rettv->vval.v_string = NULL;
10492 return;
10493 }
10494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 else
10496 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010497 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498
10499 regname = (strregname == NULL ? '"' : *strregname);
10500 if (regname == 0)
10501 regname = '"';
10502
10503 buf[0] = NUL;
10504 buf[1] = NUL;
10505 switch (get_reg_type(regname, &reglen))
10506 {
10507 case MLINE: buf[0] = 'V'; break;
10508 case MCHAR: buf[0] = 'v'; break;
10509#ifdef FEAT_VISUAL
10510 case MBLOCK:
10511 buf[0] = Ctrl_V;
10512 sprintf((char *)buf + 1, "%ld", reglen + 1);
10513 break;
10514#endif
10515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516 rettv->v_type = VAR_STRING;
10517 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518}
10519
10520/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010521 * "gettabwinvar()" function
10522 */
10523 static void
10524f_gettabwinvar(argvars, rettv)
10525 typval_T *argvars;
10526 typval_T *rettv;
10527{
10528 getwinvar(argvars, rettv, 1);
10529}
10530
10531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 * "getwinposx()" function
10533 */
10534/*ARGSUSED*/
10535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541#ifdef FEAT_GUI
10542 if (gui.in_use)
10543 {
10544 int x, y;
10545
10546 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 }
10549#endif
10550}
10551
10552/*
10553 * "getwinposy()" function
10554 */
10555/*ARGSUSED*/
10556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010558 typval_T *argvars;
10559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562#ifdef FEAT_GUI
10563 if (gui.in_use)
10564 {
10565 int x, y;
10566
10567 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 }
10570#endif
10571}
10572
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010573/*
10574 * Find window specifed by "vp" in tabpage "tp".
10575 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010576 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010577find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010578 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010579 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010580{
10581#ifdef FEAT_WINDOWS
10582 win_T *wp;
10583#endif
10584 int nr;
10585
10586 nr = get_tv_number_chk(vp, NULL);
10587
10588#ifdef FEAT_WINDOWS
10589 if (nr < 0)
10590 return NULL;
10591 if (nr == 0)
10592 return curwin;
10593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010594 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10595 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010596 if (--nr <= 0)
10597 break;
10598 return wp;
10599#else
10600 if (nr == 0 || nr == 1)
10601 return curwin;
10602 return NULL;
10603#endif
10604}
10605
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606/*
10607 * "getwinvar()" function
10608 */
10609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 typval_T *argvars;
10612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010614 getwinvar(argvars, rettv, 0);
10615}
10616
10617/*
10618 * getwinvar() and gettabwinvar()
10619 */
10620 static void
10621getwinvar(argvars, rettv, off)
10622 typval_T *argvars;
10623 typval_T *rettv;
10624 int off; /* 1 for gettabwinvar() */
10625{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 win_T *win, *oldcurwin;
10627 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010629 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010631#ifdef FEAT_WINDOWS
10632 if (off == 1)
10633 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10634 else
10635 tp = curtab;
10636#endif
10637 win = find_win_by_nr(&argvars[off], tp);
10638 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010639 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 rettv->v_type = VAR_STRING;
10642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643
10644 if (win != NULL && varname != NULL)
10645 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010646 /* Set curwin to be our win, temporarily. Also set curbuf, so
10647 * that we can get buffer-local options. */
10648 oldcurwin = curwin;
10649 curwin = win;
10650 curbuf = win->w_buffer;
10651
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 else
10655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010656 if (*varname == NUL)
10657 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10658 * scope prefix before the NUL byte is required by
10659 * find_var_in_ht(). */
10660 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010662 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010666
10667 /* restore previous notion of curwin */
10668 curwin = oldcurwin;
10669 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 }
10671
10672 --emsg_off;
10673}
10674
10675/*
10676 * "glob()" function
10677 */
10678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010680 typval_T *argvars;
10681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682{
10683 expand_T xpc;
10684
10685 ExpandInit(&xpc);
10686 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->v_type = VAR_STRING;
10688 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690}
10691
10692/*
10693 * "globpath()" function
10694 */
10695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *argvars;
10698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699{
10700 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 if (file == NULL)
10705 rettv->vval.v_string = NULL;
10706 else
10707 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708}
10709
10710/*
10711 * "has()" function
10712 */
10713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 int i;
10719 char_u *name;
10720 int n = FALSE;
10721 static char *(has_list[]) =
10722 {
10723#ifdef AMIGA
10724 "amiga",
10725# ifdef FEAT_ARP
10726 "arp",
10727# endif
10728#endif
10729#ifdef __BEOS__
10730 "beos",
10731#endif
10732#ifdef MSDOS
10733# ifdef DJGPP
10734 "dos32",
10735# else
10736 "dos16",
10737# endif
10738#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010739#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 "mac",
10741#endif
10742#if defined(MACOS_X_UNIX)
10743 "macunix",
10744#endif
10745#ifdef OS2
10746 "os2",
10747#endif
10748#ifdef __QNX__
10749 "qnx",
10750#endif
10751#ifdef RISCOS
10752 "riscos",
10753#endif
10754#ifdef UNIX
10755 "unix",
10756#endif
10757#ifdef VMS
10758 "vms",
10759#endif
10760#ifdef WIN16
10761 "win16",
10762#endif
10763#ifdef WIN32
10764 "win32",
10765#endif
10766#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10767 "win32unix",
10768#endif
10769#ifdef WIN64
10770 "win64",
10771#endif
10772#ifdef EBCDIC
10773 "ebcdic",
10774#endif
10775#ifndef CASE_INSENSITIVE_FILENAME
10776 "fname_case",
10777#endif
10778#ifdef FEAT_ARABIC
10779 "arabic",
10780#endif
10781#ifdef FEAT_AUTOCMD
10782 "autocmd",
10783#endif
10784#ifdef FEAT_BEVAL
10785 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010786# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10787 "balloon_multiline",
10788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789#endif
10790#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10791 "builtin_terms",
10792# ifdef ALL_BUILTIN_TCAPS
10793 "all_builtin_terms",
10794# endif
10795#endif
10796#ifdef FEAT_BYTEOFF
10797 "byte_offset",
10798#endif
10799#ifdef FEAT_CINDENT
10800 "cindent",
10801#endif
10802#ifdef FEAT_CLIENTSERVER
10803 "clientserver",
10804#endif
10805#ifdef FEAT_CLIPBOARD
10806 "clipboard",
10807#endif
10808#ifdef FEAT_CMDL_COMPL
10809 "cmdline_compl",
10810#endif
10811#ifdef FEAT_CMDHIST
10812 "cmdline_hist",
10813#endif
10814#ifdef FEAT_COMMENTS
10815 "comments",
10816#endif
10817#ifdef FEAT_CRYPT
10818 "cryptv",
10819#endif
10820#ifdef FEAT_CSCOPE
10821 "cscope",
10822#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010823#ifdef CURSOR_SHAPE
10824 "cursorshape",
10825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826#ifdef DEBUG
10827 "debug",
10828#endif
10829#ifdef FEAT_CON_DIALOG
10830 "dialog_con",
10831#endif
10832#ifdef FEAT_GUI_DIALOG
10833 "dialog_gui",
10834#endif
10835#ifdef FEAT_DIFF
10836 "diff",
10837#endif
10838#ifdef FEAT_DIGRAPHS
10839 "digraphs",
10840#endif
10841#ifdef FEAT_DND
10842 "dnd",
10843#endif
10844#ifdef FEAT_EMACS_TAGS
10845 "emacs_tags",
10846#endif
10847 "eval", /* always present, of course! */
10848#ifdef FEAT_EX_EXTRA
10849 "ex_extra",
10850#endif
10851#ifdef FEAT_SEARCH_EXTRA
10852 "extra_search",
10853#endif
10854#ifdef FEAT_FKMAP
10855 "farsi",
10856#endif
10857#ifdef FEAT_SEARCHPATH
10858 "file_in_path",
10859#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010860#if defined(UNIX) && !defined(USE_SYSTEM)
10861 "filterpipe",
10862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863#ifdef FEAT_FIND_ID
10864 "find_in_path",
10865#endif
10866#ifdef FEAT_FOLDING
10867 "folding",
10868#endif
10869#ifdef FEAT_FOOTER
10870 "footer",
10871#endif
10872#if !defined(USE_SYSTEM) && defined(UNIX)
10873 "fork",
10874#endif
10875#ifdef FEAT_GETTEXT
10876 "gettext",
10877#endif
10878#ifdef FEAT_GUI
10879 "gui",
10880#endif
10881#ifdef FEAT_GUI_ATHENA
10882# ifdef FEAT_GUI_NEXTAW
10883 "gui_neXtaw",
10884# else
10885 "gui_athena",
10886# endif
10887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888#ifdef FEAT_GUI_GTK
10889 "gui_gtk",
10890# ifdef HAVE_GTK2
10891 "gui_gtk2",
10892# endif
10893#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010894#ifdef FEAT_GUI_GNOME
10895 "gui_gnome",
10896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897#ifdef FEAT_GUI_MAC
10898 "gui_mac",
10899#endif
10900#ifdef FEAT_GUI_MOTIF
10901 "gui_motif",
10902#endif
10903#ifdef FEAT_GUI_PHOTON
10904 "gui_photon",
10905#endif
10906#ifdef FEAT_GUI_W16
10907 "gui_win16",
10908#endif
10909#ifdef FEAT_GUI_W32
10910 "gui_win32",
10911#endif
10912#ifdef FEAT_HANGULIN
10913 "hangul_input",
10914#endif
10915#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10916 "iconv",
10917#endif
10918#ifdef FEAT_INS_EXPAND
10919 "insert_expand",
10920#endif
10921#ifdef FEAT_JUMPLIST
10922 "jumplist",
10923#endif
10924#ifdef FEAT_KEYMAP
10925 "keymap",
10926#endif
10927#ifdef FEAT_LANGMAP
10928 "langmap",
10929#endif
10930#ifdef FEAT_LIBCALL
10931 "libcall",
10932#endif
10933#ifdef FEAT_LINEBREAK
10934 "linebreak",
10935#endif
10936#ifdef FEAT_LISP
10937 "lispindent",
10938#endif
10939#ifdef FEAT_LISTCMDS
10940 "listcmds",
10941#endif
10942#ifdef FEAT_LOCALMAP
10943 "localmap",
10944#endif
10945#ifdef FEAT_MENU
10946 "menu",
10947#endif
10948#ifdef FEAT_SESSION
10949 "mksession",
10950#endif
10951#ifdef FEAT_MODIFY_FNAME
10952 "modify_fname",
10953#endif
10954#ifdef FEAT_MOUSE
10955 "mouse",
10956#endif
10957#ifdef FEAT_MOUSESHAPE
10958 "mouseshape",
10959#endif
10960#if defined(UNIX) || defined(VMS)
10961# ifdef FEAT_MOUSE_DEC
10962 "mouse_dec",
10963# endif
10964# ifdef FEAT_MOUSE_GPM
10965 "mouse_gpm",
10966# endif
10967# ifdef FEAT_MOUSE_JSB
10968 "mouse_jsbterm",
10969# endif
10970# ifdef FEAT_MOUSE_NET
10971 "mouse_netterm",
10972# endif
10973# ifdef FEAT_MOUSE_PTERM
10974 "mouse_pterm",
10975# endif
10976# ifdef FEAT_MOUSE_XTERM
10977 "mouse_xterm",
10978# endif
10979#endif
10980#ifdef FEAT_MBYTE
10981 "multi_byte",
10982#endif
10983#ifdef FEAT_MBYTE_IME
10984 "multi_byte_ime",
10985#endif
10986#ifdef FEAT_MULTI_LANG
10987 "multi_lang",
10988#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010989#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010990#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010991 "mzscheme",
10992#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994#ifdef FEAT_OLE
10995 "ole",
10996#endif
10997#ifdef FEAT_OSFILETYPE
10998 "osfiletype",
10999#endif
11000#ifdef FEAT_PATH_EXTRA
11001 "path_extra",
11002#endif
11003#ifdef FEAT_PERL
11004#ifndef DYNAMIC_PERL
11005 "perl",
11006#endif
11007#endif
11008#ifdef FEAT_PYTHON
11009#ifndef DYNAMIC_PYTHON
11010 "python",
11011#endif
11012#endif
11013#ifdef FEAT_POSTSCRIPT
11014 "postscript",
11015#endif
11016#ifdef FEAT_PRINTER
11017 "printer",
11018#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011019#ifdef FEAT_PROFILE
11020 "profile",
11021#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011022#ifdef FEAT_RELTIME
11023 "reltime",
11024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025#ifdef FEAT_QUICKFIX
11026 "quickfix",
11027#endif
11028#ifdef FEAT_RIGHTLEFT
11029 "rightleft",
11030#endif
11031#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11032 "ruby",
11033#endif
11034#ifdef FEAT_SCROLLBIND
11035 "scrollbind",
11036#endif
11037#ifdef FEAT_CMDL_INFO
11038 "showcmd",
11039 "cmdline_info",
11040#endif
11041#ifdef FEAT_SIGNS
11042 "signs",
11043#endif
11044#ifdef FEAT_SMARTINDENT
11045 "smartindent",
11046#endif
11047#ifdef FEAT_SNIFF
11048 "sniff",
11049#endif
11050#ifdef FEAT_STL_OPT
11051 "statusline",
11052#endif
11053#ifdef FEAT_SUN_WORKSHOP
11054 "sun_workshop",
11055#endif
11056#ifdef FEAT_NETBEANS_INTG
11057 "netbeans_intg",
11058#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011059#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011060 "spell",
11061#endif
11062#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 "syntax",
11064#endif
11065#if defined(USE_SYSTEM) || !defined(UNIX)
11066 "system",
11067#endif
11068#ifdef FEAT_TAG_BINS
11069 "tag_binary",
11070#endif
11071#ifdef FEAT_TAG_OLDSTATIC
11072 "tag_old_static",
11073#endif
11074#ifdef FEAT_TAG_ANYWHITE
11075 "tag_any_white",
11076#endif
11077#ifdef FEAT_TCL
11078# ifndef DYNAMIC_TCL
11079 "tcl",
11080# endif
11081#endif
11082#ifdef TERMINFO
11083 "terminfo",
11084#endif
11085#ifdef FEAT_TERMRESPONSE
11086 "termresponse",
11087#endif
11088#ifdef FEAT_TEXTOBJ
11089 "textobjects",
11090#endif
11091#ifdef HAVE_TGETENT
11092 "tgetent",
11093#endif
11094#ifdef FEAT_TITLE
11095 "title",
11096#endif
11097#ifdef FEAT_TOOLBAR
11098 "toolbar",
11099#endif
11100#ifdef FEAT_USR_CMDS
11101 "user-commands", /* was accidentally included in 5.4 */
11102 "user_commands",
11103#endif
11104#ifdef FEAT_VIMINFO
11105 "viminfo",
11106#endif
11107#ifdef FEAT_VERTSPLIT
11108 "vertsplit",
11109#endif
11110#ifdef FEAT_VIRTUALEDIT
11111 "virtualedit",
11112#endif
11113#ifdef FEAT_VISUAL
11114 "visual",
11115#endif
11116#ifdef FEAT_VISUALEXTRA
11117 "visualextra",
11118#endif
11119#ifdef FEAT_VREPLACE
11120 "vreplace",
11121#endif
11122#ifdef FEAT_WILDIGN
11123 "wildignore",
11124#endif
11125#ifdef FEAT_WILDMENU
11126 "wildmenu",
11127#endif
11128#ifdef FEAT_WINDOWS
11129 "windows",
11130#endif
11131#ifdef FEAT_WAK
11132 "winaltkeys",
11133#endif
11134#ifdef FEAT_WRITEBACKUP
11135 "writebackup",
11136#endif
11137#ifdef FEAT_XIM
11138 "xim",
11139#endif
11140#ifdef FEAT_XFONTSET
11141 "xfontset",
11142#endif
11143#ifdef USE_XSMP
11144 "xsmp",
11145#endif
11146#ifdef USE_XSMP_INTERACT
11147 "xsmp_interact",
11148#endif
11149#ifdef FEAT_XCLIPBOARD
11150 "xterm_clipboard",
11151#endif
11152#ifdef FEAT_XTERM_SAVE
11153 "xterm_save",
11154#endif
11155#if defined(UNIX) && defined(FEAT_X11)
11156 "X11",
11157#endif
11158 NULL
11159 };
11160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 for (i = 0; has_list[i] != NULL; ++i)
11163 if (STRICMP(name, has_list[i]) == 0)
11164 {
11165 n = TRUE;
11166 break;
11167 }
11168
11169 if (n == FALSE)
11170 {
11171 if (STRNICMP(name, "patch", 5) == 0)
11172 n = has_patch(atoi((char *)name + 5));
11173 else if (STRICMP(name, "vim_starting") == 0)
11174 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011175#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11176 else if (STRICMP(name, "balloon_multiline") == 0)
11177 n = multiline_balloon_available();
11178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179#ifdef DYNAMIC_TCL
11180 else if (STRICMP(name, "tcl") == 0)
11181 n = tcl_enabled(FALSE);
11182#endif
11183#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11184 else if (STRICMP(name, "iconv") == 0)
11185 n = iconv_enabled(FALSE);
11186#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011187#ifdef DYNAMIC_MZSCHEME
11188 else if (STRICMP(name, "mzscheme") == 0)
11189 n = mzscheme_enabled(FALSE);
11190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191#ifdef DYNAMIC_RUBY
11192 else if (STRICMP(name, "ruby") == 0)
11193 n = ruby_enabled(FALSE);
11194#endif
11195#ifdef DYNAMIC_PYTHON
11196 else if (STRICMP(name, "python") == 0)
11197 n = python_enabled(FALSE);
11198#endif
11199#ifdef DYNAMIC_PERL
11200 else if (STRICMP(name, "perl") == 0)
11201 n = perl_enabled(FALSE);
11202#endif
11203#ifdef FEAT_GUI
11204 else if (STRICMP(name, "gui_running") == 0)
11205 n = (gui.in_use || gui.starting);
11206# ifdef FEAT_GUI_W32
11207 else if (STRICMP(name, "gui_win32s") == 0)
11208 n = gui_is_win32s();
11209# endif
11210# ifdef FEAT_BROWSE
11211 else if (STRICMP(name, "browse") == 0)
11212 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11213# endif
11214#endif
11215#ifdef FEAT_SYN_HL
11216 else if (STRICMP(name, "syntax_items") == 0)
11217 n = syntax_present(curbuf);
11218#endif
11219#if defined(WIN3264)
11220 else if (STRICMP(name, "win95") == 0)
11221 n = mch_windows95();
11222#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011223#ifdef FEAT_NETBEANS_INTG
11224 else if (STRICMP(name, "netbeans_enabled") == 0)
11225 n = usingNetbeans;
11226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 }
11228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230}
11231
11232/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233 * "has_key()" function
11234 */
11235 static void
11236f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *argvars;
11238 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239{
11240 rettv->vval.v_number = 0;
11241 if (argvars[0].v_type != VAR_DICT)
11242 {
11243 EMSG(_(e_dictreq));
11244 return;
11245 }
11246 if (argvars[0].vval.v_dict == NULL)
11247 return;
11248
11249 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011250 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011251}
11252
11253/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011254 * "haslocaldir()" function
11255 */
11256/*ARGSUSED*/
11257 static void
11258f_haslocaldir(argvars, rettv)
11259 typval_T *argvars;
11260 typval_T *rettv;
11261{
11262 rettv->vval.v_number = (curwin->w_localdir != NULL);
11263}
11264
11265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 * "hasmapto()" function
11267 */
11268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *argvars;
11271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272{
11273 char_u *name;
11274 char_u *mode;
11275 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011276 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011279 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 mode = (char_u *)"nvo";
11281 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011284 if (argvars[2].v_type != VAR_UNKNOWN)
11285 abbr = get_tv_number(&argvars[2]);
11286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287
Bram Moolenaar2c932302006-03-18 21:42:09 +000011288 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292}
11293
11294/*
11295 * "histadd()" function
11296 */
11297/*ARGSUSED*/
11298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 typval_T *argvars;
11301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
11303#ifdef FEAT_CMDHIST
11304 int histype;
11305 char_u *str;
11306 char_u buf[NUMBUFLEN];
11307#endif
11308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 if (check_restricted() || check_secure())
11311 return;
11312#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11314 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 if (histype >= 0)
11316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 if (*str != NUL)
11319 {
11320 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 return;
11323 }
11324 }
11325#endif
11326}
11327
11328/*
11329 * "histdel()" function
11330 */
11331/*ARGSUSED*/
11332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 typval_T *argvars;
11335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
11337#ifdef FEAT_CMDHIST
11338 int n;
11339 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11343 if (str == NULL)
11344 n = 0;
11345 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011348 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 else
11353 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 get_tv_string_buf(&argvars[1], buf));
11356 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359#endif
11360}
11361
11362/*
11363 * "histget()" function
11364 */
11365/*ARGSUSED*/
11366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 typval_T *argvars;
11369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
11371#ifdef FEAT_CMDHIST
11372 int type;
11373 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011376 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11377 if (str == NULL)
11378 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 {
11381 type = get_histtype(str);
11382 if (argvars[1].v_type == VAR_UNKNOWN)
11383 idx = get_history_idx(type);
11384 else
11385 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11386 /* -1 on type error */
11387 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393}
11394
11395/*
11396 * "histnr()" function
11397 */
11398/*ARGSUSED*/
11399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *argvars;
11402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
11404 int i;
11405
11406#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 char_u *history = get_tv_string_chk(&argvars[0]);
11408
11409 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 if (i >= HIST_CMD && i < HIST_COUNT)
11411 i = get_history_idx(i);
11412 else
11413#endif
11414 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417
11418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 * "highlightID(name)" function
11420 */
11421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427}
11428
11429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011430 * "highlight_exists()" function
11431 */
11432 static void
11433f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 typval_T *argvars;
11435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011436{
11437 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11438}
11439
11440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 * "hostname()" function
11442 */
11443/*ARGSUSED*/
11444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011446 typval_T *argvars;
11447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448{
11449 char_u hostname[256];
11450
11451 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->v_type = VAR_STRING;
11453 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454}
11455
11456/*
11457 * iconv() function
11458 */
11459/*ARGSUSED*/
11460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *argvars;
11463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464{
11465#ifdef FEAT_MBYTE
11466 char_u buf1[NUMBUFLEN];
11467 char_u buf2[NUMBUFLEN];
11468 char_u *from, *to, *str;
11469 vimconv_T vimconv;
11470#endif
11471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->v_type = VAR_STRING;
11473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
11475#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 str = get_tv_string(&argvars[0]);
11477 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11478 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 vimconv.vc_type = CONV_NONE;
11480 convert_setup(&vimconv, from, to);
11481
11482 /* If the encodings are equal, no conversion needed. */
11483 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487
11488 convert_setup(&vimconv, NULL, NULL);
11489 vim_free(from);
11490 vim_free(to);
11491#endif
11492}
11493
11494/*
11495 * "indent()" function
11496 */
11497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
11502 linenr_T lnum;
11503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509}
11510
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011511/*
11512 * "index()" function
11513 */
11514 static void
11515f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 typval_T *argvars;
11517 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011518{
Bram Moolenaar33570922005-01-25 22:26:29 +000011519 list_T *l;
11520 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011521 long idx = 0;
11522 int ic = FALSE;
11523
11524 rettv->vval.v_number = -1;
11525 if (argvars[0].v_type != VAR_LIST)
11526 {
11527 EMSG(_(e_listreq));
11528 return;
11529 }
11530 l = argvars[0].vval.v_list;
11531 if (l != NULL)
11532 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011533 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011534 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 int error = FALSE;
11537
Bram Moolenaar758711c2005-02-02 23:11:38 +000011538 /* Start at specified item. Use the cached index that list_find()
11539 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011540 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011541 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011542 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 ic = get_tv_number_chk(&argvars[3], &error);
11544 if (error)
11545 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011547
Bram Moolenaar758711c2005-02-02 23:11:38 +000011548 for ( ; item != NULL; item = item->li_next, ++idx)
11549 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011550 {
11551 rettv->vval.v_number = idx;
11552 break;
11553 }
11554 }
11555}
11556
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557static int inputsecret_flag = 0;
11558
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011559static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11560
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011562 * This function is used by f_input() and f_inputdialog() functions. The third
11563 * argument to f_input() specifies the type of completion to use at the
11564 * prompt. The third argument to f_inputdialog() specifies the value to return
11565 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 */
11567 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011568get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011569 typval_T *argvars;
11570 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011571 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 char_u *p = NULL;
11575 int c;
11576 char_u buf[NUMBUFLEN];
11577 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011579 int xp_type = EXPAND_NOTHING;
11580 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584
11585#ifdef NO_CONSOLE_INPUT
11586 /* While starting up, there is no place to enter text. */
11587 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589#endif
11590
11591 cmd_silent = FALSE; /* Want to see the prompt. */
11592 if (prompt != NULL)
11593 {
11594 /* Only the part of the message after the last NL is considered as
11595 * prompt for the command line */
11596 p = vim_strrchr(prompt, '\n');
11597 if (p == NULL)
11598 p = prompt;
11599 else
11600 {
11601 ++p;
11602 c = *p;
11603 *p = NUL;
11604 msg_start();
11605 msg_clr_eos();
11606 msg_puts_attr(prompt, echo_attr);
11607 msg_didout = FALSE;
11608 msg_starthere();
11609 *p = c;
11610 }
11611 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011613 if (argvars[1].v_type != VAR_UNKNOWN)
11614 {
11615 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11616 if (defstr != NULL)
11617 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011619 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011620 {
11621 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011622 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011623 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011624
Bram Moolenaar4463f292005-09-25 22:20:24 +000011625 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011626
Bram Moolenaar4463f292005-09-25 22:20:24 +000011627 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11628 if (xp_name == NULL)
11629 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011630
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011631 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011632
Bram Moolenaar4463f292005-09-25 22:20:24 +000011633 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11634 &xp_arg) == FAIL)
11635 return;
11636 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011637 }
11638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 if (defstr != NULL)
11640 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011641 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11642 xp_type, xp_arg);
11643
11644 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 /* since the user typed this, no need to wait for return */
11647 need_wait_return = FALSE;
11648 msg_didout = FALSE;
11649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 cmd_silent = cmd_silent_save;
11651}
11652
11653/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011654 * "input()" function
11655 * Also handles inputsecret() when inputsecret is set.
11656 */
11657 static void
11658f_input(argvars, rettv)
11659 typval_T *argvars;
11660 typval_T *rettv;
11661{
11662 get_user_input(argvars, rettv, FALSE);
11663}
11664
11665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 * "inputdialog()" function
11667 */
11668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011670 typval_T *argvars;
11671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672{
11673#if defined(FEAT_GUI_TEXTDIALOG)
11674 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11675 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11676 {
11677 char_u *message;
11678 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 message = get_tv_string_chk(&argvars[0]);
11682 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011683 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011684 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 else
11686 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 if (message != NULL && defstr != NULL
11688 && do_dialog(VIM_QUESTION, NULL, message,
11689 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 else
11692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 if (message != NULL && defstr != NULL
11694 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011695 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_string = vim_strsave(
11697 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 }
11703 else
11704#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011705 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706}
11707
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011708/*
11709 * "inputlist()" function
11710 */
11711 static void
11712f_inputlist(argvars, rettv)
11713 typval_T *argvars;
11714 typval_T *rettv;
11715{
11716 listitem_T *li;
11717 int selected;
11718 int mouse_used;
11719
11720 rettv->vval.v_number = 0;
11721#ifdef NO_CONSOLE_INPUT
11722 /* While starting up, there is no place to enter text. */
11723 if (no_console_input())
11724 return;
11725#endif
11726 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11727 {
11728 EMSG2(_(e_listarg), "inputlist()");
11729 return;
11730 }
11731
11732 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011733 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011734 lines_left = Rows; /* avoid more prompt */
11735 msg_scroll = TRUE;
11736 msg_clr_eos();
11737
11738 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11739 {
11740 msg_puts(get_tv_string(&li->li_tv));
11741 msg_putchar('\n');
11742 }
11743
11744 /* Ask for choice. */
11745 selected = prompt_for_number(&mouse_used);
11746 if (mouse_used)
11747 selected -= lines_left;
11748
11749 rettv->vval.v_number = selected;
11750}
11751
11752
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11754
11755/*
11756 * "inputrestore()" function
11757 */
11758/*ARGSUSED*/
11759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763{
11764 if (ga_userinput.ga_len > 0)
11765 {
11766 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11768 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 }
11771 else if (p_verbose > 1)
11772 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011773 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 }
11776}
11777
11778/*
11779 * "inputsave()" function
11780 */
11781/*ARGSUSED*/
11782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *argvars;
11785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786{
11787 /* Add an entry to the stack of typehead storage. */
11788 if (ga_grow(&ga_userinput, 1) == OK)
11789 {
11790 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11791 + ga_userinput.ga_len);
11792 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 }
11795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797}
11798
11799/*
11800 * "inputsecret()" function
11801 */
11802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806{
11807 ++cmdline_star;
11808 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 --cmdline_star;
11811 --inputsecret_flag;
11812}
11813
11814/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011815 * "insert()" function
11816 */
11817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *argvars;
11820 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011821{
11822 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011823 listitem_T *item;
11824 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011827 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011828 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011829 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011830 else if ((l = argvars[0].vval.v_list) != NULL
11831 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011832 {
11833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 before = get_tv_number_chk(&argvars[2], &error);
11835 if (error)
11836 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011837
Bram Moolenaar758711c2005-02-02 23:11:38 +000011838 if (before == l->lv_len)
11839 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011840 else
11841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011842 item = list_find(l, before);
11843 if (item == NULL)
11844 {
11845 EMSGN(_(e_listidx), before);
11846 l = NULL;
11847 }
11848 }
11849 if (l != NULL)
11850 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011851 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011852 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 }
11854 }
11855}
11856
11857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 * "isdirectory()" function
11859 */
11860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011868/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011869 * "islocked()" function
11870 */
11871 static void
11872f_islocked(argvars, rettv)
11873 typval_T *argvars;
11874 typval_T *rettv;
11875{
11876 lval_T lv;
11877 char_u *end;
11878 dictitem_T *di;
11879
11880 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011881 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11882 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011883 if (end != NULL && lv.ll_name != NULL)
11884 {
11885 if (*end != NUL)
11886 EMSG(_(e_trailing));
11887 else
11888 {
11889 if (lv.ll_tv == NULL)
11890 {
11891 if (check_changedtick(lv.ll_name))
11892 rettv->vval.v_number = 1; /* always locked */
11893 else
11894 {
11895 di = find_var(lv.ll_name, NULL);
11896 if (di != NULL)
11897 {
11898 /* Consider a variable locked when:
11899 * 1. the variable itself is locked
11900 * 2. the value of the variable is locked.
11901 * 3. the List or Dict value is locked.
11902 */
11903 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11904 || tv_islocked(&di->di_tv));
11905 }
11906 }
11907 }
11908 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011909 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011910 else if (lv.ll_newkey != NULL)
11911 EMSG2(_(e_dictkey), lv.ll_newkey);
11912 else if (lv.ll_list != NULL)
11913 /* List item. */
11914 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11915 else
11916 /* Dictionary item. */
11917 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11918 }
11919 }
11920
11921 clear_lval(&lv);
11922}
11923
Bram Moolenaar33570922005-01-25 22:26:29 +000011924static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011925
11926/*
11927 * Turn a dict into a list:
11928 * "what" == 0: list of keys
11929 * "what" == 1: list of values
11930 * "what" == 2: list of items
11931 */
11932 static void
11933dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011934 typval_T *argvars;
11935 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011936 int what;
11937{
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 list_T *l2;
11939 dictitem_T *di;
11940 hashitem_T *hi;
11941 listitem_T *li;
11942 listitem_T *li2;
11943 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011944 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945
11946 rettv->vval.v_number = 0;
11947 if (argvars[0].v_type != VAR_DICT)
11948 {
11949 EMSG(_(e_dictreq));
11950 return;
11951 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011952 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011953 return;
11954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011955 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011956 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011957
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011958 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011961 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011962 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011963 --todo;
11964 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011965
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011966 li = listitem_alloc();
11967 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011968 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011969 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011970
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011971 if (what == 0)
11972 {
11973 /* keys() */
11974 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011975 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011976 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11977 }
11978 else if (what == 1)
11979 {
11980 /* values() */
11981 copy_tv(&di->di_tv, &li->li_tv);
11982 }
11983 else
11984 {
11985 /* items() */
11986 l2 = list_alloc();
11987 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011988 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011989 li->li_tv.vval.v_list = l2;
11990 if (l2 == NULL)
11991 break;
11992 ++l2->lv_refcount;
11993
11994 li2 = listitem_alloc();
11995 if (li2 == NULL)
11996 break;
11997 list_append(l2, li2);
11998 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011999 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012000 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12001
12002 li2 = listitem_alloc();
12003 if (li2 == NULL)
12004 break;
12005 list_append(l2, li2);
12006 copy_tv(&di->di_tv, &li2->li_tv);
12007 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012008 }
12009 }
12010}
12011
12012/*
12013 * "items(dict)" function
12014 */
12015 static void
12016f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 typval_T *argvars;
12018 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012019{
12020 dict_list(argvars, rettv, 2);
12021}
12022
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012024 * "join()" function
12025 */
12026 static void
12027f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 typval_T *argvars;
12029 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030{
12031 garray_T ga;
12032 char_u *sep;
12033
12034 rettv->vval.v_number = 0;
12035 if (argvars[0].v_type != VAR_LIST)
12036 {
12037 EMSG(_(e_listreq));
12038 return;
12039 }
12040 if (argvars[0].vval.v_list == NULL)
12041 return;
12042 if (argvars[1].v_type == VAR_UNKNOWN)
12043 sep = (char_u *)" ";
12044 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012046
12047 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048
12049 if (sep != NULL)
12050 {
12051 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012052 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012053 ga_append(&ga, NUL);
12054 rettv->vval.v_string = (char_u *)ga.ga_data;
12055 }
12056 else
12057 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012058}
12059
12060/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012061 * "keys()" function
12062 */
12063 static void
12064f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012067{
12068 dict_list(argvars, rettv, 0);
12069}
12070
12071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 * "last_buffer_nr()" function.
12073 */
12074/*ARGSUSED*/
12075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012077 typval_T *argvars;
12078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079{
12080 int n = 0;
12081 buf_T *buf;
12082
12083 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12084 if (n < buf->b_fnum)
12085 n = buf->b_fnum;
12086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012088}
12089
12090/*
12091 * "len()" function
12092 */
12093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012095 typval_T *argvars;
12096 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012097{
12098 switch (argvars[0].v_type)
12099 {
12100 case VAR_STRING:
12101 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012102 rettv->vval.v_number = (varnumber_T)STRLEN(
12103 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012104 break;
12105 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012107 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108 case VAR_DICT:
12109 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12110 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012111 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012112 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012113 break;
12114 }
12115}
12116
Bram Moolenaar33570922005-01-25 22:26:29 +000012117static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012118
12119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012121 typval_T *argvars;
12122 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012123 int type;
12124{
12125#ifdef FEAT_LIBCALL
12126 char_u *string_in;
12127 char_u **string_result;
12128 int nr_result;
12129#endif
12130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012132 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012136
12137 if (check_restricted() || check_secure())
12138 return;
12139
12140#ifdef FEAT_LIBCALL
12141 /* The first two args must be strings, otherwise its meaningless */
12142 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12143 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012144 string_in = NULL;
12145 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 string_in = argvars[2].vval.v_string;
12147 if (type == VAR_NUMBER)
12148 string_result = NULL;
12149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151 if (mch_libcall(argvars[0].vval.v_string,
12152 argvars[1].vval.v_string,
12153 string_in,
12154 argvars[2].vval.v_number,
12155 string_result,
12156 &nr_result) == OK
12157 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012159 }
12160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161}
12162
12163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 * "libcall()" function
12165 */
12166 static void
12167f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012168 typval_T *argvars;
12169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170{
12171 libcall_common(argvars, rettv, VAR_STRING);
12172}
12173
12174/*
12175 * "libcallnr()" function
12176 */
12177 static void
12178f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 typval_T *argvars;
12180 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012181{
12182 libcall_common(argvars, rettv, VAR_NUMBER);
12183}
12184
12185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 * "line(string)" function
12187 */
12188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 typval_T *argvars;
12191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192{
12193 linenr_T lnum = 0;
12194 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012195 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012197 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 if (fp != NULL)
12199 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201}
12202
12203/*
12204 * "line2byte(lnum)" function
12205 */
12206/*ARGSUSED*/
12207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012209 typval_T *argvars;
12210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214#else
12215 linenr_T lnum;
12216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12222 if (rettv->vval.v_number >= 0)
12223 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224#endif
12225}
12226
12227/*
12228 * "lispindent(lnum)" function
12229 */
12230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012232 typval_T *argvars;
12233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234{
12235#ifdef FEAT_LISP
12236 pos_T pos;
12237 linenr_T lnum;
12238
12239 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12242 {
12243 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 curwin->w_cursor = pos;
12246 }
12247 else
12248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250}
12251
12252/*
12253 * "localtime()" function
12254 */
12255/*ARGSUSED*/
12256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012258 typval_T *argvars;
12259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262}
12263
Bram Moolenaar33570922005-01-25 22:26:29 +000012264static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265
12266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 typval_T *argvars;
12269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 int exact;
12271{
12272 char_u *keys;
12273 char_u *which;
12274 char_u buf[NUMBUFLEN];
12275 char_u *keys_buf = NULL;
12276 char_u *rhs;
12277 int mode;
12278 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012279 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280
12281 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282 rettv->v_type = VAR_STRING;
12283 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 if (*keys == NUL)
12287 return;
12288
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012289 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012292 if (argvars[2].v_type != VAR_UNKNOWN)
12293 abbr = get_tv_number(&argvars[2]);
12294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 else
12296 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012297 if (which == NULL)
12298 return;
12299
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 mode = get_map_mode(&which, 0);
12301
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012302 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012303 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 vim_free(keys_buf);
12305 if (rhs != NULL)
12306 {
12307 ga_init(&ga);
12308 ga.ga_itemsize = 1;
12309 ga.ga_growsize = 40;
12310
12311 while (*rhs != NUL)
12312 ga_concat(&ga, str2special(&rhs, FALSE));
12313
12314 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 }
12317}
12318
12319/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012320 * "map()" function
12321 */
12322 static void
12323f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012324 typval_T *argvars;
12325 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012326{
12327 filter_map(argvars, rettv, TRUE);
12328}
12329
12330/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 */
12333 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012334f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 typval_T *argvars;
12336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012338 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339}
12340
12341/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 */
12344 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012349 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
Bram Moolenaar33570922005-01-25 22:26:29 +000012352static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353
12354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012356 typval_T *argvars;
12357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 int type;
12359{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012360 char_u *str = NULL;
12361 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 char_u *pat;
12363 regmatch_T regmatch;
12364 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012365 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 char_u *save_cpo;
12367 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012368 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012369 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012370 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012371 list_T *l = NULL;
12372 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012373 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012374 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
12376 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12377 save_cpo = p_cpo;
12378 p_cpo = (char_u *)"";
12379
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012380 rettv->vval.v_number = -1;
12381 if (type == 3)
12382 {
12383 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012384 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012385 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012386 }
12387 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->v_type = VAR_STRING;
12390 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012393 if (argvars[0].v_type == VAR_LIST)
12394 {
12395 if ((l = argvars[0].vval.v_list) == NULL)
12396 goto theend;
12397 li = l->lv_first;
12398 }
12399 else
12400 expr = str = get_tv_string(&argvars[0]);
12401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012402 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12403 if (pat == NULL)
12404 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012405
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012408 int error = FALSE;
12409
12410 start = get_tv_number_chk(&argvars[2], &error);
12411 if (error)
12412 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012413 if (l != NULL)
12414 {
12415 li = list_find(l, start);
12416 if (li == NULL)
12417 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012418 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012419 }
12420 else
12421 {
12422 if (start < 0)
12423 start = 0;
12424 if (start > (long)STRLEN(str))
12425 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012426 /* When "count" argument is there ignore matches before "start",
12427 * otherwise skip part of the string. Differs when pattern is "^"
12428 * or "\<". */
12429 if (argvars[3].v_type != VAR_UNKNOWN)
12430 startcol = start;
12431 else
12432 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012433 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012435 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012436 nth = get_tv_number_chk(&argvars[3], &error);
12437 if (error)
12438 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 }
12440
12441 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12442 if (regmatch.regprog != NULL)
12443 {
12444 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012445
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012446 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012447 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012448 if (l != NULL)
12449 {
12450 if (li == NULL)
12451 {
12452 match = FALSE;
12453 break;
12454 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012455 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012456 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012457 if (str == NULL)
12458 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012459 }
12460
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012461 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012462
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012463 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012464 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012465 if (l == NULL && !match)
12466 break;
12467
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012468 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012469 if (l != NULL)
12470 {
12471 li = li->li_next;
12472 ++idx;
12473 }
12474 else
12475 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012476#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012477 startcol = (colnr_T)(regmatch.startp[0]
12478 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012479#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012480 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012481#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012482 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012483 }
12484
12485 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012487 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012489 int i;
12490
12491 /* return list with matched string and submatches */
12492 for (i = 0; i < NSUBEXP; ++i)
12493 {
12494 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012495 {
12496 if (list_append_string(rettv->vval.v_list,
12497 (char_u *)"", 0) == FAIL)
12498 break;
12499 }
12500 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012501 regmatch.startp[i],
12502 (int)(regmatch.endp[i] - regmatch.startp[i]))
12503 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012504 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012505 }
12506 }
12507 else if (type == 2)
12508 {
12509 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012510 if (l != NULL)
12511 copy_tv(&li->li_tv, rettv);
12512 else
12513 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012515 }
12516 else if (l != NULL)
12517 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 else
12519 {
12520 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 (varnumber_T)(regmatch.startp[0] - str);
12523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012526 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 }
12528 }
12529 vim_free(regmatch.regprog);
12530 }
12531
12532theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012533 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534 p_cpo = save_cpo;
12535}
12536
12537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538 * "match()" function
12539 */
12540 static void
12541f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 typval_T *argvars;
12543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544{
12545 find_some_match(argvars, rettv, 1);
12546}
12547
12548/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012549 * "matchadd()" function
12550 */
12551 static void
12552f_matchadd(argvars, rettv)
12553 typval_T *argvars;
12554 typval_T *rettv;
12555{
12556#ifdef FEAT_SEARCH_EXTRA
12557 char_u buf[NUMBUFLEN];
12558 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12559 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12560 int prio = 10; /* default priority */
12561 int id = -1;
12562 int error = FALSE;
12563
12564 rettv->vval.v_number = -1;
12565
12566 if (grp == NULL || pat == NULL)
12567 return;
12568 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012569 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012570 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012571 if (argvars[3].v_type != VAR_UNKNOWN)
12572 id = get_tv_number_chk(&argvars[3], &error);
12573 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012574 if (error == TRUE)
12575 return;
12576 if (id >= 1 && id <= 3)
12577 {
12578 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12579 return;
12580 }
12581
12582 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12583#endif
12584}
12585
12586/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012587 * "matcharg()" function
12588 */
12589 static void
12590f_matcharg(argvars, rettv)
12591 typval_T *argvars;
12592 typval_T *rettv;
12593{
12594 if (rettv_list_alloc(rettv) == OK)
12595 {
12596#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012597 int id = get_tv_number(&argvars[0]);
12598 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012599
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012600 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012601 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012602 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12603 {
12604 list_append_string(rettv->vval.v_list,
12605 syn_id2name(m->hlg_id), -1);
12606 list_append_string(rettv->vval.v_list, m->pattern, -1);
12607 }
12608 else
12609 {
12610 list_append_string(rettv->vval.v_list, NUL, -1);
12611 list_append_string(rettv->vval.v_list, NUL, -1);
12612 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012613 }
12614#endif
12615 }
12616}
12617
12618/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012619 * "matchdelete()" function
12620 */
12621 static void
12622f_matchdelete(argvars, rettv)
12623 typval_T *argvars;
12624 typval_T *rettv;
12625{
12626#ifdef FEAT_SEARCH_EXTRA
12627 rettv->vval.v_number = match_delete(curwin,
12628 (int)get_tv_number(&argvars[0]), TRUE);
12629#endif
12630}
12631
12632/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633 * "matchend()" function
12634 */
12635 static void
12636f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 typval_T *argvars;
12638 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012639{
12640 find_some_match(argvars, rettv, 0);
12641}
12642
12643/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012644 * "matchlist()" function
12645 */
12646 static void
12647f_matchlist(argvars, rettv)
12648 typval_T *argvars;
12649 typval_T *rettv;
12650{
12651 find_some_match(argvars, rettv, 3);
12652}
12653
12654/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012655 * "matchstr()" function
12656 */
12657 static void
12658f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012659 typval_T *argvars;
12660 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661{
12662 find_some_match(argvars, rettv, 2);
12663}
12664
Bram Moolenaar33570922005-01-25 22:26:29 +000012665static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012666
12667 static void
12668max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *argvars;
12670 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012671 int domax;
12672{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012673 long n = 0;
12674 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012675 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012676
12677 if (argvars[0].v_type == VAR_LIST)
12678 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012679 list_T *l;
12680 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012681
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012682 l = argvars[0].vval.v_list;
12683 if (l != NULL)
12684 {
12685 li = l->lv_first;
12686 if (li != NULL)
12687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012689 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012690 {
12691 li = li->li_next;
12692 if (li == NULL)
12693 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012695 if (domax ? i > n : i < n)
12696 n = i;
12697 }
12698 }
12699 }
12700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701 else if (argvars[0].v_type == VAR_DICT)
12702 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012703 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012704 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012706 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707
12708 d = argvars[0].vval.v_dict;
12709 if (d != NULL)
12710 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012711 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012714 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012716 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012718 if (first)
12719 {
12720 n = i;
12721 first = FALSE;
12722 }
12723 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012724 n = i;
12725 }
12726 }
12727 }
12728 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012729 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012730 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012732}
12733
12734/*
12735 * "max()" function
12736 */
12737 static void
12738f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012739 typval_T *argvars;
12740 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012741{
12742 max_min(argvars, rettv, TRUE);
12743}
12744
12745/*
12746 * "min()" function
12747 */
12748 static void
12749f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 typval_T *argvars;
12751 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012752{
12753 max_min(argvars, rettv, FALSE);
12754}
12755
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012756static int mkdir_recurse __ARGS((char_u *dir, int prot));
12757
12758/*
12759 * Create the directory in which "dir" is located, and higher levels when
12760 * needed.
12761 */
12762 static int
12763mkdir_recurse(dir, prot)
12764 char_u *dir;
12765 int prot;
12766{
12767 char_u *p;
12768 char_u *updir;
12769 int r = FAIL;
12770
12771 /* Get end of directory name in "dir".
12772 * We're done when it's "/" or "c:/". */
12773 p = gettail_sep(dir);
12774 if (p <= get_past_head(dir))
12775 return OK;
12776
12777 /* If the directory exists we're done. Otherwise: create it.*/
12778 updir = vim_strnsave(dir, (int)(p - dir));
12779 if (updir == NULL)
12780 return FAIL;
12781 if (mch_isdir(updir))
12782 r = OK;
12783 else if (mkdir_recurse(updir, prot) == OK)
12784 r = vim_mkdir_emsg(updir, prot);
12785 vim_free(updir);
12786 return r;
12787}
12788
12789#ifdef vim_mkdir
12790/*
12791 * "mkdir()" function
12792 */
12793 static void
12794f_mkdir(argvars, rettv)
12795 typval_T *argvars;
12796 typval_T *rettv;
12797{
12798 char_u *dir;
12799 char_u buf[NUMBUFLEN];
12800 int prot = 0755;
12801
12802 rettv->vval.v_number = FAIL;
12803 if (check_restricted() || check_secure())
12804 return;
12805
12806 dir = get_tv_string_buf(&argvars[0], buf);
12807 if (argvars[1].v_type != VAR_UNKNOWN)
12808 {
12809 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012810 prot = get_tv_number_chk(&argvars[2], NULL);
12811 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012812 mkdir_recurse(dir, prot);
12813 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012814 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012815}
12816#endif
12817
Bram Moolenaar0d660222005-01-07 21:51:51 +000012818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 * "mode()" function
12820 */
12821/*ARGSUSED*/
12822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827 char_u buf[2];
12828
12829#ifdef FEAT_VISUAL
12830 if (VIsual_active)
12831 {
12832 if (VIsual_select)
12833 buf[0] = VIsual_mode + 's' - 'v';
12834 else
12835 buf[0] = VIsual_mode;
12836 }
12837 else
12838#endif
12839 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12840 buf[0] = 'r';
12841 else if (State & INSERT)
12842 {
12843 if (State & REPLACE_FLAG)
12844 buf[0] = 'R';
12845 else
12846 buf[0] = 'i';
12847 }
12848 else if (State & CMDLINE)
12849 buf[0] = 'c';
12850 else
12851 buf[0] = 'n';
12852
12853 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_string = vim_strsave(buf);
12855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856}
12857
12858/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859 * "nextnonblank()" function
12860 */
12861 static void
12862f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *argvars;
12864 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865{
12866 linenr_T lnum;
12867
12868 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012870 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012871 {
12872 lnum = 0;
12873 break;
12874 }
12875 if (*skipwhite(ml_get(lnum)) != NUL)
12876 break;
12877 }
12878 rettv->vval.v_number = lnum;
12879}
12880
12881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 * "nr2char()" function
12883 */
12884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *argvars;
12887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
12889 char_u buf[NUMBUFLEN];
12890
12891#ifdef FEAT_MBYTE
12892 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 else
12895#endif
12896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 buf[1] = NUL;
12899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->v_type = VAR_STRING;
12901 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012902}
12903
12904/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012905 * "pathshorten()" function
12906 */
12907 static void
12908f_pathshorten(argvars, rettv)
12909 typval_T *argvars;
12910 typval_T *rettv;
12911{
12912 char_u *p;
12913
12914 rettv->v_type = VAR_STRING;
12915 p = get_tv_string_chk(&argvars[0]);
12916 if (p == NULL)
12917 rettv->vval.v_string = NULL;
12918 else
12919 {
12920 p = vim_strsave(p);
12921 rettv->vval.v_string = p;
12922 if (p != NULL)
12923 shorten_dir(p);
12924 }
12925}
12926
12927/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012928 * "prevnonblank()" function
12929 */
12930 static void
12931f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012934{
12935 linenr_T lnum;
12936
12937 lnum = get_tv_lnum(argvars);
12938 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12939 lnum = 0;
12940 else
12941 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12942 --lnum;
12943 rettv->vval.v_number = lnum;
12944}
12945
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012946#ifdef HAVE_STDARG_H
12947/* This dummy va_list is here because:
12948 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12949 * - locally in the function results in a "used before set" warning
12950 * - using va_start() to initialize it gives "function with fixed args" error */
12951static va_list ap;
12952#endif
12953
Bram Moolenaar8c711452005-01-14 21:53:12 +000012954/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012955 * "printf()" function
12956 */
12957 static void
12958f_printf(argvars, rettv)
12959 typval_T *argvars;
12960 typval_T *rettv;
12961{
12962 rettv->v_type = VAR_STRING;
12963 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012964#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012965 {
12966 char_u buf[NUMBUFLEN];
12967 int len;
12968 char_u *s;
12969 int saved_did_emsg = did_emsg;
12970 char *fmt;
12971
12972 /* Get the required length, allocate the buffer and do it for real. */
12973 did_emsg = FALSE;
12974 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012975 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012976 if (!did_emsg)
12977 {
12978 s = alloc(len + 1);
12979 if (s != NULL)
12980 {
12981 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012982 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012983 }
12984 }
12985 did_emsg |= saved_did_emsg;
12986 }
12987#endif
12988}
12989
12990/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012991 * "pumvisible()" function
12992 */
12993/*ARGSUSED*/
12994 static void
12995f_pumvisible(argvars, rettv)
12996 typval_T *argvars;
12997 typval_T *rettv;
12998{
12999 rettv->vval.v_number = 0;
13000#ifdef FEAT_INS_EXPAND
13001 if (pum_visible())
13002 rettv->vval.v_number = 1;
13003#endif
13004}
13005
13006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007 * "range()" function
13008 */
13009 static void
13010f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 typval_T *argvars;
13012 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013{
13014 long start;
13015 long end;
13016 long stride = 1;
13017 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021 if (argvars[1].v_type == VAR_UNKNOWN)
13022 {
13023 end = start - 1;
13024 start = 0;
13025 }
13026 else
13027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013031 }
13032
13033 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 if (error)
13035 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013036 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013037 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013038 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013039 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013040 else
13041 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013042 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013044 if (list_append_number(rettv->vval.v_list,
13045 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013046 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013047 }
13048}
13049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013050/*
13051 * "readfile()" function
13052 */
13053 static void
13054f_readfile(argvars, rettv)
13055 typval_T *argvars;
13056 typval_T *rettv;
13057{
13058 int binary = FALSE;
13059 char_u *fname;
13060 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013061 listitem_T *li;
13062#define FREAD_SIZE 200 /* optimized for text lines */
13063 char_u buf[FREAD_SIZE];
13064 int readlen; /* size of last fread() */
13065 int buflen; /* nr of valid chars in buf[] */
13066 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13067 int tolist; /* first byte in buf[] still to be put in list */
13068 int chop; /* how many CR to chop off */
13069 char_u *prev = NULL; /* previously read bytes, if any */
13070 int prevlen = 0; /* length of "prev" if not NULL */
13071 char_u *s;
13072 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013073 long maxline = MAXLNUM;
13074 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013076 if (argvars[1].v_type != VAR_UNKNOWN)
13077 {
13078 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13079 binary = TRUE;
13080 if (argvars[2].v_type != VAR_UNKNOWN)
13081 maxline = get_tv_number(&argvars[2]);
13082 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013084 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013085 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013086
13087 /* Always open the file in binary mode, library functions have a mind of
13088 * their own about CR-LF conversion. */
13089 fname = get_tv_string(&argvars[0]);
13090 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13091 {
13092 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13093 return;
13094 }
13095
13096 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013097 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013098 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013099 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013100 buflen = filtd + readlen;
13101 tolist = 0;
13102 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13103 {
13104 if (buf[filtd] == '\n' || readlen <= 0)
13105 {
13106 /* Only when in binary mode add an empty list item when the
13107 * last line ends in a '\n'. */
13108 if (!binary && readlen == 0 && filtd == 0)
13109 break;
13110
13111 /* Found end-of-line or end-of-file: add a text line to the
13112 * list. */
13113 chop = 0;
13114 if (!binary)
13115 while (filtd - chop - 1 >= tolist
13116 && buf[filtd - chop - 1] == '\r')
13117 ++chop;
13118 len = filtd - tolist - chop;
13119 if (prev == NULL)
13120 s = vim_strnsave(buf + tolist, len);
13121 else
13122 {
13123 s = alloc((unsigned)(prevlen + len + 1));
13124 if (s != NULL)
13125 {
13126 mch_memmove(s, prev, prevlen);
13127 vim_free(prev);
13128 prev = NULL;
13129 mch_memmove(s + prevlen, buf + tolist, len);
13130 s[prevlen + len] = NUL;
13131 }
13132 }
13133 tolist = filtd + 1;
13134
13135 li = listitem_alloc();
13136 if (li == NULL)
13137 {
13138 vim_free(s);
13139 break;
13140 }
13141 li->li_tv.v_type = VAR_STRING;
13142 li->li_tv.v_lock = 0;
13143 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013144 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013145
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013146 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013147 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013148 if (readlen <= 0)
13149 break;
13150 }
13151 else if (buf[filtd] == NUL)
13152 buf[filtd] = '\n';
13153 }
13154 if (readlen <= 0)
13155 break;
13156
13157 if (tolist == 0)
13158 {
13159 /* "buf" is full, need to move text to an allocated buffer */
13160 if (prev == NULL)
13161 {
13162 prev = vim_strnsave(buf, buflen);
13163 prevlen = buflen;
13164 }
13165 else
13166 {
13167 s = alloc((unsigned)(prevlen + buflen));
13168 if (s != NULL)
13169 {
13170 mch_memmove(s, prev, prevlen);
13171 mch_memmove(s + prevlen, buf, buflen);
13172 vim_free(prev);
13173 prev = s;
13174 prevlen += buflen;
13175 }
13176 }
13177 filtd = 0;
13178 }
13179 else
13180 {
13181 mch_memmove(buf, buf + tolist, buflen - tolist);
13182 filtd -= tolist;
13183 }
13184 }
13185
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013186 /*
13187 * For a negative line count use only the lines at the end of the file,
13188 * free the rest.
13189 */
13190 if (maxline < 0)
13191 while (cnt > -maxline)
13192 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013193 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013194 --cnt;
13195 }
13196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013197 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013198 fclose(fd);
13199}
13200
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013201#if defined(FEAT_RELTIME)
13202static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13203
13204/*
13205 * Convert a List to proftime_T.
13206 * Return FAIL when there is something wrong.
13207 */
13208 static int
13209list2proftime(arg, tm)
13210 typval_T *arg;
13211 proftime_T *tm;
13212{
13213 long n1, n2;
13214 int error = FALSE;
13215
13216 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13217 || arg->vval.v_list->lv_len != 2)
13218 return FAIL;
13219 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13220 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13221# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013222 tm->HighPart = n1;
13223 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013224# else
13225 tm->tv_sec = n1;
13226 tm->tv_usec = n2;
13227# endif
13228 return error ? FAIL : OK;
13229}
13230#endif /* FEAT_RELTIME */
13231
13232/*
13233 * "reltime()" function
13234 */
13235 static void
13236f_reltime(argvars, rettv)
13237 typval_T *argvars;
13238 typval_T *rettv;
13239{
13240#ifdef FEAT_RELTIME
13241 proftime_T res;
13242 proftime_T start;
13243
13244 if (argvars[0].v_type == VAR_UNKNOWN)
13245 {
13246 /* No arguments: get current time. */
13247 profile_start(&res);
13248 }
13249 else if (argvars[1].v_type == VAR_UNKNOWN)
13250 {
13251 if (list2proftime(&argvars[0], &res) == FAIL)
13252 return;
13253 profile_end(&res);
13254 }
13255 else
13256 {
13257 /* Two arguments: compute the difference. */
13258 if (list2proftime(&argvars[0], &start) == FAIL
13259 || list2proftime(&argvars[1], &res) == FAIL)
13260 return;
13261 profile_sub(&res, &start);
13262 }
13263
13264 if (rettv_list_alloc(rettv) == OK)
13265 {
13266 long n1, n2;
13267
13268# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013269 n1 = res.HighPart;
13270 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013271# else
13272 n1 = res.tv_sec;
13273 n2 = res.tv_usec;
13274# endif
13275 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13276 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13277 }
13278#endif
13279}
13280
13281/*
13282 * "reltimestr()" function
13283 */
13284 static void
13285f_reltimestr(argvars, rettv)
13286 typval_T *argvars;
13287 typval_T *rettv;
13288{
13289#ifdef FEAT_RELTIME
13290 proftime_T tm;
13291#endif
13292
13293 rettv->v_type = VAR_STRING;
13294 rettv->vval.v_string = NULL;
13295#ifdef FEAT_RELTIME
13296 if (list2proftime(&argvars[0], &tm) == OK)
13297 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13298#endif
13299}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013300
Bram Moolenaar0d660222005-01-07 21:51:51 +000013301#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13302static void make_connection __ARGS((void));
13303static int check_connection __ARGS((void));
13304
13305 static void
13306make_connection()
13307{
13308 if (X_DISPLAY == NULL
13309# ifdef FEAT_GUI
13310 && !gui.in_use
13311# endif
13312 )
13313 {
13314 x_force_connect = TRUE;
13315 setup_term_clip();
13316 x_force_connect = FALSE;
13317 }
13318}
13319
13320 static int
13321check_connection()
13322{
13323 make_connection();
13324 if (X_DISPLAY == NULL)
13325 {
13326 EMSG(_("E240: No connection to Vim server"));
13327 return FAIL;
13328 }
13329 return OK;
13330}
13331#endif
13332
13333#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013334static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013335
13336 static void
13337remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013338 typval_T *argvars;
13339 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013340 int expr;
13341{
13342 char_u *server_name;
13343 char_u *keys;
13344 char_u *r = NULL;
13345 char_u buf[NUMBUFLEN];
13346# ifdef WIN32
13347 HWND w;
13348# else
13349 Window w;
13350# endif
13351
13352 if (check_restricted() || check_secure())
13353 return;
13354
13355# ifdef FEAT_X11
13356 if (check_connection() == FAIL)
13357 return;
13358# endif
13359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360 server_name = get_tv_string_chk(&argvars[0]);
13361 if (server_name == NULL)
13362 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013363 keys = get_tv_string_buf(&argvars[1], buf);
13364# ifdef WIN32
13365 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13366# else
13367 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13368 < 0)
13369# endif
13370 {
13371 if (r != NULL)
13372 EMSG(r); /* sending worked but evaluation failed */
13373 else
13374 EMSG2(_("E241: Unable to send to %s"), server_name);
13375 return;
13376 }
13377
13378 rettv->vval.v_string = r;
13379
13380 if (argvars[2].v_type != VAR_UNKNOWN)
13381 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013383 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013386 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 v.di_tv.v_type = VAR_STRING;
13388 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 idvar = get_tv_string_chk(&argvars[2]);
13390 if (idvar != NULL)
13391 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013392 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013393 }
13394}
13395#endif
13396
13397/*
13398 * "remote_expr()" function
13399 */
13400/*ARGSUSED*/
13401 static void
13402f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 typval_T *argvars;
13404 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013405{
13406 rettv->v_type = VAR_STRING;
13407 rettv->vval.v_string = NULL;
13408#ifdef FEAT_CLIENTSERVER
13409 remote_common(argvars, rettv, TRUE);
13410#endif
13411}
13412
13413/*
13414 * "remote_foreground()" function
13415 */
13416/*ARGSUSED*/
13417 static void
13418f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 typval_T *argvars;
13420 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013421{
13422 rettv->vval.v_number = 0;
13423#ifdef FEAT_CLIENTSERVER
13424# ifdef WIN32
13425 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426 {
13427 char_u *server_name = get_tv_string_chk(&argvars[0]);
13428
13429 if (server_name != NULL)
13430 serverForeground(server_name);
13431 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013432# else
13433 /* Send a foreground() expression to the server. */
13434 argvars[1].v_type = VAR_STRING;
13435 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13436 argvars[2].v_type = VAR_UNKNOWN;
13437 remote_common(argvars, rettv, TRUE);
13438 vim_free(argvars[1].vval.v_string);
13439# endif
13440#endif
13441}
13442
13443/*ARGSUSED*/
13444 static void
13445f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013446 typval_T *argvars;
13447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013448{
13449#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013451 char_u *s = NULL;
13452# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013453 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013454# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456
13457 if (check_restricted() || check_secure())
13458 {
13459 rettv->vval.v_number = -1;
13460 return;
13461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013462 serverid = get_tv_string_chk(&argvars[0]);
13463 if (serverid == NULL)
13464 {
13465 rettv->vval.v_number = -1;
13466 return; /* type error; errmsg already given */
13467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013468# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013469 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013470 if (n == 0)
13471 rettv->vval.v_number = -1;
13472 else
13473 {
13474 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13475 rettv->vval.v_number = (s != NULL);
13476 }
13477# else
13478 rettv->vval.v_number = 0;
13479 if (check_connection() == FAIL)
13480 return;
13481
13482 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484# endif
13485
13486 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013488 char_u *retvar;
13489
Bram Moolenaar33570922005-01-25 22:26:29 +000013490 v.di_tv.v_type = VAR_STRING;
13491 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013492 retvar = get_tv_string_chk(&argvars[1]);
13493 if (retvar != NULL)
13494 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496 }
13497#else
13498 rettv->vval.v_number = -1;
13499#endif
13500}
13501
13502/*ARGSUSED*/
13503 static void
13504f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 typval_T *argvars;
13506 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013507{
13508 char_u *r = NULL;
13509
13510#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 char_u *serverid = get_tv_string_chk(&argvars[0]);
13512
13513 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013514 {
13515# ifdef WIN32
13516 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013517 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013519 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013520 if (n != 0)
13521 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13522 if (r == NULL)
13523# else
13524 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013526# endif
13527 EMSG(_("E277: Unable to read a server reply"));
13528 }
13529#endif
13530 rettv->v_type = VAR_STRING;
13531 rettv->vval.v_string = r;
13532}
13533
13534/*
13535 * "remote_send()" function
13536 */
13537/*ARGSUSED*/
13538 static void
13539f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 typval_T *argvars;
13541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542{
13543 rettv->v_type = VAR_STRING;
13544 rettv->vval.v_string = NULL;
13545#ifdef FEAT_CLIENTSERVER
13546 remote_common(argvars, rettv, FALSE);
13547#endif
13548}
13549
13550/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013551 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013552 */
13553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013557{
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 list_T *l;
13559 listitem_T *item, *item2;
13560 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013561 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013562 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013563 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 dict_T *d;
13565 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013567 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013568 if (argvars[0].v_type == VAR_DICT)
13569 {
13570 if (argvars[2].v_type != VAR_UNKNOWN)
13571 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013572 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013573 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 key = get_tv_string_chk(&argvars[1]);
13576 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 di = dict_find(d, key, -1);
13579 if (di == NULL)
13580 EMSG2(_(e_dictkey), key);
13581 else
13582 {
13583 *rettv = di->di_tv;
13584 init_tv(&di->di_tv);
13585 dictitem_remove(d, di);
13586 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013587 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013588 }
13589 }
13590 else if (argvars[0].v_type != VAR_LIST)
13591 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013592 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013593 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 int error = FALSE;
13596
13597 idx = get_tv_number_chk(&argvars[1], &error);
13598 if (error)
13599 ; /* type error: do nothing, errmsg already given */
13600 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013601 EMSGN(_(e_listidx), idx);
13602 else
13603 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013604 if (argvars[2].v_type == VAR_UNKNOWN)
13605 {
13606 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013607 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013608 *rettv = item->li_tv;
13609 vim_free(item);
13610 }
13611 else
13612 {
13613 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 end = get_tv_number_chk(&argvars[2], &error);
13615 if (error)
13616 ; /* type error: do nothing */
13617 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013618 EMSGN(_(e_listidx), end);
13619 else
13620 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013621 int cnt = 0;
13622
13623 for (li = item; li != NULL; li = li->li_next)
13624 {
13625 ++cnt;
13626 if (li == item2)
13627 break;
13628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013629 if (li == NULL) /* didn't find "item2" after "item" */
13630 EMSG(_(e_invrange));
13631 else
13632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013633 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013634 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013635 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013636 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013637 l->lv_first = item;
13638 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013639 item->li_prev = NULL;
13640 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013641 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013642 }
13643 }
13644 }
13645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013646 }
13647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648}
13649
13650/*
13651 * "rename({from}, {to})" function
13652 */
13653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 typval_T *argvars;
13656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657{
13658 char_u buf[NUMBUFLEN];
13659
13660 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13664 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665}
13666
13667/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013668 * "repeat()" function
13669 */
13670/*ARGSUSED*/
13671 static void
13672f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *argvars;
13674 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013675{
13676 char_u *p;
13677 int n;
13678 int slen;
13679 int len;
13680 char_u *r;
13681 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013682
13683 n = get_tv_number(&argvars[1]);
13684 if (argvars[0].v_type == VAR_LIST)
13685 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013686 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013687 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013688 if (list_extend(rettv->vval.v_list,
13689 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013690 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013691 }
13692 else
13693 {
13694 p = get_tv_string(&argvars[0]);
13695 rettv->v_type = VAR_STRING;
13696 rettv->vval.v_string = NULL;
13697
13698 slen = (int)STRLEN(p);
13699 len = slen * n;
13700 if (len <= 0)
13701 return;
13702
13703 r = alloc(len + 1);
13704 if (r != NULL)
13705 {
13706 for (i = 0; i < n; i++)
13707 mch_memmove(r + i * slen, p, (size_t)slen);
13708 r[len] = NUL;
13709 }
13710
13711 rettv->vval.v_string = r;
13712 }
13713}
13714
13715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 * "resolve()" function
13717 */
13718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013720 typval_T *argvars;
13721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722{
13723 char_u *p;
13724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726#ifdef FEAT_SHORTCUT
13727 {
13728 char_u *v = NULL;
13729
13730 v = mch_resolve_shortcut(p);
13731 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 }
13736#else
13737# ifdef HAVE_READLINK
13738 {
13739 char_u buf[MAXPATHL + 1];
13740 char_u *cpy;
13741 int len;
13742 char_u *remain = NULL;
13743 char_u *q;
13744 int is_relative_to_current = FALSE;
13745 int has_trailing_pathsep = FALSE;
13746 int limit = 100;
13747
13748 p = vim_strsave(p);
13749
13750 if (p[0] == '.' && (vim_ispathsep(p[1])
13751 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13752 is_relative_to_current = TRUE;
13753
13754 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013755 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 has_trailing_pathsep = TRUE;
13757
13758 q = getnextcomp(p);
13759 if (*q != NUL)
13760 {
13761 /* Separate the first path component in "p", and keep the
13762 * remainder (beginning with the path separator). */
13763 remain = vim_strsave(q - 1);
13764 q[-1] = NUL;
13765 }
13766
13767 for (;;)
13768 {
13769 for (;;)
13770 {
13771 len = readlink((char *)p, (char *)buf, MAXPATHL);
13772 if (len <= 0)
13773 break;
13774 buf[len] = NUL;
13775
13776 if (limit-- == 0)
13777 {
13778 vim_free(p);
13779 vim_free(remain);
13780 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 goto fail;
13783 }
13784
13785 /* Ensure that the result will have a trailing path separator
13786 * if the argument has one. */
13787 if (remain == NULL && has_trailing_pathsep)
13788 add_pathsep(buf);
13789
13790 /* Separate the first path component in the link value and
13791 * concatenate the remainders. */
13792 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13793 if (*q != NUL)
13794 {
13795 if (remain == NULL)
13796 remain = vim_strsave(q - 1);
13797 else
13798 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013799 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 if (cpy != NULL)
13801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 vim_free(remain);
13803 remain = cpy;
13804 }
13805 }
13806 q[-1] = NUL;
13807 }
13808
13809 q = gettail(p);
13810 if (q > p && *q == NUL)
13811 {
13812 /* Ignore trailing path separator. */
13813 q[-1] = NUL;
13814 q = gettail(p);
13815 }
13816 if (q > p && !mch_isFullName(buf))
13817 {
13818 /* symlink is relative to directory of argument */
13819 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13820 if (cpy != NULL)
13821 {
13822 STRCPY(cpy, p);
13823 STRCPY(gettail(cpy), buf);
13824 vim_free(p);
13825 p = cpy;
13826 }
13827 }
13828 else
13829 {
13830 vim_free(p);
13831 p = vim_strsave(buf);
13832 }
13833 }
13834
13835 if (remain == NULL)
13836 break;
13837
13838 /* Append the first path component of "remain" to "p". */
13839 q = getnextcomp(remain + 1);
13840 len = q - remain - (*q != NUL);
13841 cpy = vim_strnsave(p, STRLEN(p) + len);
13842 if (cpy != NULL)
13843 {
13844 STRNCAT(cpy, remain, len);
13845 vim_free(p);
13846 p = cpy;
13847 }
13848 /* Shorten "remain". */
13849 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013850 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 else
13852 {
13853 vim_free(remain);
13854 remain = NULL;
13855 }
13856 }
13857
13858 /* If the result is a relative path name, make it explicitly relative to
13859 * the current directory if and only if the argument had this form. */
13860 if (!vim_ispathsep(*p))
13861 {
13862 if (is_relative_to_current
13863 && *p != NUL
13864 && !(p[0] == '.'
13865 && (p[1] == NUL
13866 || vim_ispathsep(p[1])
13867 || (p[1] == '.'
13868 && (p[2] == NUL
13869 || vim_ispathsep(p[2]))))))
13870 {
13871 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013872 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 if (cpy != NULL)
13874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 vim_free(p);
13876 p = cpy;
13877 }
13878 }
13879 else if (!is_relative_to_current)
13880 {
13881 /* Strip leading "./". */
13882 q = p;
13883 while (q[0] == '.' && vim_ispathsep(q[1]))
13884 q += 2;
13885 if (q > p)
13886 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13887 }
13888 }
13889
13890 /* Ensure that the result will have no trailing path separator
13891 * if the argument had none. But keep "/" or "//". */
13892 if (!has_trailing_pathsep)
13893 {
13894 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013895 if (after_pathsep(p, q))
13896 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 }
13898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 }
13901# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013902 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903# endif
13904#endif
13905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
13908#ifdef HAVE_READLINK
13909fail:
13910#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013911 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912}
13913
13914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 */
13917 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013918f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013919 typval_T *argvars;
13920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921{
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 list_T *l;
13923 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaar0d660222005-01-07 21:51:51 +000013925 rettv->vval.v_number = 0;
13926 if (argvars[0].v_type != VAR_LIST)
13927 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013928 else if ((l = argvars[0].vval.v_list) != NULL
13929 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013930 {
13931 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013932 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013933 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013934 while (li != NULL)
13935 {
13936 ni = li->li_prev;
13937 list_append(l, li);
13938 li = ni;
13939 }
13940 rettv->vval.v_list = l;
13941 rettv->v_type = VAR_LIST;
13942 ++l->lv_refcount;
13943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944}
13945
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013946#define SP_NOMOVE 0x01 /* don't move cursor */
13947#define SP_REPEAT 0x02 /* repeat to find outer pair */
13948#define SP_RETCOUNT 0x04 /* return matchcount */
13949#define SP_SETPCMARK 0x08 /* set previous context mark */
13950#define SP_START 0x10 /* accept match at start position */
13951#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13952#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013953
Bram Moolenaar33570922005-01-25 22:26:29 +000013954static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013955
13956/*
13957 * Get flags for a search function.
13958 * Possibly sets "p_ws".
13959 * Returns BACKWARD, FORWARD or zero (for an error).
13960 */
13961 static int
13962get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013963 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964 int *flagsp;
13965{
13966 int dir = FORWARD;
13967 char_u *flags;
13968 char_u nbuf[NUMBUFLEN];
13969 int mask;
13970
13971 if (varp->v_type != VAR_UNKNOWN)
13972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 flags = get_tv_string_buf_chk(varp, nbuf);
13974 if (flags == NULL)
13975 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976 while (*flags != NUL)
13977 {
13978 switch (*flags)
13979 {
13980 case 'b': dir = BACKWARD; break;
13981 case 'w': p_ws = TRUE; break;
13982 case 'W': p_ws = FALSE; break;
13983 default: mask = 0;
13984 if (flagsp != NULL)
13985 switch (*flags)
13986 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013987 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013988 case 'e': mask = SP_END; break;
13989 case 'm': mask = SP_RETCOUNT; break;
13990 case 'n': mask = SP_NOMOVE; break;
13991 case 'p': mask = SP_SUBPAT; break;
13992 case 'r': mask = SP_REPEAT; break;
13993 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013994 }
13995 if (mask == 0)
13996 {
13997 EMSG2(_(e_invarg2), flags);
13998 dir = 0;
13999 }
14000 else
14001 *flagsp |= mask;
14002 }
14003 if (dir == 0)
14004 break;
14005 ++flags;
14006 }
14007 }
14008 return dir;
14009}
14010
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014012 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014014 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014015search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014016 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014017 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014018 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014020 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 char_u *pat;
14022 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014023 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 int save_p_ws = p_ws;
14025 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014026 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014027 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014028 proftime_T tm;
14029#ifdef FEAT_RELTIME
14030 long time_limit = 0;
14031#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014032 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014033 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014035 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014036 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014037 if (dir == 0)
14038 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014039 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014040 if (flags & SP_START)
14041 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014042 if (flags & SP_END)
14043 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014044
Bram Moolenaar76929292008-01-06 19:07:36 +000014045 /* Optional arguments: line number to stop searching and timeout. */
14046 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014047 {
14048 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14049 if (lnum_stop < 0)
14050 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014051#ifdef FEAT_RELTIME
14052 if (argvars[3].v_type != VAR_UNKNOWN)
14053 {
14054 time_limit = get_tv_number_chk(&argvars[3], NULL);
14055 if (time_limit < 0)
14056 goto theend;
14057 }
14058#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014059 }
14060
Bram Moolenaar76929292008-01-06 19:07:36 +000014061#ifdef FEAT_RELTIME
14062 /* Set the time limit, if there is one. */
14063 profile_setlimit(time_limit, &tm);
14064#endif
14065
Bram Moolenaar231334e2005-07-25 20:46:57 +000014066 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014067 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014068 * Check to make sure only those flags are set.
14069 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14070 * flags cannot be set. Check for that condition also.
14071 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014072 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014073 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014076 goto theend;
14077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014079 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014080 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014081 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014082 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014084 if (flags & SP_SUBPAT)
14085 retval = subpatnum;
14086 else
14087 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014088 if (flags & SP_SETPCMARK)
14089 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014091 if (match_pos != NULL)
14092 {
14093 /* Store the match cursor position */
14094 match_pos->lnum = pos.lnum;
14095 match_pos->col = pos.col + 1;
14096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 /* "/$" will put the cursor after the end of the line, may need to
14098 * correct that here */
14099 check_cursor();
14100 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014101
14102 /* If 'n' flag is used: restore cursor position. */
14103 if (flags & SP_NOMOVE)
14104 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014105 else
14106 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014107theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014109
14110 return retval;
14111}
14112
14113/*
14114 * "search()" function
14115 */
14116 static void
14117f_search(argvars, rettv)
14118 typval_T *argvars;
14119 typval_T *rettv;
14120{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014121 int flags = 0;
14122
14123 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124}
14125
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014127 * "searchdecl()" function
14128 */
14129 static void
14130f_searchdecl(argvars, rettv)
14131 typval_T *argvars;
14132 typval_T *rettv;
14133{
14134 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014135 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014136 int error = FALSE;
14137 char_u *name;
14138
14139 rettv->vval.v_number = 1; /* default: FAIL */
14140
14141 name = get_tv_string_chk(&argvars[0]);
14142 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014143 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014144 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014145 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14146 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14147 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014148 if (!error && name != NULL)
14149 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014150 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014151}
14152
14153/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014154 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014156 static int
14157searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014159 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160{
14161 char_u *spat, *mpat, *epat;
14162 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 int dir;
14165 int flags = 0;
14166 char_u nbuf1[NUMBUFLEN];
14167 char_u nbuf2[NUMBUFLEN];
14168 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014169 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014170 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014171 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 spat = get_tv_string_chk(&argvars[0]);
14175 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14176 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14177 if (spat == NULL || mpat == NULL || epat == NULL)
14178 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 /* Handle the optional fourth argument: flags */
14181 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014182 if (dir == 0)
14183 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014184
14185 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014186 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14187 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014188 if ((flags & (SP_END | SP_SUBPAT)) != 0
14189 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014190 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014191 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014192 goto theend;
14193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014195 /* Using 'r' implies 'W', otherwise it doesn't work. */
14196 if (flags & SP_REPEAT)
14197 p_ws = FALSE;
14198
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014199 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014200 if (argvars[3].v_type == VAR_UNKNOWN
14201 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 skip = (char_u *)"";
14203 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014206 if (argvars[5].v_type != VAR_UNKNOWN)
14207 {
14208 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14209 if (lnum_stop < 0)
14210 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014211#ifdef FEAT_RELTIME
14212 if (argvars[6].v_type != VAR_UNKNOWN)
14213 {
14214 time_limit = get_tv_number_chk(&argvars[6], NULL);
14215 if (time_limit < 0)
14216 goto theend;
14217 }
14218#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014219 }
14220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 if (skip == NULL)
14222 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014224 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014225 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014226
14227theend:
14228 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014229
14230 return retval;
14231}
14232
14233/*
14234 * "searchpair()" function
14235 */
14236 static void
14237f_searchpair(argvars, rettv)
14238 typval_T *argvars;
14239 typval_T *rettv;
14240{
14241 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14242}
14243
14244/*
14245 * "searchpairpos()" function
14246 */
14247 static void
14248f_searchpairpos(argvars, rettv)
14249 typval_T *argvars;
14250 typval_T *rettv;
14251{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014252 pos_T match_pos;
14253 int lnum = 0;
14254 int col = 0;
14255
14256 rettv->vval.v_number = 0;
14257
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014258 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014259 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014260
14261 if (searchpair_cmn(argvars, &match_pos) > 0)
14262 {
14263 lnum = match_pos.lnum;
14264 col = match_pos.col;
14265 }
14266
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014267 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14268 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014269}
14270
14271/*
14272 * Search for a start/middle/end thing.
14273 * Used by searchpair(), see its documentation for the details.
14274 * Returns 0 or -1 for no match,
14275 */
14276 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014277do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14278 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014279 char_u *spat; /* start pattern */
14280 char_u *mpat; /* middle pattern */
14281 char_u *epat; /* end pattern */
14282 int dir; /* BACKWARD or FORWARD */
14283 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014284 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014285 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014286 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014287 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014288{
14289 char_u *save_cpo;
14290 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14291 long retval = 0;
14292 pos_T pos;
14293 pos_T firstpos;
14294 pos_T foundpos;
14295 pos_T save_cursor;
14296 pos_T save_pos;
14297 int n;
14298 int r;
14299 int nest = 1;
14300 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014301 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014302 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014303
14304 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14305 save_cpo = p_cpo;
14306 p_cpo = (char_u *)"";
14307
Bram Moolenaar76929292008-01-06 19:07:36 +000014308#ifdef FEAT_RELTIME
14309 /* Set the time limit, if there is one. */
14310 profile_setlimit(time_limit, &tm);
14311#endif
14312
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014313 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14314 * start/middle/end (pat3, for the top pair). */
14315 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14316 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14317 if (pat2 == NULL || pat3 == NULL)
14318 goto theend;
14319 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14320 if (*mpat == NUL)
14321 STRCPY(pat3, pat2);
14322 else
14323 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14324 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014325 if (flags & SP_START)
14326 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014327
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 save_cursor = curwin->w_cursor;
14329 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014330 clearpos(&firstpos);
14331 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 pat = pat3;
14333 for (;;)
14334 {
14335 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014336 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14338 /* didn't find it or found the first match again: FAIL */
14339 break;
14340
14341 if (firstpos.lnum == 0)
14342 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014343 if (equalpos(pos, foundpos))
14344 {
14345 /* Found the same position again. Can happen with a pattern that
14346 * has "\zs" at the end and searching backwards. Advance one
14347 * character and try again. */
14348 if (dir == BACKWARD)
14349 decl(&pos);
14350 else
14351 incl(&pos);
14352 }
14353 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014355 /* clear the start flag to avoid getting stuck here */
14356 options &= ~SEARCH_START;
14357
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 /* If the skip pattern matches, ignore this match. */
14359 if (*skip != NUL)
14360 {
14361 save_pos = curwin->w_cursor;
14362 curwin->w_cursor = pos;
14363 r = eval_to_bool(skip, &err, NULL, FALSE);
14364 curwin->w_cursor = save_pos;
14365 if (err)
14366 {
14367 /* Evaluating {skip} caused an error, break here. */
14368 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014369 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 break;
14371 }
14372 if (r)
14373 continue;
14374 }
14375
14376 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14377 {
14378 /* Found end when searching backwards or start when searching
14379 * forward: nested pair. */
14380 ++nest;
14381 pat = pat2; /* nested, don't search for middle */
14382 }
14383 else
14384 {
14385 /* Found end when searching forward or start when searching
14386 * backward: end of (nested) pair; or found middle in outer pair. */
14387 if (--nest == 1)
14388 pat = pat3; /* outer level, search for middle */
14389 }
14390
14391 if (nest == 0)
14392 {
14393 /* Found the match: return matchcount or line number. */
14394 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014395 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014397 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014398 if (flags & SP_SETPCMARK)
14399 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 curwin->w_cursor = pos;
14401 if (!(flags & SP_REPEAT))
14402 break;
14403 nest = 1; /* search for next unmatched */
14404 }
14405 }
14406
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014407 if (match_pos != NULL)
14408 {
14409 /* Store the match cursor position */
14410 match_pos->lnum = curwin->w_cursor.lnum;
14411 match_pos->col = curwin->w_cursor.col + 1;
14412 }
14413
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014415 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 curwin->w_cursor = save_cursor;
14417
14418theend:
14419 vim_free(pat2);
14420 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014422
14423 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424}
14425
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014426/*
14427 * "searchpos()" function
14428 */
14429 static void
14430f_searchpos(argvars, rettv)
14431 typval_T *argvars;
14432 typval_T *rettv;
14433{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014434 pos_T match_pos;
14435 int lnum = 0;
14436 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014437 int n;
14438 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014439
14440 rettv->vval.v_number = 0;
14441
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014442 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014443 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014444
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014445 n = search_cmn(argvars, &match_pos, &flags);
14446 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014447 {
14448 lnum = match_pos.lnum;
14449 col = match_pos.col;
14450 }
14451
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014452 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14453 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014454 if (flags & SP_SUBPAT)
14455 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014456}
14457
14458
Bram Moolenaar0d660222005-01-07 21:51:51 +000014459/*ARGSUSED*/
14460 static void
14461f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014462 typval_T *argvars;
14463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465#ifdef FEAT_CLIENTSERVER
14466 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014467 char_u *server = get_tv_string_chk(&argvars[0]);
14468 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 if (server == NULL || reply == NULL)
14472 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014473 if (check_restricted() || check_secure())
14474 return;
14475# ifdef FEAT_X11
14476 if (check_connection() == FAIL)
14477 return;
14478# endif
14479
14480 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482 EMSG(_("E258: Unable to send to client"));
14483 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014485 rettv->vval.v_number = 0;
14486#else
14487 rettv->vval.v_number = -1;
14488#endif
14489}
14490
14491/*ARGSUSED*/
14492 static void
14493f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *argvars;
14495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496{
14497 char_u *r = NULL;
14498
14499#ifdef FEAT_CLIENTSERVER
14500# ifdef WIN32
14501 r = serverGetVimNames();
14502# else
14503 make_connection();
14504 if (X_DISPLAY != NULL)
14505 r = serverGetVimNames(X_DISPLAY);
14506# endif
14507#endif
14508 rettv->v_type = VAR_STRING;
14509 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510}
14511
14512/*
14513 * "setbufvar()" function
14514 */
14515/*ARGSUSED*/
14516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 typval_T *argvars;
14519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520{
14521 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 char_u nbuf[NUMBUFLEN];
14526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 rettv->vval.v_number = 0;
14528
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 if (check_restricted() || check_secure())
14530 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014531 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14532 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 varp = &argvars[2];
14535
14536 if (buf != NULL && varname != NULL && varp != NULL)
14537 {
14538 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
14541 if (*varname == '&')
14542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 long numval;
14544 char_u *strval;
14545 int error = FALSE;
14546
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 numval = get_tv_number_chk(varp, &error);
14549 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 if (!error && strval != NULL)
14551 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 }
14553 else
14554 {
14555 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14556 if (bufvarname != NULL)
14557 {
14558 STRCPY(bufvarname, "b:");
14559 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014560 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 vim_free(bufvarname);
14562 }
14563 }
14564
14565 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568}
14569
14570/*
14571 * "setcmdpos()" function
14572 */
14573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 typval_T *argvars;
14576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 int pos = (int)get_tv_number(&argvars[0]) - 1;
14579
14580 if (pos >= 0)
14581 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582}
14583
14584/*
14585 * "setline()" function
14586 */
14587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 typval_T *argvars;
14590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591{
14592 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014593 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014594 list_T *l = NULL;
14595 listitem_T *li = NULL;
14596 long added = 0;
14597 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014599 lnum = get_tv_lnum(&argvars[0]);
14600 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014602 l = argvars[1].vval.v_list;
14603 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014605 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014607
14608 rettv->vval.v_number = 0; /* OK */
14609 for (;;)
14610 {
14611 if (l != NULL)
14612 {
14613 /* list argument, get next string */
14614 if (li == NULL)
14615 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014617 li = li->li_next;
14618 }
14619
14620 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014622 break;
14623 if (lnum <= curbuf->b_ml.ml_line_count)
14624 {
14625 /* existing line, replace it */
14626 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14627 {
14628 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014629 if (lnum == curwin->w_cursor.lnum)
14630 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014631 rettv->vval.v_number = 0; /* OK */
14632 }
14633 }
14634 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14635 {
14636 /* lnum is one past the last line, append the line */
14637 ++added;
14638 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14639 rettv->vval.v_number = 0; /* OK */
14640 }
14641
14642 if (l == NULL) /* only one string argument */
14643 break;
14644 ++lnum;
14645 }
14646
14647 if (added > 0)
14648 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649}
14650
14651/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014652 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014653 */
14654/*ARGSUSED*/
14655 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014656set_qf_ll_list(wp, list_arg, action_arg, rettv)
14657 win_T *wp;
14658 typval_T *list_arg;
14659 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014660 typval_T *rettv;
14661{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014662#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014663 char_u *act;
14664 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014665#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014666
Bram Moolenaar2641f772005-03-25 21:58:17 +000014667 rettv->vval.v_number = -1;
14668
14669#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014670 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014671 EMSG(_(e_listreq));
14672 else
14673 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014674 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014675
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014676 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014677 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014678 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014679 if (act == NULL)
14680 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014681 if (*act == 'a' || *act == 'r')
14682 action = *act;
14683 }
14684
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014685 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014686 rettv->vval.v_number = 0;
14687 }
14688#endif
14689}
14690
14691/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014692 * "setloclist()" function
14693 */
14694/*ARGSUSED*/
14695 static void
14696f_setloclist(argvars, rettv)
14697 typval_T *argvars;
14698 typval_T *rettv;
14699{
14700 win_T *win;
14701
14702 rettv->vval.v_number = -1;
14703
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014704 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014705 if (win != NULL)
14706 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14707}
14708
14709/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014710 * "setmatches()" function
14711 */
14712 static void
14713f_setmatches(argvars, rettv)
14714 typval_T *argvars;
14715 typval_T *rettv;
14716{
14717#ifdef FEAT_SEARCH_EXTRA
14718 list_T *l;
14719 listitem_T *li;
14720 dict_T *d;
14721
14722 rettv->vval.v_number = -1;
14723 if (argvars[0].v_type != VAR_LIST)
14724 {
14725 EMSG(_(e_listreq));
14726 return;
14727 }
14728 if ((l = argvars[0].vval.v_list) != NULL)
14729 {
14730
14731 /* To some extent make sure that we are dealing with a list from
14732 * "getmatches()". */
14733 li = l->lv_first;
14734 while (li != NULL)
14735 {
14736 if (li->li_tv.v_type != VAR_DICT
14737 || (d = li->li_tv.vval.v_dict) == NULL)
14738 {
14739 EMSG(_(e_invarg));
14740 return;
14741 }
14742 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14743 && dict_find(d, (char_u *)"pattern", -1) != NULL
14744 && dict_find(d, (char_u *)"priority", -1) != NULL
14745 && dict_find(d, (char_u *)"id", -1) != NULL))
14746 {
14747 EMSG(_(e_invarg));
14748 return;
14749 }
14750 li = li->li_next;
14751 }
14752
14753 clear_matches(curwin);
14754 li = l->lv_first;
14755 while (li != NULL)
14756 {
14757 d = li->li_tv.vval.v_dict;
14758 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14759 get_dict_string(d, (char_u *)"pattern", FALSE),
14760 (int)get_dict_number(d, (char_u *)"priority"),
14761 (int)get_dict_number(d, (char_u *)"id"));
14762 li = li->li_next;
14763 }
14764 rettv->vval.v_number = 0;
14765 }
14766#endif
14767}
14768
14769/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014770 * "setpos()" function
14771 */
14772/*ARGSUSED*/
14773 static void
14774f_setpos(argvars, rettv)
14775 typval_T *argvars;
14776 typval_T *rettv;
14777{
14778 pos_T pos;
14779 int fnum;
14780 char_u *name;
14781
Bram Moolenaar08250432008-02-13 11:42:46 +000014782 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014783 name = get_tv_string_chk(argvars);
14784 if (name != NULL)
14785 {
14786 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14787 {
14788 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000014789 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014790 {
Bram Moolenaar08250432008-02-13 11:42:46 +000014791 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014792 if (fnum == curbuf->b_fnum)
14793 {
14794 curwin->w_cursor = pos;
14795 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000014796 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014797 }
14798 else
14799 EMSG(_(e_invarg));
14800 }
Bram Moolenaar08250432008-02-13 11:42:46 +000014801 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
14802 {
14803 /* set mark */
14804 if (setmark_pos(name[1], &pos, fnum) == OK)
14805 rettv->vval.v_number = 0;
14806 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014807 else
14808 EMSG(_(e_invarg));
14809 }
14810 }
14811}
14812
14813/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014814 * "setqflist()" function
14815 */
14816/*ARGSUSED*/
14817 static void
14818f_setqflist(argvars, rettv)
14819 typval_T *argvars;
14820 typval_T *rettv;
14821{
14822 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14823}
14824
14825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 * "setreg()" function
14827 */
14828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014830 typval_T *argvars;
14831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832{
14833 int regname;
14834 char_u *strregname;
14835 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014836 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 int append;
14838 char_u yank_type;
14839 long block_len;
14840
14841 block_len = -1;
14842 yank_type = MAUTO;
14843 append = FALSE;
14844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014848 if (strregname == NULL)
14849 return; /* type error; errmsg already given */
14850 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 if (regname == 0 || regname == '@')
14852 regname = '"';
14853 else if (regname == '=')
14854 return;
14855
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 stropt = get_tv_string_chk(&argvars[2]);
14859 if (stropt == NULL)
14860 return; /* type error */
14861 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 switch (*stropt)
14863 {
14864 case 'a': case 'A': /* append */
14865 append = TRUE;
14866 break;
14867 case 'v': case 'c': /* character-wise selection */
14868 yank_type = MCHAR;
14869 break;
14870 case 'V': case 'l': /* line-wise selection */
14871 yank_type = MLINE;
14872 break;
14873#ifdef FEAT_VISUAL
14874 case 'b': case Ctrl_V: /* block-wise selection */
14875 yank_type = MBLOCK;
14876 if (VIM_ISDIGIT(stropt[1]))
14877 {
14878 ++stropt;
14879 block_len = getdigits(&stropt) - 1;
14880 --stropt;
14881 }
14882 break;
14883#endif
14884 }
14885 }
14886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 strval = get_tv_string_chk(&argvars[1]);
14888 if (strval != NULL)
14889 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892}
14893
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014894/*
14895 * "settabwinvar()" function
14896 */
14897 static void
14898f_settabwinvar(argvars, rettv)
14899 typval_T *argvars;
14900 typval_T *rettv;
14901{
14902 setwinvar(argvars, rettv, 1);
14903}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904
14905/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014906 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014910 typval_T *argvars;
14911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014913 setwinvar(argvars, rettv, 0);
14914}
14915
14916/*
14917 * "setwinvar()" and "settabwinvar()" functions
14918 */
14919 static void
14920setwinvar(argvars, rettv, off)
14921 typval_T *argvars;
14922 typval_T *rettv;
14923 int off;
14924{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 win_T *win;
14926#ifdef FEAT_WINDOWS
14927 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014928 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929#endif
14930 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014931 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014933 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014935 rettv->vval.v_number = 0;
14936
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 if (check_restricted() || check_secure())
14938 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014939
14940#ifdef FEAT_WINDOWS
14941 if (off == 1)
14942 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14943 else
14944 tp = curtab;
14945#endif
14946 win = find_win_by_nr(&argvars[off], tp);
14947 varname = get_tv_string_chk(&argvars[off + 1]);
14948 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949
14950 if (win != NULL && varname != NULL && varp != NULL)
14951 {
14952#ifdef FEAT_WINDOWS
14953 /* set curwin to be our win, temporarily */
14954 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014955 save_curtab = curtab;
14956 goto_tabpage_tp(tp);
14957 if (!win_valid(win))
14958 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 curwin = win;
14960 curbuf = curwin->w_buffer;
14961#endif
14962
14963 if (*varname == '&')
14964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014965 long numval;
14966 char_u *strval;
14967 int error = FALSE;
14968
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 numval = get_tv_number_chk(varp, &error);
14971 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014972 if (!error && strval != NULL)
14973 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 }
14975 else
14976 {
14977 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14978 if (winvarname != NULL)
14979 {
14980 STRCPY(winvarname, "w:");
14981 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014982 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 vim_free(winvarname);
14984 }
14985 }
14986
14987#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014988 /* Restore current tabpage and window, if still valid (autocomands can
14989 * make them invalid). */
14990 if (valid_tabpage(save_curtab))
14991 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 if (win_valid(save_curwin))
14993 {
14994 curwin = save_curwin;
14995 curbuf = curwin->w_buffer;
14996 }
14997#endif
14998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999}
15000
15001/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015002 * "shellescape({string})" function
15003 */
15004 static void
15005f_shellescape(argvars, rettv)
15006 typval_T *argvars;
15007 typval_T *rettv;
15008{
15009 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15010 rettv->v_type = VAR_STRING;
15011}
15012
15013/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 */
15016 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015018 typval_T *argvars;
15019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023 p = get_tv_string(&argvars[0]);
15024 rettv->vval.v_string = vim_strsave(p);
15025 simplify_filename(rettv->vval.v_string); /* simplify in place */
15026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027}
15028
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029static int
15030#ifdef __BORLANDC__
15031 _RTLENTRYF
15032#endif
15033 item_compare __ARGS((const void *s1, const void *s2));
15034static int
15035#ifdef __BORLANDC__
15036 _RTLENTRYF
15037#endif
15038 item_compare2 __ARGS((const void *s1, const void *s2));
15039
15040static int item_compare_ic;
15041static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015042static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043#define ITEM_COMPARE_FAIL 999
15044
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048 static int
15049#ifdef __BORLANDC__
15050_RTLENTRYF
15051#endif
15052item_compare(s1, s2)
15053 const void *s1;
15054 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056 char_u *p1, *p2;
15057 char_u *tofree1, *tofree2;
15058 int res;
15059 char_u numbuf1[NUMBUFLEN];
15060 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015062 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15063 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015064 if (p1 == NULL)
15065 p1 = (char_u *)"";
15066 if (p2 == NULL)
15067 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 if (item_compare_ic)
15069 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 res = STRCMP(p1, p2);
15072 vim_free(tofree1);
15073 vim_free(tofree2);
15074 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075}
15076
15077 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078#ifdef __BORLANDC__
15079_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081item_compare2(s1, s2)
15082 const void *s1;
15083 const void *s2;
15084{
15085 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015086 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015087 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015090 /* shortcut after failure in previous call; compare all items equal */
15091 if (item_compare_func_err)
15092 return 0;
15093
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015094 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15095 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15097 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098
15099 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015100 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015101 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 clear_tv(&argv[0]);
15103 clear_tv(&argv[1]);
15104
15105 if (res == FAIL)
15106 res = ITEM_COMPARE_FAIL;
15107 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108 /* return value has wrong type */
15109 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15110 if (item_compare_func_err)
15111 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 clear_tv(&rettv);
15113 return res;
15114}
15115
15116/*
15117 * "sort({list})" function
15118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 typval_T *argvars;
15122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123{
Bram Moolenaar33570922005-01-25 22:26:29 +000015124 list_T *l;
15125 listitem_T *li;
15126 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 long len;
15128 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130 rettv->vval.v_number = 0;
15131 if (argvars[0].v_type != VAR_LIST)
15132 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 else
15134 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015136 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015137 return;
15138 rettv->vval.v_list = l;
15139 rettv->v_type = VAR_LIST;
15140 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 len = list_len(l);
15143 if (len <= 1)
15144 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146 item_compare_ic = FALSE;
15147 item_compare_func = NULL;
15148 if (argvars[1].v_type != VAR_UNKNOWN)
15149 {
15150 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015151 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 else
15153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015154 int error = FALSE;
15155
15156 i = get_tv_number_chk(&argvars[1], &error);
15157 if (error)
15158 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 if (i == 1)
15160 item_compare_ic = TRUE;
15161 else
15162 item_compare_func = get_tv_string(&argvars[1]);
15163 }
15164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015167 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 if (ptrs == NULL)
15169 return;
15170 i = 0;
15171 for (li = l->lv_first; li != NULL; li = li->li_next)
15172 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015174 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 /* test the compare function */
15176 if (item_compare_func != NULL
15177 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15178 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015179 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 {
15182 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015183 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 item_compare_func == NULL ? item_compare : item_compare2);
15185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 if (!item_compare_func_err)
15187 {
15188 /* Clear the List and append the items in the sorted order. */
15189 l->lv_first = l->lv_last = NULL;
15190 l->lv_len = 0;
15191 for (i = 0; i < len; ++i)
15192 list_append(l, ptrs[i]);
15193 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194 }
15195
15196 vim_free(ptrs);
15197 }
15198}
15199
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015200/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015201 * "soundfold({word})" function
15202 */
15203 static void
15204f_soundfold(argvars, rettv)
15205 typval_T *argvars;
15206 typval_T *rettv;
15207{
15208 char_u *s;
15209
15210 rettv->v_type = VAR_STRING;
15211 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015212#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015213 rettv->vval.v_string = eval_soundfold(s);
15214#else
15215 rettv->vval.v_string = vim_strsave(s);
15216#endif
15217}
15218
15219/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015220 * "spellbadword()" function
15221 */
15222/* ARGSUSED */
15223 static void
15224f_spellbadword(argvars, rettv)
15225 typval_T *argvars;
15226 typval_T *rettv;
15227{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015228 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015229 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015230 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015231
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015232 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015233 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015234
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015235#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015236 if (argvars[0].v_type == VAR_UNKNOWN)
15237 {
15238 /* Find the start and length of the badly spelled word. */
15239 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15240 if (len != 0)
15241 word = ml_get_cursor();
15242 }
15243 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15244 {
15245 char_u *str = get_tv_string_chk(&argvars[0]);
15246 int capcol = -1;
15247
15248 if (str != NULL)
15249 {
15250 /* Check the argument for spelling. */
15251 while (*str != NUL)
15252 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015253 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015254 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015255 {
15256 word = str;
15257 break;
15258 }
15259 str += len;
15260 }
15261 }
15262 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015263#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015264
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015265 list_append_string(rettv->vval.v_list, word, len);
15266 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015267 attr == HLF_SPB ? "bad" :
15268 attr == HLF_SPR ? "rare" :
15269 attr == HLF_SPL ? "local" :
15270 attr == HLF_SPC ? "caps" :
15271 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015272}
15273
15274/*
15275 * "spellsuggest()" function
15276 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015277/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015278 static void
15279f_spellsuggest(argvars, rettv)
15280 typval_T *argvars;
15281 typval_T *rettv;
15282{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015283#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015284 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015285 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015286 int maxcount;
15287 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015288 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015289 listitem_T *li;
15290 int need_capital = FALSE;
15291#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015292
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015293 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015294 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015295
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015296#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015297 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15298 {
15299 str = get_tv_string(&argvars[0]);
15300 if (argvars[1].v_type != VAR_UNKNOWN)
15301 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015302 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015303 if (maxcount <= 0)
15304 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015305 if (argvars[2].v_type != VAR_UNKNOWN)
15306 {
15307 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15308 if (typeerr)
15309 return;
15310 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015311 }
15312 else
15313 maxcount = 25;
15314
Bram Moolenaar4770d092006-01-12 23:22:24 +000015315 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015316
15317 for (i = 0; i < ga.ga_len; ++i)
15318 {
15319 str = ((char_u **)ga.ga_data)[i];
15320
15321 li = listitem_alloc();
15322 if (li == NULL)
15323 vim_free(str);
15324 else
15325 {
15326 li->li_tv.v_type = VAR_STRING;
15327 li->li_tv.v_lock = 0;
15328 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015330 }
15331 }
15332 ga_clear(&ga);
15333 }
15334#endif
15335}
15336
Bram Moolenaar0d660222005-01-07 21:51:51 +000015337 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015338f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015339 typval_T *argvars;
15340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341{
15342 char_u *str;
15343 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015344 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015345 regmatch_T regmatch;
15346 char_u patbuf[NUMBUFLEN];
15347 char_u *save_cpo;
15348 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015349 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015350 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015351 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352
15353 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15354 save_cpo = p_cpo;
15355 p_cpo = (char_u *)"";
15356
15357 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015358 if (argvars[1].v_type != VAR_UNKNOWN)
15359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15361 if (pat == NULL)
15362 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015363 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015364 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015365 }
15366 if (pat == NULL || *pat == NUL)
15367 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015371 if (typeerr)
15372 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15375 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015377 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015378 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015380 if (*str == NUL)
15381 match = FALSE; /* empty item at the end */
15382 else
15383 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384 if (match)
15385 end = regmatch.startp[0];
15386 else
15387 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015388 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15389 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015390 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015391 if (list_append_string(rettv->vval.v_list, str,
15392 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015393 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015394 }
15395 if (!match)
15396 break;
15397 /* Advance to just after the match. */
15398 if (regmatch.endp[0] > str)
15399 col = 0;
15400 else
15401 {
15402 /* Don't get stuck at the same match. */
15403#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015404 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015405#else
15406 col = 1;
15407#endif
15408 }
15409 str = regmatch.endp[0];
15410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411
Bram Moolenaar0d660222005-01-07 21:51:51 +000015412 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414
Bram Moolenaar0d660222005-01-07 21:51:51 +000015415 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416}
15417
Bram Moolenaar2c932302006-03-18 21:42:09 +000015418/*
15419 * "str2nr()" function
15420 */
15421 static void
15422f_str2nr(argvars, rettv)
15423 typval_T *argvars;
15424 typval_T *rettv;
15425{
15426 int base = 10;
15427 char_u *p;
15428 long n;
15429
15430 if (argvars[1].v_type != VAR_UNKNOWN)
15431 {
15432 base = get_tv_number(&argvars[1]);
15433 if (base != 8 && base != 10 && base != 16)
15434 {
15435 EMSG(_(e_invarg));
15436 return;
15437 }
15438 }
15439
15440 p = skipwhite(get_tv_string(&argvars[0]));
15441 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15442 rettv->vval.v_number = n;
15443}
15444
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445#ifdef HAVE_STRFTIME
15446/*
15447 * "strftime({format}[, {time}])" function
15448 */
15449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015451 typval_T *argvars;
15452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453{
15454 char_u result_buf[256];
15455 struct tm *curtime;
15456 time_t seconds;
15457 char_u *p;
15458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 seconds = time(NULL);
15464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 curtime = localtime(&seconds);
15467 /* MSVC returns NULL for an invalid value of seconds. */
15468 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015469 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 else
15471 {
15472# ifdef FEAT_MBYTE
15473 vimconv_T conv;
15474 char_u *enc;
15475
15476 conv.vc_type = CONV_NONE;
15477 enc = enc_locale();
15478 convert_setup(&conv, p_enc, enc);
15479 if (conv.vc_type != CONV_NONE)
15480 p = string_convert(&conv, p, NULL);
15481# endif
15482 if (p != NULL)
15483 (void)strftime((char *)result_buf, sizeof(result_buf),
15484 (char *)p, curtime);
15485 else
15486 result_buf[0] = NUL;
15487
15488# ifdef FEAT_MBYTE
15489 if (conv.vc_type != CONV_NONE)
15490 vim_free(p);
15491 convert_setup(&conv, enc, p_enc);
15492 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 else
15495# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497
15498# ifdef FEAT_MBYTE
15499 /* Release conversion descriptors */
15500 convert_setup(&conv, NULL, NULL);
15501 vim_free(enc);
15502# endif
15503 }
15504}
15505#endif
15506
15507/*
15508 * "stridx()" function
15509 */
15510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015512 typval_T *argvars;
15513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514{
15515 char_u buf[NUMBUFLEN];
15516 char_u *needle;
15517 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015518 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015520 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 needle = get_tv_string_chk(&argvars[1]);
15523 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015525 if (needle == NULL || haystack == NULL)
15526 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527
Bram Moolenaar33570922005-01-25 22:26:29 +000015528 if (argvars[2].v_type != VAR_UNKNOWN)
15529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015530 int error = FALSE;
15531
15532 start_idx = get_tv_number_chk(&argvars[2], &error);
15533 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015534 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015535 if (start_idx >= 0)
15536 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015537 }
15538
15539 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15540 if (pos != NULL)
15541 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542}
15543
15544/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015545 * "string()" function
15546 */
15547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T *argvars;
15550 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015551{
15552 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015553 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015556 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015557 /* Make a copy if we have a value but it's not in allocate memory. */
15558 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560}
15561
15562/*
15563 * "strlen()" function
15564 */
15565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 typval_T *argvars;
15568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 rettv->vval.v_number = (varnumber_T)(STRLEN(
15571 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572}
15573
15574/*
15575 * "strpart()" function
15576 */
15577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015579 typval_T *argvars;
15580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581{
15582 char_u *p;
15583 int n;
15584 int len;
15585 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015588 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 slen = (int)STRLEN(p);
15590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015591 n = get_tv_number_chk(&argvars[1], &error);
15592 if (error)
15593 len = 0;
15594 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 else
15597 len = slen - n; /* default len: all bytes that are available. */
15598
15599 /*
15600 * Only return the overlap between the specified part and the actual
15601 * string.
15602 */
15603 if (n < 0)
15604 {
15605 len += n;
15606 n = 0;
15607 }
15608 else if (n > slen)
15609 n = slen;
15610 if (len < 0)
15611 len = 0;
15612 else if (n + len > slen)
15613 len = slen - n;
15614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015615 rettv->v_type = VAR_STRING;
15616 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617}
15618
15619/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015620 * "strridx()" function
15621 */
15622 static void
15623f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 typval_T *argvars;
15625 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015626{
15627 char_u buf[NUMBUFLEN];
15628 char_u *needle;
15629 char_u *haystack;
15630 char_u *rest;
15631 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015632 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 needle = get_tv_string_chk(&argvars[1]);
15635 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636
15637 rettv->vval.v_number = -1;
15638 if (needle == NULL || haystack == NULL)
15639 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015640
15641 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015642 if (argvars[2].v_type != VAR_UNKNOWN)
15643 {
15644 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015645 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015646 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015648 }
15649 else
15650 end_idx = haystack_len;
15651
Bram Moolenaar0d660222005-01-07 21:51:51 +000015652 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015653 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015655 lastmatch = haystack + end_idx;
15656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015658 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015659 for (rest = haystack; *rest != '\0'; ++rest)
15660 {
15661 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015662 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663 break;
15664 lastmatch = rest;
15665 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015666 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667
15668 if (lastmatch == NULL)
15669 rettv->vval.v_number = -1;
15670 else
15671 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15672}
15673
15674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 * "strtrans()" function
15676 */
15677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015678f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 typval_T *argvars;
15680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015682 rettv->v_type = VAR_STRING;
15683 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684}
15685
15686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015687 * "submatch()" function
15688 */
15689 static void
15690f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *argvars;
15692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693{
15694 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015695 rettv->vval.v_string =
15696 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697}
15698
15699/*
15700 * "substitute()" function
15701 */
15702 static void
15703f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015704 typval_T *argvars;
15705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706{
15707 char_u patbuf[NUMBUFLEN];
15708 char_u subbuf[NUMBUFLEN];
15709 char_u flagsbuf[NUMBUFLEN];
15710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015711 char_u *str = get_tv_string_chk(&argvars[0]);
15712 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15713 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15714 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15715
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015717 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15718 rettv->vval.v_string = NULL;
15719 else
15720 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721}
15722
15723/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015724 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 */
15726/*ARGSUSED*/
15727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015728f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015729 typval_T *argvars;
15730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731{
15732 int id = 0;
15733#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015734 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 long col;
15736 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015737 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015739 lnum = get_tv_lnum(argvars); /* -1 on type error */
15740 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15741 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015743 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015744 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015745 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746#endif
15747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749}
15750
15751/*
15752 * "synIDattr(id, what [, mode])" function
15753 */
15754/*ARGSUSED*/
15755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 typval_T *argvars;
15758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759{
15760 char_u *p = NULL;
15761#ifdef FEAT_SYN_HL
15762 int id;
15763 char_u *what;
15764 char_u *mode;
15765 char_u modebuf[NUMBUFLEN];
15766 int modec;
15767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 id = get_tv_number(&argvars[0]);
15769 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 modec = TOLOWER_ASC(mode[0]);
15774 if (modec != 't' && modec != 'c'
15775#ifdef FEAT_GUI
15776 && modec != 'g'
15777#endif
15778 )
15779 modec = 0; /* replace invalid with current */
15780 }
15781 else
15782 {
15783#ifdef FEAT_GUI
15784 if (gui.in_use)
15785 modec = 'g';
15786 else
15787#endif
15788 if (t_colors > 1)
15789 modec = 'c';
15790 else
15791 modec = 't';
15792 }
15793
15794
15795 switch (TOLOWER_ASC(what[0]))
15796 {
15797 case 'b':
15798 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15799 p = highlight_color(id, what, modec);
15800 else /* bold */
15801 p = highlight_has_attr(id, HL_BOLD, modec);
15802 break;
15803
15804 case 'f': /* fg[#] */
15805 p = highlight_color(id, what, modec);
15806 break;
15807
15808 case 'i':
15809 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15810 p = highlight_has_attr(id, HL_INVERSE, modec);
15811 else /* italic */
15812 p = highlight_has_attr(id, HL_ITALIC, modec);
15813 break;
15814
15815 case 'n': /* name */
15816 p = get_highlight_name(NULL, id - 1);
15817 break;
15818
15819 case 'r': /* reverse */
15820 p = highlight_has_attr(id, HL_INVERSE, modec);
15821 break;
15822
15823 case 's': /* standout */
15824 p = highlight_has_attr(id, HL_STANDOUT, modec);
15825 break;
15826
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015827 case 'u':
15828 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15829 /* underline */
15830 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15831 else
15832 /* undercurl */
15833 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 break;
15835 }
15836
15837 if (p != NULL)
15838 p = vim_strsave(p);
15839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015840 rettv->v_type = VAR_STRING;
15841 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842}
15843
15844/*
15845 * "synIDtrans(id)" function
15846 */
15847/*ARGSUSED*/
15848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015849f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015850 typval_T *argvars;
15851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852{
15853 int id;
15854
15855#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857
15858 if (id > 0)
15859 id = syn_get_final_id(id);
15860 else
15861#endif
15862 id = 0;
15863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015864 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865}
15866
15867/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015868 * "synstack(lnum, col)" function
15869 */
15870/*ARGSUSED*/
15871 static void
15872f_synstack(argvars, rettv)
15873 typval_T *argvars;
15874 typval_T *rettv;
15875{
15876#ifdef FEAT_SYN_HL
15877 long lnum;
15878 long col;
15879 int i;
15880 int id;
15881#endif
15882
15883 rettv->v_type = VAR_LIST;
15884 rettv->vval.v_list = NULL;
15885
15886#ifdef FEAT_SYN_HL
15887 lnum = get_tv_lnum(argvars); /* -1 on type error */
15888 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15889
15890 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
15891 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
15892 && rettv_list_alloc(rettv) != FAIL)
15893 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015894 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015895 for (i = 0; ; ++i)
15896 {
15897 id = syn_get_stack_item(i);
15898 if (id < 0)
15899 break;
15900 if (list_append_number(rettv->vval.v_list, id) == FAIL)
15901 break;
15902 }
15903 }
15904#endif
15905}
15906
15907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 * "system()" function
15909 */
15910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015911f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015912 typval_T *argvars;
15913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015915 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015917 char_u *infile = NULL;
15918 char_u buf[NUMBUFLEN];
15919 int err = FALSE;
15920 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015922 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015923 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015924
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015925 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015926 {
15927 /*
15928 * Write the string to a temp file, to be used for input of the shell
15929 * command.
15930 */
15931 if ((infile = vim_tempname('i')) == NULL)
15932 {
15933 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015934 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015935 }
15936
15937 fd = mch_fopen((char *)infile, WRITEBIN);
15938 if (fd == NULL)
15939 {
15940 EMSG2(_(e_notopen), infile);
15941 goto done;
15942 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015943 p = get_tv_string_buf_chk(&argvars[1], buf);
15944 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015945 {
15946 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015947 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015948 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015949 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15950 err = TRUE;
15951 if (fclose(fd) != 0)
15952 err = TRUE;
15953 if (err)
15954 {
15955 EMSG(_("E677: Error writing temp file"));
15956 goto done;
15957 }
15958 }
15959
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015960 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15961 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015962
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963#ifdef USE_CR
15964 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015965 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 {
15967 char_u *s;
15968
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015969 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 {
15971 if (*s == CAR)
15972 *s = NL;
15973 }
15974 }
15975#else
15976# ifdef USE_CRNL
15977 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015978 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 {
15980 char_u *s, *d;
15981
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015982 d = res;
15983 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 {
15985 if (s[0] == CAR && s[1] == NL)
15986 ++s;
15987 *d++ = *s;
15988 }
15989 *d = NUL;
15990 }
15991# endif
15992#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015993
15994done:
15995 if (infile != NULL)
15996 {
15997 mch_remove(infile);
15998 vim_free(infile);
15999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016000 rettv->v_type = VAR_STRING;
16001 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002}
16003
16004/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016005 * "tabpagebuflist()" function
16006 */
16007/* ARGSUSED */
16008 static void
16009f_tabpagebuflist(argvars, rettv)
16010 typval_T *argvars;
16011 typval_T *rettv;
16012{
16013#ifndef FEAT_WINDOWS
16014 rettv->vval.v_number = 0;
16015#else
16016 tabpage_T *tp;
16017 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016018
16019 if (argvars[0].v_type == VAR_UNKNOWN)
16020 wp = firstwin;
16021 else
16022 {
16023 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16024 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016025 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016026 }
16027 if (wp == NULL)
16028 rettv->vval.v_number = 0;
16029 else
16030 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016031 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016032 rettv->vval.v_number = 0;
16033 else
16034 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016035 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016036 if (list_append_number(rettv->vval.v_list,
16037 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016038 break;
16039 }
16040 }
16041#endif
16042}
16043
16044
16045/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016046 * "tabpagenr()" function
16047 */
16048/* ARGSUSED */
16049 static void
16050f_tabpagenr(argvars, rettv)
16051 typval_T *argvars;
16052 typval_T *rettv;
16053{
16054 int nr = 1;
16055#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016056 char_u *arg;
16057
16058 if (argvars[0].v_type != VAR_UNKNOWN)
16059 {
16060 arg = get_tv_string_chk(&argvars[0]);
16061 nr = 0;
16062 if (arg != NULL)
16063 {
16064 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016065 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016066 else
16067 EMSG2(_(e_invexpr2), arg);
16068 }
16069 }
16070 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016071 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016072#endif
16073 rettv->vval.v_number = nr;
16074}
16075
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016076
16077#ifdef FEAT_WINDOWS
16078static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16079
16080/*
16081 * Common code for tabpagewinnr() and winnr().
16082 */
16083 static int
16084get_winnr(tp, argvar)
16085 tabpage_T *tp;
16086 typval_T *argvar;
16087{
16088 win_T *twin;
16089 int nr = 1;
16090 win_T *wp;
16091 char_u *arg;
16092
16093 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16094 if (argvar->v_type != VAR_UNKNOWN)
16095 {
16096 arg = get_tv_string_chk(argvar);
16097 if (arg == NULL)
16098 nr = 0; /* type error; errmsg already given */
16099 else if (STRCMP(arg, "$") == 0)
16100 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16101 else if (STRCMP(arg, "#") == 0)
16102 {
16103 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16104 if (twin == NULL)
16105 nr = 0;
16106 }
16107 else
16108 {
16109 EMSG2(_(e_invexpr2), arg);
16110 nr = 0;
16111 }
16112 }
16113
16114 if (nr > 0)
16115 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16116 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016117 {
16118 if (wp == NULL)
16119 {
16120 /* didn't find it in this tabpage */
16121 nr = 0;
16122 break;
16123 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016124 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016125 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016126 return nr;
16127}
16128#endif
16129
16130/*
16131 * "tabpagewinnr()" function
16132 */
16133/* ARGSUSED */
16134 static void
16135f_tabpagewinnr(argvars, rettv)
16136 typval_T *argvars;
16137 typval_T *rettv;
16138{
16139 int nr = 1;
16140#ifdef FEAT_WINDOWS
16141 tabpage_T *tp;
16142
16143 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16144 if (tp == NULL)
16145 nr = 0;
16146 else
16147 nr = get_winnr(tp, &argvars[1]);
16148#endif
16149 rettv->vval.v_number = nr;
16150}
16151
16152
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016153/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016154 * "tagfiles()" function
16155 */
16156/*ARGSUSED*/
16157 static void
16158f_tagfiles(argvars, rettv)
16159 typval_T *argvars;
16160 typval_T *rettv;
16161{
16162 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016163 tagname_T tn;
16164 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016165
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016166 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016167 {
16168 rettv->vval.v_number = 0;
16169 return;
16170 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016171
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016172 for (first = TRUE; ; first = FALSE)
16173 if (get_tagfname(&tn, first, fname) == FAIL
16174 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016175 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016176 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016177}
16178
16179/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016180 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016181 */
16182 static void
16183f_taglist(argvars, rettv)
16184 typval_T *argvars;
16185 typval_T *rettv;
16186{
16187 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016188
16189 tag_pattern = get_tv_string(&argvars[0]);
16190
16191 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016192 if (*tag_pattern == NUL)
16193 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016194
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016195 if (rettv_list_alloc(rettv) == OK)
16196 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016197}
16198
16199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 * "tempname()" function
16201 */
16202/*ARGSUSED*/
16203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016204f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016205 typval_T *argvars;
16206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207{
16208 static int x = 'A';
16209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016210 rettv->v_type = VAR_STRING;
16211 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212
16213 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16214 * names. Skip 'I' and 'O', they are used for shell redirection. */
16215 do
16216 {
16217 if (x == 'Z')
16218 x = '0';
16219 else if (x == '9')
16220 x = 'A';
16221 else
16222 {
16223#ifdef EBCDIC
16224 if (x == 'I')
16225 x = 'J';
16226 else if (x == 'R')
16227 x = 'S';
16228 else
16229#endif
16230 ++x;
16231 }
16232 } while (x == 'I' || x == 'O');
16233}
16234
16235/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016236 * "test(list)" function: Just checking the walls...
16237 */
16238/*ARGSUSED*/
16239 static void
16240f_test(argvars, rettv)
16241 typval_T *argvars;
16242 typval_T *rettv;
16243{
16244 /* Used for unit testing. Change the code below to your liking. */
16245#if 0
16246 listitem_T *li;
16247 list_T *l;
16248 char_u *bad, *good;
16249
16250 if (argvars[0].v_type != VAR_LIST)
16251 return;
16252 l = argvars[0].vval.v_list;
16253 if (l == NULL)
16254 return;
16255 li = l->lv_first;
16256 if (li == NULL)
16257 return;
16258 bad = get_tv_string(&li->li_tv);
16259 li = li->li_next;
16260 if (li == NULL)
16261 return;
16262 good = get_tv_string(&li->li_tv);
16263 rettv->vval.v_number = test_edit_score(bad, good);
16264#endif
16265}
16266
16267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 * "tolower(string)" function
16269 */
16270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016271f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 typval_T *argvars;
16273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274{
16275 char_u *p;
16276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277 p = vim_strsave(get_tv_string(&argvars[0]));
16278 rettv->v_type = VAR_STRING;
16279 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
16281 if (p != NULL)
16282 while (*p != NUL)
16283 {
16284#ifdef FEAT_MBYTE
16285 int l;
16286
16287 if (enc_utf8)
16288 {
16289 int c, lc;
16290
16291 c = utf_ptr2char(p);
16292 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016293 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 /* TODO: reallocate string when byte count changes. */
16295 if (utf_char2len(lc) == l)
16296 utf_char2bytes(lc, p);
16297 p += l;
16298 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016299 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 p += l; /* skip multi-byte character */
16301 else
16302#endif
16303 {
16304 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16305 ++p;
16306 }
16307 }
16308}
16309
16310/*
16311 * "toupper(string)" function
16312 */
16313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016314f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016315 typval_T *argvars;
16316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320}
16321
16322/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016323 * "tr(string, fromstr, tostr)" function
16324 */
16325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016326f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 typval_T *argvars;
16328 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016329{
16330 char_u *instr;
16331 char_u *fromstr;
16332 char_u *tostr;
16333 char_u *p;
16334#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016335 int inlen;
16336 int fromlen;
16337 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016338 int idx;
16339 char_u *cpstr;
16340 int cplen;
16341 int first = TRUE;
16342#endif
16343 char_u buf[NUMBUFLEN];
16344 char_u buf2[NUMBUFLEN];
16345 garray_T ga;
16346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016347 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016348 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16349 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016350
16351 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 rettv->v_type = VAR_STRING;
16353 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016354 if (fromstr == NULL || tostr == NULL)
16355 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016356 ga_init2(&ga, (int)sizeof(char), 80);
16357
16358#ifdef FEAT_MBYTE
16359 if (!has_mbyte)
16360#endif
16361 /* not multi-byte: fromstr and tostr must be the same length */
16362 if (STRLEN(fromstr) != STRLEN(tostr))
16363 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016364#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016365error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016366#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016367 EMSG2(_(e_invarg2), fromstr);
16368 ga_clear(&ga);
16369 return;
16370 }
16371
16372 /* fromstr and tostr have to contain the same number of chars */
16373 while (*instr != NUL)
16374 {
16375#ifdef FEAT_MBYTE
16376 if (has_mbyte)
16377 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016378 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016379 cpstr = instr;
16380 cplen = inlen;
16381 idx = 0;
16382 for (p = fromstr; *p != NUL; p += fromlen)
16383 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016384 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016385 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16386 {
16387 for (p = tostr; *p != NUL; p += tolen)
16388 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016389 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016390 if (idx-- == 0)
16391 {
16392 cplen = tolen;
16393 cpstr = p;
16394 break;
16395 }
16396 }
16397 if (*p == NUL) /* tostr is shorter than fromstr */
16398 goto error;
16399 break;
16400 }
16401 ++idx;
16402 }
16403
16404 if (first && cpstr == instr)
16405 {
16406 /* Check that fromstr and tostr have the same number of
16407 * (multi-byte) characters. Done only once when a character
16408 * of instr doesn't appear in fromstr. */
16409 first = FALSE;
16410 for (p = tostr; *p != NUL; p += tolen)
16411 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016412 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016413 --idx;
16414 }
16415 if (idx != 0)
16416 goto error;
16417 }
16418
16419 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016420 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016421 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016422
16423 instr += inlen;
16424 }
16425 else
16426#endif
16427 {
16428 /* When not using multi-byte chars we can do it faster. */
16429 p = vim_strchr(fromstr, *instr);
16430 if (p != NULL)
16431 ga_append(&ga, tostr[p - fromstr]);
16432 else
16433 ga_append(&ga, *instr);
16434 ++instr;
16435 }
16436 }
16437
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016438 /* add a terminating NUL */
16439 ga_grow(&ga, 1);
16440 ga_append(&ga, NUL);
16441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016442 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016443}
16444
16445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 * "type(expr)" function
16447 */
16448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449f_type(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{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016453 int n;
16454
16455 switch (argvars[0].v_type)
16456 {
16457 case VAR_NUMBER: n = 0; break;
16458 case VAR_STRING: n = 1; break;
16459 case VAR_FUNC: n = 2; break;
16460 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016461 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016462 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16463 }
16464 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465}
16466
16467/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016468 * "values(dict)" function
16469 */
16470 static void
16471f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016472 typval_T *argvars;
16473 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016474{
16475 dict_list(argvars, rettv, 1);
16476}
16477
16478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 * "virtcol(string)" function
16480 */
16481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016482f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 typval_T *argvars;
16484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485{
16486 colnr_T vcol = 0;
16487 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016488 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016490 fp = var2fpos(&argvars[0], FALSE, &fnum);
16491 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16492 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 {
16494 getvvcol(curwin, fp, NULL, NULL, &vcol);
16495 ++vcol;
16496 }
16497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016498 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499}
16500
16501/*
16502 * "visualmode()" function
16503 */
16504/*ARGSUSED*/
16505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016507 typval_T *argvars;
16508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509{
16510#ifdef FEAT_VISUAL
16511 char_u str[2];
16512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016513 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 str[0] = curbuf->b_visual_mode_eval;
16515 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016516 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517
16518 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016519 if ((argvars[0].v_type == VAR_NUMBER
16520 && argvars[0].vval.v_number != 0)
16521 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016522 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 curbuf->b_visual_mode_eval = NUL;
16524#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016525 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526#endif
16527}
16528
16529/*
16530 * "winbufnr(nr)" function
16531 */
16532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016533f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016534 typval_T *argvars;
16535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536{
16537 win_T *wp;
16538
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016539 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016543 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544}
16545
16546/*
16547 * "wincol()" function
16548 */
16549/*ARGSUSED*/
16550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 typval_T *argvars;
16553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554{
16555 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016556 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557}
16558
16559/*
16560 * "winheight(nr)" function
16561 */
16562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016564 typval_T *argvars;
16565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566{
16567 win_T *wp;
16568
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016569 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016573 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574}
16575
16576/*
16577 * "winline()" function
16578 */
16579/*ARGSUSED*/
16580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016581f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 typval_T *argvars;
16583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584{
16585 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587}
16588
16589/*
16590 * "winnr()" function
16591 */
16592/* ARGSUSED */
16593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 typval_T *argvars;
16596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597{
16598 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016599
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016601 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016603 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604}
16605
16606/*
16607 * "winrestcmd()" function
16608 */
16609/* ARGSUSED */
16610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016611f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 typval_T *argvars;
16613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614{
16615#ifdef FEAT_WINDOWS
16616 win_T *wp;
16617 int winnr = 1;
16618 garray_T ga;
16619 char_u buf[50];
16620
16621 ga_init2(&ga, (int)sizeof(char), 70);
16622 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16623 {
16624 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16625 ga_concat(&ga, buf);
16626# ifdef FEAT_VERTSPLIT
16627 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16628 ga_concat(&ga, buf);
16629# endif
16630 ++winnr;
16631 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016632 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016638 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639}
16640
16641/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016642 * "winrestview()" function
16643 */
16644/* ARGSUSED */
16645 static void
16646f_winrestview(argvars, rettv)
16647 typval_T *argvars;
16648 typval_T *rettv;
16649{
16650 dict_T *dict;
16651
16652 if (argvars[0].v_type != VAR_DICT
16653 || (dict = argvars[0].vval.v_dict) == NULL)
16654 EMSG(_(e_invarg));
16655 else
16656 {
16657 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16658 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16659#ifdef FEAT_VIRTUALEDIT
16660 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16661#endif
16662 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016663 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016664
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016665 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016666#ifdef FEAT_DIFF
16667 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16668#endif
16669 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16670 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16671
16672 check_cursor();
16673 changed_cline_bef_curs();
16674 invalidate_botline();
16675 redraw_later(VALID);
16676
16677 if (curwin->w_topline == 0)
16678 curwin->w_topline = 1;
16679 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16680 curwin->w_topline = curbuf->b_ml.ml_line_count;
16681#ifdef FEAT_DIFF
16682 check_topfill(curwin, TRUE);
16683#endif
16684 }
16685}
16686
16687/*
16688 * "winsaveview()" function
16689 */
16690/* ARGSUSED */
16691 static void
16692f_winsaveview(argvars, rettv)
16693 typval_T *argvars;
16694 typval_T *rettv;
16695{
16696 dict_T *dict;
16697
16698 dict = dict_alloc();
16699 if (dict == NULL)
16700 return;
16701 rettv->v_type = VAR_DICT;
16702 rettv->vval.v_dict = dict;
16703 ++dict->dv_refcount;
16704
16705 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16706 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16707#ifdef FEAT_VIRTUALEDIT
16708 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16709#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016710 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016711 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16712
16713 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16714#ifdef FEAT_DIFF
16715 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16716#endif
16717 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16718 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16719}
16720
16721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 * "winwidth(nr)" function
16723 */
16724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 typval_T *argvars;
16727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
16729 win_T *wp;
16730
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016731 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016733 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 else
16735#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016736 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016738 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739#endif
16740}
16741
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016743 * "writefile()" function
16744 */
16745 static void
16746f_writefile(argvars, rettv)
16747 typval_T *argvars;
16748 typval_T *rettv;
16749{
16750 int binary = FALSE;
16751 char_u *fname;
16752 FILE *fd;
16753 listitem_T *li;
16754 char_u *s;
16755 int ret = 0;
16756 int c;
16757
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016758 if (check_restricted() || check_secure())
16759 return;
16760
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016761 if (argvars[0].v_type != VAR_LIST)
16762 {
16763 EMSG2(_(e_listarg), "writefile()");
16764 return;
16765 }
16766 if (argvars[0].vval.v_list == NULL)
16767 return;
16768
16769 if (argvars[2].v_type != VAR_UNKNOWN
16770 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16771 binary = TRUE;
16772
16773 /* Always open the file in binary mode, library functions have a mind of
16774 * their own about CR-LF conversion. */
16775 fname = get_tv_string(&argvars[1]);
16776 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16777 {
16778 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16779 ret = -1;
16780 }
16781 else
16782 {
16783 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16784 li = li->li_next)
16785 {
16786 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16787 {
16788 if (*s == '\n')
16789 c = putc(NUL, fd);
16790 else
16791 c = putc(*s, fd);
16792 if (c == EOF)
16793 {
16794 ret = -1;
16795 break;
16796 }
16797 }
16798 if (!binary || li->li_next != NULL)
16799 if (putc('\n', fd) == EOF)
16800 {
16801 ret = -1;
16802 break;
16803 }
16804 if (ret < 0)
16805 {
16806 EMSG(_(e_write));
16807 break;
16808 }
16809 }
16810 fclose(fd);
16811 }
16812
16813 rettv->vval.v_number = ret;
16814}
16815
16816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016818 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 */
16820 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016821var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016823 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016824 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016826 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016828 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829
Bram Moolenaara5525202006-03-02 22:52:09 +000016830 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016831 if (varp->v_type == VAR_LIST)
16832 {
16833 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016834 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016835 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016836 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016837
16838 l = varp->vval.v_list;
16839 if (l == NULL)
16840 return NULL;
16841
16842 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016843 pos.lnum = list_find_nr(l, 0L, &error);
16844 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016845 return NULL; /* invalid line number */
16846
16847 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016848 pos.col = list_find_nr(l, 1L, &error);
16849 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016850 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016851 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016852
16853 /* We accept "$" for the column number: last column. */
16854 li = list_find(l, 1L);
16855 if (li != NULL && li->li_tv.v_type == VAR_STRING
16856 && li->li_tv.vval.v_string != NULL
16857 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16858 pos.col = len + 1;
16859
Bram Moolenaara5525202006-03-02 22:52:09 +000016860 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016861 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016862 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016863 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016864
Bram Moolenaara5525202006-03-02 22:52:09 +000016865#ifdef FEAT_VIRTUALEDIT
16866 /* Get the virtual offset. Defaults to zero. */
16867 pos.coladd = list_find_nr(l, 2L, &error);
16868 if (error)
16869 pos.coladd = 0;
16870#endif
16871
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016872 return &pos;
16873 }
16874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016875 name = get_tv_string_chk(varp);
16876 if (name == NULL)
16877 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 if (name[0] == '.') /* cursor */
16879 return &curwin->w_cursor;
16880 if (name[0] == '\'') /* mark */
16881 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016882 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16884 return NULL;
16885 return pp;
16886 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016887
16888#ifdef FEAT_VIRTUALEDIT
16889 pos.coladd = 0;
16890#endif
16891
Bram Moolenaar477933c2007-07-17 14:32:23 +000016892 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016893 {
16894 pos.col = 0;
16895 if (name[1] == '0') /* "w0": first visible line */
16896 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016897 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016898 pos.lnum = curwin->w_topline;
16899 return &pos;
16900 }
16901 else if (name[1] == '$') /* "w$": last visible line */
16902 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016903 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016904 pos.lnum = curwin->w_botline - 1;
16905 return &pos;
16906 }
16907 }
16908 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016910 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 {
16912 pos.lnum = curbuf->b_ml.ml_line_count;
16913 pos.col = 0;
16914 }
16915 else
16916 {
16917 pos.lnum = curwin->w_cursor.lnum;
16918 pos.col = (colnr_T)STRLEN(ml_get_curline());
16919 }
16920 return &pos;
16921 }
16922 return NULL;
16923}
16924
16925/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016926 * Convert list in "arg" into a position and optional file number.
16927 * When "fnump" is NULL there is no file number, only 3 items.
16928 * Note that the column is passed on as-is, the caller may want to decrement
16929 * it to use 1 for the first column.
16930 * Return FAIL when conversion is not possible, doesn't check the position for
16931 * validity.
16932 */
16933 static int
16934list2fpos(arg, posp, fnump)
16935 typval_T *arg;
16936 pos_T *posp;
16937 int *fnump;
16938{
16939 list_T *l = arg->vval.v_list;
16940 long i = 0;
16941 long n;
16942
Bram Moolenaarbde35262006-07-23 20:12:24 +000016943 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16944 * when "fnump" isn't NULL and "coladd" is optional. */
16945 if (arg->v_type != VAR_LIST
16946 || l == NULL
16947 || l->lv_len < (fnump == NULL ? 2 : 3)
16948 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016949 return FAIL;
16950
16951 if (fnump != NULL)
16952 {
16953 n = list_find_nr(l, i++, NULL); /* fnum */
16954 if (n < 0)
16955 return FAIL;
16956 if (n == 0)
16957 n = curbuf->b_fnum; /* current buffer */
16958 *fnump = n;
16959 }
16960
16961 n = list_find_nr(l, i++, NULL); /* lnum */
16962 if (n < 0)
16963 return FAIL;
16964 posp->lnum = n;
16965
16966 n = list_find_nr(l, i++, NULL); /* col */
16967 if (n < 0)
16968 return FAIL;
16969 posp->col = n;
16970
16971#ifdef FEAT_VIRTUALEDIT
16972 n = list_find_nr(l, i, NULL);
16973 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016974 posp->coladd = 0;
16975 else
16976 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016977#endif
16978
16979 return OK;
16980}
16981
16982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 * Get the length of an environment variable name.
16984 * Advance "arg" to the first character after the name.
16985 * Return 0 for error.
16986 */
16987 static int
16988get_env_len(arg)
16989 char_u **arg;
16990{
16991 char_u *p;
16992 int len;
16993
16994 for (p = *arg; vim_isIDc(*p); ++p)
16995 ;
16996 if (p == *arg) /* no name found */
16997 return 0;
16998
16999 len = (int)(p - *arg);
17000 *arg = p;
17001 return len;
17002}
17003
17004/*
17005 * Get the length of the name of a function or internal variable.
17006 * "arg" is advanced to the first non-white character after the name.
17007 * Return 0 if something is wrong.
17008 */
17009 static int
17010get_id_len(arg)
17011 char_u **arg;
17012{
17013 char_u *p;
17014 int len;
17015
17016 /* Find the end of the name. */
17017 for (p = *arg; eval_isnamec(*p); ++p)
17018 ;
17019 if (p == *arg) /* no name found */
17020 return 0;
17021
17022 len = (int)(p - *arg);
17023 *arg = skipwhite(p);
17024
17025 return len;
17026}
17027
17028/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017029 * Get the length of the name of a variable or function.
17030 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017032 * Return -1 if curly braces expansion failed.
17033 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 * If the name contains 'magic' {}'s, expand them and return the
17035 * expanded name in an allocated string via 'alias' - caller must free.
17036 */
17037 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017038get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 char_u **arg;
17040 char_u **alias;
17041 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017042 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043{
17044 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 char_u *p;
17046 char_u *expr_start;
17047 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048
17049 *alias = NULL; /* default to no alias */
17050
17051 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17052 && (*arg)[2] == (int)KE_SNR)
17053 {
17054 /* hard coded <SNR>, already translated */
17055 *arg += 3;
17056 return get_id_len(arg) + 3;
17057 }
17058 len = eval_fname_script(*arg);
17059 if (len > 0)
17060 {
17061 /* literal "<SID>", "s:" or "<SNR>" */
17062 *arg += len;
17063 }
17064
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017066 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017068 p = find_name_end(*arg, &expr_start, &expr_end,
17069 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 if (expr_start != NULL)
17071 {
17072 char_u *temp_string;
17073
17074 if (!evaluate)
17075 {
17076 len += (int)(p - *arg);
17077 *arg = skipwhite(p);
17078 return len;
17079 }
17080
17081 /*
17082 * Include any <SID> etc in the expanded string:
17083 * Thus the -len here.
17084 */
17085 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17086 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017087 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 *alias = temp_string;
17089 *arg = skipwhite(p);
17090 return (int)STRLEN(temp_string);
17091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092
17093 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017094 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 EMSG2(_(e_invexpr2), *arg);
17096
17097 return len;
17098}
17099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100/*
17101 * Find the end of a variable or function name, taking care of magic braces.
17102 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17103 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017104 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017105 * Return a pointer to just after the name. Equal to "arg" if there is no
17106 * valid name.
17107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017109find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 char_u *arg;
17111 char_u **expr_start;
17112 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017113 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 int mb_nest = 0;
17116 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 char_u *p;
17118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121 *expr_start = NULL;
17122 *expr_end = NULL;
17123 }
17124
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017125 /* Quick check for valid starting character. */
17126 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17127 return arg;
17128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017129 for (p = arg; *p != NUL
17130 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017131 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017132 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017134 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017136 if (*p == '\'')
17137 {
17138 /* skip over 'string' to avoid counting [ and ] inside it. */
17139 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17140 ;
17141 if (*p == NUL)
17142 break;
17143 }
17144 else if (*p == '"')
17145 {
17146 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17147 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17148 if (*p == '\\' && p[1] != NUL)
17149 ++p;
17150 if (*p == NUL)
17151 break;
17152 }
17153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156 if (*p == '[')
17157 ++br_nest;
17158 else if (*p == ']')
17159 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017162 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017164 if (*p == '{')
17165 {
17166 mb_nest++;
17167 if (expr_start != NULL && *expr_start == NULL)
17168 *expr_start = p;
17169 }
17170 else if (*p == '}')
17171 {
17172 mb_nest--;
17173 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17174 *expr_end = p;
17175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 }
17178
17179 return p;
17180}
17181
17182/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017183 * Expands out the 'magic' {}'s in a variable/function name.
17184 * Note that this can call itself recursively, to deal with
17185 * constructs like foo{bar}{baz}{bam}
17186 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17187 * "in_start" ^
17188 * "expr_start" ^
17189 * "expr_end" ^
17190 * "in_end" ^
17191 *
17192 * Returns a new allocated string, which the caller must free.
17193 * Returns NULL for failure.
17194 */
17195 static char_u *
17196make_expanded_name(in_start, expr_start, expr_end, in_end)
17197 char_u *in_start;
17198 char_u *expr_start;
17199 char_u *expr_end;
17200 char_u *in_end;
17201{
17202 char_u c1;
17203 char_u *retval = NULL;
17204 char_u *temp_result;
17205 char_u *nextcmd = NULL;
17206
17207 if (expr_end == NULL || in_end == NULL)
17208 return NULL;
17209 *expr_start = NUL;
17210 *expr_end = NUL;
17211 c1 = *in_end;
17212 *in_end = NUL;
17213
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017214 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017215 if (temp_result != NULL && nextcmd == NULL)
17216 {
17217 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17218 + (in_end - expr_end) + 1));
17219 if (retval != NULL)
17220 {
17221 STRCPY(retval, in_start);
17222 STRCAT(retval, temp_result);
17223 STRCAT(retval, expr_end + 1);
17224 }
17225 }
17226 vim_free(temp_result);
17227
17228 *in_end = c1; /* put char back for error messages */
17229 *expr_start = '{';
17230 *expr_end = '}';
17231
17232 if (retval != NULL)
17233 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017234 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017235 if (expr_start != NULL)
17236 {
17237 /* Further expansion! */
17238 temp_result = make_expanded_name(retval, expr_start,
17239 expr_end, temp_result);
17240 vim_free(retval);
17241 retval = temp_result;
17242 }
17243 }
17244
17245 return retval;
17246}
17247
17248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017250 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 */
17252 static int
17253eval_isnamec(c)
17254 int c;
17255{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017256 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17257}
17258
17259/*
17260 * Return TRUE if character "c" can be used as the first character in a
17261 * variable or function name (excluding '{' and '}').
17262 */
17263 static int
17264eval_isnamec1(c)
17265 int c;
17266{
17267 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268}
17269
17270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 * Set number v: variable to "val".
17272 */
17273 void
17274set_vim_var_nr(idx, val)
17275 int idx;
17276 long val;
17277{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017278 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279}
17280
17281/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017282 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 */
17284 long
17285get_vim_var_nr(idx)
17286 int idx;
17287{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017288 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289}
17290
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017291#if defined(FEAT_AUTOCMD) || defined(PROTO)
17292/*
17293 * Get string v: variable value. Uses a static buffer, can only be used once.
17294 */
17295 char_u *
17296get_vim_var_str(idx)
17297 int idx;
17298{
17299 return get_tv_string(&vimvars[idx].vv_tv);
17300}
17301#endif
17302
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303/*
17304 * Set v:count, v:count1 and v:prevcount.
17305 */
17306 void
17307set_vcount(count, count1)
17308 long count;
17309 long count1;
17310{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017311 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17312 vimvars[VV_COUNT].vv_nr = count;
17313 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314}
17315
17316/*
17317 * Set string v: variable to a copy of "val".
17318 */
17319 void
17320set_vim_var_string(idx, val, len)
17321 int idx;
17322 char_u *val;
17323 int len; /* length of "val" to use or -1 (whole string) */
17324{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017325 /* Need to do this (at least) once, since we can't initialize a union.
17326 * Will always be invoked when "v:progname" is set. */
17327 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17328
Bram Moolenaare9a41262005-01-15 22:18:47 +000017329 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017331 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017333 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017335 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336}
17337
17338/*
17339 * Set v:register if needed.
17340 */
17341 void
17342set_reg_var(c)
17343 int c;
17344{
17345 char_u regname;
17346
17347 if (c == 0 || c == ' ')
17348 regname = '"';
17349 else
17350 regname = c;
17351 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017352 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 set_vim_var_string(VV_REG, &regname, 1);
17354}
17355
17356/*
17357 * Get or set v:exception. If "oldval" == NULL, return the current value.
17358 * Otherwise, restore the value to "oldval" and return NULL.
17359 * Must always be called in pairs to save and restore v:exception! Does not
17360 * take care of memory allocations.
17361 */
17362 char_u *
17363v_exception(oldval)
17364 char_u *oldval;
17365{
17366 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017367 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368
Bram Moolenaare9a41262005-01-15 22:18:47 +000017369 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 return NULL;
17371}
17372
17373/*
17374 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17375 * Otherwise, restore the value to "oldval" and return NULL.
17376 * Must always be called in pairs to save and restore v:throwpoint! Does not
17377 * take care of memory allocations.
17378 */
17379 char_u *
17380v_throwpoint(oldval)
17381 char_u *oldval;
17382{
17383 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017384 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385
Bram Moolenaare9a41262005-01-15 22:18:47 +000017386 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 return NULL;
17388}
17389
17390#if defined(FEAT_AUTOCMD) || defined(PROTO)
17391/*
17392 * Set v:cmdarg.
17393 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17394 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17395 * Must always be called in pairs!
17396 */
17397 char_u *
17398set_cmdarg(eap, oldarg)
17399 exarg_T *eap;
17400 char_u *oldarg;
17401{
17402 char_u *oldval;
17403 char_u *newval;
17404 unsigned len;
17405
Bram Moolenaare9a41262005-01-15 22:18:47 +000017406 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017407 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017409 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017410 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017411 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 }
17413
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017414 if (eap->force_bin == FORCE_BIN)
17415 len = 6;
17416 else if (eap->force_bin == FORCE_NOBIN)
17417 len = 8;
17418 else
17419 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017420
17421 if (eap->read_edit)
17422 len += 7;
17423
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017424 if (eap->force_ff != 0)
17425 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17426# ifdef FEAT_MBYTE
17427 if (eap->force_enc != 0)
17428 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017429 if (eap->bad_char != 0)
17430 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017431# endif
17432
17433 newval = alloc(len + 1);
17434 if (newval == NULL)
17435 return NULL;
17436
17437 if (eap->force_bin == FORCE_BIN)
17438 sprintf((char *)newval, " ++bin");
17439 else if (eap->force_bin == FORCE_NOBIN)
17440 sprintf((char *)newval, " ++nobin");
17441 else
17442 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017443
17444 if (eap->read_edit)
17445 STRCAT(newval, " ++edit");
17446
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017447 if (eap->force_ff != 0)
17448 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17449 eap->cmd + eap->force_ff);
17450# ifdef FEAT_MBYTE
17451 if (eap->force_enc != 0)
17452 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17453 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017454 if (eap->bad_char != 0)
17455 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17456 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017457# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017458 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017459 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460}
17461#endif
17462
17463/*
17464 * Get the value of internal variable "name".
17465 * Return OK or FAIL.
17466 */
17467 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017468get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 char_u *name;
17470 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017472 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473{
17474 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 typval_T *tv = NULL;
17476 typval_T atv;
17477 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479
17480 /* truncate the name, so that we can use strcmp() */
17481 cc = name[len];
17482 name[len] = NUL;
17483
17484 /*
17485 * Check for "b:changedtick".
17486 */
17487 if (STRCMP(name, "b:changedtick") == 0)
17488 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017489 atv.v_type = VAR_NUMBER;
17490 atv.vval.v_number = curbuf->b_changedtick;
17491 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 }
17493
17494 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 * Check for user-defined variables.
17496 */
17497 else
17498 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017499 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 }
17503
Bram Moolenaare9a41262005-01-15 22:18:47 +000017504 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017506 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 ret = FAIL;
17509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017510 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017511 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512
17513 name[len] = cc;
17514
17515 return ret;
17516}
17517
17518/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017519 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17520 * Also handle function call with Funcref variable: func(expr)
17521 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17522 */
17523 static int
17524handle_subscript(arg, rettv, evaluate, verbose)
17525 char_u **arg;
17526 typval_T *rettv;
17527 int evaluate; /* do more than finding the end */
17528 int verbose; /* give error messages */
17529{
17530 int ret = OK;
17531 dict_T *selfdict = NULL;
17532 char_u *s;
17533 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017534 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017535
17536 while (ret == OK
17537 && (**arg == '['
17538 || (**arg == '.' && rettv->v_type == VAR_DICT)
17539 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17540 && !vim_iswhite(*(*arg - 1)))
17541 {
17542 if (**arg == '(')
17543 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017544 /* need to copy the funcref so that we can clear rettv */
17545 functv = *rettv;
17546 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017547
17548 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017549 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017550 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017551 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17552 &len, evaluate, selfdict);
17553
17554 /* Clear the funcref afterwards, so that deleting it while
17555 * evaluating the arguments is possible (see test55). */
17556 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017557
17558 /* Stop the expression evaluation when immediately aborting on
17559 * error, or when an interrupt occurred or an exception was thrown
17560 * but not caught. */
17561 if (aborting())
17562 {
17563 if (ret == OK)
17564 clear_tv(rettv);
17565 ret = FAIL;
17566 }
17567 dict_unref(selfdict);
17568 selfdict = NULL;
17569 }
17570 else /* **arg == '[' || **arg == '.' */
17571 {
17572 dict_unref(selfdict);
17573 if (rettv->v_type == VAR_DICT)
17574 {
17575 selfdict = rettv->vval.v_dict;
17576 if (selfdict != NULL)
17577 ++selfdict->dv_refcount;
17578 }
17579 else
17580 selfdict = NULL;
17581 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17582 {
17583 clear_tv(rettv);
17584 ret = FAIL;
17585 }
17586 }
17587 }
17588 dict_unref(selfdict);
17589 return ret;
17590}
17591
17592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017593 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17594 * value).
17595 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017596 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017597alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017598{
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017600}
17601
17602/*
17603 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 * The string "s" must have been allocated, it is consumed.
17605 * Return NULL for out of memory, the variable otherwise.
17606 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017607 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017608alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 char_u *s;
17610{
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613 rettv = alloc_tv();
17614 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017616 rettv->v_type = VAR_STRING;
17617 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 }
17619 else
17620 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017621 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622}
17623
17624/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017625 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017627 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017629 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630{
17631 if (varp != NULL)
17632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017633 switch (varp->v_type)
17634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017635 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017636 func_unref(varp->vval.v_string);
17637 /*FALLTHROUGH*/
17638 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017639 vim_free(varp->vval.v_string);
17640 break;
17641 case VAR_LIST:
17642 list_unref(varp->vval.v_list);
17643 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017644 case VAR_DICT:
17645 dict_unref(varp->vval.v_dict);
17646 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017647 case VAR_NUMBER:
17648 case VAR_UNKNOWN:
17649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017650 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017651 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017652 break;
17653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 vim_free(varp);
17655 }
17656}
17657
17658/*
17659 * Free the memory for a variable value and set the value to NULL or 0.
17660 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017661 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017663 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664{
17665 if (varp != NULL)
17666 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017667 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017669 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017670 func_unref(varp->vval.v_string);
17671 /*FALLTHROUGH*/
17672 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017673 vim_free(varp->vval.v_string);
17674 varp->vval.v_string = NULL;
17675 break;
17676 case VAR_LIST:
17677 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017678 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017679 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017680 case VAR_DICT:
17681 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017682 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017683 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017685 varp->vval.v_number = 0;
17686 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687 case VAR_UNKNOWN:
17688 break;
17689 default:
17690 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017692 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 }
17694}
17695
17696/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697 * Set the value of a variable to NULL without freeing items.
17698 */
17699 static void
17700init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017701 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017702{
17703 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017704 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017705}
17706
17707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 * Get the number value of a variable.
17709 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017710 * For incompatible types, return 0.
17711 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17712 * caller of incompatible types: it sets *denote to TRUE if "denote"
17713 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 */
17715 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017717 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017719 int error = FALSE;
17720
17721 return get_tv_number_chk(varp, &error); /* return 0L on error */
17722}
17723
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017724 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017725get_tv_number_chk(varp, denote)
17726 typval_T *varp;
17727 int *denote;
17728{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017729 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017731 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017733 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017734 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017735 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017736 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017737 break;
17738 case VAR_STRING:
17739 if (varp->vval.v_string != NULL)
17740 vim_str2nr(varp->vval.v_string, NULL, NULL,
17741 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017742 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017743 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017744 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017745 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017746 case VAR_DICT:
17747 EMSG(_("E728: Using a Dictionary as a number"));
17748 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017750 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017751 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017753 if (denote == NULL) /* useful for values that must be unsigned */
17754 n = -1;
17755 else
17756 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017757 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758}
17759
17760/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017761 * Get the lnum from the first argument.
17762 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017763 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 */
17765 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017766get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768{
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 linenr_T lnum;
17771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017772 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 if (lnum == 0) /* no valid number, try using line() */
17774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775 rettv.v_type = VAR_NUMBER;
17776 f_line(argvars, &rettv);
17777 lnum = rettv.vval.v_number;
17778 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 }
17780 return lnum;
17781}
17782
17783/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017784 * Get the lnum from the first argument.
17785 * Also accepts "$", then "buf" is used.
17786 * Returns 0 on error.
17787 */
17788 static linenr_T
17789get_tv_lnum_buf(argvars, buf)
17790 typval_T *argvars;
17791 buf_T *buf;
17792{
17793 if (argvars[0].v_type == VAR_STRING
17794 && argvars[0].vval.v_string != NULL
17795 && argvars[0].vval.v_string[0] == '$'
17796 && buf != NULL)
17797 return buf->b_ml.ml_line_count;
17798 return get_tv_number_chk(&argvars[0], NULL);
17799}
17800
17801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 * Get the string value of a variable.
17803 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017804 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17805 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 * If the String variable has never been set, return an empty string.
17807 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017808 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17809 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 */
17811 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017813 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814{
17815 static char_u mybuf[NUMBUFLEN];
17816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017817 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818}
17819
17820 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017821get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017823 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017825 char_u *res = get_tv_string_buf_chk(varp, buf);
17826
17827 return res != NULL ? res : (char_u *)"";
17828}
17829
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017830 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017831get_tv_string_chk(varp)
17832 typval_T *varp;
17833{
17834 static char_u mybuf[NUMBUFLEN];
17835
17836 return get_tv_string_buf_chk(varp, mybuf);
17837}
17838
17839 static char_u *
17840get_tv_string_buf_chk(varp, buf)
17841 typval_T *varp;
17842 char_u *buf;
17843{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017844 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017846 case VAR_NUMBER:
17847 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17848 return buf;
17849 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017850 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017851 break;
17852 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017853 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017854 break;
17855 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017856 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017857 break;
17858 case VAR_STRING:
17859 if (varp->vval.v_string != NULL)
17860 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017861 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017862 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017863 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017866 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867}
17868
17869/*
17870 * Find variable "name" in the list of variables.
17871 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017872 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017873 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017874 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017876 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017877find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017879 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017882 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
Bram Moolenaara7043832005-01-21 11:56:39 +000017884 ht = find_var_ht(name, &varname);
17885 if (htp != NULL)
17886 *htp = ht;
17887 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017889 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890}
17891
17892/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017894 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017897find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017898 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017899 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017900 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017901{
Bram Moolenaar33570922005-01-25 22:26:29 +000017902 hashitem_T *hi;
17903
17904 if (*varname == NUL)
17905 {
17906 /* Must be something like "s:", otherwise "ht" would be NULL. */
17907 switch (varname[-2])
17908 {
17909 case 's': return &SCRIPT_SV(current_SID).sv_var;
17910 case 'g': return &globvars_var;
17911 case 'v': return &vimvars_var;
17912 case 'b': return &curbuf->b_bufvar;
17913 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017914#ifdef FEAT_WINDOWS
17915 case 't': return &curtab->tp_winvar;
17916#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017917 case 'l': return current_funccal == NULL
17918 ? NULL : &current_funccal->l_vars_var;
17919 case 'a': return current_funccal == NULL
17920 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017921 }
17922 return NULL;
17923 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017924
17925 hi = hash_find(ht, varname);
17926 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017927 {
17928 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017929 * worked find the variable again. Don't auto-load a script if it was
17930 * loaded already, otherwise it would be loaded every time when
17931 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017932 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017933 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017934 hi = hash_find(ht, varname);
17935 if (HASHITEM_EMPTY(hi))
17936 return NULL;
17937 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017938 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017939}
17940
17941/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017942 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017943 * Set "varname" to the start of name without ':'.
17944 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017945 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017946find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 char_u *name;
17948 char_u **varname;
17949{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017950 hashitem_T *hi;
17951
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 if (name[1] != ':')
17953 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017954 /* The name must not start with a colon or #. */
17955 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 return NULL;
17957 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017958
17959 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017960 hi = hash_find(&compat_hashtab, name);
17961 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017962 return &compat_hashtab;
17963
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017965 return &globvarht; /* global variable */
17966 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 }
17968 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017969 if (*name == 'g') /* global variable */
17970 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017971 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17972 */
17973 if (vim_strchr(name + 2, ':') != NULL
17974 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017975 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017977 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017979 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017980#ifdef FEAT_WINDOWS
17981 if (*name == 't') /* tab page variable */
17982 return &curtab->tp_vars.dv_hashtab;
17983#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017984 if (*name == 'v') /* v: variable */
17985 return &vimvarht;
17986 if (*name == 'a' && current_funccal != NULL) /* function argument */
17987 return &current_funccal->l_avars.dv_hashtab;
17988 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17989 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 if (*name == 's' /* script variable */
17991 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17992 return &SCRIPT_VARS(current_SID);
17993 return NULL;
17994}
17995
17996/*
17997 * Get the string value of a (global/local) variable.
17998 * Returns NULL when it doesn't exist.
17999 */
18000 char_u *
18001get_var_value(name)
18002 char_u *name;
18003{
Bram Moolenaar33570922005-01-25 22:26:29 +000018004 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
Bram Moolenaara7043832005-01-21 11:56:39 +000018006 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 if (v == NULL)
18008 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018009 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010}
18011
18012/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018013 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 * sourcing this script and when executing functions defined in the script.
18015 */
18016 void
18017new_script_vars(id)
18018 scid_T id;
18019{
Bram Moolenaara7043832005-01-21 11:56:39 +000018020 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 hashtab_T *ht;
18022 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018023
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18025 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018026 /* Re-allocating ga_data means that an ht_array pointing to
18027 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018029 for (i = 1; i <= ga_scripts.ga_len; ++i)
18030 {
18031 ht = &SCRIPT_VARS(i);
18032 if (ht->ht_mask == HT_INIT_SIZE - 1)
18033 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018034 sv = &SCRIPT_SV(i);
18035 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018036 }
18037
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 while (ga_scripts.ga_len < id)
18039 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18041 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 }
18044 }
18045}
18046
18047/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18049 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 */
18051 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018052init_var_dict(dict, dict_var)
18053 dict_T *dict;
18054 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055{
Bram Moolenaar33570922005-01-25 22:26:29 +000018056 hash_init(&dict->dv_hashtab);
18057 dict->dv_refcount = 99999;
18058 dict_var->di_tv.vval.v_dict = dict;
18059 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018060 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18062 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063}
18064
18065/*
18066 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018067 * Frees all allocated variables and the value they contain.
18068 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 */
18070 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018071vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018072 hashtab_T *ht;
18073{
18074 vars_clear_ext(ht, TRUE);
18075}
18076
18077/*
18078 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18079 */
18080 static void
18081vars_clear_ext(ht, free_val)
18082 hashtab_T *ht;
18083 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084{
Bram Moolenaara7043832005-01-21 11:56:39 +000018085 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 hashitem_T *hi;
18087 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088
Bram Moolenaar33570922005-01-25 22:26:29 +000018089 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018090 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018091 for (hi = ht->ht_array; todo > 0; ++hi)
18092 {
18093 if (!HASHITEM_EMPTY(hi))
18094 {
18095 --todo;
18096
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018098 * ht_array might change then. hash_clear() takes care of it
18099 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 v = HI2DI(hi);
18101 if (free_val)
18102 clear_tv(&v->di_tv);
18103 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18104 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018105 }
18106 }
18107 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018108 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109}
18110
Bram Moolenaara7043832005-01-21 11:56:39 +000018111/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018112 * Delete a variable from hashtab "ht" at item "hi".
18113 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018116delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018117 hashtab_T *ht;
18118 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
Bram Moolenaar33570922005-01-25 22:26:29 +000018120 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018121
18122 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 clear_tv(&di->di_tv);
18124 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125}
18126
18127/*
18128 * List the value of one internal variable.
18129 */
18130 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018131list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018134 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018136 char_u *tofree;
18137 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018138 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018139
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018140 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018141 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018142 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018143 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144}
18145
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018147list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 char_u *prefix;
18149 char_u *name;
18150 int type;
18151 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018152 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153{
Bram Moolenaar31859182007-08-14 20:41:13 +000018154 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18155 msg_start();
18156 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 if (name != NULL) /* "a:" vars don't have a name stored */
18158 msg_puts(name);
18159 msg_putchar(' ');
18160 msg_advance(22);
18161 if (type == VAR_NUMBER)
18162 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018163 else if (type == VAR_FUNC)
18164 msg_putchar('*');
18165 else if (type == VAR_LIST)
18166 {
18167 msg_putchar('[');
18168 if (*string == '[')
18169 ++string;
18170 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018171 else if (type == VAR_DICT)
18172 {
18173 msg_putchar('{');
18174 if (*string == '{')
18175 ++string;
18176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 else
18178 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018179
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018181
18182 if (type == VAR_FUNC)
18183 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018184 if (*first)
18185 {
18186 msg_clr_eos();
18187 *first = FALSE;
18188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189}
18190
18191/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018192 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 * If the variable already exists, the value is updated.
18194 * Otherwise the variable is created.
18195 */
18196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018197set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018199 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201{
Bram Moolenaar33570922005-01-25 22:26:29 +000018202 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018204 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018205 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018207 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018208 {
18209 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18210 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18211 ? name[2] : name[0]))
18212 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018213 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018214 return;
18215 }
18216 if (function_exists(name))
18217 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018218 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018219 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018220 return;
18221 }
18222 }
18223
Bram Moolenaara7043832005-01-21 11:56:39 +000018224 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018225 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018226 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018227 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018228 return;
18229 }
18230
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018231 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018232 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018234 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018235 if (var_check_ro(v->di_flags, name)
18236 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018237 return;
18238 if (v->di_tv.v_type != tv->v_type
18239 && !((v->di_tv.v_type == VAR_STRING
18240 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018241 && (tv->v_type == VAR_STRING
18242 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018243 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018244 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018245 return;
18246 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018247
18248 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018249 * Handle setting internal v: variables separately: we don't change
18250 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 */
18252 if (ht == &vimvarht)
18253 {
18254 if (v->di_tv.v_type == VAR_STRING)
18255 {
18256 vim_free(v->di_tv.vval.v_string);
18257 if (copy || tv->v_type != VAR_STRING)
18258 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18259 else
18260 {
18261 /* Take over the string to avoid an extra alloc/free. */
18262 v->di_tv.vval.v_string = tv->vval.v_string;
18263 tv->vval.v_string = NULL;
18264 }
18265 }
18266 else if (v->di_tv.v_type != VAR_NUMBER)
18267 EMSG2(_(e_intern2), "set_var()");
18268 else
18269 v->di_tv.vval.v_number = get_tv_number(tv);
18270 return;
18271 }
18272
18273 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 }
18275 else /* add a new variable */
18276 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018277 /* Can't add "v:" variable. */
18278 if (ht == &vimvarht)
18279 {
18280 EMSG2(_(e_illvar), name);
18281 return;
18282 }
18283
Bram Moolenaar92124a32005-06-17 22:03:40 +000018284 /* Make sure the variable name is valid. */
18285 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018286 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18287 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018288 {
18289 EMSG2(_(e_illvar), varname);
18290 return;
18291 }
18292
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018293 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18294 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018295 if (v == NULL)
18296 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018297 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018298 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018300 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 return;
18302 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018303 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018308 else
18309 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018311 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018312 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314}
18315
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018316/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018317 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 * Also give an error message.
18319 */
18320 static int
18321var_check_ro(flags, name)
18322 int flags;
18323 char_u *name;
18324{
18325 if (flags & DI_FLAGS_RO)
18326 {
18327 EMSG2(_(e_readonlyvar), name);
18328 return TRUE;
18329 }
18330 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18331 {
18332 EMSG2(_(e_readonlysbx), name);
18333 return TRUE;
18334 }
18335 return FALSE;
18336}
18337
18338/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018339 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18340 * Also give an error message.
18341 */
18342 static int
18343var_check_fixed(flags, name)
18344 int flags;
18345 char_u *name;
18346{
18347 if (flags & DI_FLAGS_FIX)
18348 {
18349 EMSG2(_("E795: Cannot delete variable %s"), name);
18350 return TRUE;
18351 }
18352 return FALSE;
18353}
18354
18355/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018356 * Return TRUE if typeval "tv" is set to be locked (immutable).
18357 * Also give an error message, using "name".
18358 */
18359 static int
18360tv_check_lock(lock, name)
18361 int lock;
18362 char_u *name;
18363{
18364 if (lock & VAR_LOCKED)
18365 {
18366 EMSG2(_("E741: Value is locked: %s"),
18367 name == NULL ? (char_u *)_("Unknown") : name);
18368 return TRUE;
18369 }
18370 if (lock & VAR_FIXED)
18371 {
18372 EMSG2(_("E742: Cannot change value of %s"),
18373 name == NULL ? (char_u *)_("Unknown") : name);
18374 return TRUE;
18375 }
18376 return FALSE;
18377}
18378
18379/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018381 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018382 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018386 typval_T *from;
18387 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018389 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018390 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018391 switch (from->v_type)
18392 {
18393 case VAR_NUMBER:
18394 to->vval.v_number = from->vval.v_number;
18395 break;
18396 case VAR_STRING:
18397 case VAR_FUNC:
18398 if (from->vval.v_string == NULL)
18399 to->vval.v_string = NULL;
18400 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018401 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018402 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018403 if (from->v_type == VAR_FUNC)
18404 func_ref(to->vval.v_string);
18405 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018406 break;
18407 case VAR_LIST:
18408 if (from->vval.v_list == NULL)
18409 to->vval.v_list = NULL;
18410 else
18411 {
18412 to->vval.v_list = from->vval.v_list;
18413 ++to->vval.v_list->lv_refcount;
18414 }
18415 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018416 case VAR_DICT:
18417 if (from->vval.v_dict == NULL)
18418 to->vval.v_dict = NULL;
18419 else
18420 {
18421 to->vval.v_dict = from->vval.v_dict;
18422 ++to->vval.v_dict->dv_refcount;
18423 }
18424 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018425 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018427 break;
18428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429}
18430
18431/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018432 * Make a copy of an item.
18433 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018434 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18435 * reference to an already copied list/dict can be used.
18436 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018437 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018438 static int
18439item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018440 typval_T *from;
18441 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018442 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018443 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018444{
18445 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018446 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018447
Bram Moolenaar33570922005-01-25 22:26:29 +000018448 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018449 {
18450 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018451 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018452 }
18453 ++recurse;
18454
18455 switch (from->v_type)
18456 {
18457 case VAR_NUMBER:
18458 case VAR_STRING:
18459 case VAR_FUNC:
18460 copy_tv(from, to);
18461 break;
18462 case VAR_LIST:
18463 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018464 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018465 if (from->vval.v_list == NULL)
18466 to->vval.v_list = NULL;
18467 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18468 {
18469 /* use the copy made earlier */
18470 to->vval.v_list = from->vval.v_list->lv_copylist;
18471 ++to->vval.v_list->lv_refcount;
18472 }
18473 else
18474 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18475 if (to->vval.v_list == NULL)
18476 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018477 break;
18478 case VAR_DICT:
18479 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018480 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018481 if (from->vval.v_dict == NULL)
18482 to->vval.v_dict = NULL;
18483 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18484 {
18485 /* use the copy made earlier */
18486 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18487 ++to->vval.v_dict->dv_refcount;
18488 }
18489 else
18490 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18491 if (to->vval.v_dict == NULL)
18492 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018493 break;
18494 default:
18495 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018496 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018497 }
18498 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018499 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018500}
18501
18502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 * ":echo expr1 ..." print each argument separated with a space, add a
18504 * newline at the end.
18505 * ":echon expr1 ..." print each argument plain.
18506 */
18507 void
18508ex_echo(eap)
18509 exarg_T *eap;
18510{
18511 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018512 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018513 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 char_u *p;
18515 int needclr = TRUE;
18516 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018517 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518
18519 if (eap->skip)
18520 ++emsg_skip;
18521 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18522 {
18523 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018524 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 {
18526 /*
18527 * Report the invalid expression unless the expression evaluation
18528 * has been cancelled due to an aborting error, an interrupt, or an
18529 * exception.
18530 */
18531 if (!aborting())
18532 EMSG2(_(e_invexpr2), p);
18533 break;
18534 }
18535 if (!eap->skip)
18536 {
18537 if (atstart)
18538 {
18539 atstart = FALSE;
18540 /* Call msg_start() after eval1(), evaluating the expression
18541 * may cause a message to appear. */
18542 if (eap->cmdidx == CMD_echo)
18543 msg_start();
18544 }
18545 else if (eap->cmdidx == CMD_echo)
18546 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018547 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018548 if (p != NULL)
18549 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018551 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018553 if (*p != TAB && needclr)
18554 {
18555 /* remove any text still there from the command */
18556 msg_clr_eos();
18557 needclr = FALSE;
18558 }
18559 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 }
18561 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018562 {
18563#ifdef FEAT_MBYTE
18564 if (has_mbyte)
18565 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018566 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018567
18568 (void)msg_outtrans_len_attr(p, i, echo_attr);
18569 p += i - 1;
18570 }
18571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018573 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018576 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 arg = skipwhite(arg);
18580 }
18581 eap->nextcmd = check_nextcmd(arg);
18582
18583 if (eap->skip)
18584 --emsg_skip;
18585 else
18586 {
18587 /* remove text that may still be there from the command */
18588 if (needclr)
18589 msg_clr_eos();
18590 if (eap->cmdidx == CMD_echo)
18591 msg_end();
18592 }
18593}
18594
18595/*
18596 * ":echohl {name}".
18597 */
18598 void
18599ex_echohl(eap)
18600 exarg_T *eap;
18601{
18602 int id;
18603
18604 id = syn_name2id(eap->arg);
18605 if (id == 0)
18606 echo_attr = 0;
18607 else
18608 echo_attr = syn_id2attr(id);
18609}
18610
18611/*
18612 * ":execute expr1 ..." execute the result of an expression.
18613 * ":echomsg expr1 ..." Print a message
18614 * ":echoerr expr1 ..." Print an error
18615 * Each gets spaces around each argument and a newline at the end for
18616 * echo commands
18617 */
18618 void
18619ex_execute(eap)
18620 exarg_T *eap;
18621{
18622 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018623 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 int ret = OK;
18625 char_u *p;
18626 garray_T ga;
18627 int len;
18628 int save_did_emsg;
18629
18630 ga_init2(&ga, 1, 80);
18631
18632 if (eap->skip)
18633 ++emsg_skip;
18634 while (*arg != NUL && *arg != '|' && *arg != '\n')
18635 {
18636 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 {
18639 /*
18640 * Report the invalid expression unless the expression evaluation
18641 * has been cancelled due to an aborting error, an interrupt, or an
18642 * exception.
18643 */
18644 if (!aborting())
18645 EMSG2(_(e_invexpr2), p);
18646 ret = FAIL;
18647 break;
18648 }
18649
18650 if (!eap->skip)
18651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 len = (int)STRLEN(p);
18654 if (ga_grow(&ga, len + 2) == FAIL)
18655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 ret = FAIL;
18658 break;
18659 }
18660 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 ga.ga_len += len;
18664 }
18665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 arg = skipwhite(arg);
18668 }
18669
18670 if (ret != FAIL && ga.ga_data != NULL)
18671 {
18672 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018675 out_flush();
18676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 else if (eap->cmdidx == CMD_echoerr)
18678 {
18679 /* We don't want to abort following commands, restore did_emsg. */
18680 save_did_emsg = did_emsg;
18681 EMSG((char_u *)ga.ga_data);
18682 if (!force_abort)
18683 did_emsg = save_did_emsg;
18684 }
18685 else if (eap->cmdidx == CMD_execute)
18686 do_cmdline((char_u *)ga.ga_data,
18687 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18688 }
18689
18690 ga_clear(&ga);
18691
18692 if (eap->skip)
18693 --emsg_skip;
18694
18695 eap->nextcmd = check_nextcmd(arg);
18696}
18697
18698/*
18699 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18700 * "arg" points to the "&" or '+' when called, to "option" when returning.
18701 * Returns NULL when no option name found. Otherwise pointer to the char
18702 * after the option name.
18703 */
18704 static char_u *
18705find_option_end(arg, opt_flags)
18706 char_u **arg;
18707 int *opt_flags;
18708{
18709 char_u *p = *arg;
18710
18711 ++p;
18712 if (*p == 'g' && p[1] == ':')
18713 {
18714 *opt_flags = OPT_GLOBAL;
18715 p += 2;
18716 }
18717 else if (*p == 'l' && p[1] == ':')
18718 {
18719 *opt_flags = OPT_LOCAL;
18720 p += 2;
18721 }
18722 else
18723 *opt_flags = 0;
18724
18725 if (!ASCII_ISALPHA(*p))
18726 return NULL;
18727 *arg = p;
18728
18729 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18730 p += 4; /* termcap option */
18731 else
18732 while (ASCII_ISALPHA(*p))
18733 ++p;
18734 return p;
18735}
18736
18737/*
18738 * ":function"
18739 */
18740 void
18741ex_function(eap)
18742 exarg_T *eap;
18743{
18744 char_u *theline;
18745 int j;
18746 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 char_u *name = NULL;
18749 char_u *p;
18750 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018751 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 garray_T newargs;
18753 garray_T newlines;
18754 int varargs = FALSE;
18755 int mustend = FALSE;
18756 int flags = 0;
18757 ufunc_T *fp;
18758 int indent;
18759 int nesting;
18760 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 dictitem_T *v;
18762 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018763 static int func_nr = 0; /* number for nameless function */
18764 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018765 hashtab_T *ht;
18766 int todo;
18767 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018768 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769
18770 /*
18771 * ":function" without argument: list functions.
18772 */
18773 if (ends_excmd(*eap->arg))
18774 {
18775 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018776 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018777 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018778 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018779 {
18780 if (!HASHITEM_EMPTY(hi))
18781 {
18782 --todo;
18783 fp = HI2UF(hi);
18784 if (!isdigit(*fp->uf_name))
18785 list_func_head(fp, FALSE);
18786 }
18787 }
18788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 eap->nextcmd = check_nextcmd(eap->arg);
18790 return;
18791 }
18792
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018793 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018794 * ":function /pat": list functions matching pattern.
18795 */
18796 if (*eap->arg == '/')
18797 {
18798 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18799 if (!eap->skip)
18800 {
18801 regmatch_T regmatch;
18802
18803 c = *p;
18804 *p = NUL;
18805 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18806 *p = c;
18807 if (regmatch.regprog != NULL)
18808 {
18809 regmatch.rm_ic = p_ic;
18810
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018811 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018812 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18813 {
18814 if (!HASHITEM_EMPTY(hi))
18815 {
18816 --todo;
18817 fp = HI2UF(hi);
18818 if (!isdigit(*fp->uf_name)
18819 && vim_regexec(&regmatch, fp->uf_name, 0))
18820 list_func_head(fp, FALSE);
18821 }
18822 }
18823 }
18824 }
18825 if (*p == '/')
18826 ++p;
18827 eap->nextcmd = check_nextcmd(p);
18828 return;
18829 }
18830
18831 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018832 * Get the function name. There are these situations:
18833 * func normal function name
18834 * "name" == func, "fudi.fd_dict" == NULL
18835 * dict.func new dictionary entry
18836 * "name" == NULL, "fudi.fd_dict" set,
18837 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18838 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018839 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018840 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18841 * dict.func existing dict entry that's not a Funcref
18842 * "name" == NULL, "fudi.fd_dict" set,
18843 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018846 name = trans_function_name(&p, eap->skip, 0, &fudi);
18847 paren = (vim_strchr(p, '(') != NULL);
18848 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 {
18850 /*
18851 * Return on an invalid expression in braces, unless the expression
18852 * evaluation has been cancelled due to an aborting error, an
18853 * interrupt, or an exception.
18854 */
18855 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018857 if (!eap->skip && fudi.fd_newkey != NULL)
18858 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018859 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 else
18863 eap->skip = TRUE;
18864 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018865
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 /* An error in a function call during evaluation of an expression in magic
18867 * braces should not cause the function not to be defined. */
18868 saved_did_emsg = did_emsg;
18869 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
18871 /*
18872 * ":function func" with only function name: list function.
18873 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018874 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 {
18876 if (!ends_excmd(*skipwhite(p)))
18877 {
18878 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018879 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 }
18881 eap->nextcmd = check_nextcmd(p);
18882 if (eap->nextcmd != NULL)
18883 *p = NUL;
18884 if (!eap->skip && !got_int)
18885 {
18886 fp = find_func(name);
18887 if (fp != NULL)
18888 {
18889 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018890 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018892 if (FUNCLINE(fp, j) == NULL)
18893 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 msg_putchar('\n');
18895 msg_outnum((long)(j + 1));
18896 if (j < 9)
18897 msg_putchar(' ');
18898 if (j < 99)
18899 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018900 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 out_flush(); /* show a line at a time */
18902 ui_breakcheck();
18903 }
18904 if (!got_int)
18905 {
18906 msg_putchar('\n');
18907 msg_puts((char_u *)" endfunction");
18908 }
18909 }
18910 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018911 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018913 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 }
18915
18916 /*
18917 * ":function name(arg1, arg2)" Define function.
18918 */
18919 p = skipwhite(p);
18920 if (*p != '(')
18921 {
18922 if (!eap->skip)
18923 {
18924 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018925 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 }
18927 /* attempt to continue by skipping some text */
18928 if (vim_strchr(p, '(') != NULL)
18929 p = vim_strchr(p, '(');
18930 }
18931 p = skipwhite(p + 1);
18932
18933 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18934 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18935
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018936 if (!eap->skip)
18937 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018938 /* Check the name of the function. Unless it's a dictionary function
18939 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018940 if (name != NULL)
18941 arg = name;
18942 else
18943 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018944 if (arg != NULL && (fudi.fd_di == NULL
18945 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018946 {
18947 if (*arg == K_SPECIAL)
18948 j = 3;
18949 else
18950 j = 0;
18951 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18952 : eval_isnamec(arg[j])))
18953 ++j;
18954 if (arg[j] != NUL)
18955 emsg_funcname(_(e_invarg2), arg);
18956 }
18957 }
18958
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 /*
18960 * Isolate the arguments: "arg1, arg2, ...)"
18961 */
18962 while (*p != ')')
18963 {
18964 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18965 {
18966 varargs = TRUE;
18967 p += 3;
18968 mustend = TRUE;
18969 }
18970 else
18971 {
18972 arg = p;
18973 while (ASCII_ISALNUM(*p) || *p == '_')
18974 ++p;
18975 if (arg == p || isdigit(*arg)
18976 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18977 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18978 {
18979 if (!eap->skip)
18980 EMSG2(_("E125: Illegal argument: %s"), arg);
18981 break;
18982 }
18983 if (ga_grow(&newargs, 1) == FAIL)
18984 goto erret;
18985 c = *p;
18986 *p = NUL;
18987 arg = vim_strsave(arg);
18988 if (arg == NULL)
18989 goto erret;
18990 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18991 *p = c;
18992 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 if (*p == ',')
18994 ++p;
18995 else
18996 mustend = TRUE;
18997 }
18998 p = skipwhite(p);
18999 if (mustend && *p != ')')
19000 {
19001 if (!eap->skip)
19002 EMSG2(_(e_invarg2), eap->arg);
19003 break;
19004 }
19005 }
19006 ++p; /* skip the ')' */
19007
Bram Moolenaare9a41262005-01-15 22:18:47 +000019008 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 for (;;)
19010 {
19011 p = skipwhite(p);
19012 if (STRNCMP(p, "range", 5) == 0)
19013 {
19014 flags |= FC_RANGE;
19015 p += 5;
19016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019017 else if (STRNCMP(p, "dict", 4) == 0)
19018 {
19019 flags |= FC_DICT;
19020 p += 4;
19021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 else if (STRNCMP(p, "abort", 5) == 0)
19023 {
19024 flags |= FC_ABORT;
19025 p += 5;
19026 }
19027 else
19028 break;
19029 }
19030
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019031 /* When there is a line break use what follows for the function body.
19032 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19033 if (*p == '\n')
19034 line_arg = p + 1;
19035 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 EMSG(_(e_trailing));
19037
19038 /*
19039 * Read the body of the function, until ":endfunction" is found.
19040 */
19041 if (KeyTyped)
19042 {
19043 /* Check if the function already exists, don't let the user type the
19044 * whole function before telling him it doesn't work! For a script we
19045 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019046 if (!eap->skip && !eap->forceit)
19047 {
19048 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19049 EMSG(_(e_funcdict));
19050 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019051 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019054 if (!eap->skip && did_emsg)
19055 goto erret;
19056
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 msg_putchar('\n'); /* don't overwrite the function name */
19058 cmdline_row = msg_row;
19059 }
19060
19061 indent = 2;
19062 nesting = 0;
19063 for (;;)
19064 {
19065 msg_scroll = TRUE;
19066 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019067 sourcing_lnum_off = sourcing_lnum;
19068
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019069 if (line_arg != NULL)
19070 {
19071 /* Use eap->arg, split up in parts by line breaks. */
19072 theline = line_arg;
19073 p = vim_strchr(theline, '\n');
19074 if (p == NULL)
19075 line_arg += STRLEN(line_arg);
19076 else
19077 {
19078 *p = NUL;
19079 line_arg = p + 1;
19080 }
19081 }
19082 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 theline = getcmdline(':', 0L, indent);
19084 else
19085 theline = eap->getline(':', eap->cookie, indent);
19086 if (KeyTyped)
19087 lines_left = Rows - 1;
19088 if (theline == NULL)
19089 {
19090 EMSG(_("E126: Missing :endfunction"));
19091 goto erret;
19092 }
19093
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019094 /* Detect line continuation: sourcing_lnum increased more than one. */
19095 if (sourcing_lnum > sourcing_lnum_off + 1)
19096 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19097 else
19098 sourcing_lnum_off = 0;
19099
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 if (skip_until != NULL)
19101 {
19102 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19103 * don't check for ":endfunc". */
19104 if (STRCMP(theline, skip_until) == 0)
19105 {
19106 vim_free(skip_until);
19107 skip_until = NULL;
19108 }
19109 }
19110 else
19111 {
19112 /* skip ':' and blanks*/
19113 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19114 ;
19115
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019116 /* Check for "endfunction". */
19117 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019119 if (line_arg == NULL)
19120 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 break;
19122 }
19123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019124 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 * at "end". */
19126 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19127 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019128 else if (STRNCMP(p, "if", 2) == 0
19129 || STRNCMP(p, "wh", 2) == 0
19130 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 || STRNCMP(p, "try", 3) == 0)
19132 indent += 2;
19133
19134 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019135 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019137 if (*p == '!')
19138 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 p += eval_fname_script(p);
19140 if (ASCII_ISALPHA(*p))
19141 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019142 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 if (*skipwhite(p) == '(')
19144 {
19145 ++nesting;
19146 indent += 2;
19147 }
19148 }
19149 }
19150
19151 /* Check for ":append" or ":insert". */
19152 p = skip_range(p, NULL);
19153 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19154 || (p[0] == 'i'
19155 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19156 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19157 skip_until = vim_strsave((char_u *)".");
19158
19159 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19160 arg = skipwhite(skiptowhite(p));
19161 if (arg[0] == '<' && arg[1] =='<'
19162 && ((p[0] == 'p' && p[1] == 'y'
19163 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19164 || (p[0] == 'p' && p[1] == 'e'
19165 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19166 || (p[0] == 't' && p[1] == 'c'
19167 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19168 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19169 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019170 || (p[0] == 'm' && p[1] == 'z'
19171 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 ))
19173 {
19174 /* ":python <<" continues until a dot, like ":append" */
19175 p = skipwhite(arg + 2);
19176 if (*p == NUL)
19177 skip_until = vim_strsave((char_u *)".");
19178 else
19179 skip_until = vim_strsave(p);
19180 }
19181 }
19182
19183 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019184 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019185 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019186 if (line_arg == NULL)
19187 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019189 }
19190
19191 /* Copy the line to newly allocated memory. get_one_sourceline()
19192 * allocates 250 bytes per line, this saves 80% on average. The cost
19193 * is an extra alloc/free. */
19194 p = vim_strsave(theline);
19195 if (p != NULL)
19196 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019197 if (line_arg == NULL)
19198 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019199 theline = p;
19200 }
19201
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019202 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19203
19204 /* Add NULL lines for continuation lines, so that the line count is
19205 * equal to the index in the growarray. */
19206 while (sourcing_lnum_off-- > 0)
19207 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019208
19209 /* Check for end of eap->arg. */
19210 if (line_arg != NULL && *line_arg == NUL)
19211 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 }
19213
19214 /* Don't define the function when skipping commands or when an error was
19215 * detected. */
19216 if (eap->skip || did_emsg)
19217 goto erret;
19218
19219 /*
19220 * If there are no errors, add the function
19221 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019222 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019224 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019225 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019226 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019227 emsg_funcname("E707: Function name conflicts with variable: %s",
19228 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019229 goto erret;
19230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019232 fp = find_func(name);
19233 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019235 if (!eap->forceit)
19236 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019237 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019238 goto erret;
19239 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019240 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019241 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019242 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019243 name);
19244 goto erret;
19245 }
19246 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019247 ga_clear_strings(&(fp->uf_args));
19248 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019249 vim_free(name);
19250 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 }
19253 else
19254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019255 char numbuf[20];
19256
19257 fp = NULL;
19258 if (fudi.fd_newkey == NULL && !eap->forceit)
19259 {
19260 EMSG(_(e_funcdict));
19261 goto erret;
19262 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019263 if (fudi.fd_di == NULL)
19264 {
19265 /* Can't add a function to a locked dictionary */
19266 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19267 goto erret;
19268 }
19269 /* Can't change an existing function if it is locked */
19270 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19271 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019272
19273 /* Give the function a sequential number. Can only be used with a
19274 * Funcref! */
19275 vim_free(name);
19276 sprintf(numbuf, "%d", ++func_nr);
19277 name = vim_strsave((char_u *)numbuf);
19278 if (name == NULL)
19279 goto erret;
19280 }
19281
19282 if (fp == NULL)
19283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019284 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019285 {
19286 int slen, plen;
19287 char_u *scriptname;
19288
19289 /* Check that the autoload name matches the script name. */
19290 j = FAIL;
19291 if (sourcing_name != NULL)
19292 {
19293 scriptname = autoload_name(name);
19294 if (scriptname != NULL)
19295 {
19296 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019297 plen = (int)STRLEN(p);
19298 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019299 if (slen > plen && fnamecmp(p,
19300 sourcing_name + slen - plen) == 0)
19301 j = OK;
19302 vim_free(scriptname);
19303 }
19304 }
19305 if (j == FAIL)
19306 {
19307 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19308 goto erret;
19309 }
19310 }
19311
19312 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 if (fp == NULL)
19314 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019315
19316 if (fudi.fd_dict != NULL)
19317 {
19318 if (fudi.fd_di == NULL)
19319 {
19320 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019321 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019322 if (fudi.fd_di == NULL)
19323 {
19324 vim_free(fp);
19325 goto erret;
19326 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019327 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19328 {
19329 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019330 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019331 goto erret;
19332 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019333 }
19334 else
19335 /* overwrite existing dict entry */
19336 clear_tv(&fudi.fd_di->di_tv);
19337 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019338 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019339 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019340 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019341
19342 /* behave like "dict" was used */
19343 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019344 }
19345
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019347 STRCPY(fp->uf_name, name);
19348 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019350 fp->uf_args = newargs;
19351 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019352#ifdef FEAT_PROFILE
19353 fp->uf_tml_count = NULL;
19354 fp->uf_tml_total = NULL;
19355 fp->uf_tml_self = NULL;
19356 fp->uf_profiling = FALSE;
19357 if (prof_def_func())
19358 func_do_profile(fp);
19359#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019360 fp->uf_varargs = varargs;
19361 fp->uf_flags = flags;
19362 fp->uf_calls = 0;
19363 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019364 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365
19366erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 ga_clear_strings(&newargs);
19368 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019369ret_free:
19370 vim_free(skip_until);
19371 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374}
19375
19376/*
19377 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019378 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019380 * flags:
19381 * TFN_INT: internal function name OK
19382 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 * Advances "pp" to just after the function name (if no error).
19384 */
19385 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019386trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 char_u **pp;
19388 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019389 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019390 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019392 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 char_u *start;
19394 char_u *end;
19395 int lead;
19396 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019399
19400 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019401 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019403
19404 /* Check for hard coded <SNR>: already translated function ID (from a user
19405 * command). */
19406 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19407 && (*pp)[2] == (int)KE_SNR)
19408 {
19409 *pp += 3;
19410 len = get_id_len(pp) + 3;
19411 return vim_strnsave(start, len);
19412 }
19413
19414 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19415 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019417 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019419
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019420 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19421 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019422 if (end == start)
19423 {
19424 if (!skip)
19425 EMSG(_("E129: Function name required"));
19426 goto theend;
19427 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019428 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019429 {
19430 /*
19431 * Report an invalid expression in braces, unless the expression
19432 * evaluation has been cancelled due to an aborting error, an
19433 * interrupt, or an exception.
19434 */
19435 if (!aborting())
19436 {
19437 if (end != NULL)
19438 EMSG2(_(e_invarg2), start);
19439 }
19440 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019441 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019442 goto theend;
19443 }
19444
19445 if (lv.ll_tv != NULL)
19446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019447 if (fdp != NULL)
19448 {
19449 fdp->fd_dict = lv.ll_dict;
19450 fdp->fd_newkey = lv.ll_newkey;
19451 lv.ll_newkey = NULL;
19452 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019453 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019454 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19455 {
19456 name = vim_strsave(lv.ll_tv->vval.v_string);
19457 *pp = end;
19458 }
19459 else
19460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019461 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19462 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019463 EMSG(_(e_funcref));
19464 else
19465 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019466 name = NULL;
19467 }
19468 goto theend;
19469 }
19470
19471 if (lv.ll_name == NULL)
19472 {
19473 /* Error found, but continue after the function name. */
19474 *pp = end;
19475 goto theend;
19476 }
19477
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019478 /* Check if the name is a Funcref. If so, use the value. */
19479 if (lv.ll_exp_name != NULL)
19480 {
19481 len = (int)STRLEN(lv.ll_exp_name);
19482 name = deref_func_name(lv.ll_exp_name, &len);
19483 if (name == lv.ll_exp_name)
19484 name = NULL;
19485 }
19486 else
19487 {
19488 len = (int)(end - *pp);
19489 name = deref_func_name(*pp, &len);
19490 if (name == *pp)
19491 name = NULL;
19492 }
19493 if (name != NULL)
19494 {
19495 name = vim_strsave(name);
19496 *pp = end;
19497 goto theend;
19498 }
19499
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019500 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019501 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019502 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019503 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19504 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19505 {
19506 /* When there was "s:" already or the name expanded to get a
19507 * leading "s:" then remove it. */
19508 lv.ll_name += 2;
19509 len -= 2;
19510 lead = 2;
19511 }
19512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019513 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019514 {
19515 if (lead == 2) /* skip over "s:" */
19516 lv.ll_name += 2;
19517 len = (int)(end - lv.ll_name);
19518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019519
19520 /*
19521 * Copy the function name to allocated memory.
19522 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19523 * Accept <SNR>123_name() outside a script.
19524 */
19525 if (skip)
19526 lead = 0; /* do nothing */
19527 else if (lead > 0)
19528 {
19529 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019530 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19531 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019532 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019533 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019534 if (current_SID <= 0)
19535 {
19536 EMSG(_(e_usingsid));
19537 goto theend;
19538 }
19539 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19540 lead += (int)STRLEN(sid_buf);
19541 }
19542 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019543 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019544 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019545 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019546 goto theend;
19547 }
19548 name = alloc((unsigned)(len + lead + 1));
19549 if (name != NULL)
19550 {
19551 if (lead > 0)
19552 {
19553 name[0] = K_SPECIAL;
19554 name[1] = KS_EXTRA;
19555 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019556 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019557 STRCPY(name + 3, sid_buf);
19558 }
19559 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19560 name[len + lead] = NUL;
19561 }
19562 *pp = end;
19563
19564theend:
19565 clear_lval(&lv);
19566 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567}
19568
19569/*
19570 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19571 * Return 2 if "p" starts with "s:".
19572 * Return 0 otherwise.
19573 */
19574 static int
19575eval_fname_script(p)
19576 char_u *p;
19577{
19578 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19579 || STRNICMP(p + 1, "SNR>", 4) == 0))
19580 return 5;
19581 if (p[0] == 's' && p[1] == ':')
19582 return 2;
19583 return 0;
19584}
19585
19586/*
19587 * Return TRUE if "p" starts with "<SID>" or "s:".
19588 * Only works if eval_fname_script() returned non-zero for "p"!
19589 */
19590 static int
19591eval_fname_sid(p)
19592 char_u *p;
19593{
19594 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19595}
19596
19597/*
19598 * List the head of the function: "name(arg1, arg2)".
19599 */
19600 static void
19601list_func_head(fp, indent)
19602 ufunc_T *fp;
19603 int indent;
19604{
19605 int j;
19606
19607 msg_start();
19608 if (indent)
19609 MSG_PUTS(" ");
19610 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019611 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 {
19613 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019614 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 }
19616 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019617 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019619 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 {
19621 if (j)
19622 MSG_PUTS(", ");
19623 msg_puts(FUNCARG(fp, j));
19624 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019625 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 {
19627 if (j)
19628 MSG_PUTS(", ");
19629 MSG_PUTS("...");
19630 }
19631 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019632 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019633 if (p_verbose > 0)
19634 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635}
19636
19637/*
19638 * Find a function by name, return pointer to it in ufuncs.
19639 * Return NULL for unknown function.
19640 */
19641 static ufunc_T *
19642find_func(name)
19643 char_u *name;
19644{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019645 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019647 hi = hash_find(&func_hashtab, name);
19648 if (!HASHITEM_EMPTY(hi))
19649 return HI2UF(hi);
19650 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651}
19652
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019653#if defined(EXITFREE) || defined(PROTO)
19654 void
19655free_all_functions()
19656{
19657 hashitem_T *hi;
19658
19659 /* Need to start all over every time, because func_free() may change the
19660 * hash table. */
19661 while (func_hashtab.ht_used > 0)
19662 for (hi = func_hashtab.ht_array; ; ++hi)
19663 if (!HASHITEM_EMPTY(hi))
19664 {
19665 func_free(HI2UF(hi));
19666 break;
19667 }
19668}
19669#endif
19670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019671/*
19672 * Return TRUE if a function "name" exists.
19673 */
19674 static int
19675function_exists(name)
19676 char_u *name;
19677{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019678 char_u *nm = name;
19679 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019680 int n = FALSE;
19681
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019682 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019683 nm = skipwhite(nm);
19684
19685 /* Only accept "funcname", "funcname ", "funcname (..." and
19686 * "funcname(...", not "funcname!...". */
19687 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019688 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019689 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019690 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019691 else
19692 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019693 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019694 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019695 return n;
19696}
19697
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019698/*
19699 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019700 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019701 */
19702 static int
19703builtin_function(name)
19704 char_u *name;
19705{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019706 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19707 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019708}
19709
Bram Moolenaar05159a02005-02-26 23:04:13 +000019710#if defined(FEAT_PROFILE) || defined(PROTO)
19711/*
19712 * Start profiling function "fp".
19713 */
19714 static void
19715func_do_profile(fp)
19716 ufunc_T *fp;
19717{
19718 fp->uf_tm_count = 0;
19719 profile_zero(&fp->uf_tm_self);
19720 profile_zero(&fp->uf_tm_total);
19721 if (fp->uf_tml_count == NULL)
19722 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19723 (sizeof(int) * fp->uf_lines.ga_len));
19724 if (fp->uf_tml_total == NULL)
19725 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19726 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19727 if (fp->uf_tml_self == NULL)
19728 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19729 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19730 fp->uf_tml_idx = -1;
19731 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19732 || fp->uf_tml_self == NULL)
19733 return; /* out of memory */
19734
19735 fp->uf_profiling = TRUE;
19736}
19737
19738/*
19739 * Dump the profiling results for all functions in file "fd".
19740 */
19741 void
19742func_dump_profile(fd)
19743 FILE *fd;
19744{
19745 hashitem_T *hi;
19746 int todo;
19747 ufunc_T *fp;
19748 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019749 ufunc_T **sorttab;
19750 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019751
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019752 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019753 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19754
Bram Moolenaar05159a02005-02-26 23:04:13 +000019755 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19756 {
19757 if (!HASHITEM_EMPTY(hi))
19758 {
19759 --todo;
19760 fp = HI2UF(hi);
19761 if (fp->uf_profiling)
19762 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019763 if (sorttab != NULL)
19764 sorttab[st_len++] = fp;
19765
Bram Moolenaar05159a02005-02-26 23:04:13 +000019766 if (fp->uf_name[0] == K_SPECIAL)
19767 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19768 else
19769 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19770 if (fp->uf_tm_count == 1)
19771 fprintf(fd, "Called 1 time\n");
19772 else
19773 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19774 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19775 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19776 fprintf(fd, "\n");
19777 fprintf(fd, "count total (s) self (s)\n");
19778
19779 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19780 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019781 if (FUNCLINE(fp, i) == NULL)
19782 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019783 prof_func_line(fd, fp->uf_tml_count[i],
19784 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019785 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19786 }
19787 fprintf(fd, "\n");
19788 }
19789 }
19790 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019791
19792 if (sorttab != NULL && st_len > 0)
19793 {
19794 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19795 prof_total_cmp);
19796 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19797 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19798 prof_self_cmp);
19799 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19800 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019801}
Bram Moolenaar73830342005-02-28 22:48:19 +000019802
19803 static void
19804prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19805 FILE *fd;
19806 ufunc_T **sorttab;
19807 int st_len;
19808 char *title;
19809 int prefer_self; /* when equal print only self time */
19810{
19811 int i;
19812 ufunc_T *fp;
19813
19814 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19815 fprintf(fd, "count total (s) self (s) function\n");
19816 for (i = 0; i < 20 && i < st_len; ++i)
19817 {
19818 fp = sorttab[i];
19819 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19820 prefer_self);
19821 if (fp->uf_name[0] == K_SPECIAL)
19822 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19823 else
19824 fprintf(fd, " %s()\n", fp->uf_name);
19825 }
19826 fprintf(fd, "\n");
19827}
19828
19829/*
19830 * Print the count and times for one function or function line.
19831 */
19832 static void
19833prof_func_line(fd, count, total, self, prefer_self)
19834 FILE *fd;
19835 int count;
19836 proftime_T *total;
19837 proftime_T *self;
19838 int prefer_self; /* when equal print only self time */
19839{
19840 if (count > 0)
19841 {
19842 fprintf(fd, "%5d ", count);
19843 if (prefer_self && profile_equal(total, self))
19844 fprintf(fd, " ");
19845 else
19846 fprintf(fd, "%s ", profile_msg(total));
19847 if (!prefer_self && profile_equal(total, self))
19848 fprintf(fd, " ");
19849 else
19850 fprintf(fd, "%s ", profile_msg(self));
19851 }
19852 else
19853 fprintf(fd, " ");
19854}
19855
19856/*
19857 * Compare function for total time sorting.
19858 */
19859 static int
19860#ifdef __BORLANDC__
19861_RTLENTRYF
19862#endif
19863prof_total_cmp(s1, s2)
19864 const void *s1;
19865 const void *s2;
19866{
19867 ufunc_T *p1, *p2;
19868
19869 p1 = *(ufunc_T **)s1;
19870 p2 = *(ufunc_T **)s2;
19871 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19872}
19873
19874/*
19875 * Compare function for self time sorting.
19876 */
19877 static int
19878#ifdef __BORLANDC__
19879_RTLENTRYF
19880#endif
19881prof_self_cmp(s1, s2)
19882 const void *s1;
19883 const void *s2;
19884{
19885 ufunc_T *p1, *p2;
19886
19887 p1 = *(ufunc_T **)s1;
19888 p2 = *(ufunc_T **)s2;
19889 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19890}
19891
Bram Moolenaar05159a02005-02-26 23:04:13 +000019892#endif
19893
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019894/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019895 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019896 * Return TRUE if a package was loaded.
19897 */
19898 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019899script_autoload(name, reload)
19900 char_u *name;
19901 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019902{
19903 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019904 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019905 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019906 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019907
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019908 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019909 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019910 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019911 return FALSE;
19912
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019913 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019914
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019915 /* Find the name in the list of previously loaded package names. Skip
19916 * "autoload/", it's always the same. */
19917 for (i = 0; i < ga_loaded.ga_len; ++i)
19918 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19919 break;
19920 if (!reload && i < ga_loaded.ga_len)
19921 ret = FALSE; /* was loaded already */
19922 else
19923 {
19924 /* Remember the name if it wasn't loaded already. */
19925 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19926 {
19927 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19928 tofree = NULL;
19929 }
19930
19931 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019932 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019933 ret = TRUE;
19934 }
19935
19936 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019937 return ret;
19938}
19939
19940/*
19941 * Return the autoload script name for a function or variable name.
19942 * Returns NULL when out of memory.
19943 */
19944 static char_u *
19945autoload_name(name)
19946 char_u *name;
19947{
19948 char_u *p;
19949 char_u *scriptname;
19950
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019951 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019952 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19953 if (scriptname == NULL)
19954 return FALSE;
19955 STRCPY(scriptname, "autoload/");
19956 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019957 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019958 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019959 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019960 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019961 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019962}
19963
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19965
19966/*
19967 * Function given to ExpandGeneric() to obtain the list of user defined
19968 * function names.
19969 */
19970 char_u *
19971get_user_func_name(xp, idx)
19972 expand_T *xp;
19973 int idx;
19974{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019975 static long_u done;
19976 static hashitem_T *hi;
19977 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978
19979 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019981 done = 0;
19982 hi = func_hashtab.ht_array;
19983 }
19984 if (done < func_hashtab.ht_used)
19985 {
19986 if (done++ > 0)
19987 ++hi;
19988 while (HASHITEM_EMPTY(hi))
19989 ++hi;
19990 fp = HI2UF(hi);
19991
19992 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19993 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994
19995 cat_func_name(IObuff, fp);
19996 if (xp->xp_context != EXPAND_USER_FUNC)
19997 {
19998 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019999 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 STRCAT(IObuff, ")");
20001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 return IObuff;
20003 }
20004 return NULL;
20005}
20006
20007#endif /* FEAT_CMDL_COMPL */
20008
20009/*
20010 * Copy the function name of "fp" to buffer "buf".
20011 * "buf" must be able to hold the function name plus three bytes.
20012 * Takes care of script-local function names.
20013 */
20014 static void
20015cat_func_name(buf, fp)
20016 char_u *buf;
20017 ufunc_T *fp;
20018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020019 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 {
20021 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020022 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 }
20024 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020025 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026}
20027
20028/*
20029 * ":delfunction {name}"
20030 */
20031 void
20032ex_delfunction(eap)
20033 exarg_T *eap;
20034{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020035 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 char_u *p;
20037 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039
20040 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020041 name = trans_function_name(&p, eap->skip, 0, &fudi);
20042 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020044 {
20045 if (fudi.fd_dict != NULL && !eap->skip)
20046 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 if (!ends_excmd(*skipwhite(p)))
20050 {
20051 vim_free(name);
20052 EMSG(_(e_trailing));
20053 return;
20054 }
20055 eap->nextcmd = check_nextcmd(p);
20056 if (eap->nextcmd != NULL)
20057 *p = NUL;
20058
20059 if (!eap->skip)
20060 fp = find_func(name);
20061 vim_free(name);
20062
20063 if (!eap->skip)
20064 {
20065 if (fp == NULL)
20066 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020067 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 return;
20069 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020070 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 {
20072 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20073 return;
20074 }
20075
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020076 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 /* Delete the dict item that refers to the function, it will
20079 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020080 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020082 else
20083 func_free(fp);
20084 }
20085}
20086
20087/*
20088 * Free a function and remove it from the list of functions.
20089 */
20090 static void
20091func_free(fp)
20092 ufunc_T *fp;
20093{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020094 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020095
20096 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020097 ga_clear_strings(&(fp->uf_args));
20098 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020099#ifdef FEAT_PROFILE
20100 vim_free(fp->uf_tml_count);
20101 vim_free(fp->uf_tml_total);
20102 vim_free(fp->uf_tml_self);
20103#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020105 /* remove the function from the function hashtable */
20106 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20107 if (HASHITEM_EMPTY(hi))
20108 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020109 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020110 hash_remove(&func_hashtab, hi);
20111
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020112 vim_free(fp);
20113}
20114
20115/*
20116 * Unreference a Function: decrement the reference count and free it when it
20117 * becomes zero. Only for numbered functions.
20118 */
20119 static void
20120func_unref(name)
20121 char_u *name;
20122{
20123 ufunc_T *fp;
20124
20125 if (name != NULL && isdigit(*name))
20126 {
20127 fp = find_func(name);
20128 if (fp == NULL)
20129 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020130 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020131 {
20132 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020133 * when "uf_calls" becomes zero. */
20134 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020135 func_free(fp);
20136 }
20137 }
20138}
20139
20140/*
20141 * Count a reference to a Function.
20142 */
20143 static void
20144func_ref(name)
20145 char_u *name;
20146{
20147 ufunc_T *fp;
20148
20149 if (name != NULL && isdigit(*name))
20150 {
20151 fp = find_func(name);
20152 if (fp == NULL)
20153 EMSG2(_(e_intern2), "func_ref()");
20154 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020155 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 }
20157}
20158
20159/*
20160 * Call a user function.
20161 */
20162 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020163call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 ufunc_T *fp; /* pointer to function */
20165 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 typval_T *argvars; /* arguments */
20167 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 linenr_T firstline; /* first line of range */
20169 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020170 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171{
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 char_u *save_sourcing_name;
20173 linenr_T save_sourcing_lnum;
20174 scid_T save_current_SID;
20175 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020176 int save_did_emsg;
20177 static int depth = 0;
20178 dictitem_T *v;
20179 int fixvar_idx = 0; /* index in fixvar[] */
20180 int i;
20181 int ai;
20182 char_u numbuf[NUMBUFLEN];
20183 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020184#ifdef FEAT_PROFILE
20185 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020186 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188
20189 /* If depth of calling is getting too high, don't execute the function */
20190 if (depth >= p_mfd)
20191 {
20192 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 rettv->v_type = VAR_NUMBER;
20194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 return;
20196 }
20197 ++depth;
20198
20199 line_breakcheck(); /* check for CTRL-C hit */
20200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020201 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020204 fc.rettv = rettv;
20205 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 fc.linenr = 0;
20207 fc.returned = FALSE;
20208 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020210 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 fc.dbg_tick = debug_tick;
20212
Bram Moolenaar33570922005-01-25 22:26:29 +000020213 /*
20214 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20215 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20216 * each argument variable and saves a lot of time.
20217 */
20218 /*
20219 * Init l: variables.
20220 */
20221 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020222 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020223 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020224 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20225 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020227 name = v->di_key;
20228 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20230 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20231 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020232 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020233 v->di_tv.vval.v_dict = selfdict;
20234 ++selfdict->dv_refcount;
20235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020236
Bram Moolenaar33570922005-01-25 22:26:29 +000020237 /*
20238 * Init a: variables.
20239 * Set a:0 to "argcount".
20240 * Set a:000 to a list with room for the "..." arguments.
20241 */
20242 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20243 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020244 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 v = &fc.fixvar[fixvar_idx++].var;
20246 STRCPY(v->di_key, "000");
20247 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20248 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20249 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020250 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020251 v->di_tv.vval.v_list = &fc.l_varlist;
20252 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20253 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020254 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020255
20256 /*
20257 * Set a:firstline to "firstline" and a:lastline to "lastline".
20258 * Set a:name to named arguments.
20259 * Set a:N to the "..." arguments.
20260 */
20261 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20262 (varnumber_T)firstline);
20263 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20264 (varnumber_T)lastline);
20265 for (i = 0; i < argcount; ++i)
20266 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020267 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020268 if (ai < 0)
20269 /* named argument a:name */
20270 name = FUNCARG(fp, i);
20271 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020272 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020273 /* "..." argument a:1, a:2, etc. */
20274 sprintf((char *)numbuf, "%d", ai + 1);
20275 name = numbuf;
20276 }
20277 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20278 {
20279 v = &fc.fixvar[fixvar_idx++].var;
20280 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20281 }
20282 else
20283 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020284 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20285 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020286 if (v == NULL)
20287 break;
20288 v->di_flags = DI_FLAGS_RO;
20289 }
20290 STRCPY(v->di_key, name);
20291 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20292
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020293 /* Note: the values are copied directly to avoid alloc/free.
20294 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020296 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020297
20298 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20299 {
20300 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20301 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020302 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020303 }
20304 }
20305
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 /* Don't redraw while executing the function. */
20307 ++RedrawingDisabled;
20308 save_sourcing_name = sourcing_name;
20309 save_sourcing_lnum = sourcing_lnum;
20310 sourcing_lnum = 1;
20311 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020312 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 if (sourcing_name != NULL)
20314 {
20315 if (save_sourcing_name != NULL
20316 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20317 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20318 else
20319 STRCPY(sourcing_name, "function ");
20320 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20321
20322 if (p_verbose >= 12)
20323 {
20324 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020325 verbose_enter_scroll();
20326
Bram Moolenaar555b2802005-05-19 21:08:39 +000020327 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 if (p_verbose >= 14)
20329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020331 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020332 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020333 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334
20335 msg_puts((char_u *)"(");
20336 for (i = 0; i < argcount; ++i)
20337 {
20338 if (i > 0)
20339 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020340 if (argvars[i].v_type == VAR_NUMBER)
20341 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 else
20343 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020344 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20345 if (s != NULL)
20346 {
20347 trunc_string(s, buf, MSG_BUF_CLEN);
20348 msg_puts(buf);
20349 vim_free(tofree);
20350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 }
20352 }
20353 msg_puts((char_u *)")");
20354 }
20355 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020356
20357 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 --no_wait_return;
20359 }
20360 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020361#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020362 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020363 {
20364 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20365 func_do_profile(fp);
20366 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020367 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020368 {
20369 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020370 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020371 profile_zero(&fp->uf_tm_children);
20372 }
20373 script_prof_save(&wait_start);
20374 }
20375#endif
20376
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020378 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 save_did_emsg = did_emsg;
20380 did_emsg = FALSE;
20381
20382 /* call do_cmdline() to execute the lines */
20383 do_cmdline(NULL, get_func_line, (void *)&fc,
20384 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20385
20386 --RedrawingDisabled;
20387
20388 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020389 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020391 clear_tv(rettv);
20392 rettv->v_type = VAR_NUMBER;
20393 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 }
20395
Bram Moolenaar05159a02005-02-26 23:04:13 +000020396#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020397 if (do_profiling == PROF_YES && (fp->uf_profiling
20398 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020399 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020400 profile_end(&call_start);
20401 profile_sub_wait(&wait_start, &call_start);
20402 profile_add(&fp->uf_tm_total, &call_start);
20403 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020404 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020405 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020406 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20407 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020408 }
20409 }
20410#endif
20411
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 /* when being verbose, mention the return value */
20413 if (p_verbose >= 12)
20414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020416 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020419 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020420 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020421 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20422 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020425 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020426 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020427 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020428 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020429
Bram Moolenaar555b2802005-05-19 21:08:39 +000020430 /* The value may be very long. Skip the middle part, so that we
20431 * have some idea how it starts and ends. smsg() would always
20432 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020433 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20434 if (s != NULL)
20435 {
20436 trunc_string(s, buf, MSG_BUF_CLEN);
20437 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20438 vim_free(tofree);
20439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 }
20441 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020442
20443 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 --no_wait_return;
20445 }
20446
20447 vim_free(sourcing_name);
20448 sourcing_name = save_sourcing_name;
20449 sourcing_lnum = save_sourcing_lnum;
20450 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020451#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020452 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020453 script_prof_restore(&wait_start);
20454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455
20456 if (p_verbose >= 12 && sourcing_name != NULL)
20457 {
20458 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020459 verbose_enter_scroll();
20460
Bram Moolenaar555b2802005-05-19 21:08:39 +000020461 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020463
20464 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 --no_wait_return;
20466 }
20467
20468 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020469 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 /* The a: variables typevals were not alloced, only free the allocated
20472 * variables. */
20473 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20474
20475 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 --depth;
20477}
20478
20479/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 * Add a number variable "name" to dict "dp" with value "nr".
20481 */
20482 static void
20483add_nr_var(dp, v, name, nr)
20484 dict_T *dp;
20485 dictitem_T *v;
20486 char *name;
20487 varnumber_T nr;
20488{
20489 STRCPY(v->di_key, name);
20490 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20491 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20492 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020493 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 v->di_tv.vval.v_number = nr;
20495}
20496
20497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 * ":return [expr]"
20499 */
20500 void
20501ex_return(eap)
20502 exarg_T *eap;
20503{
20504 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 int returning = FALSE;
20507
20508 if (current_funccal == NULL)
20509 {
20510 EMSG(_("E133: :return not inside a function"));
20511 return;
20512 }
20513
20514 if (eap->skip)
20515 ++emsg_skip;
20516
20517 eap->nextcmd = NULL;
20518 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 {
20521 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020522 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020524 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 }
20526 /* It's safer to return also on error. */
20527 else if (!eap->skip)
20528 {
20529 /*
20530 * Return unless the expression evaluation has been cancelled due to an
20531 * aborting error, an interrupt, or an exception.
20532 */
20533 if (!aborting())
20534 returning = do_return(eap, FALSE, TRUE, NULL);
20535 }
20536
20537 /* When skipping or the return gets pending, advance to the next command
20538 * in this line (!returning). Otherwise, ignore the rest of the line.
20539 * Following lines will be ignored by get_func_line(). */
20540 if (returning)
20541 eap->nextcmd = NULL;
20542 else if (eap->nextcmd == NULL) /* no argument */
20543 eap->nextcmd = check_nextcmd(arg);
20544
20545 if (eap->skip)
20546 --emsg_skip;
20547}
20548
20549/*
20550 * Return from a function. Possibly makes the return pending. Also called
20551 * for a pending return at the ":endtry" or after returning from an extra
20552 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020554 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 * FALSE when the return gets pending.
20556 */
20557 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020558do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 exarg_T *eap;
20560 int reanimate;
20561 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020562 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563{
20564 int idx;
20565 struct condstack *cstack = eap->cstack;
20566
20567 if (reanimate)
20568 /* Undo the return. */
20569 current_funccal->returned = FALSE;
20570
20571 /*
20572 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20573 * not in its finally clause (which then is to be executed next) is found.
20574 * In this case, make the ":return" pending for execution at the ":endtry".
20575 * Otherwise, return normally.
20576 */
20577 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20578 if (idx >= 0)
20579 {
20580 cstack->cs_pending[idx] = CSTP_RETURN;
20581
20582 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020583 /* A pending return again gets pending. "rettv" points to an
20584 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020586 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 else
20588 {
20589 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020592 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020594 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 {
20596 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020597 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020598 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 else
20600 EMSG(_(e_outofmem));
20601 }
20602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020603 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604
20605 if (reanimate)
20606 {
20607 /* The pending return value could be overwritten by a ":return"
20608 * without argument in a finally clause; reset the default
20609 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020610 current_funccal->rettv->v_type = VAR_NUMBER;
20611 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 }
20613 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 }
20616 else
20617 {
20618 current_funccal->returned = TRUE;
20619
20620 /* If the return is carried out now, store the return value. For
20621 * a return immediately after reanimation, the value is already
20622 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020623 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020625 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020626 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020628 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 }
20630 }
20631
20632 return idx < 0;
20633}
20634
20635/*
20636 * Free the variable with a pending return value.
20637 */
20638 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639discard_pending_return(rettv)
20640 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641{
Bram Moolenaar33570922005-01-25 22:26:29 +000020642 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643}
20644
20645/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020646 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 * is an allocated string. Used by report_pending() for verbose messages.
20648 */
20649 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020650get_return_cmd(rettv)
20651 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020653 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020654 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020655 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020657 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020658 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020659 if (s == NULL)
20660 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661
20662 STRCPY(IObuff, ":return ");
20663 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20664 if (STRLEN(s) + 8 >= IOSIZE)
20665 STRCPY(IObuff + IOSIZE - 4, "...");
20666 vim_free(tofree);
20667 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668}
20669
20670/*
20671 * Get next function line.
20672 * Called by do_cmdline() to get the next line.
20673 * Returns allocated string, or NULL for end of function.
20674 */
20675/* ARGSUSED */
20676 char_u *
20677get_func_line(c, cookie, indent)
20678 int c; /* not used */
20679 void *cookie;
20680 int indent; /* not used */
20681{
Bram Moolenaar33570922005-01-25 22:26:29 +000020682 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020683 ufunc_T *fp = fcp->func;
20684 char_u *retval;
20685 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686
20687 /* If breakpoints have been added/deleted need to check for it. */
20688 if (fcp->dbg_tick != debug_tick)
20689 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020690 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 sourcing_lnum);
20692 fcp->dbg_tick = debug_tick;
20693 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020694#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020695 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020696 func_line_end(cookie);
20697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698
Bram Moolenaar05159a02005-02-26 23:04:13 +000020699 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020700 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20701 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 retval = NULL;
20703 else
20704 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020705 /* Skip NULL lines (continuation lines). */
20706 while (fcp->linenr < gap->ga_len
20707 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20708 ++fcp->linenr;
20709 if (fcp->linenr >= gap->ga_len)
20710 retval = NULL;
20711 else
20712 {
20713 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20714 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020715#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020716 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020717 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020718#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 }
20721
20722 /* Did we encounter a breakpoint? */
20723 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20724 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020725 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020727 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 sourcing_lnum);
20729 fcp->dbg_tick = debug_tick;
20730 }
20731
20732 return retval;
20733}
20734
Bram Moolenaar05159a02005-02-26 23:04:13 +000020735#if defined(FEAT_PROFILE) || defined(PROTO)
20736/*
20737 * Called when starting to read a function line.
20738 * "sourcing_lnum" must be correct!
20739 * When skipping lines it may not actually be executed, but we won't find out
20740 * until later and we need to store the time now.
20741 */
20742 void
20743func_line_start(cookie)
20744 void *cookie;
20745{
20746 funccall_T *fcp = (funccall_T *)cookie;
20747 ufunc_T *fp = fcp->func;
20748
20749 if (fp->uf_profiling && sourcing_lnum >= 1
20750 && sourcing_lnum <= fp->uf_lines.ga_len)
20751 {
20752 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020753 /* Skip continuation lines. */
20754 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20755 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020756 fp->uf_tml_execed = FALSE;
20757 profile_start(&fp->uf_tml_start);
20758 profile_zero(&fp->uf_tml_children);
20759 profile_get_wait(&fp->uf_tml_wait);
20760 }
20761}
20762
20763/*
20764 * Called when actually executing a function line.
20765 */
20766 void
20767func_line_exec(cookie)
20768 void *cookie;
20769{
20770 funccall_T *fcp = (funccall_T *)cookie;
20771 ufunc_T *fp = fcp->func;
20772
20773 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20774 fp->uf_tml_execed = TRUE;
20775}
20776
20777/*
20778 * Called when done with a function line.
20779 */
20780 void
20781func_line_end(cookie)
20782 void *cookie;
20783{
20784 funccall_T *fcp = (funccall_T *)cookie;
20785 ufunc_T *fp = fcp->func;
20786
20787 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20788 {
20789 if (fp->uf_tml_execed)
20790 {
20791 ++fp->uf_tml_count[fp->uf_tml_idx];
20792 profile_end(&fp->uf_tml_start);
20793 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020794 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020795 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20796 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020797 }
20798 fp->uf_tml_idx = -1;
20799 }
20800}
20801#endif
20802
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803/*
20804 * Return TRUE if the currently active function should be ended, because a
20805 * return was encountered or an error occured. Used inside a ":while".
20806 */
20807 int
20808func_has_ended(cookie)
20809 void *cookie;
20810{
Bram Moolenaar33570922005-01-25 22:26:29 +000020811 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812
20813 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20814 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020815 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 || fcp->returned);
20817}
20818
20819/*
20820 * return TRUE if cookie indicates a function which "abort"s on errors.
20821 */
20822 int
20823func_has_abort(cookie)
20824 void *cookie;
20825{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020826 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827}
20828
20829#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20830typedef enum
20831{
20832 VAR_FLAVOUR_DEFAULT,
20833 VAR_FLAVOUR_SESSION,
20834 VAR_FLAVOUR_VIMINFO
20835} var_flavour_T;
20836
20837static var_flavour_T var_flavour __ARGS((char_u *varname));
20838
20839 static var_flavour_T
20840var_flavour(varname)
20841 char_u *varname;
20842{
20843 char_u *p = varname;
20844
20845 if (ASCII_ISUPPER(*p))
20846 {
20847 while (*(++p))
20848 if (ASCII_ISLOWER(*p))
20849 return VAR_FLAVOUR_SESSION;
20850 return VAR_FLAVOUR_VIMINFO;
20851 }
20852 else
20853 return VAR_FLAVOUR_DEFAULT;
20854}
20855#endif
20856
20857#if defined(FEAT_VIMINFO) || defined(PROTO)
20858/*
20859 * Restore global vars that start with a capital from the viminfo file
20860 */
20861 int
20862read_viminfo_varlist(virp, writing)
20863 vir_T *virp;
20864 int writing;
20865{
20866 char_u *tab;
20867 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869
20870 if (!writing && (find_viminfo_parameter('!') != NULL))
20871 {
20872 tab = vim_strchr(virp->vir_line + 1, '\t');
20873 if (tab != NULL)
20874 {
20875 *tab++ = '\0'; /* isolate the variable name */
20876 if (*tab == 'S') /* string var */
20877 is_string = TRUE;
20878
20879 tab = vim_strchr(tab, '\t');
20880 if (tab != NULL)
20881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 if (is_string)
20883 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020884 tv.v_type = VAR_STRING;
20885 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 }
20888 else
20889 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020890 tv.v_type = VAR_NUMBER;
20891 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020893 set_var(virp->vir_line + 1, &tv, FALSE);
20894 if (is_string)
20895 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 }
20897 }
20898 }
20899
20900 return viminfo_readline(virp);
20901}
20902
20903/*
20904 * Write global vars that start with a capital to the viminfo file
20905 */
20906 void
20907write_viminfo_varlist(fp)
20908 FILE *fp;
20909{
Bram Moolenaar33570922005-01-25 22:26:29 +000020910 hashitem_T *hi;
20911 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020912 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020914 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917
20918 if (find_viminfo_parameter('!') == NULL)
20919 return;
20920
20921 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020922
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020923 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020924 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020926 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020928 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 this_var = HI2DI(hi);
20930 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020931 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020932 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020933 {
20934 case VAR_STRING: s = "STR"; break;
20935 case VAR_NUMBER: s = "NUM"; break;
20936 default: continue;
20937 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020939 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020940 if (p != NULL)
20941 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020942 vim_free(tofree);
20943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 }
20945 }
20946}
20947#endif
20948
20949#if defined(FEAT_SESSION) || defined(PROTO)
20950 int
20951store_session_globals(fd)
20952 FILE *fd;
20953{
Bram Moolenaar33570922005-01-25 22:26:29 +000020954 hashitem_T *hi;
20955 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020956 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 char_u *p, *t;
20958
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020959 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020960 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020962 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020964 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020965 this_var = HI2DI(hi);
20966 if ((this_var->di_tv.v_type == VAR_NUMBER
20967 || this_var->di_tv.v_type == VAR_STRING)
20968 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020969 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020970 /* Escape special characters with a backslash. Turn a LF and
20971 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020972 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020973 (char_u *)"\\\"\n\r");
20974 if (p == NULL) /* out of memory */
20975 break;
20976 for (t = p; *t != NUL; ++t)
20977 if (*t == '\n')
20978 *t = 'n';
20979 else if (*t == '\r')
20980 *t = 'r';
20981 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 this_var->di_key,
20983 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20984 : ' ',
20985 p,
20986 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20987 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020988 || put_eol(fd) == FAIL)
20989 {
20990 vim_free(p);
20991 return FAIL;
20992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 vim_free(p);
20994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 }
20996 }
20997 return OK;
20998}
20999#endif
21000
Bram Moolenaar661b1822005-07-28 22:36:45 +000021001/*
21002 * Display script name where an item was last set.
21003 * Should only be invoked when 'verbose' is non-zero.
21004 */
21005 void
21006last_set_msg(scriptID)
21007 scid_T scriptID;
21008{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021009 char_u *p;
21010
Bram Moolenaar661b1822005-07-28 22:36:45 +000021011 if (scriptID != 0)
21012 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021013 p = home_replace_save(NULL, get_scriptname(scriptID));
21014 if (p != NULL)
21015 {
21016 verbose_enter();
21017 MSG_PUTS(_("\n\tLast set from "));
21018 MSG_PUTS(p);
21019 vim_free(p);
21020 verbose_leave();
21021 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021022 }
21023}
21024
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025#endif /* FEAT_EVAL */
21026
21027#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21028
21029
21030#ifdef WIN3264
21031/*
21032 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21033 */
21034static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21035static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21036static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21037
21038/*
21039 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021040 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 */
21042 static int
21043get_short_pathname(fnamep, bufp, fnamelen)
21044 char_u **fnamep;
21045 char_u **bufp;
21046 int *fnamelen;
21047{
21048 int l,len;
21049 char_u *newbuf;
21050
21051 len = *fnamelen;
21052
21053 l = GetShortPathName(*fnamep, *fnamep, len);
21054 if (l > len - 1)
21055 {
21056 /* If that doesn't work (not enough space), then save the string
21057 * and try again with a new buffer big enough
21058 */
21059 newbuf = vim_strnsave(*fnamep, l);
21060 if (newbuf == NULL)
21061 return 0;
21062
21063 vim_free(*bufp);
21064 *fnamep = *bufp = newbuf;
21065
21066 l = GetShortPathName(*fnamep,*fnamep,l+1);
21067
21068 /* Really should always succeed, as the buffer is big enough */
21069 }
21070
21071 *fnamelen = l;
21072 return 1;
21073}
21074
21075/*
21076 * Create a short path name. Returns the length of the buffer it needs.
21077 * Doesn't copy over the end of the buffer passed in.
21078 */
21079 static int
21080shortpath_for_invalid_fname(fname, bufp, fnamelen)
21081 char_u **fname;
21082 char_u **bufp;
21083 int *fnamelen;
21084{
21085 char_u *s, *p, *pbuf2, *pbuf3;
21086 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021087 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088
21089 /* Make a copy */
21090 len2 = *fnamelen;
21091 pbuf2 = vim_strnsave(*fname, len2);
21092 pbuf3 = NULL;
21093
21094 s = pbuf2 + len2 - 1; /* Find the end */
21095 slen = 1;
21096 plen = len2;
21097
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021098 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 {
21100 --s;
21101 ++slen;
21102 --plen;
21103 }
21104
21105 do
21106 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021107 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021108 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 {
21110 --s;
21111 ++slen;
21112 --plen;
21113 }
21114 if (s <= pbuf2)
21115 break;
21116
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021117 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 ch = *s;
21119 *s = 0; /* get_short_pathname requires a null-terminated string */
21120
21121 /* Try it in situ */
21122 p = pbuf2;
21123 if (!get_short_pathname(&p, &pbuf3, &plen))
21124 {
21125 vim_free(pbuf2);
21126 return -1;
21127 }
21128 *s = ch; /* Preserve the string */
21129 } while (plen == 0);
21130
21131 if (plen > 0)
21132 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021133 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 *fnamelen = len = plen + slen;
21135 vim_free(*bufp);
21136 if (len > len2)
21137 {
21138 /* If there's not enough space in the currently allocated string,
21139 * then copy it to a buffer big enough.
21140 */
21141 *fname= *bufp = vim_strnsave(p, len);
21142 if (*fname == NULL)
21143 return -1;
21144 }
21145 else
21146 {
21147 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21148 *fname = *bufp = pbuf2;
21149 if (p != pbuf2)
21150 strncpy(*fname, p, plen);
21151 pbuf2 = NULL;
21152 }
21153 /* Concat the next bit */
21154 strncpy(*fname + plen, s, slen);
21155 (*fname)[len] = '\0';
21156 }
21157 vim_free(pbuf3);
21158 vim_free(pbuf2);
21159 return 0;
21160}
21161
21162/*
21163 * Get a pathname for a partial path.
21164 */
21165 static int
21166shortpath_for_partial(fnamep, bufp, fnamelen)
21167 char_u **fnamep;
21168 char_u **bufp;
21169 int *fnamelen;
21170{
21171 int sepcount, len, tflen;
21172 char_u *p;
21173 char_u *pbuf, *tfname;
21174 int hasTilde;
21175
21176 /* Count up the path seperators from the RHS.. so we know which part
21177 * of the path to return.
21178 */
21179 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021180 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 if (vim_ispathsep(*p))
21182 ++sepcount;
21183
21184 /* Need full path first (use expand_env() to remove a "~/") */
21185 hasTilde = (**fnamep == '~');
21186 if (hasTilde)
21187 pbuf = tfname = expand_env_save(*fnamep);
21188 else
21189 pbuf = tfname = FullName_save(*fnamep, FALSE);
21190
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021191 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192
21193 if (!get_short_pathname(&tfname, &pbuf, &len))
21194 return -1;
21195
21196 if (len == 0)
21197 {
21198 /* Don't have a valid filename, so shorten the rest of the
21199 * path if we can. This CAN give us invalid 8.3 filenames, but
21200 * there's not a lot of point in guessing what it might be.
21201 */
21202 len = tflen;
21203 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21204 return -1;
21205 }
21206
21207 /* Count the paths backward to find the beginning of the desired string. */
21208 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021209 {
21210#ifdef FEAT_MBYTE
21211 if (has_mbyte)
21212 p -= mb_head_off(tfname, p);
21213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 if (vim_ispathsep(*p))
21215 {
21216 if (sepcount == 0 || (hasTilde && sepcount == 1))
21217 break;
21218 else
21219 sepcount --;
21220 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 if (hasTilde)
21223 {
21224 --p;
21225 if (p >= tfname)
21226 *p = '~';
21227 else
21228 return -1;
21229 }
21230 else
21231 ++p;
21232
21233 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21234 vim_free(*bufp);
21235 *fnamelen = (int)STRLEN(p);
21236 *bufp = pbuf;
21237 *fnamep = p;
21238
21239 return 0;
21240}
21241#endif /* WIN3264 */
21242
21243/*
21244 * Adjust a filename, according to a string of modifiers.
21245 * *fnamep must be NUL terminated when called. When returning, the length is
21246 * determined by *fnamelen.
21247 * Returns valid flags.
21248 * When there is an error, *fnamep is set to NULL.
21249 */
21250 int
21251modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21252 char_u *src; /* string with modifiers */
21253 int *usedlen; /* characters after src that are used */
21254 char_u **fnamep; /* file name so far */
21255 char_u **bufp; /* buffer for allocated file name or NULL */
21256 int *fnamelen; /* length of fnamep */
21257{
21258 int valid = 0;
21259 char_u *tail;
21260 char_u *s, *p, *pbuf;
21261 char_u dirname[MAXPATHL];
21262 int c;
21263 int has_fullname = 0;
21264#ifdef WIN3264
21265 int has_shortname = 0;
21266#endif
21267
21268repeat:
21269 /* ":p" - full path/file_name */
21270 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21271 {
21272 has_fullname = 1;
21273
21274 valid |= VALID_PATH;
21275 *usedlen += 2;
21276
21277 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21278 if ((*fnamep)[0] == '~'
21279#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21280 && ((*fnamep)[1] == '/'
21281# ifdef BACKSLASH_IN_FILENAME
21282 || (*fnamep)[1] == '\\'
21283# endif
21284 || (*fnamep)[1] == NUL)
21285
21286#endif
21287 )
21288 {
21289 *fnamep = expand_env_save(*fnamep);
21290 vim_free(*bufp); /* free any allocated file name */
21291 *bufp = *fnamep;
21292 if (*fnamep == NULL)
21293 return -1;
21294 }
21295
21296 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021297 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 {
21299 if (vim_ispathsep(*p)
21300 && p[1] == '.'
21301 && (p[2] == NUL
21302 || vim_ispathsep(p[2])
21303 || (p[2] == '.'
21304 && (p[3] == NUL || vim_ispathsep(p[3])))))
21305 break;
21306 }
21307
21308 /* FullName_save() is slow, don't use it when not needed. */
21309 if (*p != NUL || !vim_isAbsName(*fnamep))
21310 {
21311 *fnamep = FullName_save(*fnamep, *p != NUL);
21312 vim_free(*bufp); /* free any allocated file name */
21313 *bufp = *fnamep;
21314 if (*fnamep == NULL)
21315 return -1;
21316 }
21317
21318 /* Append a path separator to a directory. */
21319 if (mch_isdir(*fnamep))
21320 {
21321 /* Make room for one or two extra characters. */
21322 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21323 vim_free(*bufp); /* free any allocated file name */
21324 *bufp = *fnamep;
21325 if (*fnamep == NULL)
21326 return -1;
21327 add_pathsep(*fnamep);
21328 }
21329 }
21330
21331 /* ":." - path relative to the current directory */
21332 /* ":~" - path relative to the home directory */
21333 /* ":8" - shortname path - postponed till after */
21334 while (src[*usedlen] == ':'
21335 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21336 {
21337 *usedlen += 2;
21338 if (c == '8')
21339 {
21340#ifdef WIN3264
21341 has_shortname = 1; /* Postpone this. */
21342#endif
21343 continue;
21344 }
21345 pbuf = NULL;
21346 /* Need full path first (use expand_env() to remove a "~/") */
21347 if (!has_fullname)
21348 {
21349 if (c == '.' && **fnamep == '~')
21350 p = pbuf = expand_env_save(*fnamep);
21351 else
21352 p = pbuf = FullName_save(*fnamep, FALSE);
21353 }
21354 else
21355 p = *fnamep;
21356
21357 has_fullname = 0;
21358
21359 if (p != NULL)
21360 {
21361 if (c == '.')
21362 {
21363 mch_dirname(dirname, MAXPATHL);
21364 s = shorten_fname(p, dirname);
21365 if (s != NULL)
21366 {
21367 *fnamep = s;
21368 if (pbuf != NULL)
21369 {
21370 vim_free(*bufp); /* free any allocated file name */
21371 *bufp = pbuf;
21372 pbuf = NULL;
21373 }
21374 }
21375 }
21376 else
21377 {
21378 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21379 /* Only replace it when it starts with '~' */
21380 if (*dirname == '~')
21381 {
21382 s = vim_strsave(dirname);
21383 if (s != NULL)
21384 {
21385 *fnamep = s;
21386 vim_free(*bufp);
21387 *bufp = s;
21388 }
21389 }
21390 }
21391 vim_free(pbuf);
21392 }
21393 }
21394
21395 tail = gettail(*fnamep);
21396 *fnamelen = (int)STRLEN(*fnamep);
21397
21398 /* ":h" - head, remove "/file_name", can be repeated */
21399 /* Don't remove the first "/" or "c:\" */
21400 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21401 {
21402 valid |= VALID_HEAD;
21403 *usedlen += 2;
21404 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021405 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021406 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 *fnamelen = (int)(tail - *fnamep);
21408#ifdef VMS
21409 if (*fnamelen > 0)
21410 *fnamelen += 1; /* the path separator is part of the path */
21411#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021412 if (*fnamelen == 0)
21413 {
21414 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21415 p = vim_strsave((char_u *)".");
21416 if (p == NULL)
21417 return -1;
21418 vim_free(*bufp);
21419 *bufp = *fnamep = tail = p;
21420 *fnamelen = 1;
21421 }
21422 else
21423 {
21424 while (tail > s && !after_pathsep(s, tail))
21425 mb_ptr_back(*fnamep, tail);
21426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 }
21428
21429 /* ":8" - shortname */
21430 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21431 {
21432 *usedlen += 2;
21433#ifdef WIN3264
21434 has_shortname = 1;
21435#endif
21436 }
21437
21438#ifdef WIN3264
21439 /* Check shortname after we have done 'heads' and before we do 'tails'
21440 */
21441 if (has_shortname)
21442 {
21443 pbuf = NULL;
21444 /* Copy the string if it is shortened by :h */
21445 if (*fnamelen < (int)STRLEN(*fnamep))
21446 {
21447 p = vim_strnsave(*fnamep, *fnamelen);
21448 if (p == 0)
21449 return -1;
21450 vim_free(*bufp);
21451 *bufp = *fnamep = p;
21452 }
21453
21454 /* Split into two implementations - makes it easier. First is where
21455 * there isn't a full name already, second is where there is.
21456 */
21457 if (!has_fullname && !vim_isAbsName(*fnamep))
21458 {
21459 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21460 return -1;
21461 }
21462 else
21463 {
21464 int l;
21465
21466 /* Simple case, already have the full-name
21467 * Nearly always shorter, so try first time. */
21468 l = *fnamelen;
21469 if (!get_short_pathname(fnamep, bufp, &l))
21470 return -1;
21471
21472 if (l == 0)
21473 {
21474 /* Couldn't find the filename.. search the paths.
21475 */
21476 l = *fnamelen;
21477 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21478 return -1;
21479 }
21480 *fnamelen = l;
21481 }
21482 }
21483#endif /* WIN3264 */
21484
21485 /* ":t" - tail, just the basename */
21486 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21487 {
21488 *usedlen += 2;
21489 *fnamelen -= (int)(tail - *fnamep);
21490 *fnamep = tail;
21491 }
21492
21493 /* ":e" - extension, can be repeated */
21494 /* ":r" - root, without extension, can be repeated */
21495 while (src[*usedlen] == ':'
21496 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21497 {
21498 /* find a '.' in the tail:
21499 * - for second :e: before the current fname
21500 * - otherwise: The last '.'
21501 */
21502 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21503 s = *fnamep - 2;
21504 else
21505 s = *fnamep + *fnamelen - 1;
21506 for ( ; s > tail; --s)
21507 if (s[0] == '.')
21508 break;
21509 if (src[*usedlen + 1] == 'e') /* :e */
21510 {
21511 if (s > tail)
21512 {
21513 *fnamelen += (int)(*fnamep - (s + 1));
21514 *fnamep = s + 1;
21515#ifdef VMS
21516 /* cut version from the extension */
21517 s = *fnamep + *fnamelen - 1;
21518 for ( ; s > *fnamep; --s)
21519 if (s[0] == ';')
21520 break;
21521 if (s > *fnamep)
21522 *fnamelen = s - *fnamep;
21523#endif
21524 }
21525 else if (*fnamep <= tail)
21526 *fnamelen = 0;
21527 }
21528 else /* :r */
21529 {
21530 if (s > tail) /* remove one extension */
21531 *fnamelen = (int)(s - *fnamep);
21532 }
21533 *usedlen += 2;
21534 }
21535
21536 /* ":s?pat?foo?" - substitute */
21537 /* ":gs?pat?foo?" - global substitute */
21538 if (src[*usedlen] == ':'
21539 && (src[*usedlen + 1] == 's'
21540 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21541 {
21542 char_u *str;
21543 char_u *pat;
21544 char_u *sub;
21545 int sep;
21546 char_u *flags;
21547 int didit = FALSE;
21548
21549 flags = (char_u *)"";
21550 s = src + *usedlen + 2;
21551 if (src[*usedlen + 1] == 'g')
21552 {
21553 flags = (char_u *)"g";
21554 ++s;
21555 }
21556
21557 sep = *s++;
21558 if (sep)
21559 {
21560 /* find end of pattern */
21561 p = vim_strchr(s, sep);
21562 if (p != NULL)
21563 {
21564 pat = vim_strnsave(s, (int)(p - s));
21565 if (pat != NULL)
21566 {
21567 s = p + 1;
21568 /* find end of substitution */
21569 p = vim_strchr(s, sep);
21570 if (p != NULL)
21571 {
21572 sub = vim_strnsave(s, (int)(p - s));
21573 str = vim_strnsave(*fnamep, *fnamelen);
21574 if (sub != NULL && str != NULL)
21575 {
21576 *usedlen = (int)(p + 1 - src);
21577 s = do_string_sub(str, pat, sub, flags);
21578 if (s != NULL)
21579 {
21580 *fnamep = s;
21581 *fnamelen = (int)STRLEN(s);
21582 vim_free(*bufp);
21583 *bufp = s;
21584 didit = TRUE;
21585 }
21586 }
21587 vim_free(sub);
21588 vim_free(str);
21589 }
21590 vim_free(pat);
21591 }
21592 }
21593 /* after using ":s", repeat all the modifiers */
21594 if (didit)
21595 goto repeat;
21596 }
21597 }
21598
21599 return valid;
21600}
21601
21602/*
21603 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21604 * "flags" can be "g" to do a global substitute.
21605 * Returns an allocated string, NULL for error.
21606 */
21607 char_u *
21608do_string_sub(str, pat, sub, flags)
21609 char_u *str;
21610 char_u *pat;
21611 char_u *sub;
21612 char_u *flags;
21613{
21614 int sublen;
21615 regmatch_T regmatch;
21616 int i;
21617 int do_all;
21618 char_u *tail;
21619 garray_T ga;
21620 char_u *ret;
21621 char_u *save_cpo;
21622
21623 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21624 save_cpo = p_cpo;
21625 p_cpo = (char_u *)"";
21626
21627 ga_init2(&ga, 1, 200);
21628
21629 do_all = (flags[0] == 'g');
21630
21631 regmatch.rm_ic = p_ic;
21632 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21633 if (regmatch.regprog != NULL)
21634 {
21635 tail = str;
21636 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21637 {
21638 /*
21639 * Get some space for a temporary buffer to do the substitution
21640 * into. It will contain:
21641 * - The text up to where the match is.
21642 * - The substituted text.
21643 * - The text after the match.
21644 */
21645 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21646 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21647 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21648 {
21649 ga_clear(&ga);
21650 break;
21651 }
21652
21653 /* copy the text up to where the match is */
21654 i = (int)(regmatch.startp[0] - tail);
21655 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21656 /* add the substituted text */
21657 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21658 + ga.ga_len + i, TRUE, TRUE, FALSE);
21659 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 /* avoid getting stuck on a match with an empty string */
21661 if (tail == regmatch.endp[0])
21662 {
21663 if (*tail == NUL)
21664 break;
21665 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21666 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 }
21668 else
21669 {
21670 tail = regmatch.endp[0];
21671 if (*tail == NUL)
21672 break;
21673 }
21674 if (!do_all)
21675 break;
21676 }
21677
21678 if (ga.ga_data != NULL)
21679 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21680
21681 vim_free(regmatch.regprog);
21682 }
21683
21684 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21685 ga_clear(&ga);
21686 p_cpo = save_cpo;
21687
21688 return ret;
21689}
21690
21691#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */